package
JSON parse with prototype poisoning protection
npm install secure-json-parseGitHub-flavored package documentation.
Across all versions.
Download trend unavailable.
Runtime metadata.
Tagged topics.
Estimated unpacked size across 1 packages.
60.8 kB
Includes dependencies.
JSON.parse() drop-in replacement with prototype poisoning protection.
Consider this:
> const a = '{"__proto__":{ "b":5}}';
'{"__proto__":{ "b":5}}'
> const b = JSON.parse(a);
{ __proto__: { b: 5 } }
> b.b;
undefined
> const c = Object.assign({}, b);
{}
> c.b
5The problem is that JSON.parse() retains the __proto__ property as a plain object key. By
itself, this is not a security issue. However, as soon as that object is assigned to another or
iterated on and values copied, the __proto__ property leaks and becomes the object's prototype.
npm i secure-json-parsePass the option object as a second (or third) parameter for configuring the action to take in case of a bad JSON, if nothing is configured, the default is to throw a SyntaxError.
You can choose which action to perform in case __proto__ is present, and in case constructor.prototype is present.
const sjson = require('secure-json-parse')
const goodJson = '{ "a": 5, "b": 6 }'
const badJson = '{ "a": 5, "b": 6, "__proto__": { "x": 7 }, "constructor": {"prototype": {"bar": "baz"} } }'
console.log(JSON.parse(goodJson), sjson.parse(goodJson, undefined, { protoAction: 'remove', constructorAction: 'remove' }))
console.log(JSON.parse(badJson), sjson.parse(badJson, undefined, { protoAction: 'remove', constructorAction: 'remove' }))sjson.parse(text, [reviver], [options])Parses a given JSON-formatted text into an object where:
text - the JSON text string.reviver - the JSON.parse() optional reviver argument.options - optional configuration object where:
protoAction - optional string with one of:
'error' - throw a SyntaxError when a __proto__ key is found. This is the default value.'remove' - deletes any __proto__ keys from the result object.'ignore' - skips all validation (same as calling JSON.parse() directly).constructorAction - optional string with one of:
'error' - throw a SyntaxError when a constructor.prototype key is found. This is the default value.'remove' - deletes any constructor keys from the result object.'ignore' - skips all validation (same as calling JSON.parse() directly).safe - optional boolean:
true - returns null instead of throwing when a forbidden prototype property is found.false - default behavior (throws or removes based on protoAction/constructorAction).sjson.scan(obj, [options])Scans a given object for prototype properties where:
obj - the object being scanned.options - optional configuration object where:
protoAction - optional string with one of:
'error' - throw a SyntaxError when a __proto__ key is found. This is the default value.'remove' - deletes any __proto__ keys from the input obj.constructorAction - optional string with one of:
'error' - throw a SyntaxError when a constructor.prototype key is found. This is the default value.'remove' - deletes any constructor keys from the input obj.safe - optional boolean:
true - returns null instead of throwing when a forbidden prototype property is found.false - default behavior (throws or removes based on protoAction/constructorAction).Machine: 2.4 Ghz 14-core Intel Core i7-13650HX
v22.20.0
> benchmarks@1.0.0 valid
> node valid.js
valid benchmark
┌─────────┬─────────────────────────────────────┬──────────────────┬──────────────────┬────────────────────────┬────────────────────────┬─────────┐
│ (index) │ Task name │ Latency avg (ns) │ Latency med (ns) │ Throughput avg (ops/s) │ Throughput med (ops/s) │ Samples │
├─────────┼─────────────────────────────────────┼──────────────────┼──────────────────┼────────────────────────┼────────────────────────┼─────────┤
│ 0 │ 'JSON.parse' │ '610.10 ± 0.39%' │ '600.00 ± 0.00' │ '1740515 ± 0.02%' │ '1666667 ± 0' │ 1639075 │
│ 1 │ 'JSON.parse proto' │ '875.42 ± 0.39%' │ '800.00 ± 0.00' │ '1210508 ± 0.03%' │ '1250000 ± 0' │ 1142308 │
│ 2 │ 'secure-json-parse parse' │ '634.34 ± 0.32%' │ '600.00 ± 0.00' │ '1624445 ± 0.01%' │ '1666667 ± 0' │ 1576434 │
│ 3 │ 'secure-json-parse parse proto' │ '657.25 ± 0.42%' │ '600.00 ± 0.00' │ '1666577 ± 0.03%' │ '1666667 ± 0' │ 1521499 │
│ 4 │ 'secure-json-parse safeParse' │ '646.03 ± 1.68%' │ '600.00 ± 0.00' │ '1622543 ± 0.02%' │ '1666667 ± 0' │ 1547914 │
│ 5 │ 'secure-json-parse safeParse proto' │ '912.34 ± 0.20%' │ '900.00 ± 0.00' │ '1122250 ± 0.02%' │ '1111111 ± 0' │ 1096080 │
│ 6 │ 'JSON.parse reviver' │ '3448.5 ± 0.59%' │ '3200.0 ± 0.00' │ '300173 ± 0.04%' │ '312500 ± 0' │ 289982 │
└─────────┴─────────────────────────────────────┴──────────────────┴──────────────────┴────────────────────────┴────────────────────────┴─────────┘
> benchmarks@1.0.0 ignore
> node ignore.js
ignore benchmark
This project has been forked from hapijs/bourne. All credit before commit 4690682 goes to the hapijs/bourne project contributors. After, the project will be maintained by the Fastify team.
Licensed under BSD-3-Clause.
delvedor
tommydelved@gmail.com