如何使用 Django 的内容安全策略¶
基础配置¶
要在Django项目中启用内容安全策略(CSP):
将CSP中间件添加到
中间件设置中:MIDDLEWARE = [ # ... "django.middleware.csp.ContentSecurityPolicyMiddleware", # ... ]
在settings.py文件中,使用
SECURE_CSP或SECURE_CSP_REPORT_ONLY(或两者都使用)来配置CSP策略。CSP设置文档 提供了关于这两者之间差异的更多详细信息:from django.utils.csp import CSP # To enforce a CSP policy: SECURE_CSP = { "default-src": [CSP.SELF], # Add more directives to be enforced. } # Or for report-only mode: SECURE_CSP_REPORT_ONLY = { "default-src": [CSP.SELF], # Add more directives as needed. "report-uri": "/path/to/reports-endpoint/", }
Nonce配置¶
要在CSP策略中使用一次性随机数,即Nonce,除了基本配置外,你还需要:
在CSP设置中包含
NONCE占位符值。这仅适用于script-src或style-src指令:from django.utils.csp import CSP SECURE_CSP = { "default-src": [CSP.SELF], # Allow self-hosted scripts and script tags with matching `nonce` attr. "script-src": [CSP.SELF, CSP.NONCE], # Example of the less secure 'unsafe-inline' option. "style-src": [CSP.SELF, CSP.UNSAFE_INLINE], }
将
csp()上下文处理器添加到你的TEMPLATES设置中。这样,生成的随机数就会作为``csp_nonce``上下文变量在 Django 模板中可用:TEMPLATES = [ { "BACKEND": "django.template.backends.django.DjangoTemplates", "OPTIONS": { "context_processors": [ # ... "django.template.context_processors.csp", ], }, }, ]
在你的模板中,使用
csp_nonce上下文变量,将nonce属性添加到相关的<style>或<script>内联标签中:<style nonce="{{ csp_nonce }}"> /* These inline styles will be allowed. */ </style> <script nonce="{{ csp_nonce }}"> // This inline JavaScript will be allowed. </script>
缓存与Nonce复用
当模板中使用nonce时,ContentSecurityPolicyMiddleware 会自动处理生成唯一的nonce,并将相应的 nonce-<value> 源表达式插入到 Content-Security-Policy (或 Content-Security-Policy-Report-Only)头部中。
为确保行为正确,请确保HTML和头部(header)都在同一请求中生成,而不是从缓存中获取。有关实现细节和重要的缓存注意事项,请参阅 Nonce usage 的参考文档。