帮助保护django项目的工具集合。
django-securit的Python项目详细描述
Django安全
这个包提供了许多模型、视图、中间件和表单,以促进django应用程序的安全强化。
完整文档
自动生成的django-security
文档在阅读文档时可用:
要求
- python=2.7
- django>;=1.8
对于django<;1.8,使用django security==0.9.4。
注意:对于0.10.0之前的版本,datetime
对象将被添加到会话中,并且需要django的pickle序列化程序进行(反)序列化。现在已经更改了,因此这些datetime
的字符串将被存储。如果出于这个原因仍在使用pickleSerializer,我们建议切换到django的默认jsonserializer(自django 1.6以来的默认值)以获得更好的安全性。
安装
从python包存储库安装:
pip install django-security
如果您喜欢最新的开发版本,请从 django-securitygithub上的存储库:
git clone https://github.com/sdelements/django-security.git
cd django-security
sudo python setup.py install
添加到django应用程序的settings.py
文件:
INSTALLED_APPS = (
...
'security',
...
)
在django 1.10之前,中间件模块可以添加到设置文件中的MIDDLEWARE_CLASSES
列表中:
MIDDLEWARE_CLASSES = (
...
'security.middleware.DoNotTrackMiddleware',
'security.middleware.ContentNoSniff',
'security.middleware.XssProtectMiddleware',
'security.middleware.XFrameOptionsMiddleware',
)
django 1.10之后,可以将中间件模块添加到设置文件中的MIDDLEWARE
列表中:
MIDDLEWARE = (
...
'security.middleware.DoNotTrackMiddleware',
'security.middleware.ContentNoSniff',
'security.middleware.XssProtectMiddleware',
'security.middleware.XFrameOptionsMiddleware',
)
与上面列出的模块不同,其他一些模块需要配置设置, 完全在django-security documentation中描述。 简要说明如下。
中间件
提供的中间件模块将修改web应用程序的输出和输入,在大多数情况下不需要 或最小配置。
Middleware | Description | Configuration |
---|---|---|
ClearSiteDataMiddleware | Send Clear-Site-Data header in HTTP response for any page that has been whitelisted. Recommended,. | Required. |
ContentNoSniff | Disable possibly insecure autodetection of MIME types in browsers. Recommended. | None. |
ContentSecurityPolicyMiddleware | Send Content Security Policy (CSP) header in HTTP response. Recommended, requires careful tuning. | Required. |
DoNotTrackMiddleware | Read user browser's DoNotTrack preference and pass it to application. Recommended, requires implementation in views and templates. | None. |
LoginRequiredMiddleware | Requires a user to be authenticated to view any page on the site that hasn't been white listed. | Required. |
MandatoryPasswordChangeMiddleware | Redirects any request from an authenticated user to the password change form if that user's password has expired. | Required. |
NoConfidentialCachingMiddleware | Adds No-Cache and No-Store headers to confidential pages. | Required. |
P3PPolicyMiddleware | Adds the HTTP header attribute specifying compact P3P policy. | Required. |
SessionExpiryPolicyMiddleware | Expire sessions on browser close, and on expiry times stored in the cookie itself. | Required. |
StrictTransportSecurityMiddleware | Enforce SSL/TLS connection and disable plaintext fall-back. Recommended for SSL/TLS sites. | Optional. |
XFrameOptionsMiddleware | Disable framing of the website, mitigating Clickjacking attacks. Recommended. | Optional. |
XssProtectMiddleware | Enforce browser's Cross Site Scripting protection. Recommended. | None. |
视图
csp_report
允许接收浏览器响应发送的内容安全策略冲突报告的视图 到由“contentsecuritypolicymiddleware”设置的csp头。只有当长期、连续的顾客服务提供商报告 需要分析。有一次csp设置CspBuilder要简单得多。
此视图可以配置为记录接收到的报告或将其存储在数据库中。 有关详细信息,请参见documentation。
require_ajax
确保视图处理的请求是ajax请求的视图装饰器。示例用法:
@require_ajax
def myview(request):
...
型号
CspReport
内容安全策略冲突报告对象。只有在使用ContentSecurityPolicyMiddleware
和csp_report
视图时才有意义。
使用此模型,可以在django管理站点中分析报告。
PasswordExpiry
将密码到期日期与用户关联。
日志记录
所有django-security
模块都向security
设施发送重要日志消息。应用程序应该配置一个处理程序来接收它们:
LOGGING = {
...
'loggers': {
'security': {
'handlers': ['console',],
'level': 'INFO',
'propagate': False,
'formatter': 'verbose',
},
...