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

Your parser does the costly work before the cheap check

A function that decodes any string and tries a binary parse before falling back to plain text hands attackers a free resource-exhaustion primitive.

caller sends astringcode hex-decodesitbinary parse runswith no boundtext fallbackreached too lateATTACKER INPUTIMPACTresource exhausted
Attack path · caller sends a string → code hex-decodes it → binary parse runs with no bound → text fallback reached too late

A function that decodes any string and tries a binary parse before falling back to plain text hands attackers a free resource-exhaustion primitive.

What goes wrong

The function accepts a string, hex-decodes it, and attempts to parse the result as a compact binary document. Only if that fails does it fall back to plain text. Nothing bounds how large or how complex the binary side may be.

So the attacker never sends the cheap format. They send hex that decodes into a nasty structured payload, and the expensive parse runs first, every time. The fallback that looks like a safety net is the slow path, reached only after the damage is done.

The pattern

Vulnerable
function decodeValue(input) {
  const bytes = hexDecode(input)
  try {
    return parseCbor(bytes)    // no size or complexity limit
  } catch {
    return parseJson(input)    // cheap path, only on failure
  }
}
Hardened
function decodeValue(input) {
  if (input.length > MAX_INPUT) throw new Error('too large')
  const bytes = hexDecode(input)
  try {
    return parseCbor(bytes, { maxDepth: 32, maxItems: LIMIT })
  } catch {
    return parseJson(input)
  }
}
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

Dual-format decoders appear wherever a system supports an old text format and a new binary one at the same door. The binary branch is usually the newer, less-scrutinized code, and it often runs first because it is the preferred format. An unauthenticated caller who controls the string controls which branch runs and how hard it works.

Put the size check before the decode, not after. A limit you enforce only inside the parser still lets the attacker pay you in CPU to reach it.

Check your own code