feat(file): cap read_file/edit_file size to prevent OOM (N5)
This commit is contained in:
1 parent
01b55a1446
commit
fb5103fb87
4 files changed
+112
-2
No files matched your search
@@ -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)
|
data, err := os.ReadFile(in.Path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return tool.ToolResult{Error: fmt.Sprintf("failed to read file: %v", err)}, nil
|
return tool.ToolResult{Error: fmt.Sprintf("failed to read file: %v", err)}, nil
|
||||||
|
|||||||
@@ -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 ""
|
||||||
|
}
|
||||||
+21
-2
@@ -4,6 +4,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"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 {
|
if err != nil {
|
||||||
return tool.ToolResult{Error: fmt.Sprintf("failed to read file: %v", 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
|
// Check if this is an image file
|
||||||
ext := strings.ToLower(filepath.Ext(in.Path))
|
|
||||||
if mimeType, ok := imageExtMap[ext]; ok {
|
if mimeType, ok := imageExtMap[ext]; ok {
|
||||||
mimeType = detectImageMIMEFromBytes(data, mimeType)
|
mimeType = detectImageMIMEFromBytes(data, mimeType)
|
||||||
return tool.ToolResult{
|
return tool.ToolResult{
|
||||||
|
|||||||
@@ -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")
|
||||||
|
}
|
||||||
Reference in new issue
Block a user