console: describe a typed CLI code like a link code; name the country
The POST continue path now fills the login's age and place through the same describeCLI helper as the link page, instead of reading "Started just now" with no location. The place line names the country with the subdivision code in brackets, e.g. "United Kingdom (GB-ENG)".
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
3 files changed,
+69
−12
internal/console/authcli.go+11 −4internal/console/authcli_test.go+44 −2internal/console/cliplace.go+14 −6
diff --git a/internal/console/authcli.go b/internal/console/authcli.go| index 896dc947e..ca99a6d91 100644 |
| --- a/internal/console/authcli.go |
| +++ b/internal/console/authcli.go |
| @@ -1663,13 +1663,20 @@ func (s *Server) handleCLIApprovePage(w http.ResponseWriter, r *http.Request) { |
| p.Error = copyText("cli.link_unknown") |
| break |
| } |
| - p.Request = req |
| - p.Place = s.cliPlaceFor(req.RemoteAddr) |
| - p.Started = now.Sub(req.CreatedAt) |
| + s.describeCLI(&p, req, now) |
| } |
| s.renderCLI(w, http.StatusOK, "cli_approve", p) |
| } |
| +// describeCLI fills everything the approval page says about a live login, |
| +// for both ways of reaching it (the link's GET and the typed code's POST |
| +// continue), so neither can describe a login less truthfully than the other. |
| +func (s *Server) describeCLI(p *cliPage, req *CLIAuthRequest, now time.Time) { |
| + p.Request = req |
| + p.Place = s.cliPlaceFor(req.RemoteAddr) |
| + p.Started = now.Sub(req.CreatedAt) |
| +} |
| + |
| // cliBudgetOpen reports whether neither key is over its guess budget. It is |
| // read-only (rateLimiter.tripped), so asking never spends: callers check it |
| // BEFORE resolving a code and spend only on a miss with cliBudgetSpend. |
| @@ -1772,7 +1779,7 @@ func (s *Server) handleCLIApprove(w http.ResponseWriter, r *http.Request) { |
| s.renderCLI(w, http.StatusOK, "cli_approve", p) |
| return |
| } |
| - p.Request = req |
| + s.describeCLI(&p, req, now) |
| s.renderCLI(w, http.StatusOK, "cli_approve", p) |
| } |
| } |
diff --git a/internal/console/authcli_test.go b/internal/console/authcli_test.go| index 12bd7935f..a281614c9 100644 |
| --- a/internal/console/authcli_test.go |
| +++ b/internal/console/authcli_test.go |
| @@ -376,7 +376,7 @@ func TestCLIPlaceAttribution(t *testing.T) { |
| t.Fatalf("empty reader = %+v, want nothing", got) |
| } |
| fx.srv.cfg.GeoIPReader = dbipReader(t) |
| - want := cliPlace{Text: "GB-ENG, GB", Attribution: "IP Geolocation by DB-IP", AttributionURL: "https://db-ip.com"} |
| + want := cliPlace{Text: "United Kingdom (GB-ENG)", Attribution: "IP Geolocation by DB-IP", AttributionURL: "https://db-ip.com"} |
| for _, addr := range []string{"81.2.69.142", "81.2.69.142:52114"} { |
| if got := fx.srv.cliPlaceFor(addr); got != want { |
| t.Fatalf("cliPlaceFor(%q) = %+v, want %+v", addr, got, want) |
| @@ -399,13 +399,55 @@ func TestCLIPlaceAttribution(t *testing.T) { |
| if status != http.StatusOK { |
| t.Fatalf("approval page = %d", status) |
| } |
| - for _, want := range []string{"81.2.69.142", "GB-ENG, GB", `<a href="https://db-ip.com">IP Geolocation by DB-IP</a>`} { |
| + for _, want := range []string{"81.2.69.142", "United Kingdom (GB-ENG)", `<a href="https://db-ip.com">IP Geolocation by DB-IP</a>`} { |
| if !strings.Contains(body, want) { |
| t.Fatalf("approval page missing %q:\n%s", want, body) |
| } |
| } |
| } |
| +// The place line names the country, with the subdivision code in brackets |
| +// when there is one, and falls back to the code for a country it cannot name. |
| +func TestCLIPlaceText(t *testing.T) { |
| + for _, tc := range []struct{ country, sub, want string }{ |
| + {"GB", "GB-ENG", "United Kingdom (GB-ENG)"}, |
| + {"GB", "", "United Kingdom"}, |
| + {"QQ", "", "QQ"}, |
| + {"QQ", "QQ-1", "QQ (QQ-1)"}, |
| + } { |
| + if got := cliPlaceText(tc.country, tc.sub); got != tc.want { |
| + t.Errorf("cliPlaceText(%q, %q) = %q, want %q", tc.country, tc.sub, got, tc.want) |
| + } |
| + } |
| +} |
| + |
| +// The typed-code path describes a login exactly as the link does: its age |
| +// and where it dialled from, not "just now" and no place. |
| +func TestCLITypedCodeDescribesAgeAndPlace(t *testing.T) { |
| + fx := newConsoleFixture(t) |
| + fx.srv.cfg.GeoIPReader = dbipReader(t) |
| + browser := newBrowser(t) |
| + signUp(t, fx, browser, "code-typed-age") |
| + userCode, _, err := fx.st.NewCLIAuth("typed@laptop", "81.2.69.142", time.Now().Add(-3*time.Minute)) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + status, _, body := postForm(t, browser, fx.ts.URL+"/auth/cli/approve", |
| + url.Values{"code": {userCode}, "action": {"continue"}, "csrf": {sessionToken(t, fx, browser)}}) |
| + if status != http.StatusOK || !strings.Contains(body, "typed@laptop") { |
| + t.Fatalf("continue = %d:\n%s", status, body) |
| + } |
| + if strings.Contains(body, esc(copyText("cli.started_now"))) { |
| + t.Fatalf("a three-minute-old login reads as just started:\n%s", body) |
| + } |
| + for _, want := range []string{esc(copyText("cli.started", "ago", "3m")), "United Kingdom (GB-ENG)", |
| + `<a href="https://db-ip.com">IP Geolocation by DB-IP</a>`} { |
| + if !strings.Contains(body, want) { |
| + t.Fatalf("continue page missing %q:\n%s", want, body) |
| + } |
| + } |
| +} |
| + |
| // Issuing a credential tells the human, in the inbox their identity is |
| // anchored to. Every other defence here is preventive; this is the one that |
| // makes a phished approval detectable afterwards. |
diff --git a/internal/console/cliplace.go b/internal/console/cliplace.go| index 57405d503..ee748fb0b 100644 |
| --- a/internal/console/cliplace.go |
| +++ b/internal/console/cliplace.go |
| @@ -2,8 +2,9 @@ package console |
| // The approval page's location line (agent login by link, spec §3): where a |
| // login dialled from, as the platform GeoIP answer gives it. The answer has |
| -// no city, so this is "Subdivision, Country" or "Country"; with no answer it |
| -// is empty and the page shows the address alone. |
| +// no city, so this is a country and perhaps a subdivision; with no answer it |
| +// is empty and the page shows the address alone. The country is shown by |
| +// name, the subdivision by its code in brackets (cliPlaceText). |
| import ( |
| "net" |
| @@ -37,12 +38,19 @@ func (s *Server) cliPlaceFor(remoteAddr string) cliPlace { |
| if a.State != geoip.StateHit || a.Country == "" { |
| return cliPlace{} |
| } |
| - p := cliPlace{Text: a.Country} |
| - if a.Subdivision != "" { |
| - p.Text = a.Subdivision + ", " + a.Country |
| - } |
| + p := cliPlace{Text: cliPlaceText(a.Country, a.Subdivision)} |
| if at, ok := geoip.AttributionFor(a.Source); ok { |
| p.Attribution, p.AttributionURL = at.Text, at.URL |
| } |
| return p |
| } |
| + |
| +// cliPlaceText names the country (countryName, falling back to the code it |
| +// cannot name) with the subdivision code in brackets when there is one: |
| +// "United Kingdom (GB-ENG)", or "United Kingdom". |
| +func cliPlaceText(country, subdivision string) string { |
| + if subdivision != "" { |
| + return countryName(country) + " (" + subdivision + ")" |
| + } |
| + return countryName(country) |
| +} |