From 268ac3a5e2b6f54827c763ba938d5ca65ffe4e8f Mon Sep 17 00:00:00 2001 From: modernzju Date: Sun, 21 Jun 2026 00:37:07 +0800 Subject: [PATCH] Handle short inputs in gzip and wasm detection helpers Signed-off-by: modernzju --- x/compute/client/utils/utils.go | 6 ++--- x/compute/client/utils/utils_test.go | 39 ++++++++++++---------------- 2 files changed, 19 insertions(+), 26 deletions(-) diff --git a/x/compute/client/utils/utils.go b/x/compute/client/utils/utils.go index 8fe5bc4bad..d634e6b14d 100644 --- a/x/compute/client/utils/utils.go +++ b/x/compute/client/utils/utils.go @@ -30,14 +30,14 @@ var ( wasmIdent = []byte("\x00\x61\x73\x6D") ) -// IsGzip returns checks if the file contents are gzip compressed +// IsGzip checks if the file contents are gzip compressed func IsGzip(input []byte) bool { - return bytes.Equal(input[:3], gzipIdent) + return len(input) >= len(gzipIdent) && bytes.Equal(input[:len(gzipIdent)], gzipIdent) } // IsWasm checks if the file contents are of wasm binary func IsWasm(input []byte) bool { - return bytes.Equal(input[:4], wasmIdent) + return len(input) >= len(wasmIdent) && bytes.Equal(input[:len(wasmIdent)], wasmIdent) } // GzipIt compresses the input ([]byte) diff --git a/x/compute/client/utils/utils_test.go b/x/compute/client/utils/utils_test.go index 6baba82f4a..68a5e0ab90 100644 --- a/x/compute/client/utils/utils_test.go +++ b/x/compute/client/utils/utils_test.go @@ -1,35 +1,29 @@ package utils import ( - "os" - "path/filepath" "testing" - "github.com/scrtlabs/SecretNetwork/x/compute/internal/keeper" - "github.com/stretchr/testify/require" ) -func GetTestData() ([]byte, []byte, []byte, error) { - wasmCode, err := os.ReadFile(filepath.Join("../../internal/keeper", keeper.TestContractPaths["hackatom.wasm"])) - if err != nil { - return nil, nil, nil, err - } - - gzipData, err := GzipIt(wasmCode) - if err != nil { - return nil, nil, nil, err - } +func getTestData(t *testing.T) ([]byte, []byte, []byte) { + t.Helper() + wasmCode := []byte{0x00, 0x61, 0x73, 0x6D, 0x01, 0x00, 0x00, 0x00} someRandomStr := []byte("hello world") + gzipData, err := GzipIt(wasmCode) + require.NoError(t, err) - return wasmCode, someRandomStr, gzipData, nil + return wasmCode, someRandomStr, gzipData } func TestIsWasm(t *testing.T) { - wasmCode, someRandomStr, gzipData, err := GetTestData() - require.NoError(t, err) + wasmCode, someRandomStr, gzipData := getTestData(t) + t.Log("should return false for empty data") + require.False(t, IsWasm(nil)) + t.Log("should return false for short data") + require.False(t, IsWasm([]byte{0x00, 0x61, 0x73})) t.Log("should return false for some random string data") require.False(t, IsWasm(someRandomStr)) t.Log("should return false for gzip data") @@ -39,25 +33,24 @@ func TestIsWasm(t *testing.T) { } func TestIsGzip(t *testing.T) { - wasmCode, someRandomStr, gzipData, err := GetTestData() - require.NoError(t, err) + wasmCode, someRandomStr, gzipData := getTestData(t) + require.False(t, IsGzip(nil)) + require.False(t, IsGzip([]byte{0x1F, 0x8B})) require.False(t, IsGzip(wasmCode)) require.False(t, IsGzip(someRandomStr)) require.True(t, IsGzip(gzipData)) } func TestGzipIt(t *testing.T) { - wasmCode, someRandomStr, _, err := GetTestData() + wasmCode, someRandomStr, _ := getTestData(t) originalGzipData := []byte{ 31, 139, 8, 0, 0, 0, 0, 0, 0, 255, 202, 72, 205, 201, 201, 87, 40, 207, 47, 202, 73, 1, 4, 0, 0, 255, 255, 133, 17, 74, 13, 11, 0, 0, 0, } - require.NoError(t, err) - t.Log("gzip wasm with no error") - _, err = GzipIt(wasmCode) + _, err := GzipIt(wasmCode) require.NoError(t, err) t.Log("gzip of a string should return exact gzip data")