{"_id":"secure-json-parse@4.1.1","bugs":{"url":"https://github.com/fastify/secure-json-parse/issues"},"dist":{"shasum":"065e41f68da3fb4472d01d1b08d290d257070d8f","tarball":"http://registry-npm.ywwl.com/secure-json-parse/download/secure-json-parse-4.1.1.tgz","fileCount":20,"integrity":"sha512-lnd81MQEZKjhW/Gd2TQqdd4XP6QYNdDW/EP+I7nTrFHP9y2LmaGPIHk9xsnzcOiEr1c39c/e84xFgGviQKBTRw==","signatures":[{"sig":"MEQCID4yDHY0JgV6U5NEQ6Uw+4+XcGip4Ry+b4kn55exgec9AiBzWsZnRl96q5pnIIGmspC1TpJLfqWH4Hf4JOT9ZwwGMA==","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"},{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEUCIBLriBR4ZI7H32BdA1X9nTXM/2yp9hRuBDWLztWGXz1jAiEA4yHJuOJlOtz/6xm+yPNrukM5T7opyGEy0BEKF+X2k/o="}],"attestations":{"url":"https://registry.npmjs.org/-/npm/v1/attestations/secure-json-parse@4.1.1","provenance":{"predicateType":"https://slsa.dev/provenance/v1"}},"unpackedSize":62310,"size":11421,"noattachment":false,"key":"/secure-json-parse/-/secure-json-parse-4.1.1.tgz"},"main":"index.js","name":"secure-json-parse","type":"commonjs","types":"types/index.d.ts","author":{"name":"Eran Hammer","email":"eran@sideway.com"},"funding":[{"url":"https://github.com/sponsors/fastify","type":"github"},{"url":"https://opencollective.com/fastify","type":"opencollective"}],"gitHead":"b5fc694ae2f1407667da651ec8765335994dbd2a","license":"BSD-3-Clause","scripts":{"lint":"eslint","test":"c8 --all --include index.js --check-coverage --100 npm run test:unit && npm run test:typescript","lint:fix":"eslint --fix","benchmark":"cd benchmarks && npm install && npm run all","test:unit":"tape \"test/*.test.js\"","test:browser":"airtap test/*.test.js","test:typescript":"tstyche"},"version":"4.1.1","_npmUser":{"name":"GitHub Actions","email":"npm-oidc-no-reply@github.com","trustedPublisher":{"id":"github","oidcConfigId":"347136fb-c27a-4d60-8734-ad8653092287"}},"homepage":"https://github.com/fastify/secure-json-parse#readme","keywords":["JSON","parse","safe","security","prototype","pollution"],"repository":{"url":"git+https://github.com/fastify/secure-json-parse.git","type":"git"},"_npmVersion":"11.19.0","description":"JSON parse with prototype poisoning protection","directories":{},"maintainers":[{"name":"jsumners","email":"james.sumners@gmail.com"},{"name":"delvedor","email":"tommydelved@gmail.com"},{"name":"matteo.collina","email":"hello@matteocollina.com"},{"name":"eomm","email":"behemoth89@gmail.com"},{"name":"climba03003","email":"kaka@kakawebsitedemo.com"}],"_nodeVersion":"24.21.0","contributors":[{"name":"Matteo Collina","email":"hello@matteocollina.com"},{"url":"https://delvedor.dev","name":"Tomas Della Vedova"},{"name":"Aras Abbasi","email":"aras.abbasi@gmail.com"},{"url":"https://github.com/fdawgs","name":"Frazer Smith","email":"frazer.dev@icloud.com"}],"_hasShrinkwrap":false,"devDependencies":{"c8":"^12.0.0","tape":"^5.7.5","airtap":"^5.0.0","eslint":"^9.17.0","tstyche":"^7.1.0","playwright":"^1.43.1","neostandard":"^0.13.0","airtap-playwright":"^1.0.1"},"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/secure-json-parse_4.1.1_1791628281036_0.1812050080829246"},"readme":"# secure-json-parse\n\n[![CI](https://github.com/fastify/secure-json-parse/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/fastify/secure-json-parse/actions/workflows/ci.yml)\n[![NPM version](https://img.shields.io/npm/v/secure-json-parse.svg?style=flat)](https://www.npmjs.com/package/secure-json-parse)\n[![neostandard javascript style](https://img.shields.io/badge/code_style-neostandard-brightgreen?style=flat)](https://github.com/neostandard/neostandard)\n\n`JSON.parse()` drop-in replacement with prototype poisoning protection.\n\n## Introduction\n\nConsider this:\n\n```js\n> const a = '{\"__proto__\":{ \"b\":5}}';\n'{\"__proto__\":{ \"b\":5}}'\n\n> const b = JSON.parse(a);\n{ __proto__: { b: 5 } }\n\n> b.b;\nundefined\n\n> const c = Object.assign({}, b);\n{}\n\n> c.b\n5\n```\n\nThe problem is that `JSON.parse()` retains the `__proto__` property as a plain object key. By\nitself, this is not a security issue. However, as soon as that object is assigned to another or\niterated on and values copied, the `__proto__` property leaks and becomes the object's prototype.\n\n## Install\n```\nnpm i secure-json-parse\n```\n\n## Usage\n\nPass 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`.<br/>\nYou can choose which action to perform in case `__proto__` is present, and in case `constructor.prototype` is present.\n\n```js\nconst sjson = require('secure-json-parse')\n\nconst goodJson = '{ \"a\": 5, \"b\": 6 }'\nconst badJson = '{ \"a\": 5, \"b\": 6, \"__proto__\": { \"x\": 7 }, \"constructor\": {\"prototype\": {\"bar\": \"baz\"} } }'\n\nconsole.log(JSON.parse(goodJson), sjson.parse(goodJson, undefined, { protoAction: 'remove', constructorAction: 'remove' }))\nconsole.log(JSON.parse(badJson), sjson.parse(badJson, undefined, { protoAction: 'remove', constructorAction: 'remove' }))\n```\n\n## API\n\n### `sjson.parse(text, [reviver], [options])`\n\nParses a given JSON-formatted text into an object where:\n- `text` - the JSON text string.\n- `reviver` - the `JSON.parse()` optional `reviver` argument.\n- `options` - optional configuration object where:\n    - `protoAction` - optional string with one of:\n        - `'error'` - throw a `SyntaxError` when a `__proto__` key is found. This is the default value.\n        - `'remove'` - deletes any `__proto__` keys from the result object.\n        - `'ignore'` - skips all validation (same as calling `JSON.parse()` directly).\n    - `constructorAction` - optional string with one of:\n        - `'error'` - throw a `SyntaxError` when a `constructor.prototype` key is found. This is the default value.\n        - `'remove'` - deletes any `constructor` keys from the result object.\n        - `'ignore'` - skips all validation (same as calling `JSON.parse()` directly).\n    - `safe` - optional boolean:\n        - `true` - returns `null` instead of throwing when a forbidden prototype property is found.\n        - `false` - default behavior (throws or removes based on `protoAction`/`constructorAction`).\n\n### `sjson.scan(obj, [options])`\n\nScans a given object for prototype properties where:\n- `obj` - the object being scanned.\n- `options` - optional configuration object where:\n    - `protoAction` - optional string with one of:\n        - `'error'` - throw a `SyntaxError` when a `__proto__` key is found. This is the default value.\n        - `'remove'` - deletes any `__proto__` keys from the input `obj`.\n    - `constructorAction` - optional string with one of:\n        - `'error'` - throw a `SyntaxError` when a `constructor.prototype` key is found. This is the default value.\n        - `'remove'` - deletes any `constructor` keys from the input `obj`.\n    - `safe` - optional boolean:\n        - `true` - returns `null` instead of throwing when a forbidden prototype property is found.\n        - `false` - default behavior (throws or removes based on `protoAction`/`constructorAction`).\n\n## Benchmarks\n\nMachine: 2.4 Ghz 14-core Intel Core i7-13650HX\n\n```\nv22.20.0\n\n> benchmarks@1.0.0 valid\n> node valid.js\n\nvalid benchmark\n┌─────────┬─────────────────────────────────────┬──────────────────┬──────────────────┬────────────────────────┬────────────────────────┬─────────┐\n│ (index) │ Task name                           │ Latency avg (ns) │ Latency med (ns) │ Throughput avg (ops/s) │ Throughput med (ops/s) │ Samples │\n├─────────┼─────────────────────────────────────┼──────────────────┼──────────────────┼────────────────────────┼────────────────────────┼─────────┤\n│ 0       │ 'JSON.parse'                        │ '610.10 ± 0.39%' │ '600.00 ± 0.00'  │ '1740515 ± 0.02%'      │ '1666667 ± 0'          │ 1639075 │\n│ 1       │ 'JSON.parse proto'                  │ '875.42 ± 0.39%' │ '800.00 ± 0.00'  │ '1210508 ± 0.03%'      │ '1250000 ± 0'          │ 1142308 │\n│ 2       │ 'secure-json-parse parse'           │ '634.34 ± 0.32%' │ '600.00 ± 0.00'  │ '1624445 ± 0.01%'      │ '1666667 ± 0'          │ 1576434 │\n│ 3       │ 'secure-json-parse parse proto'     │ '657.25 ± 0.42%' │ '600.00 ± 0.00'  │ '1666577 ± 0.03%'      │ '1666667 ± 0'          │ 1521499 │\n│ 4       │ 'secure-json-parse safeParse'       │ '646.03 ± 1.68%' │ '600.00 ± 0.00'  │ '1622543 ± 0.02%'      │ '1666667 ± 0'          │ 1547914 │\n│ 5       │ 'secure-json-parse safeParse proto' │ '912.34 ± 0.20%' │ '900.00 ± 0.00'  │ '1122250 ± 0.02%'      │ '1111111 ± 0'          │ 1096080 │\n│ 6       │ 'JSON.parse reviver'                │ '3448.5 ± 0.59%' │ '3200.0 ± 0.00'  │ '300173 ± 0.04%'       │ '312500 ± 0'           │ 289982  │\n└─────────┴─────────────────────────────────────┴──────────────────┴──────────────────┴────────────────────────┴────────────────────────┴─────────┘\n\n> benchmarks@1.0.0 ignore\n> node ignore.js\n\nignore benchmark\n┌─────────┬───────────────────────────────┬──────────────────┬───────────────────┬────────────────────────┬────────────────────────┬─────────┐\n│ (index) │ Task name                     │ Latency avg (ns) │ Latency med (ns)  │ Throughput avg (ops/s) │ Throughput med (ops/s) │ Samples │\n├─────────┼───────────────────────────────┼──────────────────┼───────────────────┼────────────────────────┼────────────────────────┼─────────┤\n│ 0       │ 'JSON.parse'                  │ '897.15 ± 0.53%' │ '800.00 ± 0.00'   │ '1201546 ± 0.03%'      │ '1250000 ± 0'          │ 1114647 │\n│ 1       │ 'secure-json-parse parse'     │ '891.22 ± 0.45%' │ '800.00 ± 0.00'   │ '1168492 ± 0.02%'      │ '1250000 ± 0'          │ 1122056 │\n│ 2       │ 'secure-json-parse safeParse' │ '938.74 ± 0.56%' │ '900.00 ± 0.00'   │ '1106881 ± 0.02%'      │ '1111111 ± 0'          │ 1065255 │\n│ 3       │ 'reviver'                     │ '5741.8 ± 0.79%' │ '4900.0 ± 100.00' │ '188823 ± 0.08%'       │ '204082 ± 4252'        │ 174162  │\n└─────────┴───────────────────────────────┴──────────────────┴───────────────────┴────────────────────────┴────────────────────────┴─────────┘\n\n> benchmarks@1.0.0 no_proto\n> node no__proto__.js\n\nno __proto__ benchmark\n┌─────────┬───────────────────────────────┬──────────────────┬───────────────────┬────────────────────────┬────────────────────────┬─────────┐\n│ (index) │ Task name                     │ Latency avg (ns) │ Latency med (ns)  │ Throughput avg (ops/s) │ Throughput med (ops/s) │ Samples │\n├─────────┼───────────────────────────────┼──────────────────┼───────────────────┼────────────────────────┼────────────────────────┼─────────┤\n│ 0       │ 'JSON.parse'                  │ '930.41 ± 0.56%' │ '800.00 ± 0.00'   │ '1154630 ± 0.03%'      │ '1250000 ± 0'          │ 1074798 │\n│ 1       │ 'secure-json-parse parse'     │ '996.09 ± 0.27%' │ '900.00 ± 0.00'   │ '1039752 ± 0.02%'      │ '1111111 ± 0'          │ 1003921 │\n│ 2       │ 'secure-json-parse safeParse' │ '1050.5 ± 7.38%' │ '900.00 ± 0.00'   │ '1038060 ± 0.02%'      │ '1111111 ± 0'          │ 951942  │\n│ 3       │ 'reviver'                     │ '5424.7 ± 3.23%' │ '5100.0 ± 100.00' │ '192362 ± 0.05%'       │ '196078 ± 3922'        │ 184341  │\n└─────────┴───────────────────────────────┴──────────────────┴───────────────────┴────────────────────────┴────────────────────────┴─────────┘\n\n> benchmarks@1.0.0 remove\n> node remove.js\n\nremove benchmark\n┌─────────┬───────────────────────────────┬──────────────────┬───────────────────┬────────────────────────┬────────────────────────┬─────────┐\n│ (index) │ Task name                     │ Latency avg (ns) │ Latency med (ns)  │ Throughput avg (ops/s) │ Throughput med (ops/s) │ Samples │\n├─────────┼───────────────────────────────┼──────────────────┼───────────────────┼────────────────────────┼────────────────────────┼─────────┤\n│ 0       │ 'JSON.parse'                  │ '927.86 ± 0.51%' │ '800.00 ± 0.00'   │ '1161336 ± 0.03%'      │ '1250000 ± 0'          │ 1077745 │\n│ 1       │ 'secure-json-parse parse'     │ '1968.1 ± 0.51%' │ '1900.0 ± 100.00' │ '525418 ± 0.02%'       │ '526316 ± 26316'       │ 508117  │\n│ 2       │ 'secure-json-parse safeParse' │ '930.60 ± 0.19%' │ '900.00 ± 0.00'   │ '1103037 ± 0.02%'      │ '1111111 ± 0'          │ 1074579 │\n│ 3       │ 'reviver'                     │ '5531.4 ± 0.36%' │ '5100.0 ± 100.00' │ '187392 ± 0.06%'       │ '196078 ± 3922'        │ 180786  │\n└─────────┴───────────────────────────────┴──────────────────┴───────────────────┴────────────────────────┴────────────────────────┴─────────┘\n\n> benchmarks@1.0.0 throw\n> node throw.js\n\nthrow benchmark\n┌─────────┬───────────────────────────────┬──────────────────┬───────────────────┬────────────────────────┬────────────────────────┬─────────┐\n│ (index) │ Task name                     │ Latency avg (ns) │ Latency med (ns)  │ Throughput avg (ops/s) │ Throughput med (ops/s) │ Samples │\n├─────────┼───────────────────────────────┼──────────────────┼───────────────────┼────────────────────────┼────────────────────────┼─────────┤\n│ 0       │ 'JSON.parse valid'            │ '908.52 ± 0.54%' │ '800.00 ± 0.00'   │ '1178218 ± 0.03%'      │ '1250000 ± 0'          │ 1100690 │\n│ 1       │ 'JSON.parse error'            │ '7993.2 ± 0.54%' │ '7600.0 ± 200.00' │ '128668 ± 0.06%'       │ '131579 ± 3374'        │ 125108  │\n│ 2       │ 'secure-json-parse parse'     │ '3436.4 ± 2.67%' │ '3100.0 ± 100.00' │ '312206 ± 0.05%'       │ '322581 ± 10081'       │ 291001  │\n│ 3       │ 'secure-json-parse safeParse' │ '2800.9 ± 0.33%' │ '2700.0 ± 0.00'   │ '364670 ± 0.03%'       │ '370370 ± 0'           │ 357026  │\n│ 4       │ 'reviver'                     │ '9045.6 ± 1.12%' │ '8300.0 ± 200.00' │ '115581 ± 0.08%'       │ '120482 ± 2975'        │ 110552  │\n└─────────┴───────────────────────────────┴──────────────────┴───────────────────┴────────────────────────┴────────────────────────┴─────────┘\n```\n\n## Acknowledgments\nThis project has been forked from [hapijs/bourne](https://github.com/hapijs/bourne).\nAll credit before commit [4690682](https://github.com/hapijs/bourne/commit/4690682c6cdaa06590da7b2485d5df91c09da889) goes to the hapijs/bourne project contributors.\nAfter, the project will be maintained by the Fastify team.\n\n## License\nLicensed under [BSD-3-Clause](./LICENSE).\n","publish_time":1791628281133,"_cnpm_publish_time":1791628281133,"dist-tags":{"latest":"4.1.1","next":"3.0.0"}}