| 1 | package connect |
| 2 | |
| 3 | import ( |
| 4 | "net/url" |
| 5 | "strings" |
| 6 | "testing" |
| 7 | |
| 8 | "amadan.net/oficina/equipo/internal/wire" |
| 9 | ) |
| 10 | |
| 11 | // authorize is the shape CLI 2.1.321 printed on 2026-11-10 (values fake). |
| 12 | const authorize = "https://claude.com/cai/oauth/authorize?code=true&client_id=9d1c250a-fake&response_type=code&redirect_uri=https%3A%2F%2Fplatform.claude.com%2Foauth%2Fcode%2Fcallback&scope=user%3Ainference&code_challenge=c&code_challenge_method=S256&state=s1" |
| 13 | |
| 14 | // Only Anthropic's authorize page on claude.com or claude.ai, over https, |
| 15 | // redirecting to the code page, is a link the card may show. |
| 16 | func TestAuthorizeURL(t *testing.T) { |
| 17 | if got, ok := AuthorizeURL(authorize); !ok || got != authorize { |
| 18 | t.Fatalf("refused the CLI's own link: %q %v", got, ok) |
| 19 | } |
| 20 | if _, ok := AuthorizeURL(strings.Replace(authorize, "claude.com", "claude.ai", 1)); !ok { |
| 21 | t.Fatal("refused claude.ai") |
| 22 | } |
| 23 | if u, _ := url.Parse(authorize); u.Query().Get("redirect_uri") != wire.EndpointClaudeCode { |
| 24 | t.Fatal("the code endpoint is not the link's redirect") |
| 25 | } |
| 26 | for name, bad := range map[string]string{ |
| 27 | "http": strings.Replace(authorize, "https://", "http://", 1), |
| 28 | "other host": strings.Replace(authorize, "claude.com", "claude.com.example.net", 1), |
| 29 | "subdomain": strings.Replace(authorize, "claude.com", "evil.claude.com", 1), |
| 30 | "user info": strings.Replace(authorize, "https://", "https://x@", 1), |
| 31 | "port": strings.Replace(authorize, "claude.com", "claude.com:8443", 1), |
| 32 | "loopback": strings.Replace(authorize, "https%3A%2F%2Fplatform.claude.com%2Foauth%2Fcode%2Fcallback", "http%3A%2F%2Flocalhost%3A1234%2Fcallback", 1), |
| 33 | "other page": strings.Replace(authorize, "/oauth/authorize", "/oauth/elsewhere", 1), |
| 34 | "token flow": strings.Replace(authorize, "response_type=code", "response_type=token", 1), |
| 35 | "no state": strings.Replace(authorize, "&state=s1", "", 1), |
| 36 | "fragment": authorize + "#x", |
| 37 | "not a url": "Paste code here", |
| 38 | "javascript": "javascript:alert(1)//claude.com/oauth/authorize", |
| 39 | "redirect twin": authorize + "&redirect_uri=https%3A%2F%2Fexample.net", |
| 40 | } { |
| 41 | if _, ok := AuthorizeURL(bad); ok { |
| 42 | t.Errorf("%s: accepted %q", name, bad) |
| 43 | } |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | // The screen the CLI draws: colours, cursor moves, a spinner, the link as |
| 48 | // an OSC 8 hyperlink (ended by ST or BEL), redrawn. The link is found; |
| 49 | // a truncated visible copy beside a whole hyperlink target does not hide |
| 50 | // it; a plain visible link is found when there is no hyperlink. |
| 51 | func TestAuthorizeThroughEscapes(t *testing.T) { |
| 52 | screen := "\x1b[1mWelcome to Claude Code\x1b[22m\r\n\r\nThis will guide you through long-lived (1-year) auth token setup.\r\nOpening browser to sign in…\r\n" |
| 53 | for _, f := range []string{"⠋", "⠙", "⠹"} { |
| 54 | screen += "\r\x1b[2K\x1b[36m" + f + "\x1b[39m" |
| 55 | } |
| 56 | draw := "\r\x1b[2KBrowser didn't open? Use the url below to sign in \x1b[2m(c to copy)\x1b[22m\r\n\x1b]8;id=1;" + authorize + "\x1b\\\x1b[34m" + authorize[:60] + "…\x1b[39m\x1b]8;;\x1b\\\r\n\x1b[1mPaste code here if prompted >\x1b[22m " |
| 57 | raw := screen + draw + "\x1b[3A\r\x1b[J" + strings.Replace(draw, "\x1b\\\x1b[34m", "\x07\x1b[34m", 1) |
| 58 | if got, ok := authorizeIn([]byte(raw)); !ok || got != authorize { |
| 59 | t.Fatalf("hyperlink: %q %v", got, ok) |
| 60 | } |
| 61 | text, links := visible([]byte(raw)) |
| 62 | if strings.Contains(string(text), "\x1b") || len(links) != 2 || !strings.Contains(string(text), "Paste code here if prompted >") { |
| 63 | t.Fatalf("visible %q links %q", text, links) |
| 64 | } |
| 65 | plain := screen + "\r\nhttps://claude.ai/x \x1b[34m" + authorize + "\x1b[39m\r\n" |
| 66 | if got, ok := authorizeIn([]byte(plain)); !ok || got != authorize { |
| 67 | t.Fatalf("plain: %q %v", got, ok) |
| 68 | } |
| 69 | // Not yet whole: an OSC cut off mid-way finds nothing. |
| 70 | if _, ok := authorizeIn([]byte(screen + "\x1b]8;;" + authorize[:40])); ok { |
| 71 | t.Fatal("found a link in a cut-off sequence") |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | // A token counts once something follows it (or the CLI exited); a failure |
| 76 | // on the prompt's own line (the echoed code) does not count. |
| 77 | func TestTokenAndFailure(t *testing.T) { |
| 78 | tok := "sk-ant-oat01-abc_DEF-123" |
| 79 | if tokenIn([]byte("token:\n"+tok), false) != nil { |
| 80 | t.Fatal("took a token that may go on") |
| 81 | } |
| 82 | if got := tokenIn([]byte("token:\n"+tok), true); string(got) != tok { |
| 83 | t.Fatalf("at exit: %q", got) |
| 84 | } |
| 85 | if got := tokenIn([]byte("x "+tok+"\n"), false); string(got) != tok { |
| 86 | t.Fatalf("whole: %q", got) |
| 87 | } |
| 88 | if failedAt([]byte("Paste code here if prompted > error-in-code\n")) != -1 { |
| 89 | t.Fatal("the echoed prompt line counted as a failure") |
| 90 | } |
| 91 | if at := failedAt([]byte("Exchanging…\nOAuth error: Invalid code\n")); at != len("Exchanging…\nOAuth ") { |
| 92 | t.Fatalf("failure at %d", at) |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | // Output read in pieces: a link cut anywhere is not a link until its end |
| 97 | // is there (the OSC 8 terminator, or for plain text a space or line end |
| 98 | // after it). Every cut of the hyperlinked screen and of a plain one |
| 99 | // finds nothing or the whole link, never a prefix. |
| 100 | func TestAuthorizeNeverTruncated(t *testing.T) { |
| 101 | osc := "Use the url below to sign in\r\n\x1b]8;;" + authorize + "\x1b\\" + authorize + "\x1b]8;;\x1b\\\r\nPaste code here if prompted > " |
| 102 | plain := "Use the url below to sign in\r\n" + authorize + "\r\nPaste code here if prompted > " |
| 103 | for name, screen := range map[string]string{"hyperlink": osc, "plain": plain} { |
| 104 | whole := false |
| 105 | for cut := 0; cut <= len(screen); cut++ { |
| 106 | got, ok := authorizeIn([]byte(screen[:cut])) |
| 107 | if ok && got != authorize { |
| 108 | t.Fatalf("%s cut at %d: took %q", name, cut, got) |
| 109 | } |
| 110 | whole = whole || ok |
| 111 | } |
| 112 | if !whole { |
| 113 | t.Fatalf("%s: never found", name) |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | |