Merge branch 'harden/safe-defaults': atomic file writes + 0600 perms
WriteFileAtomic helper; write_file/edit_file atomic + restrictive perms; spill files 0600. Part of the cross-repo safe-by-default hardening.
This commit is contained in:
commit
de23ad986a
7 files changed
+141
-3
No files matched your search
+2
-1
@@ -11,6 +11,7 @@ import (
|
||||
|
||||
"github.com/sausheong/harness/llm"
|
||||
"github.com/sausheong/harness/session"
|
||||
"github.com/sausheong/harness/tool"
|
||||
)
|
||||
|
||||
// defaultMaxToolResultLen is the fallback cap used when LoopConfig.MaxToolResultLen
|
||||
@@ -433,7 +434,7 @@ func spillToolResult(cfg spillConfig, toolCallID, content string) (string, error
|
||||
return "", fmt.Errorf("spill mkdir: %w", err)
|
||||
}
|
||||
path := filepath.Join(dir, toolCallID+".txt")
|
||||
if err := os.WriteFile(path, []byte(content), 0o644); err != nil {
|
||||
if err := tool.WriteFileAtomic(path, []byte(content), 0o600); err != nil {
|
||||
return "", fmt.Errorf("spill write: %w", err)
|
||||
}
|
||||
return path, nil
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
package tool
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
// WriteFileAtomic writes data to path atomically: it writes to a temp file in
|
||||
// the same directory, fsyncs, sets perm, then renames over path. A crash
|
||||
// mid-write leaves either the old file or the complete new file, never a
|
||||
// truncated one. The rename is atomic on POSIX within a single filesystem.
|
||||
func WriteFileAtomic(path string, data []byte, perm os.FileMode) error {
|
||||
dir := filepath.Dir(path)
|
||||
tmp, err := os.CreateTemp(dir, ".tmp-"+filepath.Base(path)+"-*")
|
||||
if err != nil {
|
||||
return fmt.Errorf("create temp: %w", err)
|
||||
}
|
||||
tmpName := tmp.Name()
|
||||
defer func() { _ = os.Remove(tmpName) }()
|
||||
|
||||
if _, err := tmp.Write(data); err != nil {
|
||||
_ = tmp.Close()
|
||||
return fmt.Errorf("write temp: %w", err)
|
||||
}
|
||||
if err := tmp.Sync(); err != nil {
|
||||
_ = tmp.Close()
|
||||
return fmt.Errorf("sync temp: %w", err)
|
||||
}
|
||||
if err := tmp.Close(); err != nil {
|
||||
return fmt.Errorf("close temp: %w", err)
|
||||
}
|
||||
if err := os.Chmod(tmpName, perm); err != nil {
|
||||
return fmt.Errorf("chmod temp: %w", err)
|
||||
}
|
||||
if err := os.Rename(tmpName, path); err != nil {
|
||||
return fmt.Errorf("rename temp: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package tool
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestWriteFileAtomic(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
path := filepath.Join(dir, "out.txt")
|
||||
|
||||
require.NoError(t, WriteFileAtomic(path, []byte("hello"), 0o600))
|
||||
|
||||
data, err := os.ReadFile(path)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, "hello", string(data))
|
||||
|
||||
info, err := os.Stat(path)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, os.FileMode(0o600), info.Mode().Perm())
|
||||
|
||||
require.NoError(t, WriteFileAtomic(path, []byte("world"), 0o644))
|
||||
data, _ = os.ReadFile(path)
|
||||
require.Equal(t, "world", string(data))
|
||||
|
||||
entries, err := os.ReadDir(dir)
|
||||
require.NoError(t, err)
|
||||
require.Len(t, entries, 1)
|
||||
require.Equal(t, "out.txt", entries[0].Name())
|
||||
}
|
||||
@@ -91,7 +91,11 @@ func (t *EditFileTool) Execute(_ context.Context, input json.RawMessage) (tool.T
|
||||
|
||||
newContent := strings.Replace(content, in.OldString, in.NewString, 1)
|
||||
|
||||
if err := os.WriteFile(in.Path, []byte(newContent), 0o644); err != nil {
|
||||
mode := os.FileMode(0o600)
|
||||
if info, statErr := os.Stat(in.Path); statErr == nil {
|
||||
mode = info.Mode().Perm()
|
||||
}
|
||||
if err := tool.WriteFileAtomic(in.Path, []byte(newContent), mode); err != nil {
|
||||
return tool.ToolResult{Error: fmt.Sprintf("failed to write file: %v", err)}, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
package file
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestEditFilePreservesMode(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
path := filepath.Join(dir, "code.txt")
|
||||
require.NoError(t, os.WriteFile(path, []byte("alpha beta"), 0o640))
|
||||
|
||||
tool := &EditFileTool{WorkDir: dir}
|
||||
in, _ := json.Marshal(map[string]string{
|
||||
"path": path,
|
||||
"old_string": "alpha",
|
||||
"new_string": "gamma",
|
||||
})
|
||||
res, err := tool.Execute(context.Background(), in)
|
||||
require.NoError(t, err)
|
||||
require.Empty(t, res.Error)
|
||||
|
||||
data, _ := os.ReadFile(path)
|
||||
require.Equal(t, "gamma beta", string(data))
|
||||
|
||||
info, err := os.Stat(path)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, os.FileMode(0o640), info.Mode().Perm())
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
|
||||
|
||||
@@ -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())
|
||||
}
|
||||
Reference in new issue
Block a user