| 1 | package workbench |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "net/http" |
| 6 | "net/http/httptest" |
| 7 | "net/url" |
| 8 | "os" |
| 9 | "path/filepath" |
| 10 | "strings" |
| 11 | "testing" |
| 12 | ) |
| 13 | |
| 14 | func TestRemoteHostManagementKeepsLocalSessionAndOriginBoundary(t *testing.T) { |
| 15 | s := testServer(t) |
| 16 | s.remote = newRemoteGateway(s.profile) |
| 17 | defer s.remote.manager.Close() |
| 18 | s.account.lock() |
| 19 | if got := request(s, "GET", "/api/client/hosts", "", "", false).Code; got != 401 { |
| 20 | t.Fatalf("unauthenticated host list: %d", got) |
| 21 | } |
| 22 | if got := request(s, "GET", "/api/client/context", "", "", true).Code; got != 200 { |
| 23 | t.Fatalf("locked local account prevents connection management: %d", got) |
| 24 | } |
| 25 | if got := request(s, "POST", "/api/client/disconnect", "https://untrusted.example", "{}", true).Code; got != 403 { |
| 26 | t.Fatalf("cross-origin connection mutation: %d", got) |
| 27 | } |
| 28 | s.remote.setRemote(true) |
| 29 | if got := request(s, "POST", "/api/start", s.origin, "{}", true).Code; got != 409 { |
| 30 | t.Fatalf("disconnected remote fell back to local start: %d", got) |
| 31 | } |
| 32 | if job := s.jobs.snapshot(); job.ID != "" { |
| 33 | t.Fatal("disconnected remote created a local job") |
| 34 | } |
| 35 | s.remote.setRemote(false) |
| 36 | r := httptest.NewRequest("POST", s.origin+"/api/stop", strings.NewReader("{}")) |
| 37 | r.Header.Set("Origin", s.origin) |
| 38 | r.Header.Set("Content-Type", "application/json") |
| 39 | r.Header.Set(connectionHeader, "previous-remote-connection") |
| 40 | r.AddCookie(&http.Cookie{Name: s.cookieName(), Value: s.session}) |
| 41 | w := httptest.NewRecorder() |
| 42 | s.handler().ServeHTTP(w, r) |
| 43 | if w.Code != 409 { |
| 44 | t.Fatalf("old remote request reached local runtime: %d", w.Code) |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | func TestRemoteIdentityPersistsAndCorruptionDoesNotReplaceIt(t *testing.T) { |
| 49 | profile := t.TempDir() |
| 50 | first, err := ensureRemoteInstance(profile) |
| 51 | if err != nil { |
| 52 | t.Fatal(err) |
| 53 | } |
| 54 | again, err := ensureRemoteInstance(profile) |
| 55 | if err != nil || first != again { |
| 56 | t.Fatal("identity changed on reconnect", err) |
| 57 | } |
| 58 | other, err := ensureRemoteInstance(t.TempDir()) |
| 59 | if err != nil || first == other { |
| 60 | t.Fatal("different instances share an identity", err) |
| 61 | } |
| 62 | path := filepath.Join(profile, "remote-instance.json") |
| 63 | corrupt := []byte(`{"id":"invalid"}`) |
| 64 | if err := os.WriteFile(path, corrupt, 0600); err != nil { |
| 65 | t.Fatal(err) |
| 66 | } |
| 67 | if _, err := ensureRemoteInstance(profile); err == nil { |
| 68 | t.Fatal("corrupt identity was accepted") |
| 69 | } |
| 70 | retained, _ := os.ReadFile(path) |
| 71 | if string(retained) != string(corrupt) { |
| 72 | t.Fatal("corrupt identity was silently replaced") |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | func TestRemoteDirectoryPickerHandlesEmptyProjectsAndBlocksPrivateSymlinks(t *testing.T) { |
| 77 | s := testServer(t) |
| 78 | empty := t.TempDir() |
| 79 | w := request(s, "GET", "/api/projects/directories?path="+url.QueryEscape(empty), "", "", true) |
| 80 | if w.Code != 200 { |
| 81 | t.Fatal(w.Code, w.Body.String()) |
| 82 | } |
| 83 | var result struct { |
| 84 | Directories []any `json:"directories"` |
| 85 | Selectable bool `json:"selectable"` |
| 86 | } |
| 87 | if json.Unmarshal(w.Body.Bytes(), &result) != nil || len(result.Directories) != 0 || !result.Selectable { |
| 88 | t.Fatal("empty project unavailable", w.Body.String()) |
| 89 | } |
| 90 | link := filepath.Join(t.TempDir(), "private-link") |
| 91 | paths := []string{s.profile} |
| 92 | if err := os.Symlink(s.profile, link); err == nil { |
| 93 | paths = append(paths, link) |
| 94 | } |
| 95 | for _, path := range paths { |
| 96 | w := request(s, "GET", "/api/projects/directories?path="+url.QueryEscape(path), "", "", true) |
| 97 | if w.Code != 403 { |
| 98 | t.Fatalf("private directory was listed: %d", w.Code) |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | func TestRemoteDownloadsAreScopedToPersistentInstance(t *testing.T) { |
| 104 | profile := t.TempDir() |
| 105 | first := ClientContext{Mode: "remote", InstanceID: strings.Repeat("a", 64)} |
| 106 | second := ClientContext{Mode: "remote", InstanceID: strings.Repeat("b", 64)} |
| 107 | one, err := clientDownloadProfile(profile, first) |
| 108 | if err != nil { |
| 109 | t.Fatal(err) |
| 110 | } |
| 111 | two, err := clientDownloadProfile(profile, second) |
| 112 | if err != nil { |
| 113 | t.Fatal(err) |
| 114 | } |
| 115 | saved := SavedDownload{ID: testBrowserFileID, InstanceID: first.InstanceID, Name: "project.zip", Path: filepath.Join(t.TempDir(), "project.zip")} |
| 116 | if err := rememberDownload(one, saved, false); err != nil { |
| 117 | t.Fatal(err) |
| 118 | } |
| 119 | if _, err := readSavedDownload(two, saved.ID); !os.IsNotExist(err) { |
| 120 | t.Fatal("another host inherited the local file", err) |
| 121 | } |
| 122 | if _, err := clientDownloadProfile(profile, ClientContext{Mode: "remote", InstanceID: "../../private"}); err == nil { |
| 123 | t.Fatal("invalid instance path accepted") |
| 124 | } |
| 125 | } |