Skip to content

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

improvement: set cache lifetime to token expiry

Scott Wilsond4b06ce6488f
diff --git a/backend/src/keystore/keystore.ts b/backend/src/keystore/keystore.ts
index 7260fd832a..7cb72229e3 100644
--- a/backend/src/keystore/keystore.ts
+++ b/backend/src/keystore/keystore.ts
@@ -138,7 +138,6 @@ 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/v2/password-router.ts b/backend/src/server/routes/v2/password-router.ts
index 03a7cc4866..1d4dd8c598 100644
--- a/backend/src/server/routes/v2/password-router.ts
+++ b/backend/src/server/routes/v2/password-router.ts
@@ -26,7 +26,8 @@ export const registerPasswordRouter = async (server: FastifyZodProvider) => {
type: ResetPasswordV2Type.Recovery,
newPassword: req.body.newPassword,
userId: token.userId,
- recoveryTokenJti: token.jti
+ recoveryTokenJti: token.jti,
+ recoveryTokenExpiresAt: token.exp
});
}
});
diff --git a/backend/src/services/auth/auth-password-service.ts b/backend/src/services/auth/auth-password-service.ts
index 740412e981..359d94953f 100644
--- a/backend/src/services/auth/auth-password-service.ts
+++ b/backend/src/services/auth/auth-password-service.ts
@@ -1,4 +1,4 @@
-import { KeyStorePrefixes, KeyStoreTtls, TKeyStoreFactory } from "@app/keystore/keystore";
+import { KeyStorePrefixes, TKeyStoreFactory } from "@app/keystore/keystore";
import { getConfig } from "@app/lib/config/env";
import { crypto } from "@app/lib/crypto/cryptography";
import { BadRequestError, UnauthorizedError } from "@app/lib/errors";
@@ -30,16 +30,26 @@ export const authPaswordServiceFactory = ({
smtpService,
keyStore
}: TAuthPasswordServiceFactoryDep) => {
- const resetPasswordV2 = async ({ userId, newPassword, oldPassword, type, recoveryTokenJti }: TResetPasswordV2DTO) => {
+ const resetPasswordV2 = async ({
+ userId,
+ newPassword,
+ oldPassword,
+ type,
+ recoveryTokenJti,
+ recoveryTokenExpiresAt
+ }: TResetPasswordV2DTO) => {
const cfg = getConfig();
if (type === ResetPasswordV2Type.Recovery) {
- if (!recoveryTokenJti) {
+ if (!recoveryTokenJti || !recoveryTokenExpiresAt) {
throw new UnauthorizedError({ message: "Invalid recovery token" });
}
+ // Marker TTL tracks the token's own remaining lifetime so non-default JWT_SIGNUP_LIFETIME
+ // configs can't outlast the replay marker and reopen the reuse window.
+ const ttlSeconds = Math.max(1, recoveryTokenExpiresAt - Math.floor(Date.now() / 1000));
const claimed = await keyStore.setItemWithExpiryNX(
KeyStorePrefixes.UsedAccountRecoveryToken(userId, recoveryTokenJti),
- KeyStoreTtls.UsedAccountRecoveryTokenInSeconds,
+ ttlSeconds,
"1"
);
if (!claimed) {
diff --git a/backend/src/services/auth/auth-password-type.ts b/backend/src/services/auth/auth-password-type.ts
index 48d919073c..5878e0afda 100644
--- a/backend/src/services/auth/auth-password-type.ts
+++ b/backend/src/services/auth/auth-password-type.ts
@@ -9,6 +9,7 @@ export type TResetPasswordV2DTO = {
newPassword: string;
oldPassword?: string;
recoveryTokenJti?: string;
+ recoveryTokenExpiresAt?: number;
};
export type TResetPasswordViaBackupKeyDTO = {
diff --git a/backend/src/services/auth/auth-type.ts b/backend/src/services/auth/auth-type.ts
index 0fda15cd5b..4ab41adf50 100644
--- a/backend/src/services/auth/auth-type.ts
+++ b/backend/src/services/auth/auth-type.ts
@@ -123,6 +123,7 @@ export type AuthModeAccountRecoveryTokenPayload = {
authTokenType: AuthTokenType.ACCOUNT_RECOVERY_TOKEN;
userId: string;
jti: string;
+ exp: number;
};
export enum MfaMethod {