package server import ( "net/http" "net/http/httptest" "net/url" "os" "path/filepath" "strings" "testing" ) // swapDirToSymlink returns a one-shot test hook that replaces dir with a // symlink to target. It runs after the handler has resolved and validated the // request path, which is exactly the race a validated-then-used host path // leaves open. func swapDirToSymlink(t *testing.T, dir, target string) func() { t.Helper() swapped := false return func() { if swapped { return } swapped = true if err := os.RemoveAll(dir); err != nil { t.Errorf("remove %s: %v", dir, err) return } mustSymlink(t, target, dir) } } func wsDo(t *testing.T, method, target string, h http.HandlerFunc) *httptest.ResponseRecorder { t.Helper() req := httptest.NewRequest(method, target, nil) rr := httptest.NewRecorder() h(rr, req) return rr } func mustWriteFile(t *testing.T, path, content string) { t.Helper() if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil { t.Fatal(err) } if err := os.WriteFile(path, []byte(content), 0o644); err != nil { t.Fatal(err) } } // TestWorkspaceReadRootConfined covers the read handler after the port to // os.OpenRoot: a pre-planted symlink out of the data dir is refused, a parent // swapped for an escaping symlink between validation and I/O is refused, and a // normal file still reads. func TestWorkspaceReadRootConfined(t *testing.T) { base := t.TempDir() workDir := filepath.Join(base, "work") outsideDir := filepath.Join(base, "outside") mustWriteFile(t, filepath.Join(workDir, "sub", "plain.txt"), "PLAIN_READ") mustWriteFile(t, filepath.Join(outsideDir, "secret.txt"), "TOP_SECRET") s := newTestWorkspaceServer(workDir) t.Run("normal_file_reads", func(t *testing.T) { rr := wsDo(t, http.MethodGet, "/api/workspace/read?path="+url.QueryEscape("sub/plain.txt"), s.wsRead) if rr.Code != http.StatusOK || !strings.Contains(rr.Body.String(), "PLAIN_READ") { t.Fatalf("wsRead normal file = %d (%s), want 200 with PLAIN_READ", rr.Code, rr.Body.String()) } }) t.Run("preplanted_outside_symlink_refused", func(t *testing.T) { mustSymlink(t, outsideDir, filepath.Join(workDir, "escape")) rr := wsDo(t, http.MethodGet, "/api/workspace/read?path="+url.QueryEscape("escape/secret.txt"), s.wsRead) if rr.Code != http.StatusBadRequest { t.Fatalf("wsRead pre-planted escaping symlink = %d (%s), want 400", rr.Code, rr.Body.String()) } if strings.Contains(rr.Body.String(), "TOP_SECRET") { t.Fatal("wsRead leaked outside content through pre-planted symlink") } }) t.Run("parent_swapped_to_outside_symlink_refused", func(t *testing.T) { wsBeforeIOHook = swapDirToSymlink(t, filepath.Join(workDir, "sub"), outsideDir) t.Cleanup(func() { wsBeforeIOHook = nil }) rr := wsDo(t, http.MethodGet, "/api/workspace/read?path="+url.QueryEscape("sub/secret.txt"), s.wsRead) if strings.Contains(rr.Body.String(), "TOP_SECRET") { t.Fatal("wsRead leaked outside content across a symlink swap") } if rr.Code == http.StatusOK { t.Fatalf("wsRead read a file outside the data dir across a symlink swap: %d (%s)", rr.Code, rr.Body.String()) } }) } // TestWorkspaceListRootConfined covers the list handler after the port. func TestWorkspaceListRootConfined(t *testing.T) { base := t.TempDir() workDir := filepath.Join(base, "work") outsideDir := filepath.Join(base, "outside") mustWriteFile(t, filepath.Join(workDir, "sub", "plain.txt"), "PLAIN_LIST") mustWriteFile(t, filepath.Join(outsideDir, "outside-only.txt"), "TOP_SECRET") s := newTestWorkspaceServer(workDir) t.Run("normal_directory_lists", func(t *testing.T) { rr := wsDo(t, http.MethodGet, "/api/workspace/list?path="+url.QueryEscape("sub"), s.wsList) if rr.Code != http.StatusOK || !strings.Contains(rr.Body.String(), "plain.txt") { t.Fatalf("wsList normal dir = %d (%s), want 200 with plain.txt", rr.Code, rr.Body.String()) } }) t.Run("root_lists", func(t *testing.T) { rr := wsDo(t, http.MethodGet, "/api/workspace/list?path=", s.wsList) if rr.Code != http.StatusOK || !strings.Contains(rr.Body.String(), "sub") { t.Fatalf("wsList root = %d (%s), want 200 with sub", rr.Code, rr.Body.String()) } }) t.Run("preplanted_outside_symlink_refused", func(t *testing.T) { mustSymlink(t, outsideDir, filepath.Join(workDir, "escape")) rr := wsDo(t, http.MethodGet, "/api/workspace/list?path="+url.QueryEscape("escape"), s.wsList) if rr.Code != http.StatusBadRequest { t.Fatalf("wsList pre-planted escaping symlink = %d (%s), want 400", rr.Code, rr.Body.String()) } }) t.Run("parent_swapped_to_outside_symlink_refused", func(t *testing.T) { wsBeforeIOHook = swapDirToSymlink(t, filepath.Join(workDir, "sub"), outsideDir) t.Cleanup(func() { wsBeforeIOHook = nil }) rr := wsDo(t, http.MethodGet, "/api/workspace/list?path="+url.QueryEscape("sub"), s.wsList) if strings.Contains(rr.Body.String(), "outside-only.txt") { t.Fatal("wsList leaked outside directory entries across a symlink swap") } if rr.Code == http.StatusOK { t.Fatalf("wsList listed a directory outside the data dir across a symlink swap: %d (%s)", rr.Code, rr.Body.String()) } }) } // TestWorkspaceDownloadRootConfined covers the download handler after the port // to root.Open + http.ServeContent. func TestWorkspaceDownloadRootConfined(t *testing.T) { base := t.TempDir() workDir := filepath.Join(base, "work") outsideDir := filepath.Join(base, "outside") mustWriteFile(t, filepath.Join(workDir, "sub", "plain.txt"), "PLAIN_DOWNLOAD") mustWriteFile(t, filepath.Join(outsideDir, "secret.txt"), "TOP_SECRET") s := newTestWorkspaceServer(workDir) t.Run("normal_file_downloads", func(t *testing.T) { rr := wsDo(t, http.MethodGet, "/api/workspace/download?path="+url.QueryEscape("sub/plain.txt"), s.wsDownload) if rr.Code != http.StatusOK || rr.Body.String() != "PLAIN_DOWNLOAD" { t.Fatalf("wsDownload normal file = %d (%q), want 200 PLAIN_DOWNLOAD", rr.Code, rr.Body.String()) } if cd := rr.Header().Get("Content-Disposition"); !strings.Contains(cd, "attachment") { t.Fatalf("wsDownload missing attachment Content-Disposition: %q", cd) } }) t.Run("preplanted_outside_symlink_refused", func(t *testing.T) { mustSymlink(t, outsideDir, filepath.Join(workDir, "escape")) rr := wsDo(t, http.MethodGet, "/api/workspace/download?path="+url.QueryEscape("escape/secret.txt"), s.wsDownload) if rr.Code != http.StatusBadRequest { t.Fatalf("wsDownload pre-planted escaping symlink = %d (%s), want 400", rr.Code, rr.Body.String()) } if strings.Contains(rr.Body.String(), "TOP_SECRET") { t.Fatal("wsDownload leaked outside content through pre-planted symlink") } }) t.Run("parent_swapped_to_outside_symlink_refused", func(t *testing.T) { wsBeforeIOHook = swapDirToSymlink(t, filepath.Join(workDir, "sub"), outsideDir) t.Cleanup(func() { wsBeforeIOHook = nil }) rr := wsDo(t, http.MethodGet, "/api/workspace/download?path="+url.QueryEscape("sub/secret.txt"), s.wsDownload) if strings.Contains(rr.Body.String(), "TOP_SECRET") { t.Fatal("wsDownload leaked outside content across a symlink swap") } if rr.Code == http.StatusOK { t.Fatalf("wsDownload served a file outside the data dir across a symlink swap: %d (%s)", rr.Code, rr.Body.String()) } }) } // TestWorkspaceDeleteRootConfined covers the delete handler after the port to // root.RemoveAll. A swapped parent previously made RemoveAll delete the symlink // target outside the data dir. func TestWorkspaceDeleteRootConfined(t *testing.T) { base := t.TempDir() workDir := filepath.Join(base, "work") outsideDir := filepath.Join(base, "outside") mustWriteFile(t, filepath.Join(workDir, "sub", "inner", "inside.txt"), "INSIDE") mustWriteFile(t, filepath.Join(outsideDir, "inner", "victim.txt"), "KEEP_OUTSIDE") s := newTestWorkspaceServer(workDir) t.Run("normal_directory_deletes", func(t *testing.T) { mustWriteFile(t, filepath.Join(workDir, "normaldir", "file.txt"), "X") rr := wsDo(t, http.MethodDelete, "/api/workspace/delete?path="+url.QueryEscape("normaldir"), s.wsDelete) if rr.Code != http.StatusOK { t.Fatalf("wsDelete normal dir = %d (%s), want 200", rr.Code, rr.Body.String()) } if _, err := os.Lstat(filepath.Join(workDir, "normaldir")); !os.IsNotExist(err) { t.Fatalf("wsDelete left the directory behind: %v", err) } }) t.Run("preplanted_outside_symlink_refused", func(t *testing.T) { mustSymlink(t, outsideDir, filepath.Join(workDir, "escape")) rr := wsDo(t, http.MethodDelete, "/api/workspace/delete?path="+url.QueryEscape("escape"), s.wsDelete) if rr.Code != http.StatusBadRequest { t.Fatalf("wsDelete pre-planted escaping symlink = %d (%s), want 400", rr.Code, rr.Body.String()) } if _, err := os.Stat(filepath.Join(outsideDir, "inner", "victim.txt")); err != nil { t.Fatalf("wsDelete removed outside data through pre-planted symlink: %v", err) } }) t.Run("parent_swapped_to_outside_symlink_refused", func(t *testing.T) { wsBeforeIOHook = swapDirToSymlink(t, filepath.Join(workDir, "sub"), outsideDir) t.Cleanup(func() { wsBeforeIOHook = nil }) rr := wsDo(t, http.MethodDelete, "/api/workspace/delete?path="+url.QueryEscape("sub/inner"), s.wsDelete) if _, err := os.Stat(filepath.Join(outsideDir, "inner", "victim.txt")); err != nil { t.Fatalf("wsDelete deleted outside data across a symlink swap: %v", err) } if rr.Code == http.StatusOK { t.Fatalf("wsDelete removed a directory outside the data dir across a symlink swap: %d (%s)", rr.Code, rr.Body.String()) } }) } // TestWorkspaceDeniedPrefixSwapRefused covers the intra-root TOCTOU found in // review: root.Open/root.RemoveAll follow symlinks that stay inside // cp.realRoot, so a parent swapped after wsResolveConfined to a denied subtree // (traffic/_ca, traffic/_index, traffic/_blobs, archives) would otherwise be // followed and bypass workspacePathDenied. The real-directory verification in // front of each root operation must refuse the swap. func TestWorkspaceDeniedPrefixSwapRefused(t *testing.T) { newFixture := func(t *testing.T) (workDir, caDir, archivesDir string, s *Server) { t.Helper() workDir = t.TempDir() caDir = filepath.Join(workDir, "traffic", "_ca") archivesDir = filepath.Join(workDir, "archives") mustWriteFile(t, filepath.Join(workDir, "sub", "inner", "file.txt"), "SUB") mustWriteFile(t, filepath.Join(caDir, "secret.txt"), "CA_SECRET") mustWriteFile(t, filepath.Join(caDir, "inner", "ca-victim.txt"), "CA_KEEP") mustWriteFile(t, filepath.Join(archivesDir, "inner", "archive-victim.txt"), "ARCHIVE_KEEP") return workDir, caDir, archivesDir, newTestWorkspaceServer(workDir) } t.Run("list_parent_swapped_to_denied_prefix_refused", func(t *testing.T) { workDir, _, _, s := newFixture(t) wsBeforeIOHook = swapDirToSymlink(t, filepath.Join(workDir, "sub"), filepath.Join("traffic", "_ca")) t.Cleanup(func() { wsBeforeIOHook = nil }) rr := wsDo(t, http.MethodGet, "/api/workspace/list?path="+url.QueryEscape("sub"), s.wsList) if rr.Code == http.StatusOK { t.Fatalf("wsList listed a denied subtree across a symlink swap: %d (%s)", rr.Code, rr.Body.String()) } if strings.Contains(rr.Body.String(), "secret.txt") { t.Fatal("wsList leaked traffic/_ca entries across a symlink swap") } }) t.Run("read_parent_swapped_to_denied_prefix_refused", func(t *testing.T) { workDir, _, _, s := newFixture(t) wsBeforeIOHook = swapDirToSymlink(t, filepath.Join(workDir, "sub"), filepath.Join("traffic", "_ca")) t.Cleanup(func() { wsBeforeIOHook = nil }) rr := wsDo(t, http.MethodGet, "/api/workspace/read?path="+url.QueryEscape("sub/secret.txt"), s.wsRead) if strings.Contains(rr.Body.String(), "CA_SECRET") { t.Fatal("wsRead leaked traffic/_ca content across a symlink swap") } if rr.Code == http.StatusOK { t.Fatalf("wsRead read a denied subtree across a symlink swap: %d (%s)", rr.Code, rr.Body.String()) } }) t.Run("download_parent_swapped_to_denied_prefix_refused", func(t *testing.T) { workDir, _, _, s := newFixture(t) wsBeforeIOHook = swapDirToSymlink(t, filepath.Join(workDir, "sub"), filepath.Join("traffic", "_ca")) t.Cleanup(func() { wsBeforeIOHook = nil }) rr := wsDo(t, http.MethodGet, "/api/workspace/download?path="+url.QueryEscape("sub/secret.txt"), s.wsDownload) if strings.Contains(rr.Body.String(), "CA_SECRET") { t.Fatal("wsDownload leaked traffic/_ca content across a symlink swap") } if rr.Code == http.StatusOK { t.Fatalf("wsDownload served a denied subtree across a symlink swap: %d (%s)", rr.Code, rr.Body.String()) } }) t.Run("delete_parent_swapped_to_traffic_ca_refused", func(t *testing.T) { workDir, caDir, _, s := newFixture(t) wsBeforeIOHook = swapDirToSymlink(t, filepath.Join(workDir, "sub"), filepath.Join("traffic", "_ca")) t.Cleanup(func() { wsBeforeIOHook = nil }) rr := wsDo(t, http.MethodDelete, "/api/workspace/delete?path="+url.QueryEscape("sub/inner"), s.wsDelete) if rr.Code == http.StatusOK { t.Fatalf("wsDelete removed a denied subtree across a symlink swap: %d (%s)", rr.Code, rr.Body.String()) } if _, err := os.Stat(filepath.Join(caDir, "inner", "ca-victim.txt")); err != nil { t.Fatalf("wsDelete removed traffic/_ca data across a symlink swap: %v", err) } }) t.Run("delete_parent_swapped_to_archives_refused", func(t *testing.T) { workDir, _, archivesDir, s := newFixture(t) wsBeforeIOHook = swapDirToSymlink(t, filepath.Join(workDir, "sub"), "archives") t.Cleanup(func() { wsBeforeIOHook = nil }) rr := wsDo(t, http.MethodDelete, "/api/workspace/delete?path="+url.QueryEscape("sub/inner"), s.wsDelete) if rr.Code == http.StatusOK { t.Fatalf("wsDelete removed the archives subtree across a symlink swap: %d (%s)", rr.Code, rr.Body.String()) } if _, err := os.Stat(filepath.Join(archivesDir, "inner", "archive-victim.txt")); err != nil { t.Fatalf("wsDelete removed archives data across a symlink swap: %v", err) } }) }