Skip to content

Commit d9fdfe3

Browse files
authored
Merge pull request #3477 from solidjs/ssr-error-sanitize
fix(solid,web,signals): SSR render failures reach the client sanitized (#3468)
2 parents 1de80f4 + 9fe986b commit d9fdfe3

15 files changed

Lines changed: 660 additions & 16 deletions

File tree

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
"solid-js": patch
3+
"@solidjs/web": patch
4+
"@solidjs/signals": patch
5+
---
6+
7+
SSR render failures reach the client sanitized (#3468): the wire policy the server-function handler has applied since #3113/#3116 now covers every SSR road a failure takes — the error an `<Errored>` serializes for hydration, a rejected async source serialized into the stream, a `<Loading>` fragment's `_fr` rejection, a frame stream's error chunks (the fragment's, a live hole's, the root's). Outside the dev build a plain thrown value is replaced with a generic `Error` (`"Internal Server Error"`); `message`, `cause` and own properties stay on the server. A `"use server"` function called in-process during SSR never touches the RPC wire, so before this the production page load shipped what that wire withholds.
8+
9+
- **`solid-js`** (server): `<Errored>` sanitizes _before_ rendering its fallback and serializes the same replacement, so fallback markup and the hydration record agree. A fallback printing `err().message` shows the generic message in production, as for a server-function failure. `markSafeError` (`Symbol.for("solid.SafeError")`) passes through with own properties; an Error reached as a _value_ is data and passes as written (#3113's ruling). One replacement per original, however many roads it takes. New finding `SSR_ERROR_SANITIZED` (`info`, observe + dev; `data.error` the original), beside `SSR_RENDER_ERROR_CONTAINED` which carries the failure itself. `ssrSanitizeError` is exposed to the runtimes through `solid-js/internal`. Server findings now carry their `ownerPath` in the **observe** artifact too — the core's walk reads `_parent` under its own build's property mangling and found nothing on a server owner there, so the server entry locates its findings itself.
10+
- **`@solidjs/web`** (server): the hydration serialize funnel guards every channel it writes (a promise's rejection, an async iterable's thrown step); a fragment's terminal error reaches the `_fr` rejection and a transport sink's error chunk sanitized while the abandonment ledger keeps the original; the frame sink's root error chunk and the live-hole/live-attribute error chunks carry the replacement's message.
11+
- **`@solidjs/signals`**: the `SSR_ERROR_SANITIZED` code.
12+
13+
The dev/prod line is the build variant: the `development` server artifacts keep full fidelity; production and observe sanitize.

‎documentation/solid-2.0/08-dev-diagnostics.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,12 @@ Finding (`error`, observe + dev) plus the existing behavior: the dev build **thr
486486

487487
Finding (`error`, observe + dev). A server function threw and the non-dev wire replaced the error with the generic message (RFC 10's sanitization). The client sees the replacement; this record carries the original in `data.error`. Beside the invocation channel's `outcome: "error"` it is the one place the real failure surfaces in production. A value branded with `markSafeError` passes through and is not reported.
488488

489+
#### `SSR_ERROR_SANITIZED`
490+
491+
**Message:** "[SSR_ERROR_SANITIZED] Render error replaced with a generic Error before reaching the client: TypeError: …"
492+
493+
Finding (`info`, observe + dev; channel only). A render failure was about to reach the client through one of SSR's roads — the record an `<Errored>` serializes so the client hydrates the same fallback, a rejected async source serialized into the stream, a `<Loading>` fragment's `_fr` rejection, a frame stream's error chunk (the fragment's, a live hole's, the root's) — and the non-dev wire replaced it with the generic `Error` (`"Internal Server Error"`), the same policy the server-function wire applies (`SERVER_FN_ERROR_SANITIZED`; a `"use server"` function called in-process during SSR never touches that wire, so before this the page load leaked what the RPC withheld — [#3468](https://github.com/solidjs/solid/issues/3468)). The boundary sanitizes _before_ rendering its fallback and serializes the same replacement, so fallback markup and record agree on hydration. Advisory because the failure itself is the `SSR_RENDER_ERROR_CONTAINED` finding's, which carries the original; this is the record of what the wire carried instead, `data.error` the original, once per original however many roads it took. A value branded with `markSafeError` passes through and is not reported; an Error reached as a _value_ — never thrown — is data and passes as the author wrote it (#3113's ruling). The dev build keeps full fidelity.
494+
489495
#### `FRAME_MARKER_CORRUPTED`
490496

491497
**Message:** "[FRAME_MARKER_CORRUPTED] Frame slot range "comment#0" is missing its end marker (<!--slot:comment#0:end-->) among its start marker's siblings. …"

‎documentation/solid-2.0/10-server-functions.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ Two ways to send intentional error content in production:
113113

114114
Framework error hooks compose the same way: a `wrapInvocation`/`transformResult` override that maps a thrown error expresses intent by throwing an envelope or branding its replacement with `markSafeError` — core never second-guesses a branded value, and never trusts an unbranded one.
115115

116+
The same policy covers the SSR roads a failure takes to the client — an `<Errored>` record, a rejected async source in the stream, a fragment's rejection, a frame's error chunks — so a `"use server"` function called in-process during a render (which never touches this handler) cannot ship on the page what the RPC wire withholds; see [RFC 12](12-ssr-http.md#what-a-render-failure-looks-like-from-the-client) and `SSR_ERROR_SANITIZED` in [RFC 08](08-dev-diagnostics.md#ssr_error_sanitized). `markSafeError` is the one brand on both.
117+
116118
### Single-flight
117119

118120
The protocol folds integration data (typically revalidated route data) into a mutation’s response, saving a round trip. Core standardizes only the wire shape and delivery; what the data _is_ — a data-only render, route preloads, a cache query — is entirely the integration’s business.

‎documentation/solid-2.0/12-ssr-http.md‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,14 @@ Under streaming this implies the natural constraint: status and headers must be
134134

135135
Said plainly, `httpHeader` is a **shell-time API**. Headers declared by streamed route content — anything below a `<Loading>` boundary that resolves after the shell went out — run post-flush and are committed no-ops by contract. There is no queue that holds them for a later response; the head is on the wire. If a header matters, it belongs to the shell (or to a `deferStream`-held source that keeps the shell waiting for it).
136136

137+
### What a render failure looks like from the client
138+
139+
A render failure reaches the client on several roads: the error an `<Errored>` caught, serialized so the client hydrates the same fallback; a rejected async source serialized into the stream; a `<Loading>` fragment rejecting its `_fr` promise; a frame stream's error chunks. On every one of them the **non-dev** build hands the client a generic `Error` (`"Internal Server Error"`) in place of a plain thrown value — `message`, `cause` and own properties stay on the server — the same policy the server-function wire applies ([RFC 10](10-server-functions.md#thrown-errors-sanitized-by-default)). A `"use server"` function called in-process during a render never touches that wire, so without this the production page load leaked exactly what the RPC withheld ([#3468](https://github.com/solidjs/solid/issues/3468)).
140+
141+
The boundary sanitizes _before_ rendering its fallback, and serializes the same replacement: the fallback is rendered on the server with the error and hydrates against the record, so the two must agree. A fallback that prints `err().message` therefore shows the generic message in production, as it would for a server-function failure. `markSafeError` is the escape hatch on both wires — a branded error passes through with its own properties. An Error reached as a _value_ (never thrown — a form's field errors, say) is data and passes as written.
142+
143+
The dev/prod line is the build variant: the `development` export condition's server artifacts keep full fidelity; the production and observe artifacts sanitize. The observe tier records each replacement once as `SSR_ERROR_SANITIZED` (advisory; `data.error` the original), beside the `SSR_RENDER_ERROR_CONTAINED` finding that carries the failure itself — the server keeps the truth, the wire gets the generic.
144+
137145
### The trace the request belongs to: `getTraceContext()`
138146

139147
A distributed trace follows one user action across every service it touches; each hop needs the trace's id and the id of the span that called it, carried across HTTP by the W3C Trace Context header `traceparent` (`00-<traceId>-<parentId>-<flags>`, with `tracestate` and `baggage` beside it). The runtime reads that half of the exchange once per request and exposes it:

‎packages/signals/src/core/dev.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ export type DiagnosticCode =
7575
| "SSR_CLIENT_CONTENT_MASKED"
7676
| "LATE_HEADER_WRITE"
7777
| "SERVER_FN_ERROR_SANITIZED"
78+
| "SSR_ERROR_SANITIZED"
7879
| "SERVER_WRITE"
7980
| "REVEAL_IN_RENDER_TO_STRING"
8081
| "LAZY_ASSET_UNMAPPED"

‎packages/solid/skills/reactivity-diagnostics/SKILL.md‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ Every hold, flight and show counts here at any duration; `SILENT_HOLD` and
449449

450450
The server runtime reports on the same channel, with the same `in <App> ›
451451
<Page>` line. Two groups. **Findings** (`SSR_*`, `LATE_HEADER_WRITE`,
452-
`SERVER_FN_ERROR_SANITIZED`, `FRAME_MARKER_CORRUPTED`) are facts about a
452+
`SERVER_FN_ERROR_SANITIZED`, `SSR_ERROR_SANITIZED`, `FRAME_MARKER_CORRUPTED`) are facts about a
453453
render that exist in observe builds too — an APM sees them in production; in
454454
dev they print. **Checks** (the rest) are dev-only guidance. A captured
455455
artifact from a server render carries both.
@@ -495,6 +495,16 @@ generic message; `data.error` is the original. Fix the failure it names. If
495495
the client is meant to see this error, brand it with `markSafeError` or map
496496
it in `wrapInvocation`; do not turn sanitization off.
497497

498+
### SSR_ERROR_SANITIZED
499+
500+
A render failure reached the client — an `<Errored>` record, a rejected async
501+
source in the stream, a fragment's rejection, a frame's error chunk — and the
502+
production wire replaced it with the generic `Error`; `data.error` is the
503+
original. Advisory: the failure itself is the `SSR_RENDER_ERROR_CONTAINED`
504+
finding beside it — fix that. If the client is meant to see this error, brand
505+
it with `markSafeError`; do not turn sanitization off. A fallback that prints
506+
`err().message` shows "Internal Server Error" in production by design.
507+
498508
### FRAME_MARKER_CORRUPTED
499509

500510
(Client.) A frame's slot start marker has no end marker among its siblings:

‎packages/solid/src/index.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,10 @@ export function creationStamp(): number {
132132
export function inServerComponentScope(): boolean {
133133
return false;
134134
}
135+
/** @internal — server-only: the client has no wire to sanitize for. */
136+
export function ssrSanitizeError(value: unknown): unknown {
137+
return value;
138+
}
135139
/** @internal — server-only: on the client no value carries a trace. */
136140
export function getProjectionTrace(
137141
value: unknown

‎packages/solid/src/internal.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,16 @@ export const runInServerComponentScope: <T>(fn: () => T) => T = core.runInServer
6161
/** Server: whether a server-component scope is active. Client: `false`. */
6262
export const inServerComponentScope: () => boolean = core.inServerComponentScope;
6363

64+
/**
65+
* Server: the value the client may see in place of a render failure about to
66+
* be serialized or rendered for it — the value itself in the dev build or
67+
* when branded with `markSafeError`, else one generic `Error` per original
68+
* (recorded once as `SSR_ERROR_SANITIZED`). `subject` locates the finding;
69+
* `null` from a serialization funnel. Client: identity.
70+
*/
71+
export const ssrSanitizeError: (value: unknown, subject?: object | null) => unknown =
72+
core.ssrSanitizeError;
73+
6474
/** Server: a monotonic stamp for owner creation order. Client: `0`. */
6575
export const creationStamp: () => number = core.creationStamp;
6676

‎packages/solid/src/server/diagnostics.ts‎

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,29 @@ export function emitFinding(
3636
if (!IS_OBSERVE) return;
3737
// `OBSERVE`/`DEV` are typed optional (undefined in the tiers below theirs);
3838
// the gates above are the same conditions that define them.
39-
const entry = OBSERVE!.diagnostics.emit(finding, subject);
39+
const entry = OBSERVE!.diagnostics.emit(located(finding, subject), subject);
4040
if (IS_DEV && finding.severity !== "info") DEV!.report(entry);
4141
}
4242

43+
/**
44+
* The finding with its `ownerPath` — the labels up this entry's OWN owner
45+
* chain (`createComponentOwner`'s `<Name>`) — filled in here rather than by
46+
* the core's walk: the core reads `_parent` under its own build's property
47+
* mangling (the observe and prod artifacts rename `_`-fields; `_name` alone
48+
* is reserved as the cross-package label), so its walk finds nothing on a
49+
* server owner in the observe artifact. An `ownerPath` already on the
50+
* finding wins, as in the core.
51+
*/
52+
function located(finding: Finding, subject: DiagnosticSubject | null): Finding {
53+
if (finding.ownerPath !== undefined || !subject || !("_parent" in subject)) return finding;
54+
const path: string[] = [];
55+
for (let owner: any = subject; owner; owner = owner._parent) {
56+
const name = owner._name;
57+
if (typeof name === "string" && name.length) path.push(name);
58+
}
59+
return path.length ? { ...finding, ownerPath: path.reverse() } : finding;
60+
}
61+
4362
/**
4463
* The structured record alone, for a site that THROWS its message: the
4564
* thrown error is the console face, and the core lands the once-per-code
@@ -50,7 +69,7 @@ export function recordFinding(
5069
finding: Finding,
5170
subject: DiagnosticSubject | null = getOwner()
5271
): void {
53-
if (IS_OBSERVE) OBSERVE!.diagnostics.emit(finding, subject);
72+
if (IS_OBSERVE) OBSERVE!.diagnostics.emit(located(finding, subject), subject);
5473
}
5574

5675
/**

‎packages/solid/src/server/index.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,8 @@ export {
139139
creationStamp,
140140
runInServerComponentScope,
141141
inServerComponentScope,
142-
getProjectionTrace
142+
getProjectionTrace,
143+
ssrSanitizeError
143144
} from "./signals.js";
144145
/** @internal */
145146
export { ssrHandleError, ssrScope } from "./hydration.js";

0 commit comments

Comments
 (0)