Skip to content

Commit

Permalink
Protect against circular extended configurations
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan Vance authored and Ryan Vance committed Feb 19, 2017
1 parent 0426aef commit 08ddd1b
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 0 deletions.
15 changes: 15 additions & 0 deletions lib/apply-extends.js
@@ -1,12 +1,25 @@
var fs = require('fs')
var path = require('path')
var assign = require('./assign')
var YError = require('./yerror')

var previouslyVisitedConfigs = []

function checkForCircularExtends (path) {
if (previouslyVisitedConfigs.indexOf(path) > -1) {
throw new YError("Circular extended configurations: '" + path + "'.")
}
}

function applyExtends (config, cwd, subKey) {
var defaultConfig = {}

if (config.hasOwnProperty('extends')) {
var pathToDefault = path.join(cwd, config.extends)

checkForCircularExtends(pathToDefault)

previouslyVisitedConfigs.push(pathToDefault)
delete config.extends

defaultConfig = JSON.parse(fs.readFileSync(pathToDefault, 'utf8'))
Expand All @@ -16,6 +29,8 @@ function applyExtends (config, cwd, subKey) {
defaultConfig = applyExtends(defaultConfig, path.dirname(pathToDefault), subKey)
}

previouslyVisitedConfigs = []

return assign(defaultConfig, config)
}

Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/extends/circular_1.json
@@ -0,0 +1,4 @@
{
"a": 44,
"extends": "./circular_2.json"
}
4 changes: 4 additions & 0 deletions test/fixtures/extends/circular_2.json
@@ -0,0 +1,4 @@
{
"b": "any",
"extends": "./circular_1.json"
}
7 changes: 7 additions & 0 deletions test/yargs.js
Expand Up @@ -5,6 +5,7 @@ var fs = require('fs')
var path = require('path')
var checkOutput = require('./helpers/utils').checkOutput
var yargs = require('../')
var YError = require('../lib/yerror')

require('chai').should()

Expand Down Expand Up @@ -1169,6 +1170,12 @@ describe('yargs dsl tests', function () {
argv.b.should.equal(22)
argv.z.should.equal(15)
})

it('protects against circular extended configurations', function () {
expect(function () {
yargs.config({extends: './test/fixtures/extends/circular_1.json'})
}).to.throw(YError)
})
})
})

Expand Down

0 comments on commit 08ddd1b

Please sign in to comment.