Skip to content

fix(web): reject Set and Map before superjson deserializes tRPC input - #763

Merged
adriangohjw merged 3 commits into
mainfrom
fix/superjson-set-map-dos
Nov 16, 2026
Merged

adriangohjw merged 3 commits into
mainfrom
fix/superjson-set-map-dos

Conversation

@adriangohjw

Copy link
Copy Markdown
Contributor

Summary

  • superjson 2.2.5 rebuilds a Set or Map by walking getNthKey once per path, so a crafted tRPC body is O(N^2) inside input.deserialize, before auth or zod.
  • The input transformer now rejects an annotation whose type is set or map with BAD_REQUEST. Dates, bigints, undefined, and shared references still deserialize. Output is unchanged.
  • No procedure in this app accepts a Set or Map as input.

Test plan

  • pnpm exec vitest run src/server/api/__tests__/trpc.test.ts in apps/web
  • An 8,000-element Set plus the same number of referential-equality paths is rejected in under 100ms
  • A set annotation in the first array child, and one nested past the first two children, are both rejected
  • A serialized root Set and a nested Map are rejected
  • A payload with a Date, a bigint, undefined, and a shared reference still round-trips

Made with Cursor

A crafted annotation walks getNthKey in O(N^2) before auth or zod. No procedure accepts those collections.

Co-authored-by: Cursor <cursoragent@cursor.com>
@vercel

vercel Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
starter-kit Ready Ready Preview Nov 16, 2026 8:57am UTC

Request Review

@datadog-opengovsg

datadog-opengovsg Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

🎯 Code Coverage (details)
• Patch Coverage: 100.00%
• Overall Coverage: 61.69% (+1.91%)

This comment will be updated automatically if new data arrives.
🔗 Commit SHA: 6a1dda4 | Docs | Give us feedback!

Co-authored-by: Cursor <cursoragent@cursor.com>
@adriangohjw
adriangohjw marked this pull request as ready for review November 16, 2026 08:40
Copilot AI lite review requested due to automatic review settings November 16, 2026 08:40

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The new DoS guard remains bypassable because unvalidated request-controlled metadata can trigger an unhandled exception or recursive stack overflow, requiring validation before traversing its values.

1 open issue
What changed in this PR

This pull request hardens tRPC input deserialization by rejecting serialized Set and Map annotations before SuperJSON performs potentially quadratic reconstruction, while preserving supported values and unchanged output behavior.

Changes:

  • Adds pre-deserialization detection for Set and Map annotations and rejects them with BAD_REQUEST.
  • Preserves deserialization of dates, bigints, undefined, and shared references.
  • Adds regression coverage for nested annotations, malicious payloads, performance protection, and supported values.
File Description
apps/​web/​src/​server/​api/​trpc.ts Adds guarded input transformation that scans SuperJSON metadata and rejects serialized Set and Map values before deserialization.
apps/​web/​src/​server/​api/​__tests__/​trpc.test.ts Adds tests covering Set and Map rejection, nested metadata, performance protection, and supported SuperJSON value round-tripping.

Review effort: Lite

Comment thread apps/web/src/server/api/trpc.ts Outdated
Comment on lines +88 to +92
(children !== undefined && Object.values(children).some(hasSetOrMap))
)
}

return Object.values(tree).some(hasSetOrMap)
A null children slot threw from Object.values, and a string annotation recursed until the stack overflowed, before deserialize ran.

Co-authored-by: Cursor <cursoragent@cursor.com>
@adriangohjw
adriangohjw merged commit 03ec2f0 into main Nov 16, 2026
15 checks passed
@adriangohjw
adriangohjw deleted the fix/superjson-set-map-dos branch November 16, 2026 09:01

This branch was successfully deployed

1 active deployment
Preview — 6a1dda42 Deployed Nov 16, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants