Skip to content

Commit

Permalink
even more aggressive checks for protocol pollution
Browse files Browse the repository at this point in the history
  • Loading branch information
substack committed Mar 10, 2020
1 parent 13c01a5 commit 38a4d1c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
14 changes: 11 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,21 @@ module.exports = function (args, opts) {

function setKey (obj, keys, value) {
var o = obj;
keys.slice(0,-1).forEach(function (key) {
for (var i = 0; i < keys.length-1; i++) {
var key = keys[i];
if (key === '__proto__') return;
if (o[key] === undefined) o[key] = {};
if (o[key] === {}.__proto__) o[key] = {};
if (o[key] === Object.prototype || o[key] === Number.prototype
|| o[key] === String.prototype) o[key] = {};
if (o[key] === Array.prototype) o[key] = [];
o = o[key];
});
}

var key = keys[keys.length - 1];
if (key === '__proto__') return;
if (o === Object.prototype || o === Number.prototype
|| o === String.prototype) o = {};
if (o === Array.prototype) o = [];
if (o[key] === undefined || flags.bools[key] || typeof o[key] === 'boolean') {
o[key] = value;
}
Expand Down
4 changes: 2 additions & 2 deletions test/proto.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var test = require('tape');
test('proto pollution', function (t) {
var argv = parse(['--__proto__.x','123']);
t.equal({}.x, undefined);
t.equal(argv.__proto__.x, 123);
t.equal(argv.__proto__.x, undefined);
t.equal(argv.x, undefined);
t.end();
});
Expand All @@ -14,7 +14,7 @@ test('proto pollution (array)', function (t) {
t.equal({}.z, undefined);
t.deepEqual(argv.x, [4,5]);
t.equal(argv.x.z, undefined);
t.equal(argv.x.__proto__.z, 789);
t.equal(argv.x.__proto__.z, undefined);
t.end();
});

Expand Down

0 comments on commit 38a4d1c

Please sign in to comment.