Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

run flip inside transaction

Thiago Araujo da Silvada180eb7e932
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.ts
index 084f0808e2..e9c3d1508c 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
@@ -1,3 +1,5 @@
+import { Knex } from "knex";
+
import { BadRequestError, ForbiddenRequestError, NotFoundError } from "@app/lib/errors";
import { TKmsServiceFactory } from "../kms/kms-service";
@@ -92,8 +94,8 @@ export const mfaRecoveryCodeServiceFactory = ({
* ones. Returns the fresh codes so the caller can display them once. If no
* pool exists yet it is created.
*/
- const rotateRecoveryCodes = async ({ userId }: TRotateRecoveryCodesDTO) => {
- const user = await userDAL.findById(userId);
+ const rotateRecoveryCodes = async ({ userId, tx: externalTx }: TRotateRecoveryCodesDTO) => {
+ const user = await userDAL.findById(userId, externalTx);
if (!user?.isMfaEnabled) {
throw new BadRequestError({
message: "Cannot regenerate recovery codes: MFA is not enabled for this account"
@@ -102,7 +104,7 @@ export const mfaRecoveryCodeServiceFactory = ({
const { recoveryCodes, encryptedRecoveryCodes } = generateEncryptedRecoveryCodes();
- await mfaRecoveryCodeDAL.transaction(async (tx) => {
+ const upsert = async (tx: Knex) => {
const recoveryCodeConfig = await mfaRecoveryCodeDAL.findOne({ userId }, tx);
if (recoveryCodeConfig) {
@@ -110,13 +112,19 @@ export const mfaRecoveryCodeServiceFactory = ({
} else {
await mfaRecoveryCodeDAL.create({ userId, encryptedRecoveryCodes }, tx);
}
- });
+ };
+
+ if (externalTx) {
+ await upsert(externalTx);
+ } else {
+ await mfaRecoveryCodeDAL.transaction(upsert);
+ }
return recoveryCodes;
};
- const deleteRecoveryCodes = async ({ userId }: TDeleteRecoveryCodesDTO) => {
- await mfaRecoveryCodeDAL.delete({ userId });
+ const deleteRecoveryCodes = async ({ userId, tx }: TDeleteRecoveryCodesDTO) => {
+ await mfaRecoveryCodeDAL.delete({ userId }, tx);
};
return {
diff --git a/backend/src/services/mfa-recovery-code/mfa-recovery-code-types.ts b/backend/src/services/mfa-recovery-code/mfa-recovery-code-types.ts
index 3403ea99fc..f7f3c4914c 100644
--- a/backend/src/services/mfa-recovery-code/mfa-recovery-code-types.ts
+++ b/backend/src/services/mfa-recovery-code/mfa-recovery-code-types.ts
@@ -1,3 +1,5 @@
+import { Knex } from "knex";
+
export type TGetRecoveryCodesDTO = {
userId: string;
};
@@ -9,8 +11,10 @@ export type TVerifyAndConsumeRecoveryCodeDTO = {
export type TRotateRecoveryCodesDTO = {
userId: string;
+ tx?: Knex;
};
export type TDeleteRecoveryCodesDTO = {
userId: string;
+ tx?: Knex;
};
diff --git a/backend/src/services/user/user-service.ts b/backend/src/services/user/user-service.ts
index fd308d0b97..5b1df742d0 100644
--- a/backend/src/services/user/user-service.ts
+++ b/backend/src/services/user/user-service.ts
@@ -132,28 +132,34 @@ export const userServiceFactory = ({
}
}
- let mfaMethods;
+ let mfaMethods: string[] | undefined;
if (isMfaEnabled === undefined) {
mfaMethods = undefined;
} else {
mfaMethods = isMfaEnabled ? ["email"] : [];
}
- const updatedUser = await userDAL.updateById(userId, {
- isMfaEnabled,
- mfaMethods,
- selectedMfaMethod
- });
+ const updatedUser = await userDAL.transaction(async (tx) => {
+ const user2fa = await userDAL.updateById(
+ userId,
+ {
+ isMfaEnabled,
+ mfaMethods,
+ selectedMfaMethod
+ },
+ tx
+ );
- if (isMfaEnabled === true && !wasMfaEnabled) {
- await mfaRecoveryCodeService.rotateRecoveryCodes({ userId });
- } else if (isMfaEnabled === false) {
- await Promise.all([
- mfaRecoveryCodeService.deleteRecoveryCodes({ userId }),
- totpConfigDAL.delete({ userId }),
- webAuthnCredentialDAL.delete({ userId })
- ]);
- }
+ if (isMfaEnabled === true && !wasMfaEnabled) {
+ await mfaRecoveryCodeService.rotateRecoveryCodes({ userId, tx });
+ } else if (isMfaEnabled === false) {
+ await mfaRecoveryCodeService.deleteRecoveryCodes({ userId, tx });
+ await totpConfigDAL.delete({ userId }, tx);
+ await webAuthnCredentialDAL.delete({ userId }, tx);
+ }
+
+ return user2fa;
+ });
return updatedUser;
};