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) } }