Skip to content

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

fix: add better servicing

Tre Dubrava305608e55245
diff --git a/backend/src/ee/services/ldap-config/ldap-config-service.ts b/backend/src/ee/services/ldap-config/ldap-config-service.ts
index 2c2fb21ff0..ae287bb39e 100644
--- a/backend/src/ee/services/ldap-config/ldap-config-service.ts
+++ b/backend/src/ee/services/ldap-config/ldap-config-service.ts
@@ -474,6 +474,25 @@ export const ldapConfigServiceFactory = ({
newUser = userWithSameUsername;
}
+ // Prevent cross-org account takeover: if an existing user was found by email,
+ // verify they are already a member of this org before binding the LDAP identity.
+ if (newUser) {
+ const [existingMembership] = await orgDAL.findMembership(
+ {
+ [`${TableName.Membership}.actorUserId` as "actorUserId"]: newUser.id,
+ [`${TableName.Membership}.scopeOrgId` as "scopeOrgId"]: orgId,
+ [`${TableName.Membership}.scope` as "scope"]: AccessScope.Organization
+ },
+ { tx }
+ );
+ if (!existingMembership) {
+ throw new ForbiddenRequestError({
+ message:
+ "User is not a member of this organization. Please contact your organization admin to receive an invite."
+ });
+ }
+ }
+
if (!newUser) {
const uniqueUsername = await normalizeUsername(username, userDAL);
newUser = await userDAL.create(
diff --git a/backend/src/ee/services/oidc/oidc-config-service.ts b/backend/src/ee/services/oidc/oidc-config-service.ts
index 6b764f2628..b3ac192177 100644
--- a/backend/src/ee/services/oidc/oidc-config-service.ts
+++ b/backend/src/ee/services/oidc/oidc-config-service.ts
@@ -278,6 +278,27 @@ export const oidcConfigServiceFactory = ({
}
}
+ // Prevent cross-org account takeover: if an existing user was found by email,
+ // verify they are already a member of this org before binding the SSO identity.
+ // Without this check, an attacker could configure OIDC on their own org with a
+ // victim's email and get a provider token signed for the victim's userId.
+ if (newUser) {
+ const [existingMembership] = await orgDAL.findMembership(
+ {
+ [`${TableName.Membership}.actorUserId` as "actorUserId"]: newUser.id,
+ [`${TableName.Membership}.scopeOrgId` as "scopeOrgId"]: orgId,
+ [`${TableName.Membership}.scope` as "scope"]: AccessScope.Organization
+ },
+ { tx }
+ );
+ if (!existingMembership) {
+ throw new ForbiddenRequestError({
+ message:
+ "User is not a member of this organization. Please contact your organization admin to receive an invite."
+ });
+ }
+ }
+
if (!newUser) {
const uniqueUsername = await normalizeUsername(externalId, userDAL);
newUser = await userDAL.create(
diff --git a/backend/src/ee/services/saml-config/saml-config-service.ts b/backend/src/ee/services/saml-config/saml-config-service.ts
index 1745e08fe0..85fddab620 100644
--- a/backend/src/ee/services/saml-config/saml-config-service.ts
+++ b/backend/src/ee/services/saml-config/saml-config-service.ts
@@ -626,6 +626,25 @@ export const samlConfigServiceFactory = ({
newUser = userWithSameUsername;
}
+ // Prevent cross-org account takeover: if an existing user was found by email,
+ // verify they are already a member of this org before binding the SAML identity.
+ if (newUser) {
+ const [existingMembership] = await orgDAL.findMembership(
+ {
+ [`${TableName.Membership}.actorUserId` as "actorUserId"]: newUser.id,
+ [`${TableName.Membership}.scopeOrgId` as "scopeOrgId"]: orgId,
+ [`${TableName.Membership}.scope` as "scope"]: AccessScope.Organization
+ },
+ { tx }
+ );
+ if (!existingMembership) {
+ throw new ForbiddenRequestError({
+ message:
+ "User is not a member of this organization. Please contact your organization admin to receive an invite."
+ });
+ }
+ }
+
if (!newUser) {
const uniqueUsername = await normalizeUsername(`${firstName ?? ""}-${lastName ?? ""}`, userDAL);
newUser = await userDAL.create(
diff --git a/backend/src/server/routes/v1/sso-router.ts b/backend/src/server/routes/v1/sso-router.ts
index bd5c92080d..fdd9d868be 100644
--- a/backend/src/server/routes/v1/sso-router.ts
+++ b/backend/src/server/routes/v1/sso-router.ts
@@ -616,6 +616,14 @@ export const registerSsoRouter = async (server: FastifyZodProvider) => {
);
}
+ if (data.isMfaEnabled) {
+ return {
+ token: data.token.access,
+ isMfaEnabled: true,
+ mfaMethod: data.mfaMethod
+ } as const;
+ }
+
if ([AuthMethod.GOOGLE, AuthMethod.GITHUB, AuthMethod.GITLAB].includes(data.decodedProviderToken.authMethod)) {
void res.setCookie("jid", data.token.refresh, {
httpOnly: true,
diff --git a/backend/src/services/auth/auth-login-service.ts b/backend/src/services/auth/auth-login-service.ts
index ec42c01f37..bd159cd3db 100644
--- a/backend/src/services/auth/auth-login-service.ts
+++ b/backend/src/services/auth/auth-login-service.ts
@@ -1310,6 +1310,7 @@ export const authLoginServiceFactory = ({
* 2. To avoid attaching the access token to the URL, which could be logged. The provider token has a very short lifespan, reducing security risks.
*/
const oauth2TokenExchange = async ({ userAgent, ip, providerAuthToken, email }: TOauthTokenExchangeDTO) => {
+ const appCfg = getConfig();
const decodedProviderToken = validateProviderAuthToken(providerAuthToken, email);
const { authMethod, userName } = decodedProviderToken;
@@ -1329,6 +1330,47 @@ export const authLoginServiceFactory = ({
if (!userEnc) throw new BadRequestError({ message: "User encryption not found" });
+ // Check MFA before issuing tokens — mirrors the logic in selectOrganization
+ const user = await userDAL.findById(userEnc.userId);
+ const org = organizationId ? await orgDAL.findById(organizationId) : null;
+ const shouldCheckMfa = org?.enforceMfa || user.isMfaEnabled;
+
+ if (shouldCheckMfa) {
+ enforceUserLockStatus(Boolean(user.isLocked), user.temporaryLockDateEnd);
+
+ const orgMfaMethod = org?.enforceMfa ? (org.selectedMfaMethod ?? MfaMethod.EMAIL) : undefined;
+ const userMfaMethod = user.isMfaEnabled ? (user.selectedMfaMethod ?? MfaMethod.EMAIL) : undefined;
+ const mfaMethod = orgMfaMethod ?? userMfaMethod;
+
+ const mfaToken = crypto.jwt().sign(
+ {
+ authMethod,
+ authTokenType: AuthTokenType.MFA_TOKEN,
+ userId: userEnc.userId,
+ organizationId
+ },
+ appCfg.AUTH_SECRET,
+ {
+ expiresIn: appCfg.JWT_MFA_LIFETIME
+ }
+ );
+
+ if (mfaMethod === MfaMethod.EMAIL && userEnc.email) {
+ await sendUserMfaCode({
+ userId: userEnc.userId,
+ email: userEnc.email
+ });
+ }
+
+ return {
+ token: { access: mfaToken, refresh: "" },
+ isMfaEnabled: true,
+ mfaMethod,
+ user: userEnc,
+ decodedProviderToken
+ } as const;
+ }
+
const token = await generateUserTokens({
user: { ...userEnc, id: userEnc.userId },
ip,
diff --git a/frontend/src/hooks/api/auth/queries.tsx b/frontend/src/hooks/api/auth/queries.tsx
index b4b002c610..9f88e71e4b 100644
--- a/frontend/src/hooks/api/auth/queries.tsx
+++ b/frontend/src/hooks/api/auth/queries.tsx
@@ -22,6 +22,7 @@ import {
LoginV3DTO,
LoginV3Res,
MfaMethod,
+ OauthTokenExchangeRes,
ResetPasswordDTO,
ResetPasswordV2DTO,
ResetUserPasswordV2DTO,
@@ -123,7 +124,10 @@ export const useLogin2 = () => {
};
export const oauthTokenExchange = async (details: TOauthTokenExchangeDTO) => {
- const { data } = await apiRequest.post<Login2Res>("/api/v1/sso/token-exchange", details);
+ const { data } = await apiRequest.post<OauthTokenExchangeRes>(
+ "/api/v1/sso/token-exchange",
+ details
+ );
return data;
};
diff --git a/frontend/src/hooks/api/auth/types.ts b/frontend/src/hooks/api/auth/types.ts
index 028a360460..a06bc0be70 100644
--- a/frontend/src/hooks/api/auth/types.ts
+++ b/frontend/src/hooks/api/auth/types.ts
@@ -74,6 +74,11 @@ export type Login2Res = {
tag?: string;
};
+export type OauthTokenExchangeRes = Login2Res & {
+ isMfaEnabled?: boolean;
+ mfaMethod?: string;
+};
+
export type LoginV3Res = {
accessToken: string;
mfaEnabled: boolean;
diff --git a/frontend/src/pages/auth/LoginPage/components/PasswordStep/PasswordStep.tsx b/frontend/src/pages/auth/LoginPage/components/PasswordStep/PasswordStep.tsx
index 8315833210..1a00776634 100644
--- a/frontend/src/pages/auth/LoginPage/components/PasswordStep/PasswordStep.tsx
+++ b/frontend/src/pages/auth/LoginPage/components/PasswordStep/PasswordStep.tsx
@@ -62,6 +62,17 @@ export const PasswordStep = ({
providerAuthToken
});
+ if (oauthLogin.isMfaEnabled) {
+ SecurityClient.setMfaToken(oauthLogin.token);
+ if (oauthLogin.mfaMethod) {
+ setRequiredMfaMethod(oauthLogin.mfaMethod as MfaMethod);
+ }
+ toggleShowMfa.on();
+ setMfaSuccessCallback(() => handleExchange);
+ setIsLoading(false);
+ return;
+ }
+
// unset provider auth token in case it was used
SecurityClient.setProviderAuthToken("");
// set JWT token