html - 沙盒、IFrame 和允许同源

标签 html iframe

我一直在阅读有关 <iframe> 的 HTML5 添加内容的信息标签。添加的内容之一是包含允许将文档加载到 iframe 中的沙盒标志。与其父浏览器上下文交互。

在阅读了一些文档之后,我正在寻求一些清晰度。我读过MDN's allow-same-origin 的描述标志:

Allows the content to be treated as being from its normal origin. If this keyword is not used, the embedded content is treated as being from a unique origin.

在阅读 W3C's 之后,我认为没有太大帮助。规范:

...[I]t can be used to embed content from a third-party site, sandboxed to prevent that site from opening pop-up windows, etc, without preventing the embedded page from communicating back to its originating site, using the database APIs to store data, etc.

我的问题具体是关于 MDN 根据 W3C 的规范将什么称为“正常来源”:当提及“正常来源”时,MDN 说明 <iframe> 中包含的文档内容。标记被视为共享文档来源页面的来源,例如一个 YouTube 视频相信 - 并且可以像交流一样 - 它仍然是 YouTube 的一部分?或者,<iframe>文档有权访问父浏览器上下文?

最佳答案

看了LFC的回答我还是有点懵,但是他们提供的链接已经很好的解释了。总结如下:

假设我们想在我们的网站上添加一个推文按钮。我们可以这样做:

<iframe src="https://platform.twitter.com/widgets/tweet_button.html"></iframe>

但我们可能授予 Twitter 的权限超出了他们的需要。所以我们想对它们进行沙箱处理。 Twitter 显然需要知道用户是否已登录(例如,也许他们可以在推文按钮旁边显示您的头像),因此 iframe 需要能够访问 twitter.com cookie 和与 twitter.com 相关的其他数据(本地存储等)。因此,通过设置 allow-same-origin,我们允许 iframe 使用来自 twitter.com 的数据。

Twitter 可能还需要向 twitter.com 资源发出请求,如果您没有设置 allow-same-origin,这些请求将被视为跨域请求>,因此这些请求可能会被浏览器阻止 - 除非资源具有允许跨源请求的 header 。

关于html - 沙盒、IFrame 和允许同源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31184505/

相关文章:

javascript - 从 Node.js(无框架)在网站上显示数据

javascript - 如何使页面元素出现在具有焦点的控件周围?

html - iframe 中的 WCAG 2.0 问题

javascript动态创建iframe,但是如何附加属性呢?

html - CSS隐藏具有不同宽度列的表格上的溢出文本

javascript - 带有切换的 jQuery 效果 .fadeTo()?

html - AWS API网关: why is the invoke url returning HTML code instead of actual web page?

javascript - beforeunload事件之前的Angular8 IFrame

javascript - JS - Youtube Iframe API 返回错误的当前时间和持续时间?

javascript - 在来自不同域的 iFrame 上运行小书签