Repository navigation
fix: bound array and inline table nesting depth to prevent stack-overflow DoS - #1092
Merged
Merged
Conversation
parseVal dispatches '[' to parseValArray and '{' to parseInlineTable,
each of which loops back into parseVal for their elements. This mutual
recursion had no depth limit, so a document with deeply nested arrays or
inline tables (e.g. "a=" followed by millions of '[') drove the parser
to overflow the goroutine stack.
A stack overflow is raised by runtime.throw, not panic, so no recover()
can intercept it: the entire process dies, taking down every goroutine.
Any service decoding attacker-controlled TOML with toml.Unmarshal or
toml.Decoder.Decode was therefore remotely crashable with a single
multi-megabyte request.
Track the current nesting depth on the Parser and reject documents that
nest arrays and inline tables beyond maxValueNesting (10000, matching
encoding/json) with a ParserError. Because both the seen-tracker and the
unmarshaler only ever recurse over an AST the parser produced, capping
the parser bounds every downstream recursion as well.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014ubqgLjdqASwaW2zNKkqRU
The existing regression tests exercise the guard via the unstable Parser. Add a test that drives it through the public toml.Unmarshal and toml.Decoder.Decode entry points an attacker actually reaches, for both deeply nested arrays and inline tables, and confirms modestly nested input still decodes. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_014ubqgLjdqASwaW2zNKkqRU
The stable Unmarshal/Decode tests already exercise the nesting-depth guard end-to-end, so the equivalent unstable.Parser tests are redundant. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_014ubqgLjdqASwaW2zNKkqRU
1 task
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.
Summary
toml.Unmarshalandtoml.Decoder.Decodecan be driven to afatal error: stack overflowby a document with deeply nested arrays or inline tables. In the parser,parseValdispatches[toparseValArrayand{toparseInlineTable, each of which loops back intoparseValfor their elements. This mutual recursion had no depth limit.A stack overflow is raised by
runtime.throw, notpanic, so norecover()can intercept it โ the entire process dies, taking every goroutine with it. Any service decoding attacker-controlled TOML is remotely crashable with a single multi-megabyte request (~5โ6 MB suffices under Go's default 1 GiB goroutine-stack ceiling).I confirmed this reproduces on current
v2HEAD before fixing:Fix
Track the current nesting depth on the
Parserand reject documents that nest arrays and inline tables beyondmaxValueNesting(10000, matching the limitencoding/jsonuses) with a regularParserError.The parser is the single choke point: both decode paths route container values through
parseVal(the AST path viaNextExpression/Expression, and the fused generic path viaparserbridge.ParseValue). The seen-tracker (internal/tracker) and the unmarshaler only ever recurse over an AST the parser already produced, and each pass runs sequentially โ fully unwinding before the next โ so peak stack is bounded by parser depth. Capping the parser therefore bounds every downstream recursion as well, with no API change.The depth check happens before the increment, so a document exactly at the limit still parses; only input beyond it is rejected.
Testing
TestParserValueNestingLimit(arrays + inline tables) asserts over-nested input yields a*ParserErrorinstead of crashing.TestParserValueNestingAtLimitasserts input nested exactly to the limit still parses โ the guard doesn't reject valid documents.toml.Unmarshalintointerface{}: the 3-million-deep PoC now returns a clean error, depth 10000 decodes, depth 10001 errors.go test ./...passes;gofmtandgo vetare clean.Note
The OSS-Fuzz harness caps input at 2048 bytes (
ossfuzz/fuzz.go), well below the multi-MB payload needed to trigger this, which is likely why continuous fuzzing never surfaced it. Raising or removing that cap would let fuzzing catch this class of regression, but I left it out of this PR to keep the change focused on the fix.๐ค Generated with Claude Code
Generated by Claude Code