- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我正在构建一个用于运行不受信任代码的沙箱。出于这个原因,我创建了一个沙盒 iframe(仅在其 sandbox
属性中设置了 allow-scripts
权限)以保护来源,然后在该 iframe 中我创建一个 web-worker 以确保一个单独的线程并防止卡住主应用程序,以防不受信任的代码具有例如无限循环。
问题是,如果我尝试通过 https 加载沙箱,最新的 Google Chrome 不允许创建工作程序。在其他浏览器上它可以工作,如果我通过 http 在 Chrome 中加载沙箱,它也可以工作。
代码如下:
index.html:
<!DOCTYPE html>
<html>
<head>
<title>Sandbox test</title>
<script type="text/javascript" src="main.js"></script>
</head>
<body></body>
</html>
main.js:
// determining absolute path of iframe.html
var scripts = document.getElementsByTagName('script');
var url = scripts[scripts.length-1].src
.split('/')
.slice(0, -1)
.join('/')+'/iframe.html';
window.addEventListener("load", function() {
var iframe = document.createElement('iframe');
iframe.src = url;
iframe.sandbox = 'allow-scripts';
iframe.style.display = 'none';
document.body.appendChild(iframe);
window.addEventListener('message', function(e) {
if (e.origin=='null' && e.source == iframe.contentWindow) {
document.write(e.data.text);
}
});
}, 0);
iframe.html:
<script src="iframe.js"></script>
iframe.js:
var code = 'self.postMessage({text: "sandbox created"});';
var url = window.URL.createObjectURL(
new Blob([code], {type: 'text/javascript'})
);
var worker = new Worker(url);
// forwarding messages to parent
worker.addEventListener('message', function(m) {
parent.postMessage(m.data, '*');
});
演示:
http://asvd.github.io/sandbox/index.html - http 演示(无处不在)
https://asvd.github.io/sandbox/index.html - https 演示(在 Chrome 中不起作用)
https://github.com/asvd/asvd.github.io/tree/master/sandbox - 来源(与这个问题中的内联完全一样)
Google Chrome 然后提示:
混合内容:位于“https://asvd.github.io/sandbox/iframe.html”的页面' 通过 HTTPS 加载,但请求了不安全的 Worker 脚本 'blob:null/a9f2af00-47b1-45c1-874e-be4003523794'。此请求已被阻止;内容必须通过 HTTPS 提供。
我还尝试通过 https 从文件而不是 blob 加载工作代码,但这在任何地方都不允许,因为我无法从 iframe 访问同源文件。
我想知道是否有机会在不向 iframe 添加 allow-same-origin
权限的情况下在 Chrome 中使用这样的沙箱。
最佳答案
正如您所发现的,Chrome 不允许您从 https 页面访问非 https 内容(例如数据 blob),并且还会将 blob URL 视为非 https。如果没有 allow-same-origin
,它就无法从任何域加载任何工作脚本文件。
我唯一的建议是从单独的 https 服务域(/子域)提供 iframe,然后同时使用 allow-scripts
和 allow-same-origin
。由于位于单独的域中,iframe 中的代码仍然无法访问父页面的 DOM/数据。
关于javascript - 如何在沙盒 iframe 中创建工作人员?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30558817/
是否有用于手动测试的代码覆盖工具?比如我新写了30行代码,编译,然后运行,有什么办法可以快速验证这30行都运行了吗? 另外,后来,在我将代码 checkin 正式版本后,有什么方法可以验证测试部门在进
老实说,这是一个家庭作业问题,但我已经浪费了好几个小时,而且无法正确解决。它返回错误数量的结果或错误的数据: 我需要选择参与指导电影和/或在电影中表演的每个人以及他们所做的次数,如果至少 5 次。 有
我正在尝试测试 MacOS 的应用内购买。输入测试用户凭据后,App Store 提示:“当前收据无效或 ds 人员 ID 不匹配。”并且购买失败。 最佳答案 我尝试了很多方法来解决这个问题。 Get
我正在为 Jenkins 使用 ActiveDirectory 插件,因此用户必须使用他们的凭据登录到 Jenkins。然后用户在 Jenkins 中被称为 joe.doe,这很完美。 当同一个人 c
如何从 Infopath 人员/组选取器检索电子邮件地址?当我将人员/组选取器添加到 infopath 表单时,我只得到 3 个字段 DisplayName、AccountId、AccountType
在 Snow Leopard 中,可以在 iCal 事件中显示空闲/忙碌时间。我搜索了 CalStore.framework 的 header ,但找不到任何描述该字段的属性。如何检索日历事件的忙/闲
是否有人成功地从专门针对 SharePoint 2013 的新建或编辑表单中获取用户(个人或组)字段的值? 我已经尝试了通过搜索互联网找到的所有解决方案以及我自己能想到的所有解决方案,所有结果都为空白
所以我需要将一个 Twitter 帐户添加到 ABRecordRef 中。然而,最快的方法似乎是获取社交资料属性的多值引用,创建它的可变版本,查找它是否有 Twitter 条目,如果已经有,则创建
我正在尝试将使用 Tomcat(最初是 5.5,但可以与 7 一起使用)在 MyEclipse 中开发的应用程序部署到我们的演示服务器 (Sun Java Web Server 7)。不幸的是,所有设
我是一名优秀的程序员,十分优秀!