Skip to content

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

throw in case of inactive membership

Thiago Araujo da Silva435f3d3a4ce7
diff --git a/backend/CLAUDE.md b/backend/CLAUDE.md
index 58e6176f19..4b1a1343b4 100644
--- a/backend/CLAUDE.md
+++ b/backend/CLAUDE.md
@@ -386,6 +386,13 @@ human has claimed (accepted, email-verified, holding a password), anything alrea
(any alias, any org), ghosts, anything whose identifier is not already canonical (see above), and
anything without a membership in the org doing the login.
+One case is a refusal to *log in* rather than a refusal to adopt: a placeholder whose membership in
+the login org is **inactive**. Declining is not neutral there, because the caller reads a `null` as
+"no placeholder" and creates a second account with a fresh active membership, handing a deactivated
+person their org back (`selectOrganization` then accepts it). So the inactive check is resolved
+before every remaining decline — the alias check and the cross-tenant check both sit after it — and
+it throws the same `ForbiddenRequestError` an already-aliased deactivated member gets.
+
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
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 25ec9d915d..10b38b7d77 100644
--- a/backend/src/services/user-alias/user-alias-fns.test.ts
+++ b/backend/src/services/user-alias/user-alias-fns.test.ts
@@ -3,7 +3,7 @@ import { describe, expect, test, vi } from "vitest";
import { TUsers } from "@app/db/schemas";
import { DatabaseErrorCode } from "@app/lib/error-codes";
-import { DatabaseError } from "@app/lib/errors";
+import { DatabaseError, ForbiddenRequestError } from "@app/lib/errors";
import { adoptProvisionedShadowUser, resolveAliasUserIds } from "./user-alias-fns";
@@ -103,7 +103,7 @@ const OTHER_ORG_ID = "org-2";
const buildDeps = ({
candidate = shadowUser(),
existingAlias = null,
- membership = [{ id: "membership-1", scopeOrgId: ORG_ID }],
+ membership = [{ id: "membership-1", scopeOrgId: ORG_ID, isActive: true }],
orgs = []
}: {
candidate?: TUsers | null;
@@ -221,7 +221,7 @@ describe("adoptProvisionedShadowUser", () => {
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-1", scopeOrgId: ORG_ID, isActive: true },
{ id: "membership-2", scopeOrgId: OTHER_ORG_ID }
],
orgs: [{ id: OTHER_ORG_ID, rootOrgId: null }]
@@ -234,7 +234,7 @@ describe("adoptProvisionedShadowUser", () => {
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-1", scopeOrgId: ORG_ID, isActive: true },
{ id: "membership-2", scopeOrgId: OTHER_ORG_ID }
],
orgs: [{ id: OTHER_ORG_ID, rootOrgId: ORG_ID }]
@@ -243,6 +243,29 @@ describe("adoptProvisionedShadowUser", () => {
expect((await adopt(deps))?.user.username).toBe(ASSERTED_EMAIL);
});
+ // Declining is not neutral for a deactivated member: the caller reads null as "no placeholder"
+ // and creates a second account with a fresh active membership, handing them the org back.
+ test("fails the login when this org's placeholder has been deactivated", async () => {
+ const deps = buildDeps({ membership: [{ id: "membership-1", scopeOrgId: ORG_ID, isActive: false }] });
+
+ await expect(adopt(deps)).rejects.toThrow(ForbiddenRequestError);
+ expect(deps.userDAL.updateById).not.toHaveBeenCalled();
+ });
+
+ test("fails the login for a deactivated placeholder even when adoption would be declined anyway", async () => {
+ const deps = buildDeps({
+ existingAlias: { id: "alias-1" },
+ membership: [
+ { id: "membership-1", scopeOrgId: ORG_ID, isActive: false },
+ { id: "membership-2", scopeOrgId: OTHER_ORG_ID, isActive: true }
+ ],
+ orgs: [{ id: OTHER_ORG_ID, rootOrgId: null }]
+ });
+
+ await expect(adopt(deps)).rejects.toThrow(ForbiddenRequestError);
+ expect(deps.userDAL.updateById).not.toHaveBeenCalled();
+ });
+
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 3310e7bdee..b16598455b 100644
--- a/backend/src/services/user-alias/user-alias-fns.ts
+++ b/backend/src/services/user-alias/user-alias-fns.ts
@@ -2,7 +2,7 @@ import { Knex } from "knex";
import { AccessScope, TableName, TUserAliases, TUsers } from "@app/db/schemas";
import { DatabaseErrorCode } from "@app/lib/error-codes";
-import { DatabaseError } from "@app/lib/errors";
+import { DatabaseError, ForbiddenRequestError } from "@app/lib/errors";
import { unique } from "@app/lib/fn";
import { sanitizeEmail } from "@app/lib/validator/validate-email";
@@ -219,6 +219,10 @@ type TAdoptProvisionedShadowUserResult = {
* Returns null if there is nothing safe to adopt. Only call this once a lookup on the asserted
* email has missed. A non-null result carries `adoptedFromUsername`, which is null on the yield
* path: the user is real either way, but only a non-null value means an account was rewritten.
+ *
+ * Throws when the placeholder is this org's own but its membership has been deactivated. A null
+ * there would read as "no placeholder" and let the caller create a second account with a fresh
+ * active membership, so the deactivation has to fail the login instead.
*/
export const adoptProvisionedShadowUser = async ({
externalId,
@@ -245,11 +249,6 @@ export const adoptProvisionedShadowUser = async ({
// email-verified, or has a password all mean it belongs to someone.
if (candidate.isGhost || candidate.isAccepted || candidate.isEmailVerified || candidate.hashedPassword) return null;
- // Any alias, in any org, means some IdP already owns this account. Without this check one org's
- // IdP could steal an account bound to another's.
- const existingAlias = await userAliasDAL.findOne({ userId: candidate.id }, tx);
- if (existingAlias) return null;
-
// Keeps this to placeholders the org itself created through an authorized invite.
const orgMemberships = await orgDAL.findMembership(
{
@@ -258,7 +257,22 @@ export const adoptProvisionedShadowUser = async ({
},
{ tx }
);
- if (!orgMemberships.some((membership) => membership.scopeOrgId === orgId)) return null;
+ const orgMembership = orgMemberships.find((membership) => membership.scopeOrgId === orgId);
+ if (!orgMembership) return null;
+
+ // Resolved before every remaining decline, because declining is not neutral here: the caller
+ // reads a null as "no placeholder" and creates a second account with a fresh active membership,
+ // handing a deactivated person their org back. Adopting is no better, since it mutates a
+ // deactivated account. Failing the login is the only safe answer, and it is what an already
+ // aliased deactivated member gets.
+ if (!orgMembership.isActive) {
+ throw new ForbiddenRequestError({ message: "User organization membership is inactive" });
+ }
+
+ // Any alias, in any org, means some IdP already owns this account. Without this check one org's
+ // IdP could steal an account bound to another's.
+ const existingAlias = await userAliasDAL.findOne({ userId: candidate.id }, tx);
+ if (existingAlias) 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