Skip to content

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

fix Cross-tenant placeholder adoption

Thiago Araujo da Silva2b9c6683e9ab
diff --git a/backend/CLAUDE.md b/backend/CLAUDE.md
index 1fb6704a57..15f8e40b69 100644
--- a/backend/CLAUDE.md
+++ b/backend/CLAUDE.md
@@ -378,9 +378,22 @@ in, leaving a placeholder account keyed on the identifier instead of the mailbox
`adoptProvisionedShadowUser` (same file, wired into `oidcLogin`'s no-alias branch) adopts that row
and rewrites it to the asserted mailbox rather than creating a second account. It refuses anything a
human has claimed (accepted, email-verified, holding a password), anything already bound to an IdP
-(any alias, any org), ghosts, and anything without a membership in the org doing the login. It also
-recovers from a unique violation on `users.username`, because the caller's preceding read is not a
-lock. SAML and LDAP have structurally identical branches and are deliberately not wired up.
+(any alias, any org), ghosts, and anything without a membership in the org doing the login.
+
+It also refuses a placeholder that holds an org membership **outside** the login org's own sub-org
+family, and that one is the security-critical check rather than a tidiness one. The username lookup
+that finds the placeholder is global, so a second tenant that invited the same identifier shares the
+row; adopting it would hand the login org's IdP subject that tenant's memberships, and
+`selectOrganization` accepts a membership in any status (promoting `Invited` to `Accepted` on
+arrival), so nothing downstream stops the inherited access from being used. A project membership
+always implies an org membership in the same org, so the org-scope check covers project access too.
+
+Adoption also recovers from a unique violation on `users.username`, because the caller's preceding
+read is not a lock. That recovery has to run inside a savepoint (`tx.transaction()`, which knex
+compiles to `SAVEPOINT`/`ROLLBACK TO SAVEPOINT` on the same connection): Postgres aborts the whole
+transaction on a constraint violation, so an unscoped retry would fail with `25P02`, taking the
+caller's remaining alias and membership writes with it. SAML and LDAP have structurally identical
+branches and are deliberately not wired up.
### Permission System (CASL)
diff --git a/backend/src/ee/services/oidc/oidc-config-service.ts b/backend/src/ee/services/oidc/oidc-config-service.ts
index 75995112db..4a6f32e22f 100644
--- a/backend/src/ee/services/oidc/oidc-config-service.ts
+++ b/backend/src/ee/services/oidc/oidc-config-service.ts
@@ -91,7 +91,7 @@ type TOidcConfigServiceFactoryDep = {
userAliasDAL: Pick<TUserAliasDALFactory, "create" | "findOne" | "updateById">;
orgDAL: Pick<
TOrgDALFactory,
- "createMembership" | "updateMembershipById" | "findMembership" | "findOrgById" | "findOne" | "updateById"
+ "createMembership" | "updateMembershipById" | "findMembership" | "findOrgById" | "findOne" | "find" | "updateById"
>;
membershipGroupDAL: Pick<TMembershipGroupDALFactory, "find">;
membershipRoleDAL: Pick<TMembershipRoleDALFactory, "create">;
@@ -319,6 +319,7 @@ export const oidcConfigServiceFactory = ({
externalId,
assertedEmail: sanitizedEmail,
orgId,
+ rootOrgId: organization.rootOrgId,
userDAL,
userAliasDAL,
orgDAL,
diff --git a/backend/src/services/user-alias/user-alias-fns.test.ts b/backend/src/services/user-alias/user-alias-fns.test.ts
index d020700354..d7ee96c637 100644
--- a/backend/src/services/user-alias/user-alias-fns.test.ts
+++ b/backend/src/services/user-alias/user-alias-fns.test.ts
@@ -98,14 +98,18 @@ const shadowUser = (overrides: Partial<TUsers> = {}) =>
...overrides
}) as TUsers;
+const OTHER_ORG_ID = "org-2";
+
const buildDeps = ({
candidate = shadowUser(),
existingAlias = null,
- membership = [{ id: "membership-1" }]
+ membership = [{ id: "membership-1", scopeOrgId: ORG_ID }],
+ orgs = []
}: {
candidate?: TUsers | null;
existingAlias?: object | null;
membership?: object[];
+ orgs?: object[];
} = {}) => {
const userDAL = {
findOne: vi.fn().mockResolvedValue(candidate),
@@ -114,7 +118,10 @@ const buildDeps = ({
.mockImplementation((id: string, update: Partial<TUsers>) => ({ ...shadowUser(), ...update, id }))
};
const userAliasDAL = { findOne: vi.fn().mockResolvedValue(existingAlias) };
- const orgDAL = { findMembership: vi.fn().mockResolvedValue(membership) };
+ const orgDAL = {
+ findMembership: vi.fn().mockResolvedValue(membership),
+ find: vi.fn().mockResolvedValue(orgs)
+ };
// A real tx aborts on the unique violation, so the update has to run inside a savepoint for the
// recovery lookup (and the caller's remaining writes) to stay runnable. `transaction` off a tx is
// knex's savepoint; the handle is distinct here so the tests can tell the two scopes apart.
@@ -124,11 +131,16 @@ const buildDeps = ({
return { userDAL, userAliasDAL, orgDAL, tx, savepoint };
};
-const adopt = ({ tx, savepoint: _savepoint, ...deps }: ReturnType<typeof buildDeps>, externalId = EXTERNAL_ID) =>
+const adopt = (
+ { tx, savepoint: _savepoint, ...deps }: ReturnType<typeof buildDeps>,
+ externalId = EXTERNAL_ID,
+ rootOrgId: string | null = null
+) =>
adoptProvisionedShadowUser({
externalId,
assertedEmail: ASSERTED_EMAIL,
orgId: ORG_ID,
+ rootOrgId,
tx: tx as unknown as Knex,
...deps
});
@@ -189,12 +201,39 @@ describe("adoptProvisionedShadowUser", () => {
});
test("declines when the placeholder holds no membership in this org", async () => {
- const deps = buildDeps({ membership: [] });
+ const deps = buildDeps({ membership: [{ id: "membership-1", scopeOrgId: OTHER_ORG_ID }] });
+
+ expect(await adopt(deps)).toBeNull();
+ expect(deps.userDAL.updateById).not.toHaveBeenCalled();
+ });
+
+ // Username lookup is global, so another tenant that invited the same identifier shares this row.
+ // Adopting it would hand this org's IdP subject that tenant's memberships and project access.
+ test("declines when the placeholder also holds a membership in an unrelated org", async () => {
+ const deps = buildDeps({
+ membership: [
+ { id: "membership-1", scopeOrgId: ORG_ID },
+ { id: "membership-2", scopeOrgId: OTHER_ORG_ID }
+ ],
+ orgs: [{ id: OTHER_ORG_ID, rootOrgId: null }]
+ });
expect(await adopt(deps)).toBeNull();
expect(deps.userDAL.updateById).not.toHaveBeenCalled();
});
+ test("adopts when the extra membership is a sub-org of this one, which is the same tenant", async () => {
+ const deps = buildDeps({
+ membership: [
+ { id: "membership-1", scopeOrgId: ORG_ID },
+ { id: "membership-2", scopeOrgId: OTHER_ORG_ID }
+ ],
+ orgs: [{ id: OTHER_ORG_ID, rootOrgId: ORG_ID }]
+ });
+
+ expect((await adopt(deps))?.username).toBe(ASSERTED_EMAIL);
+ });
+
test("yields to the winner when a concurrent write takes the asserted email first", async () => {
const deps = buildDeps();
const winner = { ...shadowUser(), id: "winner-1", username: ASSERTED_EMAIL } as TUsers;
diff --git a/backend/src/services/user-alias/user-alias-fns.ts b/backend/src/services/user-alias/user-alias-fns.ts
index b84337444d..66bf60906e 100644
--- a/backend/src/services/user-alias/user-alias-fns.ts
+++ b/backend/src/services/user-alias/user-alias-fns.ts
@@ -198,9 +198,10 @@ type TAdoptProvisionedShadowUserDTO = {
// Caller has already sanitized this and verified its domain against the org.
assertedEmail: string;
orgId: string;
+ rootOrgId?: string | null;
userDAL: Pick<TUserDALFactory, "findOne" | "updateById">;
userAliasDAL: Pick<TUserAliasDALFactory, "findOne">;
- orgDAL: Pick<TOrgDALFactory, "findMembership">;
+ orgDAL: Pick<TOrgDALFactory, "findMembership" | "find">;
tx: Knex;
};
@@ -217,6 +218,7 @@ export const adoptProvisionedShadowUser = async ({
externalId,
assertedEmail,
orgId,
+ rootOrgId,
userDAL,
userAliasDAL,
orgDAL,
@@ -241,15 +243,28 @@ export const adoptProvisionedShadowUser = async ({
if (existingAlias) return null;
// Keeps this to placeholders the org itself created through an authorized invite.
- const [orgMembership] = await orgDAL.findMembership(
+ const orgMemberships = await orgDAL.findMembership(
{
[`${TableName.Membership}.actorUserId` as "actorUserId"]: candidate.id,
- [`${TableName.Membership}.scopeOrgId` as "scopeOrgId"]: orgId,
[`${TableName.Membership}.scope` as "scope"]: AccessScope.Organization
},
{ tx }
);
- if (!orgMembership) return null;
+ if (!orgMemberships.some((membership) => membership.scopeOrgId === orgId)) return null;
+
+ // That membership proves the placeholder belongs to this org, not that it belongs only to this
+ // org. Username lookup is global, so a second tenant that invited the same identifier shares the
+ // row, and adopting it would hand this org's IdP subject that tenant's memberships and project
+ // access. Sub-orgs of one root are one tenant; anything reaching past that is left alone. A
+ // project membership always implies an org membership in the same org, so this covers both.
+ const familyRootOrgId = rootOrgId || orgId;
+ const outsideOrgIds = unique(
+ orgMemberships.map((membership) => membership.scopeOrgId).filter((scopeOrgId) => scopeOrgId !== orgId)
+ );
+ if (outsideOrgIds.length) {
+ const outsideOrgs = await orgDAL.find({ $in: { id: outsideOrgIds } }, { tx });
+ if (outsideOrgs.some((org) => (org.rootOrgId || org.id) !== familyRootOrgId)) return null;
+ }
// Postgres aborts the whole transaction on a constraint violation, so the update needs its own
// savepoint: without one, the recovery lookup below and every write the caller still owes after