Skip to content

Commit

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

* Fix printable chars check

(cherry picked from commit 1a22c29)

Co-authored-by: pinosu <95283998+pinosu@users.noreply.github.com>
  • Loading branch information
mergify[bot] and pinosu authored Oct 11, 2023
1 parent c51dcca commit d3d28a0
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 @@ -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

0 comments on commit d3d28a0

Please sign in to comment.