The vulnerability, a recursive stack overflow in the SQLFluff parser, is caused by uncontrolled recursion when parsing deeply nested SQL queries. The analysis of the security patches reveals that the core issue was the lack of a depth limit in the parsing logic, both in the Python implementation and the optional Rust-based parser.
The patch introduces a new configuration parameter, max_parse_depth, which defaults to 255. This limit is enforced in the key recursive and iterative parts of the parser.
Three main areas were identified as vulnerable:
-
Bracket Matching (resolve_bracket): The Python function for parsing nested brackets was purely recursive. The patch wraps the recursive call in a new context manager (deeper_match) that tracks and limits the recursion depth.
-
General Grammar Matching (BaseGrammar.match via deeper_match): The fundamental parsing mechanism involves grammar objects recursively calling match on sub-grammars. The deeper_match context manager was introduced as a central fix to track depth across all grammar matching, preventing stack overflow from any form of deeply nested grammar structure (e.g., nested CASE statements, subqueries, etc.).
-
Rust Parser (Parser.call_rule_as_root): The Rust parser uses an iterative, stack-based approach to avoid native recursion limits. However, this simulated stack could still grow without bounds. The patch adds a check within the main parsing loop to limit the size of this stack, effectively mitigating the same denial-of-service vector.
By identifying these functions, a security engineer can understand that any interaction with the SQLFluff parsing engine, when fed a malicious query, could have triggered this vulnerability. The runtime profile during an exploit would show deep call stacks involving resolve_bracket and various match methods, or high memory usage by the Rust parser's internal stack, until the process crashes.