Repository navigation
Description
Summary
The openCypher parser throws StackOverflowError on a ~2 KB query with about 1000 nested parentheses. The SQL parser accepts the same nesting depth without trouble, so the behaviour is specific to Cypher and the safe handling already exists elsewhere in the engine.
Version: 26.8.1.
Reproducer
final Path dir = Files.createTempDirectory("arcadedb-so");
try (final DatabaseFactory factory = new DatabaseFactory(dir.resolve("db").toString());
final Database db = factory.create()) {
db.getSchema().createVertexType("V");
final int depth = 1001;
final String cypher = "MATCH (n) WHERE " + "(".repeat(depth) + "1=1" + ")".repeat(depth);
db.getQueryEngine("opencypher").analyze(cypher); // StackOverflowError
}Bisecting the depth on a stock JVM (Temurin 21.0.3, default stack size):
| depth | result |
|---|---|
| 1000 | parsed |
| 1001 | StackOverflowError |
The payload at the threshold is 2032 bytes.
The same depth in SQL is parsed without incident:
db.getQueryEngine("sql").analyze("SELECT FROM V WHERE " + "(".repeat(1001) + "1=1" + ")".repeat(1001));
// returns normallyOther shapes reach it too, all through the same recursion: MATCH (n) WHERE NOT NOT NOT ... true at ~1000 repetitions, and flat chains such as a=1 OR a=1 OR ... or 'x' + 'x' + ... at ~20 000 terms.
Where it recurses
com.arcadedb.query.opencypher.grammar.Cypher25Parser$Expression6Context.<init>(Cypher25Parser.java:11836)
com.arcadedb.query.opencypher.grammar.Cypher25Parser.expression6(Cypher25Parser.java:11855)
com.arcadedb.query.opencypher.grammar.Cypher25Parser.expression7(Cypher25Parser.java:11437)
com.arcadedb.query.opencypher.grammar.Cypher25Parser.expression8(Cypher25Parser.java:11364)
com.arcadedb.query.opencypher.grammar.Cypher25Parser.expression9(Cypher25Parser.java:11287)
com.arcadedb.query.opencypher.grammar.Cypher25Parser.expression10(Cypher25Parser.java:11205)
The generated parser walks the full expression-precedence cascade for every nesting level, so each parenthesis costs roughly ten Java frames. That is what makes 1000 levels enough, and it is why the input can stay this small.
Impact
Not a crash: AbstractServerHttpHandler catches Throwable, so an HTTP request carrying such a query degrades to a 500 rather than killing the worker thread. What remains is worth fixing:
- A malformed query is answered with a 500 instead of the 400 a parse failure should produce.
- The cost is asymmetric. Two kilobytes on the wire force the server to grow and unwind an entire thread stack. Issued concurrently across the worker pool, this is a cheap way to keep every worker busy doing nothing useful.
- The reach is wider than the HTTP command endpoint: the new
mcpmodule exposes Cypher-backed tools, andanalyze()is on the path there too. StackOverflowErroris anError. Catching it and continuing is defensible during parsing, where no database state has been touched, but it makes the boundary between "recoverable bad input" and "the JVM is in trouble" depend on where the overflow happened rather than on what it means.
Suggested fix
Bound the parser's recursion the way the SQL side already does, and report exceeding it as an ordinary parse error. Two common approaches:
- Count nesting depth in the parse listener and fail with
CommandParsingExceptionpast a configurable maximum, which keeps the failure a 400 and gives operators a knob. - Run the parse on a thread with a known stack size and treat
StackOverflowErrorthere as a parse failure. This bounds the blast radius but still pays the unwind, so the first option is preferable.
Either way the diagnostic should name the limit, since a legitimate generated query near the boundary is otherwise indistinguishable from an attack.
Activity
docs: move #5851 release note to a new 26.9.1 doc
fix(cypher): bound expression nesting depth to prevent StackOverflowE…
fix(query): close chain-length gap in Cypher and add matching guard f…