I'm having trouble getting new Function
to work in a Web Worker. I have an HTML page that spawns a Web Worker. This Web Worker executes code through new Function(str)
. I'm trying to use this in a packaged Chrome app, which requires a page using eval
-like code to be explicitly listed as a sandboxed page in the manifest.
Now, there are two options:
- Do list the page to be sandboxed. If I do so, I can use
new Function
, but I cannot spawn a Web Worker because I cannot make any requests (the sandboxed page has a unique origin).new Worker(...)
throws aSECURITY_ERR
.new Function
works in sandboxnew Worker
fails in sandbox due to unique origin
- Don't list the page to be sandboxed. If I do so, I can spawn a Web Worker, but the worker cannot use
new Function
because it isn't sandboxed.new Function(...)
throws anEvalError
complaining about the use of it.new Function
fails in non-sandbox due to beingeval
-likenew Worker
works in non-sandbox
My CSP is as follows:
sandbox allow-scripts script-src 'self' 'unsafe-eval'; object-src 'self'
What can I do to get new Function
working in a Web Worker?
There's a technique called inline workers, I would suggest using that.
This is described with example code on the HTML5 rocks site in their WebWorkers tutorial. This way you could list the site as sandboxed, but since there's no need to do external requests, it should work in sandboxed mode as well.