iframe 如何删除自己的沙箱?

Off*_*rmo 7 javascript browser iframe dom

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

\n

MDN 给出以下警告:

\n
\n

关于沙箱的注意事项:

\n

当嵌入文档与嵌入页面具有相同来源时,强烈建议不要同时使用allow-scripts和allow-same-origin,因为这会让嵌入文档删除沙箱属性\xe2\x80\x94,使其不再存在比根本不使用沙箱属性更安全。

\n
\n

Firefox 开发工具向我显示此警告:

\n
\n

沙箱属性同时具有允许脚本和允许同源的 iframe可以删除其沙箱

\n
\n

我的问题是:在这种情况下(sandbox="allow-same-origin allow-scripts" ) iframe 如何删除其沙箱?什么js代码可以执行这个?

\n

从 iframe 中,我尝试查看window.opener但它为空。window.parent不是指父级(编辑:不正确,我错误地使用了开发工具)。我无法从 iframe 本身找到对 \xc2\xabiframe\xc2\xbb 的引用...

\n

Hen*_*ker 2

您可以通过 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>
Run Code Online (Sandbox Code Playgroud)

恶意儿童.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>
Run Code Online (Sandbox Code Playgroud)

这是具有此设置的Code Sanbox

  • AFAICT,仅当两个页面均来自同一来源时才有效。否则,“parent.document”在浏览器中会失败,并显示“未捕获的 DOMException:访问跨源对象上的属性“文档”的权限被拒绝”。 (3认同)