我正在使用同源 iframe 加载外部小部件(通过跨源脚本加载的 tlk.io)。我正在尝试为 iframe/widget 提供尽可能低的权限,以将其与我的应用程序隔离。
MDN 给出以下警告:
关于沙箱的注意事项:
当嵌入的文档与嵌入页面具有相同的来源时,强烈建议不要同时使用 allow-scripts 和 allow-same-origin,因为这会让嵌入的文档删除沙箱属性- 使其不比不使用更安全沙箱属性。
Firefox devtools 向我展示了这个警告:
对于沙箱属性同时具有 allow-scripts 和 allow-same-origin 的 iframe可以删除它的 sandboxing。
我的问题是:在这种情况下 ( sandbox="allow-same-origin allow-scripts"
) iframe 如何删除其沙箱?什么js代码会执行这个?
从 iframe 中,我尝试查看window.opener
但它为空。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。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句