Skip to content

Commit 1a711b2

Browse files
fix: reject a handleRequest nonce outside CSPNonce up front
Validate options.nonce before the middleware chain runs, next to the renderMode pre-check: a string, or a { script, style } pair with both keys, each a non-empty string or false. An empty value (undefined, null, '') still means no nonce. Previously scriptNonce() silently dropped any other value, leaving the tags without a nonce. Co-authored-by: Cursor <[email protected]>
1 parent 22b460e commit 1a711b2

4 files changed

Lines changed: 66 additions & 2 deletions

File tree

‎.changeset/handle-request-nonce-pair.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
'@solidjs/vite-plugin': patch
33
---
44

5-
`handleRequest(request, { nonce })` accepts the `{ script, style }` form of `@solidjs/web`'s `CSPNonce` and uses its `script` value for the scripts the handler writes: the injected client-entry tag and the post-flush redirect fallback. A pair used to throw `TypeError: value.replace is not a function`. The option is now declared in the `virtual:solid-ssr-handler` types.
5+
`handleRequest(request, { nonce })` accepts the `{ script, style }` form of `@solidjs/web`'s `CSPNonce` and uses its `script` value for the scripts the handler writes: the injected client-entry tag and the post-flush redirect fallback. A pair used to throw `TypeError: value.replace is not a function`. A value outside `CSPNonce` (anything but a string or a `{ script, style }` object with both keys, each a non-empty string or `false`) now rejects the call up front with an error naming the option, before the middleware chain runs; an empty value (`undefined`, `null` or `''`) still means no nonce. The option is now declared in the `virtual:solid-ssr-handler` types.

‎examples/start-ssr/test/run.mjs‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1520,6 +1520,43 @@ async function runProdMode() {
15201520
styleOnlyRedirect.text.includes('<script>window.location='),
15211521
styleOnlyRedirect.error,
15221522
);
1523+
// An empty nonce means none, as in the runtime.
1524+
for (const empty of [null, '']) {
1525+
const emptyPage = await settledText(() =>
1526+
builtHandler.handleRequest(new Request(origin + '/'), { nonce: empty }),
1527+
);
1528+
record(
1529+
mode,
1530+
'build',
1531+
`an empty nonce (${JSON.stringify(empty)}) leaves the client entry un-nonced`,
1532+
emptyPage.text.includes('<script type="module" src="'),
1533+
emptyPage.error,
1534+
);
1535+
}
1536+
// Anything outside CSPNonce rejects the call itself, before the chain
1537+
// runs: the host's own error, not a contained request failure.
1538+
for (const [label, invalid] of [
1539+
['a number', 42],
1540+
['an array', ['x']],
1541+
['a pair without style', { script: 'x' }],
1542+
['a misspelled key', { scirpt: 'x', style: 'y' }],
1543+
['an empty script', { script: '', style: 'y' }],
1544+
['a number as style', { script: 'x', style: 1 }],
1545+
]) {
1546+
const outcome = await builtHandler
1547+
.handleRequest(new Request(origin + '/'), { nonce: invalid })
1548+
.then(
1549+
(response) => `resolved ${response.status}`,
1550+
(error) => String(error),
1551+
);
1552+
record(
1553+
mode,
1554+
'build',
1555+
`an invalid nonce (${label}) rejects handleRequest`,
1556+
outcome.includes('handleRequest options.nonce must be'),
1557+
outcome,
1558+
);
1559+
}
15231560
record(
15241561
mode,
15251562
'build',

‎src/ssr/index.ts‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1263,6 +1263,31 @@ export function startServe(
12631263
);
12641264
}
12651265

1266+
// CSP nonce (`handleRequest(request, { nonce })`): @solidjs/web's
1267+
// `CSPNonce`, a string or a `{ script, style }` pair with both keys,
1268+
// each a non-empty string or `false`. An empty value (undefined, null or
1269+
// '') means none, as in the runtime. Anything else is rejected: projected
1270+
// as is, a typo'd key or a number would leave the tags without a nonce
1271+
// without a word.
1272+
lines.push(
1273+
``,
1274+
`function assertNonce(nonce, source) {`,
1275+
` if (nonce == null || typeof nonce === 'string') return nonce || undefined;`,
1276+
` const prototype = typeof nonce === 'object' ? Object.getPrototypeOf(nonce) : undefined;`,
1277+
` const destination = (value) => value === false || (typeof value === 'string' && value !== '');`,
1278+
` if (`,
1279+
` (prototype === Object.prototype || prototype === null) &&`,
1280+
` Object.keys(nonce).every((key) => key === 'script' || key === 'style') &&`,
1281+
` destination(nonce.script) &&`,
1282+
` destination(nonce.style)`,
1283+
` ) {`,
1284+
` return nonce;`,
1285+
` }`,
1286+
` const got = Array.isArray(nonce) ? 'an array' : prototype ? 'an object with keys ' + JSON.stringify(Object.keys(nonce)) : typeof nonce;`,
1287+
` throw new Error('[@solidjs/vite-plugin] ' + source + ' must be a string, a { script, style } object (each a non-empty string or false), or undefined; got ' + got);`,
1288+
`}`,
1289+
);
1290+
12661291
// No `_$SC` bootstrap injection: the runtime's serialized
12671292
// server-component references self-bootstrap the registry (each
12681293
// hydration script's first reference carries it as an idempotent
@@ -1500,6 +1525,7 @@ export function startServe(
15001525
// rejects up front, before the chain runs, instead of being contained
15011526
// as a request failure.
15021527
` if (options.renderMode !== undefined) assertRenderMode(options.renderMode, 'handleRequest options.renderMode');`,
1528+
` assertNonce(options.nonce, 'handleRequest options.nonce');`,
15031529
// `options.event` is the public wrapper->event extension seam: extra
15041530
// fields (conventionally `nativeEvent`, the platform's raw request
15051531
// object) spread over the event's defaults at creation, so hosts and

‎virtual-solid-manifest.d.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ declare module "virtual:solid-ssr-handler" {
5555
* CSP nonce for the scripts the handler writes into the document: the
5656
* injected client-entry tag and the fallback of a redirect decided
5757
* after the shell flushed. A `{ script, style }` pair contributes its
58-
* `script` value.
58+
* `script` value. Any value outside `CSPNonce` rejects the call before
59+
* the middleware chain runs.
5960
*/
6061
nonce?: CSPNonce;
6162
/**

0 commit comments

Comments
 (0)