Skip to content

fix: bound array and inline table nesting depth to prevent stack-overflow DoS - #1092

Merged
pelletier merged 3 commits into
v2from
claude/go-toml-recursion-fix-h0anpj
Jul 5, 2026
Merged

pelletier merged 3 commits into
v2from
claude/go-toml-recursion-fix-h0anpj

Conversation

@pelletier

Copy link
Copy Markdown
Owner

Summary

toml.Unmarshal and toml.Decoder.Decode can be driven to a fatal error: stack overflow by a document with deeply nested arrays or inline tables. In the parser, parseVal dispatches [ to parseValArray and { to parseInlineTable, each of which loops back into parseVal for their elements. This mutual recursion had no depth limit.

A stack overflow is raised by runtime.throw, not panic, so no recover() 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 v2 HEAD before fixing:

payload := "a=" + strings.Repeat("[", 3000000) + strings.Repeat("]", 3000000)
toml.Unmarshal([]byte(payload), &v)
// runtime: goroutine stack exceeds 1000000000-byte limit
// fatal error: stack overflow
//   unstable.(*Parser).parseValArray(...) parser.go:726
//   unstable.(*Parser).parseVal(...)      parser.go:517
//   ... (repeats)

Fix

Track the current nesting depth on the Parser and reject documents that nest arrays and inline tables beyond maxValueNesting (10000, matching the limit encoding/json uses) with a regular ParserError.

The parser is the single choke point: both decode paths route container values through parseVal (the AST path via NextExpression/Expression, and the fused generic path via parserbridge.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

  • New TestParserValueNestingLimit (arrays + inline tables) asserts over-nested input yields a *ParserError instead of crashing.
  • New TestParserValueNestingAtLimit asserts input nested exactly to the limit still parses โ€” the guard doesn't reject valid documents.
  • Verified end-to-end through toml.Unmarshal into interface{}: the 3-million-deep PoC now returns a clean error, depth 10000 decodes, depth 10001 errors.
  • Full go test ./... passes; gofmt and go vet are 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

claude added 3 commits July 5, 2026 00:20
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Issues describing a bug in go-toml.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants