Chrome上的Javascript跨源异常,但Firefox上没有

Chrome上的Javascript跨源异常,但Firefox上没有,javascript,iframe,google-chrome-extension,firefox-addon-webextensions,cross-domain-policy,Javascript,Iframe,Google Chrome Extension,Firefox Addon Webextensions,Cross Domain Policy,我最近开发了一个Firefox扩展,它允许我在几个网站上自动执行一些操作(主要是检索数据) 该扩展运行良好,因此我目前正在开发该扩展的Chrome版本 Chrome和Firefox扩展之间存在一些差异,但主要是两个扩展中运行的代码相同 问题 大部分都可以,但是我在一个特定的网站上遇到了麻烦,这个网站可以很好地与Firefox扩展配合使用 在这个网站上,我试图得到一个表,它位于一个iframe中,它本身位于另一个iframe中 当访问第二个iframe时,我面临以下异常 Uncaught Sec

我最近开发了一个Firefox扩展,它允许我在几个网站上自动执行一些操作(主要是检索数据)

该扩展运行良好,因此我目前正在开发该扩展的Chrome版本

Chrome和Firefox扩展之间存在一些差异,但主要是两个扩展中运行的代码相同


问题

大部分都可以,但是我在一个特定的网站上遇到了麻烦,这个网站可以很好地与Firefox扩展配合使用

在这个网站上,我试图得到一个表,它位于一个iframe中,它本身位于另一个iframe中

当访问第二个iframe时,我面临以下异常

Uncaught SecurityError: Blocked a frame with origin "https://subdomain.domain.fr" from accessing a frame with origin "https://subdomain.domain.fr". The frame requesting access set "document.domain" to "domain.fr", but the frame being accessed did not. Both must set "document.domain" to the same value to allow access
该页面的结构如下

<iframe id="iframe_centrale" src="/[...]">
    [...]
    <iframe id="iframe" src="/[...]">
        [...]
        <table></table>
        [...]
    </iframe>
    [...]
</iframe>
var tableElement = null;
var iframe = null;
iframe = document.getElementById('iframe_centrale');
if(null != iframe)
{
    iframe = iframe.contentWindow.document.getElementById("iframe");
    if(null != iframe)
    {
        tableElement = iframe.contentWindow.document.evaluate("//table", iframe.contentDocument, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
        if((null != tableElement)
        {
            // Read table
        }
    }
}

问题分析

尝试访问位于另一个域上的ressource时,通常会遇到激发的异常

但是在我的例子中,两个iframe都位于同一个域上,因为
src
属性是一个相对url

同样的代码在Firefox上正常工作的事实也证实了我可以访问第二个iframe


更多信息

我面临的另一个错误是,在浏览过程中,我将一个表单填充到iframe中,下面的异常是triggeredonce,但是表单仍然正确填充

Uncaught DOMException: Blocked a frame with origin "https://subdomain.domain.fr" from accessing a cross-origin frame.
由于它在一个变异观察者循环中,指令可能第二次工作,它可以解释为什么表单被填充

填写表单后,我点击一个按钮,进入一个iframe,它本身进入一个iframe。 当点击按钮时,我有以下异常

Refused to run the JavaScript URL because it violates the following Content Security Policy directive: "script-src 'self'". Either the 'unsafe-inline' keyword, a hash ('sha256-...'), or a nonce ('nonce-...') is required to enable inline execution
再次,点击被做成一个变异观察者循环,这样它可能会被触发一次,然后工作,但我被重定向到下一页,所以点击是工作的


从Firefox进行调查

因为我可以从Firefox访问两个iFrame的内容,所以我尝试打印基本文档和两个iFrame的“域”和“url”信息

#############################################################
### BASE PAGE : domain.fr / https://subdomain.domain.fr/path1
### IFRAME 1 : domain.fr / https://subdomain.domain.fr/path2
TypeError: iframe2 is null

[...]

#############################################################
### BASE PAGE : domain.fr / https://subdomain.domain.fr/path1
### IFRAME 1 : domain.fr / https://subdomain.domain.fr/path2
### IFRAME 2 : domain.fr / about:blank

[...]

#############################################################
### BASE PAGE : domain.fr / https://subdomain.domain.fr/path1
### IFRAME 1 : domain.fr / https://subdomain.domain.fr/path2
### IFRAME 2 : subdomain.domain.fr / https://subdomain.domain.fr/path3
因此,我认为问题的出现是因为当url从
about:blank
更新为
https://subdomain.domain.fr/path3


问题

为什么使用位于同一域/子域上的iframe触发异常

是否有其他方法访问第二个iframe内容并使用Chrome对其进行操作


有人知道为什么这段代码与Firefox兼容,而与Chrome兼容吗?

错误消息中说明了原因:

The frame requesting access set "document.domain" to "domain.fr", but the frame being
accessed did not. Both must set "document.domain" to the same value to allow access
同源策略基于“”术语:
方案
+
主机
+
端口
+
。起源通常是不可变的,只有元组起源的域可以更改,并且只能通过
document.domain
API(端口号将另外重置为null

您的一个框架带有原点
https://subdomain.domain.fr:443
document.domain
属性设置为
https://domain.fr:null

要允许它访问页面上的其他iFrame,必须设置
document.domain=”https://domain.fr“
发送给他们所有人。或者,如果可能的话,
document.domain
属性可以用于任何iframe和主窗口

为什么您的代码在Firefox中工作而在Chrome中不工作,这有点奇怪——两种浏览器的最新版本都以相同的方式支持
document.domain
属性

请注意
document.domain
属性,如果可能,最好避免使用它

拒绝运行JavaScript URL

内容安全策略似乎阻止了
构造。它不阻止链接跟踪,只是阻止javascript执行