GLM-5.3 Flash · 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
9
Comments matching a bug
3
Comments not matching
6
Cost of this PR
$0.186

Published comments (9)

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

Backup-code login bypasses password verification — A leaked backup code alone then grants full account access, since the password check is skipped when a `backupCode` is posted with any dummy `totpCode`. Verify `credentials.password` inside the backup-code branch before authenticating.

mediumbugapps/web/components/settings/DisableTwoFactorModal.tsx:113-121

twoFactorLostAccess state persists after modal is closed or cancelled — Reopening the modal then shows the backup-code screen with stale `backupCode`/`totpCode` values instead of the default TOTP view. Reset `twoFactorLostAccess` and the form values when the modal closes or opens.

mediumbugapps/web/pages/api/auth/two-factor/totp/disable.ts:58-64

Non-string backupCode throws TypeError instead of returning 400 — A truthy non-string `backupCode` (number, object, array) passes the guard and `replaceAll` throws an unhandled TypeError, surfacing as a generic 500. Check `typeof req.body.backupCode === "string"` and return the 400 `IncorrectBackupCode` response otherwise.

mediumbugapps/web/playwright/login.2fa.e2e.ts:50-53

2FA switch assertion passes a Promise to expect and never fails — The unawaited Promise from `isChecked()` is always truthy, so the assertion can never fail and this test proves nothing about the 2FA switch. Await `expect` on the `[data-state="checked"]` locator or use `toBeChecked()` instead.

mediumbugapps/web/playwright/login.2fa.e2e.ts:124-127

Closing backup code dialog asserts nothing, test passes if dialog stays open — A broken close button would go unnoticed, because the follow-up switch assertion still passes behind an open modal and the next step discards the dialog. After the click, assert the dialog is gone, e.g. `toBeHidden()` on `backup-codes-close`.

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

Dialog close via ESC/overlay skips onEnable() and resetState(), leaving stale 2FA state — ESC or overlay clicks skip both `onEnable()` and `resetState()`, leaving `viewer.me` stale in `two-factor-auth.tsx` so 2FA still shows disabled and the modal reopens on backup codes. Trigger `onEnable()` from the dialog's close path too, not just the Close button.

mediumbugpackages/features/auth/lib/next-auth-options.ts:143-156

Backup-code consumption is a non-atomic read-modify-write race — Two concurrent logins can both pass the `indexOf` check, leaving a consumed code valid or letting the same one authenticate twice. Make the write conditional — `updateMany` matching the previously read `backupCodes` — and fail when no rows change.

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

Backup code comparison is case-sensitive while stored codes are lowercase hex — Stored codes are lowercase hex from `crypto.randomBytes(5).toString("hex")`, so a valid code typed with uppercase is wrongly rejected with `IncorrectBackupCode`. Compare both sides lowercased, here and in `disable.ts`.

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

Backup-code comparison is not constant-time, leaking match info via timing — Response times reveal how many leading characters of a submitted `backupCode` match a stored one, which is enough to reconstruct a code that together with the password authenticates the account. Compare hashes with `crypto.timingSafeEqual` over equal-length buffers instead of `indexOf`. **Also found in:** - `apps/web/pages/api/auth/two-factor/totp/disable.ts:58-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.