Skip to content

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

fix(org): count created organizations regardless of membership

Vlad Ligaidbadde7b2daa
diff --git a/backend/e2e-test/routes/v2/org-create-limit.spec.ts b/backend/e2e-test/routes/v2/org-create-limit.spec.ts
index 1de7dc6c7a..cdc0a9ebce 100644
--- a/backend/e2e-test/routes/v2/org-create-limit.spec.ts
+++ b/backend/e2e-test/routes/v2/org-create-limit.spec.ts
@@ -6,7 +6,7 @@ import { loginUser, selectOrg } from "../../testUtils/auth";
const orgDAL = orgDALFactory(testDb);
-const countCreatedOrgs = () => orgDAL.countJoinedRootOrgsCreatedByUserId(seedData1.id, testDb);
+const countCreatedOrgs = () => orgDAL.countRootOrgsCreatedByUserId(seedData1.id, testDb);
// Anything left behind would carry into the next test's count and blame the wrong one.
const createdOrgIds: string[] = [];
@@ -68,15 +68,13 @@ describe("Organizations counted against a user's create limit", () => {
await expect(countCreatedOrgs()).resolves.toBe(0);
});
- test("an org the user created but does not belong to does not count", async () => {
+ test("an org the user created but does not belong to still counts", async () => {
await insertOrg({ name: "org-limit-orphan", slug: "org-limit-orphan", createdByUserId: seedData1.id });
- await expect(countCreatedOrgs()).resolves.toBe(0);
+ await expect(countCreatedOrgs()).resolves.toBe(1);
});
- // SCIM sets isActive from the IdP's active flag without touching status, so a deactivated creator
- // would otherwise hold a slot in an org they cannot use.
- test("an org the user is deactivated in does not count", async () => {
+ test("an org the user is deactivated in still counts", async () => {
const orgId = await insertOrg({
name: "org-limit-deactivated",
slug: "org-limit-deactivated",
@@ -90,7 +88,7 @@ describe("Organizations counted against a user's create limit", () => {
status: OrgMembershipStatus.Accepted
});
- await expect(countCreatedOrgs()).resolves.toBe(0);
+ await expect(countCreatedOrgs()).resolves.toBe(1);
});
test("a sub-org does not count", async () => {
diff --git a/backend/src/services/org/org-dal.ts b/backend/src/services/org/org-dal.ts
index a198994449..a0bd9055ca 100644
--- a/backend/src/services/org/org-dal.ts
+++ b/backend/src/services/org/org-dal.ts
@@ -465,33 +465,20 @@ export const orgDALFactory = (db: TDbClient) => {
}
};
- // Root orgs this user created and still belongs to. Orgs they were only invited into never count,
- // so joining any number of organizations stays unrestricted. An org they created but were since
- // removed from, or deactivated in, does not count either: it is no longer theirs to use, and
- // counting it would leave them unable to create one anywhere. Sub-orgs are excluded, they are the
- // supported way to add more. Takes the transaction rather than an optional one so the read cannot land on a replica,
- // where lag would let a second create pass the check the advisory lock exists to serialize.
- const countJoinedRootOrgsCreatedByUserId = async (userId: string, tx: Knex) => {
+ // Membership is deliberately not a condition: with it, a second account can remove the creator to
+ // free the slot and the pair farms orgs indefinitely. Takes the transaction rather than an optional
+ // one so the read cannot land on a replica, where lag would defeat the advisory lock.
+ const countRootOrgsCreatedByUserId = async (userId: string, tx: Knex) => {
try {
const count = await tx(TableName.Organization)
.where(`${TableName.Organization}.createdByUserId`, userId)
.whereNull(`${TableName.Organization}.rootOrgId`)
- .whereExists((qb) => {
- void qb
- .select(tx.raw("1"))
- .from(TableName.Membership)
- .where(`${TableName.Membership}.actorUserId`, userId)
- .where(`${TableName.Membership}.scope`, AccessScope.Organization)
- .where(`${TableName.Membership}.status`, OrgMembershipStatus.Accepted)
- .where(`${TableName.Membership}.isActive`, true)
- .whereRaw(`"${TableName.Membership}"."scopeOrgId" = "${TableName.Organization}"."id"`);
- })
.count("*", { as: "count" })
.first();
return Number((count as unknown as CountResult)?.count ?? 0);
} catch (error) {
- throw new DatabaseError({ error, name: "Count joined root orgs created by user id" });
+ throw new DatabaseError({ error, name: "Count root orgs created by user id" });
}
};
@@ -1171,7 +1158,7 @@ export const orgDALFactory = (db: TDbClient) => {
findOrgById,
findOrgBySlug,
findAllOrgsByUserId,
- countJoinedRootOrgsCreatedByUserId,
+ countRootOrgsCreatedByUserId,
findOrganizationsByFilter,
findOrgMembersByUsername,
findOrgMembersByRole,
diff --git a/backend/src/services/org/org-service.ts b/backend/src/services/org/org-service.ts
index d667e6abf3..b57cb8903f 100644
--- a/backend/src/services/org/org-service.ts
+++ b/backend/src/services/org/org-service.ts
@@ -693,19 +693,17 @@ export const orgServiceFactory = ({
}: {
userId?: string;
orgName: string;
- // Set by the user-facing create-org endpoint, and only on cloud. Signup and instance-admin
- // creation call this without it and keep their existing behavior.
+ // Set by the user-facing create-org endpoint, and only on cloud.
blockIfUserHasCreatedOrg?: boolean;
},
trx?: Knex
) => {
const createOrg = async (tx: Knex) => {
- // The advisory lock serializes concurrent creates for the same user so two requests cannot both
- // read a count of zero and each land an org.
+ // Serializes concurrent creates so two requests cannot both read a count of zero.
if (blockIfUserHasCreatedOrg && userId) {
await tx.raw("SELECT pg_advisory_xact_lock(?)", [PgSqlLock.CreateOrganization(userId)]);
- const createdOrgs = await orgDAL.countJoinedRootOrgsCreatedByUserId(userId, tx);
+ const createdOrgs = await orgDAL.countRootOrgsCreatedByUserId(userId, tx);
if (createdOrgs > 0) {
throw new ConflictError({
message: