Skip to content

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

fix: clear jid cookie on token refresh failure and handle expired tokens in logout

Devin AI7821af6c0365
diff --git a/backend/src/server/routes/v1/auth-router.ts b/backend/src/server/routes/v1/auth-router.ts
index bebcb0191a..f26fae3adb 100644
--- a/backend/src/server/routes/v1/auth-router.ts
+++ b/backend/src/server/routes/v1/auth-router.ts
@@ -23,10 +23,15 @@ export const registerAuthRoutes = async (server: FastifyZodProvider) => {
}
},
handler: async (req, res) => {
- const { decodedToken } = await server.services.authToken.validateRefreshToken(req.cookies.jid);
const appCfg = getConfig();
- await server.services.login.logout(decodedToken.userId, decodedToken.tokenVersionId);
+ try {
+ const { decodedToken } = await server.services.authToken.validateRefreshToken(req.cookies.jid);
+ await server.services.login.logout(decodedToken.userId, decodedToken.tokenVersionId);
+ } catch {
+ // If token validation fails (e.g. expired/malformed refresh token),
+ // we still proceed to clear all session cookies below.
+ }
void res.cookie("jid", "", {
httpOnly: true,
@@ -89,9 +94,25 @@ export const registerAuthRoutes = async (server: FastifyZodProvider) => {
})
}
},
- handler: async (req) => {
- const { decodedToken, tokenVersion } = await server.services.authToken.validateRefreshToken(req.cookies.jid);
+ handler: async (req, res) => {
const appCfg = getConfig();
+
+ let decodedToken;
+ let tokenVersion;
+ try {
+ ({ decodedToken, tokenVersion } = await server.services.authToken.validateRefreshToken(req.cookies.jid));
+ } catch (err) {
+ // Clear the expired/invalid jid cookie so it doesn't cause login loops
+ // when the browser keeps sending the stale cookie on subsequent requests.
+ void res.cookie("jid", "", {
+ httpOnly: true,
+ path: "/",
+ sameSite: "strict",
+ secure: appCfg.HTTPS_ENABLED,
+ maxAge: 0
+ });
+ throw err;
+ }
let expiresIn: string | number = appCfg.JWT_AUTH_LIFETIME;
if (decodedToken.organizationId) {