Skip to content

JSON: reject deeply nested input instead of overflowing the stack - #33

Merged
aaylward merged 1 commit into
mainfrom
claude/jsonrpc-task-2v9quv
Jul 7, 2026
Merged

aaylward merged 1 commit into
mainfrom
claude/jsonrpc-task-2v9quv

Conversation

@aaylward

@aaylward aaylward commented Jul 7, 2026

Copy link
Copy Markdown
Collaborator

What

Fixes an unauthenticated remote DoS. Every generated simpleRestJson and jsonRpc2 server calls smithy::json::Decode() directly on the untrusted request body (examples/*/generated/src/server.cc). A body of deeply nested arrays or objects (~20k levels) overflows the stack in nlohmann's recursive-descent parser — and again in our recursive FromBackend — and crashes the process with SIGSEGV. I confirmed the crash locally: depth 10k decodes fine, depth 20k+ segfaults.

CBOR already guards against exactly this with DecodeValue's depth counter (kMaxDepth = 64); JSON had no equivalent.

The fix adds an O(n), iterative pre-scan (ExceedsMaxDepth) that rejects input past a fixed nesting depth (512 — far beyond any legitimate document, well under the overflow threshold) before the recursive parser runs. Brackets inside strings don't nest, so the scan tracks string/escape state.

Testing

  • New RejectsDeeplyNestedInputInsteadOfStackOverflow: drives 100k-deep [ and { bombs through Decode and asserts a clean error (not a crash), plus a bracket-heavy-but-flat document that must still parse (guarding against the string-state logic over-counting).
  • Full local suite green: 52/52 (Beast targets excluded locally per the usual proxy limitation).
  • The guard runs on every decode; no existing JSON, protocol-conformance, or integration test regressed.

Found while auditing for other latent crashes in the vein of the SIGPIPE fix from the previous PR. See the PR discussion for the broader hostile-input test surface I'd suggest next.

Checklist

  • CI green
  • Regression test added and verified against the pre-fix crash
  • No wire-behavior change (rejects only pathological input no real client sends)

🤖 Generated with Claude Code

https://claude.ai/code/session_019ytv3VMrURFYP2mdWhk3un


Generated by Claude Code

Every generated simpleRestJson and jsonRpc2 server calls
smithy::json::Decode() directly on the untrusted request body. A body of
deeply nested arrays/objects (~20k levels) overflowed the stack in
nlohmann's recursive-descent parser — and again in FromBackend — and
crashed the process: an unauthenticated remote DoS. CBOR already guards
this via DecodeValue's depth counter; JSON did not.

Add an O(n), iterative pre-scan that rejects input past a fixed nesting
depth (512, far beyond any legitimate document) before the recursive
parser runs. Brackets inside strings don't nest, so the scan tracks
string/escape state. Regression test drives 100k-deep arrays and objects
through Decode and asserts a clean error, plus a bracket-heavy-but-flat
document that must still parse.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019ytv3VMrURFYP2mdWhk3un
@aaylward
aaylward merged commit dcc50bf into main Jul 7, 2026
12 checks passed
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