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 eab0cb6 commit c893d30
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 7 deletions.
23 changes: 18 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,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 @@ -657,6 +657,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 @@ -676,11 +680,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 @@ -952,4 +958,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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "yargs-parser",
"version": "15.0.0",
"version": "15.0.1",
"description": "the mighty option parser used by yargs",
"main": "index.js",
"scripts": {
Expand Down
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 @@ -429,6 +429,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 @@ -3275,4 +3294,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 c893d30

Please sign in to comment.