Bound nesting depth to prevent stack-overflow DoS (GHSA-82x6-q7mm-w9cf)#72
Merged
Conversation
Deeply nested arrays or inline tables drove the recursive-descent parser past the call-stack limit, crashing with an uncatchable RangeError (GHSA-82x6-q7mm-w9cf). A depth guard on the `value` rule now caps nesting (default 500) and raises a normal parse error instead. The limit is configurable via `toml.parse(input, { maxDepth })`. The guard pairs each increment with a decrement on both the matching and backtracking paths, so a failed value probe (e.g. after a trailing comma) cannot leak the counter and falsely reject valid input.
BinaryMuse
marked this pull request as ready for review
July 13, 2026 22:04
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.
Fixes security advisory
GHSA-82x6-q7mm-w9cf:toml.parse()on deeply nested arrays or inline tables (a ~5 KB payload of a few thousand[levels) drives the generated recursive-descent parser past Node's call-stack limit, crashing with an uncatchableRangeError. Since that's not aSyntaxError, application error handling that filters for parse errors rethrows it — a remote, unauthenticated DoS.Fix
A depth guard on the
valuerule — the single point all array/inline-table recursion funnels through — caps nesting (default 500) and raises the library's normal parse error (.line/.column, viagenError) once exceeded. The limit is configurable:toml.parse(input, { maxDepth }).The counter is leak-free under PEG backtracking. The advisory's suggested
++depth … { depth-- }snippet decrements only on the success path, so every failedvalueprobe (e.g. the one the parser backtracks over after a trailing comma) leaks an increment and can falsely reject valid input likea=[[1,],[2,],…].value_choicehere pairs the increment with a decrement on both the matching path (its action) and the non-matching path (a trailing predicate), so backtracking can't leak the counter — there's a regression test for exactly this.The compiler recurses over the same AST but overflows later than the parser (survived depth ~2,200 while the parser crashed at ~2,500), so bounding parser depth also protects it.
maxDepthdefaults to 500 — far beyond any realistic config nesting, well under the crash threshold, with headroom for smaller stacks (worker threads, reduced--stack-size). Setting it far higher re-exposes the overflow, so it's meant for lowering, not raising.