From 1dd813d249cd88e67b7296e33ee7ac1e9773a0ad Mon Sep 17 00:00:00 2001 From: Pino' Surace Date: Thu, 5 Oct 2023 09:21:25 +0200 Subject: [PATCH 1/2] Add label validation for non printable chars --- x/wasm/types/tx_test.go | 18 ++++++++++++++++++ x/wasm/types/validation.go | 7 +++++++ 2 files changed, 25 insertions(+) diff --git a/x/wasm/types/tx_test.go b/x/wasm/types/tx_test.go index fea5d7e14b..4533991324 100644 --- a/x/wasm/types/tx_test.go +++ b/x/wasm/types/tx_test.go @@ -116,6 +116,24 @@ 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 in label": { + msg: MsgInstantiateContract{ + Sender: goodAddress, + CodeID: firstCodeID, + Label: "foo\v", + Msg: []byte("{}"), + }, + valid: false, + }, "label too long": { msg: MsgInstantiateContract{ Sender: goodAddress, diff --git a/x/wasm/types/validation.go b/x/wasm/types/validation.go index 20cca98fd6..b940a901cc 100644 --- a/x/wasm/types/validation.go +++ b/x/wasm/types/validation.go @@ -4,6 +4,7 @@ import ( "fmt" "net/url" "strings" + "unicode" "github.com/distribution/reference" @@ -45,6 +46,12 @@ 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") + } return nil } From 0de4811c2517bce13f9b9810f8e8be83cc581a93 Mon Sep 17 00:00:00 2001 From: Pino' Surace Date: Thu, 5 Oct 2023 14:20:48 +0200 Subject: [PATCH 2/2] Fix printable chars check --- x/wasm/types/tx_test.go | 20 +++++++++++++++++++- x/wasm/types/validation.go | 13 ++++++++----- 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/x/wasm/types/tx_test.go b/x/wasm/types/tx_test.go index 4533991324..1045b95028 100644 --- a/x/wasm/types/tx_test.go +++ b/x/wasm/types/tx_test.go @@ -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, @@ -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, diff --git a/x/wasm/types/validation.go b/x/wasm/types/validation.go index b940a901cc..bd67e3bc26 100644 --- a/x/wasm/types/validation.go +++ b/x/wasm/types/validation.go @@ -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 }