From 0ae96d5e08dabe7ec2ccdf8307f6783abed51918 Mon Sep 17 00:00:00 2001 From: Gabriel Paradiso Date: Thu, 9 Jan 2025 16:41:11 +0100 Subject: [PATCH 1/3] fix: check size and len(src) match to avoid partial or truncated writes --- pkg/workflows/wasm/host/module.go | 4 ++++ pkg/workflows/wasm/host/module_test.go | 26 ++++++++++++++++++++------ 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/pkg/workflows/wasm/host/module.go b/pkg/workflows/wasm/host/module.go index 601b69632..48aa75118 100644 --- a/pkg/workflows/wasm/host/module.go +++ b/pkg/workflows/wasm/host/module.go @@ -728,6 +728,10 @@ func write(memory, src []byte, ptr, size int32) int64 { return -1 } + if len(src) != int(size) { + return -1 + } + if int32(len(memory)) < ptr+size { return -1 } diff --git a/pkg/workflows/wasm/host/module_test.go b/pkg/workflows/wasm/host/module_test.go index a19c43fa2..6d5f7ed62 100644 --- a/pkg/workflows/wasm/host/module_test.go +++ b/pkg/workflows/wasm/host/module_test.go @@ -553,7 +553,7 @@ func Test_read(t *testing.T) { } func Test_write(t *testing.T) { - t.Run("successfully write to slice", func(t *testing.T) { + t.Run("OK-successfully_write_to_slice", func(t *testing.T) { giveSrc := []byte("hello, world") memory := make([]byte, 12) n := write(memory, giveSrc, 0, int32(len(giveSrc))) @@ -561,21 +561,35 @@ func Test_write(t *testing.T) { assert.Equal(t, []byte("hello, world"), memory[:len(giveSrc)]) }) - t.Run("cannot write to slice because memory too small", func(t *testing.T) { + t.Run("NOK-cannot_write_to_slice_because_memory_too_small", func(t *testing.T) { giveSrc := []byte("hello, world") memory := make([]byte, len(giveSrc)-1) n := write(memory, giveSrc, 0, int32(len(giveSrc))) - assert.Equal(t, n, int64(-1)) + assert.Equal(t, int64(-1), n) }) - t.Run("fails to write to invalid access", func(t *testing.T) { + t.Run("NOK-fails_to_write_to_invalid_access", func(t *testing.T) { giveSrc := []byte("hello, world") memory := make([]byte, len(giveSrc)) n := write(memory, giveSrc, 0, -1) - assert.Equal(t, n, int64(-1)) + assert.Equal(t, int64(-1), n) n = write(memory, giveSrc, -1, 1) - assert.Equal(t, n, int64(-1)) + assert.Equal(t, int64(-1), n) + }) + + t.Run("NOK-truncated_write_due_to_size_being_smaller_than_len", func(t *testing.T) { + giveSrc := []byte("hello, world") + memory := make([]byte, 12) + n := write(memory, giveSrc, 0, int32(len(giveSrc)-2)) + assert.Equal(t, int64(-1), n) + }) + + t.Run("NOK-unwanted_data_when_size_exceeds_written_data", func(t *testing.T) { + giveSrc := []byte("hello, world") + memory := make([]byte, 20) + n := write(memory, giveSrc, 0, 20) + assert.Equal(t, int64(-1), n) }) } From fed0e8b5de4b40b6e1498bd72380b7273117dd6b Mon Sep 17 00:00:00 2001 From: Gabriel Paradiso Date: Thu, 9 Jan 2025 16:45:10 +0100 Subject: [PATCH 2/3] fix: return the number of bytes copied --- pkg/workflows/wasm/host/module.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/pkg/workflows/wasm/host/module.go b/pkg/workflows/wasm/host/module.go index 48aa75118..f8bd77d02 100644 --- a/pkg/workflows/wasm/host/module.go +++ b/pkg/workflows/wasm/host/module.go @@ -736,7 +736,5 @@ func write(memory, src []byte, ptr, size int32) int64 { return -1 } buffer := memory[ptr : ptr+size] - dataLen := int64(len(src)) - copy(buffer, src) - return dataLen + return int64(copy(buffer, src)) } From 6de231a3c36c544c2f8aa83e14124d7738b9907c Mon Sep 17 00:00:00 2001 From: Gabriel Paradiso Date: Thu, 9 Jan 2025 17:29:02 +0100 Subject: [PATCH 3/3] chore: align test naming --- pkg/workflows/wasm/host/module_test.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/pkg/workflows/wasm/host/module_test.go b/pkg/workflows/wasm/host/module_test.go index 6d5f7ed62..66390adeb 100644 --- a/pkg/workflows/wasm/host/module_test.go +++ b/pkg/workflows/wasm/host/module_test.go @@ -553,7 +553,7 @@ func Test_read(t *testing.T) { } func Test_write(t *testing.T) { - t.Run("OK-successfully_write_to_slice", func(t *testing.T) { + t.Run("successfully write to slice", func(t *testing.T) { giveSrc := []byte("hello, world") memory := make([]byte, 12) n := write(memory, giveSrc, 0, int32(len(giveSrc))) @@ -561,14 +561,14 @@ func Test_write(t *testing.T) { assert.Equal(t, []byte("hello, world"), memory[:len(giveSrc)]) }) - t.Run("NOK-cannot_write_to_slice_because_memory_too_small", func(t *testing.T) { + t.Run("cannot write to slice because memory too small", func(t *testing.T) { giveSrc := []byte("hello, world") memory := make([]byte, len(giveSrc)-1) n := write(memory, giveSrc, 0, int32(len(giveSrc))) assert.Equal(t, int64(-1), n) }) - t.Run("NOK-fails_to_write_to_invalid_access", func(t *testing.T) { + t.Run("fails to write to invalid access", func(t *testing.T) { giveSrc := []byte("hello, world") memory := make([]byte, len(giveSrc)) n := write(memory, giveSrc, 0, -1) @@ -578,14 +578,14 @@ func Test_write(t *testing.T) { assert.Equal(t, int64(-1), n) }) - t.Run("NOK-truncated_write_due_to_size_being_smaller_than_len", func(t *testing.T) { + t.Run("truncated write due to size being smaller than len", func(t *testing.T) { giveSrc := []byte("hello, world") memory := make([]byte, 12) n := write(memory, giveSrc, 0, int32(len(giveSrc)-2)) assert.Equal(t, int64(-1), n) }) - t.Run("NOK-unwanted_data_when_size_exceeds_written_data", func(t *testing.T) { + t.Run("unwanted data when size exceeds written data", func(t *testing.T) { giveSrc := []byte("hello, world") memory := make([]byte, 20) n := write(memory, giveSrc, 0, 20)