Skip to content

Commit

Permalink
Add label validation for non printable chars (CosmWasm#1650)
Browse files Browse the repository at this point in the history
* Add label validation for non printable chars

* Fix printable chars check
  • Loading branch information
pinosu authored Oct 9, 2023
1 parent 99367e1 commit 1a22c29
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
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,
},
"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 {
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

0 comments on commit 1a22c29

Please sign in to comment.