carlos / platform Public

console: the email step-up doors are claim doors, like the passkey callback

POST /auth/email/stepup/code and POST /auth/email/link replay the held
form through the router, so both are classified as doors driving a
held control-plane clear to completion (foreign-exclusive and open
runs), the shape of GET /auth/stepup/callback. Their nonApp probes go.
The step-up tests check their COUNT queries; the one-email CLI test
pins the count at approval, before the issuance notice.
Co-Authored-By: Claude <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev 4249e7877aef599b112cf7afdf1a6e3ecb4a81e9 parent 9bf42ae
3 files changed, +108 −100
  • internal/console/claim_invariant_fixtures_test.go +74 −5
  • internal/console/claim_invariant_probes_nonapp_test.go +6 −83
  • internal/console/emailstepup_test.go +28 −12

CI failed — run details

diff --git a/internal/console/claim_invariant_fixtures_test.go b/internal/console/claim_invariant_fixtures_test.go
index 474b09e68..6553bfad9 100644
--- a/internal/console/claim_invariant_fixtures_test.go
+++ b/internal/console/claim_invariant_fixtures_test.go
@@ -150,6 +150,60 @@ func claimLegacyAppTokenRevokeDoor(path string) *claimDoor {
}
}
+// claimEmailStepUpDoor is the passkey step-up callback's door for a session
+// that signed in by email: the held action is the control-plane clear, which
+// writes titogo's row, and finish completes the step-up through the entry
+// under test with the emailed code or link token, returning the final status.
+// Each run is a fresh email session with a fresh step-up send budget: a
+// completed step-up stays fresh for StepUpWindow, and the per-address send
+// limit would otherwise answer the second run, so this door must be the
+// emailed step-up every time.
+func claimEmailStepUpDoor(finish func(t *testing.T, cx *claimCase, c *http.Client, code, link string) int) *claimDoor {
+ return &claimDoor{
+ apps: claimRefs("bab/titogo"),
+ setup: func(t *testing.T, cx *claimCase) {
+ cx.mustForm(t, "/a/bab/apps/titogo/control-plane/set")
+ cx.srv.consoleMail = &fakeConsoleMail{ready: true}
+ cx.srv.challengeSent = make(chan struct{}, 4)
+ },
+ do: func(t *testing.T, cx *claimCase) int {
+ raw, err := cx.st.CreateSessionMethod(claimFxOwner, "", MethodEmail, time.Now())
+ if err != nil {
+ t.Fatal(err)
+ }
+ c := claimBrowserWithCookie(t, cx, sessionCookie, raw)
+ cx.srv.limits.mu.Lock()
+ cx.srv.limits.stepUps = keyedWindow{}
+ cx.srv.limits.mu.Unlock()
+ m := cx.rec.mark()
+ status, _, body := postForm(t, c, cx.ts.URL+"/a/bab/apps/titogo/control-plane/clear", url.Values{"csrf": {raw}})
+ if status != http.StatusOK || !strings.Contains(body, `action="/auth/email/stepup/code"`) {
+ t.Fatalf("control-plane clear did not ask for an emailed confirmation: %d:\n%s", status, body)
+ }
+ for _, e := range cx.rec.since(m) {
+ if !e.Control && (e.Unscoped || len(e.Scopes) != 0) {
+ t.Fatalf("the held form wrote %s before the emailed step-up", e)
+ }
+ }
+ select {
+ case <-cx.srv.challengeSent:
+ case <-time.After(10 * time.Second):
+ t.Fatal("the step-up email was never sent")
+ }
+ sent := cx.srv.consoleMail.(*fakeConsoleMail).sentCopy()
+ if len(sent) == 0 {
+ t.Fatal("no step-up email reached the console mailer")
+ }
+ _, msg, _ := strings.Cut(sent[len(sent)-1], "|")
+ cm, lm := stepUpCodeRe.FindStringSubmatch(msg), stepUpLinkRe.FindStringSubmatch(msg)
+ if cm == nil || lm == nil {
+ t.Fatalf("the step-up email has no code or link:\n%s", msg)
+ }
+ return finish(t, cx, c, cm[1], lm[1])
+ },
+ }
+}
+
// legacyAppTokenLive reads the staged token's row from the owner's listing.
func (cx *claimCase) legacyAppTokenLive(t *testing.T) bool {
t.Helper()
@@ -1216,11 +1270,26 @@ var claimFixtures = map[string]claimFixture{
// (TestPersonalSettingsStillRevokesALegacyAppToken), and that DELETE is a
// guarded cli_tokens write for the token's app. Person tokens (app '') are
// the same handler's other branch; service credentials are refused.
- "route POST /auth/cli/tokens/revoke": {door: claimLegacyAppTokenRevokeDoor("/auth/cli/tokens/revoke")},
- "route POST /auth/continue": claimNonApp("starts sign-in: email challenges"),
- "route POST /auth/email/code": claimNonApp("signs in by an emailed code: email challenges, user, session and step-up"),
- "route POST /auth/email/link": claimNonApp("signs in by an emailed link: email challenges, user, session and step-up; or re-confirms a session and resumes its held action through the router"),
- "route POST /auth/email/stepup/code": claimNonApp("re-confirms a session by an emailed code: email challenges and step-up, then resumes its held action through the router"),
+ "route POST /auth/cli/tokens/revoke": {door: claimLegacyAppTokenRevokeDoor("/auth/cli/tokens/revoke")},
+ "route POST /auth/continue": claimNonApp("starts sign-in: email challenges"),
+ "route POST /auth/email/code": claimNonApp("signs in by an emailed code: email challenges, user, session and step-up"),
+ // Not nonApp, for the reason the passkey step-up callback is a door: an
+ // email session's step-up (emailed link or code) replays the held form
+ // through the router, re-entering the ordinary handler, so an app
+ // mutation lands through these entries. The link's sign-in branch writes
+ // only challenges, user, session and step-up.
+ "route POST /auth/email/link": {door: claimEmailStepUpDoor(func(t *testing.T, cx *claimCase, c *http.Client, _, link string) int {
+ status, body, nonce := openLink(t, cx.consoleFixture, c, link)
+ if status != http.StatusOK || nonce == "" {
+ t.Fatalf("the step-up link's confirm page = %d:\n%s", status, body)
+ }
+ status, _, _ = postLink(t, cx.consoleFixture, c, link, nonce)
+ return status
+ })},
+ "route POST /auth/email/stepup/code": {door: claimEmailStepUpDoor(func(t *testing.T, cx *claimCase, c *http.Client, code, _ string) int {
+ status, _, _ := postForm(t, c, cx.ts.URL+"/auth/email/stepup/code", url.Values{"code": {code}})
+ return status
+ })},
"route POST /auth/signout": claimNonApp("ends the session"),
"route POST /fleet/store/{account}/{decision}": claimNonApp("operator entitlement decision for an account"),
"route POST /invite/{token}": claimNonApp("accepts an invitation: membership"),
diff --git a/internal/console/claim_invariant_probes_nonapp_test.go b/internal/console/claim_invariant_probes_nonapp_test.go
index 7362d5250..f8e30cf95 100644
--- a/internal/console/claim_invariant_probes_nonapp_test.go
+++ b/internal/console/claim_invariant_probes_nonapp_test.go
@@ -187,28 +187,6 @@ func claimEmailLinkPageProbe() claimProbe {
}
}
-func claimEmailLinkProbe() claimProbe {
- var c *http.Client
- return claimProbe{
- setup: claimEmailChallengeWorld,
- do: func(t *testing.T, cx *claimCase) claimReply {
- c = claimBrowserWithCookie(t, cx, linkConfirmCookie, "claim-probe-nonce")
- status, loc, body := postForm(t, c, cx.ts.URL+"/auth/email/link", url.Values{"t": {cx.vars["link"]}, "nonce": {"claim-probe-nonce"}})
- return cx.browserReply(c, status, loc, body)
- },
- status: http.StatusSeeOther, loc: "/a/",
- effect: func(t *testing.T, cx *claimCase, r claimReply) string {
- if !cx.signedInAs(c, claimFxOwner) {
- return "the emailed link opened no session"
- }
- if _, ok := cx.st.PeekEmailLink(cx.vars["link"], time.Now()); ok {
- return "the emailed link is still good after it was used"
- }
- return ""
- },
- }
-}
-
func claimEmailCodeProbe() claimProbe {
var c *http.Client
return claimProbe{
@@ -231,59 +209,6 @@ func claimEmailCodeProbe() claimProbe {
}
}
-// claimStepUpCodeProbe: an email session's held removal of kim, and the
-// step-up challenge emailed for it, its session, id and code kept in vars;
-// the right code resumes the removal.
-func claimStepUpCodeProbe() claimProbe {
- var c *http.Client
- return claimProbe{
- setup: func(t *testing.T, cx *claimCase) {
- claimMemberWorld(t, cx)
- now := time.Now()
- raw, err := cx.st.CreateSessionMethod(claimFxOwner, "", MethodEmail, now)
- if err != nil {
- t.Fatal(err)
- }
- pending, err := cx.st.StashPendingAction(raw, claimFxOwner, http.MethodPost, "/a/bab/members/remove",
- url.Values{"address": {"kim@keymail.dev"}}.Encode(), now)
- if err != nil {
- t.Fatal(err)
- }
- id, _, code, err := cx.st.CreateEmailChallenge(EmailChallenge{
- Purpose: ChallengeStepUp, Address: claimFxOwner, SessionHash: hashToken(raw),
- PendingHash: hashToken(pending), ExpiresAt: now.Add(stepUpFlowTTL),
- }, "", now)
- if err != nil {
- t.Fatal(err)
- }
- cx.vars["stepupsession"], cx.vars["challenge"], cx.vars["code"] = raw, id, code
- },
- do: func(t *testing.T, cx *claimCase) claimReply {
- c = claimBrowserWithCookie(t, cx, sessionCookie, cx.vars["stepupsession"])
- u, err := url.Parse(cx.ts.URL)
- if err != nil {
- t.Fatal(err)
- }
- c.Jar.SetCookies(u, []*http.Cookie{{Name: challengeCookie, Value: cx.vars["challenge"], Path: "/"}})
- status, loc, body := postForm(t, c, cx.ts.URL+"/auth/email/stepup/code", url.Values{"code": {cx.vars["code"]}})
- return cx.browserReply(c, status, loc, body)
- },
- status: http.StatusSeeOther, loc: "/a/bab/team",
- effect: func(t *testing.T, cx *claimCase, r claimReply) string {
- if role, _ := cx.st.Role(OpsAccountID, "kim@keymail.dev"); role != "" {
- return `the held removal did not resume: kim@keymail.dev still holds role "` + role + `"`
- }
- if !cx.st.FreshStepUp(cx.vars["stepupsession"], claimFxOwner, time.Now(), StepUpWindow) {
- return "the step-up code recorded no presence"
- }
- if _, ok := cx.st.PeekEmailChallenge(cx.vars["challenge"], time.Now()); ok {
- return "the step-up challenge is still standing after its code was used"
- }
- return ""
- },
- }
-}
-
func claimContinueProbe() claimProbe {
// An address Keymail's lookup does not know: the email door, which
// records a challenge and answers with the check page.
@@ -410,14 +335,12 @@ var claimNonAppProbes = map[string]claimProbe{
},
// ---- sign-in and session ----
- "route GET /auth/signin": claimSigninProbe(),
- "route GET /auth/callback": claimCallbackProbe(),
- "route GET /auth/confirm": claimConfirmProbe(),
- "route POST /auth/continue": claimContinueProbe(),
- "route GET /auth/email/link": claimEmailLinkPageProbe(),
- "route POST /auth/email/link": claimEmailLinkProbe(),
- "route POST /auth/email/code": claimEmailCodeProbe(),
- "route POST /auth/email/stepup/code": claimStepUpCodeProbe(),
+ "route GET /auth/signin": claimSigninProbe(),
+ "route GET /auth/callback": claimCallbackProbe(),
+ "route GET /auth/confirm": claimConfirmProbe(),
+ "route POST /auth/continue": claimContinueProbe(),
+ "route GET /auth/email/link": claimEmailLinkPageProbe(),
+ "route POST /auth/email/code": claimEmailCodeProbe(),
"route POST /auth/signout": {
do: claimFormReply("/auth/signout"),
status: http.StatusSeeOther, loc: "/",
diff --git a/internal/console/emailstepup_test.go b/internal/console/emailstepup_test.go
index 6d0024052..c115c61bb 100644
--- a/internal/console/emailstepup_test.go
+++ b/internal/console/emailstepup_test.go
@@ -82,6 +82,16 @@ func askStepUp(t *testing.T, fx *consoleFixture, m *fakeConsoleMail, c *http.Cli
return cm[1], lm[1]
}
+// pendingRows counts the held actions.
+func pendingRows(t *testing.T, fx *consoleFixture) int {
+ t.Helper()
+ var n int
+ if err := fx.st.db.QueryRow(`SELECT COUNT(*) FROM pending_actions`).Scan(&n); err != nil {
+ t.Fatal(err)
+ }
+ return n
+}
+
func postStepUpCode(t *testing.T, fx *consoleFixture, c *http.Client, code string) (int, string, string) {
t.Helper()
return postFrom(t, fx, c, "/auth/email/stepup/code", fx.ts.URL, url.Values{"code": {code}})
@@ -94,9 +104,11 @@ func TestEmailSessionStepUpByCode(t *testing.T) {
code, _ := askStepUp(t, fx, m, c, raw, base)
// The held action is parked server-side, without the session token.
- var stashed, leaked int
- fx.st.db.QueryRow(`SELECT COUNT(*) FROM pending_actions`).Scan(&stashed)
- fx.st.db.QueryRow(`SELECT COUNT(*) FROM pending_actions WHERE form LIKE ?`, "%"+raw+"%").Scan(&leaked)
+ stashed := pendingRows(t, fx)
+ var leaked int
+ if err := fx.st.db.QueryRow(`SELECT COUNT(*) FROM pending_actions WHERE form LIKE ?`, "%"+raw+"%").Scan(&leaked); err != nil {
+ t.Fatal(err)
+ }
if stashed != 1 || leaked != 0 {
t.Fatalf("pending actions: %d parked, %d holding the session token", stashed, leaked)
}
@@ -335,8 +347,7 @@ func TestEmailSessionStepUpFailsClosed(t *testing.T) {
if status != http.StatusServiceUnavailable || !strings.Contains(body, "Could not hold on to that action. Please try again.") {
t.Fatalf("step-up with the mailer down = %d:\n%s", status, body)
}
- var stashed int
- fx.st.db.QueryRow(`SELECT COUNT(*) FROM pending_actions`).Scan(&stashed)
+ stashed := pendingRows(t, fx)
if stashed != 0 || challengeRows(t, fx) != 0 || len(m.sentCopy()) != 1 || kimRole(t, fx) != RoleMember {
t.Fatalf("mailer down: %d stashed, %d challenges, %d emails", stashed, challengeRows(t, fx), len(m.sentCopy()))
}
@@ -351,8 +362,7 @@ func TestEmailSessionStepUpFailsClosed(t *testing.T) {
if status != http.StatusOK || !strings.Contains(body, esc(copyText("stepup.sent", "address", "bob@example.com"))) {
t.Fatalf("second ask inside the minute = %d:\n%s", status, body)
}
- var stashed int
- fx.st.db.QueryRow(`SELECT COUNT(*) FROM pending_actions`).Scan(&stashed)
+ stashed := pendingRows(t, fx)
if stashed != 1 || challengeRows(t, fx) != 1 || len(m.sentCopy()) != 2 {
t.Fatalf("second ask: %d stashed, %d challenges, %d emails", stashed, challengeRows(t, fx), len(m.sentCopy()))
}
@@ -423,6 +433,13 @@ func TestCLILoginByEmailUserOneEmail(t *testing.T) {
if status != http.StatusOK || !strings.Contains(body, "Approved") {
t.Fatalf("approve = %d -> %q, want approved at once:\n%s", status, loc, body)
}
+ // Approved with the sign-in email alone: no step-up challenge, and the
+ // mailer has seen exactly one message (the issuance notice the poll sends
+ // next is not a confirmation).
+ if sent := m.sentCopy(); len(sent) != 1 || challengeRows(t, fx) != 0 {
+ t.Fatalf("at approval the console mailer had %d messages and %d challenges stood, want only the sign-in email: %q",
+ len(sent), challengeRows(t, fx), sent)
+ }
_, poll := postJSON(t, fx.ts.URL+"/auth/cli/poll", map[string]string{"device_code": deviceCode}, "")
if str(t, poll, "status") != CLIAuthApproved || str(t, poll, "address") != addr || str(t, poll, "token") == "" {
t.Fatalf("poll after approval = %v", poll)
@@ -432,10 +449,9 @@ func TestCLILoginByEmailUserOneEmail(t *testing.T) {
case <-time.After(5 * time.Second):
t.Fatal("no issuance notice attempt")
}
- if sent := m.sentCopy(); len(sent) != 1 {
- t.Fatalf("console mailer got %d messages, want exactly the sign-in email: %q", len(sent), sent)
- }
- if challengeRows(t, fx) != 0 {
- t.Fatal("a step-up challenge was created")
+ for _, msg := range m.sentCopy() {
+ if stepUpCodeRe.MatchString(msg) {
+ t.Fatalf("a step-up email went out:\n%s", msg)
+ }
}
}