【问题标题】:Is there any possibility to capture CSP errors through Javascript是否有可能通过 Javascript 捕获 CSP 错误
【发布时间】:2015-10-29 19:49:09
【问题描述】:

我在我的网站标题中添加了 Content-Security-Policy,我正在尝试使用 Javascript 记录客户端发生的 Content-Security-Policy 错误。

您能否告诉我任何通过 Javascript 捕获 CSP 错误的可能性

【问题讨论】:

  • 在客户端 - 不可能。否则this 可能会有所帮助

标签: javascript error-logging content-security-policy


【解决方案1】:

SecurityPolicyViolationEvent DOM 事件已添加到 Content Security Policy Level 2。来自Changes from Level 1 部分:

SecurityPolicyViolationEvent 因违规而被解雇,因为 在§6.3 Firing Violation Events中描述。

但是,浏览器支持有限(请参阅http://caniuse.com/#feat=contentsecuritypolicy2)

【讨论】:

    【解决方案2】:

    您的标头可以包含report-uri,它指定要向其报告 CSP 违规的端点。您可以自己收集这些信息,也可以将它们发送到错误报告服务,例如 Sentry。

    def middleware(request, response):
        response['Content-Security-Policy'] = \
            "default-src *; " \
            "script-src 'self' 'unsafe-eval' 'unsafe-inline' cdn.example.com cdn.ravenjs.com; " \
            "style-src 'self' 'unsafe-inline' cdn.example.com; " \
            "img-src * data:; " \
            "report-uri https://app.getsentry.com/api/54785/csp-report/?sentry_key=SENTRY_KEY"
        return response
    

    【讨论】:

      猜你喜欢
      • 2012-07-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-19
      • 2017-03-24
      • 2011-06-18
      • 1970-01-01
      相关资源
      最近更新 更多