Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.
TypeScript acmeagentsai-agentscertificate-managementdevsecopsenvironment-variableskubernetesopen-sourcepampkipostgresprivate-caprivileged-access-managementsecret-managementsecret-managersecretssecrets-managementsecuritysecurity-toolsvault
implement MFA unlock email handling with keystore lockout mechanism
Victor Santosf81a59380d4c
diff --git a/backend/src/keystore/keystore.ts b/backend/src/keystore/keystore.tsindex 45b633de93..e0fe0b1883 100644--- a/backend/src/keystore/keystore.ts+++ b/backend/src/keystore/keystore.ts@@ -83,6 +83,8 @@ export const KeyStorePrefixes = {PkiAcmeNonce: (nonce: string) => `pki-acme-nonce:${nonce}` as const,MfaSession: (mfaSessionId: string) => `mfa-session:${mfaSessionId}` as const,WebAuthnChallenge: (userId: string) => `webauthn-challenge:${userId}` as const,+ UserMfaLockoutLock: (userId: string) => `user-mfa-lockout-lock:${userId}` as const,+ UserMfaUnlockEmailSent: (userId: string) => `user-mfa-unlock-email-sent:${userId}` as const,AiMcpServerOAuth: (sessionId: string) => `ai-mcp-server-oauth:${sessionId}` as const,diff --git a/backend/src/server/routes/index.ts b/backend/src/server/routes/index.tsindex c3a3b4bee5..aadd84c835 100644--- a/backend/src/server/routes/index.ts+++ b/backend/src/server/routes/index.ts@@ -942,7 +942,8 @@ export const registerRoutes = async (auditLogService,notificationService,membershipRoleDAL,- membershipUserDAL+ membershipUserDAL,+ keyStore});const passwordService = authPaswordServiceFactory({tokenService,diff --git a/backend/src/services/auth/auth-login-service.ts b/backend/src/services/auth/auth-login-service.tsindex 7b34c67f23..64ce7bc883 100644--- a/backend/src/services/auth/auth-login-service.ts+++ b/backend/src/services/auth/auth-login-service.ts@@ -10,6 +10,7 @@ import {} from "@app/db/schemas";import { EventType, TAuditLogServiceFactory } from "@app/ee/services/audit-log/audit-log-types";import { isAuthMethodSaml } from "@app/ee/services/permission/permission-fns";+import { KeyStorePrefixes, TKeyStoreFactory } from "@app/keystore/keystore";import { getConfig } from "@app/lib/config/env";import { crypto, generateSrpServerKey, srpCheckClientProof } from "@app/lib/crypto";import { getUserPrivateKey } from "@app/lib/crypto/srp";@@ -66,6 +67,7 @@ type TAuthLoginServiceFactoryDep = {membershipUserDAL: TMembershipUserDALFactory;membershipRoleDAL: TMembershipRoleDALFactory;notificationService: Pick<TNotificationServiceFactory, "createUserNotifications">;+ keyStore: Pick<TKeyStoreFactory, "acquireLock" | "setItemWithExpiry" | "getItem">;};export type TAuthLoginFactory = ReturnType<typeof authLoginServiceFactory>;@@ -78,7 +80,8 @@ export const authLoginServiceFactory = ({auditLogService,notificationService,membershipUserDAL,- membershipRoleDAL+ membershipRoleDAL,+ keyStore}: TAuthLoginServiceFactoryDep) => {/** Private@@ -899,20 +902,41 @@ export const authLoginServiceFactory = ({const updatedUser = await processFailedMfaAttempt(userId);if (updatedUser.isLocked) {if (updatedUser.email) {- const unlockToken = await tokenService.createTokenForUser({- type: TokenType.TOKEN_USER_UNLOCK,- userId: updatedUser.id- });+ // Use a keystore lock to prevent sending duplicate unlock emails during concurrent requests+ let lock: Awaited<ReturnType<typeof keyStore.acquireLock>> | undefined;+ try {+ lock = await keyStore.acquireLock([KeyStorePrefixes.UserMfaLockoutLock(userId)], 3000, {+ retryCount: 0+ });- await smtpService.sendMail({- template: SmtpTemplates.UnlockAccount,- subjectLine: "Unlock your Infisical account",- recipients: [updatedUser.email],- substitutions: {- token: unlockToken,- callback_url: `${appCfg.SITE_URL}/api/v1/user/${updatedUser.id}/unlock`+ // Check if an unlock email was already sent recently (within 5 minutes)+ const emailAlreadySent = await keyStore.getItem(KeyStorePrefixes.UserMfaUnlockEmailSent(userId));+ if (!emailAlreadySent) {+ const unlockToken = await tokenService.createTokenForUser({+ type: TokenType.TOKEN_USER_UNLOCK,+ userId: updatedUser.id+ });++ await smtpService.sendMail({+ template: SmtpTemplates.UnlockAccount,+ subjectLine: "Unlock your Infisical account",+ recipients: [updatedUser.email],+ substitutions: {+ token: unlockToken,+ callback_url: `${appCfg.SITE_URL}/api/v1/user/${updatedUser.id}/unlock`+ }+ });++ // Mark that an unlock email was sent, expires after 5 minutes+ await keyStore.setItemWithExpiry(KeyStorePrefixes.UserMfaUnlockEmailSent(userId), 300, "1");}- });+ } catch {+ // This is expected when another request is handling the email. The acquireLock will fail.+ } finally {+ if (lock) {+ await lock.release();+ }+ }}}