Skip to content

Commit

Permalink
Fix ErrCreateFailed code + zip size limit (#540)
Browse files Browse the repository at this point in the history
* Fix ErrCreateFailed code

* Enlarge gzip size limit

* Point to the same const

* Fixed tests
  • Loading branch information
toml01 authored Sep 10, 2020
1 parent 8dc0347 commit 490fba9
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 12 deletions.
6 changes: 2 additions & 4 deletions x/compute/internal/keeper/ioutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package keeper
import (
"bytes"
"compress/gzip"
"github.com/enigmampc/SecretNetwork/x/compute/internal/types"
"io"
"io/ioutil"
)
Expand All @@ -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 {
Expand All @@ -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))
}
9 changes: 5 additions & 4 deletions x/compute/internal/keeper/ioutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"strings"
"testing"

"github.com/enigmampc/SecretNetwork/x/compute/internal/types"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -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,
Expand All @@ -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,
},
}
Expand Down
6 changes: 3 additions & 3 deletions x/compute/internal/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion x/compute/internal/types/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 490fba9

Please sign in to comment.