diff --git a/x/wasm/ioutils/ioutil.go b/x/wasm/ioutils/ioutil.go index a34a43e9ea..0b3f6dc29f 100644 --- a/x/wasm/ioutils/ioutil.go +++ b/x/wasm/ioutils/ioutil.go @@ -5,13 +5,15 @@ import ( "compress/gzip" "io" + sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" + "github.com/CosmWasm/wasmd/x/wasm/types" ) // Uncompress expects a valid gzip source to unpack or fails. See IsGzip func Uncompress(gzipSrc []byte, limit uint64) ([]byte, error) { if uint64(len(gzipSrc)) > limit { - return nil, types.ErrLimit + return nil, types.ErrLimit.Wrapf("max %d bytes", limit) } zr, err := gzip.NewReader(bytes.NewReader(gzipSrc)) if err != nil { @@ -19,7 +21,11 @@ func Uncompress(gzipSrc []byte, limit uint64) ([]byte, error) { } zr.Multistream(false) defer zr.Close() - return io.ReadAll(LimitReader(zr, int64(limit))) + bz, err := io.ReadAll(LimitReader(zr, int64(limit))) + if types.ErrLimit.Is(err) { + return nil, sdkerrors.Wrapf(err, "max %d bytes", limit) + } + return bz, err } // LimitReader returns a Reader that reads from r diff --git a/x/wasm/ioutils/ioutil_test.go b/x/wasm/ioutils/ioutil_test.go index 3399abf293..4a02f343bc 100644 --- a/x/wasm/ioutils/ioutil_test.go +++ b/x/wasm/ioutils/ioutil_test.go @@ -10,6 +10,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + "github.com/tendermint/tendermint/libs/rand" "github.com/CosmWasm/wasmd/x/wasm/types" ) @@ -52,8 +53,8 @@ func TestUncompress(t *testing.T) { src: asGzip(bytes.Repeat([]byte{0x1}, maxSize+1)), expError: types.ErrLimit, }, - "handle other big gzip output": { - src: asGzip(bytes.Repeat([]byte{0x1}, 2*maxSize)), + "handle big gzip archive": { + src: asGzip(rand.Bytes(2 * maxSize)), expError: types.ErrLimit, }, } diff --git a/x/wasm/keeper/keeper.go b/x/wasm/keeper/keeper.go index 50cd842893..508a04d6fa 100644 --- a/x/wasm/keeper/keeper.go +++ b/x/wasm/keeper/keeper.go @@ -149,7 +149,7 @@ func (k Keeper) create(ctx sdk.Context, creator sdk.AccAddress, wasmCode []byte, ctx.GasMeter().ConsumeGas(k.gasRegister.UncompressCosts(len(wasmCode)), "Uncompress gzip bytecode") wasmCode, err = ioutils.Uncompress(wasmCode, uint64(types.MaxWasmSize)) if err != nil { - return 0, checksum, sdkerrors.Wrap(types.ErrCreateFailed, err.Error()) + return 0, checksum, types.ErrCreateFailed.Wrap(sdkerrors.Wrap(err, "uncompress wasm archive").Error()) } } @@ -191,7 +191,7 @@ func (k Keeper) importCode(ctx sdk.Context, codeID uint64, codeInfo types.CodeIn var err error wasmCode, err = ioutils.Uncompress(wasmCode, uint64(types.MaxWasmSize)) if err != nil { - return sdkerrors.Wrap(types.ErrCreateFailed, err.Error()) + return types.ErrCreateFailed.Wrap(sdkerrors.Wrap(err, "uncompress wasm archive").Error()) } } newCodeHash, err := k.wasmVM.Create(wasmCode)