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 (backport #1650) #1659

Merged
merged 1 commit into from
Oct 11, 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 @@ -115,6 +115,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,
},
"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/docker/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 {
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