Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.
TypeScript acmeagentsai-agentscertificate-managementdevsecopsenvironment-variableskubernetesopen-sourcepampkipostgresprivate-caprivileged-access-managementsecret-managementsecret-managersecretssecrets-managementsecuritysecurity-toolsvault
fix(org): require membership in the created-organization count
Vlad Ligai325a08851ef1
diff --git a/backend/e2e-test/routes/v2/org-create-limit.spec.ts b/backend/e2e-test/routes/v2/org-create-limit.spec.tsindex cdc0a9ebce..44d5f4f8a2 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.countRootOrgsCreatedByUserId(seedData1.id, testDb);+const countCreatedOrgs = () => orgDAL.countJoinedRootOrgsCreatedByUserId(seedData1.id, testDb);// Anything left behind would carry into the next test's count and blame the wrong one.const createdOrgIds: string[] = [];@@ -68,13 +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 still counts", async () => {+ test("an org the user created but does not belong to does not count", async () => {await insertOrg({ name: "org-limit-orphan", slug: "org-limit-orphan", createdByUserId: seedData1.id });- await expect(countCreatedOrgs()).resolves.toBe(1);+ await expect(countCreatedOrgs()).resolves.toBe(0);});- test("an org the user is deactivated in still counts", async () => {+ test("an org the user is deactivated in does not count", async () => {const orgId = await insertOrg({name: "org-limit-deactivated",slug: "org-limit-deactivated",@@ -88,7 +88,7 @@ describe("Organizations counted against a user's create limit", () => {status: OrgMembershipStatus.Accepted});- await expect(countCreatedOrgs()).resolves.toBe(1);+ await expect(countCreatedOrgs()).resolves.toBe(0);});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.tsindex a0bd9055ca..74731e41be 100644--- a/backend/src/services/org/org-dal.ts+++ b/backend/src/services/org/org-dal.ts@@ -465,20 +465,30 @@ export const orgDALFactory = (db: TDbClient) => {}};- // 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) => {+ // Membership is required so a removed or deactivated creator is not locked out everywhere. A second+ // account with admin can therefore free the slot; accepted, since that costs an invite cycle per org.+ // Takes the transaction, not an optional one, so the read cannot hit a replica and defeat the lock.+ const countJoinedRootOrgsCreatedByUserId = 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 root orgs created by user id" });+ throw new DatabaseError({ error, name: "Count joined root orgs created by user id" });}};@@ -1158,7 +1168,7 @@ export const orgDALFactory = (db: TDbClient) => {findOrgById,findOrgBySlug,findAllOrgsByUserId,- countRootOrgsCreatedByUserId,+ countJoinedRootOrgsCreatedByUserId,findOrganizationsByFilter,findOrgMembersByUsername,findOrgMembersByRole,diff --git a/backend/src/services/org/org-service.ts b/backend/src/services/org/org-service.tsindex b57cb8903f..46a5350580 100644--- a/backend/src/services/org/org-service.ts+++ b/backend/src/services/org/org-service.ts@@ -703,7 +703,7 @@ export const orgServiceFactory = ({if (blockIfUserHasCreatedOrg && userId) {await tx.raw("SELECT pg_advisory_xact_lock(?)", [PgSqlLock.CreateOrganization(userId)]);- const createdOrgs = await orgDAL.countRootOrgsCreatedByUserId(userId, tx);+ const createdOrgs = await orgDAL.countJoinedRootOrgsCreatedByUserId(userId, tx);if (createdOrgs > 0) {throw new ConflictError({message: