Blog / A04:2021 Insecure Design
A04:2021 · Insecure Design

Whoever sends the data picks how deep you recurse

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.

receive nesteddocumentwalk each nestedobjectdepth uncheckedthread stackexhaustedATTACKER INPUTIMPACTresource exhausted
Attack path · receive nested document → walk each nested object → depth unchecked → thread stack exhausted

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

Vulnerable
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])
  }
}
Hardened
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])
  }
}
How the pipeline adjudicated itPROVEN
ABCDEFGH

Every gate held. The exploit path was reproduced against a negative control, the impact was calibrated, and the finding was signed report-ready.

Ed25519 · per-tenant signing key · gate ledger A–H · verify offline against the published payload hash

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