Skip to content

Trusted Types compatibility for Emscripten threads #14962

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

Merged
merged 9 commits into from
Sep 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion src/library_pthread.js
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,20 @@ 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
#endif
PThread.unusedWorkers.push(new Worker(new URL('{{{ PTHREAD_WORKER_FILE }}}', import.meta.url)));
return;
}
Expand All @@ -414,6 +427,13 @@ var LibraryPThread = {
#endif
#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
#endif
PThread.unusedWorkers.push(new Worker(pthreadMainJs));
},
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
12 changes: 12 additions & 0 deletions src/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,21 @@ 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
#endif
importScripts(e.data.urlOrBlob);
} 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
#endif
importScripts(objectUrl);
URL.revokeObjectURL(objectUrl);
}
Expand Down