console: claim fresh sign-in only where Keymail advertises reauth
Opus review nits on dab9ffc8: check SupportsStepUp before asking for prompt=login (fallback is the ordinary sign-in and the claim's own step-up), document that the recorded presence is session-wide like email's, and pin a stale claim callback after an ordinary sign-in.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
internal/console/demo_claim_signin_test.go+38 −0internal/console/emailsignin.go+4 −0internal/console/server.go+19 −1
CI passed — run details
Intent
<task-notification> <task-id>aa7fc0f68b55c55b5</task-id> <tool-use-id>toolu_01VY34YCypYHHPMgGsDnwWw9</tool-use-id> <output-file>/home/paulca/.cache/tmp/claude-1001/-home-paulca-amadan-net-carlos-platform/22215d20-2b53-4378-8426-ae55e2362d13/tasks/aa7fc0f68b55c55b5.output</output-file> <status>completed</status> <summary>Agent "Opus security review one-signin" finished</summary> <note>A task-notification fires each time this agent stops with no live background children of its own. The user can send it another message and resume it, so the same task-id may notify more than once.</note> <result>**Verdict: APPROVE WITH NITS.** I found no way to get the ".f" marker or a step_ups row without a fresh Keymail gesture. The targeted tests pass (`TestDemoClaimSignin*`, `TestStepUp*`). I had to run them outside the sandbox because `~/.cache/go-build` is read-only inside it. **Why it holds** - **Forging or injecting the marker:** only `handleSignin` sets it (server.go:3941-3945), in a cookie that is HttpOnly, Secure and SameSite=Lax. The cookie's state must match the state Keymail echoes back, and that state was minted for the prompt=login authorize URL. If an ordinary sign-in starts later, it overwrites the cookie with a new state, so the marked ceremony's callback fails the state check. An injected cookie needs the victim's state, which never leaves their browser. Login CSRF (CSRF that signs the victim into an attacker-chosen account) only gives the attacker's own account a fresh row, which gains nothing. - **Swapping the next cookie or query:** freshness comes only from the auth cookie (server.go:3972-3974). The `ordinaryWithFreshAuthTime` test covers this, and a stray `&next=` on the callback changes nothing. - **Mixing codes between flows:** `ExchangeGrant(..., false)` replays `/auth/callback`, so a code minted for step-up is rejected. `handleStepUpCallback` behaves the same as before; it only moved its window check into `usableStepUpAuthTime` (stepup.go:153). `AuthorizeURL` and `StepUpURL` are unchanged. - **Other callers:** only `handleCallback` passes the new variadic argument, and `completeSigninSession` has one caller (emailsignin.go:266). An ordinary Keymail sign-in passes a zero time and records nothing. - **Timing:** the same window applies as for step-up: from 60 s ahead to 5 min old, measured from Keymail's `auth_time`. A missing `auth_time` records nothing. - **Row binding:** the row is keyed to the new session and the resolved person, which is what `FreshStepUp` compares. - **Tests:** they go through the real handlers. With the fix reverted, `fresh` fails because the select redirects to step-up, and the authorize test fails too. **Nits** 1. **No capability check before asking for prompt=login** (server.go:3943). The step-up path asks Keymail whether it supports re-authentication first; the claim sign-in sends prompt=login blind. Freshness still depends only on Keymail's promise to omit `auth_time` unless a gesture happened, so this is safe. But a Keymail that rejects unknown prompt values would break claim sign-in. Fix: add a comment saying the auth_time contract is the only guarantee, or fall back to an ordinary authorize URL when Keymail doesn't advertise support. 2. **The row is not limited to the claim** (emailsignin.go:324-329). For 5 minutes it satisfies every gated action: device approval, member removal, account deletion. Email sign-in already works this way, and a theft inside the window looks the same as one after a step-up. Fix: say so in the `finishSignin` comment. 3. **Mapped persons:** when a Keymail address is mapped to a different person, the row is recorded for the person, not `grant.Address`. That is correct, but it is the only way such a session can ever become fresh. The later Keymail step-up always fails the address match in stepup.go (an existing behaviour). Fix: note this in a comment, or open a follow-up issue. 4. **Missing test:** start a claim sign-in, then an ordinary one, then call back with the claim's state. It should be refused with no row recorded. The logic is sound; nothing pins it. I edited nothing, started no servers and created no scratch files.</result> <usage><subagent_tokens>71579</subagent_tokens><tool_uses>11</tool_uses><duration_ms>146555</duration_ms></usage> </task-notification>
Attributed to this commit: 23.8 g CO₂e (2,778,918 tokens). This is an attribution, not a measurement — usage counted from this commit's own marker forward to the next one on demo-claim-one-signin, never backward from it. A turn that produced more than one commit books all of its tokens to the last one, and a concurrent session on this branch can add to the same figure. Some of it was priced using the shared default rate, because no model-specific factor row exists yet for at least one model involved. The Carbon view has the repo total, which is never derived from this attribution.
diff --git a/internal/console/demo_claim_signin_test.go b/internal/console/demo_claim_signin_test.go| index 46bdd29ce..00ddb0de8 100644 |
| --- a/internal/console/demo_claim_signin_test.go |
| +++ b/internal/console/demo_claim_signin_test.go |
| @@ -263,3 +263,41 @@ func TestStepUpAuthTimeBoundaries(t *testing.T) { |
| }) |
| } |
| } |
| + |
| +// A Keymail that does not advertise re-authentication is never asked for a |
| +// fresh gesture: the claim gets the ordinary sign-in and records nothing. |
| +func TestDemoClaimSigninNoReauthFallsBack(t *testing.T) { |
| + s, fk, b := claimSigninFixture(t) |
| + fk.mu.Lock() |
| + fk.reauth = false |
| + fk.mu.Unlock() |
| + start := claimSigninStart(t, b) |
| + if parts := strings.Split(b.cookies[authCookie].Value, "."); len(parts) != 2 { |
| + t.Fatalf("auth cookie parts = %v, want the ordinary two", parts) |
| + } |
| + u, err := url.Parse(start.Header().Get("Location")) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if q := u.Query(); q.Has("prompt") || q.Has("max_age") { |
| + t.Fatalf("fresh gesture asked of a deployment without reauth: %s", u) |
| + } |
| + claimSigninCallback(t, b, fk, start, time.Now(), "") |
| + if b.session == "" || claimBrowserCount(t, s, "step_ups") != 0 { |
| + t.Fatalf("session=%q step_ups=%d", b.session, claimBrowserCount(t, s, "step_ups")) |
| + } |
| +} |
| + |
| +// A later ordinary sign-in replaces the claim's marked cookie; the claim |
| +// ceremony's callback then fails the state check and records nothing. |
| +func TestDemoClaimSigninOverwrittenByOrdinary(t *testing.T) { |
| + s, fk, b := claimSigninFixture(t) |
| + start := claimSigninStart(t, b) |
| + if w := b.request("GET", "/auth/signin", nil); w.Code != 303 { |
| + t.Fatalf("ordinary signin: %d", w.Code) |
| + } |
| + w := claimSigninCallback(t, b, fk, start, time.Now(), "") |
| + if w.Code != 400 || !strings.Contains(w.Body.String(), "Sign-in flow mismatch") || b.session != "" || claimBrowserCount(t, s, "step_ups") != 0 { |
| + t.Fatalf("stale claim callback: %d session=%q", w.Code, b.session) |
| + } |
| +} |
diff --git a/internal/console/emailsignin.go b/internal/console/emailsignin.go| index 2908e67f9..28450fe32 100644 |
| --- a/internal/console/emailsignin.go |
| +++ b/internal/console/emailsignin.go |
| @@ -258,6 +258,10 @@ func safeNext(raw string) string { |
| // before ordinary redirects. The exact claim return skips account landing; |
| // its explicit confirmation uses the claim person lock and account cap. |
| // freshAuthTime carries the claim callback's validated Keymail gesture, if any. |
| +// Like an email sign-in's, the presence it records is not scoped to the claim: |
| +// for StepUpWindow it satisfies every step-up gated action of this new session, |
| +// exactly as a step-up just after sign-in would. A Keymail address mapped to |
| +// another person records against that person, the session's own address. |
| func (s *Server) finishSignin(w http.ResponseWriter, r *http.Request, proved, keymailToken, method, next string, freshAuthTime ...time.Time) { |
| var authTime time.Time |
| if len(freshAuthTime) > 0 { |
diff --git a/internal/console/server.go b/internal/console/server.go| index e135588b3..b4c3fdb82 100644 |
| --- a/internal/console/server.go |
| +++ b/internal/console/server.go |
| @@ -3938,7 +3938,13 @@ func (s *Server) handleSignin(w http.ResponseWriter, r *http.Request) { |
| next := safeNext(r.URL.Query().Get("next")) |
| auth := state + "." + verifier |
| target := s.km.AuthorizeURL(state, ChallengeS256(verifier)) |
| - if next == "/demo/claim" { |
| + // The claim sign-in asks for a fresh gesture so the claim's own step-up |
| + // is satisfied by this one ceremony. Only a deployment that advertises |
| + // re-authentication is asked (as stepUpGate does); any other gets the |
| + // ordinary sign-in and the claim's step-up asks as before. The asking is |
| + // not the proof: handleCallback records presence only from a reported |
| + // auth_time inside the step-up window. |
| + if next == "/demo/claim" && s.claimSigninCanAskFresh(r) { |
| auth += ".f" |
| target = s.km.FreshSigninURL(state, ChallengeS256(verifier), StepUpWindow) |
| } |
| @@ -3953,6 +3959,18 @@ func (s *Server) handleSignin(w http.ResponseWriter, r *http.Request) { |
| http.Redirect(w, r, target, http.StatusSeeOther) |
| } |
| +// claimSigninCanAskFresh is the capability check before a fresh-gesture |
| +// sign-in. An error or a "no" falls back to the ordinary sign-in. |
| +func (s *Server) claimSigninCanAskFresh(r *http.Request) bool { |
| + ctx, cancel := context.WithTimeout(r.Context(), 10*time.Second) |
| + defer cancel() |
| + ok, err := s.km.SupportsStepUp(ctx) |
| + if err != nil { |
| + log.Printf("console: claim sign-in capability check: %v", err) |
| + } |
| + return err == nil && ok |
| +} |
| + |
| // handleCallback finishes the ceremony: verify state, exchange the code |
| // (with PKCE verifier and the console's sign_pub), and either sign the user |
| // up (new address) or in. The grant token is stored per user — it is the |