Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.
TypeScript acmeagentsai-agentscertificate-managementdevsecopsenvironment-variableskubernetesopen-sourcepampkipostgresprivate-caprivileged-access-managementsecret-managementsecret-managersecretssecrets-managementsecuritysecurity-toolsvault
fix(auth): derive OAuth callback URL from request origin to prevent session loss
Devin AI825e25521b65
diff --git a/backend/src/server/routes/v1/sso-router.ts b/backend/src/server/routes/v1/sso-router.tsindex f65842c9ef..b389cb157c 100644--- a/backend/src/server/routes/v1/sso-router.ts+++ b/backend/src/server/routes/v1/sso-router.ts@@ -40,6 +40,12 @@ const passport = new Authenticator({ key: "sso", userProperty: "passportUser" })let serverInstance: FastifyZodProvider | null = null;+// Derive the OAuth callback URL from the incoming request so that the session cookie domain+// matches the callback domain. This prevents session loss when the user starts on one domain+// (e.g. us.infisical.com) but SITE_URL points to another (e.g. app.infisical.com).+const getOAuthCallbackUrl = (req: { protocol: string; hostname: string }, path: string) =>+ `${req.protocol}://${req.hostname}${path}`;+export const registerOauthMiddlewares = (server: FastifyZodProvider) => {serverInstance = server;const appCfg = getConfig();@@ -440,10 +446,11 @@ export const registerSsoRouter = async (server: FastifyZodProvider) => {return (passport.authenticate("google", {scope: ["profile", "email"],+ callbackURL: getOAuthCallbackUrl(req, "/api/v1/sso/google"),authInfo: false// this is due to zod type difference// eslint-disable-next-line @typescript-eslint/no-explicit-any- }) as any+ } as any) as any)(req, res);}],@@ -456,6 +463,7 @@ export const registerSsoRouter = async (server: FastifyZodProvider) => {await req.session.destroy();const passportResult = req.passportUser;const cbPort = passportResult.callbackPort;+ const reqOrigin = getOAuthCallbackUrl(req, "");if (passportResult.result === ProviderAuthResult.SESSION) {// This login completed the user's signup (provider-verified email), so it never goes@@ -495,7 +503,7 @@ export const registerSsoRouter = async (server: FastifyZodProvider) => {secure: appCfg.HTTPS_ENABLED});addAuthOriginDomainCookie(res);- const sessionUrl = new URL("/login/select-organization", appCfg.SITE_URL);+ const sessionUrl = new URL("/login/select-organization", reqOrigin || appCfg.SITE_URL);if (isAdminLogin) sessionUrl.searchParams.set("isAdminLogin", isAdminLogin);if (cbPort) sessionUrl.searchParams.set("callback_port", String(cbPort));// Provider-verified signups never render the signup page that pushes the GTM conversion event,@@ -506,7 +514,7 @@ export const registerSsoRouter = async (server: FastifyZodProvider) => {if (passportResult.result === ProviderAuthResult.SIGNUP_REQUIRED) {const serverCfg = await getServerCfg();- const signupUrl = new URL("/signup/sso", appCfg.SITE_URL);+ const signupUrl = new URL("/signup/sso", reqOrigin || appCfg.SITE_URL);signupUrl.searchParams.set("token", passportResult.signupToken);if (serverCfg.defaultAuthOrgId && !appCfg.isCloud) signupUrl.searchParams.set("defaultOrgAllowed", "true");if (cbPort) signupUrl.searchParams.set("callback_port", String(cbPort));@@ -519,12 +527,18 @@ export const registerSsoRouter = async (server: FastifyZodProvider) => {server.route({url: "/google",method: "GET",- preValidation: passport.authenticate("google", {- session: false,- failureRedirect: "/login/provider/error",- authInfo: false- // this is due to zod type difference- }) as never,+ preValidation: [+ async (req, res) => {+ return (+ passport.authenticate("google", {+ session: false,+ failureRedirect: "/login/provider/error",+ callbackURL: getOAuthCallbackUrl(req, "/api/v1/sso/google"),+ authInfo: false+ } as any) as any+ )(req, res);+ }+ ],handler: handleOAuthCallbackRedirect});@@ -560,9 +574,10 @@ export const registerSsoRouter = async (server: FastifyZodProvider) => {return (passport.authenticate("github", {session: false,+ callbackURL: getOAuthCallbackUrl(req, "/api/v1/sso/github"),authInfo: false// this is due to zod type difference- }) as any+ } as any) as any)(req, res);}],@@ -611,12 +626,18 @@ export const registerSsoRouter = async (server: FastifyZodProvider) => {server.route({url: "/github",method: "GET",- preValidation: passport.authenticate("github", {- session: false,- failureRedirect: "/login/provider/error",- authInfo: false- // this is due to zod type difference- }) as any,+ preValidation: [+ async (req: any, res: any) => {+ return (+ passport.authenticate("github", {+ session: false,+ failureRedirect: "/login/provider/error",+ callbackURL: getOAuthCallbackUrl(req, "/api/v1/sso/github"),+ authInfo: false+ } as any) as any+ )(req, res);+ }+ ],handler: async (req: any, res: any) => {if (req.passportUser.externalProviderAccessToken) {void res.cookie(INFISICAL_PROVIDER_GITHUB_ACCESS_TOKEN, req.passportUser.externalProviderAccessToken, {@@ -663,10 +684,11 @@ export const registerSsoRouter = async (server: FastifyZodProvider) => {return (passport.authenticate("gitlab", {session: false,+ callbackURL: getOAuthCallbackUrl(req, "/api/v1/sso/gitlab"),authInfo: false// this is due to zod type difference// eslint-disable-next-line @typescript-eslint/no-explicit-any- }) as any+ } as any) as any)(req, res);}],@@ -676,13 +698,18 @@ export const registerSsoRouter = async (server: FastifyZodProvider) => {server.route({url: "/gitlab",method: "GET",- preValidation: passport.authenticate("gitlab", {- session: false,- failureRedirect: "/login/provider/error",- authInfo: false- // this is due to zod type difference- // eslint-disable-next-line @typescript-eslint/no-explicit-any- }) as any,+ preValidation: [+ async (req: any, res: any) => {+ return (+ passport.authenticate("gitlab", {+ session: false,+ failureRedirect: "/login/provider/error",+ callbackURL: getOAuthCallbackUrl(req, "/api/v1/sso/gitlab"),+ authInfo: false+ } as any) as any+ )(req, res);+ }+ ],handler: handleOAuthCallbackRedirect});