Skip to content

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

check root org for bypass

Thiago Araujo da Silva12e414e86a42
diff --git a/backend/AGENTS.md b/backend/AGENTS.md
index e5034b5b45..b0cbc602b8 100644
--- a/backend/AGENTS.md
+++ b/backend/AGENTS.md
@@ -306,7 +306,10 @@ enforces SSO admits only sessions its own IdP issued: `validateOrgSSO` compares
org's root org on every user permission check, reading it from `userAuthInfo` in the request context
(only when the check is for the requester), so it covers `selectOrganization`, OAuth consent, and a
session that was already open when enforcement was turned on. Members who can bypass SSO are let
-through, and a token with no claim (issued before it existed) is taken at its auth method.
+through, and a token with no claim (issued before it existed) is taken at its auth method. A sub-org
+has no enforcement of its own: `permissionDAL.getPermission` reads `authEnforced`,
+`googleSsoAuthEnforced` and `bypassOrgAuthEnabled` from its root org, and only a root org role can
+bypass them, so a sub-org admin role exempts no one.
**Every path that issues or re-issues a user token must carry the claim forward** (MFA, refresh,
signup, org deletion, delegated OAuth codes and tokens). A dropped claim reads as a pre-claim token
diff --git a/backend/e2e-test/routes/v3/auth-sso-session-binding.spec.ts b/backend/e2e-test/routes/v3/auth-sso-session-binding.spec.ts
index a6fc32360e..58506c0fa1 100644
--- a/backend/e2e-test/routes/v3/auth-sso-session-binding.spec.ts
+++ b/backend/e2e-test/routes/v3/auth-sso-session-binding.spec.ts
@@ -157,6 +157,83 @@ describe("Auth SSO session binding", () => {
expect(refused.json().message).toMatch(/another organization's identity provider/);
});
+ describe("Sub-organizations", () => {
+ let subOrgId: string;
+
+ const addMemberToSubOrg = async (userId: string, role: OrgMembershipRole) => {
+ const db = getDb();
+ const [membership] = await db(TableName.Membership)
+ .insert({
+ scope: AccessScope.Organization,
+ actorUserId: userId,
+ scopeOrgId: subOrgId,
+ status: OrgMembershipStatus.Accepted,
+ isActive: true
+ })
+ .returning("*");
+ await db(TableName.MembershipRole).insert({ membershipId: membership.id, role });
+ };
+
+ // A session another org's IdP issued, already open in a sub-org of org A before A enforces SSO.
+ const openSubOrgSession = async (subOrgRole: OrgMembershipRole) => {
+ const { userId, tokens } = await loginThroughOrgBIdp();
+ await addMemberToOrgA(userId);
+ await addMemberToSubOrg(userId, subOrgRole);
+ const { statusCode, payload } = await selectOrg(tokens.access, subOrgId);
+ expect(statusCode).toBe(200);
+ return () =>
+ testServer.inject({
+ method: "GET",
+ url: `/api/v2/organizations/${subOrgId}/memberships`,
+ headers: { authorization: `Bearer ${payload.token}` }
+ });
+ };
+
+ beforeAll(async () => {
+ const [subOrg] = await getDb()(TableName.Organization)
+ .insert({
+ name: "Session Binding Sub Org",
+ slug: `binding-sub-${crypto.randomUUID().slice(0, 8)}`,
+ rootOrgId: ORG_A_ID,
+ parentOrgId: ORG_A_ID
+ })
+ .returning("*");
+ subOrgId = subOrg.id;
+ });
+
+ afterEach(async () => {
+ await getDb()(TableName.Organization).where({ id: ORG_A_ID }).update({ bypassOrgAuthEnabled: false });
+ });
+
+ afterAll(async () => {
+ const db = getDb();
+ await db(TableName.Membership).where({ scopeOrgId: subOrgId }).del();
+ await db(TableName.Organization).where({ id: subOrgId }).del();
+ });
+
+ test("A sub-org session another org's IdP issued stops working once the root org starts enforcing SSO", async () => {
+ const listSubOrgMemberships = await openSubOrgSession(OrgMembershipRole.Member);
+ expect((await listSubOrgMemberships()).statusCode).toBe(200);
+
+ await setAuthEnforced(ORG_A_ID, true);
+ const refused = await listSubOrgMemberships();
+
+ expect(refused.statusCode).toBe(403);
+ expect(refused.json().message).toMatch(/another organization's identity provider/);
+ });
+
+ test("A sub-org admin role does not bypass the root org's enforcement", async () => {
+ const listSubOrgMemberships = await openSubOrgSession(OrgMembershipRole.Admin);
+
+ await getDb()(TableName.Organization).where({ id: ORG_A_ID }).update({ bypassOrgAuthEnabled: true });
+ await setAuthEnforced(ORG_A_ID, true);
+ const refused = await listSubOrgMemberships();
+
+ expect(refused.statusCode).toBe(403);
+ expect(refused.json().message).toMatch(/another organization's identity provider/);
+ });
+ });
+
test("An enforced org's OAuth application refuses consent from a session another org's IdP issued", async () => {
const created = await testServer.inject({
method: "POST",
diff --git a/backend/src/ee/services/permission/permission-dal.ts b/backend/src/ee/services/permission/permission-dal.ts
index 17354aea9b..3919385b80 100644
--- a/backend/src/ee/services/permission/permission-dal.ts
+++ b/backend/src/ee/services/permission/permission-dal.ts
@@ -414,7 +414,21 @@ export const permissionDALFactory = (db: TDbClient): TPermissionDALFactory => {
]
});
- return data;
+ // SSO enforcement is set on the root org only (sub-orgs cannot change it), so a sub-org takes its
+ // root's settings; otherwise a sub-org session would never be checked against them.
+ const rootOrgId = data[0]?.rootOrgId;
+ if (!rootOrgId) return data;
+ const rootOrg = await (tx || db)
+ .replicaNode()(TableName.Organization)
+ .where({ id: rootOrgId })
+ .select("authEnforced", "googleSsoAuthEnforced", "bypassOrgAuthEnabled")
+ .first();
+ return data.map((membership) => ({
+ ...membership,
+ orgAuthEnforced: rootOrg?.authEnforced,
+ orgGoogleSsoAuthEnforced: Boolean(rootOrg?.googleSsoAuthEnforced),
+ bypassOrgAuthEnabled: Boolean(rootOrg?.bypassOrgAuthEnabled)
+ }));
} catch (error) {
throw new DatabaseError({ error, name: "Get Permission" });
}
diff --git a/backend/src/ee/services/permission/permission-service.ts b/backend/src/ee/services/permission/permission-service.ts
index 48eef05625..a600627dde 100644
--- a/backend/src/ee/services/permission/permission-service.ts
+++ b/backend/src/ee/services/permission/permission-service.ts
@@ -450,7 +450,24 @@ export const permissionServiceFactory = ({
conditionsMatcher
});
- const canBypassSso = fullPermission.can(OrgPermissionSsoActions.BypassSsoEnforcement, OrgPermissionSubjects.Sso);
+ let canBypassSso = fullPermission.can(OrgPermissionSsoActions.BypassSsoEnforcement, OrgPermissionSubjects.Sso);
+
+ // A sub-org inherits its root's enforcement, so only a root org role may bypass it, the same as
+ // selectOrganization and project permissions decide it; a sub-org role must not exempt anyone.
+ const isSsoEnforced = permissionData[0].orgAuthEnforced || permissionData[0].orgGoogleSsoAuthEnforced;
+ if (actor === ActorType.USER && rootOrgId && isSsoEnforced) {
+ const rootPermissionData = await permissionDAL.getPermission({
+ scopeData: { scope: AccessScope.Organization, orgId: rootOrgId },
+ actorId,
+ actorType: ActorType.USER
+ });
+ canBypassSso =
+ rootPermissionData.length > 0 &&
+ createMongoAbility<OrgPermissionSet>(
+ buildOrgPermissionRules(flattenActiveRolesFromMemberships(rootPermissionData, OrgMembershipRole.Custom)),
+ { conditionsMatcher }
+ ).can(OrgPermissionSsoActions.BypassSsoEnforcement, OrgPermissionSubjects.Sso);
+ }
// SSO enforcement applies only to users. Evaluate it against the full ability — SSO bypass is a
// property of the user's roles, not of what an OAuth client was delegated.