Fix direct sign-in follow-ups and add atomic consumer completion
8 files changed,
+122
−9
handoff/atomic_test.go+42 −0handoff/store.go+34 −0internal/home/id_direct.go+0 −4internal/home/id_followups_test.go+34 −0internal/home/id_passkey.go+6 −1internal/home/id_review_test.go+2 −2internal/home/passkey.go+2 −2internal/home/templates/recovery_codes.html+2 −0
CI cancelled — superseded by a newer push — run details
diff --git a/handoff/atomic_test.go b/handoff/atomic_test.go| new file mode 100644 |
| index 0000000..82b327b |
| --- /dev/null |
| +++ b/handoff/atomic_test.go |
| @@ -0,0 +1,42 @@ |
| +package handoff |
| + |
| +import ( |
| + "amadan.net/rastrillo/rastrillo/assertion" |
| + "context" |
| + "database/sql" |
| + "testing" |
| +) |
| + |
| +func TestReceivePendingRefusesUnknownNonce(t *testing.T) { |
| + f := fresh(t) |
| + _, nonce := f.pending(t) |
| + f.db.Exec("DELETE FROM oficina_handoff_pending") |
| + if _, err := f.s.ReceivePending(context.Background(), f.token(t, nonce), f.now); err != ErrRefused { |
| + t.Fatalf("unknown nonce: %v", err) |
| + } |
| + if count(t, f.db, "oficina_handoff_inbox") != 0 { |
| + t.Fatal("retained unsolicited receipt") |
| + } |
| +} |
| +func TestFinishAtomicRollsBackAppAndProof(t *testing.T) { |
| + f := fresh(t) |
| + secret, nonce := f.pending(t) |
| + handle, _ := f.received(t, nonce) |
| + f.db.Exec("CREATE TABLE minted (subject TEXT)") |
| + _, _, err := f.s.FinishAtomic(context.Background(), secret, handle, "", f.now, func(tx *sql.Tx, c assertion.Claims, _ string) error { |
| + if _, e := tx.Exec("INSERT INTO minted VALUES(?)", c.Subject); e != nil { |
| + t.Fatal(e) |
| + } |
| + return ErrRefused |
| + }) |
| + if err != ErrRefused || count(t, f.db, "minted") != 0 || count(t, f.db, "oficina_handoff_pending") != 1 { |
| + t.Fatal("partial commit", err) |
| + } |
| + _, _, err = f.s.FinishAtomic(context.Background(), secret, handle, "", f.now, func(tx *sql.Tx, c assertion.Claims, _ string) error { |
| + _, e := tx.Exec("INSERT INTO minted VALUES(?)", c.Subject) |
| + return e |
| + }) |
| + if err != nil || count(t, f.db, "minted") != 1 || count(t, f.db, "oficina_handoff_pending") != 0 { |
| + t.Fatal("atomic completion", err) |
| + } |
| +} |
diff --git a/handoff/store.go b/handoff/store.go| index 65c2281..30f76fd 100644 |
| --- a/handoff/store.go |
| +++ b/handoff/store.go |
| @@ -267,6 +267,15 @@ func (s *Store) cancel(ctx context.Context, secret string) error { |
| // Receive deliberately needs no cookie: a cross-site POST may carry none. A |
| // repeated assertion gets no new inbox row or cookie, even before Finish runs. |
| func (s *Store) Receive(ctx context.Context, token string, now time.Time) (string, error) { |
| + return s.receive(ctx, token, now, false) |
| +} |
| + |
| +// ReceivePending refuses receipts without a live browser transaction. |
| +func (s *Store) ReceivePending(ctx context.Context, token string, now time.Time) (string, error) { |
| + return s.receive(ctx, token, now, true) |
| +} |
| + |
| +func (s *Store) receive(ctx context.Context, token string, now time.Time, requirePending bool) (string, error) { |
| claims, e := s.verifier.Verify(token, now) |
| if e != nil { |
| return "", ErrRefused |
| @@ -287,6 +296,14 @@ func (s *Store) Receive(ctx context.Context, token string, now time.Time) (strin |
| } |
| defer tx.Rollback() |
| var count int |
| + if requirePending { |
| + if e = tx.QueryRowContext(ctx, `SELECT COUNT(*) FROM oficina_handoff_pending WHERE nonce_hash=? AND expires_at>?`, digest(claims.Nonce), n).Scan(&count); e != nil { |
| + return "", ErrStorage |
| + } |
| + if count != 1 { |
| + return "", ErrRefused |
| + } |
| + } |
| if e = tx.QueryRowContext(ctx, `SELECT COUNT(*) FROM oficina_handoff_replay WHERE jti_hash=?`, jti).Scan(&count); e != nil { |
| return "", ErrStorage |
| } |
| @@ -322,6 +339,12 @@ func (s *Store) Receive(ctx context.Context, token string, now time.Time) (strin |
| // existingSubject is resolved before calling: reading a session from inside |
| // this transaction could deadlock an app's single-connection writer. |
| func (s *Store) Finish(ctx context.Context, secret, handle, existingSubject string, now time.Time) (assertion.Claims, string, error) { |
| + return s.FinishAtomic(ctx, secret, handle, existingSubject, now, nil) |
| +} |
| + |
| +// FinishAtomic consumes the exchange and applies the consumer mutation in one writer transaction. |
| +// The hook must use tx for every lookup and write, and must not publish a cookie before commit. |
| +func (s *Store) FinishAtomic(ctx context.Context, secret, handle, existingSubject string, now time.Time, complete func(*sql.Tx, assertion.Claims, string) error) (assertion.Claims, string, error) { |
| zero := assertion.Claims{} |
| if !validHandle(secret) || !validHandle(handle) { |
| return zero, "", ErrRefused |
| @@ -383,6 +406,17 @@ func (s *Store) Finish(ctx context.Context, secret, handle, existingSubject stri |
| return zero, "", ErrStorage |
| } |
| } |
| + if fresh && (claims.AuthTime > n || fence-claims.AuthTime > freshAuthLifetime) { |
| + if tx.Commit() != nil { |
| + return zero, "", ErrStorage |
| + } |
| + return zero, "", ErrRefused |
| + } |
| + if complete != nil { |
| + if e = complete(tx, claims, string(path)); e != nil { |
| + return zero, "", e |
| + } |
| + } |
| if tx.Commit() != nil { |
| return zero, "", ErrStorage |
| } |
diff --git a/internal/home/id_direct.go b/internal/home/id_direct.go| index 7fc7be5..cebd1c0 100644 |
| --- a/internal/home/id_direct.go |
| +++ b/internal/home/id_direct.go |
| @@ -57,10 +57,6 @@ func consumeDirect(tx *sql.Tx, challenge []byte, expires, now int64) error { |
| if _, err := tx.Exec("DELETE FROM home_id_direct_used WHERE expires_at<=?", now); err != nil { |
| return errIDProof |
| } |
| - var n int |
| - if tx.QueryRow("SELECT count(*) FROM home_id_direct_used").Scan(&n) != nil || n >= 10000 { |
| - return errIDProof |
| - } |
| if _, err := tx.Exec("INSERT INTO home_id_direct_used VALUES(?,?)", hex.EncodeToString(challenge), expires); err != nil { |
| return errIDProof |
| } |
diff --git a/internal/home/id_followups_test.go b/internal/home/id_followups_test.go| new file mode 100644 |
| index 0000000..3081b57 |
| --- /dev/null |
| +++ b/internal/home/id_followups_test.go |
| @@ -0,0 +1,34 @@ |
| +package home |
| + |
| +import ( |
| + "amadan.net/oficina/home/internal/home/sealtest" |
| + "amadan.net/rastrillo/rastrillo/webauthn/authtest" |
| + "net/http/httptest" |
| + "net/url" |
| + "strings" |
| + "testing" |
| +) |
| + |
| +func TestIDZeroCodesPreservesReturn(t *testing.T) { |
| + a := newTestApp(t) |
| + s := signInFully(t, a, sealtest.CanaryAddress) |
| + k := enrol(t, a, s) |
| + target := "/authorize/resume" |
| + w := httptest.NewRecorder() |
| + writeReturn(w, target) |
| + b := idJSON(t, a, "/passkey/direct/begin", nil) |
| + zero := uint32(0) |
| + done := idJSON(t, a, "/passkey/direct/finish", assertionBody(k, challengeFrom(t, b), authtest.Options{RPID: credentialHost, Origin: Origin, Count: &zero}), namedCookie(t, b, ceremonyCookie), w.Result().Cookies()[0]) |
| + if !strings.Contains(done.Body.String(), "return_to=") { |
| + t.Fatal("lost original target", done.Body.String()) |
| + } |
| + path := "/passkey/codes?return_to=" + url.QueryEscape(target) |
| + page := exchangeRequest(a.App, "GET", Origin+path, "", nil, sessionCookie(done)) |
| + if !strings.Contains(page.Body.String(), `name="return_to" value="/authorize/resume"`) { |
| + t.Fatal("codes form lost return target") |
| + } |
| + codes := exchangeRequest(a.App, "POST", Origin+"/passkey/codes", Origin, url.Values{"return_to": {target}}, sessionCookie(done)) |
| + if !strings.Contains(codes.Body.String(), `href="/authorize/resume"`) { |
| + t.Fatal("no continuation after codes") |
| + } |
| +} |
diff --git a/internal/home/id_passkey.go b/internal/home/id_passkey.go| index 634d06f..9c3c9f3 100644 |
| --- a/internal/home/id_passkey.go |
| +++ b/internal/home/id_passkey.go |
| @@ -8,6 +8,7 @@ import ( |
| "encoding/json" |
| "errors" |
| "net/http" |
| + "net/url" |
| "strings" |
| "time" |
| @@ -396,7 +397,11 @@ func (a *App) idFinish(purpose string) http.HandlerFunc { |
| return |
| } |
| if codes == 0 { |
| - to = "/passkey/codes" |
| + if to != "/" && to != "/passkey/codes" { |
| + to = "/passkey/codes?return_to=" + url.QueryEscape(credentialReturn(to)) |
| + } else { |
| + to = "/passkey/codes" |
| + } |
| } |
| token, e := a.idMint(tx, r, sessions.Session{Subject: owner, Method: method, AuthTime: time.Unix(now, 0)}, now) |
| if e != nil { |
diff --git a/internal/home/id_review_test.go b/internal/home/id_review_test.go| index 47170ed..c3646d9 100644 |
| --- a/internal/home/id_review_test.go |
| +++ b/internal/home/id_review_test.go |
| @@ -303,8 +303,8 @@ func TestIDDirectReceiptsBounded(t *testing.T) { |
| r := idJSON(t, a, "/passkey/direct/finish", body, c) |
| switch kind { |
| case "capacity": |
| - if r.Code == 200 { |
| - t.Fatal("receipt cap bypassed") |
| + if r.Code != 200 { |
| + t.Fatal("another credential can exhaust direct sign-in") |
| } |
| case "expiry": |
| if r.Code != 200 { |
diff --git a/internal/home/passkey.go b/internal/home/passkey.go| index 7bb04c7..1dcce38 100644 |
| --- a/internal/home/passkey.go |
| +++ b/internal/home/passkey.go |
| @@ -131,7 +131,7 @@ func (a *App) handleRecoveryCodesPage(w http.ResponseWriter, r *http.Request) { |
| a.serverError(w, r) |
| return |
| } |
| - a.render(w, r, http.StatusOK, "recovery_codes.html", map[string]any{"Remaining": remaining}) |
| + a.render(w, r, http.StatusOK, "recovery_codes.html", map[string]any{"Remaining": remaining, "ReturnTo": credentialReturn(r.URL.Query().Get("return_to"))}) |
| } |
| // handleRegenerateRecoveryCodes mints a fresh set and shows it once. |
| @@ -170,7 +170,7 @@ func (a *App) handleRegenerateRecoveryCodes(w http.ResponseWriter, r *http.Reque |
| a.serverError(w, r) |
| return |
| } |
| - a.render(w, r, http.StatusOK, "recovery_codes.html", map[string]any{"Codes": codes}) |
| + a.render(w, r, http.StatusOK, "recovery_codes.html", map[string]any{"Codes": codes, "ReturnTo": credentialReturn(r.FormValue("return_to"))}) |
| } |
| // serveAsset serves one of the two script files. |
diff --git a/internal/home/templates/recovery_codes.html b/internal/home/templates/recovery_codes.html| index 36c0bc0..e4a801f 100644 |
| --- a/internal/home/templates/recovery_codes.html |
| +++ b/internal/home/templates/recovery_codes.html |
| @@ -11,7 +11,9 @@ There is no operator who can undo that, by design.</p> |
| <p>You have {{.Data.Remaining}} unused recovery codes.</p> |
| <p>Generating a new set <strong>invalidates every code you have now</strong>.</p> |
| <form method="post" action="/passkey/codes"> |
| + <input type="hidden" name="return_to" value="{{.Data.ReturnTo}}"> |
| <button type="submit" rst-btn="danger">Generate a new set</button> |
| </form> |
| {{end}} |
| +{{if .Data.Codes}}<a rst-btn href="{{.Data.ReturnTo}}">Continue</a>{{end}} |
| {{end}} |