From b55897a60b944ffe785bdc07d6056515c28e2703 Mon Sep 17 00:00:00 2001 From: Grant Linville Date: Tue, 3 Sep 2024 11:44:39 -0400 Subject: [PATCH 1/2] fix: sys.read: never read files that contain a null byte Signed-off-by: Grant Linville --- pkg/builtin/builtin.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/pkg/builtin/builtin.go b/pkg/builtin/builtin.go index f8f44ba9..b3503666 100644 --- a/pkg/builtin/builtin.go +++ b/pkg/builtin/builtin.go @@ -489,6 +489,12 @@ func SysRead(_ context.Context, _ []string, input string, _ chan<- string) (stri if len(data) == 0 { return fmt.Sprintf("The file %s has no contents", params.Filename), nil } + + // Assume the file is not text if it contains a null byte + if bytes.Contains(data, []byte{0}) { + return fmt.Sprintf("The file %s cannot be read because it is not a plaintext file", params.Filename), nil + } + return string(data), nil } From 66b00d7edf208df18737202e0039d9f63de98c5c Mon Sep 17 00:00:00 2001 From: Grant Linville Date: Tue, 3 Sep 2024 12:16:58 -0400 Subject: [PATCH 2/2] use bytes.IndexByte Signed-off-by: Grant Linville --- pkg/builtin/builtin.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/builtin/builtin.go b/pkg/builtin/builtin.go index b3503666..c17f2c80 100644 --- a/pkg/builtin/builtin.go +++ b/pkg/builtin/builtin.go @@ -491,7 +491,7 @@ func SysRead(_ context.Context, _ []string, input string, _ chan<- string) (stri } // Assume the file is not text if it contains a null byte - if bytes.Contains(data, []byte{0}) { + if bytes.IndexByte(data, 0) != -1 { return fmt.Sprintf("The file %s cannot be read because it is not a plaintext file", params.Filename), nil }