Skip to content

Commit

Permalink
Merge pull request #2 from fastify/fork
Browse files Browse the repository at this point in the history
Fork and refactor
  • Loading branch information
delvedor authored Apr 30, 2019
2 parents 15dbecc + 079d83e commit 0c7b41e
Show file tree
Hide file tree
Showing 7 changed files with 314 additions and 282 deletions.
6 changes: 6 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
Copyright (c) 2019 The Fastify Team
Copyright (c) 2019, Sideway Inc, and project contributors
All rights reserved.

Expand All @@ -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
16 changes: 11 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
<a href="http://hapijs.com"><img src="https://raw.githubusercontent.com/hapijs/assets/master/images/family.png" width="180px" align="right" /></a>
# 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

Expand Down Expand Up @@ -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.
80 changes: 80 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -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
}
97 changes: 0 additions & 97 deletions lib/index.js

This file was deleted.

38 changes: 19 additions & 19 deletions package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
Loading

0 comments on commit 0c7b41e

Please sign in to comment.