Add label validation for non printable chars (#1650)
* Add label validation for non printable chars * Fix printable chars check
This commit is contained in:
@@ -116,6 +116,42 @@ func TestInstantiateContractValidation(t *testing.T) {
|
|||||||
},
|
},
|
||||||
valid: false,
|
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": {
|
"label too long": {
|
||||||
msg: MsgInstantiateContract{
|
msg: MsgInstantiateContract{
|
||||||
Sender: goodAddress,
|
Sender: goodAddress,
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"net/url"
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
|
"unicode"
|
||||||
|
|
||||||
"github.com/distribution/reference"
|
"github.com/distribution/reference"
|
||||||
|
|
||||||
@@ -45,6 +46,15 @@ func ValidateLabel(label string) error {
|
|||||||
if label != strings.TrimSpace(label) {
|
if label != strings.TrimSpace(label) {
|
||||||
return ErrInvalid.Wrap("label must not start/end with whitespaces")
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user