Skip to content

Commit 1a22c29

Browse files
authored
Add label validation for non printable chars (#1650)
* Add label validation for non printable chars * Fix printable chars check
1 parent 99367e1 commit 1a22c29

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

x/wasm/types/tx_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,42 @@ func TestInstantiateContractValidation(t *testing.T) {
116116
},
117117
valid: false,
118118
},
119+
"white space ending label": {
120+
msg: MsgInstantiateContract{
121+
Sender: goodAddress,
122+
CodeID: firstCodeID,
123+
Label: "foo ",
124+
Msg: []byte("{}"),
125+
},
126+
valid: false,
127+
},
128+
"non printable chars ending label": {
129+
msg: MsgInstantiateContract{
130+
Sender: goodAddress,
131+
CodeID: firstCodeID,
132+
Label: "foo\v",
133+
Msg: []byte("{}"),
134+
},
135+
valid: false,
136+
},
137+
"non printable chars in label": {
138+
msg: MsgInstantiateContract{
139+
Sender: goodAddress,
140+
CodeID: firstCodeID,
141+
Label: "f\voo",
142+
Msg: []byte("{}"),
143+
},
144+
valid: false,
145+
},
146+
"non printable chars beginning label": {
147+
msg: MsgInstantiateContract{
148+
Sender: goodAddress,
149+
CodeID: firstCodeID,
150+
Label: "\vfoo",
151+
Msg: []byte("{}"),
152+
},
153+
valid: false,
154+
},
119155
"label too long": {
120156
msg: MsgInstantiateContract{
121157
Sender: goodAddress,

x/wasm/types/validation.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"net/url"
66
"strings"
7+
"unicode"
78

89
"github.com/distribution/reference"
910

@@ -45,6 +46,15 @@ func ValidateLabel(label string) error {
4546
if label != strings.TrimSpace(label) {
4647
return ErrInvalid.Wrap("label must not start/end with whitespaces")
4748
}
49+
labelWithPrintableCharsOnly := strings.Map(func(r rune) rune {
50+
if unicode.IsPrint(r) {
51+
return r
52+
}
53+
return -1
54+
}, label)
55+
if label != labelWithPrintableCharsOnly {
56+
return ErrInvalid.Wrap("label must have printable characters only")
57+
}
4858
return nil
4959
}
5060

0 commit comments

Comments
 (0)