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

Deep JSON is a free crash for your parser

A public method walks caller-supplied nested structures with no depth limit. Enough nesting runs the stack out and takes the whole process with it.

accept nestedJSONrecurse once perlevelno depth guardstack overflowkills processATTACKER INPUTIMPACTresource exhausted
Attack path · accept nested JSON → recurse once per level → no depth guard → stack overflow kills process

A public method walks caller-supplied nested structures with no depth limit. Enough nesting runs the stack out and takes the whole process with it.

What goes wrong

One JSON document, nested deep enough, is all it takes to drop the process. Converting one data format to another usually means a recursive walk: handle a node, then recurse into its children. Clean and obvious. The catch is that the depth of that recursion is set by the input, and the input comes from whoever is calling.

Feed it an object nested a few tens of thousands of levels deep and the conversion functions call each other until the thread's stack is gone. Nothing catches that. The process dies. Building the payload is a one-line loop, and the request that carries it looks like ordinary JSON.

The pattern

Vulnerable
function toBinary(node) {
  if (isMap(node)) {
    for (const [k, v] of entries(node))
      out.put(k, toBinary(v))     // recurses as deep as the input
  } else if (isList(node)) {
    for (const item of node) toBinary(item)
  }
  return out
}
Hardened
function toBinary(node, depth = 0) {
  if (depth > MAX_DEPTH) throw new Error("nesting too deep")
  if (isMap(node)) {
    for (const [k, v] of entries(node))
      out.put(k, toBinary(v, depth + 1))
  } else if (isList(node)) {
    for (const item of node) toBinary(item, depth + 1)
  }
  return out
}
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

This one is rated moderate for a reason: it is a crash, not a breach. But a crash on demand is still an outage on demand. If the conversion sits behind an unauthenticated endpoint, anyone can restart your workers whenever they feel like it.

Depth limits are the kind of guard you add once and forget. Pick a maximum nesting depth comfortably above anything real traffic produces, and reject the rest.

Check your own code