允许 cross-site 个子域之间的请求而不更改第二个子域的文件内容

Allowing cross-site requests between subdomains without changing file contents of second sub domain

我目前正在尝试打包一个 Web 应用程序 (ConnectWise) 以包含在我公司的中央 Intranet 站点中。在大多数情况下,这是一个相当简单的过程;创建一个带有 iframe 的包含页面,将 iframe 指向 ConnectWise url。这部分适用于几乎所有功能。

问题出现在应用程序的某些 select 功能期间(在本例中,是创建时间表条目的过程的一部分),这些功能根本无法工作。 Chrome 给出以下控制台输出。

Uncaught SecurityError: Failed to read the 'frame' property from 'Window': Blocked a frame with origin "https://app.example.com" from accessing a frame with origin "https://host.example.com". Protocols, domains, and ports must match.

我知道这是由 cross-site 和 same-origin 策略的安全选项引起的。鉴于以下几点,有没有办法克服这个问题?

我已经尝试在每个服务器上设置 Access-Control-Allow-Origin,这是迄今为止我遇到的唯一不涉及能够更改应用程序服务器的文件内容的方法。当给定

的设置(和设置组合)时,这似乎不起作用

编辑:

this "duplicate" question 的解决方案不适用于此处。我没有 更改 iframed 页面 (app.example.com) 的文件内容(包括 javascript)的权限。此外,需要 运行 权限的脚本是 iframe 中的页面,而不是承载 iframe 的页面。

不,不可能。

Access-Control-Allow-Origin 主要影响从 HTTP 请求获取原始数据,而不是实时 DOM。

postMessage可以让不同来源的框架互通,但需要两个页面都包含JS。

CORS headers 例如 Access-Control-Allow-Origin 只影响 AJAX 请求,不影响 DOM 访问。

但是,如果它们都在同一个域中但在不同的子域中,那么您可以在每个页面上包含以下内容:

document.domain = 'example.com';

From MDN:

One document is allowed to access another if they have both set document.domain to the same value, indicating their intent to cooperate

如果 app.example.com 有任何 script 包含到 host.example.com 那么你可以将上面的代码放在那些脚本中来设置域。

例如

<script src="https://host.example.com/setup.js"></script>