【问题标题】:Load subdomains in iframes while avoiding cross-origin framing using document.domain在 iframe 中加载子域,同时避免使用 document.domain 进行跨域框架
【发布时间】:2019-04-09 15:54:31
【问题描述】:

我有一个安装了多个 WooCommerce 的 Wordpress 多站点。我正在尝试通过 iFrame 获取安装过程中所有区域的仪表板报告:

  • zh.foo.com
  • au.foo.com
  • eu.foo.com

在主仪表板页面上,我使用以下代码注入所需的 iFrame:

jQuery(document).ready(function($) {
        /*If the user is on the main dashboard...*/
        if(window.location.href.indexOf("https://foo.com/wp-admin/network/") > -1) {
            console.log('Admin Dashboard - Activating global iFrame stats');
            /*Create area for iFrames*/
            $('#dashboard-widgets').after('<div class="iframe-container"><strong>Cross Network Stats (US, UK, AU, EU)</strong></div>');
            /*United States*/
            /*Add US iFrame (on same domain - working)*/
            $('.iframe-container').append('<div><strong style="font-size: 2em;">United States:</strong><br><br><iframe src="https://foo.com/wp-admin/edit.php?post_type=shop_order" width="100%" height="450px"></iframe></div>');
            /*United Kingdom*/
            /*Add UK iFrame (on subdomain - not working)*/
            $('.iframe-container').append('<div><strong style="font-size: 2em;">United Kingdom:</strong><br><br><iframe src="https://en.foo.com/wp-admin/edit.php?post_type=shop_order" width="100%" height="450px"></iframe></div>');
        }
    });

第一个 iFrame 按预期工作,但第二个不按预期工作,因为它位于 iFrame 中并会产生 X-Frame 安全错误:


X-Frame-Options 拒绝加载: https://en.foo.com/wp-admin/edit.php?post_type=shop_order 没有 允许跨域框架。


我已经读到可以通过使用“document.domain”来避免这种情况。因此,我在全局管理页脚的页脚中注入了以下代码:

echo '<script>document.domain = "foo.com";</script>';

但是,我仍然遇到与以前相同的问题。

通过 jQuery 调用 iFrame 时是否需要将 document.domain 作为 javascript 函数触发?

【问题讨论】:

  • 很久没有和document.domain打交道了,这可能很棘手......如果它仍然有效的话。总是必须立即宣布……在其他一切之前。可能还会阅读 X-Frame-Options
  • 谢谢,我已将脚本置于所有其他脚本之上,但仍然出现相同的错误。

标签: javascript wordpress iframe x-frame-options


【解决方案1】:

您必须更改您的 Content-Security-Policy 标头。

Content-Security-Policy: frame-ancestors 'self' *.foo.com

This answer 更深入一点。

【讨论】:

  • 尝试添加到我的 apache2.conf 文件(在 Ubuntu 中)并得到以下信息:“AH00526:/etc/apache2/apache2.conf 第 204 行的语法错误:无效命令 'Content-Security- Policy:',可能拼写错误或由未包含在服务器配置中的模块定义”。我假设我正在做这个王...
  • 我实际上对 Apache 中的标头一无所知。这只是标头的“标准”表示法,Apache 的语法可能不同。
猜你喜欢
  • 2019-11-04
  • 1970-01-01
  • 2022-01-12
  • 1970-01-01
  • 2013-08-17
  • 1970-01-01
  • 1970-01-01
  • 2015-02-15
  • 2013-12-29
相关资源
最近更新 更多