From fb5103fb87c09278b03984ff0d2818480f943fce Mon Sep 17 00:00:00 2001 From: sausheong Date: Sat, 13 Jun 2026 11:27:34 +0800 Subject: [PATCH] feat(file): cap read_file/edit_file size to prevent OOM (N5) --- tools/file/editfile.go | 4 +++ tools/file/limits.go | 27 +++++++++++++++++ tools/file/readfile.go | 23 ++++++++++++-- tools/file/readfile_test.go | 60 +++++++++++++++++++++++++++++++++++++ 4 files changed, 112 insertions(+), 2 deletions(-) create mode 100644 tools/file/limits.go create mode 100644 tools/file/readfile_test.go diff --git a/tools/file/editfile.go b/tools/file/editfile.go index 2bad1bb..54e43e1 100644 --- a/tools/file/editfile.go +++ b/tools/file/editfile.go @@ -74,6 +74,10 @@ func (t *EditFileTool) Execute(_ context.Context, input json.RawMessage) (tool.T } } + if msg := checkFileSize(in.Path, int64(maxTextFileSize), "text"); msg != "" { + return tool.ToolResult{Error: msg}, nil + } + data, err := os.ReadFile(in.Path) if err != nil { return tool.ToolResult{Error: fmt.Sprintf("failed to read file: %v", err)}, nil diff --git a/tools/file/limits.go b/tools/file/limits.go new file mode 100644 index 0000000..d1903a5 --- /dev/null +++ b/tools/file/limits.go @@ -0,0 +1,27 @@ +package file + +import ( + "fmt" + "os" +) + +const ( + maxTextFileSize = 10 * 1024 * 1024 // 10 MB + maxImageFileSize = 5 * 1024 * 1024 // 5 MB +) + +// checkFileSize stats path and returns a non-empty error string if the file +// exceeds limit. kind is "text" or "image" for the message. Empty return means +// within bounds (or stat failed, in which case the caller's read surfaces the +// real error). +func checkFileSize(path string, limit int64, kind string) string { + fi, err := os.Stat(path) + if err != nil { + return "" + } + if fi.Size() > limit { + return fmt.Sprintf("file too large: %d bytes exceeds the %d byte limit for %s files; read a specific range or use a different tool", + fi.Size(), limit, kind) + } + return "" +} diff --git a/tools/file/readfile.go b/tools/file/readfile.go index 8a6c793..703c1b1 100644 --- a/tools/file/readfile.go +++ b/tools/file/readfile.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "fmt" + "io" "os" "path/filepath" "strings" @@ -93,13 +94,31 @@ func (t *ReadFileTool) Execute(_ context.Context, input json.RawMessage) (tool.T } } - data, err := os.ReadFile(in.Path) + ext := strings.ToLower(filepath.Ext(in.Path)) + limit := int64(maxTextFileSize) + kind := "text" + if _, isImg := imageExtMap[ext]; isImg { + limit = int64(maxImageFileSize) + kind = "image" + } + if msg := checkFileSize(in.Path, limit, kind); msg != "" { + return tool.ToolResult{Error: msg}, nil + } + + f, err := os.Open(in.Path) if err != nil { return tool.ToolResult{Error: fmt.Sprintf("failed to read file: %v", err)}, nil } + defer f.Close() + data, err := io.ReadAll(io.LimitReader(f, limit+1)) + if err != nil { + return tool.ToolResult{Error: fmt.Sprintf("failed to read file: %v", err)}, nil + } + if int64(len(data)) > limit { + return tool.ToolResult{Error: fmt.Sprintf("file too large: exceeds the %d byte limit for %s files; read a specific range or use a different tool", limit, kind)}, nil + } // Check if this is an image file - ext := strings.ToLower(filepath.Ext(in.Path)) if mimeType, ok := imageExtMap[ext]; ok { mimeType = detectImageMIMEFromBytes(data, mimeType) return tool.ToolResult{ diff --git a/tools/file/readfile_test.go b/tools/file/readfile_test.go new file mode 100644 index 0000000..744123a --- /dev/null +++ b/tools/file/readfile_test.go @@ -0,0 +1,60 @@ +package file + +import ( + "context" + "encoding/json" + "os" + "path/filepath" + "testing" + + "github.com/stretchr/testify/require" +) + +func writeSized(t *testing.T, dir, name string, n int) string { + t.Helper() + p := filepath.Join(dir, name) + require.NoError(t, os.WriteFile(p, make([]byte, n), 0o600)) + return p +} + +func TestReadFile_RejectsOversizedText(t *testing.T) { + dir := t.TempDir() + p := writeSized(t, dir, "big.txt", maxTextFileSize+1) + tool := &ReadFileTool{} + in, _ := json.Marshal(map[string]string{"path": p}) + res, err := tool.Execute(context.Background(), in) + require.NoError(t, err) + require.Contains(t, res.Error, "too large") +} + +func TestReadFile_AllowsNormalText(t *testing.T) { + dir := t.TempDir() + p := filepath.Join(dir, "ok.txt") + require.NoError(t, os.WriteFile(p, []byte("hello"), 0o600)) + tool := &ReadFileTool{} + in, _ := json.Marshal(map[string]string{"path": p}) + res, err := tool.Execute(context.Background(), in) + require.NoError(t, err) + require.Empty(t, res.Error) + require.Equal(t, "hello", res.Output) +} + +func TestReadFile_RejectsOversizedImage(t *testing.T) { + dir := t.TempDir() + p := writeSized(t, dir, "big.png", maxImageFileSize+1) + tool := &ReadFileTool{} + in, _ := json.Marshal(map[string]string{"path": p}) + res, err := tool.Execute(context.Background(), in) + require.NoError(t, err) + require.Contains(t, res.Error, "too large") +} + +func TestEditFile_RejectsOversized(t *testing.T) { + dir := t.TempDir() + p := writeSized(t, dir, "big.txt", maxTextFileSize+1) + tool := &EditFileTool{} + in, _ := json.Marshal(map[string]string{"path": p, "old_string": "a", "new_string": "b"}) + res, err := tool.Execute(context.Background(), in) + require.NoError(t, err) + require.Contains(t, res.Error, "too large") +}