未触发内容安全策略违反事件

问题描述

我正在尝试为我的应用程序设置CSP标头。我有意从font-src删除了CDN 和style-src指令,以便触发违规。我可以在Chrome Dev Tools控制台上看到违规情况,但是未触发对标头中指定的csp-endpoint的请求。我尝试通过设置report-uri属性来尝试,但这也没有帮助。我正在Chrome 84上对此进行测试,该Chrome应该支持report-to指令。我在这里想念什么吗?

Content-Security-Policy-Report-Only: default-src 'none'; script-src 'self' 'unsafe-inline' 'unsafe-eval' www.googletagmanager.com www.google-analytics.com; style-src 'self' data: 'unsafe-inline' www.tagmanager.google.com www.fonts.googleapis.com fast.fonts.net; img-src 'self' data: www.googletagmanager.com www.google-analytics.com; form-action 'self'; font-src 'self'; connect-src 'self'; report-to csp-endpoint

Report-To: {"endpoints":[{"url":"http://localhost:8080/myapp/csp-violation-report"}],"group":"csp-endpoint","max_age":10886400}

解决方法

  1. 报告终结点应具有secure context(实际上-https :)。非安全端点将被忽略。

  2. 有一些problems with resolving of localhost name,并非所有用户代理都将其视为可信任的。 localhost应该符合specific(第5段)名称解析规则(请参见Let 'localhost' be localhost RFC更新)。

简要-尝试使用https://127.0.0.1:8080/myapp/csp-violation-report。可能是安全的http s https://localhost:8080/myapp/csp-violation-report对于Chrome足够了(Chrome 84 supports the report-to指令,但Firefox不能)。