Config - don't allow setting key-paths inside of strings, arrays, etc

This commit is contained in:
Max Brunsfeld
2015-07-30 18:28:06 -07:00
parent 9e9f670ac3
commit 84f72c880a
2 changed files with 22 additions and 5 deletions

View File

@@ -1142,8 +1142,8 @@ describe "Config", ->
type: 'integer'
default: 12
expect(atom.config.getSchema('foo.baz')).toBeUndefined()
expect(atom.config.getSchema('foo.bar.anInt.baz')).toBeUndefined()
expect(atom.config.getSchema('foo.baz')).toBe(null)
expect(atom.config.getSchema('foo.bar.anInt.baz')).toBe(false)
it "respects the schema for scoped settings", ->
schema =
@@ -1380,6 +1380,10 @@ describe "Config", ->
expect(atom.config.set('foo.bar.aString', nope: 'nope')).toBe false
expect(atom.config.get('foo.bar.aString')).toBe 'ok'
it 'does not allow setting children of that key-path', ->
expect(atom.config.set('foo.bar.aString.something', 123)).toBe false
expect(atom.config.get('foo.bar.aString')).toBe 'ok'
describe 'when the schema has a "maximumLength" key', ->
it "trims the string to be no longer than the specified maximum", ->
schema =
@@ -1438,6 +1442,11 @@ describe "Config", ->
atom.config.set 'foo.bar', ['2', '3', '4']
expect(atom.config.get('foo.bar')).toEqual [2, 3, 4]
it 'does not allow setting children of that key-path', ->
expect(atom.config.set('foo.bar.child', 123)).toBe false
expect(atom.config.set('foo.bar.child.grandchild', 123)).toBe false
expect(atom.config.get('foo.bar')).toEqual [1, 2, 3]
describe 'when the value has a "color" type', ->
beforeEach ->
schema =

View File

@@ -665,12 +665,18 @@ class Config
#
# Returns an {Object} eg. `{type: 'integer', default: 23, minimum: 1}`.
# Returns `null` when the keyPath has no schema specified.
# Returns `false` when the key-path is not accessible from the root schema.
getSchema: (keyPath) ->
keys = splitKeyPath(keyPath)
schema = @schema
for key in keys
break unless schema?
schema = schema.properties?[key]
if schema.type is 'object'
childSchema = schema.properties?[key]
unless childSchema?
return null
else
return false
schema = childSchema
schema
# Extended: Get the {String} path to the config file being used.
@@ -948,7 +954,9 @@ class Config
catch e
undefined
else
value = @constructor.executeSchemaEnforcers(keyPath, value, schema) if schema = @getSchema(keyPath)
if (schema = @getSchema(keyPath))?
throw new Error("Illegal key path #{keyPath}") if schema is false
value = @constructor.executeSchemaEnforcers(keyPath, value, schema)
value
# When the schema is changed / added, there may be values set in the config