Skip to content
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

Trusted Types compatibility for Emscripten threads #14962

Merged
merged 9 commits into from
Sep 9, 2021
28 changes: 27 additions & 1 deletion src/library_pthread.js
Original file line number Diff line number Diff line change
Expand Up @@ -402,8 +402,24 @@ var LibraryPThread = {
#if PTHREADS_DEBUG
out('Allocating a new web worker from ' + new URL('{{{ PTHREAD_WORKER_FILE }}}', import.meta.url));
#endif
// Use bundler-friendly `new Worker(new URL(..., import.meta.url))` pattern; works in browsers too.
#if TRUSTED_TYPES
// Use Trusted Types compatible wrappers.
if (typeof trustedTypes !== 'undefined' && trustedTypes.createPolicy) {
var p = trustedTypes.createPolicy(
'emscripten#workerPolicy1',
{
createScriptURL: function(ignored) {
return new URL('{{{ PTHREAD_WORKER_FILE }}}', import.meta.url);
}
}
);
PThread.unusedWorkers.push(new Worker(p.createScriptURL('ignored')));
} else {
PThread.unusedWorkers.push(new Worker(new URL('{{{ PTHREAD_WORKER_FILE }}}', import.meta.url)));
aaronshim marked this conversation as resolved.
Show resolved Hide resolved
}
#else
PThread.unusedWorkers.push(new Worker(new URL('{{{ PTHREAD_WORKER_FILE }}}', import.meta.url)));
#endif
return;
}
#endif
Expand All @@ -415,7 +431,17 @@ var LibraryPThread = {
#if PTHREADS_DEBUG
out('Allocating a new web worker from ' + pthreadMainJs);
#endif
#if TRUSTED_TYPES
// Use Trusted Types compatible wrappers.
if (typeof trustedTypes !== 'undefined' && trustedTypes.createPolicy) {
var p = trustedTypes.createPolicy('emscripten#workerPolicy2', { createScriptURL: function(ignored) { return pthreadMainJs } });
PThread.unusedWorkers.push(new Worker(p.createScriptURL('ignored')));
} else {
PThread.unusedWorkers.push(new Worker(pthreadMainJs));
}
#else
PThread.unusedWorkers.push(new Worker(pthreadMainJs));
#endif
},

getNewWorker: function() {
Expand Down
6 changes: 6 additions & 0 deletions src/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -1951,6 +1951,12 @@ var AUTOLOAD_DYLIBS = 1;
// though these syscalls will fail (or do nothing) at runtime.
var ALLOW_UNIMPLEMENTED_SYSCALLS = 1;

// Allow calls to Worker(...) and importScripts(...) to be Trusted Types compatible.
// Trusted Types is a Web Platform feature designed to mitigate DOM XSS by restricting
// the usage of DOM sink APIs. See https://w3c.github.io/webappsec-trusted-types/.
// [link]
var TRUSTED_TYPES = 0;

//===========================================
// Internal, used for testing only, from here
//===========================================
Expand Down
18 changes: 18 additions & 0 deletions src/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,28 @@ self.onmessage = function(e) {
});
#else
if (typeof e.data.urlOrBlob === 'string') {
#if TRUSTED_TYPES
if (typeof self.trustedTypes !== 'undefined' && self.trustedTypes.createPolicy) {
var p = self.trustedTypes.createPolicy('emscripten#workerPolicy3', { createScriptURL: function(ignored) { return e.data.urlOrBlob } });
importScripts(p.createScriptURL('ignored'));
} else {
importScripts(e.data.urlOrBlob);
}
#else
importScripts(e.data.urlOrBlob);
#endif
} else {
var objectUrl = URL.createObjectURL(e.data.urlOrBlob);
#if TRUSTED_TYPES
if (typeof self.trustedTypes !== 'undefined' && self.trustedTypes.createPolicy) {
var p = self.trustedTypes.createPolicy('emscripten#workerPolicy3', { createScriptURL: function(ignored) { return objectUrl } });
importScripts(p.createScriptURL('ignored'));
} else {
importScripts(objectUrl);
}
#else
importScripts(objectUrl);
#endif
URL.revokeObjectURL(objectUrl);
}
#if MODULARIZE
Expand Down