Skip to content

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

feat: added llm review

=d085d44f839e
diff --git a/backend/src/server/routes/mfa-step-up-fns.test.ts b/backend/src/server/routes/mfa-step-up-fns.test.ts
index 824664f603..45f4bafe57 100644
--- a/backend/src/server/routes/mfa-step-up-fns.test.ts
+++ b/backend/src/server/routes/mfa-step-up-fns.test.ts
@@ -140,14 +140,13 @@ describe("ensureStepUpMfa binds prior proof to the factors the action would chal
});
describe("ensureFactorEnrollmentStepUpMfa", () => {
- const enrollmentArgs = { userId: USER_ID, tokenVersionId: TOKEN_VERSION };
+ const enrollmentArgs = { userId: USER_ID, tokenVersionId: TOKEN_VERSION, enrollingMethod: MfaMethod.WEBAUTHN };
const withEnrollmentMethods = (enrollmentMethods: TResolved | null, patch: ServerPatch = {}) => {
const built = makeServer(patch);
- Object.assign(built.user, {
- getFactorEnrollmentStepUpMfaMethods: vi.fn().mockResolvedValue(enrollmentMethods)
- });
- return built;
+ const getFactorEnrollmentStepUpMfaMethods = vi.fn().mockResolvedValue(enrollmentMethods);
+ Object.assign(built.user, { getFactorEnrollmentStepUpMfaMethods });
+ return { ...built, getFactorEnrollmentStepUpMfaMethods };
};
test("lets the first factor through without a challenge", async () => {
@@ -158,7 +157,7 @@ describe("ensureFactorEnrollmentStepUpMfa", () => {
});
test("challenges an existing factor without needing an org", async () => {
- const { server, mfaSession, user } = withEnrollmentMethods({
+ const { server, mfaSession, user, getFactorEnrollmentStepUpMfaMethods } = withEnrollmentMethods({
challenge: MfaMethod.WEBAUTHN,
accepted: [MfaMethod.WEBAUTHN, MfaMethod.TOTP]
});
@@ -167,6 +166,7 @@ describe("ensureFactorEnrollmentStepUpMfa", () => {
name: "SESSION_MFA_REQUIRED",
details: { mfaMethod: MfaMethod.WEBAUTHN }
});
+ expect(getFactorEnrollmentStepUpMfaMethods).toHaveBeenCalledWith(USER_ID, MfaMethod.WEBAUTHN);
expect(user.getStepUpMfaMethod).not.toHaveBeenCalled();
expect(mfaSession.hasRecentMfaAuth).toHaveBeenCalledWith(USER_ID, TOKEN_VERSION, [
MfaMethod.WEBAUTHN,
diff --git a/backend/src/server/routes/mfa-step-up-fns.ts b/backend/src/server/routes/mfa-step-up-fns.ts
index ce96564ab7..7e827f572e 100644
--- a/backend/src/server/routes/mfa-step-up-fns.ts
+++ b/backend/src/server/routes/mfa-step-up-fns.ts
@@ -116,9 +116,9 @@ export const ensureStepUpMfa = async (
// retried request through, so callers never pass the session id back.
export const ensureFactorEnrollmentStepUpMfa = async (
server: FastifyZodProvider,
- { userId, tokenVersionId }: { userId: string; tokenVersionId: string }
+ { userId, tokenVersionId, enrollingMethod }: { userId: string; tokenVersionId: string; enrollingMethod: MfaMethod }
) => {
- const mfaMethods = await server.services.user.getFactorEnrollmentStepUpMfaMethods(userId);
+ const mfaMethods = await server.services.user.getFactorEnrollmentStepUpMfaMethods(userId, enrollingMethod);
if (!mfaMethods) return;
await ensureStepUpMfa(server, {
diff --git a/backend/src/server/routes/v1/user-router.ts b/backend/src/server/routes/v1/user-router.ts
index cc833f8a47..f864dc1cd5 100644
--- a/backend/src/server/routes/v1/user-router.ts
+++ b/backend/src/server/routes/v1/user-router.ts
@@ -276,7 +276,8 @@ export const registerUserRouter = async (server: FastifyZodProvider) => {
handler: async (req) => {
await ensureFactorEnrollmentStepUpMfa(server, {
userId: req.permission.id,
- tokenVersionId: getStepUpSessionId(req)
+ tokenVersionId: getStepUpSessionId(req),
+ enrollingMethod: MfaMethod.TOTP
});
return server.services.totp.registerUserTotp({
userId: req.permission.id
@@ -307,7 +308,8 @@ export const registerUserRouter = async (server: FastifyZodProvider) => {
handler: async (req) => {
await ensureFactorEnrollmentStepUpMfa(server, {
userId: req.permission.id,
- tokenVersionId: getStepUpSessionId(req)
+ tokenVersionId: getStepUpSessionId(req),
+ enrollingMethod: MfaMethod.TOTP
});
return server.services.totp.verifyUserTotpConfig({
userId: req.permission.id,
@@ -437,7 +439,8 @@ export const registerUserRouter = async (server: FastifyZodProvider) => {
handler: async (req) => {
await ensureFactorEnrollmentStepUpMfa(server, {
userId: req.permission.id,
- tokenVersionId: getStepUpSessionId(req)
+ tokenVersionId: getStepUpSessionId(req),
+ enrollingMethod: MfaMethod.WEBAUTHN
});
return server.services.webAuthn.generateRegistrationOptions({
userId: req.permission.id
@@ -483,7 +486,8 @@ export const registerUserRouter = async (server: FastifyZodProvider) => {
handler: async (req) => {
await ensureFactorEnrollmentStepUpMfa(server, {
userId: req.permission.id,
- tokenVersionId: getStepUpSessionId(req)
+ tokenVersionId: getStepUpSessionId(req),
+ enrollingMethod: MfaMethod.WEBAUTHN
});
return server.services.webAuthn.verifyRegistrationResponse({
userId: req.permission.id,
diff --git a/backend/src/server/routes/v2/user-router.ts b/backend/src/server/routes/v2/user-router.ts
index a94d61b997..2b1e4d190b 100644
--- a/backend/src/server/routes/v2/user-router.ts
+++ b/backend/src/server/routes/v2/user-router.ts
@@ -130,7 +130,8 @@ export const registerUserRouter = async (server: FastifyZodProvider) => {
handler: async (req) => {
await ensureFactorEnrollmentStepUpMfa(server, {
userId: req.permission.id,
- tokenVersionId: getStepUpSessionId(req)
+ tokenVersionId: getStepUpSessionId(req),
+ enrollingMethod: req.body.method
});
if (req.body.method === MfaMethod.TOTP) {
diff --git a/backend/src/services/user/user-service.test.ts b/backend/src/services/user/user-service.test.ts
index 00f803e6af..4ec78a52dd 100644
--- a/backend/src/services/user/user-service.test.ts
+++ b/backend/src/services/user/user-service.test.ts
@@ -360,50 +360,77 @@ describe("getStepUpMfaMethod", () => {
});
describe("getFactorEnrollmentStepUpMfaMethods", () => {
+ const passkeyEnforcingOrg = { id: ENFORCED_ORG_ID, enforceMfa: true, selectedMfaMethod: MfaMethod.WEBAUTHN };
+
test("lets an account with no factor enroll its first one unchallenged", async () => {
+ const service = buildMfaService({ memberOrgIds: [ENFORCED_ORG_ID], orgs: [passkeyEnforcingOrg] });
+
+ await expect(service.getFactorEnrollmentStepUpMfaMethods(USER_ID, MfaMethod.WEBAUTHN)).resolves.toBeNull();
+ });
+
+ test("adding a passkey while one exists requires a passkey, not a preferred email", async () => {
const service = buildMfaService({
+ userRow: { isMfaEnabled: true, selectedMfaMethod: MfaMethod.EMAIL },
memberOrgIds: [ENFORCED_ORG_ID],
- orgs: [{ id: ENFORCED_ORG_ID, enforceMfa: true, selectedMfaMethod: MfaMethod.WEBAUTHN }]
+ orgs: [passkeyEnforcingOrg],
+ passkeyCount: 1
});
- await expect(service.getFactorEnrollmentStepUpMfaMethods(USER_ID)).resolves.toBeNull();
+ await expect(service.getFactorEnrollmentStepUpMfaMethods(USER_ID, MfaMethod.WEBAUTHN)).resolves.toEqual({
+ challenge: MfaMethod.WEBAUTHN,
+ accepted: [MfaMethod.WEBAUTHN]
+ });
});
- test("challenges an existing passkey before another one is added", async () => {
+ test("an authenticator app another org uses can't add a passkey while one exists", async () => {
const service = buildMfaService({
- userRow: { isMfaEnabled: true, selectedMfaMethod: MfaMethod.WEBAUTHN },
+ userRow: { isMfaEnabled: true, selectedMfaMethod: MfaMethod.TOTP },
+ memberOrgIds: [ENFORCED_ORG_ID, "totp-org"],
+ orgs: [passkeyEnforcingOrg, { id: "totp-org", enforceMfa: true, selectedMfaMethod: MfaMethod.TOTP }],
+ hasTotp: true,
passkeyCount: 1
});
- await expect(service.getFactorEnrollmentStepUpMfaMethods(USER_ID)).resolves.toEqual({
+ await expect(service.getFactorEnrollmentStepUpMfaMethods(USER_ID, MfaMethod.WEBAUTHN)).resolves.toEqual({
challenge: MfaMethod.WEBAUTHN,
accepted: [MfaMethod.WEBAUTHN]
});
});
- test("challenges the preferred factor and accepts any of the user's factors", async () => {
+ test("the first passkey can be proven with any factor, preferring the user's choice", async () => {
const service = buildMfaService({
userRow: { isMfaEnabled: true, selectedMfaMethod: MfaMethod.TOTP },
memberOrgIds: [ENFORCED_ORG_ID],
- orgs: [{ id: ENFORCED_ORG_ID, enforceMfa: true, selectedMfaMethod: MfaMethod.WEBAUTHN }],
- hasTotp: true,
- passkeyCount: 2
+ orgs: [passkeyEnforcingOrg],
+ hasTotp: true
});
- await expect(service.getFactorEnrollmentStepUpMfaMethods(USER_ID)).resolves.toEqual({
+ await expect(service.getFactorEnrollmentStepUpMfaMethods(USER_ID, MfaMethod.WEBAUTHN)).resolves.toEqual({
challenge: MfaMethod.TOTP,
- accepted: [MfaMethod.WEBAUTHN, MfaMethod.TOTP]
+ accepted: [MfaMethod.TOTP]
});
});
- test("counts email MFA the user turned on, even where an org requires a factor they lack", async () => {
+ test("the first authenticator app can be proven with any factor", async () => {
+ const service = buildMfaService({
+ userRow: { isMfaEnabled: true, selectedMfaMethod: MfaMethod.WEBAUTHN },
+ passkeyCount: 1
+ });
+
+ await expect(service.getFactorEnrollmentStepUpMfaMethods(USER_ID, MfaMethod.TOTP)).resolves.toEqual({
+ challenge: MfaMethod.WEBAUTHN,
+ accepted: [MfaMethod.WEBAUTHN]
+ });
+ });
+
+ test("email MFA the user turned on proves the first passkey an org newly requires", async () => {
const service = buildMfaService({
userRow: { isMfaEnabled: true, selectedMfaMethod: MfaMethod.EMAIL },
memberOrgIds: [ENFORCED_ORG_ID],
- orgs: [{ id: ENFORCED_ORG_ID, enforceMfa: true, selectedMfaMethod: MfaMethod.WEBAUTHN }]
+ orgs: [passkeyEnforcingOrg]
});
- await expect(service.getFactorEnrollmentStepUpMfaMethods(USER_ID)).resolves.toEqual({
+ await expect(service.getFactorEnrollmentStepUpMfaMethods(USER_ID, MfaMethod.WEBAUTHN)).resolves.toEqual({
challenge: MfaMethod.EMAIL,
accepted: [MfaMethod.EMAIL]
});
@@ -415,7 +442,7 @@ describe("getFactorEnrollmentStepUpMfaMethods", () => {
orgs: [{ id: ENFORCED_ORG_ID, enforceMfa: true, selectedMfaMethod: MfaMethod.EMAIL }]
});
- await expect(service.getFactorEnrollmentStepUpMfaMethods(USER_ID)).resolves.toEqual({
+ await expect(service.getFactorEnrollmentStepUpMfaMethods(USER_ID, MfaMethod.WEBAUTHN)).resolves.toEqual({
challenge: MfaMethod.EMAIL,
accepted: [MfaMethod.EMAIL]
});
@@ -427,6 +454,6 @@ describe("getFactorEnrollmentStepUpMfaMethods", () => {
smtp: false
});
- await expect(service.getFactorEnrollmentStepUpMfaMethods(USER_ID)).resolves.toBeNull();
+ await expect(service.getFactorEnrollmentStepUpMfaMethods(USER_ID, MfaMethod.WEBAUTHN)).resolves.toBeNull();
});
});
diff --git a/backend/src/services/user/user-service.ts b/backend/src/services/user/user-service.ts
index 3d033df5a8..4a5a4c923d 100644
--- a/backend/src/services/user/user-service.ts
+++ b/backend/src/services/user/user-service.ts
@@ -693,11 +693,14 @@ export const userServiceFactory = ({
};
// Adding a factor must be proven with one the account already has, or a password-only
- // session could plant its own. Any of the user's factors will do: org enforcement governs
- // how a user logs in, not which factor proves it is them. Returns null for an account
- // with none, so enforced-MFA onboarding at login can still enroll the first one.
+ // session could plant its own. A new factor of a kind the user already has passes every
+ // login that asks for that kind, so only that kind can prove it; otherwise a weaker factor
+ // could open an org that enforces the stronger one. Mirrors removal (getStepUpMfaMethod).
+ // The first factor of a kind takes any of the user's factors, and an account with none
+ // gets null so enforced-MFA onboarding at login still works.
const getFactorEnrollmentStepUpMfaMethods = async (
- userId: string
+ userId: string,
+ enrollingMethod: MfaMethod
): Promise<{ challenge: MfaMethod; accepted: MfaMethod[] } | null> => {
const user = await userDAL.findById(userId);
if (!user) throw new NotFoundError({ message: "User not found" });
@@ -712,15 +715,16 @@ export const userServiceFactory = ({
enforcingOrgs.some((org) => (org.selectedMfaMethod ?? MfaMethod.EMAIL) === MfaMethod.EMAIL);
const hasEmail = isEmailRequiredAtLogin && (await isMfaMethodConfigured(user, MfaMethod.EMAIL));
- const accepted = [
+ const userFactors = [
...(hasPasskey ? [MfaMethod.WEBAUTHN] : []),
...(hasTotp ? [MfaMethod.TOTP] : []),
...(hasEmail ? [MfaMethod.EMAIL] : [])
];
- if (!accepted.length) return null;
+ if (!userFactors.length) return null;
+ if (userFactors.includes(enrollingMethod)) return { challenge: enrollingMethod, accepted: [enrollingMethod] };
- const preferred = accepted.find((method) => method === user.selectedMfaMethod);
- return { challenge: preferred ?? accepted[0], accepted };
+ const preferred = userFactors.find((method) => method === user.selectedMfaMethod);
+ return { challenge: preferred ?? userFactors[0], accepted: userFactors };
};
const deleteUser = async (userId: string) => {
diff --git a/frontend/src/components/auth/Mfa.tsx b/frontend/src/components/auth/Mfa.tsx
index 95dc3a0538..fb9b5734d6 100644
--- a/frontend/src/components/auth/Mfa.tsx
+++ b/frontend/src/components/auth/Mfa.tsx
@@ -42,7 +42,10 @@ const MAX_MFA_ATTEMPTS = 5;
const CLIENT_RESEND_DELAY_SECONDS = 20;
type Props = {
- successCallback: () => void | Promise<void>;
+ // needsFactorSetup: the user got in with a recovery code instead of setting up the
+ // method their org requires. The caller picks where they go, e.g. a CLI login must
+ // still finish its token handoff first.
+ successCallback: (options?: { needsFactorSetup?: boolean }) => void | Promise<void>;
closeMfa?: () => void;
email: string;
method: MfaMethod;
@@ -132,17 +135,15 @@ export const Mfa = ({ successCallback, closeMfa, email, method, onChangeAccount
};
const completeLogin = async () => {
- await successCallback();
+ await successCallback({ needsFactorSetup: isRecoveringFromSetup });
if (closeMfa) {
closeMfa();
}
- // The recovery code got the user in, but the method the org requires is still not set up.
if (isRecoveringFromSetup) {
createNotification({
- text: `Set up your ${MFA_METHOD_LABELS[method].toLowerCase()} now. Your organization requires it to sign in.`,
+ text: `Set up your ${MFA_METHOD_LABELS[method].toLowerCase()} in Personal Settings. Your organization requires it to sign in.`,
type: "info"
});
- navigate({ to: "/personal-settings", search: { selectedTab: "authentication" } });
}
};
diff --git a/frontend/src/pages/auth/SelectOrgPage/SelectOrgPage.tsx b/frontend/src/pages/auth/SelectOrgPage/SelectOrgPage.tsx
index 46f18abfd6..12f193b1fa 100644
--- a/frontend/src/pages/auth/SelectOrgPage/SelectOrgPage.tsx
+++ b/frontend/src/pages/auth/SelectOrgPage/SelectOrgPage.tsx
@@ -220,7 +220,11 @@ export const SelectOrgPage = () => {
}, [selectedRootOrg, searchTerm]);
// For sub-orgs, inherit the root org's SSO settings but override the ID
- const handleSelectOrganization = async (org: TOrgWithSubOrgs, subOrgId?: string) => {
+ const handleSelectOrganization = async (
+ org: TOrgWithSubOrgs,
+ subOrgId?: string,
+ { needsFactorSetup }: { needsFactorSetup?: boolean } = {}
+ ) => {
const targetOrgId = subOrgId || org.id;
const canBypassOrgAuth = org.bypassOrgAuthEnabled && isBreakglassRoute;
@@ -305,7 +309,9 @@ export const SelectOrgPage = () => {
} else {
setAuthToken(token);
createNotification({ text: "Successfully logged in", type: "success" });
- if (redirectTo?.startsWith(`/organizations/${targetOrgId}/`)) {
+ if (needsFactorSetup) {
+ navigate({ to: "/personal-settings", search: { selectedTab: "authentication" } });
+ } else if (redirectTo?.startsWith(`/organizations/${targetOrgId}/`)) {
navigate({ href: redirectTo });
} else {
navigateUserToOrg({ navigate, organizationId: targetOrgId });
@@ -461,11 +467,12 @@ export const SelectOrgPage = () => {
</Helmet>
<Mfa
email={user.email as string}
- successCallback={() => {
+ successCallback={(options) => {
if (mfaOrgInfo.current) {
return handleSelectOrganization(
mfaOrgInfo.current.rootOrg,
- mfaOrgInfo.current.subOrgId
+ mfaOrgInfo.current.subOrgId,
+ options
);
}
return undefined;