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 all commits
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
36 changes: 36 additions & 0 deletions x/wasm/types/tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,42 @@ 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 ending label": {
msg: MsgInstantiateContract{
Sender: goodAddress,
CodeID: firstCodeID,
Label: "foo\v",
Msg: []byte("{}"),
},
valid: false,
},
alpe marked this conversation as resolved.
Show resolved Hide resolved
"non printable chars in label": {
msg: MsgInstantiateContract{
Sender: goodAddress,
CodeID: firstCodeID,
Label: "f\voo",
Msg: []byte("{}"),
},
valid: false,
},
"non printable chars beginning label": {
msg: MsgInstantiateContract{
Sender: goodAddress,
CodeID: firstCodeID,
Label: "\vfoo",
Msg: []byte("{}"),
},
valid: false,
},
"label too long": {
msg: MsgInstantiateContract{
Sender: goodAddress,
Expand Down
10 changes: 10 additions & 0 deletions x/wasm/types/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"net/url"
"strings"
"unicode"

"github.com/distribution/reference"

Expand Down Expand Up @@ -45,6 +46,15 @@ func ValidateLabel(label string) error {
if label != strings.TrimSpace(label) {
return ErrInvalid.Wrap("label must not start/end with whitespaces")
}
labelWithPrintableCharsOnly := strings.Map(func(r rune) rune {
Copy link
Contributor

Choose a reason for hiding this comment

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

very nice use of strings.Map 👍

if unicode.IsPrint(r) {
return r
}
return -1
}, label)
if label != labelWithPrintableCharsOnly {
return ErrInvalid.Wrap("label must have printable characters only")
}
return nil
}

Expand Down