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
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
}
}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)
}
}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
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
- Enforce an input-size cap before any decode or parse.
- Apply depth and item limits to the binary parser, which is the branch usually left unbounded.
- Check the order: cheap validation belongs before expensive parsing.
- Confirm the fallback path cannot be forced to run on every request.