Skip to content

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

wrap delete in transaction

Thiago Araujo da Silva5558aefb5f56
diff --git a/backend/src/services/totp/totp-service.ts b/backend/src/services/totp/totp-service.ts
index e0a9231893..5e5d3cb09e 100644
--- a/backend/src/services/totp/totp-service.ts
+++ b/backend/src/services/totp/totp-service.ts
@@ -178,26 +178,31 @@ export const totpServiceFactory = ({ totpConfigDAL, kmsService, userDAL, keyStor
};
const deleteUserTotpConfig = async ({ userId }: TDeleteUserTotpConfigDTO) => {
- const totpConfig = await totpConfigDAL.findOne({
- userId
- });
+ await totpConfigDAL.transaction(async (tx) => {
+ const totpConfig = await totpConfigDAL.findOne(
+ {
+ userId
+ },
+ tx
+ );
- if (!totpConfig) {
- throw new NotFoundError({
- message: "TOTP configuration not found"
- });
- }
+ if (!totpConfig) {
+ throw new NotFoundError({
+ message: "TOTP configuration not found"
+ });
+ }
- await totpConfigDAL.deleteById(totpConfig.id);
+ await totpConfigDAL.deleteById(totpConfig.id, tx);
- // 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 });
- }
+ // 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, tx);
+ if (user?.selectedMfaMethod === MfaMethod.TOTP) {
+ await userDAL.updateById(userId, { selectedMfaMethod: MfaMethod.EMAIL }, tx);
+ }
+ });
};
return {
diff --git a/backend/src/services/webauthn/webauthn-service.ts b/backend/src/services/webauthn/webauthn-service.ts
index a457584cb2..f5ddac0cb7 100644
--- a/backend/src/services/webauthn/webauthn-service.ts
+++ b/backend/src/services/webauthn/webauthn-service.ts
@@ -324,33 +324,35 @@ export const webAuthnServiceFactory = ({
* Delete a WebAuthn credential
*/
const deleteWebAuthnCredential = async ({ userId, id }: TDeleteWebAuthnCredentialDTO) => {
- const credential = await webAuthnCredentialDAL.findById(id);
+ await webAuthnCredentialDAL.transaction(async (tx) => {
+ const credential = await webAuthnCredentialDAL.findById(id, tx);
- if (!credential) {
- throw new NotFoundError({
- message: "Credential not found"
- });
- }
+ if (!credential) {
+ throw new NotFoundError({
+ message: "Credential not found"
+ });
+ }
- if (userId !== credential.userId) {
- throw new ForbiddenRequestError({
- message: "Credential does not belong to this user"
- });
- }
+ if (userId !== credential.userId) {
+ throw new ForbiddenRequestError({
+ message: "Credential does not belong to this user"
+ });
+ }
- 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 });
+ await webAuthnCredentialDAL.deleteById(credential.id, tx);
+
+ // 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 }, { tx });
+ if (remainingCredentials.length === 0) {
+ const user = await userDAL.findById(userId, tx);
+ if (user?.selectedMfaMethod === MfaMethod.WEBAUTHN) {
+ await userDAL.updateById(userId, { selectedMfaMethod: MfaMethod.EMAIL }, tx);
+ }
}
- }
+ });
return {
success: true