帮助保护django项目的工具集合。

django-securit的Python项目详细描述


Django安全

Build Status

这个包提供了许多模型、视图、中间件和表单,以促进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
ClearSiteDataMiddlewareSend Clear-Site-Data header in HTTP response for any page that has been whitelisted. Recommended,. Required.
ContentNoSniffDisable possibly insecure autodetection of MIME types in browsers. Recommended.None.
ContentSecurityPolicyMiddlewareSend Content Security Policy (CSP) header in HTTP response. Recommended, requires careful tuning. Required.
DoNotTrackMiddlewareRead user browser's DoNotTrack preference and pass it to application. Recommended, requires implementation in views and templates. None.
LoginRequiredMiddlewareRequires a user to be authenticated to view any page on the site that hasn't been white listed. Required.
MandatoryPasswordChangeMiddlewareRedirects any request from an authenticated user to the password change form if that user's password has expired. Required.
NoConfidentialCachingMiddlewareAdds No-Cache and No-Store headers to confidential pages. Required.
P3PPolicyMiddlewareAdds the HTTP header attribute specifying compact P3P policy. Required.
SessionExpiryPolicyMiddlewareExpire sessions on browser close, and on expiry times stored in the cookie itself. Required.
StrictTransportSecurityMiddlewareEnforce SSL/TLS connection and disable plaintext fall-back. Recommended for SSL/TLS sites. Optional.
XFrameOptionsMiddlewareDisable framing of the website, mitigating Clickjacking attacks. Recommended.Optional.
XssProtectMiddlewareEnforce browser's Cross Site Scripting protection. Recommended.None.

视图

csp_report

允许接收浏览器响应发送的内容安全策略冲突报告的视图 到由“contentsecuritypolicymiddleware”设置的csp头。只有当长期、连续的顾客服务提供商报告 需要分析。有一次csp设置CspBuilder要简单得多。

此视图可以配置为记录接收到的报告或将其存储在数据库中。 有关详细信息,请参见documentation

require_ajax

确保视图处理的请求是ajax请求的视图装饰器。示例用法:

@require_ajax
def myview(request):
    ...

型号

CspReport

内容安全策略冲突报告对象。只有在使用ContentSecurityPolicyMiddlewarecsp_report视图时才有意义。 使用此模型,可以在django管理站点中分析报告。

PasswordExpiry

将密码到期日期与用户关联。

日志记录

所有django-security模块都向security设施发送重要日志消息。应用程序应该配置一个处理程序来接收它们:

LOGGING = {
    ...
    'loggers': {
        'security': {
            'handlers': ['console',],
            'level': 'INFO',
            'propagate': False,
            'formatter': 'verbose',
        },
    ...

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
在java中,如何使用lambda表达式按月计算排名?   java如何更改createTempFile中的默认名称?   java无法找到或加载主类net。Fabrimc。德夫朗。主织物   java如何将应用程序绑定到端口161?   使用Spring Boot运行基于Flyway Java的回调   java如何将源代码库添加到IntelliJ(例如来自Github)?   用于MacOSX上应用程序打包的DMG后台java自定义下拉图标   Java泛型和返回类型   java Docker volume mapping+windows=难以置信的慢?   java gwt gxt文本字段。强制无效   试图使用Map<Class,Function<T,R>>替换Java8中的instanceOf,无法放入映射   java Spring SpEL错误   java如何格式化输出值?   java异步HTTP请求,排队请求   带有Kafka、ClassCastException的java Avro模式?   io我们是否需要在java中为Windows和linux提供单独的文件路径   哈姆克雷斯特爪哇。util。IllegalFormatFlagsException:标志=“”   java组合框所选项目   java如何停止程序以等待用户操作?