fix(json-schema): prevent prototype injection when coercing objects#1726
Merged
Merged
Conversation
The object branch of `JsonSchemaCoercer` accumulated coerced properties
into a plain `{}`, so an input key named `__proto__` hit the
`Object.prototype` setter instead of creating an own property: the key
was dropped from the output and the attacker-supplied object became the
prototype of the coerced value.
It also looked up sub-schemas with `schema.properties?.[key]`, which
resolves `constructor`, `toString` and `__proto__` through
`Object.prototype`. Those inherited values were then used as schemas,
which coerce nothing and report `satisfied: true`, letting such keys
bypass the unknown-property path (and, in a union, select the wrong
branch).
Accumulate into `NullProtoObj` and guard the lookup with
`Object.hasOwn`.
Contributor
|
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
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: |
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
dinwwwh
added a commit
that referenced
this pull request
Jul 25, 2026
Same two prototype chain bugs as #1726, in `ZodSmartCoercionPlugin` (zod 3) and `experimental_ZodSmartCoercionPlugin` (zod 4). Both plugins coerce untrusted input before validation, so the keys below are attacker controlled. ### 1. Coerced properties were collected into a plain `{}` `newObj['__proto__'] = value` triggers the inherited `__proto__` setter instead of creating an own property. This affects the `object` and `record` branches in both plugins. Fix: collect into `NullProtoObj`, the helper already used for untrusted keys in `bracket-notation.ts` and `rpc-matcher.ts`. ### 2. `shape[key]` returned inherited members For keys like `constructor`, `toString` and `__proto__` the lookup resolves to `Object.prototype` members, which were then dereferenced as Zod schemas: ``` schema: z.object({ a: z.number() }) input: { a: '123', constructor: '456' } zod 3 => TypeError: Cannot read properties of undefined (reading 'Symbol(ORPC_CUSTOM_ZOD_DEF)') zod 4 => TypeError: Cannot read properties of undefined (reading 'def') ``` The throw happens before the assignment, so in practice these keys made coercion fail rather than injecting a prototype. Any client could break a request this way. Fix: guard the lookup with `Object.hasOwn`, so unknown keys fall through to `catchall` as they already do for every other name. ### Verification `zodSmartCoercionPlugin prevents prototype injection` added to both test files, covering `object`, `record` and `catchall`. Both fail without the fix. The zod coercer suites pass (583 tests). `tsc -b` and eslint are clean.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Fixes two prototype chain bugs in the
objectbranch ofJsonSchemaCoercer#coerce. The coercer runs on untrusted input before validation (SmartCoercionPlugin), so the keys below are attacker controlled.1. Coerced properties were collected into a plain
{}coercedItems['__proto__'] = valuetriggers the inherited__proto__setter instead of creating an own property:Object.prototypeis not touched, but the returned value inherits attacker controlled properties, and anything reading them through the prototype chain sees them.Fix: collect into
NullProtoObj, the helper already used for untrusted keys inbracket-notation.tsandrpc-matcher.ts.2.
schema.properties?.[key]returned inherited membersFor keys like
constructor,toStringand__proto__the lookup resolves toObject.prototypemembers, which were then used as sub-schemas. They coerce nothing and reportsatisfied: true, so those keys skipped the unknown property path and selected the wrong union branch:Fix: guard the lookup with
Object.hasOwn.Verification
prevents prototype injection when coercing objectscovers both cases and fails without the fix. The json-schema, openapi and openapi-client suites pass (412 tests).tsc -band eslint are clean.