fix(zod): prevent prototype injection in smart coercion#1727
Merged
Conversation
`ZodSmartCoercionPlugin` and `experimental_ZodSmartCoercionPlugin`
collected coerced object and record properties into a plain `{}`, so an
input key named `__proto__` hit the `Object.prototype` setter instead of
creating an own property.
They also looked up sub-schemas with `shape[key]`, which resolves
`constructor`, `toString` and `__proto__` through `Object.prototype`.
Those inherited values were then dereferenced as Zod schemas and threw a
`TypeError`, so any request carrying such a key failed coercion.
Collect 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! |
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.

Same two prototype chain bugs as #1726, in
ZodSmartCoercionPlugin(zod 3) andexperimental_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__'] = valuetriggers the inherited__proto__setter instead of creating an own property. This affects theobjectandrecordbranches in both plugins.Fix: collect into
NullProtoObj, the helper already used for untrusted keys inbracket-notation.tsandrpc-matcher.ts.2.
shape[key]returned inherited membersFor keys like
constructor,toStringand__proto__the lookup resolves toObject.prototypemembers, which were then dereferenced as Zod schemas: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 tocatchallas they already do for every other name.Verification
zodSmartCoercionPlugin prevents prototype injectionadded to both test files, coveringobject,recordandcatchall. Both fail without the fix. The zod coercer suites pass (583 tests).tsc -band eslint are clean.