Repository navigation
Limit recursion depth on turtle imports - #1545
Merged
Merged
Conversation
bplatz
approved these changes
Jul 26, 2026
bplatz
left a comment
Contributor
There was a problem hiding this comment.
Verified locally: 106 tests pass, clippy and fmt clean. Guard placement covers every recursion cycle, flat collections don't consume depth, and the ceiling holds with room to spare on the stack sizes this actually runs on.
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.
Issue #1480 reported that the Turtle parser's recursive constructs — blank-node property lists (
[ … ]), collections (( … )), and reified triples (<< … >>) — recurse without a depth limit, so a small adversarial document can overflow the stack. A Rust stack overflow is uncatchable and aborts the whole process, making this a denial-of-service vector on any surface that parses untrusted Turtle. The bug was confirmed by reproduction before the fix: a ~1MB document of 100,000 nested property lists aborted the test process with SIGABRT.Changes
fluree-graph-turtle/src/error.rs: newpub const MAX_NESTING_DEPTH: u32 = 128, documented alongside the existingMAX_INPUT_BYTESinput bound. The three constructs share one counter, so mixed nesting is bounded too. 128 matches common practice (serde_json's default recursion limit) and is far beyond real-world Turtle nesting.fluree-graph-turtle/src/parser.rs: anesting_depthfield and awith_nestingclosure wrapper that owns the entire invariant in one place — the ceiling check (erroring with a positioned parse error past the limit), the increment, and an unconditional decrement — so call sites cannot increment without the matching decrement, and the counter stays correct even if parse-error recovery is ever added. Applied at all four recursion entry points:parse_blank_node_property_list,parse_collection,parse_collection_as_list(the object-position indexed-list variant, a fourth entry beyond the issue's list of three — caught because an unguarded outer level let collections nest one past the ceiling), andparse_reified_triple. Guarding these four bounds every recursion cycle: reified-triple nesting through the subject/object positions loops back throughparse_reified_triple, and annotation bodies are already capped at one level by the star gate.### Parser Limitssubsection indocs/transactions/turtle.mdcovering both the nesting ceiling and the pre-existing 4 GiB input bound, a resource-limits note under the Turtle entry indocs/reference/compatibility.md, and a# Limitssection in the crate docs with intra-doc links to the two constants.Tests
Five cases in the parser's test module, with single-
format!/str::repeatinput generators matching the Cypher crate's recursion-test idiom:nesting_at_the_ceiling_parses— all three constructs parse at exactly depth 128, pinning the inclusive boundary against a stricter-by-one guard regression.nesting_past_the_ceiling_errors_cleanly— all three constructs fail at depth 129 with the "maximum depth" parse error (the reified case runs through the star-capableStarSink, since the default sink rejects star forms outright).deeply_nested_property_lists_do_not_overflow_the_stack— the original 100k DoS shape returns the clean error. This is not redundant with the depth-129 case: under a regression where the ceiling check stops short-circuiting before the recursive descent, 129 frames still parse or error cleanly while only the 100k input reproduces the overflow.sibling_nesting_does_not_accumulate_depth— 512 sibling nestings (deliberately more than the 128 ceiling) parse fine, proving only the nesting chain counts.()lexes as a singleNiltoken, so nested collections must carry a real innermost item to count levels correctly.Fixes #1480