feat: 2fa backup codes#9
Conversation
Co-authored-by: Peer Richelsen <peeroke@gmail.com>
|
@claude review this PR |
1 similar comment
|
@claude review this PR |
|
Claude finished @akshayutture-augment's task —— View job PR Review: feat: 2fa backup codesI've completed a comprehensive review of this PR that adds backup code functionality to the two-factor authentication system. Here are my findings: 🔒 Critical Security Issues1. Timing Attack Vulnerability in Backup Code ValidationLocation: const index = backupCodes.indexOf(credentials.backupCode.replaceAll("-", ""));
if (index === -1) throw new Error(ErrorCode.IncorrectBackupCode);Issue: Using Recommendation: Use constant-time comparison: import { timingSafeEqual } from 'crypto';
// Compare each code in constant time
let foundIndex = -1;
const inputCode = credentials.backupCode.replaceAll("-", "");
const inputBuffer = Buffer.from(inputCode);
for (let i = 0; i < backupCodes.length; i++) {
if (backupCodes[i] && backupCodes[i].length === inputCode.length) {
const codeBuffer = Buffer.from(backupCodes[i]);
if (timingSafeEqual(inputBuffer, codeBuffer)) {
foundIndex = i;
break;
}
}
}2. Backup Code Deletion Logic is FlawedLocation: backupCodes[index] = null;Issue: Setting to
Recommendation: Either remove the element entirely or replace with a secure random value: backupCodes.splice(index, 1); // Remove completely
// OR
backupCodes[index] = crypto.randomBytes(5).toString("hex"); // Replace with random3. Missing Rate Limiting on Backup Code AttemptsLocation: Issue: While TOTP codes have natural rate limiting (they rotate), backup codes don't. An attacker could potentially brute-force backup codes since:
Recommendation: Add rate limiting specifically for backup code attempts: await checkRateLimitAndThrowError({
identifier: `backup-code:${user.email}`,
rateLimitingType: "strict" // More restrictive than normal login
});
|
| Category | Critical | High | Medium | Low |
|---|---|---|---|---|
| Security | 3 | 3 | 0 | 0 |
| Bugs | 0 | 2 | 1 | 0 |
| Code Quality | 0 | 0 | 3 | 0 |
| Testing | 0 | 1 | 1 | 0 |
| Documentation | 0 | 0 | 2 | 0 |
| Total | 3 | 6 | 7 | 0 |
🎯 Recommendations Before Merge
Must Fix (Blocking):
- ✋ Fix timing attack vulnerability in backup code comparison
- ✋ Add rate limiting for backup code attempts
- ✋ Fix backup code deletion logic (null vs removal)
- ✋ Fix backup code display flow (show before enabling 2FA)
Should Fix (High Priority):
- Implement complete E2E tests for backup code login
- Fix memory leak with blob URL
- Add backup code regeneration feature
- Improve user warnings about secure storage
Nice to Have:
- Extract magic numbers to constants
- Add input validation patterns
- Create backup code utility functions
- Add unit tests for crypto operations
🔗 Related Files Reviewed
packages/prisma/schema.prisma:205- DB schemaapps/web/pages/api/auth/two-factor/totp/setup.ts:61- Code generationapps/web/pages/api/auth/two-factor/totp/disable.ts:48-65- Disable with backup codepackages/features/auth/lib/next-auth-options.ts:131-156- Login with backup codeapps/web/components/auth/BackupCode.tsx- UI componentapps/web/components/settings/EnableTwoFactorModal.tsx- Setup flow
|
This PR is being marked as stale due to inactivity. |
Test 3
Replicated from ai-code-review-evaluation/cal.com-greptile#3