带有内容安全策略的iFrame Sandbox

时间:2014-06-25 13:49:35

标签: html5 google-chrome iframe sandbox content-security-policy

我认为这只是对规范的一个简单误解。但是,我遇到了在沙盒保护的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中运行此文件时,它会显示错误:

  

拒绝加载脚本“http://example.com/script.js”因为   它违反了以下内容安全策略指令:   “script-src localhost:9000”。

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

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

1 个答案:

答案 0 :(得分:8)

当您使用具有唯一来源的沙盒页面时,您无法在CSP中放置没有方案的主机,这就是违反该政策的原因。使用script-src https://example.com或script-src http://example.com甚至script-src https://example.com https://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匹配算法不允许无方案指令匹配(下面显示的算法的相关部分):< / p>

  

6.6.1.6. url是否在重定向计数的原点匹配表达式?   给定一个URL( url ),一个源表达式(表达式),一个源( origin )和一个数字(重定向计数),这个算法返回&#34;匹配&#34;如果url匹配表达式,&#34;不匹配&#34;否则。

     

...

     

如果表达式与主机源语法匹配:

     
      
  1. 如果 url 的主持人是null,请返回&#34;不匹配&#34;。

         
        
    1. 如果 url 的主持人是null,请返回&#34;不匹配&#34;。
    2.   
    3. 如果表达式没有方案部分,则返回&#34;不匹配&#34;除非满足下列条件之一:

           
          
      1. origin 的方案是url的方案
      2.   
      3. 原产地的计划是&#34; http&#34;和 url 的&#34; https&#34;,&#34计划之一; ws&#34;,或&#34; wss&#34;。
      4.   
      5. 原产地的方案是&#34; https&#34;和 url 的方案是&#34; wss&#34;。
      6.   
    4.   
  2.   

在给定的例子中:

  • originnull(因为sandbox没有使用allow-same-origin)。
  • url http://example.com/script.js

null来源的方案与最后三种情况中的任何一种都不匹配,因此没有方案的主机名称不会匹配任何网址,因此违反了该政策。

相关问题