gpt4 book ai didi

javascript - iframe 如何删除自己的沙箱?

转载 作者:行者123 更新时间:2023-12-04 07:45:24 28 4
gpt4 key购买 nike

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

Notes about sandboxing:

When the embedded document has the same origin as the embedding page, it is strongly discouraged to use both allow-scripts and allow-same-origin, as that lets the embedded document remove the sandbox attribute — making it no more secure than not using the sandbox attribute at all.


Firefox devtools 向我展示了这个警告:

An iframe which has both allow-scripts and allow-same-origin for its sandbox attribute can remove its 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>
恶意 child .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有了这个设置。

关于javascript - iframe 如何删除自己的沙箱?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67223608/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com