Skip to content

Commit

Permalink
Merge pull request canonical#13476 from MiguelPires/aspect-schema-key…
Browse files Browse the repository at this point in the history
…-format

aspects: check map key format in parsing and validation
  • Loading branch information
ernestl authored Jan 19, 2024
2 parents c6cd6f8 + a48690a commit 85fc26f
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
18 changes: 18 additions & 0 deletions aspects/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,10 @@ func (v *mapSchema) Validate(raw []byte) error {
return validationErrorf(`cannot accept null value for "map" type`)
}

if err := validMapKeys(mapValue); err != nil {
return validationErrorFrom(err)
}

if v.entrySchemas != nil {
for key := range mapValue {
if _, ok := v.entrySchemas[key]; !ok {
Expand Down Expand Up @@ -323,6 +327,16 @@ func (v *mapSchema) Validate(raw []byte) error {
return nil
}

func validMapKeys(v map[string]json.RawMessage) error {
for k := range v {
if !validSubkey.Match([]byte(k)) {
return fmt.Errorf(`key %q doesn't conform to required format`, k)
}
}

return nil
}

func (v *mapSchema) parseConstraints(constraints map[string]json.RawMessage) error {
err := checkExclusiveMapConstraints(constraints)
if err != nil {
Expand All @@ -336,6 +350,10 @@ func (v *mapSchema) parseConstraints(constraints map[string]json.RawMessage) err
return fmt.Errorf(`cannot parse map's "schema" constraint: %v`, err)
}

if err := validMapKeys(entries); err != nil {
return fmt.Errorf(`cannot parse map: %w`, err)
}

v.entrySchemas = make(map[string]Schema, len(entries))
for key, value := range entries {
entrySchema, err := v.topSchema.parse(value)
Expand Down
28 changes: 28 additions & 0 deletions aspects/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,34 @@ func (*schemaSuite) TestMapSchemaRequiredNotInSchema(c *C) {
c.Assert(err, ErrorMatches, `cannot parse map's "required" constraint: required key "baz" must have schema entry`)
}

func (*schemaSuite) TestMapSchemaWithInvalidKeyFormat(c *C) {
schemaStr := []byte(`{
"schema": {
"-foo": "string"
}
}`)

_, err := aspects.ParseSchema(schemaStr)
c.Assert(err, ErrorMatches, `cannot parse map: key "-foo" doesn't conform to required format`)
}

func (*schemaSuite) TestMapRejectsInputMapWithInvalidKeyFormat(c *C) {
schemaStr := []byte(`{
"schema": {
"foo": "int"
}
}`)

schema, err := aspects.ParseSchema(schemaStr)
c.Assert(err, IsNil)

input := []byte(`{
"-foo": 1
}`)
err = schema.Validate(input)
c.Assert(err, ErrorMatches, `cannot accept top level element: key "-foo" doesn't conform to required format`)
}

func (*schemaSuite) TestMapInvalidConstraintCombos(c *C) {
type testcase struct {
name string
Expand Down

0 comments on commit 85fc26f

Please sign in to comment.