Skip to content

Commit 6591ef7

Browse files
authored
Merge pull request #7 from rgrove/fix-hasOwnProperty
Don't assume `hasOwnProperty` is safe
2 parents 36fd918 + 796d566 commit 6591ef7

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

lib/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict';
22

3+
const hasOwnProperty = Object.prototype.hasOwnProperty;
34

45
const internals = {
56
suspectRx: /"(?:_|\\u005f)(?:_|\\u005f)(?:p|\\u0070)(?:r|\\u0072)(?:o|\\u006f)(?:t|\\u0074)(?:o|\\u006f)(?:_|\\u005f)(?:_|\\u005f)"\s*\:/
@@ -65,7 +66,7 @@ exports.scan = function (obj, options) {
6566
next = [];
6667

6768
for (const node of nodes) {
68-
if (node.hasOwnProperty('__proto__')) {
69+
if (hasOwnProperty.call(node, '__proto__')) {
6970
if (options.protoAction !== 'remove') {
7071
throw new SyntaxError('Object contains forbidden prototype property');
7172
}

test/index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,5 +127,14 @@ describe('Bourne', () => {
127127

128128
expect(() => Bourne.scan(obj)).to.throw(SyntaxError);
129129
});
130+
131+
it('does not break when hasOwnProperty is overwritten', () => {
132+
133+
const text = '{ "a": 5, "b": 6, "hasOwnProperty": "text", "__proto__": { "x": 7 } }';
134+
const obj = JSON.parse(text);
135+
136+
Bourne.scan(obj, { protoAction: 'remove' });
137+
expect(obj).to.equal({ a: 5, b: 6, hasOwnProperty: 'text' });
138+
});
130139
});
131140
});

0 commit comments

Comments
 (0)