Skip to content

Limit recursion depth on turtle imports - #1545

Merged
zonotope merged 3 commits into
mainfrom
fix/turtle-depth-limit
Jul 27, 2026
Merged

zonotope merged 3 commits into
mainfrom
fix/turtle-depth-limit

Conversation

@zonotope

Copy link
Copy Markdown
Contributor

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: new pub const MAX_NESTING_DEPTH: u32 = 128, documented alongside the existing MAX_INPUT_BYTES input 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: a nesting_depth field and a with_nesting closure 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), and parse_reified_triple. Guarding these four bounds every recursion cycle: reified-triple nesting through the subject/object positions loops back through parse_reified_triple, and annotation bodies are already capped at one level by the star gate.
  • Documentation: a ### Parser Limits subsection in docs/transactions/turtle.md covering both the nesting ceiling and the pre-existing 4 GiB input bound, a resource-limits note under the Turtle entry in docs/reference/compatibility.md, and a # Limits section 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::repeat input 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-capable StarSink, 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.
  • One lexer subtlety the generators encode: bare () lexes as a single Nil token, so nested collections must carry a real innermost item to count levels correctly.

Fixes #1480

@zonotope
zonotope requested review from aaj3f and bplatz July 24, 2026 16:16

@bplatz bplatz left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@zonotope
zonotope merged commit 5e70f4e into main Jul 27, 2026
20 of 21 checks passed
@zonotope
zonotope deleted the fix/turtle-depth-limit branch July 27, 2026 15:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

parser(turtle): recursion depth ceiling for nested constructs

2 participants