Skip to content

Commit

Permalink
fix: backport __proto__ fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
bcoe committed Mar 13, 2020
1 parent 7e01a2c commit 6e36df1
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 6 deletions.
23 changes: 18 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ function parse (args, opts) {
setKey(argv, splitKey, value)

// handle populating aliases of the full key
if (flags.aliases[key]) {
if (flags.aliases[key] && flags.aliases[key].forEach) {
flags.aliases[key].forEach(function (x) {
x = x.split('.')
setKey(argv, x, value)
Expand Down Expand Up @@ -659,6 +659,10 @@ function parse (args, opts) {
if (!configuration['dot-notation']) keys = [keys.join('.')]

keys.slice(0, -1).forEach(function (key, index) {
// TODO(bcoe): in the next major version of yargs, switch to
// Object.create(null) for dot notation:
key = sanitizeKey(key)

if (typeof o === 'object' && o[key] === undefined) {
o[key] = {}
}
Expand All @@ -678,11 +682,13 @@ function parse (args, opts) {
}
})

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

var isTypeArray = checkAllAliases(keys.join('.'), flags.arrays)
var isValueArray = Array.isArray(value)
var duplicate = configuration['duplicate-arguments-array']
const isTypeArray = checkAllAliases(keys.join('.'), flags.arrays)
const isValueArray = Array.isArray(value)
let duplicate = configuration['duplicate-arguments-array']

// nargs has higher priority than duplicate
if (!duplicate && checkAllAliases(key, flags.nargs)) {
Expand Down Expand Up @@ -900,4 +906,11 @@ Parser.detailed = function (args, opts) {
return parse(args.slice(), opts)
}

// TODO(bcoe): in the next major version of yargs, switch to
// Object.create(null) for dot notation:
function sanitizeKey (key) {
if (key === '__proto__') return '___proto___'
return key
}

module.exports = Parser
11 changes: 10 additions & 1 deletion test/fixtures/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,14 @@
"z": 55,
"foo": "baz",
"version": "1.0.2",
"truthy": true
"truthy": true,
"toString": "method name",
"__proto__": {
"aaa": 99
},
"bar": {
"__proto__": {
"bbb": 100
}
}
}
26 changes: 26 additions & 0 deletions test/yargs-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,25 @@ describe('yargs-parser', function () {
describe('config', function () {
var jsonPath = path.resolve(__dirname, './fixtures/config.json')

it('should not pollute the prototype', function () {
const argv = parser(['--foo', 'bar'], {
alias: {
z: 'zoom'
},
default: {
settings: jsonPath
},
config: 'settings'
})

argv.should.have.property('herp', 'derp')
argv.should.have.property('zoom', 55)
argv.should.have.property('foo').and.deep.equal('bar')

expect({}.bbb).to.equal(undefined)
expect({}.aaa).to.equal(undefined)
})

// See: https://github.com/chevex/yargs/issues/12
it('should load options and values from default config if specified', function () {
var argv = parser([ '--foo', 'bar' ], {
Expand Down Expand Up @@ -2963,4 +2982,11 @@ describe('yargs-parser', function () {
})
})
})

it('should not pollute the prototype', function () {
parser(['-f.__proto__.foo', '99', '-x.y.__proto__.bar', '100', '--__proto__', '200'])
Object.keys({}.__proto__).length.should.equal(0) // eslint-disable-line
expect({}.foo).to.equal(undefined)
expect({}.bar).to.equal(undefined)
})
})

0 comments on commit 6e36df1

Please # to comment.