Recursion depth set by the input is recursion depth set by the attacker. A deeply nested field tree walks your process straight off the end of its stack.
What goes wrong
Here is the rule this code forgets: any time recursion depth follows the shape of external input, the caller controls how deep you go. Fine at three levels. Fatal at fifty thousand.
The traversal visits every nested object to rewrite its fields. No counter, no ceiling. A crafted document with enough nesting blows the stack and aborts the operation. The interesting part is how ordinary the attack looks. It is valid JSON, it parses fine, and it only misbehaves once the recursive step reaches for a frame it cannot get.
The pattern
function rewriteFields(section) {
for (const field in section) {
if (isNestedObject(section[field]))
rewriteFields(section[field]) // depth equals input depth
else
section[field] = translate(section[field])
}
}const MAX_DEPTH = 64
function rewriteFields(section, depth = 0) {
if (depth > MAX_DEPTH) throw new Error("input nesting exceeds limit")
for (const field in section) {
if (isNestedObject(section[field]))
rewriteFields(section[field], depth + 1)
else
section[field] = translate(section[field])
}
}Every gate held. The exploit path was reproduced against a negative control, the impact was calibrated, and the finding was signed report-ready.
Why it matters to you
Rate limiting will not save you, because one request is enough to take down a worker and cheap enough to repeat. The cost asymmetry is the whole problem: the attacker sends bytes, you spend a process.
Treat depth as an input you validate like any other. Sixty-four levels covers essentially any legitimate document. Past that, say no early and move on.
Check your own code
- List the recursive functions that run before authentication or schema validation.
- Give each one a depth argument and a fixed maximum.
- Return a controlled error on overflow instead of letting the runtime throw.
- Measure the deepest nesting your real traffic sends, then set the cap above it.