From 490fba9243e6cb291462e9d3c1bcbd1975c0df1e Mon Sep 17 00:00:00 2001 From: Tom Langer Date: Thu, 10 Sep 2020 16:16:30 +0300 Subject: [PATCH] Fix ErrCreateFailed code + zip size limit (#540) * Fix ErrCreateFailed code * Enlarge gzip size limit * Point to the same const * Fixed tests --- x/compute/internal/keeper/ioutil.go | 6 ++---- x/compute/internal/keeper/ioutil_test.go | 9 +++++---- x/compute/internal/types/errors.go | 6 +++--- x/compute/internal/types/validation.go | 2 +- 4 files changed, 11 insertions(+), 12 deletions(-) diff --git a/x/compute/internal/keeper/ioutil.go b/x/compute/internal/keeper/ioutil.go index 74b34d1b7..640545e1c 100644 --- a/x/compute/internal/keeper/ioutil.go +++ b/x/compute/internal/keeper/ioutil.go @@ -3,6 +3,7 @@ package keeper import ( "bytes" "compress/gzip" + "github.com/enigmampc/SecretNetwork/x/compute/internal/types" "io" "io/ioutil" ) @@ -12,9 +13,6 @@ import ( // and https://github.com/golang/go/blob/master/src/net/http/sniff.go#L186 var gzipIdent = []byte("\x1F\x8B\x08") -// limit max bytes read to prevent gzip bombs -const maxSize = 400 * 1024 - // uncompress returns gzip uncompressed content or given src when not gzip. func uncompress(src []byte) ([]byte, error) { if len(src) < 3 { @@ -29,5 +27,5 @@ func uncompress(src []byte) ([]byte, error) { } zr.Multistream(false) - return ioutil.ReadAll(io.LimitReader(zr, maxSize)) + return ioutil.ReadAll(io.LimitReader(zr, types.MaxWasmSize)) } diff --git a/x/compute/internal/keeper/ioutil_test.go b/x/compute/internal/keeper/ioutil_test.go index f5f48592e..ff999c3cf 100644 --- a/x/compute/internal/keeper/ioutil_test.go +++ b/x/compute/internal/keeper/ioutil_test.go @@ -9,6 +9,7 @@ import ( "strings" "testing" + "github.com/enigmampc/SecretNetwork/x/compute/internal/types" "github.com/stretchr/testify/require" ) @@ -41,8 +42,8 @@ func TestUncompress(t *testing.T) { expResult: []byte{0x1, 0x2}, }, "handle big input slice": { - src: []byte(strings.Repeat("a", maxSize+1)), - expResult: []byte(strings.Repeat("a", maxSize+1)), + src: []byte(strings.Repeat("a", types.MaxWasmSize+1)), + expResult: []byte(strings.Repeat("a", types.MaxWasmSize+1)), }, "handle gzip identifier only": { src: gzipIdent, @@ -57,11 +58,11 @@ func TestUncompress(t *testing.T) { expError: io.ErrUnexpectedEOF, }, "handle big gzip output": { - src: asGzip(strings.Repeat("a", maxSize+1)), + src: asGzip(strings.Repeat("a", types.MaxWasmSize+1)), expError: io.ErrUnexpectedEOF, }, "handle other big gzip output": { - src: asGzip(strings.Repeat("a", 2*maxSize)), + src: asGzip(strings.Repeat("a", 2*types.MaxWasmSize)), expError: io.ErrUnexpectedEOF, }, } diff --git a/x/compute/internal/types/errors.go b/x/compute/internal/types/errors.go index 700f0e52b..567a09776 100644 --- a/x/compute/internal/types/errors.go +++ b/x/compute/internal/types/errors.go @@ -13,9 +13,6 @@ import ( var ( DefaultCodespace = ModuleName - // ErrCreateFailed error for wasm code that has already been uploaded or failed - ErrCreateFailed = sdkErrors.Register(DefaultCodespace, 1, "create contract failed") - // ErrInstantiateFailed error for rust instantiate contract failure ErrInstantiateFailed = sdkErrors.Register(DefaultCodespace, 2, "instantiate contract failed") @@ -54,6 +51,9 @@ var ( // ErrDuplicate error for content that exsists ErrDuplicate = sdkErrors.Register(DefaultCodespace, 14, "duplicate") + + // ErrCreateFailed error for wasm code that has already been uploaded or failed + ErrCreateFailed = sdkErrors.Register(DefaultCodespace, 15, "create contract failed") ) func IsEncryptedErrorCode(code uint32) bool { diff --git a/x/compute/internal/types/validation.go b/x/compute/internal/types/validation.go index 87cf1aa63..f03fda050 100644 --- a/x/compute/internal/types/validation.go +++ b/x/compute/internal/types/validation.go @@ -8,7 +8,7 @@ import ( ) const ( - MaxWasmSize = 2 * 1024 * 1024 + MaxWasmSize = 2 * 1024 * 1024 // 2MB // MaxLabelSize is the longest label that can be used when Instantiating a contract MaxLabelSize = 128