gpt4 book ai didi

html - 具有内容安全策略的 iFrame 沙箱

转载 作者:太空狗 更新时间:2023-10-29 14:55:39 26 4
gpt4 key购买 nike

我认为这只是对规范的一个简单误解。但是,我在将脚本包含在受沙盒保护的 iFrame 中时遇到了问题。具体来说,我正在处理的代码如下。

在 top.html 中:

<iframe src="framed.html" sandbox="allow-scripts"></iframe>

在 framed.html 中

...
<head>
<meta http-equiv="Content-Security-Policy" content="script-src example.com">
<script src="http://example.com/script.js"></script>
</head>
...

在 Chrome 中运行此文件时,出现错误:

Refused to load the script 'http://example.com/script.js' because it violates the following Content Security Policy directive: "script-src localhost:9000".

为什么会阻止脚本加载?我知道如果没有 allow-same-origin,iFrame 将获得一个完全独特的来源,不等于任何其他来源。因此,script-src 'self' 将不起作用。但是,我正在尝试从 CSP 中明确要求的来源加载脚本。想法?

更新:已创建JSFiddle展示问题。

最佳答案

当您使用具有唯一来源的沙盒页面时,您不能在 CSP 中放置没有方案的主机,这就是违反政策的原因。使用 script-src https://example.comscript-src http://example.com 甚至 script-src https://example。 com http://example.com,CSP 将正确放宽(请注意,CSP 是基于白名单的,默认情况下大多数事情都是不允许的)。


作为the grammar from the CSP specification显示,CSP 指令中的方案是 optional :

; Schemes: "https:" / "custom-scheme:" / "another.custom-scheme:"
scheme-source = scheme-part ":"

; Hosts: "example.com" / "*.example.com" / "https://*.example.com:12/path/to/file.js"
host-source = [ scheme-part "://" ] host-part [ port-part ] [ path-part ]
scheme-part = scheme
; scheme is defined in section 3.1 of RFC 3986.
host-part = "*" / [ "*." ] 1*host-char *( "." 1*host-char )
host-char = ALPHA / DIGIT / "-"
port-part = ":" ( 1*DIGIT / "*" )
path-part = path-abempty
; path-abempty is defined in section 3.3 of RFC 3986.

但没有 allow-same-origin token 的沙盒框架将有一个 null 来源,并且 URL 匹配算法不允许无方案指令匹配(算法的相关部分如下所示):

6.6.1.6. Does url match expression in origin with redirect count?

Given a URL (url), a source expression (expression), an origin (origin), and a number (redirect count), this algorithm returns "Matches" if url matches expression, and "Does Not Match" otherwise.

...

  1. If expression matches the host-source grammar:

    1. If url’s host is null, return "Does Not Match".

    2. If expression does not have a scheme-part, then return "Does Not Match" unless one of the following conditions is met:

      1. origin’s scheme is url’s scheme
      2. origin’s scheme is "http", and url’s scheme one of "https", "ws", or "wss".
      3. origin’s scheme is "https", and url’s scheme is "wss".

在给定的例子中:

  • origin 的方案是 null(因为使用了 sandbox 而没有 allow-same-origin ).
  • urlhttp://example.com/script.js

null 来源的 scheme 与后三种情况都不匹配,因此没有 scheme 的主机名将不会与任何 URL 匹配,因此违反了策略。

关于html - 具有内容安全策略的 iFrame 沙箱,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24410553/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com