diff --git a/server/workspace.go b/server/workspace.go index 82d339d..19044e5 100644 --- a/server/workspace.go +++ b/server/workspace.go @@ -39,8 +39,9 @@ func (s *Server) wsResolveConfined(rel string) (confinedPath, bool) { // workspaceDeniedPrefixes are platform-internal subtrees under the data dir // that the workspace file manager must not expose. traffic/_ca holds the -// capture proxy CA private key; traffic/_index holds the SQLite exchange index. -var workspaceDeniedPrefixes = []string{"traffic/_ca", "traffic/_index"} +// capture proxy CA private key; traffic/_index holds the SQLite exchange index; +// traffic/_blobs holds the captured bodies the index refers to by hash. +var workspaceDeniedPrefixes = []string{"traffic/_ca", "traffic/_index", "traffic/_blobs"} // workspacePathDenied reports whether realPath lands in a platform-internal // subtree. It compares the canonicalized path relative to the real root, so a @@ -69,6 +70,27 @@ func workspacePathDenied(realRoot, realPath string) bool { return false } +// workspaceHoldsDenied reports whether a platform-internal subtree lies beneath +// path, so that removing path would remove it too. Segments are normalised as +// in workspacePathDenied. +func workspaceHoldsDenied(realRoot, path string) bool { + rel, err := filepath.Rel(realRoot, path) + if err != nil { + return true // fail closed + } + segs := strings.Split(filepath.ToSlash(rel), "/") + for i, seg := range segs { + segs[i] = strings.ToLower(strings.TrimRight(seg, ". ")) + } + rel = strings.Join(segs, "/") + for _, prefix := range workspaceDeniedPrefixes { + if strings.HasPrefix(prefix, rel+"/") { + return true + } + } + return false +} + // wsRel renders an absolute path back as a workspace-relative path (forward slashes). func (s *Server) wsRel(abs string) string { base, err := resolveRealRoot(s.m.dir) @@ -235,6 +257,12 @@ func (s *Server) wsDelete(w http.ResponseWriter, r *http.Request) { writeErr(w, 400, "cannot delete the workspace root directory") return } + // entryPath is what RemoveAll removes: a symlink is removed as a link, a + // directory with everything beneath it. + if workspaceHoldsDenied(cp.realRoot, cp.entryPath) { + writeErr(w, 400, "cannot delete a directory that holds platform data (the capture CA, index and blob store)") + return + } if !cp.exists { writeErr(w, 404, "path does not exist") return diff --git a/server/workspace_deny_test.go b/server/workspace_deny_test.go index 70e08fd..447407f 100644 --- a/server/workspace_deny_test.go +++ b/server/workspace_deny_test.go @@ -1,6 +1,9 @@ package server import ( + "net/http" + "net/http/httptest" + "net/url" "os" "path/filepath" "testing" @@ -66,3 +69,77 @@ func TestWorkspacePathDenied(t *testing.T) { } } } + +// Deleting a directory that holds a platform-internal subtree would remove the +// capture CA key and the exchange index with it, and the blob store is as +// internal as the index. Neither is reachable through the file manager. +func TestWorkspaceDeleteKeepsPlatformPathsAndBlobsAreDenied(t *testing.T) { + dir := t.TempDir() + for _, p := range []string{"traffic/_ca", "traffic/_index", "traffic/_blobs/sha256/ab", "traffic/host", "other/sub"} { + if err := os.MkdirAll(filepath.Join(dir, p), 0o755); err != nil { + t.Fatal(err) + } + } + kept := []string{"traffic/_ca/mitmproxy-ca.pem", "traffic/_index/index.sqlite", "traffic/_blobs/sha256/ab/abcd.bin", "traffic/host/note.txt"} + for _, p := range append(kept, "other/sub/note.txt") { + if err := os.WriteFile(filepath.Join(dir, p), []byte("x"), 0o600); err != nil { + t.Fatal(err) + } + } + s := &Server{m: &Manager{dir: dir}} + call := func(h http.HandlerFunc, method, target string) int { + rec := httptest.NewRecorder() + h(rec, httptest.NewRequest(method, target, nil)) + return rec.Code + } + + // The parent of the denied subtrees, in the spellings that reach it. + for _, rel := range []string{"traffic", "traffic/", "TRAFFIC", "traffic."} { + if code := call(s.wsDelete, "DELETE", "/api/workspace/delete?path="+url.QueryEscape(rel)); code != 400 { + t.Errorf("DELETE %q: status=%d, want 400", rel, code) + } + } + for _, p := range kept { + if _, err := os.Stat(filepath.Join(dir, p)); err != nil { + t.Errorf("%s is gone after the refused deletes: %v", p, err) + } + } + + // The blob store is denied like the CA and the index. + for _, rel := range []string{"traffic/_blobs", "traffic/_blobs/sha256/ab/abcd.bin", "TRAFFIC/_BLOBS/sha256"} { + for name, h := range map[string]http.HandlerFunc{"list": s.wsList, "read": s.wsRead, "download": s.wsDownload} { + if code := call(h, "GET", "/api/workspace/"+name+"?path="+url.QueryEscape(rel)); code != 400 { + t.Errorf("%s %q: status=%d, want 400", name, rel, code) + } + } + if code := call(s.wsDelete, "DELETE", "/api/workspace/delete?path="+url.QueryEscape(rel)); code != 400 { + t.Errorf("DELETE %q: status=%d, want 400", rel, code) + } + } + if _, err := os.Stat(filepath.Join(dir, "traffic/_blobs/sha256/ab/abcd.bin")); err != nil { + t.Errorf("the blob is gone: %v", err) + } + + // A symlink to the traffic directory is removed as a link; its target stays. + if err := os.Symlink(filepath.Join(dir, "traffic"), filepath.Join(dir, "alias")); err == nil { + if code := call(s.wsDelete, "DELETE", "/api/workspace/delete?path=alias"); code != 200 { + t.Errorf("DELETE of a symlink to traffic: status=%d, want 200", code) + } + if _, err := os.Lstat(filepath.Join(dir, "alias")); err == nil { + t.Error("the symlink was not removed") + } + if _, err := os.Stat(filepath.Join(dir, "traffic/_ca/mitmproxy-ca.pem")); err != nil { + t.Errorf("deleting the symlink removed the CA key: %v", err) + } + } + + // Ordinary trees, and the traffic of one host, are still deletable. + for _, rel := range []string{"traffic/host", "other"} { + if code := call(s.wsDelete, "DELETE", "/api/workspace/delete?path="+url.QueryEscape(rel)); code != 200 { + t.Errorf("DELETE %q: status=%d, want 200", rel, code) + } + if _, err := os.Stat(filepath.Join(dir, rel)); err == nil { + t.Errorf("%s still exists after its delete", rel) + } + } +}