如何找到内容安全策略违规报告的来源?

How can I find the source of a Content Security Policy violation report?

当我访问我的网页时,我在浏览器中收到内容安全策略违规报告:

[Report Only] Refused to load the image '' because it violates the following Content Security Policy directive: "img-src 'self' ".

当我调查 HTTP 响应 headers 时,Content-Security-Policy-Report-Only header 或 Content-Security-Policy header 似乎都不是由源服务器设置的.

如何找到此违规报告的来源?考虑到相关的 header(s) 似乎没有设置,我不确定为什么会生成它。

可能有 2 个选项:

1. 您在页面中嵌入了 third-party iframe。您观察到的违规行为属于那个 iframe,它发布了自己的 CSP header。由于浏览器只有一个用于主页和所有 iframe 的控制台,因此您也会看到 third-party iframe 错误。
尝试使用其他浏览器,其中一些浏览器可能会更冗长并显示被阻止的 URI。无论如何,你与 third-party CSP 无关。

2. 如果访问不存在的网页,服务器的中间件可以为状态码为4xx/5xx的页面发布默认的CSP。概率低,因为在这种情况下不使用 Report-Only 模式。

发布 Content-Security-Policy-Report-Only 不是在 HTTP header 中而是在元标记中是不可能的,因为元标记不支持 Report-Only 模式。因此我押注 opt 1.