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
fix reset MFA when removing existing selected
Thiago Araujo da Silva2c249c6b3e09
diff --git a/backend/src/services/mfa-recovery-code/mfa-recovery-code-service.ts b/backend/src/services/mfa-recovery-code/mfa-recovery-code-service.tsindex d74f705d98..5bda4214ab 100644--- a/backend/src/services/mfa-recovery-code/mfa-recovery-code-service.ts+++ b/backend/src/services/mfa-recovery-code/mfa-recovery-code-service.ts@@ -70,7 +70,10 @@ export const mfaRecoveryCodeServiceFactory = ({});}- const recoveryCodes = decryptWithRoot(recoveryCodeConfig.encryptedRecoveryCodes).toString().split(",");+ const recoveryCodes = decryptWithRoot(recoveryCodeConfig.encryptedRecoveryCodes)+ .toString()+ .split(",")+ .filter(Boolean);const remainingRecoveryCodes = recoveryCodes.filter((code) => code !== recoveryCode);if (remainingRecoveryCodes.length === recoveryCodes.length) {throw new ForbiddenRequestError({diff --git a/backend/src/services/totp/totp-service.ts b/backend/src/services/totp/totp-service.tsindex b8e9b98dc0..e0a9231893 100644--- a/backend/src/services/totp/totp-service.ts+++ b/backend/src/services/totp/totp-service.ts@@ -3,6 +3,7 @@ import { authenticator } from "otplib";import { KeyStorePrefixes, KeyStoreTtls, TKeyStoreFactory } from "@app/keystore/keystore";import { BadRequestError, ForbiddenRequestError, NotFoundError } from "@app/lib/errors";+import { MfaMethod } from "../auth/auth-type";import { TKmsServiceFactory } from "../kms/kms-service";import { TUserDALFactory } from "../user/user-dal";import { TTotpConfigDALFactory } from "./totp-config-dal";@@ -188,6 +189,15 @@ export const totpServiceFactory = ({ totpConfigDAL, kmsService, userDAL, keyStor}await totpConfigDAL.deleteById(totpConfig.id);++ // If the user's preferred method is the TOTP config we just removed, reset it+ // so getRequiredMfaMethod doesn't keep challenging for a method that no longer+ // has a verified config, which would force the user back into enrollment. Fall+ // back to email, which is always available.+ const user = await userDAL.findById(userId);+ if (user?.selectedMfaMethod === MfaMethod.TOTP) {+ await userDAL.updateById(userId, { selectedMfaMethod: MfaMethod.EMAIL });+ }};return {diff --git a/backend/src/services/webauthn/webauthn-service.ts b/backend/src/services/webauthn/webauthn-service.tsindex 8648d17862..a457584cb2 100644--- a/backend/src/services/webauthn/webauthn-service.ts+++ b/backend/src/services/webauthn/webauthn-service.ts@@ -12,6 +12,7 @@ import { KeyStorePrefixes, KeyStoreTtls, TKeyStoreFactory } from "@app/keystore/import { getConfig } from "@app/lib/config/env";import { BadRequestError, ForbiddenRequestError, NotFoundError } from "@app/lib/errors";+import { MfaMethod } from "../auth/auth-type";import { TAuthTokenServiceFactory } from "../auth-token/auth-token-service";import { TokenType } from "../auth-token/auth-token-types";import { TUserDALFactory } from "../user/user-dal";@@ -339,6 +340,18 @@ export const webAuthnServiceFactory = ({await webAuthnCredentialDAL.deleteById(credential.id);+ // If that was the user's last credential and webauthn is their preferred+ // method, reset the preference so getRequiredMfaMethod doesn't keep+ // challenging for webauthn with no credential left, which would lock the+ // user out. Fall back to email, which is always available.+ const remainingCredentials = await webAuthnCredentialDAL.find({ userId });+ if (remainingCredentials.length === 0) {+ const user = await userDAL.findById(userId);+ if (user?.selectedMfaMethod === MfaMethod.WEBAUTHN) {+ await userDAL.updateById(userId, { selectedMfaMethod: MfaMethod.EMAIL });+ }+ }+return {success: true};diff --git a/frontend/src/components/mfa/MfaEnrollment.tsx b/frontend/src/components/mfa/MfaEnrollment.tsxindex 2e6a495c10..25630f1ae2 100644--- a/frontend/src/components/mfa/MfaEnrollment.tsx+++ b/frontend/src/components/mfa/MfaEnrollment.tsx@@ -35,7 +35,9 @@ export const MfaEnrollment = ({ method, onComplete }: Props) => {const prepare = async () => {// The enrollment endpoints authenticate with the regular access token, so- // clear the temporary MFA token first.+ // clear the temporary MFA token first. Capture it so we can restore it if+ // the request fails and the user needs to fall back to the login flow.+ const mfaTempToken = getMfaTempToken();SecurityClient.setMfaToken("");try {// Enabling is the single place recovery codes are minted; idempotent if@@ -43,6 +45,9 @@ export const MfaEnrollment = ({ method, onComplete }: Props) => {await updateUserMfa({ isMfaEnabled: true });setPhase("verify");} catch (error: any) {+ // Restore the temp token so the user can reload and retry the normal+ // login flow instead of being stuck on the preparing screen.+ SecurityClient.setMfaToken(mfaTempToken);createNotification({text: error?.response?.data?.message || "Failed to start two-factor setup",type: "error"