同源策略似乎不起作用

Same origin policy seemingly not working

我正在尝试详细了解同源政策。为此,我试图看看它是如何与代码一起工作的(我在网络开发方面完全是菜鸟)所以我启动了一个 Flask 服务器并创建了一个域 local.com 和一个子域 store.local.com。然后在子域的 store.html 页面中,我用 src=http://local.com 创建了一个 iframe - 没有以任何方式修改 document.domain 属性 ,如 here .

根据我的阅读,我认为这会失败,但它正确显示了超级域 index.html 页面的内容。我已经重复了在两台不同的物理计算机上托管这两个域的实验,但这并没有改变任何东西。 (我已经尝试过 Firefox 和 Chrome)

现在我也尝试使用 src=http://google.com 并且这确实被 SOP 阻止了。所以此刻我有点困惑,任何人都可以帮助我理解它吗?谢谢。

IFrame 通常不受同源策略约束。

但是,如果您在调用 http://google.com 时查看 HTTP 响应 header,您将看到以下 header:

x-frame-options:SAMEORIGIN

这很明确tells your browser that it should prevent the page from being displayed across origins


MDN describes this particular behavior:

Here are some examples of resources which may be embedded cross-origin:

[...]

  • Anything with <frame> and <iframe>. A site can use the X-Frame-Options header to prevent this form of cross-origin interaction.