Skip to content

Commit d3d28a0

Browse files
mergify[bot]pinosu
andauthored
Add label validation for non printable chars (#1650) (#1659)
* Add label validation for non printable chars * Fix printable chars check (cherry picked from commit 1a22c29) Co-authored-by: pinosu <[email protected]>
1 parent c51dcca commit d3d28a0

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
@@ -115,6 +115,42 @@ func TestInstantiateContractValidation(t *testing.T) {
115115
},
116116
valid: false,
117117
},
118+
"white space ending label": {
119+
msg: MsgInstantiateContract{
120+
Sender: goodAddress,
121+
CodeID: firstCodeID,
122+
Label: "foo ",
123+
Msg: []byte("{}"),
124+
},
125+
valid: false,
126+
},
127+
"non printable chars ending label": {
128+
msg: MsgInstantiateContract{
129+
Sender: goodAddress,
130+
CodeID: firstCodeID,
131+
Label: "foo\v",
132+
Msg: []byte("{}"),
133+
},
134+
valid: false,
135+
},
136+
"non printable chars in label": {
137+
msg: MsgInstantiateContract{
138+
Sender: goodAddress,
139+
CodeID: firstCodeID,
140+
Label: "f\voo",
141+
Msg: []byte("{}"),
142+
},
143+
valid: false,
144+
},
145+
"non printable chars beginning label": {
146+
msg: MsgInstantiateContract{
147+
Sender: goodAddress,
148+
CodeID: firstCodeID,
149+
Label: "\vfoo",
150+
Msg: []byte("{}"),
151+
},
152+
valid: false,
153+
},
118154
"label too long": {
119155
msg: MsgInstantiateContract{
120156
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/docker/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)