| 1 | package workbench |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "net/http" |
| 6 | "net/http/httptest" |
| 7 | "testing" |
| 8 | "time" |
| 9 | ) |
| 10 | |
| 11 | func TestTerminalLoginLinkUsesGuestPrivateBrowser(t *testing.T) { |
| 12 | const address = "https://claude.ai/oauth/authorize?state=fixture" |
| 13 | s := testServer(t) |
| 14 | s.ready = true |
| 15 | terminal := newTerminalSession(nil) |
| 16 | terminal.id = "1234567890abcdef" |
| 17 | s.terminals.sessions = map[string]*terminalSession{terminal.id: terminal} |
| 18 | finished := make(chan struct{}) |
| 19 | guest := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 20 | var in struct{ URL, Login string } |
| 21 | if err := json.NewDecoder(r.Body).Decode(&in); err != nil || in.Login != terminal.id { |
| 22 | t.Error("incorrect login request") |
| 23 | } |
| 24 | switch r.URL.Path { |
| 25 | case "/login-open": |
| 26 | if in.URL != address { |
| 27 | t.Error("lost authorization parameters") |
| 28 | } |
| 29 | _, _ = w.Write([]byte(`{"id":"tab","url":"private"}`)) |
| 30 | case "/login-finish": |
| 31 | if in.URL != "" { |
| 32 | t.Error("cleanup exposed URL") |
| 33 | } |
| 34 | _, _ = w.Write([]byte(`{"ok":true}`)) |
| 35 | close(finished) |
| 36 | default: |
| 37 | t.Error("unexpected browser route") |
| 38 | } |
| 39 | })) |
| 40 | defer guest.Close() |
| 41 | s.browserControl = localBrowserClient(t, s.lifetimeContext(), guest) |
| 42 | defer s.browserControl.close() |
| 43 | for _, invalid := range []string{"http://claude.ai/oauth/authorize?x=1", "https://claude.ai:90/oauth/authorize?x=1", "https://[email protected]/oauth/authorize?x=1", "https://claude.ai/redirect?x=1", "https://claude.ai/oauth/authorize"} { |
| 44 | if claudeAuthorizationURL(invalid) { |
| 45 | t.Fatalf("accepted %q", invalid) |
| 46 | } |
| 47 | } |
| 48 | w := request(s, "POST", "/api/terminals/"+terminal.id+"/login-link", s.origin, `{"url":"`+address+`"}`, true) |
| 49 | if w.Code != 200 || w.Body.String() != "{\"id\":\"tab\"}\n" { |
| 50 | t.Fatalf("%d %s", w.Code, w.Body.String()) |
| 51 | } |
| 52 | close(terminal.done) |
| 53 | select { |
| 54 | case <-finished: |
| 55 | case <-time.After(2 * time.Second): |
| 56 | t.Fatal("closing the terminal retained its login tab") |
| 57 | } |
| 58 | } |