Skip to content
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
5 changes: 5 additions & 0 deletions .changeset/fix-trusted-script-url.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@qwik.dev/partytown': patch
---

fix: serialize TrustedScript and TrustedScriptURL values as strings for the main thread
2 changes: 2 additions & 0 deletions src/lib/sandbox/main-access-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
}

Expand Down
7 changes: 4 additions & 3 deletions src/lib/web-worker/worker-serialization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
// https://developer.mozilla.org/en-US/docs/Web/API/Trusted_Types_API
return [SerializedType.Primitive, value.toString()];
} else if (value instanceof ArrayBuffer) {
Expand All @@ -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,
Expand Down Expand Up @@ -371,4 +373,3 @@ const Attr = class {
}
};

declare const TrustedHTML: any;
9 changes: 8 additions & 1 deletion tests/platform/trusted-types/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<html lang="en">
<head>
<meta charset="utf-8" />
<meta http-equiv="Content-Security-Policy" content="require-trusted-types-for 'script'; trusted-types partytown" />
<meta http-equiv="Content-Security-Policy" content="require-trusted-types-for 'script'; trusted-types partytown repro" />
<title>Trusted Types</title>
<script>partytown = { debug: true };</script>
<script src="/~partytown/debug/partytown.js"></script>
Expand All @@ -12,6 +12,13 @@
d.innerHTML = '<i>html set from worker</i>';
document.body.appendChild(d);
d.insertAdjacentHTML('beforeend', '<em>adjacent html</em>');
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');
</script>
</head>
Expand Down
1 change: 1 addition & 0 deletions tests/platform/trusted-types/sw.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

4 changes: 4 additions & 0 deletions tests/platform/trusted-types/trusted-types.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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([]);
});
Loading