Repository navigation
fix(server): ignore malformed peer messages instead of rejecting - #2553
Conversation
A message that fails to decode made the peer adapters reject. In websocket and message-port upgrade() nothing caught it, so one malformed frame from a client could crash a Node.js server with an unhandled rejection. Decode first and ignore such messages, as v2 does.
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the
|
More templates
@orpc/ai-sdk
@orpc/arktype
@orpc/client
@orpc/contract
@orpc/experimental-durable-iterator
@orpc/hey-api
@orpc/interop
@orpc/json-schema
@orpc/nest
@orpc/openapi
@orpc/openapi-client
@orpc/otel
@orpc/experimental-pino
@orpc/experimental-publisher
@orpc/experimental-publisher-durable-object
@orpc/experimental-ratelimit
@orpc/react
@orpc/react-query
@orpc/experimental-react-swr
@orpc/server
@orpc/shared
@orpc/solid-query
@orpc/standard-server
@orpc/standard-server-aws-lambda
@orpc/standard-server-fastify
@orpc/standard-server-fetch
@orpc/standard-server-node
@orpc/standard-server-peer
@orpc/svelte-query
@orpc/tanstack-query
@orpc/trpc
@orpc/valibot
@orpc/vue-colada
@orpc/vue-query
@orpc/zod
commit: |
Deploying with
|
| Status | Name | Latest Commit | Updated (UTC) |
|---|---|---|---|
| ❌ Deployment failed View logs |
orpc | 5cc4552 | Nov 25 2026, 08:49 AM |
…ethod or headers
A request message could decode with non-string header values, such as
{ origin: 5 }, and crash flattenHeader in root interceptors like CORSPlugin
and BatchHandlerPlugin, outside the handler's error handling. Check the id,
method and headers when decoding, so the adapters ignore such messages.
There was a problem hiding this comment.
✅ No new issues found.
Approach: sound — decoding moves to the adapter boundary, so a malformed frame never reaches ServerPeer, while genuine server-side failures (failing send, unserializable output) still surface as before.
Reviewed changes — read the complete 12-file diff end to end, traced the ServerPeer → experimental_ServerPeerWithoutCodec swap against the library source, and ran the server suite, typecheck and lint.
- New decode helper —
decodeServerPeerRequestMessagewrapsdecodeRequestMessageand returnsundefinedon any decode failure; additive export on@orpc/server/standard-peer. - Four adapter refactors —
websocket,ws,bun-wsandcrosswsnow constructexperimental_ServerPeerWithoutCodecand encode responses themselves, then decode beforepeer.messageand return early on failure. This is equivalent to the oldServerPeerwrapper, which performed exactly those two steps internally (packages/standard-server-peer/src/server.ts:27-69), so request ids, abort signals, event-iterator frames and hibernation callbacks are unaffected. message-port— keeps its peer instance and wraps both decode branches (plain-objectdeserializeRequestMessageforexperimental_transfer, and binarydecodeRequestMessage) in a single try/catch.- Tests — malformed-frame coverage in all five adapters plus helper unit tests; the
ws"logs error" case was correctly re-pointed at a throwingsendso the remaining listener-level catch stays exercised, rather than deleting it along with the decode path it used to cover.
Verified locally: pnpm vitest run packages/server (58 files, 415 tests, including bun-ws and crossws), pnpm --filter @orpc/server type:check, and eslint on the touched adapter directories all pass.
deepseek-v4.1-flash-1017 (free via Pullfrog for OSS) | 𝕏
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
There was a problem hiding this comment.
✅ No new issues found.
Reviewed changes — the delta since the prior review (51f4494): one commit hardening the request codec plus its tests, no changes to the adapter work already covered.
deserializeRequestMessagevalidation — the REQUEST branch now throwsTypeError('Invalid request message')unlessidandmethodare strings and headers are a plain object whose values arestring | string[] | undefined. That closes the non-decode crash path where{"h":{"origin":5}}decoded fine and then hitflattenHeaderinside root interceptors that run outside the handler catch:CORSPlugin(packages/server/src/plugins/cors.ts:81) andBatchHandlerPlugin(packages/server/src/plugins/batch.ts:73) are the only two unguardedflattenHeadercall sites on incoming request headers, so the check targets the right shape.- Blast radius —
deserializeRequestMessageis only consumed server-side (decodeRequestMessageand the message-port object path); the client never deserializes request messages, andserializeRequestMessageonly emitsstring/string[]/undefinedheader values, so no legitimate round trip is rejected. Defaults are still applied before the check, soh: null/m: nulldegrade to empty headers /POSTinstead of being dropped. - Tests — table-driven coverage of the rejected shapes plus an acceptance case for valid header forms in
codec.test.ts, and a websocket adapter case with a numericoriginheader that would have produced a response (and failed the assertion) before the validation.
Verified: codec request-message tests pass, packages/server suite still passes (58 files / 415 tests), both package typechecks and eslint are clean. Note that on this runner (Node 24) three packages/standard-server-peer/src/server.test.ts cases fail on a 1ms File.lastModified / FormData deep-equality mismatch in the response direction — unrelated to this diff.
deepseek-v4.1-flash-1017 (free via Pullfrog for OSS) | 𝕏
… the codec The codec decodes into well-typed messages and should not police client input. Move the id, method and headers check to the server standard-peer helper, as v2 does with isClientPeerSendMessage, and use it in message-port too.
There was a problem hiding this comment.
✅ No new issues found.
Approach: sound — validation moves from the codec into the adapter boundary, which is where untrusted input enters; the decoded REQUEST payload is always a plain object by the time it is checked, so the relocation does not drop any crash coverage.
Reviewed changes — the delta since the prior pullfrog review (1b4df77): one commit relocating request validation with equivalent behavior.
- Validation relocated —
isValidServerPeerRequestMessageis now exported frompackages/server/src/adapters/standard-peer/utils.tsand covers id, method and header shapes;decodeServerPeerRequestMessageapplies it inside its try/catch, andmessage-portcalls it explicitly after decode.isStandardHeadersmoved here from the codec. - Codec reverted —
deserializeRequestMessageno longer throwsTypeError('Invalid request message'), and its table-driven test block was removed.@orpc/standard-server-peeris back to a pure decode layer, matching the PR body. - Tests moved — the rejected-shape table now sits in
utils.test.tsaroundisValidServerPeerRequestMessage(plus adecodeServerPeerRequestMessagecase for a numericoriginheader), and themessage-portadapter test gained the same malformed-header frame.
The two validation sites are equivalent for every adapter path: deserializeRequestMessage dereferences payload.u before returning, so a REQUEST payload reaching the helper is always an object and payload.method cannot throw; malformed url/missing payload still throw inside the adapter try/catch and are ignored.
Verified: pnpm vitest run packages/server/src/adapters packages/server/src/plugins (33 files / 175 tests, all five adapters plus the helper tests) and pnpm --filter @orpc/server type:check pass. The packages/standard-server-peer suite still shows the pre-existing Node 24 FormData / File.lastModified deep-equality failures in code this PR does not touch.
deepseek-v4.1-flash-1017 (free via Pullfrog for OSS) | 𝕏

A message that fails to decode (not JSON, or the wrong shape) made the peer adapters reject. In
RPCHandler.upgrade()ofwebsocketandmessage-port, nothing caught that rejection, so one malformed frame from a client could crash a Node.js server. Themessage()methods ofbun-wsandcrosswsrejected too, and the docs call them without acatch.Each adapter now decodes the message first and ignores it if it fails to decode or has the wrong shape, as v2 does. Errors from the server side (a failing
send, unserializable output) still surface as before.Some wrong shapes used to decode, such as a header value that is a number or
null. They then crashedflattenHeaderin root interceptors likeCORSPluginandBatchHandlerPlugin, which run outside the handler's error handling. Like v2'sisClientPeerSendMessage, the adapters now check the id, method and headers before handling a message.@orpc/standard-server-peeris unchanged.decodeServerPeerRequestMessageandisValidServerPeerRequestMessagetostandard-peerwebsocket,bun-ws,crossws,wsandmessage-portadaptersstandard-peer