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 label validation for non printable chars #1650

Merged
merged 2 commits into from
Oct 9, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions x/wasm/types/tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,24 @@ func TestInstantiateContractValidation(t *testing.T) {
},
valid: false,
},
"white space ending label": {
msg: MsgInstantiateContract{
Sender: goodAddress,
CodeID: firstCodeID,
Label: "foo ",
Msg: []byte("{}"),
},
valid: false,
},
"non printable chars in label": {
msg: MsgInstantiateContract{
Sender: goodAddress,
CodeID: firstCodeID,
Label: "foo\v",
Msg: []byte("{}"),
},
valid: false,
},
alpe marked this conversation as resolved.
Show resolved Hide resolved
"label too long": {
msg: MsgInstantiateContract{
Sender: goodAddress,
Expand Down
7 changes: 7 additions & 0 deletions x/wasm/types/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"fmt"
"net/url"
"strings"
"unicode"

"github.com/distribution/reference"

Expand Down Expand Up @@ -45,6 +46,12 @@
if label != strings.TrimSpace(label) {
return ErrInvalid.Wrap("label must not start/end with whitespaces")
}
labelWithoutNonPrintableChars := strings.TrimFunc(label, func(r rune) bool {
return !unicode.IsGraphic(r)
})
if label != labelWithoutNonPrintableChars {
return ErrInvalid.Wrap("label must not have non printable characters")

Check warning on line 53 in x/wasm/types/validation.go

View check run for this annotation

Codecov / codecov/patch

x/wasm/types/validation.go#L53

Added line #L53 was not covered by tests
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: let's avoid double negative ;-)

Suggested change
return ErrInvalid.Wrap("label must not have non printable characters")
return ErrInvalid.Wrap("label must have printable characters only")

}
return nil
}

Expand Down