在错误页面上隐藏 Django 设置值(对于 Celery)

问题描述 投票:0回答:1

我们在 Django 项目中使用 Celery,并将一个

CELERY_RESULT_DBURI
值(它携带数据库的密码)放入我们的
settings.py
文件中。显然,我觉得在每个回溯中使用明文密码不太安全。

我知道过滤错误报告,但我现在能想到的唯一解决方案是通过复制

SafeExceptionReporterFilter
方法并以某种方式将
get_traceback_frame_variables()
猴子修补到其中来重写
CELERY_RESULT_DBURI
类。你们中有人知道更好的解决方案吗?您是如何解决数据库密码泄露的问题的?

django celery django-celery django-settings traceback
1个回答
0
投票

我知道这已经很旧了,但我想发布一个答案,因为我在研究类似问题时遇到了这个问题。 OP 关于过滤错误报告的做法是正确的。我希望这对某人有帮助!

import re
from django.views.debug import SafeExceptionReporterFilter

# These are the default patterns Django will match as of 4.2
HIDDEN_DEFAULT = 'API|TOKEN|KEY|SECRET|PASS|SIGNATURE|HTTP_COOKIE|'
EXTRA = 'CELERY|SOME_OTHER_KEY'

class CustomExceptionReporterFilter(SafeExceptionReporterFilter):

    def __init__(self):
        super().__init__()
        self.hidden_settings = re.compile(
            pattern=f'{HIDDEN_DEFAULT}{EXTRA}',
            flags=re.IGNORECASE
        )

然后将其添加到您的settings.py 文件中:

DEFAULT_EXCEPTION_REPORTER_FILTER = "path.to.module.CustomExceptionReporterFilter"

以下是 Django 文档中有关自定义错误报告的一些附加信息:https://docs.djangoproject.com/en/4.2/howto/error-reporting/#custom-error-reports

© www.soinside.com 2019 - 2024. All rights reserved.