您如何找到导致 CSP 错误的脚本?
How do you find which script is causing the CSP error?
Google 突然开始抛出这条消息:
"Content Security Policy of your site blocks the use of 'eval' in JavaScript"
我没有策略配置,所以为了测试,我尝试将我的 CSP 设置为(在 HTML 和 web.config 中):
<add name="Content-Security-Policy" value="default-src *; style-src 'self' 'unsafe-inline';
script-src 'self' 'unsafe-inline' 'unsafe-eval' https://js.squareup.com/; report-uri /csp_report_parser;" />
我在报告中一无所获,google 也不告诉我消息来源:
如何找到违规代码?
谢谢!
将 'report-sample' 标记添加到 script-src 指令。它应该发送导致违规的代码样本 - 40 个字符,还将发送行号(我可以看到你确实使用了违规报告功能)。
在浏览器控制台中,您可以看到发生违规的函数名称(绿色箭头)和行号(下方打印屏幕中的蓝色箭头)。
report-sample
的结果用红色下划线标出:
您可以通过 javascript 捕获 SecurityPolicyViolation event - 所有将在违规报告中发送的内容都可以访问,包括 line/column 号码。
Google started throwing this message out of the blue:
这不可能是天方夜谭。可能是软件升级后激活了默认的 CSP 规则,或者您触摸了设置。
无论如何你的 CSP script-src 'self' 'unsafe-inline' 'unsafe-eval'
允许 eval-expressions,所以你在某个地方发布了另一个 CSP。
Google 突然开始抛出这条消息:
"Content Security Policy of your site blocks the use of 'eval' in JavaScript"
我没有策略配置,所以为了测试,我尝试将我的 CSP 设置为(在 HTML 和 web.config 中):
<add name="Content-Security-Policy" value="default-src *; style-src 'self' 'unsafe-inline';
script-src 'self' 'unsafe-inline' 'unsafe-eval' https://js.squareup.com/; report-uri /csp_report_parser;" />
我在报告中一无所获,google 也不告诉我消息来源:
如何找到违规代码?
谢谢!
将 'report-sample' 标记添加到 script-src 指令。它应该发送导致违规的代码样本 - 40 个字符,还将发送行号(我可以看到你确实使用了违规报告功能)。
在浏览器控制台中,您可以看到发生违规的函数名称(绿色箭头)和行号(下方打印屏幕中的蓝色箭头)。
report-sample
的结果用红色下划线标出:您可以通过 javascript 捕获 SecurityPolicyViolation event - 所有将在违规报告中发送的内容都可以访问,包括 line/column 号码。
Google started throwing this message out of the blue:
这不可能是天方夜谭。可能是软件升级后激活了默认的 CSP 规则,或者您触摸了设置。
无论如何你的 CSP script-src 'self' 'unsafe-inline' 'unsafe-eval'
允许 eval-expressions,所以你在某个地方发布了另一个 CSP。