diff --git a/LICENSE.md b/LICENSE.md index 8387144..4fd2157 100755 --- a/LICENSE.md +++ b/LICENSE.md @@ -1,3 +1,4 @@ +Copyright (c) 2019 The Fastify Team Copyright (c) 2019, Sideway Inc, and project contributors All rights reserved. @@ -7,3 +8,8 @@ Redistribution and use in source and binary forms, with or without modification, * The names of any contributors may not be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS OFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + +The complete list of contributors can be found at: +- https://github.com/hapijs/bourne/graphs/contributors +- https://github.com/fastify/secure-json-parse/graphs/contributors diff --git a/README.md b/README.md index 3ee5af0..1e26a9a 100755 --- a/README.md +++ b/README.md @@ -1,10 +1,8 @@ - +# secure-json-parse -# Bourne. JSON Bourne. +[![Build Status](https://dev.azure.com/fastify/fastify/_apis/build/status/fastify.secure-json-parse?branchName=master)](https://dev.azure.com/fastify/fastify/_build/latest?definitionId=8&branchName=master) [![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](http://standardjs.com/) -`JSON.parse()` drop-in replacement with prototype poisoning protection - -[![Build Status](https://travis-ci.org/hapijs/bourne.svg)](https://travis-ci.org/hapijs/bourne) +`JSON.parse()` drop-in replacement with prototype poisoning protection. ## Introduction @@ -52,3 +50,11 @@ Scans a given object for prototype properties 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`. + +# Acknowledgements +This project has been forked from [hapijs/bourne](https://github.com/hapijs/bourne). +All the credits before the commit [4690682](https://github.com/hapijs/bourne/commit/4690682c6cdaa06590da7b2485d5df91c09da889) goes to the hapijs/bourne project contributors. +After, the project will be maintained by the Fastify team. + +# License +Licensed under BSD-3-Clause. diff --git a/index.js b/index.js new file mode 100755 index 0000000..a606821 --- /dev/null +++ b/index.js @@ -0,0 +1,80 @@ +'use strict' + +const suspectRx = /"(?:_|\\u005[Ff])(?:_|\\u005[Ff])(?:p|\\u0070)(?:r|\\u0072)(?:o|\\u006[Ff])(?:t|\\u0074)(?:o|\\u006[Ff])(?:_|\\u005[Ff])(?:_|\\u005[Ff])"\s*:/ + +function parse (text, reviver, options) { + // Normalize arguments + if (options == null) { + if (reviver != null && typeof reviver === 'object') { + options = reviver + reviver = undefined + } else { + options = {} + } + } + + // Parse normally, allowing exceptions + const obj = JSON.parse(text, reviver) + + // options.protoAction: 'error' (default) / 'remove' / 'ignore' + if (options.protoAction === 'ignore') { + return obj + } + + // Ignore null and non-objects + if (!obj || typeof obj !== 'object') { + return obj + } + + // Check original string for potential exploit + if (!text.match(suspectRx)) { + return obj + } + + // Scan result for proto keys + scan(obj, options) + + return obj +} + +function scan (obj, options) { + options = options || {} + + var next = [obj] + + while (next.length) { + const nodes = next + next = [] + + for (const node of nodes) { + if (Object.prototype.hasOwnProperty.call(node, '__proto__')) { // Avoid calling node.hasOwnProperty directly + if (options.protoAction !== 'remove') { + throw new SyntaxError('Object contains forbidden prototype property') + } + + delete node.__proto__ // eslint-disable-line + } + + for (const key in node) { + const value = node[key] + if (value && typeof value === 'object') { + next.push(node[key]) + } + } + } + } +} + +function safeParse (text, reviver) { + try { + return parse(text, reviver) + } catch (ignoreError) { + return null + } +} + +module.exports = { + parse, + scan, + safeParse +} diff --git a/lib/index.js b/lib/index.js deleted file mode 100755 index a5407a0..0000000 --- a/lib/index.js +++ /dev/null @@ -1,97 +0,0 @@ -'use strict'; - - -const internals = { - suspectRx: /"(?:_|\\u005[Ff])(?:_|\\u005[Ff])(?:p|\\u0070)(?:r|\\u0072)(?:o|\\u006[Ff])(?:t|\\u0074)(?:o|\\u006[Ff])(?:_|\\u005[Ff])(?:_|\\u005[Ff])"\s*\:/ -}; - - -exports.parse = function (text, reviver, options) { - - // Normalize arguments - - if (!options) { - if (reviver && - typeof reviver === 'object') { - - options = reviver; - reviver = undefined; - } - else { - options = {}; - } - } - - // Parse normally, allowing exceptions - - const obj = JSON.parse(text, reviver); - - // options.protoAction: 'error' (default) / 'remove' / 'ignore' - - if (options.protoAction === 'ignore') { - return obj; - } - - // Ignore null and non-objects - - if (!obj || - typeof obj !== 'object') { - - return obj; - } - - // Check original string for potential exploit - - if (!text.match(internals.suspectRx)) { - return obj; - } - - // Scan result for proto keys - - exports.scan(obj, options); - - return obj; -}; - - -exports.scan = function (obj, options) { - - options = options || {}; - - let next = [obj]; - - while (next.length) { - const nodes = next; - next = []; - - for (const node of nodes) { - if (Object.prototype.hasOwnProperty.call(node, '__proto__')) { // Avoid calling node.hasOwnProperty directly - if (options.protoAction !== 'remove') { - throw new SyntaxError('Object contains forbidden prototype property'); - } - - delete node.__proto__; - } - - for (const key in node) { - const value = node[key]; - if (value && - typeof value === 'object') { - - next.push(node[key]); - } - } - } - } -}; - - -exports.safeParse = function (text, reviver) { - - try { - return exports.parse(text, reviver); - } - catch (ignoreError) { - return null; - } -}; diff --git a/package.json b/package.json index ae6b625..4e58d3d 100644 --- a/package.json +++ b/package.json @@ -1,24 +1,24 @@ { - "name": "@hapi/bourne", + "name": "secure-json-parse", + "version": "1.0.0", "description": "JSON parse with prototype poisoning protection", - "version": "1.3.2", - "repository": "git://github.com/hapijs/bourne", - "main": "lib/index.js", - "keywords": [ - "JSON", - "parse", - "safe", - "prototype" - ], - "dependencies": {}, - "devDependencies": { - "@hapi/code": "5.x.x", - "@hapi/lab": "18.x.x", - "benchmark": "^2.1.4" - }, + "main": "index.js", "scripts": { - "test": "lab -a @hapi/code -t 100 -L", - "test-cov-html": "lab -a @hapi/code -r html -o coverage.html" + "test": "tap test.js" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/fastify/secure-json-parse.git" }, - "license": "BSD-3-Clause" + "keywords": [], + "license": "BSD-3-Clause", + "bugs": { + "url": "https://github.com/fastify/secure-json-parse/issues" + }, + "homepage": "https://github.com/fastify/secure-json-parse#readme", + "dependencies": {}, + "devDependencies": { + "standard": "^12.0.1", + "tap": "^12.7.0" + } } diff --git a/test.js b/test.js new file mode 100644 index 0000000..1018e6b --- /dev/null +++ b/test.js @@ -0,0 +1,198 @@ +'use strict' + +const test = require('tap').test +const j = require('./index') + +test('parse', t => { + t.test('parses object string', t => { + t.deepEqual( + j.parse('{"a": 5, "b": 6}'), + JSON.parse('{"a": 5, "b": 6}') + ) + t.end() + }) + + t.test('parses null string', t => { + t.strictEqual( + j.parse('null'), + JSON.parse('null') + ) + t.end() + }) + + t.test('parses 0 string', t => { + t.strictEqual( + j.parse('0'), + JSON.parse('0') + ) + t.end() + }) + + t.test('parses string string', t => { + t.strictEqual( + j.parse('"X"'), + JSON.parse('"X"') + ) + t.end() + }) + + t.test('parses object string (reviver)', t => { + const reviver = (key, value) => { + return typeof value === 'number' ? value + 1 : value + } + + t.deepEqual( + j.parse('{"a": 5, "b": 6}', reviver), + JSON.parse('{"a": 5, "b": 6}', reviver) + ) + t.end() + }) + + t.test('sanitizes object string (reviver, options)', t => { + const reviver = (key, value) => { + return typeof value === 'number' ? value + 1 : value + } + + t.deepEqual( + j.parse('{"a": 5, "b": 6,"__proto__": { "x": 7 }}', reviver, { protoAction: 'remove' }), + { a: 6, b: 7 } + ) + t.end() + }) + + t.test('sanitizes object string (options)', t => { + t.deepEqual( + j.parse('{"a": 5, "b": 6,"__proto__": { "x": 7 }}', { protoAction: 'remove' }), + { a: 5, b: 6 } + ) + t.end() + }) + + t.test('sanitizes object string (null, options)', t => { + t.deepEqual( + j.parse('{"a": 5, "b": 6,"__proto__": { "x": 7 }}', null, { protoAction: 'remove' }), + { a: 5, b: 6 } + ) + t.end() + }) + + t.test('sanitizes object string (null, options)', t => { + t.deepEqual( + j.parse('{"a": 5, "b": 6,"__proto__": { "x": 7 }}', { protoAction: 'remove' }), + { a: 5, b: 6 } + ) + t.end() + }) + + t.test('sanitizes nested object string', t => { + t.deepEqual( + j.parse('{ "a": 5, "b": 6, "__proto__": { "x": 7 }, "c": { "d": 0, "e": "text", "__proto__": { "y": 8 }, "f": { "g": 2 } } }', { protoAction: 'remove' }), + { a: 5, b: 6, c: { d: 0, e: 'text', f: { g: 2 } } } + ) + t.end() + }) + + t.test('ignores proto property', t => { + t.deepEqual( + j.parse('{ "a": 5, "b": 6, "__proto__": { "x": 7 } }', { protoAction: 'ignore' }), + JSON.parse('{ "a": 5, "b": 6, "__proto__": { "x": 7 } }') + ) + t.end() + }) + + t.test('ignores proto value', t => { + t.deepEqual( + j.parse('{"a": 5, "b": "__proto__"}'), + { a: 5, b: '__proto__' } + ) + t.end() + }) + + t.test('errors on proto property', t => { + t.throws(() => j.parse('{ "a": 5, "b": 6, "__proto__": { "x": 7 } }'), SyntaxError) + t.throws(() => j.parse('{ "a": 5, "b": 6, "__proto__" : { "x": 7 } }'), SyntaxError) + t.throws(() => j.parse('{ "a": 5, "b": 6, "__proto__" \n\r\t : { "x": 7 } }'), SyntaxError) + t.throws(() => j.parse('{ "a": 5, "b": 6, "__proto__" \n \r \t : { "x": 7 } }'), SyntaxError) + t.end() + }) + + t.test('errors on proto property (null, null)', t => { + t.throws(() => j.parse('{ "a": 5, "b": 6, "__proto__": { "x": 7 } }', null, null), SyntaxError) + t.end() + }) + + t.test('errors on proto property (explicit options)', t => { + t.throws(() => j.parse('{ "a": 5, "b": 6, "__proto__": { "x": 7 } }', { protoAction: 'error' }), SyntaxError) + t.end() + }) + + t.test('errors on proto property (unicode)', t => { + t.throws(() => j.parse('{ "a": 5, "b": 6, "\\u005f_proto__": { "x": 7 } }'), SyntaxError) + t.throws(() => j.parse('{ "a": 5, "b": 6, "_\\u005fp\\u0072oto__": { "x": 7 } }'), SyntaxError) + t.throws(() => j.parse('{ "a": 5, "b": 6, "\\u005f\\u005f\\u0070\\u0072\\u006f\\u0074\\u006f\\u005f\\u005f": { "x": 7 } }'), SyntaxError) + t.throws(() => j.parse('{ "a": 5, "b": 6, "\\u005F_proto__": { "x": 7 } }'), SyntaxError) + t.throws(() => j.parse('{ "a": 5, "b": 6, "_\\u005Fp\\u0072oto__": { "x": 7 } }'), SyntaxError) + t.throws(() => j.parse('{ "a": 5, "b": 6, "\\u005F\\u005F\\u0070\\u0072\\u006F\\u0074\\u006F\\u005F\\u005F": { "x": 7 } }'), SyntaxError) + t.end() + }) + + t.end() +}) + +test('scan', t => { + t.test('sanitizes nested object string', t => { + const text = '{ "a": 5, "b": 6, "__proto__": { "x": 7 }, "c": { "d": 0, "e": "text", "__proto__": { "y": 8 }, "f": { "g": 2 } } }' + const obj = JSON.parse(text) + + j.scan(obj, { protoAction: 'remove' }) + t.deepEqual(obj, { a: 5, b: 6, c: { d: 0, e: 'text', f: { g: 2 } } }) + t.end() + }) + + t.test('errors on proto property', t => { + const text = '{ "a": 5, "b": 6, "__proto__": { "x": 7 }, "c": { "d": 0, "e": "text", "__proto__": { "y": 8 }, "f": { "g": 2 } } }' + const obj = JSON.parse(text) + + t.throws(() => j.scan(obj), SyntaxError) + t.end() + }) + + t.test('does not break when hasOwnProperty is overwritten', t => { + const text = '{ "a": 5, "b": 6, "hasOwnProperty": "text", "__proto__": { "x": 7 } }' + const obj = JSON.parse(text) + + j.scan(obj, { protoAction: 'remove' }) + t.deepEqual(obj, { a: 5, b: 6, hasOwnProperty: 'text' }) + t.end() + }) + + t.end() +}) + +test('safeParse', t => { + t.test('parses object string', t => { + t.deepEqual( + j.safeParse('{"a": 5, "b": 6}'), + { a: 5, b: 6 } + ) + t.end() + }) + + t.test('returns null on proto object string', t => { + t.strictEqual( + j.safeParse('{ "a": 5, "b": 6, "__proto__": { "x": 7 } }'), + null + ) + t.end() + }) + + t.test('returns null on invalid object string', t => { + t.strictEqual( + j.safeParse('{"a": 5, "b": 6'), + null + ) + t.end() + }) + + t.end() +}) diff --git a/test/index.js b/test/index.js deleted file mode 100755 index 5916205..0000000 --- a/test/index.js +++ /dev/null @@ -1,161 +0,0 @@ -'use strict'; - -const Code = require('@hapi/code'); -const Bourne = require('..'); -const Lab = require('@hapi/lab'); - - -const internals = {}; - - -const { describe, it } = exports.lab = Lab.script(); -const expect = Code.expect; - - -describe('Bourne', () => { - - describe('parse()', () => { - - it('parses object string', () => { - - expect(Bourne.parse('{"a": 5, "b": 6}')).to.equal({ a: 5, b: 6 }); - }); - - it('parses null string', () => { - - expect(Bourne.parse('null')).to.equal(null); - }); - - it('parses zero string', () => { - - expect(Bourne.parse('0')).to.equal(0); - }); - - it('parses string string', () => { - - expect(Bourne.parse('"x"')).to.equal('x'); - }); - - it('parses object string (reviver)', () => { - - const reviver = (key, value) => { - - return typeof value === 'number' ? value + 1 : value; - }; - - expect(Bourne.parse('{"a": 5, "b": 6}', reviver)).to.equal({ a: 6, b: 7 }); - }); - - it('sanitizes object string (reviver, options)', () => { - - const reviver = (key, value) => { - - return typeof value === 'number' ? value + 1 : value; - }; - - expect(Bourne.parse('{ "a": 5, "b": 6, "__proto__": { "x": 7 } }', reviver, { protoAction: 'remove' })).to.equal({ a: 6, b: 7 }); - }); - - it('sanitizes object string (options)', () => { - - expect(Bourne.parse('{ "a": 5, "b": 6, "__proto__": { "x": 7 } }', { protoAction: 'remove' })).to.equal({ a: 5, b: 6 }); - }); - - it('sanitizes object string (null, options)', () => { - - expect(Bourne.parse('{ "a": 5, "b": 6, "__proto__": { "x": 7 } }', null, { protoAction: 'remove' })).to.equal({ a: 5, b: 6 }); - }); - - it('sanitizes nested object string', () => { - - const text = '{ "a": 5, "b": 6, "__proto__": { "x": 7 }, "c": { "d": 0, "e": "text", "__proto__": { "y": 8 }, "f": { "g": 2 } } }'; - expect(Bourne.parse(text, { protoAction: 'remove' })).to.equal({ a: 5, b: 6, c: { d: 0, e: 'text', f: { g: 2 } } }); - }); - - it('ignores proto property', () => { - - const text = '{ "a": 5, "b": 6, "__proto__": { "x": 7 } }'; - expect(Bourne.parse(text, { protoAction: 'ignore' })).to.equal(JSON.parse(text)); - }); - - it('ignores proto value', () => { - - expect(Bourne.parse('{"a": 5, "b": "__proto__"}')).to.equal({ a: 5, b: '__proto__' }); - }); - - it('errors on proto property', () => { - - expect(() => Bourne.parse('{ "a": 5, "b": 6, "__proto__": { "x": 7 } }')).to.throw(SyntaxError); - expect(() => Bourne.parse('{ "a": 5, "b": 6, "__proto__" : { "x": 7 } }')).to.throw(SyntaxError); - expect(() => Bourne.parse('{ "a": 5, "b": 6, "__proto__" \n\r\t : { "x": 7 } }')).to.throw(SyntaxError); - expect(() => Bourne.parse('{ "a": 5, "b": 6, "__proto__" \n \r \t : { "x": 7 } }')).to.throw(SyntaxError); - }); - - it('errors on proto property (null, null)', () => { - - expect(() => Bourne.parse('{ "a": 5, "b": 6, "__proto__": { "x": 7 } }', null, null)).to.throw(SyntaxError); - }); - - it('errors on proto property (explicit options)', () => { - - expect(() => Bourne.parse('{ "a": 5, "b": 6, "__proto__": { "x": 7 } }', { protoAction: 'error' })).to.throw(SyntaxError); - }); - - it('errors on proto property (unicode)', () => { - - expect(() => Bourne.parse('{ "a": 5, "b": 6, "\\u005f_proto__": { "x": 7 } }')).to.throw(SyntaxError); - expect(() => Bourne.parse('{ "a": 5, "b": 6, "_\\u005fp\\u0072oto__": { "x": 7 } }')).to.throw(SyntaxError); - expect(() => Bourne.parse('{ "a": 5, "b": 6, "\\u005f\\u005f\\u0070\\u0072\\u006f\\u0074\\u006f\\u005f\\u005f": { "x": 7 } }')).to.throw(SyntaxError); - expect(() => Bourne.parse('{ "a": 5, "b": 6, "\\u005F_proto__": { "x": 7 } }')).to.throw(SyntaxError); - expect(() => Bourne.parse('{ "a": 5, "b": 6, "_\\u005Fp\\u0072oto__": { "x": 7 } }')).to.throw(SyntaxError); - expect(() => Bourne.parse('{ "a": 5, "b": 6, "\\u005F\\u005F\\u0070\\u0072\\u006F\\u0074\\u006F\\u005F\\u005F": { "x": 7 } }')).to.throw(SyntaxError); - }); - }); - - describe('scan()', () => { - - it('sanitizes nested object string', () => { - - const text = '{ "a": 5, "b": 6, "__proto__": { "x": 7 }, "c": { "d": 0, "e": "text", "__proto__": { "y": 8 }, "f": { "g": 2 } } }'; - const obj = JSON.parse(text); - - Bourne.scan(obj, { protoAction: 'remove' }); - expect(obj).to.equal({ a: 5, b: 6, c: { d: 0, e: 'text', f: { g: 2 } } }); - }); - - it('errors on proto property', () => { - - const text = '{ "a": 5, "b": 6, "__proto__": { "x": 7 }, "c": { "d": 0, "e": "text", "__proto__": { "y": 8 }, "f": { "g": 2 } } }'; - const obj = JSON.parse(text); - - expect(() => Bourne.scan(obj)).to.throw(SyntaxError); - }); - - it('does not break when hasOwnProperty is overwritten', () => { - - const text = '{ "a": 5, "b": 6, "hasOwnProperty": "text", "__proto__": { "x": 7 } }'; - const obj = JSON.parse(text); - - Bourne.scan(obj, { protoAction: 'remove' }); - expect(obj).to.equal({ a: 5, b: 6, hasOwnProperty: 'text' }); - }); - }); - - describe('safeParse()', () => { - - it('parses object string', () => { - - expect(Bourne.safeParse('{"a": 5, "b": 6}')).to.equal({ a: 5, b: 6 }); - }); - - it('returns null on proto object string', () => { - - expect(Bourne.safeParse('{ "a": 5, "b": 6, "__proto__": { "x": 7 } }')).to.be.null(); - }); - - it('returns null on invalid object string', () => { - - expect(Bourne.safeParse('{"a": 5, "b": 6')).to.be.null(); - }); - }); -});