-
Notifications
You must be signed in to change notification settings - Fork 125
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
new Function causes Content Security Policy failure #31
Comments
It's used for passing functions from the main thread to the worker so they can be executed there - since you can't pass a function directly we have to stringify it and then rehydrate it on the other side. I'm happy to entertain alternative approaches for doing that. If it helps, the input to these is always a JS function to begin with, so if you're worried about arbitrary strings getting evaluated into executable JS then that would already have happened before it gets to this code anyway. Also, it executes within a web worker so it's somewhat sandboxed. If there's a way I can annotate just this section of code as safe that your tools would recognize, let me know. |
I think I have an alternative implementation for the worker function rehydration that uses Would that satisfy your CSP requirements? |
I'm new to this technology, so there is a high risk anything I say is mistaken. If I understood correctly, a change to use I think it would be an improvement allowing to keep most of the policy intact protecting against security bugs we could expect in other code we use with troika. The safest would be the worker could use an url to the same origin. I guess that would require some sort of orhestration for the correct deployment and knowing the urls. Sounds difficult. Thanks. |
I think you're right about the As you pointed out, a separate URL for the worker code would be difficult, to the point of being a non-starter IMO. Many (perhaps most) users of Troika are currently just using |
Some more food for thought on this... from https://www.w3.org/TR/CSP2/#source-list-guid-matching :
|
A CDN would still need a fallback, because there are local uses that do not have access to Internet. But if you can provide the file(s) you'd place on CDN, perhaps it would make sense having the worker url(s) be configurable. If not configured, it could build the I did realize |
Thanks for the input, I'll look into the possibility of separate files further. I found Mapbox's approach to this problem; I suspect that won't be an exact solution though, because Troika uses the worker modules as a general multithreading implementation where it can run arbitrary code off-thread when needed, and have those pieces of code talk together in the same worker. So unlike Mapbox there isn't just a single file that holds all worker code. There would need to be a main worker file, and then separate files that it would import, one for each modular piece of code, and make those all reference each other appropriately. Another possible "fix" may be to fall back to running the code in the main thread, when the worker can't be used. That may be acceptable for many applications. So that I'm aware, what part(s) of Troika are you using, so I can target this fix if needed? I've been assuming this is for |
Yes, we are using The Mapbox seemed to be dealing with the same problems. Nice find! You use rollup as well and it seems to be a great help in building the bundles. If there are many bundles, it is not a big problem. Perhaps they could have common naming so that they are easy to copy with one glob ( |
Functions are now rehydrated using `importScripts(blobURL)` rather than `new Function()`. This makes it slightly easier to whitelist in CSP rules (see issue #31), and improves debugging since they now show up as sources in devtools. Added a `name` parameter to defineWorkerModule which gets inserted as a comment in the rehydrated source so it's easier to find where each function comes from.
FYI I've just released version 0.23.0 which contains the limited change from |
Thanks! I added on our work list to upgrade, modify the CSP and see it works. We'll report back here what happened once we get to it. |
FYI, as of c754d0b#diff-24c89a991ff540df6b54f39f0115695f (in version 0.26.0) there is now a fallback mode where the code will run in the main thread as normal non-eval'd JS code if it cannot initialize the Worker for some reason. This should allow it to work in very strict CSP scenarios, at the expense of main thread performance. |
Thanks. I really feel our issue is being taken care of. Sorry we've been slow testing the new versions. I poked the issue in hopes we'd get to update and test. |
Hi. Finally back on this. We upgraded to troika-three-text@0.33.1, but some CSP repated problems are still there. I'm not sure if this is intended.
We do not get the text and get following error from Chrome 85.0.4183.121.
Firefox 81.0 also gives similar errors:
Adding
restores text and errors. |
Ah, I see now, So, by specifying As a possible workaround, I mentioned on May 24 that if the worker is prevented from being created (e.g. by removing the |
Yes. I think we don't have any ideas how to improve this any more. Except maybe document the CSP. I'll close. Thank you! |
Use of new Function in troika/packages/troika-worker-utils/src/WorkerModules.js (v0.20.0) causes CSP failure:
It requires content security directive
script-src 'unsafe-eval'
.Would there be a safer alternative for new Function so that there would be no need to allow this unsafe functionality in policy?
The text was updated successfully, but these errors were encountered: