Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

test(projects-api): reject gateway ingestion project deletion without gateway:manage (#19612)

Tobias Wochinger3c9c1c048861
diff --git a/web/src/__tests__/server/projects-api.servertest.ts b/web/src/__tests__/server/projects-api.servertest.ts
index b371cdc299..8c64787a08 100644
--- a/web/src/__tests__/server/projects-api.servertest.ts
+++ b/web/src/__tests__/server/projects-api.servertest.ts
@@ -21,6 +21,9 @@ import { type NextApiRequest, type NextApiResponse } from "next";
import { createMocks } from "node-mocks-http";
import projectApiKeysHandler from "@/src/pages/api/public/projects/[projectId]/apiKeys";
import projectApiKeyHandler from "@/src/pages/api/public/projects/[projectId]/apiKeys/[apiKeyId]";
+import projectByIdHandler from "@/src/pages/api/public/projects/[projectId]";
+import { authenticator } from "@/src/features/apiKey/server";
+import { type AuthorizationContext } from "@/src/features/auth/policy/types";
import {
organizationApiKeyCreationSchema,
projectApiKeyCreationSchema,
@@ -865,6 +868,83 @@ describe("Projects API", () => {
).resolves.toMatchObject({ deletedAt: expect.any(Date) });
});
+ describe("gateway ingestion project in enforce mode", () => {
+ const originalMode = env.API_AUTH_MIGRATION;
+ let orgId: string;
+ let ingestionProjectId: string;
+ let auth: string;
+ let context: AuthorizationContext;
+
+ beforeEach(async () => {
+ Object.assign(env, { API_AUTH_MIGRATION: "enforce" });
+ ({ orgId, projectId: ingestionProjectId } =
+ await createOrgProjectAndApiKey({ plan: "Enterprise" }));
+ const orgKey = await createApiKey(prisma, {
+ tenant: OrganizationId(orgId),
+ scope: OrganizationId(orgId),
+ role: SystemRoleId("LEGACY_ORGANIZATION_API_KEY"),
+ createdBy: "system",
+ });
+ auth = createBasicAuthHeader(orgKey.publicKey, orgKey.secretKey);
+ const authentication = await authenticator.authenticate({
+ headers: { authorization: auth },
+ });
+ if (!authentication.success) throw authentication.error;
+ context = authentication.context;
+ await prisma.gatewayConfig.create({
+ data: {
+ organizationId: orgId,
+ defaultIngestionProjectId: ingestionProjectId,
+ ingestionMode: "USAGE",
+ },
+ });
+ });
+
+ afterEach(() => {
+ vi.restoreAllMocks();
+ Object.assign(env, { API_AUTH_MIGRATION: originalMode });
+ });
+
+ // No system role grants project:delete without gateway:manage, so the
+ // principal is the org key's real context with that one action removed.
+ const deleteIngestionProject = async (ctx: AuthorizationContext) => {
+ vi.spyOn(authenticator, "authenticate").mockResolvedValueOnce({
+ success: true,
+ context: ctx,
+ });
+ const { req, res } = createMocks<NextApiRequest, NextApiResponse>({
+ method: "DELETE",
+ query: { projectId: ingestionProjectId },
+ headers: { authorization: auth },
+ });
+ await projectByIdHandler(req, res);
+ return res;
+ };
+
+ it("rejects a key with project:delete but without gateway:manage", async () => {
+ const policies = context.policies.map((policy) => ({
+ ...policy,
+ actions: policy.actions.filter(
+ (action) => action !== "gateway:manage",
+ ),
+ }));
+
+ const res = await deleteIngestionProject({ ...context, policies });
+
+ expect(res._getStatusCode()).toBe(403);
+ expect(res._getJSONData().message).toContain("gateway:manage");
+ await expect(
+ prisma.gatewayConfig.findUnique({ where: { organizationId: orgId } }),
+ ).resolves.toMatchObject({
+ defaultIngestionProjectId: ingestionProjectId,
+ ingestionMode: "USAGE",
+ });
+ await expect(
+ prisma.project.findUnique({ where: { id: ingestionProjectId } }),
+ ).resolves.toMatchObject({ deletedAt: null });
+ });
+ });
+
it("should return 403 when using project API key instead of organization API key", async () => {
const result = await makeAPICall<{ message: string }>(
"DELETE",