feat(auth): support WebAuthn passkeys #14

Merged
exler merged 3 commits from feat/passkeys into main 2026-11-14 10:57:48 +01:00
Collaborator

Summary

  • Add usernameless WebAuthn passkey sign-in, supporting discoverable FIDO2 security keys such as YubiKey and synced passkeys such as Bitwarden.
  • Add /account enrollment and removal for multiple credentials; keep password+TOTP login as a fallback.
  • Bind credentials to existing accounts, require user verification and exact issuer origin, and require recent strong authentication before passkey changes.
  • Revoke the sessions, unredeemed authorization codes and access tokens created by a removed passkey.
  • Bound unauthenticated challenge creation with per-client admission limits and a persistent global cap; make one-time challenges and device-bound signature-counter updates atomic.

Verification

  • make test — passed.
  • go vet ./... — passed.
  • gofmt — clean; sqlc diff — clean.
  • Focused passkey admission, freshness and counter regressions repeated five times — passed.
  • Independent Fara review: no blocking findings on the reviewed source snapshot.
  • Local go test -race ./... could not run on this host: ThreadSanitizer exits before tests with unsupported VMA range (Found 47 - Supported 48). Forgejo Actions run 34 passed the CI test job, including go test -race ./... on Linux/amd64; the Docker publish job was skipped for this pull request.

Limits and follow-up

  • No real browser, YubiKey or Bitwarden device test, live migration, or deployment was performed.
  • The 20-passkey ceiling is checked at enrollment start; simultaneous enrollments can exceed it slightly. Review classified this as non-blocking.
  • The per-client limiter is in memory and resets on restart; the database-backed global pending-challenge cap remains in force.
  • Before production use, validate the migration on a backup and exercise the login/enrollment flow with the target browsers and authenticators.
## Summary - Add usernameless WebAuthn passkey sign-in, supporting discoverable FIDO2 security keys such as YubiKey and synced passkeys such as Bitwarden. - Add `/account` enrollment and removal for multiple credentials; keep password+TOTP login as a fallback. - Bind credentials to existing accounts, require user verification and exact issuer origin, and require recent strong authentication before passkey changes. - Revoke the sessions, unredeemed authorization codes and access tokens created by a removed passkey. - Bound unauthenticated challenge creation with per-client admission limits and a persistent global cap; make one-time challenges and device-bound signature-counter updates atomic. ## Verification - `make test` — passed. - `go vet ./...` — passed. - `gofmt` — clean; `sqlc diff` — clean. - Focused passkey admission, freshness and counter regressions repeated five times — passed. - Independent Fara review: no blocking findings on the reviewed source snapshot. - Local `go test -race ./...` could not run on this host: ThreadSanitizer exits before tests with `unsupported VMA range (Found 47 - Supported 48)`. Forgejo Actions run 34 passed the CI test job, including `go test -race ./...` on Linux/amd64; the Docker publish job was skipped for this pull request. ## Limits and follow-up - No real browser, YubiKey or Bitwarden device test, live migration, or deployment was performed. - The 20-passkey ceiling is checked at enrollment start; simultaneous enrollments can exceed it slightly. Review classified this as non-blocking. - The per-client limiter is in memory and resets on restart; the database-backed global pending-challenge cap remains in force. - Before production use, validate the migration on a backup and exercise the login/enrollment flow with the target browsers and authenticators.
[verified] feat(auth): add WebAuthn passkey sign-in
All checks were successful
Push Docker Image / test (pull_request) Successful in 2m8s
Push Docker Image / push (pull_request) Has been skipped
510e32269b
exler approved these changes 2026-11-14 07:12:10 +01:00
Owner
@ada @fara
ada left a comment

Ada security audit (source-only, read-only)

Initiating source: comment, ID 721, authored by exler (bare audit request on this pull request).
Reviewed head SHA: 510e32269b (base ffbc2896c5).
Scope: all 18 changed files (+1869/-54) for WebAuthn passkey sign-in and enrollment. Covered: internal/server/passkeys.go; the login, account and session wiring in handlers.go, auth.go, server.go and http.go; internal/db/queries.sql and migrations 0009/0010; internal/db/open.go (foreign keys enabled in the DSN); the login and account templates and app.js; CSRF coverage of the new routes; and the go-webauthn v0.18.2 login flag and counter checks from the local module cache. Source fetched pinned to the head SHA through the API.

Verdict: No blocking security findings in reviewed scope

Findings (none blocking):

  1. Low. Passkey sign-in start can be saturated by unauthenticated clients (availability only).
    Location: internal/server/passkeys.go:127-135 (per-client limiter refuses new keys once 10000 are live) and 386-406 (global pending-login cap); internal/db/queries.sql:243-246 (AdmitLoginChallenge counts all pending login rows).
    Prerequisite: a few distinct client addresses. Arithmetic from the source constants, not measured: about 10 sources at 20 starts per minute each fill the 1000-slot cap within the 5-minute challenge TTL.
    Impact: passkey sign-in starts return 429 for other users during that window. Password plus TOTP sign-in is not affected, and no authentication bypass or data exposure was found.
    Confidence: high for the mechanism, not reproduced at runtime.
    Minimal remedy: accept as a documented capacity trade-off, or reserve capacity per client key so one source cannot consume the shared cap.

  2. Low. Passkey removal revokes tokens only for sessions that are still live.
    Location: internal/server/passkeys.go:291-303; compare internal/server/auth.go:266-276 (same pattern for session revocation) and internal/server/oidc.go:29 (tokenTTL = 1 hour). Logout (handlers.go:216) ends the session but does not revoke its tokens.
    Impact: access tokens or unredeemed codes issued by a passkey session that has already ended are not revoked by removal, and remain valid until expiry, at most 1 hour. This matches the existing logout behaviour rather than a new regression.
    Confidence: high for the source behaviour.
    Minimal remedy: either document the bound, or revoke tokens by the passkey link if the stronger guarantee is required.

Hardening notes (not findings):

  • passkeys.go:382-389: the discoverable-login challenge is generated before per-client admission. Moving admission first avoids unauthenticated library work for throttled clients.
  • passkeys.go:430-447: passkey sign-in does not check LockedUntil or the in-memory password-failure lock. User verification makes this a different factor, so it is acceptable, but the design should be documented so password lockout is not assumed to cover passkeys.

Checks verified from source:

  • Passkey removal, enrollment and reauth are guarded by session freshness; impersonation is refused.
  • Credential-to-account binding: the user handle must match the credential's owner; disabled accounts are rejected.
  • Counter and clone handling: the SQL compare-and-set is consistent with the library's CloneWarning logic, and the library rejects backup-eligible flag changes (login.go:403).
  • CSRF is enforced on the new POST routes and forms; redirects go through safeNext.
  • Cascade on removal depends on foreign_keys(1), which open.go sets in the DSN.

Tests and limits:

  • Source-only audit: no repository code was executed and no sandbox was prepared for tests in this run.
  • The PR's claimed results (make test, go vet, go test -race in CI) were not verified by Ada.
  • Not covered: the full test-file contents (only test names and selected expectations were reviewed), go.sum integrity, the generated queries.sql.go beyond consistency with queries.sql, browser behaviour of app.js, live WebAuthn ceremonies with real authenticators, reverse-proxy and X-Forwarded-For deployment configuration, and deployed migration behaviour.
<!-- ada-security-audit request=c707a2224dc26b90a99c7acd9274b94ea76a5b128c979c4ff3991cb71b4d6575 head=510e32269be602cb112feb61e213fadbf2bd1314 --> Ada security audit (source-only, read-only) Initiating source: comment, ID 721, authored by exler (bare audit request on this pull request). Reviewed head SHA: 510e32269be602cb112feb61e213fadbf2bd1314 (base ffbc2896c53f46e0141877666b2b875f82c607e6). Scope: all 18 changed files (+1869/-54) for WebAuthn passkey sign-in and enrollment. Covered: internal/server/passkeys.go; the login, account and session wiring in handlers.go, auth.go, server.go and http.go; internal/db/queries.sql and migrations 0009/0010; internal/db/open.go (foreign keys enabled in the DSN); the login and account templates and app.js; CSRF coverage of the new routes; and the go-webauthn v0.18.2 login flag and counter checks from the local module cache. Source fetched pinned to the head SHA through the API. Verdict: No blocking security findings in reviewed scope Findings (none blocking): 1. Low. Passkey sign-in start can be saturated by unauthenticated clients (availability only). Location: internal/server/passkeys.go:127-135 (per-client limiter refuses new keys once 10000 are live) and 386-406 (global pending-login cap); internal/db/queries.sql:243-246 (AdmitLoginChallenge counts all pending login rows). Prerequisite: a few distinct client addresses. Arithmetic from the source constants, not measured: about 10 sources at 20 starts per minute each fill the 1000-slot cap within the 5-minute challenge TTL. Impact: passkey sign-in starts return 429 for other users during that window. Password plus TOTP sign-in is not affected, and no authentication bypass or data exposure was found. Confidence: high for the mechanism, not reproduced at runtime. Minimal remedy: accept as a documented capacity trade-off, or reserve capacity per client key so one source cannot consume the shared cap. 2. Low. Passkey removal revokes tokens only for sessions that are still live. Location: internal/server/passkeys.go:291-303; compare internal/server/auth.go:266-276 (same pattern for session revocation) and internal/server/oidc.go:29 (tokenTTL = 1 hour). Logout (handlers.go:216) ends the session but does not revoke its tokens. Impact: access tokens or unredeemed codes issued by a passkey session that has already ended are not revoked by removal, and remain valid until expiry, at most 1 hour. This matches the existing logout behaviour rather than a new regression. Confidence: high for the source behaviour. Minimal remedy: either document the bound, or revoke tokens by the passkey link if the stronger guarantee is required. Hardening notes (not findings): - passkeys.go:382-389: the discoverable-login challenge is generated before per-client admission. Moving admission first avoids unauthenticated library work for throttled clients. - passkeys.go:430-447: passkey sign-in does not check LockedUntil or the in-memory password-failure lock. User verification makes this a different factor, so it is acceptable, but the design should be documented so password lockout is not assumed to cover passkeys. Checks verified from source: - Passkey removal, enrollment and reauth are guarded by session freshness; impersonation is refused. - Credential-to-account binding: the user handle must match the credential's owner; disabled accounts are rejected. - Counter and clone handling: the SQL compare-and-set is consistent with the library's CloneWarning logic, and the library rejects backup-eligible flag changes (login.go:403). - CSRF is enforced on the new POST routes and forms; redirects go through safeNext. - Cascade on removal depends on foreign_keys(1), which open.go sets in the DSN. Tests and limits: - Source-only audit: no repository code was executed and no sandbox was prepared for tests in this run. - The PR's claimed results (make test, go vet, go test -race in CI) were not verified by Ada. - Not covered: the full test-file contents (only test names and selected expectations were reviewed), go.sum integrity, the generated queries.sql.go beyond consistency with queries.sql, browser behaviour of app.js, live WebAuthn ceremonies with real authenticators, reverse-proxy and X-Forwarded-For deployment configuration, and deployed migration behaviour.
fara left a comment

Engineering review

  • Target: exler/thermopylae PR #14 "feat(auth): support WebAuthn passkeys" (branch feat/passkeys -> main)
  • Request: exler/thermopylae#14:comment:721 (author exler; body SHA-256 verified; contains @fara)
  • Snapshot: ffbc2896c5 -> 510e32269b (clean detached worktree, one commit ahead of base)
  • Scope: all 18 changed files (+1869/-54). Core: internal/server/passkeys.go, internal/server/auth.go, internal/server/handlers.go, internal/server/admin.go (recovery paths, unchanged but affected), migrations 0009/0010, queries.sql, passkeys_test.go, README, template/JS. Excluded: generated queries.sql.go (checked with sqlc diff, clean) and go.sum (module downloads succeeded).
  • Verdict: Changes requested

Findings

[P1] Admin 2FA reset leaves attacker-enrolled passkeys valid

  • Location: internal/server/admin.go:246-256 (reset-totp, calls endSessions at 252), internal/server/admin.go:59-64 (endSessions deletes sessions and access tokens only), internal/server/admin.go:48-56 (setAdminPassword, same helper). The passkeys table is never touched by admin recovery.
  • Trigger / impact: An attacker who holds a victim's password and TOTP enrolls a passkey via /account/passkeys/*. The admin then runs "Reset 2FA" (templates/admin_user.html:62), whose confirm text promises "Signs this user out everywhere and cancels their access tokens. They enroll a new authenticator at next sign-in." Afterwards the attacker's passkey still produces a full session through /login/passkey/finish, and that session can enroll further passkeys (/account/passkeys/begin only needs a recent reauth, which a passkey ceremony provides via reauth_at). The recovery therefore leaves persistent access that the admin believes was removed. The README (line 53) tells users to recover by "sign in with password and TOTP, then remove" the passkey, which an attacker-held passkey session can also do.
  • Evidence: source-proven, and reproduced with a review-only overlay test (not committed; kept outside the checkout):
    • go test -overlay=<scratch>/overlay.json -count=1 -v -run TestReviewAttackerPasskey ./internal/server/ gives REPRO A: after admin reset-totp, the attacker's passkey still signs in with a full session and can enroll a new passkey (FAIL, exit 1).
    • Password-reset path: the attacker session is gated by MustChangePassword (302 to /account/password), so enrollment is refused with 403. The passkey itself still authenticates, so the credential survives the reset. A second probe (victim's own password change) hit a 403 on my harness and is unverified; I do not rely on it.
  • Minimal remedy: when a user's TOTP or password is reset, also delete that user's passkeys and their passkey-bound sessions/codes/tokens. Keep this in one shared helper used by endSessions/recovery, not a parallel policy. Add a regression test in passkey_test.go that enrolls a passkey, runs reset-totp and the password reset, and asserts the passkey no longer signs in.
  • Confidence: high for the reset-totp repro; the fix is small and local.

Architecture and technical debt

  • Sound: the passkey design is coherent. The challenge is one-use via DELETE ... RETURNING; the login counter is a compare-and-set (UpdatePasskeyUse) that matches the pinned library's UpdateCounter semantics; the cookie-bound login challenge avoids account enumeration; manageGuard blocks impersonation and requires reauth_at within 5 minutes; passkeyDelete ends the sessions and codes tied to the removed credential. Passkey deletion and session cascade run in one transaction.
  • Accepted, documented debt (not blockers): the per-client limiter is in memory and resets on restart; the 20-passkey cap is checked at enrollment start, so concurrent enrollments can exceed it slightly (author noted, reviewed as non-blocking). Both are bounded and recorded in the PR body.
  • Minor, non-blocking (P3): passkeyLoginBegin (passkeys.go:382) builds the WebAuthn assertion before the admission check (:387). A throttled client can still trigger a cheap signing call, but nothing is stored, so this is only wasted CPU.
  • The reset-totp and password-reset flows are the place where new credential types must be added. The PR adds a credential type but does not register it with the existing recovery helper. This is the root cause of the P1 above and the most likely place for drift on the next change.

Verification and limitations

  • git status clean at 510e322; generated code matches (sqlc diff exit 0).
  • go vet ./... exit 0. gofmt -l . exit 0 (no output).
  • go test ./... exit 0: internal/db ok, internal/server ok (61.95s), internal/totp ok.
  • go test -run 'Passkey|PasswordChangeDoesNotRenew' -v ./internal/server/: all 13 passkey tests PASS.
  • go test -race ./... failed locally on every package with ThreadSanitizer: unsupported VMA range (Found 47 - Supported 48) before running tests. This is environmental, and the PR body notes it. Forgejo CI run 34 (pull_request, status Push Docker Image / test (pull_request): success, 2m8s), which runs go test -race ./..., passed for head 510e322. I did not independently reproduce the race run.
  • Review-only overlay repro: TestReviewAttackerPasskeyPersistsAfterAdminTOTPReset fails (REPRO A). The checkout still has no changes after the run (git status clean; overlay file lives under scratch).
  • Not run: real browser or hardware passkey (YubiKey/Bitwarden), live migration on a copy of production data, deployment, and the victim-password-change probe (inconclusive, 403 in my harness).
  • Residual risk: the in-memory limiter and single-instance assumptions are not exercised under multi-instance operation (the README states this constraint).
# Engineering review <!-- fara-source-request: exler/thermopylae#14:comment:721:d3ae7a647a678a1d7ac0fb9da72bcdf7fc2d7a0c1bf0058aedc3ea44b0c4f6f1 --> - Target: exler/thermopylae PR #14 "feat(auth): support WebAuthn passkeys" (branch feat/passkeys -> main) - Request: exler/thermopylae#14:comment:721 (author exler; body SHA-256 verified; contains `@fara`) - Snapshot: ffbc2896c53f46e0141877666b2b875f82c607e6 -> 510e32269be602cb112feb61e213fadbf2bd1314 (clean detached worktree, one commit ahead of base) - Scope: all 18 changed files (+1869/-54). Core: `internal/server/passkeys.go`, `internal/server/auth.go`, `internal/server/handlers.go`, `internal/server/admin.go` (recovery paths, unchanged but affected), migrations 0009/0010, `queries.sql`, `passkeys_test.go`, README, template/JS. Excluded: generated `queries.sql.go` (checked with `sqlc diff`, clean) and `go.sum` (module downloads succeeded). - Verdict: **Changes requested** ## Findings ### [P1] Admin 2FA reset leaves attacker-enrolled passkeys valid - Location: `internal/server/admin.go:246-256` (`reset-totp`, calls `endSessions` at 252), `internal/server/admin.go:59-64` (`endSessions` deletes sessions and access tokens only), `internal/server/admin.go:48-56` (`setAdminPassword`, same helper). The `passkeys` table is never touched by admin recovery. - Trigger / impact: An attacker who holds a victim's password and TOTP enrolls a passkey via `/account/passkeys/*`. The admin then runs "Reset 2FA" (`templates/admin_user.html:62`), whose confirm text promises "Signs this user out everywhere and cancels their access tokens. They enroll a new authenticator at next sign-in." Afterwards the attacker's passkey still produces a `full` session through `/login/passkey/finish`, and that session can enroll further passkeys (`/account/passkeys/begin` only needs a recent reauth, which a passkey ceremony provides via `reauth_at`). The recovery therefore leaves persistent access that the admin believes was removed. The README (line 53) tells users to recover by "sign in with password and TOTP, then remove" the passkey, which an attacker-held passkey session can also do. - Evidence: source-proven, and reproduced with a review-only overlay test (not committed; kept outside the checkout): - `go test -overlay=<scratch>/overlay.json -count=1 -v -run TestReviewAttackerPasskey ./internal/server/` gives `REPRO A: after admin reset-totp, the attacker's passkey still signs in with a full session and can enroll a new passkey` (FAIL, exit 1). - Password-reset path: the attacker session is gated by `MustChangePassword` (302 to `/account/password`), so enrollment is refused with 403. The passkey itself still authenticates, so the credential survives the reset. A second probe (victim's own password change) hit a 403 on my harness and is unverified; I do not rely on it. - Minimal remedy: when a user's TOTP or password is reset, also delete that user's passkeys and their passkey-bound sessions/codes/tokens. Keep this in one shared helper used by `endSessions`/recovery, not a parallel policy. Add a regression test in `passkey_test.go` that enrolls a passkey, runs `reset-totp` and the password reset, and asserts the passkey no longer signs in. - Confidence: high for the reset-totp repro; the fix is small and local. ## Architecture and technical debt - Sound: the passkey design is coherent. The challenge is one-use via `DELETE ... RETURNING`; the login counter is a compare-and-set (`UpdatePasskeyUse`) that matches the pinned library's `UpdateCounter` semantics; the cookie-bound login challenge avoids account enumeration; `manageGuard` blocks impersonation and requires `reauth_at` within 5 minutes; `passkeyDelete` ends the sessions and codes tied to the removed credential. Passkey deletion and session cascade run in one transaction. - Accepted, documented debt (not blockers): the per-client limiter is in memory and resets on restart; the 20-passkey cap is checked at enrollment start, so concurrent enrollments can exceed it slightly (author noted, reviewed as non-blocking). Both are bounded and recorded in the PR body. - Minor, non-blocking (P3): `passkeyLoginBegin` (`passkeys.go:382`) builds the WebAuthn assertion before the admission check (`:387`). A throttled client can still trigger a cheap signing call, but nothing is stored, so this is only wasted CPU. - The `reset-totp` and password-reset flows are the place where new credential types must be added. The PR adds a credential type but does not register it with the existing recovery helper. This is the root cause of the P1 above and the most likely place for drift on the next change. ## Verification and limitations - `git status` clean at `510e322`; generated code matches (`sqlc diff` exit 0). - `go vet ./...` exit 0. `gofmt -l .` exit 0 (no output). - `go test ./...` exit 0: `internal/db` ok, `internal/server` ok (61.95s), `internal/totp` ok. - `go test -run 'Passkey|PasswordChangeDoesNotRenew' -v ./internal/server/`: all 13 passkey tests PASS. - `go test -race ./...` failed locally on every package with `ThreadSanitizer: unsupported VMA range (Found 47 - Supported 48)` before running tests. This is environmental, and the PR body notes it. Forgejo CI run 34 (`pull_request`, status `Push Docker Image / test (pull_request)`: success, 2m8s), which runs `go test -race ./...`, passed for head `510e322`. I did not independently reproduce the race run. - Review-only overlay repro: `TestReviewAttackerPasskeyPersistsAfterAdminTOTPReset` fails (REPRO A). The checkout still has no changes after the run (`git status` clean; overlay file lives under scratch). - Not run: real browser or hardware passkey (YubiKey/Bitwarden), live migration on a copy of production data, deployment, and the victim-password-change probe (inconclusive, 403 in my harness). - Residual risk: the in-memory limiter and single-instance assumptions are not exercised under multi-instance operation (the README states this constraint).
fix(passkeys): check rate limits before making any assertions
All checks were successful
Push Docker Image / test (pull_request) Successful in 2m3s
Push Docker Image / push (pull_request) Has been skipped
8423f6eb7b
exler merged commit b2c9546d1d into main 2026-11-14 10:57:48 +01:00
exler deleted branch feat/passkeys 2026-11-14 10:57:48 +01:00
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
4 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
exler/thermopylae!14
No description provided.