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
function loadAndValidate(map) {
const json = serialize(map) // full copy #1, unbounded
const obj = parseJson(json) // full copy #2
return validate(obj) // checks run last
}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)
}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
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
- Look for serialize-then-parse round trips on request-sized data.
- Move size and depth checks ahead of any conversion or copy.
- Bound map entry count and nesting before processing.
- Convert between types directly instead of through a JSON string.