oficina / home Public

Home: Keymail fallback form keeps force; gestureless Keymail is not fresh

The sign-in form rendered at /signin?force=1 now posts to
/signin?force=1 for every flow, not only step-ups. The browser submits
the form's action, so every failed Keymail return used to classify
again and go straight back to Keymail. TestCredentialReturnKeymailAndFallback
posted to /signin?force=1 directly, which hid it; it now submits the
form's own action, as the new tests do.

A session whose Method is exactly "keymail" and whose AuthTime is zero
no longer satisfies a step-up: sessions.Fresh falls back to At, which
is when Home minted it, not a gesture. Resume sends it to sign-in, where
the Keymail prompt=login or email rules apply. Second-factor sessions
set AuthTime themselves and are unaffected; magic-link sessions keep the
At fallback.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 1f18405296608e6d6f6659e608f635dd2c2e76cc parent 7132f95
5 files changed, +108 −19
  • implementation/fresh-assertion.md +3 −3
  • internal/home/exchange.go +12 −1
  • internal/home/keymail_fresh_test.go +82 −3
  • internal/home/signin.go +8 −11
  • internal/home/signin_return_test.go +3 −1

CI passed — run details

diff --git a/implementation/fresh-assertion.md b/implementation/fresh-assertion.md
index 1365de1..5478580 100644
--- a/implementation/fresh-assertion.md
+++ b/implementation/fresh-assertion.md
@@ -28,7 +28,7 @@ keymaildev/signin v0.1.1 sends prompt=login when the federation server's /.well-
Begin. A fresh-intent begin classifies as usual. If the result is a Keymail continuation whose authorize URL carries exactly prompt=login, Home starts Keymail and sends no mail. If it is a Keymail continuation without prompt=login (a server that does not advertise reauth), Home discards it unsent and calls Begin again with the magic link forced: today's behaviour. A non-Keymail address gets the magic link, as before. The check is on the URL actually handed to the browser rather than on a separate classification, so it tests the fact that matters instead of predicting it, and classification still runs after the per-IP budget. The cost is a second address-budget token on the no-reauth path only.
-Callback. During a fresh flow (a valid sealed intent with MaxAge set) the Keymail return is credited only if AuthTime is non-zero, no later than now, and now - AuthTime <= MaxAge (300 s). Otherwise no session is minted and the browser goes to /signin?force=1, the existing Keymail fallback, with the intent and the /authorize/resume navigation cookie untouched. Under a fresh intent the form rendered at /signin?force=1 posts to /signin?force=1, because the browser submits the form's action and not the URL that rendered it, and without that the fallback would classify again and loop back to Keymail. The flag is convenience, not security: dropping it buys another Keymail round trip held to the same check. The ordinary form is unchanged.
+Callback. During a fresh flow (a valid sealed intent with MaxAge set) the Keymail return is credited only if AuthTime is non-zero, no later than now, and now - AuthTime <= MaxAge (300 s). Otherwise no session is minted and the browser goes to /signin?force=1, the existing Keymail fallback, with the intent and the /authorize/resume navigation cookie untouched. The form rendered at /signin?force=1 posts to /signin?force=1, because the browser submits the form's action and not the URL that rendered it. Before this, every Keymail fallback, ordinary ones included, classified again on submit and went straight back to Keymail; the existing tests posted to /signin?force=1 directly and hid it. The flag is convenience, not security: dropping it buys another Keymail round trip held to the same checks. The plain /signin form still posts to /signin.
This check cannot be left to resume. Resume reads a zero AuthTime as the session's At, which is the callback's own clock, so a standing Keymail approval with no reported gesture would pass resume's freshness test. Resume's existing checks (sessions.Fresh, verified <= now, subject binding) still run afterwards, unchanged.
@@ -36,6 +36,6 @@ No clock-skew allowance. A future AuthTime is refused outright. rastrillo's asse
Unchanged: ordinary SSO and ordinary Keymail sign-in, including whatever prompt the library chooses there, with no gesture check on the return; subject binding (a different person's fresh Keymail gesture still gets 400 at resume); the passkey and recovery gates, which run in completeSignIn on both doors; and every sealed-intent bound. AuthTime is never advanced: the assertion carries Keymail's gesture time, tested to the second.
-Acceptance (keymail_fresh_test.go, against the existing keymail stub with "reauth" added): fresh intent plus a reauth server gives an authorize URL with exactly prompt=login and no mail; a gesture 90 s old yields an assertion whose auth_time equals Keymail's; zero, 301 s, one hour, +5 s and +1 h AuthTime give no session and fall back to email, and that email link completes the step-up through the form a browser would submit; a server without reauth gets forced mail; a non-Keymail address gets mail; ordinary Keymail with zero AuthTime still signs in; subject switching through Keymail is refused. Mutation: replacing the window with true fails all five fallback cases, as do removing the future bound or the age bound alone; dropping the prompt=login requirement fails the no-reauth tests; dropping the form's override fails the fallback test. Removing only the zero check survives, because a zero time is also more than 300 s old; the explicit check stays as documentation of the case Keymail defines.
+Acceptance (keymail_fresh_test.go, against the existing keymail stub with "reauth" added): fresh intent plus a reauth server gives an authorize URL with exactly prompt=login and no mail; a gesture 90 s old yields an assertion whose auth_time equals Keymail's; zero, 301 s, one hour, +5 s and +1 h AuthTime give no session and fall back to email, and that email link completes the step-up through the form a browser would submit; a server without reauth gets forced mail; a non-Keymail address gets mail; ordinary Keymail with zero AuthTime still signs in; subject switching through Keymail is refused. Mutation: replacing the window with true fails all five fallback cases, as do removing the future bound or the age bound alone; dropping the prompt=login requirement fails the no-reauth tests; dropping the form's override fails the fresh fallback, the ordinary fallback and TestCredentialReturnKeymailAndFallback. Removing only the zero check survives, because a zero time is also more than 300 s old; the explicit check stays as documentation of the case Keymail defines.
-Residual, not changed here: outside a fresh flow, a Keymail session with zero AuthTime still has its At used as its verified time, so if one is minted by an ordinary sign-in and a fresh intent arrives within five minutes, resume credits it without mail ("fresh session succeeds without unnecessary mail"). That predates this change and is the standing-session gap in a different door.
+A Keymail session with no gesture time never satisfies a step-up. Resume refuses a session whose Method is exactly "keymail" and whose AuthTime is zero, even inside the window: sessions.Fresh would fall back to At, which is when Home minted the session, not anything the person did. The step-up then goes to sign-in, where the rules above apply (Keymail with prompt=login, or email). A completed second factor ("keymail+passkey", "keymail+recovery") sets AuthTime itself and is unaffected; magic-link sessions keep the At fallback, because the click is the gesture. Tested with a control (the same flow with a reported gesture mints) and mutation-checked: removing the guard fails the no-gesture case.
diff --git a/internal/home/exchange.go b/internal/home/exchange.go
index bf87bb1..32b23a3 100644
--- a/internal/home/exchange.go
+++ b/internal/home/exchange.go
@@ -10,6 +10,7 @@ import (
"amadan.net/rastrillo/rastrillo/assertion"
"amadan.net/rastrillo/rastrillo/sessions"
json "github.com/go-json-experiment/json"
+ "github.com/keymaildev/signin"
)
const assertionIntentCookie = "__Host-oficina-assertion-intent"
@@ -92,7 +93,7 @@ func (a *App) handleAssertion(w http.ResponseWriter, r *http.Request) {
a.failAssertion(w, r, assertionSubject)
return
}
- if intent.MaxAge != 0 && (!sessions.Fresh(session, time.Duration(intent.MaxAge)*time.Second, now) || authTime.After(now)) {
+ if intent.MaxAge != 0 && (!sessions.Fresh(session, time.Duration(intent.MaxAge)*time.Second, now) || authTime.After(now) || keymailWithoutGesture(session)) {
http.Redirect(w, r, "/signin?return_to=%2Fauthorize%2Fresume", http.StatusSeeOther)
return
}
@@ -109,3 +110,13 @@ func (a *App) handleAssertion(w http.ResponseWriter, r *http.Request) {
http.SetCookie(w, &http.Cookie{Name: assertionIntentCookie, Value: "", Path: "/", MaxAge: -1, Secure: true, HttpOnly: true, SameSite: http.SameSiteLaxMode})
a.render(w, r, http.StatusOK, "assertion.html", map[string]any{"Action": target + handoff.CallbackPath, "Token": token})
}
+
+// keymailWithoutGesture reports a session whose only factor is a Keymail
+// approval that reported no gesture time. sessions.Fresh falls back to
+// At for such a session, and At is when Home minted it, not when the
+// person did anything: a standing Keymail inbox session would pass a
+// step-up for being new. A second factor (Method "keymail+passkey",
+// "keymail+recovery") sets AuthTime itself, so it is not caught here.
+func keymailWithoutGesture(session sessions.Session) bool {
+ return session.Method == string(signin.MethodKeymail) && session.AuthTime.IsZero()
+}
diff --git a/internal/home/keymail_fresh_test.go b/internal/home/keymail_fresh_test.go
index 740ace2..843d308 100644
--- a/internal/home/keymail_fresh_test.go
+++ b/internal/home/keymail_fresh_test.go
@@ -1,9 +1,11 @@
package home
import (
+ "html"
"net/http"
"net/http/httptest"
"net/url"
+ "regexp"
"strings"
"testing"
"time"
@@ -142,13 +144,13 @@ func TestFreshKeymailWithoutAFreshGestureFallsBackToEmail(t *testing.T) {
// override: the browser submits its action, not the URL it
// was reached by.
form := exchangeRequest(p.credential.App, "GET", Origin+"/signin?force=1", "", nil, intent, navigation)
- if form.Code != 200 || !strings.Contains(form.Body.String(), `action="/signin?force=1"`) {
- t.Fatal("fallback form does not keep the email override", form.Code)
+ if form.Code != 200 {
+ t.Fatal("fallback form", form.Code)
}
if c := intentFrom(form); c != nil {
navigation = c
}
- sent := freshBegin(t, p, "/signin?force=1", freshKeymailAddress, intent, navigation)
+ sent := freshBegin(t, p, formAction(t, form), freshKeymailAddress, intent, navigation)
if sent.Code != 303 || sent.Header().Get("Location") != "/signin/sent" {
t.Fatal("fallback did not email", sent.Code)
}
@@ -161,6 +163,19 @@ func TestFreshKeymailWithoutAFreshGestureFallsBackToEmail(t *testing.T) {
}
}
+// formAction is the sign-in form's own action, which is what a browser
+// submits — not the URL that rendered the form. Tests that POST to a URL
+// of their own choosing are how the ordinary fallback's lost override
+// survived.
+func formAction(t *testing.T, r *httptest.ResponseRecorder) string {
+ t.Helper()
+ m := regexp.MustCompile(`<form method="post" action="([^"]*)"`).FindStringSubmatch(r.Body.String())
+ if len(m) != 2 {
+ t.Fatal("no sign-in form")
+ }
+ return html.UnescapeString(m[1])
+}
+
func intentCleared(r *httptest.ResponseRecorder) bool {
for _, c := range r.Result().Cookies() {
if c.Name == assertionIntentCookie {
@@ -232,3 +247,67 @@ func TestFreshKeymailCannotSwitchSubject(t *testing.T) {
t.Fatal("a different person's fresh Keymail gesture satisfied the original step-up", r.Code)
}
}
+
+// The ordinary Keymail fallback, driven as a browser drives it: a failed
+// return lands on /signin?force=1, and the person submits the form that
+// page renders. The override has to be in that form's action, or the
+// next attempt classifies again and goes straight back to Keymail.
+func TestOrdinaryKeymailFallbackFormEmails(t *testing.T) {
+ app := newTestApp(t)
+ newKeymailStub(t, app, freshKeymailAddress)
+ pending, _ := beginKeymail(t, app, freshKeymailAddress)
+ back := callback(t, app, "code-1", "invalid-state", pending)
+ if back.Header().Get("Location") != "/signin?force=1" || sessionCookie(back) != nil {
+ t.Fatal("failed Keymail return did not fall back", back.Code)
+ }
+ form := returnRequest(app, "GET", back.Header().Get("Location"), nil)
+ sent := returnRequest(app, "POST", formAction(t, form), url.Values{"address": {freshKeymailAddress}})
+ if sent.Code != 303 || sent.Header().Get("Location") != "/signin/sent" {
+ t.Fatal("the fallback form went back to Keymail instead of emailing", sent.Code)
+ }
+ if m, ok := app.mailer.last(); !ok || m.To != freshKeymailAddress {
+ t.Fatal("no email was sent")
+ }
+ // And the plain form stays plain.
+ if got := formAction(t, returnRequest(app, "GET", "/signin", nil)); got != "/signin" {
+ t.Fatalf("plain form action=%q", got)
+ }
+}
+
+// A Keymail session that reported no gesture time has nothing a step-up
+// can credit: its At is when Home minted it, not when the person did
+// anything. It must not pass a later fresh intent just for being new.
+// The control is the same flow with a reported gesture, which passes.
+func TestKeymailSessionWithoutGestureTimeIsNotFresh(t *testing.T) {
+ for name, gesture := range map[string]bool{"no gesture": false, "gesture": true} {
+ t.Run(name, func(t *testing.T) {
+ p := newExchangePair(t)
+ stub := newKeymailStub(t, p.credential, freshKeymailAddress) // reauth: false
+ if !gesture {
+ stub.authTime = time.Time{}
+ }
+ pending, state := beginKeymail(t, p.credential, freshKeymailAddress)
+ session := sessionCookie(callback(t, p.credential, "code-1", state, pending))
+ if session == nil {
+ t.Fatal("ordinary Keymail sign-in failed")
+ }
+ _, intent := p.startFresh(t, session)
+ resume := exchangeRequest(p.credential.App, "GET", Origin+"/authorize/resume", "", nil, intent, session)
+ if gesture {
+ if resume.Code != 200 || !strings.Contains(resume.Body.String(), `name="assertion"`) {
+ t.Fatal("a Keymail session with a fresh gesture was refused", resume.Code)
+ }
+ return
+ }
+ if resume.Code != 303 || resume.Header().Get("Location") != "/signin?return_to=%2Fauthorize%2Fresume" || strings.Contains(resume.Body.String(), `name="assertion"`) {
+ t.Fatal("a Keymail session with no gesture time satisfied a step-up", resume.Code)
+ }
+ form := exchangeRequest(p.credential.App, "GET", Origin+resume.Header().Get("Location"), "", nil, intent, session)
+ navigation := namedCookie(t, form, returnCookie)
+ begin := freshBegin(t, p, formAction(t, form), freshKeymailAddress, intent, session, navigation)
+ if begin.Code != 303 || begin.Header().Get("Location") != "/signin/sent" {
+ t.Fatal("step-up did not fall back to email", begin.Code)
+ }
+ })
+ }
+}
diff --git a/internal/home/signin.go b/internal/home/signin.go
index befa0c6..d73d14d 100644
--- a/internal/home/signin.go
+++ b/internal/home/signin.go
@@ -161,21 +161,18 @@ func clientIP(r *http.Request) string {
// handleSigninForm renders the address form.
//
-// In a fresh step-up the email override has to survive into the POST,
-// because the browser submits the form's action and not the URL that
-// rendered it: without this, a Keymail return that proved no fresh
-// gesture would fall back to a form that classifies the address again
-// and sends the person straight back to Keymail. Only the fresh flow
-// carries it, so the ordinary form and its fallback are unchanged.
-// Dropping the flag from the action is harmless: it buys another
-// Keymail round trip, held to the same gesture check.
+// The email override (force=1) has to survive into the POST, because the
+// browser submits the form's action and not the URL that rendered it.
+// Every failed Keymail return lands on /signin?force=1; without this the
+// next attempt classified the address again and went straight back to
+// Keymail, so "not a dead end" was not true in a browser. Dropping the
+// flag from the action is harmless: it buys another Keymail round trip,
+// held to the same checks.
func (a *App) handleSigninForm(w http.ResponseWriter, r *http.Request) {
rememberReturn(w, r)
data := map[string]any{}
if r.URL.Query().Get("force") == "1" {
- if intent, ok := a.readAssertionIntent(r, time.Now()); ok && intent.MaxAge != 0 {
- data["Action"] = "/signin?force=1"
- }
+ data["Action"] = "/signin?force=1"
}
a.render(w, r, http.StatusOK, "signin.html", data)
}
diff --git a/internal/home/signin_return_test.go b/internal/home/signin_return_test.go
index 16cc2e8..a940518 100644
--- a/internal/home/signin_return_test.go
+++ b/internal/home/signin_return_test.go
@@ -82,7 +82,9 @@ func TestCredentialReturnKeymailAndFallback(t *testing.T) {
if changed := intentFrom(form); changed != nil {
intent = changed
}
- returnRequest(app, "POST", "/signin?force=1", url.Values{"address": {"person@keymail.invalid"}}, intent)
+ // The form's own action, as a browser submits it: posting
+ // to /signin?force=1 directly hid that the form dropped it.
+ returnRequest(app, "POST", formAction(t, form), url.Values{"address": {"person@keymail.invalid"}}, intent)
got = returnRequest(app, "POST", linkPath, url.Values{"token": {capturedLink(t, app)}}, intent)
}
if got.Header().Get("Location") != "/passkey/codes" || sessionCookie(got) == nil {