| 1 | package workbench |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "net/url" |
| 6 | "os" |
| 7 | "os/exec" |
| 8 | "path/filepath" |
| 9 | "testing" |
| 10 | |
| 11 | "github.com/sylvinus/agent-vm/internal/mounts" |
| 12 | ) |
| 13 | |
| 14 | |
| 15 | |
| 16 | func TestWindowsJunctionCannotExposePrivateDirectory(t *testing.T) { |
| 17 | s := testServer(t) |
| 18 | parent := t.TempDir() |
| 19 | alias := filepath.Join(parent, "环境别名") |
| 20 | cmd := exec.Command("cmd.exe", "/d", "/v:off", "/c", "mklink", "/J", alias, s.profile) |
| 21 | if out, err := cmd.CombinedOutput(); err != nil { |
| 22 | t.Fatalf("create directory junction: %v, %s", err, out) |
| 23 | } |
| 24 | if err := os.Mkdir(filepath.Join(s.profile, "stored-data"), 0700); err != nil { |
| 25 | t.Fatal(err) |
| 26 | } |
| 27 | for _, dir := range []string{alias, filepath.Join(alias, "stored-data")} { |
| 28 | if _, err := validateWorkspace(dir, s.profile); err == nil { |
| 29 | t.Fatal("private alias accepted as a workbench project", dir) |
| 30 | } |
| 31 | if _, denied := mounts.UnsafeProject(dir, mounts.Refs("", "", s.profile)); !denied { |
| 32 | t.Fatal("private alias accepted as a CLI share", dir) |
| 33 | } |
| 34 | if w := request(s, "GET", "/api/projects/directories?path="+url.QueryEscape(dir), "", "", true); w.Code != 403 { |
| 35 | t.Fatal("private alias browsable", w.Code) |
| 36 | } |
| 37 | } |
| 38 | visible := filepath.Join(parent, "public") |
| 39 | if err := os.Mkdir(visible, 0700); err != nil { |
| 40 | t.Fatal(err) |
| 41 | } |
| 42 | w := request(s, "GET", "/api/projects/directories?path="+url.QueryEscape(parent), "", "", true) |
| 43 | var listing struct{ Directories []struct{ Name string } } |
| 44 | if w.Code != 200 || json.Unmarshal(w.Body.Bytes(), &listing) != nil || len(listing.Directories) != 1 || listing.Directories[0].Name != "public" { |
| 45 | t.Fatal("directory picker exposed the private alias or hid the safe sibling", w.Code, w.Body.String()) |
| 46 | } |
| 47 | } |