Skip to content

Commit

Permalink
Fix printable chars check
Browse files Browse the repository at this point in the history
  • Loading branch information
pinosu committed Oct 5, 2023
1 parent 1dd813d commit 0de4811
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
20 changes: 19 additions & 1 deletion x/wasm/types/tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func TestInstantiateContractValidation(t *testing.T) {
},
valid: false,
},
"non printable chars in label": {
"non printable chars ending label": {
msg: MsgInstantiateContract{
Sender: goodAddress,
CodeID: firstCodeID,
Expand All @@ -134,6 +134,24 @@ func TestInstantiateContractValidation(t *testing.T) {
},
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
13 changes: 8 additions & 5 deletions x/wasm/types/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,14 @@ func ValidateLabel(label string) error {
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")
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 0de4811

Please sign in to comment.