package connect import ( "net/url" "strings" "testing" "amadan.net/oficina/equipo/internal/wire" ) // authorize is the shape CLI 2.1.321 printed on 2026-11-10 (values fake). 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" // Only Anthropic's authorize page on claude.com or claude.ai, over https, // redirecting to the code page, is a link the card may show. func TestAuthorizeURL(t *testing.T) { if got, ok := AuthorizeURL(authorize); !ok || got != authorize { t.Fatalf("refused the CLI's own link: %q %v", got, ok) } if _, ok := AuthorizeURL(strings.Replace(authorize, "claude.com", "claude.ai", 1)); !ok { t.Fatal("refused claude.ai") } if u, _ := url.Parse(authorize); u.Query().Get("redirect_uri") != wire.EndpointClaudeCode { t.Fatal("the code endpoint is not the link's redirect") } for name, bad := range map[string]string{ "http": strings.Replace(authorize, "https://", "http://", 1), "other host": strings.Replace(authorize, "claude.com", "claude.com.example.net", 1), "subdomain": strings.Replace(authorize, "claude.com", "evil.claude.com", 1), "user info": strings.Replace(authorize, "https://", "https://x@", 1), "port": strings.Replace(authorize, "claude.com", "claude.com:8443", 1), "loopback": strings.Replace(authorize, "https%3A%2F%2Fplatform.claude.com%2Foauth%2Fcode%2Fcallback", "http%3A%2F%2Flocalhost%3A1234%2Fcallback", 1), "other page": strings.Replace(authorize, "/oauth/authorize", "/oauth/elsewhere", 1), "token flow": strings.Replace(authorize, "response_type=code", "response_type=token", 1), "no state": strings.Replace(authorize, "&state=s1", "", 1), "fragment": authorize + "#x", "not a url": "Paste code here", "javascript": "javascript:alert(1)//claude.com/oauth/authorize", "redirect twin": authorize + "&redirect_uri=https%3A%2F%2Fexample.net", } { if _, ok := AuthorizeURL(bad); ok { t.Errorf("%s: accepted %q", name, bad) } } } // The screen the CLI draws: colours, cursor moves, a spinner, the link as // an OSC 8 hyperlink (ended by ST or BEL), redrawn. The link is found; // a truncated visible copy beside a whole hyperlink target does not hide // it; a plain visible link is found when there is no hyperlink. func TestAuthorizeThroughEscapes(t *testing.T) { 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" for _, f := range []string{"⠋", "⠙", "⠹"} { screen += "\r\x1b[2K\x1b[36m" + f + "\x1b[39m" } 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 " raw := screen + draw + "\x1b[3A\r\x1b[J" + strings.Replace(draw, "\x1b\\\x1b[34m", "\x07\x1b[34m", 1) if got, ok := authorizeIn([]byte(raw)); !ok || got != authorize { t.Fatalf("hyperlink: %q %v", got, ok) } text, links := visible([]byte(raw)) if strings.Contains(string(text), "\x1b") || len(links) != 2 || !strings.Contains(string(text), "Paste code here if prompted >") { t.Fatalf("visible %q links %q", text, links) } plain := screen + "\r\nhttps://claude.ai/x \x1b[34m" + authorize + "\x1b[39m\r\n" if got, ok := authorizeIn([]byte(plain)); !ok || got != authorize { t.Fatalf("plain: %q %v", got, ok) } // Not yet whole: an OSC cut off mid-way finds nothing. if _, ok := authorizeIn([]byte(screen + "\x1b]8;;" + authorize[:40])); ok { t.Fatal("found a link in a cut-off sequence") } } // A token counts once something follows it (or the CLI exited); a failure // on the prompt's own line (the echoed code) does not count. func TestTokenAndFailure(t *testing.T) { tok := "sk-ant-oat01-abc_DEF-123" if tokenIn([]byte("token:\n"+tok), false) != nil { t.Fatal("took a token that may go on") } if got := tokenIn([]byte("token:\n"+tok), true); string(got) != tok { t.Fatalf("at exit: %q", got) } if got := tokenIn([]byte("x "+tok+"\n"), false); string(got) != tok { t.Fatalf("whole: %q", got) } if failedAt([]byte("Paste code here if prompted > error-in-code\n")) != -1 { t.Fatal("the echoed prompt line counted as a failure") } if at := failedAt([]byte("Exchanging…\nOAuth error: Invalid code\n")); at != len("Exchanging…\nOAuth ") { t.Fatalf("failure at %d", at) } } // Output read in pieces: a link cut anywhere is not a link until its end // is there (the OSC 8 terminator, or for plain text a space or line end // after it). Every cut of the hyperlinked screen and of a plain one // finds nothing or the whole link, never a prefix. func TestAuthorizeNeverTruncated(t *testing.T) { osc := "Use the url below to sign in\r\n\x1b]8;;" + authorize + "\x1b\\" + authorize + "\x1b]8;;\x1b\\\r\nPaste code here if prompted > " plain := "Use the url below to sign in\r\n" + authorize + "\r\nPaste code here if prompted > " for name, screen := range map[string]string{"hyperlink": osc, "plain": plain} { whole := false for cut := 0; cut <= len(screen); cut++ { got, ok := authorizeIn([]byte(screen[:cut])) if ok && got != authorize { t.Fatalf("%s cut at %d: took %q", name, cut, got) } whole = whole || ok } if !whole { t.Fatalf("%s: never found", name) } } }