From 281f7cd3e57956633604e3940d94a65ee40c713b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89verton=20Toffanetto?= Date: Mon, 12 Oct 2026 20:43:08 -0300 Subject: [PATCH] fix(web): scriptNonce and styleNonce reach the client entry and public types Both helpers only existed in the server build. Their declarations live in types/server.d.ts, which no export condition of "." points at, and dist/web.js does not export them, so importing them from "@solidjs/web" failed to typecheck and broke the client bundle of a document rendered on both sides. They move to src/nonce.ts and both entries re-export that one implementation, as they already do with the cookie codec. The signatures accept null to match the CSPNonce | null that @solidjs/vite-plugin declares for event.nonce. --- .changeset/client-nonce-helpers.md | 5 +++ packages/web/src/client.ts | 3 ++ packages/web/src/nonce.ts | 26 ++++++++++++ packages/web/src/server-mock.ts | 4 ++ packages/web/src/server.ts | 22 ++-------- .../web/test/runtime/nonce-client.spec.ts | 40 +++++++++++++++++++ 6 files changed, 81 insertions(+), 19 deletions(-) create mode 100644 .changeset/client-nonce-helpers.md create mode 100644 packages/web/src/nonce.ts create mode 100644 packages/web/test/runtime/nonce-client.spec.ts diff --git a/.changeset/client-nonce-helpers.md b/.changeset/client-nonce-helpers.md new file mode 100644 index 0000000000..d494f30eaf --- /dev/null +++ b/.changeset/client-nonce-helpers.md @@ -0,0 +1,5 @@ +--- +"@solidjs/web": patch +--- + +`scriptNonce` and `styleNonce` are exported from the client entry and the public types, as one implementation both entries share. They only existed in the server build, so `import { scriptNonce } from "@solidjs/web"` failed to typecheck and broke client bundles. Both also accept `null`, matching the `CSPNonce | null` that `@solidjs/vite-plugin` declares for `event.nonce`. diff --git a/packages/web/src/client.ts b/packages/web/src/client.ts index 2e2bd78496..0799796f43 100644 --- a/packages/web/src/client.ts +++ b/packages/web/src/client.ts @@ -335,6 +335,9 @@ export const getTraceContext: () => TraceContext | undefined = voidFn; // it, so it costs a bundle exactly what user code asks for and // tree-shakes away otherwise (guarded in scripts/size-guard.mjs). export { parseCookieHeader, serializeCookie } from "./cookies.js"; +// The CSP nonce projection (see nonce.js): the REAL implementation, shared +// with the server entry, so a document rendered on both sides can call it. +export { scriptNonce, styleNonce } from "./nonce.js"; // The flash cookie's isomorphic half (name/detection/clearing — cookie // utilities, see cookies.js) and the codec-free server-function layer // (detection + the late-bound RPC seam, see server-functions/registry.js). diff --git a/packages/web/src/nonce.ts b/packages/web/src/nonce.ts new file mode 100644 index 0000000000..d5f14b0193 --- /dev/null +++ b/packages/web/src/nonce.ts @@ -0,0 +1,26 @@ +// CSP nonce projection: one destination's half of a render `nonce`, for the +// surfaces that take a single nonce string. Pure and isomorphic, so both +// entries export this one implementation, like the cookie codec: a document +// rendered on both sides can call it, and client builds that don't call it +// tree-shake it away. +import type { CSPNonce } from "./server-mock.js"; + +function destinationNonce( + nonce: CSPNonce | null | undefined, + destination: "script" | "style" +): string | undefined { + if (nonce == null) return undefined; + if (typeof nonce === "string") return nonce || undefined; + const value = nonce[destination]; + return typeof value === "string" && value ? value : undefined; +} + +/** The script-destination half of a render `nonce`. */ +export function scriptNonce(nonce?: CSPNonce | null): string | undefined { + return destinationNonce(nonce, "script"); +} + +/** The style-destination half of a render `nonce`. */ +export function styleNonce(nonce?: CSPNonce | null): string | undefined { + return destinationNonce(nonce, "style"); +} diff --git a/packages/web/src/server-mock.ts b/packages/web/src/server-mock.ts index b72f5f460e..9a02d0fdcd 100644 --- a/packages/web/src/server-mock.ts +++ b/packages/web/src/server-mock.ts @@ -144,6 +144,10 @@ export type AssetResolverFn = ( * CSP nonce for the tags a server render emits. A string applies to both * nonce-aware destinations; a `{ script, style }` pair routes each tag to * its destination's nonce, with `false` leaving that destination un-nonced. + * + * Only `renderToString` / `renderToStream` take this shape. Surfaces that + * emit one script (`HydrationScript`, `generateHydrationScript`, + * `createSSRResponse`) take a string; project with `scriptNonce`. */ export type CSPNonce = string | { script: string | false; style: string | false }; diff --git a/packages/web/src/server.ts b/packages/web/src/server.ts index e1bd7fae81..44b3f1a1c0 100644 --- a/packages/web/src/server.ts +++ b/packages/web/src/server.ts @@ -73,6 +73,9 @@ export { getServerFunctionRPC, isServerFunction } from "../server-functions/src/registry.js"; +// The CSP nonce projection, shared with the client entry (see nonce.js). +import { scriptNonce } from "./nonce.js"; +export { scriptNonce, styleNonce } from "./nonce.js"; import { HEAD_ELIGIBLE_TAGS, HEAD_ATTR_NAME, @@ -1425,25 +1428,6 @@ function normalizeNonce(nonce) { }; } -function destinationNonce(nonce, destination) { - if (nonce == null) return undefined; - if (typeof nonce === "string") return nonce || undefined; - const value = nonce[destination]; - return typeof value === "string" && value ? value : undefined; -} /** The script-destination half of a render `nonce`. */ -export function scriptNonce(nonce?: CSPNonce): string | undefined; - -/** The script-destination half of a render `nonce`. */ -export function scriptNonce(nonce) { - return destinationNonce(nonce, "script"); -} /** The style-destination half of a render `nonce`. */ -export function styleNonce(nonce?: CSPNonce): string | undefined; - -/** The style-destination half of a render `nonce`. */ -export function styleNonce(nonce) { - return destinationNonce(nonce, "style"); -} - // Attribute names are ASCII case-insensitive, so a caller-supplied `Nonce` // counts as one too. function hasNonceProp(props) { diff --git a/packages/web/test/runtime/nonce-client.spec.ts b/packages/web/test/runtime/nonce-client.spec.ts new file mode 100644 index 0000000000..426ceac8b0 --- /dev/null +++ b/packages/web/test/runtime/nonce-client.spec.ts @@ -0,0 +1,40 @@ +/** + * @vitest-environment jsdom + * + * The CSP nonce projection on the client entry: the REAL implementation, + * shared with the server entry, never a stub. A document rendered on both + * sides calls it with the request event's nonce on the server and with + * nothing on the client. Identity against the server entry pins "one + * implementation, two entries"; the table pins the projection itself, + * `null` included (the type the vite plugin gives `event.nonce`). + */ +import { describe, expect, it } from "vitest"; +import { scriptNonce, styleNonce } from "../../src/index.js"; +import * as server from "../../src/server.js"; + +describe("CSP nonce projection on the client entry", () => { + it("is the same implementation the server entry exports", () => { + expect(scriptNonce).toBe(server.scriptNonce); + expect(styleNonce).toBe(server.styleNonce); + }); + + it("projects each destination's half of a render nonce", () => { + const cases = [ + ["n", "n", "n"], + [{ script: "s", style: "t" }, "s", "t"], + [{ script: "s", style: false }, "s", undefined], + [{ script: false, style: "t" }, undefined, "t"], + [{ script: false, style: false }, undefined, undefined], + ["", undefined, undefined], + [{ script: "", style: "" }, undefined, undefined], + [null, undefined, undefined], + [undefined, undefined, undefined] + ] as const; + + for (const [nonce, script, style] of cases) { + expect(scriptNonce(nonce)).toBe(script); + expect(styleNonce(nonce)).toBe(style); + } + expect(scriptNonce()).toBeUndefined(); + }); +});