JSON: reject deeply nested input instead of overflowing the stack - #33
Merged
Merged
Conversation
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
4 tasks
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.
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 recursiveFromBackend— 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
RejectsDeeplyNestedInputInsteadOfStackOverflow: drives 100k-deep[and{bombs throughDecodeand 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).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
🤖 Generated with Claude Code
https://claude.ai/code/session_019ytv3VMrURFYP2mdWhk3un
Generated by Claude Code