javascript - 切换 iFrame 沙盒

标签 javascript html iframe sandbox

我正在尝试弄清楚是否可以通过编程方式更改 iFrame 的沙箱值。

来自 MDN iframe :

When the embedded document has the same origin as the main page, it is strongly discouraged to use both allow-scripts and allow-same-origin at the same time, as that allows the embedded document to programmatically remove the sandbox attribute. Although it is accepted, this case is no more secure than not using the sandbox attribute.

但是,更改父项中的属性并不会像在子项中暗示的那样成功触发沙盒。

document.getElementById('myFrame').setAttribute('sandbox', 'allow-scripts');

在该 iFrame 中加载的页面可以成功获得对父级的访问权限,如果沙盒正在运行则不会出现这种情况。属性确实发生了变化,但安全性存在缺陷,因为它不会阻止对专利的访问。

console.log(window.parent.document);

以上内容仍然适用于“以编程方式”启用沙箱的 iFrame。 Chrome 和 Firefox 都是这种情况,这意味着要么是预期的未记录的功能,要么是实现不当的规范。

有人对什么是适当的期望有任何想法吗?

最佳答案

请试试这个 fiddle :http://jsfiddle.net/yAwxy/ 当脚本运行时它不工作 onLoad b/c iframe 已经在工作。 如果您尝试 onDomready,则应用沙盒 b/c 脚本尚未运行。 如果您使用此 fiddle 登录:

<iframe id="myFrame" srcdoc="<script>console.log('Executing script inside iFrame')</script>">
</iframe>

在脚本中

console.log('Executing script inside page')

当脚本 console.log('Executing script inside page') 被包装在 onLoad 时,输出是:

Executing script inside iFrame 
Executing script inside page 

当包装 onDomready 时:

Executing script inside page 
Executing script inside iFrame 

另见 http://jsfiddle.net/yAwxy/1/ .

所以要改变规则,他们必须改变OndomReady

关于javascript - 切换 iFrame 沙盒,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21652320/

相关文章:

javascript - 显示微调器,直到 iframe 加载了 http post 响应

javascript - 如何引导在友好的 IFRAME 中加载的 AngularJS 应用程序

java - 文本选择在 vaadin 应用程序内的 iframe 中不起作用

javascript - 使用 Angular 上传文件时浏览器崩溃

javascript - axios createError.js :16 Uncaught (in promise) Error: Request failed with status code 400

javascript - 如何在selenium中打印页面并关闭打印窗口

javascript - 隐藏的 div 中的元素 ID 是否可用?

javascript - 如何为输入标签分配一些值

javascript - 无法覆盖响应式设计中的 css 元素

javascript - 下拉字段中插入的文本未反射(reflect)