diff --git a/.changeset/fix-trusted-script-url.md b/.changeset/fix-trusted-script-url.md new file mode 100644 index 00000000..f6fe5226 --- /dev/null +++ b/.changeset/fix-trusted-script-url.md @@ -0,0 +1,5 @@ +--- +'@qwik.dev/partytown': patch +--- + +fix: serialize TrustedScript and TrustedScriptURL values as strings for the main thread diff --git a/src/lib/sandbox/main-access-handler.ts b/src/lib/sandbox/main-access-handler.ts index 10fe25e7..496430c8 100644 --- a/src/lib/sandbox/main-access-handler.ts +++ b/src/lib/sandbox/main-access-handler.ts @@ -215,6 +215,8 @@ const applyToInstance = ( args[1] = trustedType('createHTML', args[1]); } else if (previous === 'write' || previous === 'writeln') { args = args.map((arg) => trustedType('createHTML', arg)); + } else if (previous === 'register') { + args[0] = trustedType('createScriptURL', args[0]); } } diff --git a/src/lib/web-worker/worker-serialization.ts b/src/lib/web-worker/worker-serialization.ts index 7d57e651..233fa226 100644 --- a/src/lib/web-worker/worker-serialization.ts +++ b/src/lib/web-worker/worker-serialization.ts @@ -66,7 +66,7 @@ export const serializeForMain = ( SerializedType.Event, serializeObjectForMain($winId$, $instanceId$, value, false, added), ]; - } else if (supportsTrustedHTML && value instanceof TrustedHTML) { + } else if (trustedTypes.some((T) => value instanceof T)) { // https://developer.mozilla.org/en-US/docs/Web/API/Trusted_Types_API return [SerializedType.Primitive, value.toString()]; } else if (value instanceof ArrayBuffer) { @@ -87,7 +87,9 @@ export const serializeForMain = ( } }; -const supportsTrustedHTML = typeof TrustedHTML === 'function'; +const trustedTypes = ['TrustedHTML', 'TrustedScript', 'TrustedScriptURL'] + .map((n) => (globalThis as any)[n]) + .filter((T) => typeof T === 'function'); const serializeObjectForMain = ( winId: WinId, @@ -371,4 +373,3 @@ const Attr = class { } }; -declare const TrustedHTML: any; diff --git a/tests/platform/trusted-types/index.html b/tests/platform/trusted-types/index.html index 2e34d288..9754c760 100644 --- a/tests/platform/trusted-types/index.html +++ b/tests/platform/trusted-types/index.html @@ -2,7 +2,7 @@ - + Trusted Types @@ -12,6 +12,13 @@ d.innerHTML = 'html set from worker'; document.body.appendChild(d); d.insertAdjacentHTML('beforeend', 'adjacent html'); + var policy = trustedTypes.createPolicy('repro', { createScriptURL: (v) => v }); + var url = policy.createScriptURL('/script-url.js'); + document.getElementById('out').setAttribute('data-url', url); + navigator.serviceWorker.register(policy.createScriptURL('/tests/platform/trusted-types/sw.js')).then( + () => document.getElementById('out').setAttribute('data-sw', 'registered'), + (e) => document.getElementById('out').setAttribute('data-sw', String(e)) + ); document.body.classList.add('completed'); diff --git a/tests/platform/trusted-types/sw.js b/tests/platform/trusted-types/sw.js new file mode 100644 index 00000000..8b137891 --- /dev/null +++ b/tests/platform/trusted-types/sw.js @@ -0,0 +1 @@ + diff --git a/tests/platform/trusted-types/trusted-types.spec.ts b/tests/platform/trusted-types/trusted-types.spec.ts index a4c23471..23232293 100644 --- a/tests/platform/trusted-types/trusted-types.spec.ts +++ b/tests/platform/trusted-types/trusted-types.spec.ts @@ -15,6 +15,10 @@ test('trusted types', async ({ page }) => { // html set from the worker must pass Trusted Types enforcement await expect(page.locator('i')).toHaveText('html set from worker'); await expect(page.locator('em')).toHaveText('adjacent html'); + // TrustedScriptURL must reach the main thread as its string value + await expect(out).toHaveAttribute('data-url', '/script-url.js'); + // a string script url must be re-wrapped for the enforced register() sink + await expect(out).toHaveAttribute('data-sw', 'registered'); expect(errors).toEqual([]); });