Skip to content

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

fix(gateway): enforce gateway invoke when API key roles are enforced (#18814)

Tobias Wochingerfb4449adbdd9
diff --git a/web/src/__tests__/server/ai-gateway-control-plane.servertest.ts b/web/src/__tests__/server/ai-gateway-control-plane.servertest.ts
index eb4eda3814..050ea08197 100644
--- a/web/src/__tests__/server/ai-gateway-control-plane.servertest.ts
+++ b/web/src/__tests__/server/ai-gateway-control-plane.servertest.ts
@@ -837,6 +837,54 @@ describe("AI gateway control plane", () => {
lookup.mockRestore();
});
+ it("enforces gateway:invoke when API key role enforcement is on", async () => {
+ const originalMigration = env.API_AUTH_MIGRATION;
+ const originalCache = env.LANGFUSE_AI_GATEWAY_CACHE_RESOLVE_ENABLED;
+ Object.assign(env, { LANGFUSE_AI_GATEWAY_CACHE_RESOLVE_ENABLED: "false" });
+ try {
+ const { caller, org } = await prepare();
+ const gatewayKey = await caller.aiGateway.createApiKey({
+ orgId: org.id,
+ metadata: {},
+ });
+ const authenticator = new GatewayApiKeyAuthenticator(prisma);
+ const request = {
+ fastHashedSecretKey: createShaHash(gatewayKey.secretKey, env.SALT),
+ apiFormat: "openai.responses" as const,
+ };
+
+ Object.assign(env, { API_AUTH_MIGRATION: "enforce" });
+ await expect(
+ authenticator.authenticateRequest(request),
+ ).resolves.toMatchObject({
+ organizationId: org.id,
+ });
+
+ await prisma.roleAssignment.updateMany({
+ where: { apiKeyId: gatewayKey.id },
+ data: { systemRole: "INGEST", roleId: "system/INGEST" },
+ });
+ await expect(
+ authenticator.authenticateRequest(request),
+ ).rejects.toMatchObject({
+ status: 403,
+ message: "API key is not allowed to invoke the gateway",
+ });
+
+ Object.assign(env, { API_AUTH_MIGRATION: "legacy" });
+ await expect(
+ authenticator.authenticateRequest(request),
+ ).resolves.toMatchObject({
+ organizationId: org.id,
+ });
+ } finally {
+ Object.assign(env, {
+ API_AUTH_MIGRATION: originalMigration,
+ LANGFUSE_AI_GATEWAY_CACHE_RESOLVE_ENABLED: originalCache,
+ });
+ }
+ });
+
it("blocks deletion of the default ingestion project", async () => {
const { caller, org, project } = await prepare();
await caller.aiGateway.updateConfig({
diff --git a/web/src/features/ai-gateway/server/auth/gatewayApiKeyAuthenticator.ts b/web/src/features/ai-gateway/server/auth/gatewayApiKeyAuthenticator.ts
index ff88a662b7..48cbd0bfc7 100644
--- a/web/src/features/ai-gateway/server/auth/gatewayApiKeyAuthenticator.ts
+++ b/web/src/features/ai-gateway/server/auth/gatewayApiKeyAuthenticator.ts
@@ -1,5 +1,9 @@
import type { GatewayProvider, PrismaClient } from "@langfuse/shared/src/db";
+import { ApiKeyId, OrganizationId } from "@langfuse/shared/rbac";
+import { env } from "@/src/env.mjs";
+import { authorize } from "@/src/features/rbac/authorize";
+import { getRolesForPrincipal } from "@/src/features/rbac/getRolesForPrincipal";
import { isGatewayEnabledForOrganization } from "@/src/features/ai-gateway/server/availability";
import { GatewayControlPlaneError } from "@/src/features/ai-gateway/server/gatewayControlPlaneError";
import {
@@ -27,7 +31,7 @@ export class GatewayApiKeyAuthenticator {
private readonly cache: GatewayResolveCache;
constructor(
- prisma: PrismaClient,
+ private readonly prisma: PrismaClient,
config: GatewayApiKeyAuthenticatorConfig = {},
) {
this.repository = new GatewayResolveRepository(prisma);
@@ -101,6 +105,10 @@ export class GatewayApiKeyAuthenticator {
: null,
};
+ if (env.API_AUTH_MIGRATION === "enforce") {
+ await assertCanInvokeGateway(this.prisma, row.apiKeyId, organizationId);
+ }
+
await this.cache.set({
...params,
context,
@@ -110,6 +118,41 @@ export class GatewayApiKeyAuthenticator {
}
}
+/** assertCanInvokeGateway denies the key when its stored roles do not grant gateway:invoke on the organization. */
+async function assertCanInvokeGateway(
+ prisma: PrismaClient,
+ apiKeyId: string,
+ organizationId: string,
+) {
+ const roles = await getRolesForPrincipal(prisma, ApiKeyId(apiKeyId));
+ const tenant = OrganizationId(organizationId);
+ const allowed = authorize(
+ {
+ principal: {
+ kind: "apiKey",
+ apiKeyId,
+ userId: null,
+ isInAppAgentKey: false,
+ publicKey: "",
+ scope: "ORGANIZATION",
+ presentation: "privateKey",
+ organizations: [],
+ boundResource: { orgId: organizationId },
+ },
+ policies: roles.flatMap((role) => role.policies),
+ },
+ tenant,
+ "gateway:invoke",
+ tenant,
+ ).success;
+ if (!allowed) {
+ throw new GatewayControlPlaneError(
+ "API key is not allowed to invoke the gateway",
+ 403,
+ );
+ }
+}
+
function supportedProviders(apiFormat: GatewayApiFormat): GatewayProvider[] {
return gatewayProviders.filter((provider) =>
providerSupportsApiFormat(provider, apiFormat),