Skip to content

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

assert project permission

Thiago Araujo da Silva35a1de821750
diff --git a/backend/src/services/alert/providers/identity-credential-alert-dal.test.ts b/backend/src/services/alert/providers/identity-credential-alert-dal.test.ts
new file mode 100644
index 0000000000..7f1f7e95bb
--- /dev/null
+++ b/backend/src/services/alert/providers/identity-credential-alert-dal.test.ts
@@ -0,0 +1,92 @@
+import { TableName } from "@app/db/schemas";
+
+import { identityCredentialAlertDALFactory } from "./identity-credential-alert-dal";
+
+// Records the query builder calls so we can assert which identities a scan is allowed to return.
+// This is the enforcement point for the permission boundary of a project-owned identity: an
+// org-scoped alert only ever checks org-level identity permission, so it must never surface the
+// credential metadata of an identity whose reads are gated by a project (see
+// getUniversalAuthClientSecrets in identity-ua-service).
+const buildDAL = () => {
+ const calls = {
+ join: [] as unknown[][],
+ where: [] as unknown[][],
+ whereNull: [] as unknown[],
+ // Calls made inside a `.where((builder) => ...)` group.
+ groupedWhereNull: [] as unknown[],
+ groupedOrWhere: [] as unknown[][]
+ };
+
+ const group = {
+ whereNull: (col: unknown) => {
+ calls.groupedWhereNull.push(col);
+ return group;
+ },
+ orWhere: (...args: unknown[]) => {
+ calls.groupedOrWhere.push(args);
+ return group;
+ }
+ };
+
+ const chain = {
+ join: (...args: unknown[]) => {
+ calls.join.push(args);
+ return chain;
+ },
+ where: (...args: unknown[]) => {
+ if (typeof args[0] === "function") {
+ (args[0] as (builder: typeof group) => void)(group);
+ return chain;
+ }
+ calls.where.push(args);
+ return chain;
+ },
+ whereNull: (col: unknown) => {
+ calls.whereNull.push(col);
+ return chain;
+ },
+ whereRaw: () => chain,
+ orderByRaw: () => chain,
+ select: async () => []
+ };
+
+ const db = {
+ replicaNode: () => () => chain,
+ ref: (col: string) => ({ withSchema: () => ({ as: () => col }) }),
+ raw: (sql: string) => sql
+ } as never;
+
+ return { dal: identityCredentialAlertDALFactory(db), calls };
+};
+
+const scanArgs = {
+ orgId: "org-1",
+ alertBeforeInterval: "30 days",
+ leadInterval: "1 day",
+ asOf: new Date("2026-01-01T00:00:00Z")
+};
+
+describe("identity credential alert dal", () => {
+ test("an org-scoped scan excludes project-owned identities", async () => {
+ const { dal, calls } = buildDAL();
+
+ await dal.findExpiringUaClientSecrets(scanArgs);
+
+ expect(calls.whereNull).toContainEqual(`${TableName.Identity}.projectId`);
+ // No project membership join: the scan spans the whole org.
+ expect(calls.where.some((args) => args[0] === "projectMembership.scope")).toBe(false);
+ });
+
+ test("a project-scoped scan keeps org-level identities but only its own project's identities", async () => {
+ const { dal, calls } = buildDAL();
+
+ await dal.findExpiringUaClientSecrets({ ...scanArgs, projectId: "proj-1" });
+
+ expect(calls.where).toContainEqual(["projectMembership.scopeProjectId", "proj-1"]);
+ // Membership in the project is not sufficient on its own: an identity owned by another project
+ // stays out even when it has been shared into this one.
+ expect(calls.groupedWhereNull).toContainEqual(`${TableName.Identity}.projectId`);
+ expect(calls.groupedOrWhere).toContainEqual([`${TableName.Identity}.projectId`, "proj-1"]);
+ expect(calls.whereNull).not.toContainEqual(`${TableName.Identity}.projectId`);
+ });
+});
diff --git a/backend/src/services/alert/providers/identity-credential-alert-dal.ts b/backend/src/services/alert/providers/identity-credential-alert-dal.ts
index 08a47e2b49..a0e6dea468 100644
--- a/backend/src/services/alert/providers/identity-credential-alert-dal.ts
+++ b/backend/src/services/alert/providers/identity-credential-alert-dal.ts
@@ -65,7 +65,12 @@ export const identityCredentialAlertDALFactory = (db: TDbClient) => {
"projectMembership.actorIdentityId"
)
.where("projectMembership.scope", AccessScope.Project)
- .where("projectMembership.scopeProjectId", projectId);
+ .where("projectMembership.scopeProjectId", projectId)
+ .where((bd) =>
+ void bd.whereNull(`${TableName.Identity}.projectId`).orWhere(`${TableName.Identity}.projectId`, projectId)
+ );
+ } else {
+ void query.whereNull(`${TableName.Identity}.projectId`);
}
if (identityId) {
@@ -87,14 +92,22 @@ export const identityCredentialAlertDALFactory = (db: TDbClient) => {
}
};
- const isIdentityInOrg = async (identityId: string, orgId: string, tx?: Knex): Promise<boolean> => {
+ const findIdentityInOrg = async (
+ identityId: string,
+ orgId: string,
+ tx?: Knex
+ ): Promise<{ projectId: string | null } | undefined> => {
try {
const row = (await (tx || db.replicaNode())(TableName.Membership)
- .where({ actorIdentityId: identityId, scopeOrgId: orgId, scope: AccessScope.Organization })
- .first()) as { id: string } | undefined;
- return Boolean(row);
+ .join(TableName.Identity, `${TableName.Membership}.actorIdentityId`, `${TableName.Identity}.id`)
+ .where(`${TableName.Membership}.actorIdentityId`, identityId)
+ .where(`${TableName.Membership}.scopeOrgId`, orgId)
+ .where(`${TableName.Membership}.scope`, AccessScope.Organization)
+ .select(db.ref("projectId").withSchema(TableName.Identity))
+ .first()) as { projectId: string | null } | undefined;
+ return row;
} catch (error) {
- throw new DatabaseError({ error, name: "IsIdentityInOrg" });
+ throw new DatabaseError({ error, name: "FindIdentityInOrg" });
}
};
@@ -121,5 +134,5 @@ export const identityCredentialAlertDALFactory = (db: TDbClient) => {
}
};
- return { findExpiringUaClientSecrets, isIdentityInOrg, isIdentityInProject, getProjectType };
+ return { findExpiringUaClientSecrets, findIdentityInOrg, isIdentityInProject, getProjectType };
};
diff --git a/backend/src/services/alert/providers/identity-credential-alert-provider.test.ts b/backend/src/services/alert/providers/identity-credential-alert-provider.test.ts
index b56114c4b5..fa7a5ceb57 100644
--- a/backend/src/services/alert/providers/identity-credential-alert-provider.test.ts
+++ b/backend/src/services/alert/providers/identity-credential-alert-provider.test.ts
@@ -50,6 +50,9 @@ const buildProvider = (opts?: {
abilityRules?: { action: string; subject: string; conditions?: Record<string, unknown> }[];
inOrg?: boolean;
inProject?: boolean;
+ // Owning project of the bound identity: null for an org-level identity, a project id when the
+ // identity was created in project scope.
+ ownerProjectId?: string | null;
projectType?: string | null;
}) => {
const dal = {
@@ -64,7 +67,8 @@ const buildProvider = (opts?: {
opts?.onFind?.(args);
return opts?.secrets ?? [];
},
- isIdentityInOrg: async () => opts?.inOrg ?? true,
+ findIdentityInOrg: async () =>
+ (opts?.inOrg ?? true) ? { projectId: opts?.ownerProjectId ?? null } : undefined,
isIdentityInProject: async () => opts?.inProject ?? true,
getProjectType: async () => opts?.projectType ?? null
};
@@ -354,4 +358,28 @@ describe("identity credential alert provider", () => {
provider.assertResourceInScope({ orgId: "org-1", projectId: "proj-1", resourceId: "ident-1" })
).rejects.toThrow();
});
+
+ // A project-owned identity keeps its project's permission boundary: its client secret metadata is
+ // gated by that project's identity permission, which an org-scoped alert never evaluates. Org
+ // membership alone must not be enough to bind it.
+ test("assertResourceInScope rejects a project-owned identity on an org-scoped alert", async () => {
+ const provider = buildProvider({ inOrg: true, ownerProjectId: "proj-1" });
+ await expect(provider.assertResourceInScope({ orgId: "org-1", resourceId: "ident-1" })).rejects.toThrow(
+ /belongs to a project/
+ );
+ });
+
+ test("assertResourceInScope rejects a project-owned identity on another project's alert", async () => {
+ const provider = buildProvider({ inOrg: true, inProject: true, ownerProjectId: "proj-1" });
+ await expect(
+ provider.assertResourceInScope({ orgId: "org-1", projectId: "proj-2", resourceId: "ident-1" })
+ ).rejects.toThrow(/belongs to a project/);
+ });
+
+ test("assertResourceInScope allows a project-owned identity on its own project's alert", async () => {
+ const provider = buildProvider({ inOrg: true, inProject: true, ownerProjectId: "proj-1" });
+ await expect(
+ provider.assertResourceInScope({ orgId: "org-1", projectId: "proj-1", resourceId: "ident-1" })
+ ).resolves.toBeUndefined();
+ });
});
diff --git a/backend/src/services/alert/providers/identity-credential-alert-provider.ts b/backend/src/services/alert/providers/identity-credential-alert-provider.ts
index 5054d5cb11..72ef30a84d 100644
--- a/backend/src/services/alert/providers/identity-credential-alert-provider.ts
+++ b/backend/src/services/alert/providers/identity-credential-alert-provider.ts
@@ -184,11 +184,17 @@ export const identityCredentialAlertProviderFactory = ({
}): Promise<void> => {
if (!input.resourceId) return;
- const inOrg = await identityCredentialAlertDAL.isIdentityInOrg(input.resourceId, input.orgId);
- if (!inOrg) {
+ const identity = await identityCredentialAlertDAL.findIdentityInOrg(input.resourceId, input.orgId);
+ if (!identity) {
throw new NotFoundError({ message: `Identity '${input.resourceId}' was not found in this organization` });
}
+ if (identity.projectId && identity.projectId !== input.projectId) {
+ throw new ForbiddenRequestError({
+ message: `Identity '${input.resourceId}' belongs to a project. Create this alert within that project so its identity permissions are enforced.`
+ });
+ }
+
if (input.projectId) {
const inProject = await identityCredentialAlertDAL.isIdentityInProject(input.resourceId, input.projectId);
if (!inProject) {