【问题标题】:enable subdomain iframe access启用子域 iframe 访问
【发布时间】:2016-11-23 18:33:35
【问题描述】:

我正在尝试在 y.example.com 托管的网页上显示来自 x.example.com 的 iframe

这是我所做的设置

雄猫:

<filter>
    <filter-name>ClickJackFilterEnabled</filter-name>
    <filter-class>org.apache.catalina.filters.HttpHeaderSecurityFilter</filter-class>
    <init-param>
        <param-name>antiClickJackingEnabled</param-name>
        <param-value>false</param-value>
    </init-param>
</filter>

<filter-mapping>
    <filter-name>ClickJackFilterEnabled</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

Spring 安全性:

    httpResponse.setHeader("Content-Security-Policy", "default-src 'self' *.example.com; style-src 'self' *.googleapis.com *.amazonaws.com 'unsafe-inline'; script-src 'self' 'unsafe-inline' 'unsafe-eval'; child-src 'self' *.example.com; font-src *;img-src 'self' *.amazonaws.com");
    httpResponse.setHeader("Access-Control-Allow-Origin", "http://*.example.com");

当我打开带有嵌入式 iframe 的页面时,我仍然收到此错误:

Refused to display 'http://x.example.com/' in a frame because it set 'X-Frame-Options' to 'SAMEORIGIN'.

Uncaught SecurityError: Sandbox access violation: Blocked a frame at "http://y.example.com" from accessing a frame at "http://x.example.com".  The frame being accessed is sandboxed and lacks the "allow-same-origin" flag.

当我使用 curl 检查标题时,标题 X-Frame-Options 不存在

这是 curl 的输出

* Rebuilt URL to: y.example.com/
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
   0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0*   Trying 127.0.0.1...
* Connected to y.example.com (127.0.0.1) port 80 (#0)
> GET / HTTP/1.1
> Host: y.example.com
> User-Agent: curl/7.47.0
> Accept: */*
> 
< HTTP/1.1 200 OK
< Server: Apache-Coyote/1.1
< X-Content-Type-Options: nosniff
< X-XSS-Protection: 1; mode=block
< Cache-Control: no-cache, no-store, max-age=0, must-revalidate
< Pragma: no-cache
< Expires: 0
< X-Application-Context: application:dev:8080
< Content-Security-Policy: default-src 'self' *.example.com; style-src 'self' *.googleapis.com *.amazonaws.com 'unsafe-inline'; script-src 'self' 'unsafe-inline' 'unsafe-eval'; child-src 'self' *.example.com; font-src *;img-src 'self' *.amazonaws.com
< Access-Control-Allow-Origin: http://*.example.com
< Content-Type: text/html;charset=UTF-8
< Content-Language: en-IN
< Transfer-Encoding: chunked
< Date: Wed, 20 Jul 2016 13:21:57 GMT
< 
{ [8200 bytes data]

我错过了什么?

更新:

我试着设置

document.domain = "example.com"

在两个网页上,我仍然收到错误

Refused to display 'http://x.example.com/' in a frame because it set 'X-Frame-Options' to 'SAMEORIGIN'

当我输入时

document.domain

在 javascript 控制台中,我得到了

"example.com"

在两个网页上。所以两个页面的来源是相同的。

【问题讨论】:

    标签: spring tomcat iframe subdomain x-frame-options


    【解决方案1】:

    终于解决了这个问题。问题可能在于浏览器如何处理 X-Frame-Options 标头。

    设置背后的概念

    document.domain = "example.com" 
    

    是使两个网页的来源相同。浏览器允许将源设置为父域,因此 axexample.com 的页面可以将 document.domain 的值设置为 x.example.comexample.com

    现在,即使将 document.domain 设置为 example.com,我还是收到了错误

    Refused to display 'http://x.example.com/' in a frame because it set 'X-Frame-Options' to 'SAMEORIGIN'
    

    当我完全删除 X-Frame-Options 标头时,使用

    http.headers().frameOptions().disable();
    

    在 securityConfiguration.java 中,它起作用了!

    我不知道浏览器不支持 document.domain 设置的原因。在 Mozilla 文档或其他任何地方都找不到关于此的任何内容。希望这对某人有所帮助。

    【讨论】:

      【解决方案2】:

      x.example.com 是发送 SAMEORIGIN 标头的那个。 y.example.com 不能覆盖它,因为这样就无法阻止 iframe 包含。一个站点必须授予其他站点包含其内容的权限(由于缺乏来源政策或具有权限的站点列表)。

      检查来自 x.example.com 的标头,您应该会看到该政策实际上阻止了 iframe。

      【讨论】:

      • 两个子域托管在同一台服务器上,设置相同,两个子域返回的标头完全相同。
      猜你喜欢
      • 2014-05-20
      • 1970-01-01
      • 2013-03-05
      • 1970-01-01
      • 2010-10-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-02
      相关资源
      最近更新 更多