Close the gap between the sign-out-all mark and a directory sign-in
Human
Prompt
Coordinator, 2026-11-14: Fable final check on 5281e03: APPROVE AFTER FIXES, one defect N1: the directory's sign-out-all mark check and the session insert are not atomic. (a) directoryNativeExchange: do the signedOutBefore check inside the transaction that inserts the session. (b) Browser finish complete: after sessions.SignIn, re-check the mark; if iat <= mark, delete the new session and fail with the signed-out copy. Add a test for each ordering.
From this commit's own message. The prompt ledger has no entries for it — which is what a squashed, rebased or amended commit looks like, and what every commit looks like in a repo without a ledger.
Summary
(a) The native exchange checks the mark again inside the transaction that inserts the session; the directory's writer is one connection, so no receipt commits in between. (b) The browser finish re-checks after sessions.SignIn; if a press landed in between, the new session row is deleted, its Set-Cookie is replaced by a clearing one, and the person sees the 'All devices are signed out' page (403) instead of a generic failure, carried from Complete to Fail by a small ResponseWriter wrapper on the finish route. A no-op test seam (afterMarkCheck) sits between the first check and the write; TestNativeSignOutAllRaceOrderings commits a mark there for each door, and each fix was mutation-tested. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
5 files changed,
+142
−2
internal/home/directory.go+20 −2internal/home/directory_native.go+12 −0internal/home/handoff_failure.go+7 −0internal/home/native_review_test.go+54 −0internal/home/signout_all.go+49 −0
CI passed — run details
diff --git a/internal/home/directory.go b/internal/home/directory.go| index 38edafb..5626d0b 100644 |
| --- a/internal/home/directory.go |
| +++ b/internal/home/directory.go |
| @@ -49,9 +49,22 @@ func newDirectory(cfg Runtime, d *db.DB, logger *slog.Logger) (*App, error) { |
| complete := func(w http.ResponseWriter, r *http.Request, c assertion.Claims) error { |
| // An assertion issued before "Sign out all devices" buys nothing. |
| if err := a.directorySigninAllowed(r.Context(), c); err != nil { |
| + markSignedOutAllFinish(w) |
| return err |
| } |
| - return a.sessions.SignIn(w, r, sessions.Session{Subject: c.Subject, Method: "assertion", AuthTime: time.Unix(c.AuthTime, 0)}) |
| + afterMarkCheck() |
| + if err := a.sessions.SignIn(w, r, sessions.Session{Subject: c.Subject, Method: "assertion", AuthTime: time.Unix(c.AuthTime, 0)}); err != nil { |
| + return err |
| + } |
| + // SignIn writes on its own, so a receipt may have committed between |
| + // the check above and it. Check again now the session exists; if the |
| + // press came in between, the new session goes too. |
| + if err := a.directorySigninAllowed(r.Context(), c); err != nil { |
| + a.undoSignIn(w, r) |
| + markSignedOutAllFinish(w) |
| + return err |
| + } |
| + return nil |
| } |
| a.consumer, err = handoff.NewConsumer(handoff.ConsumerConfig{Store: store, ConsumerID: "directory", CurrentSubject: func(r *http.Request) string { |
| s, ok := sessions.Current(r) |
| @@ -129,7 +142,12 @@ func (a *App) directoryRouter() http.Handler { |
| } |
| r.Get(handoff.StartPath, a.consumer.Start) |
| r.Get(a.fresh.StartPath(), a.fresh.Start) |
| - r.Get(handoff.FinishPath, a.consumer.Finish) |
| + r.Get(handoff.FinishPath, func(w http.ResponseWriter, r *http.Request) { |
| + // The writer lets Complete tell Fail that the refusal was "Sign |
| + // out all devices", so the person sees that rather than a |
| + // generic failure. |
| + a.consumer.Finish(&finishWriter{ResponseWriter: w}, r) |
| + }) |
| r.Get(nativeTeamsPath, a.directoryNativeTeams) |
| r.Get(noTeamsHelpPath, a.handleNoTeamsHelp) |
| r.Get("/signin", func(w http.ResponseWriter, r *http.Request) { |
diff --git a/internal/home/directory_native.go b/internal/home/directory_native.go| index e4dce6b..efa8fbd 100644 |
| --- a/internal/home/directory_native.go |
| +++ b/internal/home/directory_native.go |
| @@ -92,6 +92,7 @@ func (a *App) directoryNativeExchange(w http.ResponseWriter, r *http.Request) { |
| answer(http.StatusUnauthorized, "failed") |
| return |
| } |
| + afterMarkCheck() |
| // The jti is reserved in handoff's own committed transaction, as |
| // connect's NativeExchange does; a reserved jti that then mints nothing |
| // costs the app one fresh assertion (spec §7, accepted non-atomicity). |
| @@ -109,6 +110,17 @@ func (a *App) directoryNativeExchange(w http.ResponseWriter, r *http.Request) { |
| return |
| } |
| defer tx.Rollback() |
| + // The mark again, inside the transaction that inserts the session: the |
| + // directory's writer is one connection, so a receipt cannot commit |
| + // between this check and the insert. The check above only saves a jti. |
| + if before, err := signedOutBefore(r.Context(), tx, "home_directory_signed_out_all", claims.Subject, claims.IssuedAt); err != nil || before { |
| + if err != nil { |
| + answer(http.StatusServiceUnavailable, "unavailable") |
| + } else { |
| + answer(http.StatusUnauthorized, "failed") |
| + } |
| + return |
| + } |
| token, hash, err := sessions.NewToken() |
| if err != nil { |
| answer(http.StatusServiceUnavailable, "unavailable") |
diff --git a/internal/home/handoff_failure.go b/internal/home/handoff_failure.go| index cb12d38..85a4fc6 100644 |
| --- a/internal/home/handoff_failure.go |
| +++ b/internal/home/handoff_failure.go |
| @@ -54,6 +54,13 @@ func (a *App) failAssertion(w http.ResponseWriter, r *http.Request, code handoff |
| } |
| func (a *App) failDirectoryHandoff(w http.ResponseWriter, r *http.Request, status int) { |
| + if signedOutAllFinish(w) { |
| + // The assertion predates "Sign out all devices": say that, not |
| + // "something went wrong". |
| + w.Header().Set("Cache-Control", "no-store") |
| + a.render(w, r, http.StatusForbidden, "directory_signed_out.html", map[string]any{"AllDevices": true, "Action": a.origins.Credential + signoutPath}) |
| + return |
| + } |
| stage := "directory_unknown" |
| switch r.URL.Path { |
| case handoff.StartPath: |
diff --git a/internal/home/native_review_test.go b/internal/home/native_review_test.go| index bf62014..81218f2 100644 |
| --- a/internal/home/native_review_test.go |
| +++ b/internal/home/native_review_test.go |
| @@ -292,3 +292,57 @@ func TestNativeSignOutAllHopFailureAndOtherCookie(t *testing.T) { |
| t.Fatal("another person's directory session ended") |
| } |
| } |
| + |
| +// Fable's final check N1: a receipt that commits between a sign-in's first |
| +// mark check and its session write still wins, on both directory doors. |
| +// Serial: it swaps the afterMarkCheck seam. |
| +func TestNativeSignOutAllRaceOrderings(t *testing.T) { |
| + prev := afterMarkCheck |
| + t.Cleanup(func() { afterMarkCheck = prev }) |
| + f := newMountedChooser(t) |
| + cred := f.p.credential |
| + dirSessions := func(person string) int { |
| + var n int |
| + f.p.directory.db.Writer().QueryRow("SELECT count(*) FROM sessions WHERE subject=?", person).Scan(&n) |
| + return n |
| + } |
| + pressNow := func(person string) func() { |
| + return func() { |
| + if _, err := f.p.directory.db.Writer().Exec("INSERT INTO home_directory_signed_out_all(person_ref,at) VALUES(?,?) ON CONFLICT(person_ref) DO UPDATE SET at=excluded.at", person, time.Now().Unix()+5); err != nil { |
| + t.Error(err) |
| + } |
| + } |
| + } |
| + |
| + // (a) The native exchange: the in-transaction check refuses. |
| + d := newDevice(t) |
| + grant, _ := d.signInByCode(t, cred, "race-native@example.invalid") |
| + var person string |
| + cred.db.Writer().QueryRow("SELECT person_ref FROM home_id_grants").Scan(&person) |
| + verifier, challenge := verifierFor(70) |
| + token := decodeReply(t, d.assert(t, cred, grant, directoryOrigin, challenge))["assertion"].(string) |
| + afterMarkCheck = pressNow(person) |
| + if w := directoryCall(t, f, "POST", nativeExchangePath, exchangeBody(token, verifier), nil, nil); w.Code != 401 { |
| + t.Fatal("native exchange minted across a press", w.Code, w.Body.String()) |
| + } |
| + if n := dirSessions(person); n != 0 { |
| + t.Fatal("native session survived the press", n) |
| + } |
| + |
| + // (b) The browser finish: the re-check after SignIn undoes the session. |
| + afterMarkCheck = func() {} |
| + session := signInFully(t, cred, "race-web@example.invalid") |
| + webPerson := subjectOf(t, cred, session) |
| + pending, intent := f.p.start(t) |
| + browserToken, _ := f.p.mint(t, intent, session) |
| + callback := exchangeRequest(f.p.directory, "POST", directoryOrigin+handoff.CallbackPath, Origin, map[string][]string{"assertion": {browserToken}}) |
| + inbox := namedCookie(t, callback, "__Host-oficina-assertion-inbox") |
| + afterMarkCheck = pressNow(webPerson) |
| + fin := exchangeRequest(f.p.directory, "GET", directoryOrigin+handoff.FinishPath, "", nil, pending, inbox) |
| + if sessionCookie(fin) != nil || fin.Code != 403 || !strings.Contains(fin.Body.String(), "All devices are signed out") { |
| + t.Fatal("browser finish across a press", fin.Code, sessionCookie(fin) != nil) |
| + } |
| + if n := dirSessions(webPerson); n != 0 { |
| + t.Fatal("browser session survived the press", n) |
| + } |
| +} |
diff --git a/internal/home/signout_all.go b/internal/home/signout_all.go| index b149daf..2b25a71 100644 |
| --- a/internal/home/signout_all.go |
| +++ b/internal/home/signout_all.go |
| @@ -227,3 +227,52 @@ func (a *App) handleSignedOutAll(w http.ResponseWriter, r *http.Request) { |
| idCookie(w, "__Host-oficina-signed-out-all", "1", 300) |
| http.Redirect(w, r, "/signed-out", http.StatusSeeOther) |
| } |
| + |
| +// afterMarkCheck is a test seam: it runs between a sign-in's first mark |
| +// check and the write that follows, so a test can commit a receipt in that |
| +// gap. It does nothing in production. |
| +var afterMarkCheck = func() {} |
| + |
| +// finishWriter carries one fact from the directory's Complete to its Fail |
| +// hook through handoff.Consumer.Finish: the sign-in was refused because the |
| +// person pressed "Sign out all devices". |
| +type finishWriter struct { |
| + http.ResponseWriter |
| + signedOutAll bool |
| +} |
| + |
| +func markSignedOutAllFinish(w http.ResponseWriter) { |
| + if fw, ok := w.(*finishWriter); ok { |
| + fw.signedOutAll = true |
| + } |
| +} |
| + |
| +func signedOutAllFinish(w http.ResponseWriter) bool { |
| + fw, ok := w.(*finishWriter) |
| + return ok && fw.signedOutAll |
| +} |
| + |
| +// undoSignIn deletes the session sessions.SignIn just minted, found by the |
| +// cookie it set on this response, and replaces that cookie with a clearing |
| +// one. Nothing has been sent yet: handoff's Finish writes only after |
| +// Complete returns. |
| +func (a *App) undoSignIn(w http.ResponseWriter, r *http.Request) { |
| + name := a.sessions.CookieName() |
| + h := w.Header() |
| + kept := h.Values("Set-Cookie")[:0:0] |
| + for _, line := range h.Values("Set-Cookie") { |
| + c, err := http.ParseSetCookie(line) |
| + if err == nil && c.Name == name && c.Value != "" { |
| + if _, err := a.db.Writer().ExecContext(r.Context(), "DELETE FROM sessions WHERE token_hash=?", sessions.HashToken(c.Value)); err != nil { |
| + a.logger.Warn("undoing a refused directory sign-in failed") |
| + } |
| + continue |
| + } |
| + kept = append(kept, line) |
| + } |
| + h.Del("Set-Cookie") |
| + for _, line := range kept { |
| + h.Add("Set-Cookie", line) |
| + } |
| + idCookie(w, name, "", -1) |
| +} |