Skip to content

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

fix: mark password recovery token as consumed

Scott Wilson46b52591e3c0
diff --git a/backend/src/keystore/keystore.ts b/backend/src/keystore/keystore.ts
index 9df284b857..7260fd832a 100644
--- a/backend/src/keystore/keystore.ts
+++ b/backend/src/keystore/keystore.ts
@@ -84,6 +84,7 @@ export const KeyStorePrefixes = {
UserMfaLockoutLock: (userId: string) => `user-mfa-lockout-lock:${userId}` as const,
UserMfaUnlockEmailSent: (userId: string) => `user-mfa-unlock-email-sent:${userId}` as const,
UsedTotpCode: (userId: string, code: string) => `used-totp-code:${userId}:${code}` as const,
+ UsedAccountRecoveryToken: (userId: string, jti: string) => `used-account-recovery-token:${userId}:${jti}` as const,
AiMcpServerOAuth: (sessionId: string) => `ai-mcp-server-oauth:${sessionId}` as const,
@@ -137,6 +138,7 @@ export const KeyStoreTtls = {
MfaSessionInSeconds: 300, // 5 minutes
WebAuthnChallengeInSeconds: 300, // 5 minutes
UsedTotpCodeInSeconds: 120, // covers the full ±30s acceptance window (window:1 → 90s) with margin
+ UsedAccountRecoveryTokenInSeconds: 900, // 15 minutes — matches JWT_SIGNUP_LIFETIME default
ProjectSSEConnectionTtlSeconds: 180, // Must be > heartbeat interval (60s) * 2
TelemetryIdentifyIdentityInSeconds: 86400, // 24 hours
RefreshTokenGraceInSeconds: 10,
diff --git a/backend/src/server/routes/index.ts b/backend/src/server/routes/index.ts
index bbe53d35bf..65a8ad4b11 100644
--- a/backend/src/server/routes/index.ts
+++ b/backend/src/server/routes/index.ts
@@ -1082,7 +1082,8 @@ export const registerRoutes = async (
smtpService,
authDAL,
userDAL,
- totpConfigDAL
+ totpConfigDAL,
+ keyStore
});
const accountRecoveryService = accountRecoveryServiceFactory({
diff --git a/backend/src/server/routes/v2/password-router.ts b/backend/src/server/routes/v2/password-router.ts
index 2ff658aa8a..03a7cc4866 100644
--- a/backend/src/server/routes/v2/password-router.ts
+++ b/backend/src/server/routes/v2/password-router.ts
@@ -25,7 +25,8 @@ export const registerPasswordRouter = async (server: FastifyZodProvider) => {
await server.services.password.resetPasswordV2({
type: ResetPasswordV2Type.Recovery,
newPassword: req.body.newPassword,
- userId: token.userId
+ userId: token.userId,
+ recoveryTokenJti: token.jti
});
}
});
diff --git a/backend/src/services/account-recovery/account-recovery-service.ts b/backend/src/services/account-recovery/account-recovery-service.ts
index c773f888a8..4098dc574a 100644
--- a/backend/src/services/account-recovery/account-recovery-service.ts
+++ b/backend/src/services/account-recovery/account-recovery-service.ts
@@ -113,7 +113,8 @@ export const accountRecoveryServiceFactory = ({
const token = crypto.jwt().sign(
{
authTokenType: AuthTokenType.ACCOUNT_RECOVERY_TOKEN,
- userId: user.id
+ userId: user.id,
+ jti: crypto.randomBytes(16).toString("hex")
},
cfg.AUTH_SECRET,
{ expiresIn: cfg.JWT_SIGNUP_LIFETIME }
diff --git a/backend/src/services/auth/auth-password-service.ts b/backend/src/services/auth/auth-password-service.ts
index 8856da4f7b..740412e981 100644
--- a/backend/src/services/auth/auth-password-service.ts
+++ b/backend/src/services/auth/auth-password-service.ts
@@ -1,6 +1,7 @@
+import { KeyStorePrefixes, KeyStoreTtls, TKeyStoreFactory } from "@app/keystore/keystore";
import { getConfig } from "@app/lib/config/env";
import { crypto } from "@app/lib/crypto/cryptography";
-import { BadRequestError } from "@app/lib/errors";
+import { BadRequestError, UnauthorizedError } from "@app/lib/errors";
import { logger } from "@app/lib/logger";
import { OrgServiceActor } from "@app/lib/types";
@@ -19,13 +20,33 @@ type TAuthPasswordServiceFactoryDep = {
tokenService: TAuthTokenServiceFactory;
smtpService: TSmtpService;
totpConfigDAL: Pick<TTotpConfigDALFactory, "delete">;
+ keyStore: Pick<TKeyStoreFactory, "setItemWithExpiryNX">;
};
export type TAuthPasswordFactory = ReturnType<typeof authPaswordServiceFactory>;
-export const authPaswordServiceFactory = ({ userDAL, tokenService, smtpService }: TAuthPasswordServiceFactoryDep) => {
- const resetPasswordV2 = async ({ userId, newPassword, oldPassword, type }: TResetPasswordV2DTO) => {
+export const authPaswordServiceFactory = ({
+ userDAL,
+ tokenService,
+ smtpService,
+ keyStore
+}: TAuthPasswordServiceFactoryDep) => {
+ const resetPasswordV2 = async ({ userId, newPassword, oldPassword, type, recoveryTokenJti }: TResetPasswordV2DTO) => {
const cfg = getConfig();
+ if (type === ResetPasswordV2Type.Recovery) {
+ if (!recoveryTokenJti) {
+ throw new UnauthorizedError({ message: "Invalid recovery token" });
+ }
+ const claimed = await keyStore.setItemWithExpiryNX(
+ KeyStorePrefixes.UsedAccountRecoveryToken(userId, recoveryTokenJti),
+ KeyStoreTtls.UsedAccountRecoveryTokenInSeconds,
+ "1"
+ );
+ if (!claimed) {
+ throw new UnauthorizedError({ message: "Invalid recovery token" });
+ }
+ }
+
const user = await userDAL.findById(userId);
if (!user || !user.isAccepted || !user.isEmailVerified) {
throw new BadRequestError({ message: `Invalid token` });
diff --git a/backend/src/services/auth/auth-password-type.ts b/backend/src/services/auth/auth-password-type.ts
index ceb70d4118..48d919073c 100644
--- a/backend/src/services/auth/auth-password-type.ts
+++ b/backend/src/services/auth/auth-password-type.ts
@@ -8,6 +8,7 @@ export type TResetPasswordV2DTO = {
userId: string;
newPassword: string;
oldPassword?: string;
+ recoveryTokenJti?: string;
};
export type TResetPasswordViaBackupKeyDTO = {
diff --git a/backend/src/services/auth/auth-type.ts b/backend/src/services/auth/auth-type.ts
index 1374bcf085..0fda15cd5b 100644
--- a/backend/src/services/auth/auth-type.ts
+++ b/backend/src/services/auth/auth-type.ts
@@ -122,6 +122,7 @@ export type AuthModeSignUpTokenPayload = {
export type AuthModeAccountRecoveryTokenPayload = {
authTokenType: AuthTokenType.ACCOUNT_RECOVERY_TOKEN;
userId: string;
+ jti: string;
};
export enum MfaMethod {