Skip to content

Open-source personal finance manager. Self-hosted, privacy-first.

fix(login): don't spend a passkey challenge after an aborted ceremony (#892)

Albert Dolkun79d214c7412b
diff --git a/frontend/src/pages/login.test.tsx b/frontend/src/pages/login.test.tsx
index ccdca82a..38037cdf 100644
--- a/frontend/src/pages/login.test.tsx
+++ b/frontend/src/pages/login.test.tsx
@@ -21,7 +21,11 @@ vi.mock('@/contexts/auth-context', () => ({ useAuth: () => authContext }))
const api = vi.hoisted(() => ({
setup: { status: vi.fn() },
- auth: { oidcConfig: vi.fn() },
+ auth: {
+ oidcConfig: vi.fn(),
+ passkeyAuthenticationOptions: vi.fn(),
+ verifyPasskeyAuthentication: vi.fn(),
+ },
admin: { registrationStatus: vi.fn(), defaultColors: vi.fn() },
}))
vi.mock('@/lib/api', () => ({
@@ -30,11 +34,14 @@ vi.mock('@/lib/api', () => ({
admin: api.admin,
}))
-vi.mock('@/lib/webauthn', () => ({
- isPasskeySupported: () => false,
- passkeyFailure: () => 'unknown',
+const webauthn = vi.hoisted(() => ({
+ isPasskeySupported: vi.fn(),
+ isConditionalPasskeySupported: vi.fn(),
+ passkeyFailure: vi.fn(),
startPasskeyAuthentication: vi.fn(),
+ startConditionalPasskeyAuthentication: vi.fn(),
}))
+vi.mock('@/lib/webauthn', () => webauthn)
vi.mock('next-themes', () => ({ useTheme: () => ({ resolvedTheme: 'dark' }) }))
@@ -56,6 +63,9 @@ beforeEach(() => {
})
api.admin.registrationStatus.mockResolvedValue({ enabled: true })
api.admin.defaultColors.mockResolvedValue({ light: null, dark: null })
+ webauthn.isPasskeySupported.mockReturnValue(false)
+ webauthn.isConditionalPasskeySupported.mockResolvedValue(false)
+ webauthn.passkeyFailure.mockReturnValue('unknown')
})
async function renderLogin() {
@@ -189,6 +199,36 @@ describe('LoginPage', () => {
).toBeInTheDocument()
})
+ it('does not spend the challenge when the conditional ceremony is aborted', async () => {
+ // The challenge is one-shot on the server (Redis `getdel`). Verifying one
+ // the user walked away from burns it, and the next attempt gets nothing.
+ webauthn.isPasskeySupported.mockReturnValue(true)
+ webauthn.isConditionalPasskeySupported.mockResolvedValue(true)
+ api.auth.passkeyAuthenticationOptions.mockResolvedValue({
+ options: {},
+ challenge_id: 'challenge-1',
+ })
+ let handBackCredential: (credential: unknown) => void = () => {}
+ webauthn.startConditionalPasskeyAuthentication.mockReturnValue(
+ new Promise((resolve) => {
+ handBackCredential = resolve
+ }),
+ )
+
+ const { unmount } = renderWithProviders(<LoginPage />, { route: '/login' })
+ await waitFor(() =>
+ expect(webauthn.startConditionalPasskeyAuthentication).toHaveBeenCalled(),
+ )
+
+ // Leaving the page aborts the ceremony, but the pending browser promise
+ // can still settle afterwards.
+ unmount()
+ handBackCredential({ id: 'credential' })
+ await new Promise((resolve) => setTimeout(resolve, 0))
+
+ expect(api.auth.verifyPasskeyAuthentication).not.toHaveBeenCalled()
+ })
+
it('survives the optional config calls failing', async () => {
// A reverse proxy that blocks /api/admin must not blank the login form.
api.admin.registrationStatus.mockRejectedValue(new Error('403'))
diff --git a/frontend/src/pages/login.tsx b/frontend/src/pages/login.tsx
index 9e4e4bb6..73fcf637 100644
--- a/frontend/src/pages/login.tsx
+++ b/frontend/src/pages/login.tsx
@@ -49,7 +49,7 @@ export default function LoginPage() {
const [error, setError] = useState('')
const [isLoading, setIsLoading] = useState(false)
const [isPasskeyLoading, setIsPasskeyLoading] = useState(false)
- const [passkeySupported, setPasskeySupported] = useState(false)
+ const [passkeySupported] = useState(isPasskeySupported)
const [registrationEnabled, setRegistrationEnabled] = useState(true)
const [oidcConfig, setOidcConfig] = useState<OIDCConfig | null>(null)
const [oidcConfigFailed, setOidcConfigFailed] = useState(false)
@@ -70,7 +70,6 @@ export default function LoginPage() {
const showAuthDivider = localAuthEnabled && (showPasskeyLogin || oidcEnabled)
useEffect(() => {
- setPasskeySupported(isPasskeySupported())
if (token) {
navigate('/', { replace: true })
return
@@ -136,6 +135,10 @@ export default function LoginPage() {
options.options,
abortController.signal,
)
+ // A challenge is one-shot server-side: verifying one the user already
+ // walked away from burns it and answers a ceremony nobody is watching.
+ if (abortController.signal.aborted) return
+
const result = await authApi.verifyPasskeyAuthentication(options.challenge_id, credential)
if (abortController.signal.aborted) return
@@ -416,7 +419,11 @@ export default function LoginPage() {
{localAuthEnabled && (
<CardContent className="space-y-4 px-8 pt-4">
{error && (
- <div className="p-3 text-sm text-destructive bg-destructive/10 rounded-lg">
+ <div
+ id="login-error"
+ role="alert"
+ className="p-3 text-sm text-destructive bg-destructive/10 rounded-lg"
+ >
{error}
</div>
)}
@@ -426,9 +433,13 @@ export default function LoginPage() {
id="email"
type="email"
value={email}
- onChange={(e) => setEmail(e.target.value)}
+ onChange={(e) => {
+ setEmail(e.target.value)
+ setError('')
+ }}
placeholder="[email protected]"
autoComplete="username webauthn"
+ aria-describedby={error ? 'login-error' : undefined}
required
/>
</div>