2014-06-25 57 views
4

我认为这是规范的只是一个简单的误解。不过,我在将脚本包含在受沙箱保护的iFrame中时遇到了问题。具体来说,我正在处理的代码如下。的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展示此问题。

+0

这是与Firefox相同 – mems

回答

6

当您使用沙盒页面具有独特的出身,你不能把一个主机,而无需方案的CSP,这就是为什么违反策略。使用脚本SRC https://example.com或脚本SRC http://example.com甚至脚本的SRC https://example.comhttps://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令牌沙盒框架将有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.

...

If expression matches the host-source grammar:

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

    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".

在t他给出的例子:

  • originnull(因为使用的​​没有allow-same-origin的)。
  • 网址http://example.com/script.js

null起源的方案不符合任何过去三年的情况下,所以没有计划将主机名不匹配任何URL,因此违反策略。

+0

哇 - 这是令人难以置信的愚蠢的我。非常感谢帮忙。我想我只是[MDN]上的示例(https://developer.mozilla.org/en-US/docs/Web/Security/CSP/CSP_policy_directives)显示了没有该方案的CSP。 – huntaub

+1

@huntaub想一想,我错了。规范**允许**省略该方案(因为它在方括号内,[这意味着该标记是可选的](http://tools.ietf.org/html/rfc5234#section-3.8))。这个规范看起来更可能没有被正确地遵守,即一个浏览器错误。你能不接受我的回答吗?然后我将删除它。 –

+0

是的 - 我可以不接受你的回答,虽然 - 我不会删除它。这对于找出_why_这是行不通的。 – huntaub