morepath的内容安全策略
more.content-securit的Python项目详细描述
用法
使用默认内容安全策略保护所有视图:
frommorepathimportAppfrommore.content_securityimportContentSecurityAppfrommore.content_securityimportContentSecurityPolicyfrommore.content_securityimportSELFclassMyApp(App,ContentSecurityApp):pass@MyApp.setting('content_security_policy','default')defdefault_policy():returnContentSecurityPolicy(default_src={SELF},script_src={SELF,'https://analytics.example.org'})
要扩展模型默认视图的默认策略:
@MyApp.view(model=Document)defview_document(self,request):# the actual default policy is not modified here!request.content_security_policy.script_src.add('https://cdnjs.com')....
我们还可以使用完全不同的策略:
@MyApp.view(model=Document)defview_document(self,request):request.content_security_policy=ContentSecurityPolicy()
此外,我们可以在内联脚本/样式表中使用nonce。那些意志 自动添加到“script src”、“style src”指令:
@MyApp.html(model=Document)defview_document(self,request):return""" <html> ... <script nonce="{}">...</script> </html> """.format(request.content_security_policy_nonce('script'))
请注意,我们对nonce使用自定义请求类。如果你有自己的, 您需要将其扩展如下:
frommorepath.requestimportRequestfrommore.content_securityimportContentSecurityRequestclassCustomRequest(Request,ContentSecurityRequest):passclassMyApp(App,ContentSecurityApp):request_class=CustomRequest
若要仅使用“仅内容安全策略报告”标题,请使用以下内容:
@MyApp.setting('content_security_policy','default')defdefault_policy():returnContentSecurityPolicy(report_only=True,default_src={SELF})
运行测试
安装tox并运行它:
pip install tox tox
将测试限制为特定的python版本:
tox -e py27
许可证
more.content_security在经过修订的bsd许可证下发布
更改日志
0.2.0(2018-02-02)
- 添加重写策略应用函数的功能。 [资源]
- 添加缺少的不安全评估常数。 [资源]
0.1.0(2018-02-01)
- 初次发布。 [资源]