Skip to content

armgit / umami

Public Imported from GitHub
0

Umami is a privacy-first web and product analytics platform. Traffic, campaigns, behavior, conversions, and revenue in one place. No cookies, no surveillance, self-hosted or in the cloud.

fix(2FA): backup code double-use race condition

Rudy B.3e165a8c6fea
diff --git a/src/app/api/2fa/verify/route.ts b/src/app/api/2fa/verify/route.ts
index 636af3caa..eea621b2b 100644
--- a/src/app/api/2fa/verify/route.ts
+++ b/src/app/api/2fa/verify/route.ts
@@ -81,10 +81,20 @@ export async function POST(request: Request) {
});
}
- await prisma.client.twoFactorBackupCode.update({
- where: { id: unusedCodes[matchIndex].id },
+ const consumed = await prisma.client.twoFactorBackupCode.updateMany({
+ where: { id: unusedCodes[matchIndex].id, used: false },
data: { used: true },
});
+
+ if (consumed.count === 0) {
+ const { lockedUntil } = await recordFailedAttempt(userId);
+ return badRequest({
+ code: 'two-factor-error-invalid-backup-code',
+ message: 'Invalid backup code',
+ ...(lockedUntil && { lockedUntil }),
+ });
+ }
+
await resetRateLimit(userId);
} else {
const { token } = body;