If your decoder treats every embedded byte string as a fresh document to parse, an attacker nests them and one call becomes thousands, each without a limit.
What goes wrong
During conversion, any byte string the decoder reaches gets treated as another encoded document and parsed on the spot. There is no ceiling on input size, nesting depth, item count, or allocation.
An attacker builds a byte string that contains a byte string that contains a byte string, and so on down. Each layer looks tiny. Decoded, they fan out into deep recursion or a huge allocation. Your one conversion call turns into a stack of them, and the process either pins a core or runs out of heap.
The pattern
function toPlain(node) {
if (isByteString(node)) {
return toPlain(decodeCbor(node.bytes)) // bytes become a new doc
}
if (isArray(node)) return node.map(toPlain)
return node
}function toPlain(node, depth = 0) {
if (depth > MAX_DEPTH) throw new Error('too deep')
if (isByteString(node)) {
if (node.bytes.length > MAX_EMBED) throw new Error('too large')
return toPlain(decodeCbor(node.bytes, { maxItems: LIMIT }), depth + 1)
}
if (isArray(node)) return node.map(n => toPlain(n, depth + 1))
return node
}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
Compact binary formats show up in credential wallets, device messaging, and token payloads because a few bytes can carry a lot. A few bytes can also describe a lot of work. If you decode embedded structures without a depth counter, a peer you never authenticated can drive your parser into the floor.
Any decoder that can call itself on data it just read needs a hard recursion limit. Set it once at the boundary and enforce it on every layer.
Check your own code
- Add a depth counter to any decoder that recurses into embedded data.
- Cap the byte length of an embedded document before you parse it.
- Set item-count and allocation limits on the underlying parser.
- Fuzz the decoder with deeply nested inputs and watch memory.