Skip to content

Commit c893d30

Browse files
author
bcoe
committed
fix: backport __proto__ fixes
1 parent eab0cb6 commit c893d30

File tree

4 files changed

+55
-7
lines changed

4 files changed

+55
-7
lines changed

index.js

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@ function parse (args, opts) {
417417
setKey(argv, splitKey, value)
418418

419419
// handle populating aliases of the full key
420-
if (flags.aliases[key]) {
420+
if (flags.aliases[key] && flags.aliases[key].forEach) {
421421
flags.aliases[key].forEach(function (x) {
422422
x = x.split('.')
423423
setKey(argv, x, value)
@@ -657,6 +657,10 @@ function parse (args, opts) {
657657
if (!configuration['dot-notation']) keys = [keys.join('.')]
658658

659659
keys.slice(0, -1).forEach(function (key, index) {
660+
// TODO(bcoe): in the next major version of yargs, switch to
661+
// Object.create(null) for dot notation:
662+
key = sanitizeKey(key)
663+
660664
if (typeof o === 'object' && o[key] === undefined) {
661665
o[key] = {}
662666
}
@@ -676,11 +680,13 @@ function parse (args, opts) {
676680
}
677681
})
678682

679-
var key = keys[keys.length - 1]
683+
// TODO(bcoe): in the next major version of yargs, switch to
684+
// Object.create(null) for dot notation:
685+
const key = sanitizeKey(keys[keys.length - 1])
680686

681-
var isTypeArray = checkAllAliases(keys.join('.'), flags.arrays)
682-
var isValueArray = Array.isArray(value)
683-
var duplicate = configuration['duplicate-arguments-array']
687+
const isTypeArray = checkAllAliases(keys.join('.'), flags.arrays)
688+
const isValueArray = Array.isArray(value)
689+
let duplicate = configuration['duplicate-arguments-array']
684690

685691
// nargs has higher priority than duplicate
686692
if (!duplicate && checkAllAliases(key, flags.nargs)) {
@@ -952,4 +958,11 @@ Parser.detailed = function (args, opts) {
952958
return parse(args.slice(), opts)
953959
}
954960

961+
// TODO(bcoe): in the next major version of yargs, switch to
962+
// Object.create(null) for dot notation:
963+
function sanitizeKey (key) {
964+
if (key === '__proto__') return '___proto___'
965+
return key
966+
}
967+
955968
module.exports = Parser

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "yargs-parser",
3-
"version": "15.0.0",
3+
"version": "15.0.1",
44
"description": "the mighty option parser used by yargs",
55
"main": "index.js",
66
"scripts": {

test/fixtures/config.json

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,14 @@
33
"z": 55,
44
"foo": "baz",
55
"version": "1.0.2",
6-
"truthy": true
6+
"truthy": true,
7+
"toString": "method name",
8+
"__proto__": {
9+
"aaa": 99
10+
},
11+
"bar": {
12+
"__proto__": {
13+
"bbb": 100
14+
}
15+
}
716
}

test/yargs-parser.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,25 @@ describe('yargs-parser', function () {
429429
describe('config', function () {
430430
var jsonPath = path.resolve(__dirname, './fixtures/config.json')
431431

432+
it('should not pollute the prototype', function () {
433+
const argv = parser(['--foo', 'bar'], {
434+
alias: {
435+
z: 'zoom'
436+
},
437+
default: {
438+
settings: jsonPath
439+
},
440+
config: 'settings'
441+
})
442+
443+
argv.should.have.property('herp', 'derp')
444+
argv.should.have.property('zoom', 55)
445+
argv.should.have.property('foo').and.deep.equal('bar')
446+
447+
expect({}.bbb).to.equal(undefined)
448+
expect({}.aaa).to.equal(undefined)
449+
})
450+
432451
// See: https://github.com/chevex/yargs/issues/12
433452
it('should load options and values from default config if specified', function () {
434453
var argv = parser(['--foo', 'bar'], {
@@ -3275,4 +3294,11 @@ describe('yargs-parser', function () {
32753294
})
32763295
})
32773296
})
3297+
3298+
it('should not pollute the prototype', function () {
3299+
parser(['-f.__proto__.foo', '99', '-x.y.__proto__.bar', '100', '--__proto__', '200'])
3300+
Object.keys({}.__proto__).length.should.equal(0) // eslint-disable-line
3301+
expect({}.foo).to.equal(undefined)
3302+
expect({}.bar).to.equal(undefined)
3303+
})
32783304
})

0 commit comments

Comments
 (0)