Files
archie/internal/tools/builtin/write.go
T
sam db346b159d fix(webui): mark cookies Secure behind a TLS proxy; run gosec in the gate (#1919)
Cookies took Secure from r.TLS, which is nil behind a proxy that terminates
TLS, so sign-in cookies went out without it. Every dashboard cookie now goes
through one builder that reads the scheme the way the origin check does.

The worktree reset and the agent worker's chown walk use os.Root, so a
symlink swapped in mid-walk can't point them outside the worktree.

gosec runs in golangci-lint, tests included. G104, G115, G204 and G304 are
excluded in config; each remaining finding is fixed or suppressed at the site
with its reason.
2026-11-13 03:16:15 +11:00

150 lines
4.5 KiB
Go

package builtin
import (
"context"
"encoding/json"
"errors"
"fmt"
"os"
"path/filepath"
"github.com/samcharles93/archie-core/internal/fsutil"
)
// WriteParams are the parameters for the write tool.
type WriteParams struct {
Path string `json:"path"`
Content string `json:"content"`
Overwrite bool `json:"overwrite,omitempty"`
}
var writeSchema = Schema{
Name: "write",
Description: "Create or overwrite a file with the given content. Creates parent directories as needed. Set overwrite to true to replace an existing file; otherwise the tool will refuse to overwrite.",
Parameters: json.RawMessage(`{
"type": "object",
"properties": {
"path": {
"type": "string",
"description": "Absolute or relative file path to write"
},
"content": {
"type": "string",
"description": "The full file content to write"
},
"overwrite": {
"type": "boolean",
"description": "Allow overwriting an existing file. Defaults to false - the tool will error if the file already exists unless this is true."
}
},
"required": ["path", "content"]
}`),
}
// NewWriteTool creates the built-in write tool.
func NewWriteTool(cwd string, mq *MutationQueue, rt *ReadTracker) Tool {
return Tool{
Schema: writeSchema,
Source: "builtin",
Emoji: "📝",
Execute: makeWriteExecutor(cwd, mq, rt),
}
}
func makeWriteExecutor(cwd string, mq *MutationQueue, rt *ReadTracker) Executor {
return func(ctx context.Context, params json.RawMessage, _ UIBridge) (Result, error) {
var p WriteParams
if err := json.Unmarshal(params, &p); err != nil {
return Result{Content: fmt.Sprintf("invalid parameters: %v", err), IsError: true}, nil
}
if len(p.Content) > maxWriteBytes {
return Result{Content: fmt.Sprintf("content too large (%s > %s)", FormatSize(len(p.Content)), FormatSize(maxWriteBytes)), IsError: true}, nil
}
path := resolvePath(cwd, p.Path)
if !isConfined(cwd, path) {
return Result{Content: "path escapes working directory", IsError: true}, nil
}
writeCtx, cancel := context.WithTimeout(ctx, DefaultToolTimeout)
defer cancel()
var result Result
err := runWithContext(writeCtx, func() error {
// Check for accidental overwrite and enforce read-before-write.
// Both checks only apply when the file already exists; new files
// are always allowed.
oldContent, block := checkExistingFile(path, cwd, p.Path, p.Overwrite, rt)
if block != nil {
result = *block
return nil
}
release := mq.Acquire(path)
defer release()
dir := filepath.Dir(path)
if err := os.MkdirAll(dir, 0o755); err != nil {
result = Result{Content: fmt.Sprintf("error creating directory: %v", err), IsError: true}
return nil
}
if err := fsutil.WriteFileAtomic(path, []byte(p.Content), 0o644); err != nil {
result = Result{Content: fmt.Sprintf("error writing file: %v", err), IsError: true}
return nil
}
result = Result{
Content: fmt.Sprintf("wrote %d bytes to %s", len(p.Content), path),
Details: DiffDetails{Path: path, OldContent: oldContent, NewContent: p.Content},
}
return nil
})
if err != nil {
if errors.Is(err, context.DeadlineExceeded) {
return Result{Content: fmt.Sprintf("write timed out after %v", DefaultToolTimeout), IsError: true}, nil
}
return Result{Content: fmt.Sprintf("write failed: %v", err), IsError: true}, nil
}
return result, nil
}
}
// isRegularFile reports whether path exists and is a regular file. A missing
// file, a directory, a symlink, or any other non-regular entry returns false,
// which lets new files skip the overwrite and read-before-write checks.
func isRegularFile(path string) bool {
info, err := os.Stat(path) //nolint:gosec // G703: callers confine path with isConfined first.
return err == nil && info.Mode().IsRegular()
}
// checkExistingFile returns an existing file's content and, when the write
// must not proceed, an error result. It enforces read-before-write and
// overwrite opt-in.
func checkExistingFile(path, cwd, relPath string, overwrite bool, rt *ReadTracker) (string, *Result) {
if !isRegularFile(path) {
return "", nil
}
if rt != nil {
if err := rt.CheckRead(cwd, relPath); err != nil {
return "", &Result{Content: err.Error(), IsError: true}
}
}
if !overwrite {
return "", &Result{
Content: fmt.Sprintf(
"file %q already exists - set overwrite to true to replace it, or use the edit tool for partial changes",
path,
),
IsError: true,
}
}
if data, err := os.ReadFile(path); err == nil {
return string(data), nil
}
return "", nil
}