security(tools): add 20 MB file write size limit

Reject writes exceeding 20 MB in WriteFileTool to prevent disk
exhaustion from unexpectedly large LLM-generated file content.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
admin-mf 2026-03-05 23:58:33 -06:00
parent 0e2c7d1abd
commit ec870eaf8a

View file

@ -13,6 +13,8 @@ import (
"github.com/sipeed/picoclaw/pkg/fileutil" "github.com/sipeed/picoclaw/pkg/fileutil"
) )
const maxWriteSize = 20 * 1024 * 1024 // 20 MB — limit for file writes via the write tool
// validatePath ensures the given path is within the workspace if restrict is true. // validatePath ensures the given path is within the workspace if restrict is true.
func validatePath(path, workspace string, restrict bool) (string, error) { func validatePath(path, workspace string, restrict bool) (string, error) {
if workspace == "" { if workspace == "" {
@ -178,6 +180,10 @@ func (t *WriteFileTool) Execute(ctx context.Context, args map[string]any) *ToolR
return ErrorResult("content is required") return ErrorResult("content is required")
} }
if len(content) > maxWriteSize {
return ErrorResult(fmt.Sprintf("content too large: %d bytes exceeds %d byte limit", len(content), maxWriteSize))
}
if err := t.fs.WriteFile(path, []byte(content)); err != nil { if err := t.fs.WriteFile(path, []byte(content)); err != nil {
return ErrorResult(err.Error()) return ErrorResult(err.Error())
} }