feat(tools/file): write_file atomic + 0600

This commit is contained in:
sausheong committed 2026-06-13 00:50:46 +08:00
1 parent c4750347e6
commit 7ae944f0eb
2 files changed
+27 -1

No files matched your search

+1 -1
View File
@@ -72,7 +72,7 @@ func (t *WriteFileTool) Execute(_ context.Context, input json.RawMessage) (tool.
return tool.ToolResult{Error: fmt.Sprintf("failed to create directory: %v", err)}, nil
}
if err := os.WriteFile(in.Path, []byte(in.Content), 0o644); err != nil {
if err := tool.WriteFileAtomic(in.Path, []byte(in.Content), 0o600); err != nil {
return tool.ToolResult{Error: fmt.Sprintf("failed to write file: %v", err)}, nil
}
+26
View File
@@ -0,0 +1,26 @@
package file
import (
"context"
"encoding/json"
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/require"
)
func TestWriteFileIsRestrictive(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "secret.txt")
tool := &WriteFileTool{WorkDir: dir}
in, _ := json.Marshal(map[string]string{"path": path, "content": "data"})
res, err := tool.Execute(context.Background(), in)
require.NoError(t, err)
require.Empty(t, res.Error)
info, err := os.Stat(path)
require.NoError(t, err)
require.Equal(t, os.FileMode(0o600), info.Mode().Perm())
}