Skip to content

Commit 38a4d1c

Browse files
author
substack
committed
even more aggressive checks for protocol pollution
1 parent 13c01a5 commit 38a4d1c

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

index.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,21 @@ module.exports = function (args, opts) {
6868

6969
function setKey (obj, keys, value) {
7070
var o = obj;
71-
keys.slice(0,-1).forEach(function (key) {
71+
for (var i = 0; i < keys.length-1; i++) {
72+
var key = keys[i];
73+
if (key === '__proto__') return;
7274
if (o[key] === undefined) o[key] = {};
73-
if (o[key] === {}.__proto__) o[key] = {};
75+
if (o[key] === Object.prototype || o[key] === Number.prototype
76+
|| o[key] === String.prototype) o[key] = {};
77+
if (o[key] === Array.prototype) o[key] = [];
7478
o = o[key];
75-
});
79+
}
7680

7781
var key = keys[keys.length - 1];
82+
if (key === '__proto__') return;
83+
if (o === Object.prototype || o === Number.prototype
84+
|| o === String.prototype) o = {};
85+
if (o === Array.prototype) o = [];
7886
if (o[key] === undefined || flags.bools[key] || typeof o[key] === 'boolean') {
7987
o[key] = value;
8088
}

test/proto.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ var test = require('tape');
44
test('proto pollution', function (t) {
55
var argv = parse(['--__proto__.x','123']);
66
t.equal({}.x, undefined);
7-
t.equal(argv.__proto__.x, 123);
7+
t.equal(argv.__proto__.x, undefined);
88
t.equal(argv.x, undefined);
99
t.end();
1010
});
@@ -14,7 +14,7 @@ test('proto pollution (array)', function (t) {
1414
t.equal({}.z, undefined);
1515
t.deepEqual(argv.x, [4,5]);
1616
t.equal(argv.x.z, undefined);
17-
t.equal(argv.x.__proto__.z, 789);
17+
t.equal(argv.x.__proto__.z, undefined);
1818
t.end();
1919
});
2020

0 commit comments

Comments
 (0)