javascript - How to create a worker in a sandboxed iframe? -


i building sandbox running untrusted code. reason create sandboxed iframe (which has allow-scripts permission set in sandbox attribute) in order protect origin, , inside iframe create web-worker ensure separate thread , prevent freezing main application in case untrusted code has infinite loop instance.

the problem is, if try load sandbox on https, recent google chrome not allow create worker. on other browsers works, , works if load sandbox in chrome via http.

here code:

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 parent worker.addeventlistener('message', function(m) {     parent.postmessage(m.data, '*'); }); 

demo:

http://asvd.github.io/sandbox/index.html - http demo (works everywhere)

https://asvd.github.io/sandbox/index.html - https demo (doesn't work in chrome)

https://github.com/asvd/asvd.github.io/tree/master/sandbox - source (exactly inlined in question)

google chrome complains:

mixed content: page @ 'https://asvd.github.io/sandbox/iframe.html' loaded on https, requested insecure worker script 'blob:null/a9f2af00-47b1-45c1-874e-be4003523794'. request has been blocked; content must served on https.

i tried load worker code https file instead of blob, not permitted anywhere, since cannot access files of same origin iframe.

i wondering if there opportunity make such sandbox work in chrome, without adding allow-same-origin permission iframe.

as have discovered, chrome won't let access non-https content (such data blob) https page, , treats blob urls not being https. , without allow-same-origin, can't load worker script files domain.

my suggestion have iframe served separate https-served domain (/subdomain), , have both allow-scripts , allow-same-origin. due being on separate domain, code in iframe still won't able access dom/data of parent page.


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -