From f3eb8e5bfc4516a06d6e3210bd382034a0aef054 Mon Sep 17 00:00:00 2001 From: Aditya Mukhopadhyay Date: Sat, 22 Aug 2026 15:59:05 +0530 Subject: [PATCH 1/2] fix(datalog): bound parser recursion depth to prevent stack overflow (#321) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Parser::parse_value mutually recurses through parse_list/parse_vector/ parse_map with no depth tracking, so deeply nested or unbalanced input (e.g. thousands of unclosed parens) exhausts the stack and aborts the process instead of returning a parse error. The nightly datalog_parser fuzzer found this via a malformed, multi-level-nested query string. Add a depth counter to Parser, checked on every parse_value call, that returns a graceful error once nesting exceeds 100 levels — well beyond any legitimate query, per the existing MAX_STRING_LENGTH-style limits already used in this parser. Fixes #321 Co-Authored-By: Claude Sonnet 5 Claude-Session: https://claude.ai/code/session_01YMWSutMLJJ7Y5b8hs9ZaCU --- .../seed_deep_nesting_recursion.bin | Bin 0 -> 154 bytes src/query/datalog/parser.rs | 30 +++++++++++++++++- 2 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 fuzz/corpus/datalog_parser/seed_deep_nesting_recursion.bin diff --git a/fuzz/corpus/datalog_parser/seed_deep_nesting_recursion.bin b/fuzz/corpus/datalog_parser/seed_deep_nesting_recursion.bin new file mode 100644 index 0000000000000000000000000000000000000000..314b892525013d05db301f646b645e875dec746b GIT binary patch literal 154 zcmb1f_jdH~7m2niOU%hk(M>E-a*P22tF+7q>$XFX1{mlf_&OLO8c4DlsR~wkiMa}? y_Ia^Tm0, pos: usize, + depth: usize, } impl Parser { fn new(tokens: Vec) -> Self { - Parser { tokens, pos: 0 } + Parser { + tokens, + pos: 0, + depth: 0, + } } fn peek(&self) -> Option<&Token> { @@ -362,6 +368,20 @@ impl Parser { } fn parse_value(&mut self) -> Result { + self.depth += 1; + if self.depth > MAX_RECURSION_DEPTH { + self.depth -= 1; + return Err(format!( + "Exceeded maximum recursion depth of {}", + MAX_RECURSION_DEPTH + )); + } + let result = self.parse_value_inner(); + self.depth -= 1; + result + } + + fn parse_value_inner(&mut self) -> Result { match self.peek() { Some(Token::LeftParen) => self.parse_list(), Some(Token::LeftBracket) => self.parse_vector(), @@ -1935,6 +1955,14 @@ mod tests { assert!(result.unwrap_err().contains("exceeds maximum length")); } + #[test] + fn test_parse_edn_deep_nesting_returns_error() { + let deeply_nested = "(".repeat(10_000); + let result = parse_edn(&deeply_nested); + assert!(result.is_err()); + assert!(result.unwrap_err().contains("recursion depth")); + } + #[test] fn test_tokenize_keyword_with_question_mark() { // Regression: '?' ended the keyword, so ":attended?" lexed as From 03727fe4db73c30bf29fda4046f4d3a000ad3a8c Mon Sep 17 00:00:00 2001 From: Aditya Mukhopadhyay Date: Sat, 22 Aug 2026 16:08:10 +0530 Subject: [PATCH 2/2] fix(fuzz): use a seed that actually reproduces the #321 crash The reconstructed hex bytes from the issue report failed in tokenize() on a leading control character before ever reaching the recursion- guarded parser, so the seed never exercised the vulnerable code path on either the pre-fix or fixed build. Replace it with 5000 unclosed '(' characters, verified to genuinely SIGABRT with a stack overflow against the pre-fix parser and to be cleanly rejected with the new recursion-depth error on the fixed one. Co-Authored-By: Claude Sonnet 5 Claude-Session: https://claude.ai/code/session_01YMWSutMLJJ7Y5b8hs9ZaCU --- .../seed_deep_nesting_recursion.bin | Bin 154 -> 5000 bytes 1 file changed, 0 insertions(+), 0 deletions(-) diff --git a/fuzz/corpus/datalog_parser/seed_deep_nesting_recursion.bin b/fuzz/corpus/datalog_parser/seed_deep_nesting_recursion.bin index 314b892525013d05db301f646b645e875dec746b..2bb321fd70300de77b523d86fa8debe19eb1b500 100644 GIT binary patch literal 5000 tcmeIu!2kdN00SYN^Cuj|euYqBz<>b*1`HT5V8DO@0|pEjFkryIdtkm;4Q>Dc literal 154 zcmb1f_jdH~7m2niOU%hk(M>E-a*P22tF+7q>$XFX1{mlf_&OLO8c4DlsR~wkiMa}? y_Ia^Tm0