Skip to content

Commit 0c7b41e

Browse files
authored
Merge pull request #2 from fastify/fork
Fork and refactor
2 parents 15dbecc + 079d83e commit 0c7b41e

File tree

7 files changed

+314
-282
lines changed

7 files changed

+314
-282
lines changed

LICENSE.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
Copyright (c) 2019 The Fastify Team
12
Copyright (c) 2019, Sideway Inc, and project contributors
23
All rights reserved.
34

@@ -7,3 +8,8 @@ Redistribution and use in source and binary forms, with or without modification,
78
* The names of any contributors may not be used to endorse or promote products derived from this software without specific prior written permission.
89

910
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.
11+
12+
13+
The complete list of contributors can be found at:
14+
- https://github.com/hapijs/bourne/graphs/contributors
15+
- https://github.com/fastify/secure-json-parse/graphs/contributors

README.md

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
<a href="http://hapijs.com"><img src="https://raw.githubusercontent.com/hapijs/assets/master/images/family.png" width="180px" align="right" /></a>
1+
# secure-json-parse
22

3-
# Bourne. JSON Bourne.
3+
[![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/)
44

5-
`JSON.parse()` drop-in replacement with prototype poisoning protection
6-
7-
[![Build Status](https://travis-ci.org/hapijs/bourne.svg)](https://travis-ci.org/hapijs/bourne)
5+
`JSON.parse()` drop-in replacement with prototype poisoning protection.
86

97
## Introduction
108

@@ -52,3 +50,11 @@ Scans a given object for prototype properties where:
5250
- `protoAction` - optional string with one of:
5351
- `'error'` - throw a `SyntaxError` when a `__proto__` key is found. This is the default value.
5452
- `'remove'` - deletes any `__proto__` keys from the input `obj`.
53+
54+
# Acknowledgements
55+
This project has been forked from [hapijs/bourne](https://github.com/hapijs/bourne).
56+
All the credits before the commit [4690682](https://github.com/hapijs/bourne/commit/4690682c6cdaa06590da7b2485d5df91c09da889) goes to the hapijs/bourne project contributors.
57+
After, the project will be maintained by the Fastify team.
58+
59+
# License
60+
Licensed under BSD-3-Clause.

index.js

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
'use strict'
2+
3+
const suspectRx = /"(?:_|\\u005[Ff])(?:_|\\u005[Ff])(?:p|\\u0070)(?:r|\\u0072)(?:o|\\u006[Ff])(?:t|\\u0074)(?:o|\\u006[Ff])(?:_|\\u005[Ff])(?:_|\\u005[Ff])"\s*:/
4+
5+
function parse (text, reviver, options) {
6+
// Normalize arguments
7+
if (options == null) {
8+
if (reviver != null && typeof reviver === 'object') {
9+
options = reviver
10+
reviver = undefined
11+
} else {
12+
options = {}
13+
}
14+
}
15+
16+
// Parse normally, allowing exceptions
17+
const obj = JSON.parse(text, reviver)
18+
19+
// options.protoAction: 'error' (default) / 'remove' / 'ignore'
20+
if (options.protoAction === 'ignore') {
21+
return obj
22+
}
23+
24+
// Ignore null and non-objects
25+
if (!obj || typeof obj !== 'object') {
26+
return obj
27+
}
28+
29+
// Check original string for potential exploit
30+
if (!text.match(suspectRx)) {
31+
return obj
32+
}
33+
34+
// Scan result for proto keys
35+
scan(obj, options)
36+
37+
return obj
38+
}
39+
40+
function scan (obj, options) {
41+
options = options || {}
42+
43+
var next = [obj]
44+
45+
while (next.length) {
46+
const nodes = next
47+
next = []
48+
49+
for (const node of nodes) {
50+
if (Object.prototype.hasOwnProperty.call(node, '__proto__')) { // Avoid calling node.hasOwnProperty directly
51+
if (options.protoAction !== 'remove') {
52+
throw new SyntaxError('Object contains forbidden prototype property')
53+
}
54+
55+
delete node.__proto__ // eslint-disable-line
56+
}
57+
58+
for (const key in node) {
59+
const value = node[key]
60+
if (value && typeof value === 'object') {
61+
next.push(node[key])
62+
}
63+
}
64+
}
65+
}
66+
}
67+
68+
function safeParse (text, reviver) {
69+
try {
70+
return parse(text, reviver)
71+
} catch (ignoreError) {
72+
return null
73+
}
74+
}
75+
76+
module.exports = {
77+
parse,
78+
scan,
79+
safeParse
80+
}

lib/index.js

Lines changed: 0 additions & 97 deletions
This file was deleted.

package.json

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
{
2-
"name": "@hapi/bourne",
2+
"name": "secure-json-parse",
3+
"version": "1.0.0",
34
"description": "JSON parse with prototype poisoning protection",
4-
"version": "1.3.2",
5-
"repository": "git://github.com/hapijs/bourne",
6-
"main": "lib/index.js",
7-
"keywords": [
8-
"JSON",
9-
"parse",
10-
"safe",
11-
"prototype"
12-
],
13-
"dependencies": {},
14-
"devDependencies": {
15-
"@hapi/code": "5.x.x",
16-
"@hapi/lab": "18.x.x",
17-
"benchmark": "^2.1.4"
18-
},
5+
"main": "index.js",
196
"scripts": {
20-
"test": "lab -a @hapi/code -t 100 -L",
21-
"test-cov-html": "lab -a @hapi/code -r html -o coverage.html"
7+
"test": "tap test.js"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "git+https://github.com/fastify/secure-json-parse.git"
2212
},
23-
"license": "BSD-3-Clause"
13+
"keywords": [],
14+
"license": "BSD-3-Clause",
15+
"bugs": {
16+
"url": "https://github.com/fastify/secure-json-parse/issues"
17+
},
18+
"homepage": "https://github.com/fastify/secure-json-parse#readme",
19+
"dependencies": {},
20+
"devDependencies": {
21+
"standard": "^12.0.1",
22+
"tap": "^12.7.0"
23+
}
2424
}

0 commit comments

Comments
 (0)