Confirm POSTs never draw a page to a stale session; boot sweep tested
Fable's approval notes (discussion 94), items 1 and 2: - Both confirm POSTs compute freshness once before Open and, after it, redirect whenever the session is not fresh or the in-write check found it stale, before any render (a malformed form included). The check inside the write is still the one that counts. - TestOperatorBootSchedulesSweepsForSurvivingRows: a surviving plan row gets one boot-scheduled sweep at its end, and the sweep deletes it.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
4 files changed,
+65
−3
internal/home/operator_ceremony_test.go+8 −0internal/home/operator_confirm.go+16 −2internal/home/operator_http.go+6 −1internal/home/operator_http_test.go+35 −0
diff --git a/internal/home/operator_ceremony_test.go b/internal/home/operator_ceremony_test.go| index 99366ed..1f25986 100644 |
| --- a/internal/home/operator_ceremony_test.go |
| +++ b/internal/home/operator_ceremony_test.go |
| @@ -70,6 +70,11 @@ func TestOperatorDirectoryConfirmCeremony(t *testing.T) { |
| if post.Code != 303 || f.openCount(t, f.directory) != 0 { |
| t.Fatal("stale POST opened", post.Code) |
| } |
| + // Any other id is a redirect too, never a page drawn to a stale session. |
| + other := exchangeRequest(f.directory, "POST", directoryOrigin+operatorConfirm, directoryOrigin, url.Values{"plan": {fixtureToken(91)}}, session) |
| + if other.Code != 303 || strings.Contains(other.Body.String(), operatorName) { |
| + t.Fatal("stale POST with another plan id rendered", other.Code) |
| + } |
| } |
| session = f.paulDirectory(t, f.paulCredential(t)) |
| if rec := f.confirm(t, "directory", session, ""); rec.Code != 200 || f.openCount(t, f.directory) != 1 { |
| @@ -201,6 +206,9 @@ func TestOperatorCredentialConfirmRefusesKeymailWithoutGestureAndRecovery(t *tes |
| if r := exchangeRequest(f.credential.App, "POST", Origin+operatorConfirm, Origin, url.Values{"plan": {fixtureToken(90)}}, cookie); r.Code != 303 || f.openCount(t, f.credential.App) != 0 { |
| t.Fatal(method, "opened", r.Code) |
| } |
| + if r := exchangeRequest(f.credential.App, "POST", Origin+operatorConfirm, Origin, url.Values{"plan": {fixtureToken(91)}}, cookie); r.Code != 303 { |
| + t.Fatal(method, "stale POST with another plan id rendered", r.Code) |
| + } |
| } |
| } |
diff --git a/internal/home/operator_confirm.go b/internal/home/operator_confirm.go| index 2ec2082..ae0b90c 100644 |
| --- a/internal/home/operator_confirm.go |
| +++ b/internal/home/operator_confirm.go |
| @@ -111,7 +111,14 @@ func (a *App) handleDirectoryConfirmPost(w http.ResponseWriter, r *http.Request) |
| if !ok { |
| return |
| } |
| + // Freshness once up front, so no stale session is drawn a page; the |
| + // check inside the write below is still the one that counts. |
| + fresh := directoryFresh(s, time.Now()) |
| if err := r.ParseForm(); err != nil { |
| + if !fresh { |
| + http.Redirect(w, r, operatorConfirm, http.StatusSeeOther) |
| + return |
| + } |
| a.renderOperatorRefusal(w, r, "operator_confirm.html", http.StatusBadRequest, "Nothing was opened.") |
| return |
| } |
| @@ -125,7 +132,7 @@ func (a *App) handleDirectoryConfirmPost(w http.ResponseWriter, r *http.Request) |
| } |
| return true |
| }) |
| - if stale { |
| + if !fresh || stale { |
| http.Redirect(w, r, operatorConfirm, http.StatusSeeOther) |
| return |
| } |
| @@ -236,7 +243,14 @@ func (a *App) handleCredentialConfirmPost(w http.ResponseWriter, r *http.Request |
| if !ok { |
| return |
| } |
| + // Freshness once up front, so no stale session is drawn a page; the |
| + // check inside the write below is still the one that counts. |
| + fresh := credentialFresh(s, time.Now()) |
| if err := r.ParseForm(); err != nil { |
| + if !fresh { |
| + http.Redirect(w, r, operatorConfirm, http.StatusSeeOther) |
| + return |
| + } |
| a.renderOperatorRefusal(w, r, "operator_confirm_id.html", http.StatusBadRequest, "Nothing was opened.") |
| return |
| } |
| @@ -256,7 +270,7 @@ func (a *App) handleCredentialConfirmPost(w http.ResponseWriter, r *http.Request |
| } |
| return typed != "" && hmac.Equal([]byte(blindIndex(pepper, typed)), []byte(v.OwnerIndex)) |
| }) |
| - if stale { |
| + if !fresh || stale { |
| http.Redirect(w, r, operatorConfirm, http.StatusSeeOther) |
| return |
| } |
diff --git a/internal/home/operator_http.go b/internal/home/operator_http.go| index 4cc4252..279f6e5 100644 |
| --- a/internal/home/operator_http.go |
| +++ b/internal/home/operator_http.go |
| @@ -67,6 +67,8 @@ type operatorHTTPSettings struct { |
| provisioning *family.Keypair |
| rosterKeys []roster.Key |
| now func() time.Time |
| + // after schedules expiry sweeps; nil is time.AfterFunc (tests capture). |
| + after func(time.Duration, func()) |
| } |
| var errOperatorConfig = errExchangeConfig |
| @@ -241,7 +243,10 @@ func (a *App) newOperatorEndpoint(cfg *Runtime) error { |
| if err != nil { |
| return errOperatorConfig |
| } |
| - e := &operatorEndpoint{role: cfg.role, subject: s.config.OperatorSubject, prefilter: s.prefilter, verifier: s.verifier, plans: plans, apps: operatorApps(), now: s.now, after: func(d time.Duration, f func()) { time.AfterFunc(d, f) }} |
| + e := &operatorEndpoint{role: cfg.role, subject: s.config.OperatorSubject, prefilter: s.prefilter, verifier: s.verifier, plans: plans, apps: operatorApps(), now: s.now, after: s.after} |
| + if e.after == nil { |
| + e.after = func(d time.Duration, f func()) { time.AfterFunc(d, f) } |
| + } |
| if cfg.role == "credential" { |
| e.host = a.credentialHost |
| resolve, err := NewOperatorPersonResolver(a.secrets.Pepper) |
diff --git a/internal/home/operator_http_test.go b/internal/home/operator_http_test.go| index 4bc54af..f144fed 100644 |
| --- a/internal/home/operator_http_test.go |
| +++ b/internal/home/operator_http_test.go |
| @@ -617,3 +617,38 @@ func TestOperatorExpirySweepsRunUnprompted(t *testing.T) { |
| t.Fatal("expired window row left") |
| } |
| } |
| + |
| +// A plan row that survives a restart is still swept at its end: boot |
| +// schedules one sweep per surviving row (PlanStore.Ends). |
| +func TestOperatorBootSchedulesSweepsForSurvivingRows(t *testing.T) { |
| + f := newOperatorFixture(t) |
| + if rec := f.post("directory", operatorPlanPath, f.plan(t, "directory", "add-apps", []string{"docs"}, nil), nil); rec.Code != 200 { |
| + t.Fatal(rec.Code) |
| + } |
| + var end int64 |
| + if err := f.directory.db.Writer().QueryRow("SELECT expires_at FROM operator_plans").Scan(&end); err != nil { |
| + t.Fatal(err) |
| + } |
| + // Boot again on the same database, with the scheduler captured. |
| + cfg := *f.directory.runtime |
| + settings := *cfg.operator |
| + clock := time.Now() |
| + settings.now = func() time.Time { return clock } |
| + var waits []time.Duration |
| + var sweeps []func() |
| + settings.after = func(d time.Duration, fn func()) { waits = append(waits, d); sweeps = append(sweeps, fn) } |
| + cfg.operator = &settings |
| + a, err := NewRuntime(cfg, f.directory.db, quietLogger()) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + want := time.Unix(end, 0).Sub(clock) + time.Second |
| + if len(waits) != 1 || waits[0] != want { |
| + t.Fatal("boot scheduled", waits, "want", want) |
| + } |
| + clock = time.Unix(end, 0).Add(time.Second) |
| + sweeps[0]() |
| + if f.countRows(t, a, "operator_plans") != 0 { |
| + t.Fatal("surviving plan row not swept at its end") |
| + } |
| +} |