Add rename_path, move_path and delete_path for home files

The chat Files panel needs to rename, move and delete entries. Three
websocket requests, dispatched beside list_directory, operate on one
entry inside the operator's home and reply with path_mutated {path} or
an error frame (does not exist, already exists, is not empty, outside
the home directory, cannot be moved into itself).

The entry's parent folder and the move destination are resolved through
symlinks and must stay inside home; the entry's own symlink is never
followed, so a link is renamed, moved or removed as a link. The home
directory itself, anything outside it and pseudo filesystems are
refused. A folder is deleted only when empty unless recursive is set;
os.RemoveAll unlinks symlinks rather than descending into them.
This commit is contained in:
2026-11-09 01:32:31 +00:00
parent a02b271509
commit c61490aeb0
6 changed files with 555 additions and 0 deletions
+16
View File
@@ -632,6 +632,22 @@ sessions pipeline runs create. The protocol types are:
similar) are refused, permission problems are reported as
`permission denied: <path>`, and a reply carries at most 2000 entries with
`truncated: true` when more exist.
- `rename_path`, `move_path` and `delete_path` back the chat Files panel's
Rename, Move and Delete. Bodies: `rename_path: {"path", "name"}` (a plain new
name in the same folder: no separators, not `.`/`..`, at most 255 bytes),
`move_path: {"path", "into"}` (the entry keeps its name; an existing name in
`into` is refused, as is moving a folder into itself or a descendant) and
`delete_path: {"path", "recursive"}` (files and symlinks always; a folder only
when empty unless `recursive` is true). Every path is absolute or `~`-based and
confined to the home subtree like `list_directory`: the parent folder (and
`into`) is resolved through symlinks and must stay inside home, the home
directory itself is never renamed, moved or deleted, pseudo filesystems are
refused, and the entry's own symlink is never followed (the link is renamed,
moved or removed; its target is untouched). The reply is
`path_mutated: {"path"}` with the resulting path under home; refusals are
`error` frames saying `... does not exist`, `... already exists`,
`... is not empty`, `... cannot be moved into itself` or `... is outside the
home directory ...`.
- `server_stats` returns `server_stats_result` with a `server_stats` snapshot of
host, CPU, memory, local storage, Pipelines data, process and GPU information.
Sampling is bounded to about three seconds; unavailable sections carry an
+45
View File
@@ -47,3 +47,48 @@ type DirectoryEntry struct {
Size int64 `json:"size,omitempty"`
Modified string `json:"modified,omitempty"`
}
// Home file mutations for the chat Files panel. Each request names one entry
// inside the server user's home by absolute or "~/" path and is answered with a
// PathMutated reply carrying the resulting path, or an Error reply whose text
// says why (does not exist, already exists, is not empty, outside the home
// directory). The home directory itself and anything outside it are never
// touched; symlinks are renamed, moved or removed as links, never followed.
const (
RenamePath = "rename_path"
MovePath = "move_path"
DeletePath = "delete_path"
PathMutated = "path_mutated"
)
// FolderNotEmpty is the phrase a delete_path refusal carries when the folder
// has contents and Recursive was false; a client then offers a recursive delete.
const FolderNotEmpty = "is not empty"
// RenamePathRequest is the rename_path body: the entry's path and its new
// plain name within the same folder (no separators, not "." or "..").
type RenamePathRequest struct {
Path string `json:"path"`
Name string `json:"name"`
}
// MovePathRequest is the move_path body: the entry's path and the folder it
// moves into, keeping its name. An existing name in Into is refused, as is
// moving a folder into itself or one of its descendants.
type MovePathRequest struct {
Path string `json:"path"`
Into string `json:"into"`
}
// DeletePathRequest is the delete_path body. A file or symlink is always
// removed; a folder only when empty unless Recursive is true.
type DeletePathRequest struct {
Path string `json:"path"`
Recursive bool `json:"recursive,omitempty"`
}
// PathMutatedResult is the path_mutated reply: the renamed, moved or deleted
// entry's resulting path, expressed under the home directory.
type PathMutatedResult struct {
Path string `json:"path"`
}
+6
View File
@@ -266,6 +266,12 @@ type Message struct {
// the home-restricted folder picker listing (see directories.go).
ListDirectory *DirectoryListRequest `json:"list_directory,omitempty"`
DirectoryListing *DirectoryListingResult `json:"directory_listing,omitempty"`
// RenamePath, MovePath and DeletePath are the Files panel's home file
// mutations and PathMutated their shared reply (see directories.go).
RenamePath *RenamePathRequest `json:"rename_path,omitempty"`
MovePath *MovePathRequest `json:"move_path,omitempty"`
DeletePath *DeletePathRequest `json:"delete_path,omitempty"`
PathMutated *PathMutatedResult `json:"path_mutated,omitempty"`
// ChatAttachmentImageRequest is the chat_attachment_image body and
// ChatAttachmentImage its reply.
ChatAttachmentImageRequest *ChatAttachmentImageRequest `json:"chat_attachment_image_request,omitempty"`
+217
View File
@@ -0,0 +1,217 @@
package server
import (
"errors"
"io"
"io/fs"
"os"
"path/filepath"
"strings"
"git.a-wan.net/alan/pipelines/internal/protocol"
)
// handleHomeFileMutation answers rename_path, move_path and delete_path. It
// returns true when the request was one of them; the reply (path_mutated or
// error, echoing the request ID) is written from its own goroutine so a slow
// recursive delete never stalls the connection's other traffic.
func handleHomeFileMutation(request protocol.Message, write func(protocol.Message) error) bool {
switch request.Type {
case protocol.RenamePath, protocol.MovePath, protocol.DeletePath:
default:
return false
}
go func() {
path, err := applyHomeFileMutation(request)
if err != nil {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()})
return
}
_ = write(protocol.Message{Type: protocol.PathMutated, ID: request.ID, PathMutated: &protocol.PathMutatedResult{Path: path}})
}()
return true
}
func applyHomeFileMutation(request protocol.Message) (string, error) {
home, err := OperatorHomeDirectory()
if err != nil {
return "", err
}
switch request.Type {
case protocol.RenamePath:
if request.RenamePath == nil {
return "", errors.New("rename_path body is missing")
}
return renameHomePath(home, request.RenamePath.Path, request.RenamePath.Name)
case protocol.MovePath:
if request.MovePath == nil {
return "", errors.New("move_path body is missing")
}
return moveHomePath(home, request.MovePath.Path, request.MovePath.Into)
case protocol.DeletePath:
if request.DeletePath == nil {
return "", errors.New("delete_path body is missing")
}
return deleteHomePath(home, request.DeletePath.Path, request.DeletePath.Recursive)
}
return "", errors.New("unsupported mutation " + request.Type)
}
// homeEntry is one existing entry inside home. Its parent folder is resolved
// through symlinks and checked against home; the entry's own name is not
// followed, so a symlink is operated on as a link and never as its target.
type homeEntry struct {
home string
realHome string
shown string // the path under home, as the operator sees it
real string // the resolved parent joined with the entry's name
info fs.FileInfo
}
func locateHomeEntry(home, requested string) (homeEntry, error) {
home = filepath.Clean(home)
realHome, err := filepath.EvalSymlinks(home)
if err != nil {
return homeEntry{}, errors.New("resolve home directory: " + err.Error())
}
target, err := expandHomePath(home, requested)
if err != nil {
return homeEntry{}, err
}
if target == home || target == realHome {
return homeEntry{}, errors.New("the home directory itself cannot be renamed, moved or deleted")
}
name := filepath.Base(target)
if name == "." || name == ".." || name == string(filepath.Separator) {
return homeEntry{}, errors.New("path " + requested + " does not name an entry")
}
relParent, realParent, err := resolveInsideHome(realHome, filepath.Dir(target))
if err != nil {
return homeEntry{}, err
}
if specialFilesystem(realParent) {
return homeEntry{}, errors.New(target + " is on a special filesystem and cannot be changed")
}
entry := homeEntry{
home: home,
realHome: realHome,
shown: filepath.Join(home, relParent, name),
real: filepath.Join(realParent, name),
}
entry.info, err = os.Lstat(entry.real)
if err != nil {
return homeEntry{}, describePathError(entry.shown, err)
}
return entry, nil
}
// renameHomePath gives the entry a new plain name in its own folder. An
// existing name is refused, except the entry's own (a case-only rename on a
// case-insensitive filesystem).
func renameHomePath(home, requested, newName string) (string, error) {
entry, err := locateHomeEntry(home, requested)
if err != nil {
return "", err
}
name, err := uploadFileName(newName)
if err != nil {
return "", err
}
dest := filepath.Join(filepath.Dir(entry.real), name)
shown := filepath.Join(filepath.Dir(entry.shown), name)
if existing, err := os.Lstat(dest); err == nil && !os.SameFile(existing, entry.info) {
return "", errors.New(shown + " already exists")
}
if err := os.Rename(entry.real, dest); err != nil {
return "", describePathError(entry.shown, err)
}
return shown, nil
}
// moveHomePath moves the entry, keeping its name, into another folder inside
// home. The destination folder is resolved through symlinks and must stay
// inside home; an existing name there is refused, as is moving a folder into
// itself or a descendant.
func moveHomePath(home, requested, into string) (string, error) {
entry, err := locateHomeEntry(home, requested)
if err != nil {
return "", err
}
intoTarget, err := expandHomePath(entry.home, into)
if err != nil {
return "", err
}
relInto, realInto, err := resolveInsideHome(entry.realHome, intoTarget)
if err != nil {
return "", err
}
shownInto := filepath.Join(entry.home, relInto)
info, err := os.Stat(realInto)
if err != nil {
return "", describePathError(shownInto, err)
}
if !info.IsDir() {
return "", errors.New(shownInto + " is not a folder")
}
if specialFilesystem(realInto) {
return "", errors.New(shownInto + " is on a special filesystem and cannot be changed")
}
if entry.info.IsDir() && (realInto == entry.real || strings.HasPrefix(realInto, entry.real+string(filepath.Separator))) {
return "", errors.New(entry.shown + " cannot be moved into itself")
}
dest := filepath.Join(realInto, entry.info.Name())
shown := filepath.Join(shownInto, entry.info.Name())
if _, err := os.Lstat(dest); err == nil {
return "", errors.New(shown + " already exists")
}
if err := os.Rename(entry.real, dest); err != nil {
return "", describePathError(entry.shown, err)
}
return shown, nil
}
// deleteHomePath removes a file or symlink (the link, never its target), an
// empty folder, or with recursive a folder and its contents. os.RemoveAll
// unlinks symlinks it meets rather than descending into them.
func deleteHomePath(home, requested string, recursive bool) (string, error) {
entry, err := locateHomeEntry(home, requested)
if err != nil {
return "", err
}
if !entry.info.IsDir() {
if err := os.Remove(entry.real); err != nil {
return "", describePathError(entry.shown, err)
}
return entry.shown, nil
}
if recursive {
if err := os.RemoveAll(entry.real); err != nil {
return "", describePathError(entry.shown, err)
}
return entry.shown, nil
}
empty, err := directoryEmpty(entry.real)
if err != nil {
return "", describePathError(entry.shown, err)
}
if !empty {
return "", errors.New(entry.shown + " " + protocol.FolderNotEmpty)
}
if err := os.Remove(entry.real); err != nil {
return "", describePathError(entry.shown, err)
}
return entry.shown, nil
}
func directoryEmpty(path string) (bool, error) {
dir, err := os.Open(path)
if err != nil {
return false, err
}
defer dir.Close()
_, err = dir.ReadDir(1)
if errors.Is(err, io.EOF) {
return true, nil
}
return false, err
}
+268
View File
@@ -0,0 +1,268 @@
package server
import (
"os"
"path/filepath"
"strings"
"testing"
"git.a-wan.net/alan/pipelines/internal/protocol"
"github.com/coder/websocket/wsjson"
)
// newMutationHome builds a scratch home with a file, a nested folder, an empty
// folder, and two symlinks that escape to a sibling directory outside home.
func newMutationHome(t *testing.T) (home, outside string) {
t.Helper()
root := t.TempDir()
home = filepath.Join(root, "home")
outside = filepath.Join(root, "outside")
for _, dir := range []string{"project/sub", "empty", "other"} {
mustMkdir(t, filepath.Join(home, dir))
}
mustMkdir(t, outside)
files := map[string]string{
filepath.Join(home, "project", "kept.txt"): "kept",
filepath.Join(home, "project", "sub", "inner.txt"): "inner",
filepath.Join(home, "other", "taken.txt"): "taken",
filepath.Join(outside, "secret.txt"): "secret",
}
for path, body := range files {
if err := os.WriteFile(path, []byte(body), 0o644); err != nil {
t.Fatal(err)
}
}
if err := os.Symlink(outside, filepath.Join(home, "escape")); err != nil {
t.Fatal(err)
}
if err := os.Symlink(filepath.Join(outside, "secret.txt"), filepath.Join(home, "link.txt")); err != nil {
t.Fatal(err)
}
return home, outside
}
func exists(path string) bool {
_, err := os.Lstat(path)
return err == nil
}
func wantRefusal(t *testing.T, err error, phrase string) {
t.Helper()
if err == nil || !strings.Contains(err.Error(), phrase) {
t.Fatalf("error = %v, want one containing %q", err, phrase)
}
}
func TestRenameHomePath(t *testing.T) {
home, outside := newMutationHome(t)
got, err := renameHomePath(home, "~/project/kept.txt", "renamed.txt")
if err != nil {
t.Fatal(err)
}
if want := filepath.Join(home, "project", "renamed.txt"); got != want || !exists(want) || exists(filepath.Join(home, "project", "kept.txt")) {
t.Fatalf("rename = %q; renamed exists %v", got, exists(want))
}
got, err = renameHomePath(home, filepath.Join(home, "project"), "work")
if err != nil || got != filepath.Join(home, "work") || !exists(filepath.Join(home, "work", "renamed.txt")) {
t.Fatalf("folder rename = %q, %v", got, err)
}
// The link is renamed; its target outside home is untouched.
got, err = renameHomePath(home, "~/link.txt", "moved-link.txt")
if err != nil || got != filepath.Join(home, "moved-link.txt") {
t.Fatalf("link rename = %q, %v", got, err)
}
if target, err := os.Readlink(filepath.Join(home, "moved-link.txt")); err != nil || target != filepath.Join(outside, "secret.txt") {
t.Fatalf("link target = %q, %v", target, err)
}
if data, _ := os.ReadFile(filepath.Join(outside, "secret.txt")); string(data) != "secret" {
t.Fatalf("outside file changed: %q", data)
}
}
func TestRenameHomePathRefusals(t *testing.T) {
home, outside := newMutationHome(t)
cases := []struct {
name, path, newName, phrase string
}{
{"existing name", "~/project/kept.txt", "sub", "already exists"},
{"separator", "~/project/kept.txt", "a/b", "path separators"},
{"dot dot", "~/project/kept.txt", "..", "file name is missing"},
{"blank", "~/project/kept.txt", " ", "file name is missing"},
{"missing", "~/project/nope.txt", "x", "does not exist"},
{"home itself", "~", "x", "home directory itself"},
{"home by path", home, "x", "home directory itself"},
{"outside", filepath.Join(outside, "secret.txt"), "x", "outside the home directory"},
{"dot dot escape", filepath.Join(home, "..", "outside", "secret.txt"), "x", "outside the home directory"},
{"through escaping symlink", "~/escape/secret.txt", "x", "outside the home directory"},
{"relative", "project/kept.txt", "x", "must be absolute"},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
_, err := renameHomePath(home, tc.path, tc.newName)
wantRefusal(t, err, tc.phrase)
})
}
if !exists(filepath.Join(home, "project", "kept.txt")) || !exists(filepath.Join(outside, "secret.txt")) {
t.Fatal("a refused rename changed the tree")
}
}
func TestMoveHomePath(t *testing.T) {
home, _ := newMutationHome(t)
got, err := moveHomePath(home, "~/project/kept.txt", "~/empty")
if err != nil {
t.Fatal(err)
}
if want := filepath.Join(home, "empty", "kept.txt"); got != want || !exists(want) || exists(filepath.Join(home, "project", "kept.txt")) {
t.Fatalf("move = %q", got)
}
got, err = moveHomePath(home, filepath.Join(home, "project", "sub"), home)
if err != nil || got != filepath.Join(home, "sub") || !exists(filepath.Join(home, "sub", "inner.txt")) {
t.Fatalf("folder move = %q, %v", got, err)
}
// A symlink moves as a link.
got, err = moveHomePath(home, "~/link.txt", "~/other")
if err != nil || got != filepath.Join(home, "other", "link.txt") {
t.Fatalf("link move = %q, %v", got, err)
}
if info, err := os.Lstat(filepath.Join(home, "other", "link.txt")); err != nil || info.Mode()&os.ModeSymlink == 0 {
t.Fatalf("moved link = %v, %v", info, err)
}
}
func TestMoveHomePathRefusals(t *testing.T) {
home, outside := newMutationHome(t)
if err := os.WriteFile(filepath.Join(home, "other", "kept.txt"), []byte("blocker"), 0o644); err != nil {
t.Fatal(err)
}
cases := []struct {
name, path, into, phrase string
}{
{"target exists", "~/project/kept.txt", "~/other", "already exists"},
{"same folder", "~/project/kept.txt", "~/project", "already exists"},
{"into itself", "~/project", "~/project", "into itself"},
{"into descendant", "~/project", "~/project/sub", "into itself"},
{"into a file", "~/project/kept.txt", "~/other/taken.txt", "is not a folder"},
{"into missing", "~/project/kept.txt", "~/nowhere", "does not exist"},
{"into outside", "~/project/kept.txt", outside, "outside the home directory"},
{"into escaping symlink", "~/project/kept.txt", "~/escape", "outside the home directory"},
{"source outside", filepath.Join(outside, "secret.txt"), "~/empty", "outside the home directory"},
{"source through symlink", "~/escape/secret.txt", "~/empty", "outside the home directory"},
{"source home", "~", "~/empty", "home directory itself"},
{"source missing", "~/project/nope.txt", "~/empty", "does not exist"},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
_, err := moveHomePath(home, tc.path, tc.into)
wantRefusal(t, err, tc.phrase)
})
}
if data, _ := os.ReadFile(filepath.Join(home, "other", "kept.txt")); string(data) != "blocker" {
t.Fatalf("existing target replaced: %q", data)
}
if !exists(filepath.Join(home, "project", "kept.txt")) || !exists(filepath.Join(outside, "secret.txt")) || !exists(filepath.Join(home, "project", "sub", "inner.txt")) {
t.Fatal("a refused move changed the tree")
}
}
func TestDeleteHomePath(t *testing.T) {
home, outside := newMutationHome(t)
got, err := deleteHomePath(home, "~/project/kept.txt", false)
if err != nil || got != filepath.Join(home, "project", "kept.txt") || exists(got) {
t.Fatalf("file delete = %q, %v", got, err)
}
if _, err := deleteHomePath(home, "~/empty", false); err != nil || exists(filepath.Join(home, "empty")) {
t.Fatalf("empty folder delete: %v", err)
}
_, err = deleteHomePath(home, "~/project", false)
wantRefusal(t, err, protocol.FolderNotEmpty)
if !exists(filepath.Join(home, "project", "sub", "inner.txt")) {
t.Fatal("non-recursive delete removed contents")
}
// A folder holding an escaping symlink is removed without following it.
if err := os.Symlink(outside, filepath.Join(home, "project", "sub", "out")); err != nil {
t.Fatal(err)
}
if got, err := deleteHomePath(home, "~/project", true); err != nil || got != filepath.Join(home, "project") || exists(got) {
t.Fatalf("recursive delete = %q, %v", got, err)
}
if data, _ := os.ReadFile(filepath.Join(outside, "secret.txt")); string(data) != "secret" {
t.Fatalf("recursive delete followed a symlink: %q", data)
}
// Deleting a symlink removes the link only.
if _, err := deleteHomePath(home, "~/link.txt", false); err != nil || exists(filepath.Join(home, "link.txt")) {
t.Fatalf("link delete: %v", err)
}
if _, err := deleteHomePath(home, "~/escape", true); err != nil || exists(filepath.Join(home, "escape")) {
t.Fatalf("escaping folder link delete: %v", err)
}
if !exists(filepath.Join(outside, "secret.txt")) {
t.Fatal("deleting a folder symlink removed its target")
}
}
func TestDeleteHomePathRefusals(t *testing.T) {
home, outside := newMutationHome(t)
cases := []struct {
name, path, phrase string
}{
{"home", "~", "home directory itself"},
{"home by path", home + "/", "home directory itself"},
{"outside", filepath.Join(outside, "secret.txt"), "outside the home directory"},
{"outside folder", outside, "outside the home directory"},
{"through escaping symlink", "~/escape/secret.txt", "outside the home directory"},
{"dot dot", filepath.Join(home, "project", "..", "..", "outside"), "outside the home directory"},
{"missing", "~/project/nope.txt", "does not exist"},
{"relative", "project", "must be absolute"},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
_, err := deleteHomePath(home, tc.path, true)
wantRefusal(t, err, tc.phrase)
})
}
if !exists(filepath.Join(outside, "secret.txt")) || !exists(home) || !exists(filepath.Join(home, "project", "kept.txt")) {
t.Fatal("a refused delete changed the tree")
}
}
func TestHomeFileMutationOverWebsocket(t *testing.T) {
home, outside := newMutationHome(t)
t.Setenv("HOME", home)
ctx, _, conn := dialChatOptions(t, Options{})
send := func(message protocol.Message) protocol.Message {
t.Helper()
if err := wsjson.Write(ctx, conn, message); err != nil {
t.Fatal(err)
}
var reply protocol.Message
if err := wsjson.Read(ctx, conn, &reply); err != nil {
t.Fatal(err)
}
return reply
}
reply := send(protocol.Message{Type: protocol.RenamePath, ID: "m1", RenamePath: &protocol.RenamePathRequest{Path: "~/project/kept.txt", Name: "new.txt"}})
if reply.Type != protocol.PathMutated || reply.ID != "m1" || reply.PathMutated == nil || reply.PathMutated.Path != filepath.Join(home, "project", "new.txt") {
t.Fatalf("rename reply = %+v", reply)
}
reply = send(protocol.Message{Type: protocol.MovePath, ID: "m2", MovePath: &protocol.MovePathRequest{Path: "~/project/new.txt", Into: "~/empty"}})
if reply.Type != protocol.PathMutated || reply.ID != "m2" || reply.PathMutated.Path != filepath.Join(home, "empty", "new.txt") {
t.Fatalf("move reply = %+v", reply)
}
reply = send(protocol.Message{Type: protocol.DeletePath, ID: "m3", DeletePath: &protocol.DeletePathRequest{Path: "~/project"}})
if reply.Type != protocol.Error || reply.ID != "m3" || !strings.Contains(reply.Error, protocol.FolderNotEmpty) {
t.Fatalf("non-empty delete reply = %+v", reply)
}
reply = send(protocol.Message{Type: protocol.DeletePath, ID: "m4", DeletePath: &protocol.DeletePathRequest{Path: "~/project", Recursive: true}})
if reply.Type != protocol.PathMutated || reply.ID != "m4" || exists(filepath.Join(home, "project")) {
t.Fatalf("recursive delete reply = %+v", reply)
}
reply = send(protocol.Message{Type: protocol.DeletePath, ID: "m5", DeletePath: &protocol.DeletePathRequest{Path: filepath.Join(outside, "secret.txt")}})
if reply.Type != protocol.Error || reply.ID != "m5" || !strings.Contains(reply.Error, "outside the home directory") {
t.Fatalf("outside delete reply = %+v", reply)
}
reply = send(protocol.Message{Type: protocol.RenamePath, ID: "m6"})
if reply.Type != protocol.Error || reply.ID != "m6" || !strings.Contains(reply.Error, "body is missing") {
t.Fatalf("empty body reply = %+v", reply)
}
}
+3
View File
@@ -278,6 +278,9 @@ func HandlerWithOptions(ctx context.Context, options Options, stores ...*pipelin
if handleDirectoryListing(request, write) {
continue
}
if handleHomeFileMutation(request, write) {
continue
}
if request.Type == protocol.DiscoverServersRequest {
go handleServerDiscovery(connCtx, request, write)
continue