Skip to content

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

do no reprompt MFA in case of changing between sub orgs

Thiago Araujo da Silva0accfdf3f052
diff --git a/backend/e2e-test/routes/v3/auth-mfa.spec.ts b/backend/e2e-test/routes/v3/auth-mfa.spec.ts
index 69c0afe366..3954b522e5 100644
--- a/backend/e2e-test/routes/v3/auth-mfa.spec.ts
+++ b/backend/e2e-test/routes/v3/auth-mfa.spec.ts
@@ -1,6 +1,8 @@
+import crypto from "node:crypto";
+
import { decode } from "jsonwebtoken";
-import { TableName } from "@app/db/schemas";
+import { AccessScope, OrgMembershipRole, OrgMembershipStatus, TableName } from "@app/db/schemas";
import { seedData1 } from "@app/db/seed-data";
import { MfaMethod } from "@app/services/auth/auth-type";
@@ -97,4 +99,112 @@ describe("Auth MFA V3", () => {
expect(res.statusCode).toBeGreaterThanOrEqual(400);
});
+
+ // The MFA policy belongs to the root org, so one verified session must cover every switch between
+ // that root and its sub-orgs; only a different root org may prompt again.
+ describe("sub-organization switching", () => {
+ const rootOrgId = seedData1.organization.id;
+ const createdOrgIds: string[] = [];
+ let subOrgA: string;
+ let subOrgB: string;
+ let otherRootOrgId: string;
+
+ const makeOrgWithSeedUser = async (parentRootOrgId?: string) => {
+ const [org] = await db()(TableName.Organization)
+ .insert({
+ name: `mfa-${crypto.randomUUID()}`,
+ slug: `mfa-${crypto.randomUUID()}`,
+ rootOrgId: parentRootOrgId ?? null,
+ parentOrgId: parentRootOrgId ?? null,
+ enforceMfa: true
+ })
+ .returning("*");
+ createdOrgIds.push(org.id);
+ const [membership] = await db()(TableName.Membership)
+ .insert({
+ scope: AccessScope.Organization,
+ scopeOrgId: org.id,
+ actorUserId: seedData1.id,
+ status: OrgMembershipStatus.Accepted,
+ isActive: true
+ })
+ .returning("*");
+ await db()(TableName.MembershipRole).insert({ membershipId: membership.id, role: OrgMembershipRole.Member });
+ return org.id as string;
+ };
+
+ const verifyMfa = async (mfaToken: string) => {
+ const code = (smtp().getLastEmail()?.substitutions as Record<string, string>)?.code;
+ expect(code).toBeDefined();
+ const res = await testServer.inject({
+ method: "POST",
+ url: "/api/v2/auth/mfa/verify",
+ headers: { authorization: `Bearer ${mfaToken}` },
+ body: { mfaToken: code, mfaMethod: MfaMethod.EMAIL }
+ });
+ expect(res.statusCode).toBe(200);
+ return res.json().token as string;
+ };
+
+ // Mirrors the web app: the verified token is scoped to the root org, so landing in the org that
+ // was selected takes one more select-org call with it.
+ const loginInto = async (orgId: string) => {
+ const { accessToken } = await loginUser(seedData1.email, seedData1.password);
+ smtp().clear();
+ const { payload } = await selectOrg(accessToken, orgId);
+ expect(payload.isMfaEnabled).toBe(true);
+ const verifiedToken = await verifyMfa(payload.token);
+ const { payload: scoped } = await selectOrg(verifiedToken, orgId);
+ expect(scoped.isMfaEnabled).toBe(false);
+ return scoped.token as string;
+ };
+
+ beforeAll(async () => {
+ subOrgA = await makeOrgWithSeedUser(rootOrgId);
+ subOrgB = await makeOrgWithSeedUser(rootOrgId);
+ otherRootOrgId = await makeOrgWithSeedUser();
+ });
+
+ afterAll(async () => {
+ await db()(TableName.Organization).whereIn("id", createdOrgIds).del();
+ });
+
+ test("parent then child does not prompt again", async () => {
+ const token = await loginInto(rootOrgId);
+
+ const { payload } = await selectOrg(token, subOrgA);
+
+ expect(payload.isMfaEnabled).toBe(false);
+ expect(decode(payload.token)).toMatchObject({ organizationId: rootOrgId, subOrganizationId: subOrgA });
+ });
+
+ test("child then parent does not prompt again", async () => {
+ const token = await loginInto(subOrgA);
+ expect(decode(token)).toMatchObject({ organizationId: rootOrgId, subOrganizationId: subOrgA });
+
+ const { payload } = await selectOrg(token, rootOrgId);
+
+ expect(payload.isMfaEnabled).toBe(false);
+ expect(decode(payload.token)).toMatchObject({ organizationId: rootOrgId });
+ expect(decode(payload.token)).not.toHaveProperty("subOrganizationId");
+ });
+
+ test("child then sibling child does not prompt again", async () => {
+ const token = await loginInto(subOrgA);
+
+ const { payload } = await selectOrg(token, subOrgB);
+
+ expect(payload.isMfaEnabled).toBe(false);
+ expect(decode(payload.token)).toMatchObject({ organizationId: rootOrgId, subOrganizationId: subOrgB });
+ });
+
+ test("a different root org still prompts", async () => {
+ const token = await loginInto(subOrgA);
+
+ const { payload } = await selectOrg(token, otherRootOrgId);
+
+ expect(payload.isMfaEnabled).toBe(true);
+ expect(payload.mfaMethod).toBe(MfaMethod.EMAIL);
+ });
+ });
});
diff --git a/backend/src/server/routes/v3/login-router.ts b/backend/src/server/routes/v3/login-router.ts
index 84131d92c3..68bea9c404 100644
--- a/backend/src/server/routes/v3/login-router.ts
+++ b/backend/src/server/routes/v3/login-router.ts
@@ -76,7 +76,7 @@ export const registerLoginRouter = async (server: FastifyZodProvider) => {
ipAddress: req.realIp,
userId: req.auth.userId,
userAuthMethod: req.auth.authMethod,
- actorOrgId: req.auth.orgId,
+ actorRootOrgId: req.auth.rootOrgId,
isMfaVerified: req.auth.isMfaVerified,
mfaMethod: req.auth.mfaMethod
});
diff --git a/backend/src/services/auth/auth-login-service.ts b/backend/src/services/auth/auth-login-service.ts
index fdf03b7530..e9beeb66bb 100644
--- a/backend/src/services/auth/auth-login-service.ts
+++ b/backend/src/services/auth/auth-login-service.ts
@@ -1153,7 +1153,7 @@ export const authLoginServiceFactory = ({
organizationId,
userId,
userAuthMethod,
- actorOrgId,
+ actorRootOrgId,
isMfaVerified,
mfaMethod
}: {
@@ -1162,7 +1162,7 @@ export const authLoginServiceFactory = ({
organizationId: string;
userId: string;
userAuthMethod: AuthMethod;
- actorOrgId?: string;
+ actorRootOrgId?: string;
isMfaVerified?: boolean;
mfaMethod?: MfaMethod;
}) => {
@@ -1288,8 +1288,8 @@ export const authLoginServiceFactory = ({
}
const { isMfaRequired, requiredMfaMethod } = getRequiredMfaMethod(rootOrg, user);
- // Check if organization has changed
- const hasOrganizationChanged = actorOrgId ? actorOrgId !== rootOrg.id : false;
+ // The MFA policy belongs to the root org, so moving between a root org and its sub-orgs is not a change.
+ const hasOrganizationChanged = actorRootOrgId ? actorRootOrgId !== rootOrg.id : false;
// Check if MFA method has changed
const hasMfaMethodChanged = mfaMethod !== requiredMfaMethod;
// Trigger MFA if required and either not verified or something changed