Merge branch 'worktree-agent-ab6edc0d99947642f'

This commit is contained in:
2026-11-09 00:11:40 +00:00
19 changed files with 1706 additions and 99 deletions
+149
View File
@@ -0,0 +1,149 @@
package server
import (
"encoding/json"
"errors"
"io"
"io/fs"
"net/http"
"net/url"
"os"
"path/filepath"
"strings"
"unicode/utf8"
)
// maxHomeFileUploadBytes bounds one file stored through POST /files. It
// matches the chat upload limit so both raw-body routes behave alike.
const maxHomeFileUploadBytes = maxChatUploadBytes
type homeFileUploadReply struct {
Name string `json:"name"`
Path string `json:"path"`
Size int64 `json:"size"`
}
// serveHomeFileUpload stores the request body as a new file inside the
// folder named by ?path=, which must lie inside the operator's home. The name
// travels in X-Pipelines-Upload-Name; an existing file is never overwritten.
func serveHomeFileUpload(w http.ResponseWriter, r *http.Request) {
home, err := OperatorHomeDirectory()
if err != nil {
http.NotFound(w, r)
return
}
serveHomeFileUploadTo(w, r, home)
}
func serveHomeFileUploadTo(w http.ResponseWriter, r *http.Request, home string) {
fail := func(status int, message string) { http.Error(w, message, status) }
rawName := r.Header.Get(chatUploadHeader)
if rawName == "" {
fail(http.StatusBadRequest, "missing "+chatUploadHeader)
return
}
if r.Header.Get("Sec-Fetch-Site") == "cross-site" {
fail(http.StatusForbidden, "cross-site upload refused")
return
}
decoded, err := url.PathUnescape(rawName)
if err != nil {
decoded = rawName
}
name, err := uploadFileName(decoded)
if err != nil {
fail(http.StatusBadRequest, err.Error())
return
}
if r.ContentLength > maxHomeFileUploadBytes {
fail(http.StatusRequestEntityTooLarge, "file exceeds the 100 MiB limit")
return
}
home = filepath.Clean(home)
realHome, err := filepath.EvalSymlinks(home)
if err != nil {
fail(http.StatusNotFound, "home directory unavailable")
return
}
target, err := expandHomePath(home, r.URL.Query().Get("path"))
if err != nil {
fail(http.StatusNotFound, "folder not found")
return
}
_, realDir, err := resolveInsideHome(realHome, target)
if err != nil {
if strings.Contains(err.Error(), "outside the home directory") {
fail(http.StatusForbidden, "folder access denied")
} else {
fail(http.StatusNotFound, "folder not found")
}
return
}
info, err := os.Stat(realDir)
if err != nil || !info.IsDir() {
fail(http.StatusNotFound, "folder not found")
return
}
if specialFilesystem(realDir) {
fail(http.StatusForbidden, "folder access denied")
return
}
file, err := os.OpenFile(filepath.Join(realDir, name), os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0o644)
if err != nil {
switch {
case errors.Is(err, fs.ErrExist):
fail(http.StatusConflict, "a file named "+name+" already exists")
case errors.Is(err, fs.ErrPermission):
fail(http.StatusForbidden, "folder access denied")
default:
fail(http.StatusInternalServerError, "write file: "+err.Error())
}
return
}
// The process umask may narrow the create mode; the stored file is meant
// to be readable like one the operator saved themselves.
size, err := int64(0), file.Chmod(0o644)
if err == nil {
size, err = io.Copy(file, io.LimitReader(r.Body, maxHomeFileUploadBytes+1))
}
if err == nil && size > maxHomeFileUploadBytes {
err = errors.New("file exceeds the 100 MiB limit")
}
if err == nil {
err = file.Sync()
}
if closeErr := file.Close(); err == nil {
err = closeErr
}
if err != nil {
_ = os.Remove(file.Name())
status := http.StatusBadRequest
if size > maxHomeFileUploadBytes {
status = http.StatusRequestEntityTooLarge
}
fail(status, err.Error())
return
}
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Cache-Control", "no-store")
_ = json.NewEncoder(w).Encode(homeFileUploadReply{Name: name, Path: filepath.Join(target, name), Size: size})
}
// uploadFileName accepts one plain file name: no directory parts, no NUL,
// not "." or "..", and short enough for common filesystems. The name is kept
// as given rather than rewritten, so the stored file is the one the operator
// asked for.
func uploadFileName(value string) (string, error) {
name := strings.TrimSpace(value)
switch {
case name == "" || name == "." || name == "..":
return "", errors.New("file name is missing")
case strings.ContainsAny(name, "/\\\x00"):
return "", errors.New("file name must not contain path separators")
case !utf8.ValidString(name):
return "", errors.New("file name is not valid UTF-8")
case len(name) > 255:
return "", errors.New("file name is too long")
}
return name, nil
}
+153
View File
@@ -0,0 +1,153 @@
package server
import (
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
)
func postHomeFile(t *testing.T, home, dir, name, body string) *httptest.ResponseRecorder {
t.Helper()
req := httptest.NewRequest(http.MethodPost, "/files?path="+dir, strings.NewReader(body))
if name != "" {
req.Header.Set(chatUploadHeader, name)
}
req.Header.Set("Content-Type", "text/plain")
rec := httptest.NewRecorder()
serveHomeFileUploadTo(rec, req, home)
return rec
}
func TestServeHomeFileUpload(t *testing.T) {
root := t.TempDir()
home := filepath.Join(root, "home")
outside := filepath.Join(root, "outside")
for _, dir := range []string{filepath.Join(home, "project"), outside} {
if err := os.MkdirAll(dir, 0o755); err != nil {
t.Fatal(err)
}
}
if err := os.WriteFile(filepath.Join(home, "project", "kept.txt"), []byte("original"), 0o644); err != nil {
t.Fatal(err)
}
if err := os.Symlink(outside, filepath.Join(home, "escape")); err != nil {
t.Fatal(err)
}
t.Run("stores a new file in the shown folder", func(t *testing.T) {
rec := postHomeFile(t, home, "~/project", "big%20photo.txt", "hello upload")
if rec.Code != http.StatusOK {
t.Fatalf("status = %d %s", rec.Code, rec.Body)
}
var reply homeFileUploadReply
if err := json.Unmarshal(rec.Body.Bytes(), &reply); err != nil {
t.Fatal(err)
}
want := filepath.Join(home, "project", "big photo.txt")
if reply.Name != "big photo.txt" || reply.Path != want || reply.Size != int64(len("hello upload")) {
t.Fatalf("reply = %+v", reply)
}
info, err := os.Stat(want)
if err != nil || info.Mode().Perm() != 0o644 {
t.Fatalf("stored file = %v %v", info, err)
}
data, _ := os.ReadFile(want)
if string(data) != "hello upload" {
t.Fatalf("contents = %q", data)
}
})
t.Run("operator home seam and absolute path", func(t *testing.T) {
originalHomeDirectory := operatorHomeDirectory
operatorHomeDirectory = func() (string, error) { return home, nil }
t.Cleanup(func() { operatorHomeDirectory = originalHomeDirectory })
req := httptest.NewRequest(http.MethodPost, "/files?path="+filepath.Join(home, "project"), strings.NewReader("x"))
req.Header.Set(chatUploadHeader, "seam.txt")
rec := httptest.NewRecorder()
serveHomeFileUpload(rec, req)
if rec.Code != http.StatusOK {
t.Fatalf("status = %d %s", rec.Code, rec.Body)
}
if _, err := os.Stat(filepath.Join(home, "project", "seam.txt")); err != nil {
t.Fatal(err)
}
})
t.Run("existing file is refused and kept", func(t *testing.T) {
rec := postHomeFile(t, home, "~/project", "kept.txt", "replacement")
if rec.Code != http.StatusConflict || !strings.Contains(rec.Body.String(), "already exists") {
t.Fatalf("response = %d %s", rec.Code, rec.Body)
}
data, _ := os.ReadFile(filepath.Join(home, "project", "kept.txt"))
if string(data) != "original" {
t.Fatalf("contents = %q, want original", data)
}
})
t.Run("empty file is stored", func(t *testing.T) {
rec := postHomeFile(t, home, "~", "empty.txt", "")
if rec.Code != http.StatusOK {
t.Fatalf("status = %d %s", rec.Code, rec.Body)
}
info, err := os.Stat(filepath.Join(home, "empty.txt"))
if err != nil || info.Size() != 0 {
t.Fatalf("stored file = %v %v", info, err)
}
})
refusals := []struct {
label string
dir string
name string
want int
}{
{"missing name", "~/project", "", http.StatusBadRequest},
{"name with slash", "~/project", "sub%2Fname.txt", http.StatusBadRequest},
{"dot dot name", "~/project", "..", http.StatusBadRequest},
{"outside home", filepath.Join(home, "..", "outside"), "x.txt", http.StatusForbidden},
{"symlink outside home", "~/escape", "x.txt", http.StatusForbidden},
{"missing folder", "~/nowhere", "x.txt", http.StatusNotFound},
{"file is not a folder", "~/project/kept.txt", "x.txt", http.StatusNotFound},
{"relative path", "project", "x.txt", http.StatusNotFound},
}
for _, tc := range refusals {
t.Run(tc.label, func(t *testing.T) {
rec := postHomeFile(t, home, tc.dir, tc.name, "x")
if rec.Code != tc.want {
t.Fatalf("status = %d %s, want %d", rec.Code, rec.Body, tc.want)
}
})
}
if entries, _ := os.ReadDir(outside); len(entries) != 0 {
t.Fatalf("outside directory gained %d entries", len(entries))
}
t.Run("declared size over the limit", func(t *testing.T) {
req := httptest.NewRequest(http.MethodPost, "/files?path=~/project", strings.NewReader("x"))
req.Header.Set(chatUploadHeader, "huge.bin")
req.ContentLength = maxHomeFileUploadBytes + 1
rec := httptest.NewRecorder()
serveHomeFileUploadTo(rec, req, home)
if rec.Code != http.StatusRequestEntityTooLarge {
t.Fatalf("status = %d %s", rec.Code, rec.Body)
}
if _, err := os.Stat(filepath.Join(home, "project", "huge.bin")); !os.IsNotExist(err) {
t.Fatalf("oversized file was stored: %v", err)
}
})
t.Run("cross-site post is refused", func(t *testing.T) {
req := httptest.NewRequest(http.MethodPost, "/files?path=~/project", strings.NewReader("x"))
req.Header.Set(chatUploadHeader, "cross.txt")
req.Header.Set("Sec-Fetch-Site", "cross-site")
rec := httptest.NewRecorder()
serveHomeFileUploadTo(rec, req, home)
if rec.Code != http.StatusForbidden {
t.Fatalf("status = %d %s", rec.Code, rec.Body)
}
})
}
+1
View File
@@ -155,6 +155,7 @@ func HandlerWithOptions(ctx context.Context, options Options, stores ...*pipelin
})
mux.HandleFunc("GET /discovery", serveServerDiscovery)
mux.HandleFunc("GET /files", serveHomeFile)
mux.HandleFunc("POST /files", serveHomeFileUpload)
mux.HandleFunc("POST /chat-uploads", serveChatUpload)
mux.HandleFunc("GET /healthz", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")