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

Serializing input just to parse it again pays twice

Turning a caller-controlled map into a JSON string and parsing it right back doubles the allocation for big inputs, and does it before any validation runs.

caller sends amapcode serializesit to JSONparses the stringbacklarge inputallocates…ATTACKER INPUTIMPACTresource exhausted
Attack path · caller sends a map → code serializes it to JSON → parses the string back → large input allocates twice before checks

Turning a caller-controlled map into a JSON string and parsing it right back doubles the allocation for big inputs, and does it before any validation runs.

What goes wrong

A map that came straight from the caller gets serialized to a JSON string and then parsed back into objects, all to reach a shape the validator expects. Neither the map size nor its nesting is bounded, and this round trip happens before validation.

For a big map that means two full copies in memory and two passes of work, back to back, on input nobody has vetted. The attacker sends one large nested structure and you do double the allocation on their behalf. The validation that would have rejected it runs only after you have already paid for it.

The pattern

Vulnerable
function loadAndValidate(map) {
  const json = serialize(map)   // full copy #1, unbounded
  const obj = parseJson(json)   // full copy #2
  return validate(obj)          // checks run last
}
Hardened
function loadAndValidate(map) {
  if (map.size > MAX_ENTRIES) throw new Error('too many entries')
  checkDepth(map, MAX_DEPTH)
  const obj = convertInPlace(map)  // no serialize round trip
  return validate(obj)
}
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

The serialize-then-reparse trick is a common shortcut for converting between types, and it stays invisible in a profiler until real traffic hits it. Under hostile input it becomes a memory amplifier that runs ahead of your defenses. Work you do before validation is work an attacker gets for free.

Convert types directly when you can, and move size and depth checks ahead of any conversion. Validation that runs last protects nothing from a resource attack.

Check your own code