Amazon Ergo 1.0 · Heavy

feat-2fa-backup-codes-cal-com

cal.com · TypeScript · M · 100–299 lines · 6 files

The generalist plus 9 specialist lens agents in parallel, then the same verify and dedup steps. Judge: Claude Opus 5.5.

Gold bugs found
3 of 8
Comments published
5
Comments matching a bug
3
Comments not matching
2
Cost of this PR
$3.818

Published comments (5)

highbugpackages/features/auth/lib/next-auth-options.ts:131-156

Backup-code redemption is a non-atomic read-modify-write — Concurrent logins can redeem the same backup code twice or overwrite each other's updates, resurrecting a consumed code. Replace the unconditional `prisma.user.update` with an atomic conditional update or transaction that confirms the stored `user.backupCodes` ciphertext is unchanged and checks the affected-row count.

highsecuritypackages/features/auth/lib/next-auth-options.ts:128-145

Backup code login bypasses password verification when totpCode is present — A valid `credentials.backupCode` plus any non-empty `credentials.totpCode` skips password verification, letting the backup code replace the password instead of serving as a second factor. Require the password whenever `user.password` exists, or at minimum whenever `credentials.backupCode` is used, before accepting it.

mediumbugapps/web/pages/auth/login.tsx:158

Login shows settings-oriented backup-code guidance to locked-out users — Unauthenticated users cannot generate codes in settings, so `missing_backup_codes` leaves them without an actionable recovery path. Use login-specific guidance, such as authenticating with an authenticator code or contacting an administrator.

mediumbugapps/web/components/settings/EnableTwoFactorModal.tsx:134-136

onEnable is skipped when the 2FA dialog is dismissed after enabling — If `onOpenChange` dismisses the dialog after server-side enablement, `onEnable` never runs, leaving the parent stale and 2FA apparently disabled. Call it on successful enablement or on every close path from the backup-codes step, not just the Close button. **Also found in:** - `apps/web/components/settings/EnableTwoFactorModal.tsx:132-135`

mediumsecuritypackages/features/auth/lib/next-auth-options.ts:144-145

Backup codes verified with short-circuiting indexOf (timing side channel) — Verification time depends on the matched code's position and shared prefix, leaking information about these authentication credentials. Replace `backupCodes.indexOf` with SHA-256 digests compared via `crypto.timingSafeEqual`, scanning every entry without early exit. **Also found in:** - `apps/web/pages/api/auth/two-factor/totp/disable.ts:61-64`

Gold bugs found (3)

criticalThe credentials authorize() skips the password check when any totpCode is present, and the new backup-code branch never validates that totpCode, so a backup code plus a dummy totpCode logs in without the account password.
mediumAfter enabling 2FA, onEnable is only called from the backup-codes Close button; dismissing the dialog by Escape/overlay never notifies the parent, so the settings page keeps showing 2FA disabled.
highBackup-code consumption is a non-atomic read-modify-write, so concurrent logins can accept the same backup code more than once or overwrite each other's removal.

Gold bugs missed (5)

mediumThe backup-code copy e2e test only waits for the success toast, which is shown unconditionally, so it never verifies the clipboard content.
highThe hand-built `mockUser: User` fixture in inviteMemberUtils.test.ts lacks the new backupCodes field, so it no longer satisfies the Prisma User type and type-checking that file fails.
lowThe backup-code field became a PasswordField but keeps an explicit type="password" prop that overrides the internal visibility state, so the show/hide toggle never unmasks the input.
mediumEnableTwoFactorModal resets its step only from the footer buttons; dismissing the new backup-codes step via Escape or overlay leaves step at DisplayBackupCodes (and skips onEnable, so the settings switch is not refreshed), so reopening the enable modal shows the old backup-codes screen instead of the password step.
mediumIn lost-access mode the backup-code field's native required/length attributes are inert because the login form uses noValidate and has no RHF/zod rule for backupCode; submitting an empty code returns SecondFactorRequired, which the client handles silently, so the user gets no feedback.