Skip to content

Commit

Permalink
Remove check for wasm limit size in state sync
Browse files Browse the repository at this point in the history
  • Loading branch information
pinosu committed Jun 29, 2023
1 parent 448cc64 commit 24eb158
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 4 deletions.
2 changes: 1 addition & 1 deletion x/wasm/client/cli/gov_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func parseVerificationFlags(gzippedWasm []byte, flags *flag.FlagSet) (string, st
// wasm is gzipped in parseStoreCodeArgs
// checksum generation will be decoupled here
// reference https://github.com/CosmWasm/wasmvm/issues/359
raw, err := ioutils.Uncompress(gzippedWasm, uint64(types.MaxWasmSize))
raw, err := ioutils.UncompressWithLimit(gzippedWasm, uint64(types.MaxWasmSize))
if err != nil {
return "", "", nil, fmt.Errorf("invalid zip: %w", err)
}
Expand Down
16 changes: 15 additions & 1 deletion x/wasm/ioutils/ioutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,21 @@ import (
)

// Uncompress expects a valid gzip source to unpack or fails. See IsGzip
func Uncompress(gzipSrc []byte, limit uint64) ([]byte, error) {
func Uncompress(gzipSrc []byte) ([]byte, error) {
zr, err := gzip.NewReader(bytes.NewReader(gzipSrc))
if err != nil {
return nil, err
}
zr.Multistream(false)
defer zr.Close()

bz, err := io.ReadAll(zr)
return bz, err
}

// UncompressWithLimit fails if wasm size exceeds the limit.
// It expects a valid gzip source to unpack or fails. See IsGzip
func UncompressWithLimit(gzipSrc []byte, limit uint64) ([]byte, error) {
if uint64(len(gzipSrc)) > limit {
return nil, types.ErrLimit.Wrapf("max %d bytes", limit)
}
Expand Down
47 changes: 46 additions & 1 deletion x/wasm/ioutils/ioutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,51 @@ func TestUncompress(t *testing.T) {
wasmGzipped, err := os.ReadFile("../keeper/testdata/hackatom.wasm.gzip")
require.NoError(t, err)

specs := map[string]struct {
src []byte
expError error
expResult []byte
}{
"handle wasm compressed": {
src: wasmGzipped,
expResult: wasmRaw,
},
"handle gzip identifier only": {
src: gzipIdent,
expError: io.ErrUnexpectedEOF,
},
"handle broken gzip": {
src: append(gzipIdent, byte(0x1)),
expError: io.ErrUnexpectedEOF,
},
"handle incomplete gzip": {
src: wasmGzipped[:len(wasmGzipped)-5],
expError: io.ErrUnexpectedEOF,
},
"handle limit gzip output": {
src: asGzip(bytes.Repeat([]byte{0x1}, 400_000)),
expResult: bytes.Repeat([]byte{0x1}, 400_000),
},
}
for msg, spec := range specs {
t.Run(msg, func(t *testing.T) {
r, err := Uncompress(spec.src)
require.True(t, errors.Is(spec.expError, err), "exp %v got %+v", spec.expError, err)
if spec.expError != nil {
return
}
assert.Equal(t, spec.expResult, r)
})
}
}

func TestUncompressWithLimit(t *testing.T) {
wasmRaw, err := os.ReadFile("../keeper/testdata/hackatom.wasm")
require.NoError(t, err)

wasmGzipped, err := os.ReadFile("../keeper/testdata/hackatom.wasm.gzip")
require.NoError(t, err)

const maxSize = 400_000

specs := map[string]struct {
Expand Down Expand Up @@ -61,7 +106,7 @@ func TestUncompress(t *testing.T) {
}
for msg, spec := range specs {
t.Run(msg, func(t *testing.T) {
r, err := Uncompress(spec.src, maxSize)
r, err := UncompressWithLimit(spec.src, maxSize)
require.True(t, errors.Is(spec.expError, err), "exp %v got %+v", spec.expError, err)
if spec.expError != nil {
return
Expand Down
2 changes: 1 addition & 1 deletion x/wasm/keeper/snapshotter.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func restoreV1(_ sdk.Context, k *Keeper, compressedCode []byte) error {
if !ioutils.IsGzip(compressedCode) {
return types.ErrInvalid.Wrap("not a gzip")
}
wasmCode, err := ioutils.Uncompress(compressedCode, uint64(types.MaxWasmSize))
wasmCode, err := ioutils.Uncompress(compressedCode)
if err != nil {
return errorsmod.Wrap(types.ErrCreateFailed, err.Error())
}
Expand Down

0 comments on commit 24eb158

Please sign in to comment.