{"name":"secure-json-parse","version":"2.7.0","description":"JSON parse with prototype poisoning protection","main":"index.js","types":"types/index.d.ts","scripts":{"benchmark":"cd benchmarks && npm install && npm run all","lint":"standard","test":"nyc npm run test:unit && npm run test:typescript","test:unit":"tape \"test/*.test.js\"","test:typescript":"tsd","test:browser":"airtap test/*.test.js"},"repository":{"type":"git","url":"git+https://github.com/fastify/secure-json-parse.git"},"keywords":["JSON","parse","safe","security","prototype","pollution"],"license":"BSD-3-Clause","bugs":{"url":"https://github.com/fastify/secure-json-parse/issues"},"homepage":"https://github.com/fastify/secure-json-parse#readme","devDependencies":{"airtap":"^4.0.1","airtap-playwright":"^1.0.1","nyc":"^14.1.1","playwright":"^1.7.1","standard":"^17.0.0","tape":"^5.1.1","tsd":"^0.25.0"},"gitHead":"448277710dddfe4e87a8d529e82645d78974bc07","_id":"secure-json-parse@2.7.0","_nodeVersion":"16.19.0","_npmVersion":"9.2.0","dist":{"integrity":"sha512-6aU+Rwsezw7VR8/nyvKTx8QpWH9FrcYiXXlqC4z5d5XQBDRqtbfsRjnwGyqbi3gddNtWHuEk9OANUotL26qKUw==","shasum":"5a5f9cd6ae47df23dba3151edd06855d47e09862","tarball":"http://registry-npm.ywwl.com/secure-json-parse/download/secure-json-parse-2.7.0.tgz","fileCount":17,"unpackedSize":40656,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIBthW5iEGcAOqDuUSKr4vLckgOyAJA81ffrvX6nEABn9AiBEp3ry1SfO7PurCgCmhwgNvwfAFKbh06aUCb+apXZY1w=="}],"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJjvXXSACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2VmrkHg//TZZuUhj9Dx0SdKeGDFCZ64gc0Io3k+IZRxNtUrhxnT9stjWC\r\nBi2KbRudkMdidq13jLVHce/nuiRohY8BVOP1SbHpiuPpbChThndDsO8RCb2c\r\n2QWR4OZOuugu++mnzIX0gwBILQCTWxac4bmW8L14yPVydZsjuMDiuJQv2djU\r\nTD0xwEdrdP+xPxHzNIJwNgVKPGKmF+659K40BGeplHxKdktRXF51rwHZk/1Y\r\nW8Kl4+9dG2MhUP1IwGU5FtLuprpEb/quP21tK4zu34NVyUOMWywm7M3s1mxZ\r\n+N5DyCHqpcs7xVKs5wU3v3hwI5uhujg8oO2bhwZRwa1vE0wZ+d7ENyo/F6Wc\r\nR8kJWntE+Pv0S98KNPqnxc45djMiS6+G2KsSZjVwhK/pG/Mqg1E7ANto7w1z\r\nVFfGuKfiZeseHfxHuz5y6DK7CRqOGTLbhZhfubqhbJ9QKX4XFQ70TzmpBRXv\r\nWklaN2JkjW6yQt9+MylSsiOLnkLKr4dLFPNpxd2rXWf0J9sgePcEnduZ13hk\r\nCV/yRacY05SR5B64jQ2i7Mw7gKTKMAgBw+u5WrGGJTmkhnf5YL2rDEAPMyYe\r\n4ceusEDtBah8SM1GATmZBrUe3+qaJtjXLZ9pVb+oMEInkHdf67mQX9DaQ1Ja\r\nrclWhOdfcRNrnEWO/ylwS/sBRlM+7ci5hAA=\r\n=rxyd\r\n-----END PGP SIGNATURE-----\r\n","size":8124,"noattachment":false,"key":"/secure-json-parse/-/secure-json-parse-2.7.0.tgz"},"_npmUser":{"name":"matteo.collina","email":"hello@matteocollina.com"},"directories":{},"maintainers":[{"name":"matteo.collina","email":"hello@matteocollina.com"},{"name":"delvedor","email":"tommydelved@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/secure-json-parse_2.7.0_1673360849958_0.9545636492287337"},"_hasShrinkwrap":false,"readme":"# secure-json-parse\n\n![CI](https://github.com/fastify/secure-json-parse/workflows/CI/badge.svg)\n[![NPM version](https://img.shields.io/npm/v/secure-json-parse.svg?style=flat)](https://www.npmjs.com/package/secure-json-parse)\n[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](https://standardjs.com/)\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, { protoAction: 'remove', constructorAction: 'remove' }))\nconsole.log(JSON.parse(badJson), sjson.parse(badJson, { 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\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\n## Benchmarks\n\nMachine: 2,7 GHz Quad-Core Intel Core i7\n\n```\nv14.8.0\n\n> node ignore.js\n\nJSON.parse x 679,376 ops/sec ±1.15% (84 runs sampled)\nsecure-json-parse x 649,605 ops/sec ±0.58% (87 runs sampled)\nreviver x 244,414 ops/sec ±1.05% (88 runs sampled)\nFastest is JSON.parse\n\n> node no__proto__.js\n\nJSON.parse x 652,190 ops/sec ±0.67% (86 runs sampled)\nsecure-json-parse x 589,785 ops/sec ±1.01% (88 runs sampled)\nreviver x 218,075 ops/sec ±1.58% (87 runs sampled)\nFastest is JSON.parse\n\n> node remove.js\n\nJSON.parse x 683,527 ops/sec ±0.62% (88 runs sampled)\nsecure-json-parse x 316,926 ops/sec ±0.63% (87 runs sampled)\nreviver x 214,167 ops/sec ±0.63% (86 runs sampled)\nFastest is JSON.parse\n\n> node throw.js\n\nJSON.parse x 682,548 ops/sec ±0.60% (88 runs sampled)\nJSON.parse error x 170,716 ops/sec ±0.93% (87 runs sampled)\nsecure-json-parse x 104,483 ops/sec ±0.62% (87 runs sampled)\nreviver x 114,197 ops/sec ±0.63% (87 runs sampled)\nFastest is JSON.parse\n```\n\n# Acknowledgements\nThis project has been forked from [hapijs/bourne](https://github.com/hapijs/bourne).\nAll the credits before the 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.\n","publish_time":1673360850112,"_cnpm_publish_time":1673360850112,"dist-tags":{"latest":"4.1.1","next":"3.0.0"}}