在 JavaScript 中跟踪安全策略权限

Tracing security policy permissions in JavaScript

本文关键字:安全策略 权限 跟踪 JavaScript      更新时间:2023-09-26

尽管设置了以下标头:

Access-Control-Allow-Origin: *
Content-Security-Policy: default-src 'self' 'unsafe-inline' *.speech.is http://bits.speech.is https://bits.speech.is http://speech.is

JavaScript 仍然阻止我访问 iframe.contentWindow.document:

Error: Permission denied to access property 'document'

从火狐控制台speech.contentWindow.document后在http://bits.speech.is.Chrome给出了更冗长但仍然神秘的东西:

SecurityError: Blocked a frame with origin "http://bits.speech.is" from accessing a frame with origin "http://208.113.212.187". Protocols, domains, and ports must match.
code: 18
message: "Blocked a frame with origin "http://bits.speech.is" from accessing a frame with origin "http://208.113.212.187". Protocols, domains, and ports must match."
name: "SecurityError"
stack: "Error: Blocked a frame with origin "http://bits.speech.is" from accessing a frame with origin "http://208.113.212.187". Protocols, domains, and ports must match.↵    at <anonymous>:2:21↵    at Object.InjectedScript._evaluateOn (<anonymous>:580:39)↵    at Object.InjectedScript._evaluateAndWrap (<anonymous>:539:52)↵    at Object.InjectedScript.evaluate (<anonymous>:458:21)"

有没有办法跟踪此安全策略的设置位置? 如何弄清楚我还错过了什么?

我能够找到的微调CSP的最佳方法是以下两者的结合:

  • 查看浏览器控制台
  • 将 CSP 配置为发送的 CSP 报告,因为它们有时包含更多详细信息,可以控制台消息

对于后者,您需要将report-uri添加到CSP标头中,我使用 http://cspbuilder.info/唯一的URL作为报告收集器。您还可以设置Content-Security-Policy-Report-Only变体,直到它开始工作。

如果您编写正在使用的浏览器,也会有所帮助。该消息看起来不像来自Chrome的CSP违规错误 - 它们比这个更详细,而且非常有用。