[BUG]: Expression parser's recursion depth guard is unreachable (Dart and web_core)
- [x] I have searched the existing issues to make sure this bug has not already been reported.
Describe the Bug
ExpressionParser declares a maximum recursion depth of 10, but nothing enforces it. The check lives in parse(), which is only ever entered at depth 0, while both recursive paths bypass it:
- Nested interpolations (
${${...}}) recurse throughparseExpression(), which accepts adepthbut never compares it against the limit. - Function-call arguments (
f(a: g(b: ...))) recurse throughparseExpressionInternal()withdepthpassed through unchanged, so that nesting is never counted at all.
Recursion on parse is therefore unbounded. Both implementations share the shape and the defect:
| limit declared | reachable | deeply nested template | |
|---|---|---|---|
dart/a2ui_core |
maxDepth = 10 |
no | StackOverflowError |
renderers/web_core |
MAX_DEPTH = 10 |
no | RangeError: Maximum call stack size exceeded |
The relevant lines in Dart are L25 (the only check), L101 (parseExpression, no check) and L180 (arguments, depth not incremented); in web_core they are L36, L115 and L198.
Nesting depth is agent-controlled: formatString hands its value argument straight to the parser, and that argument is an ordinary string in the message payload.
Steps to Reproduce
Dart, against dart/a2ui_core at main (676a899):
final parser = ExpressionParser();
String nestedCalls(int n) => '\${${'f(a: ' * n}1${')' * n}}';
parser.parse(nestedCalls(1000)); // parses fine; the guard never fires
parser.parse(nestedCalls(5000)); // StackOverflowError
web_core, against the published @a2ui/[email protected]:
const p = new ExpressionParser();
const nested = (n) => '${'.repeat(n) + 'x' + '}'.repeat(n);
p.parse(nested(1000)); // parses fine
p.parse(nested(5000)); // RangeError: Maximum call stack size exceeded
Measured for both nesting forms (${f(a: f(a: ...))} and ${${...}}) in both languages:
| nesting depth | Dart | web_core |
|---|---|---|
| 10 | parsed, no error | parsed, no error |
| 100 | parsed, no error | parsed, no error |
| 1000 | parsed, no error | parsed, no error |
| 5000 | StackOverflowError |
RangeError |
Expected Behavior
A template nested past the declared limit is rejected with A2uiExpressionError, which is what the guard was written to throw and what the rest of the expression layer raises. Today the caller instead gets a StackOverflowError / RangeError, outside the A2uiError hierarchy consumers handle — and in Dart, avoid_catching_errors (enabled repo-wide) discourages catching it at all.
Environment Details
- OS: macOS
- Browser/Platform: Dart 3.12.2, Node.js 25.2.1
- SDK/Package Name & Version:
a2ui_coreat main (676a899),@a2ui/web_core0.10.7 - Protocol Version: v0.9
Additional Context
The fix has the same shape in both languages: move the depth check into parseExpressionInternal() — the single point both recursive paths pass through — and pass depth + 1 when parsing function-call arguments.
I have the Dart side ready with tests and will open a PR for it shortly.
I am deliberately not sending the web_core change yet: #2433 is currently editing that same file to add template length and parts caps. Those caps are orthogonal to this one — they bound how large an input is, not how deeply it nests, so neither stops the recursion described here — and the fix is best applied on top of that PR rather than conflicting with it. Happy to send it as a follow-up once it lands.
Both halves are now up: #2491 for Dart and #2492 for web_core.
I said above that I would hold the web_core change until #2433 landed. Having checked that concretely rather than assuming it, holding turns out to be unnecessary: the two changes touch different methods, they merge with no conflicts in either file, and with both applied the basic catalog suite passes in full (112/112).
The caps in #2433 stay orthogonal to this — they bound how large an input is, not how deeply it nests — and #2492's pathological-nesting test asserts on the error kind rather than its message, so it still holds once a length cap rejects that input first. Whichever lands first, the other should need no rebase.
Hi @diegolopezrm, Thank you for raising the issue. We have reproduced the issue, and we appreciate your contribution. We verified that your PR resolves the issue, and it will be reviewed. Thank you for your patience and understanding.
This one's done. The guard is reachable in both Dart and web_core on main now, from #2491 and #2492. Closing it.