Skip to content
Closed
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/client-nonce-helpers.md
Original file line number Diff line number Diff line change
@@ -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`.
3 changes: 3 additions & 0 deletions packages/web/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
26 changes: 26 additions & 0 deletions packages/web/src/nonce.ts
Original file line number Diff line number Diff line change
@@ -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");
}
4 changes: 4 additions & 0 deletions packages/web/src/server-mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };

Expand Down
22 changes: 3 additions & 19 deletions packages/web/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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) {
Expand Down
40 changes: 40 additions & 0 deletions packages/web/test/runtime/nonce-client.spec.ts
Original file line number Diff line number Diff line change
@@ -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();
});
});
Loading