-
Notifications
You must be signed in to change notification settings - Fork 57
MM-69978: strengthen validation of card property data #243
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鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| // Copyright (c) 2020-present Mattermost, Inc. All Rights Reserved. | ||
| // See LICENSE.txt for license information. | ||
|
|
||
| package model | ||
|
|
||
| import "fmt" | ||
|
|
||
| type ErrInvalidCardProperty struct { | ||
| msg string | ||
| } | ||
|
|
||
| func NewErrInvalidCardProperty(msg string) ErrInvalidCardProperty { | ||
| return ErrInvalidCardProperty{msg: msg} | ||
| } | ||
|
|
||
| func (e ErrInvalidCardProperty) Error() string { | ||
| return fmt.Sprintf("invalid card property, %s", e.msg) | ||
| } | ||
|
|
||
| // Card property templates and values are persisted as free-form JSON and | ||
| // rendered directly by the clients, which only handle strings and arrays of | ||
| // strings. Anything else is rejected before it reaches the database. | ||
| func IsValidCardPropertyValue(value any) bool { | ||
| switch v := value.(type) { | ||
| case nil, string, []string: | ||
| return true | ||
| case []any: | ||
| for _, item := range v { | ||
| if _, ok := item.(string); !ok { | ||
| return false | ||
| } | ||
| } | ||
| return true | ||
| default: | ||
| return false | ||
| } | ||
| } | ||
|
|
||
| func ValidateCardPropertyValues(values map[string]any) error { | ||
| for propertyID, value := range values { | ||
| if !IsValidCardPropertyValue(value) { | ||
| return NewErrInvalidCardProperty(fmt.Sprintf("value of property %q must be a string or an array of strings", propertyID)) | ||
| } | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func ValidateCardPropertyTemplate(template map[string]any) error { | ||
| id, ok := template["id"].(string) | ||
| if !ok || id == "" { | ||
| return NewErrInvalidCardProperty("id must be a non empty string") | ||
| } | ||
|
|
||
| for _, field := range []string{"name", "type"} { | ||
| value, exists := template[field] | ||
| if !exists { | ||
| continue | ||
| } | ||
|
|
||
| if _, ok := value.(string); !ok { | ||
| return NewErrInvalidCardProperty(fmt.Sprintf("%s of property %q must be a string", field, id)) | ||
| } | ||
| } | ||
|
|
||
| return validateCardPropertyOptions(id, template["options"]) | ||
| } | ||
|
|
||
| func ValidateCardPropertyTemplates(templates []map[string]any) error { | ||
| for _, template := range templates { | ||
| if err := ValidateCardPropertyTemplate(template); err != nil { | ||
| return err | ||
| } | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func validateCardPropertyOptions(propertyID string, options any) error { | ||
| invalidErr := NewErrInvalidCardProperty(fmt.Sprintf("options of property %q must be a list of objects with string fields", propertyID)) | ||
|
|
||
| switch opts := options.(type) { | ||
| case nil: | ||
| return nil | ||
| case []any: | ||
| for _, option := range opts { | ||
| optionMap, ok := option.(map[string]any) | ||
| if !ok || !isValidCardPropertyOption(optionMap) { | ||
| return invalidErr | ||
| } | ||
| } | ||
| case []map[string]any: | ||
| for _, option := range opts { | ||
| if !isValidCardPropertyOption(option) { | ||
| return invalidErr | ||
| } | ||
| } | ||
| default: | ||
| return invalidErr | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func isValidCardPropertyOption(option map[string]any) bool { | ||
| for _, field := range []string{"id", "value", "color"} { | ||
| value, exists := option[field] | ||
| if !exists { | ||
| continue | ||
| } | ||
|
|
||
| if _, ok := value.(string); !ok { | ||
| return false | ||
| } | ||
| } | ||
|
|
||
| return true | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.