Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add check for schema map[string]interface{} asssertion #269

Merged
merged 3 commits into from
Sep 12, 2019

Conversation

dbadura
Copy link
Contributor

@dbadura dbadura commented Sep 12, 2019

When you use GoLoader and put something that is not map[string]interface{} the library panic on nil value, because in draft.go, line 95 m := documentNode.(map[string]interface{}), assertion is not checked.
our issue

@dbadura dbadura marked this pull request as ready for review September 12, 2019 12:33
@dbadura
Copy link
Contributor Author

dbadura commented Sep 12, 2019

After testing this library with errcheck with -assert flag, I found more not checked assertions.

draft.go:95:7:  m := documentNode.(map[string]interface{})
jsonLoader.go:204:15:   defer f.Close()
schema.go:88:8: b := documentNode.(bool)
schema.go:102:7:        m := documentNode.(map[string]interface{})
schema.go:260:21:       arrayOfTypes := m[KEY_TYPE].([]interface{})
schema.go:299:41:       currentSchema.additionalProperties = m[KEY_ADDITIONAL_PROPERTIES].(bool)
schema.go:321:28:       patternPropertiesMap := m[KEY_PATTERN_PROPERTIES].(map[string]interface{})
schema.go:425:36:       currentSchema.additionalItems = m[KEY_ADDITIONAL_ITEMS].(bool)
schema.go:729:22:       requiredValues := m[KEY_REQUIRED].([]interface{})
schema.go:792:32:       currentSchema.uniqueItems = m[KEY_UNIQUE_ITEMS].(bool)
schema.go:1019:7:       m := documentNode.(map[string]interface{})
schema.go:1042:7:       m := documentNode.(map[string]interface{})
schema.go:1049:14:      values := m[k].([]interface{})
schemaPool.go:86:30:    p.parseReferencesRecursive(v, ref, draft)
schemaPool.go:128:33:   p.parseReferencesRecursive(v, *localRef, draft)
schemaPool.go:132:31:   p.parseReferencesRecursive(v, *localRef, draft)
schemaPool.go:203:19:   p.parseReferences(document, refToURL, true)
utils.go:119:16:        jsonNumber := what.(json.Number)
utils.go:137:13:        number := what.(json.Number)
utils.go:160:13:        number := what.(json.Number)
validation.go:119:13:   value := currentNode.(json.Number)
validation.go:173:24:   castCurrentNode := currentNode.([]interface{})
validation.go:198:24:   castCurrentNode = convertDocumentNode(currentNode).(map[string]interface{})
validation.go:231:14:   value := currentNode.(bool)
validation.go:253:14:   value := currentNode.(string)
validation.go:484:30:   additionalItemSchema := currentSubSchema.additionalItems.(*subSchema)
validation.go:712:17:   stringValue := value.(string)
validation.go:776:12:   number := value.(json.Number)

So I am going to fix.

@dbadura dbadura changed the title add check for schema map[string]interface{} asssertion WIP add check for schema map[string]interface{} asssertion Sep 12, 2019
Copy link
Collaborator

@johandorland johandorland left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is definitely an overlooked oopsie, so thanks for the PR. I have one minor nitpick, but that is about it. 👍

draft.go Outdated
@@ -92,7 +92,10 @@ func parseSchemaURL(documentNode interface{}) (string, *Draft, error) {
if isKind(documentNode, reflect.Bool) {
return "", nil, nil
}
m := documentNode.(map[string]interface{})
m, ok := documentNode.(map[string]interface{})
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Most of the code uses the isKind helper method, so to keep the style similar to the rest of the code it's probably better to change this into:

if !isKind(documentNode, reflect.Map) {
    return "", nil, errors.New("schema is invalid")
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed : )

@johandorland
Copy link
Collaborator

@dbadura Thank you for looking at those issues. I looked at a couple of them and I suspect most of them are false positives. From the couple I quickly looked at all of them had a type check somewhere, although not in the format errcheck expects. If you find another error that definitely needs to be checked, please let me know, but I don't want to clutter the code with redundant error handling because some linting tool said so.

@dbadura dbadura changed the title WIP add check for schema map[string]interface{} asssertion add check for schema map[string]interface{} asssertion Sep 12, 2019
@johandorland johandorland merged commit 6a016cf into xeipuuv:master Sep 12, 2019
@dbadura dbadura deleted the fix-schema-validator-panic branch September 12, 2019 15:22
@dbadura
Copy link
Contributor Author

dbadura commented Sep 12, 2019

Thank you. I noticed how you do the assertion type later : )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants