Skip to content

Commit d8f1ea8

Browse files
authored
fix: apply nonce to streaming redirect and SPA entry scripts (#2252)
1 parent e9e8632 commit d8f1ea8

3 files changed

Lines changed: 20 additions & 1 deletion

File tree

.changeset/lucky-moths-nonce.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
"@solidjs/start": patch
3+
---
4+
5+
Apply the configured `nonce` to the two script tags that were still missing it, so a strict `script-src` CSP no longer needs `unsafe-inline`:
6+
7+
- The client-side redirect that streaming mode emits after the shell has already flushed (`<script>window.location=...</script>`) now carries the nonce.
8+
- The SPA entry script tag now carries the nonce, matching the SSR entry script.

packages/start/src/server/handler.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,10 +210,20 @@ function handleStreamCompleteRedirect(context: PageEvent) {
210210
return ({ write }: { write: (html: string) => void }) => {
211211
context.complete = true;
212212
const to = context.response && context.response.headers.get("Location");
213-
to && write(`<script>window.location=${JSON.stringify(to).replace(/</g, "\\u003c")}</script>`);
213+
if (!to) return;
214+
// The shell has already flushed, so the redirect has to happen client side.
215+
// Carry the nonce so a strict `script-src` CSP doesn't block it.
216+
const nonce = context.nonce ? ` nonce="${escapeAttribute(context.nonce)}"` : "";
217+
write(
218+
`<script${nonce}>window.location=${JSON.stringify(to).replace(/</g, "\\u003c")}</script>`,
219+
);
214220
};
215221
}
216222

223+
function escapeAttribute(value: string) {
224+
return value.replace(/&/g, "&amp;").replace(/"/g, "&quot;").replace(/</g, "&lt;");
225+
}
226+
217227
function stripBaseUrl(path: string) {
218228
const base = import.meta.env.SERVER_BASE_URL || import.meta.env.BASE_URL || "/";
219229
return stripPathBase(path, base);

packages/start/src/server/spa/StartServer.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export function StartServer(props: { document: Component<DocumentComponentProps>
3131
<PatchVirtualDevStyles nonce={nonce} />
3232
<script
3333
type="module"
34+
nonce={nonce}
3435
src={getSsrManifest("client").path(import.meta.env.START_CLIENT_ENTRY_URL)}
3536
/>
3637
</>

0 commit comments

Comments
 (0)