Deserialization turns text or bytes back into objects. It is insecure when the format can create live objects or run code, and an attacker controls the input. Java serialization, PHP unserialize and Python pickle are the classic cases.
In Node.js it shows up in these places:
node-serialize: itsunserialize()rebuilds functions from strings. A function written as an IIFE (a function that calls itself) runs during parsing. This was CVE-2017-5941.- YAML:
js-yamlversion 3'sload()could build JavaScript functions from tags like!!js/function. Use version 4, whereload()is safe by default. eval,new Functionandvmused on input. Node's docs say thevmmodule is not a security mechanism.- Unsafe merges after parsing:
JSON.parseis safe on its own, but a deep merge of the result can copy a__proto__key. That is prototype pollution.
Defences:
- Use data-only formats such as JSON. They can't carry code.
- Validate the parsed result with a schema before you use it.
- Never pick a class or function based on a name in the input.
- Sign data that you later trust back, such as session state in a cookie, so tampering is detected.
- Keep parsers patched.
- Deserialization is insecure when the format can create live objects or run code from attacker input.
node-serializeand oldjs-yamlload()could run or build functions from input.eval,new Functionandvmare not safe for untrusted input.JSON.parseis safe, but unsafe merges afterwards can cause prototype pollution.- Use data-only formats, validate with a schema and sign data you trust back.
Read more