Skip to content

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

fix(ai-gateway): reject user-bound keys whose owner can no longer invoke the gateway (#19542)

Hassieb Pakzadb89b13ed0ae8
diff --git a/packages/shared/scripts/seeder/scenarios/gateway-traffic.ts b/packages/shared/scripts/seeder/scenarios/gateway-traffic.ts
index b3cb81b469..9ae371b0e3 100644
--- a/packages/shared/scripts/seeder/scenarios/gateway-traffic.ts
+++ b/packages/shared/scripts/seeder/scenarios/gateway-traffic.ts
@@ -1,4 +1,5 @@
-import { prisma } from "../../../src/db";
+import { prisma, type Role } from "../../../src/db";
+import { organizationRoleAccessRights } from "../../../src/features/rbac/organizationAccessRights";
import {
OrganizationId,
SystemRoleId,
@@ -39,8 +40,9 @@ import { countRows, escapeLike } from "./verify";
*
* Writes go to the ingestion project of the --project's organization, not to
* --project itself. Missing gateway keys are created (bound round-robin to
- * organization members) until the organization has --keys of them. One extra
- * service key, bound to no user, sends requests whose user ID the caller set.
+ * organization members whose role can invoke the gateway) until the
+ * organization has --keys of them. One extra service key, bound to no user,
+ * sends requests whose user ID the caller set.
*/
const MODELS = [
@@ -51,6 +53,13 @@ const MODELS = [
const SDK_EVENTS_PER_DAY = 3;
+// The gateway rejects keys whose owner's organization role cannot invoke it.
+const GATEWAY_INVOKER_ROLES = (
+ Object.keys(organizationRoleAccessRights) as Role[]
+).filter((role) =>
+ organizationRoleAccessRights[role].includes("gateway:invoke"),
+);
+
const ensureGatewayKeys = async (params: {
organizationId: string;
keyCount: number;
@@ -71,13 +80,16 @@ const ensureGatewayKeys = async (params: {
if (keys.length >= params.keyCount) return keys;
const members = await prisma.organizationMembership.findMany({
- where: { orgId: params.organizationId },
+ where: {
+ orgId: params.organizationId,
+ role: { in: GATEWAY_INVOKER_ROLES },
+ },
select: { userId: true },
orderBy: { createdAt: "asc" },
});
if (members.length === 0) {
throw new SeedError(
- "The gateway organization has no members to bind keys to",
+ "The gateway organization has no members that can invoke the gateway to bind keys to",
"run `pnpm run dx` to seed the default organization and its members",
);
}
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 c40529f16e..e1a4a15600 100644
--- a/web/src/__tests__/server/ai-gateway-control-plane.servertest.ts
+++ b/web/src/__tests__/server/ai-gateway-control-plane.servertest.ts
@@ -49,6 +49,7 @@ import {
type GatewayResolveError,
GatewayResolveService,
} from "@/src/features/ai-gateway/server/resolve/resolveService";
+import { invalidateGatewayResolveCacheForApiKey } from "@/src/features/ai-gateway/server/resolve/gatewayResolveCache";
import { GatewayModelsService } from "@/src/features/ai-gateway/server/provider/models/gatewayModelsService";
import { GatewayApiKeyService } from "@/src/features/ai-gateway/server/apiKey/gatewayApiKeyService";
import { GatewayConfigService } from "@/src/features/ai-gateway/server/config/gatewayConfigService";
@@ -2228,6 +2229,111 @@ describe("AI gateway control plane", () => {
}
});
+ describe("key owner role", () => {
+ const authenticateKey = (secretKey: string) =>
+ new GatewayApiKeyAuthenticator(prisma).authenticateModelsRequest({
+ fastHashedSecretKey: createShaHash(secretKey, env.SALT),
+ apiFormat: "openai.responses",
+ });
+ const setOwnerRole = (orgId: string, userId: string, role: Role | null) =>
+ role
+ ? prisma.organizationMembership.update({
+ where: { orgId_userId: { orgId, userId } },
+ data: { role },
+ })
+ : prisma.organizationMembership.delete({
+ where: { orgId_userId: { orgId, userId } },
+ });
+ const ownerNotAllowed = {
+ status: 403,
+ message: "API key owner is not allowed to invoke the gateway",
+ };
+
+ it.each([
+ { change: "demoted to VIEWER", role: Role.VIEWER },
+ { change: "demoted to NONE", role: Role.NONE },
+ { change: "removed from the organization", role: null },
+ ])(
+ "rejects user-bound keys whose owner was $change out of band",
+ async ({ role }) => {
+ const { caller, org, user } = await prepare();
+ const key = await caller.aiGateway.createApiKey({
+ orgId: org.id,
+ name: "User key",
+ metadata: {},
+ });
+ await setOwnerRole(org.id, user.id, role);
+ const lookup = vi.spyOn(prisma.apiKey, "findFirst");
+
+ for (let attempt = 0; attempt < 2; attempt++) {
+ await expect(authenticateKey(key.secretKey)).rejects.toMatchObject(
+ ownerNotAllowed,
+ );
+ }
+ // A rejection is not cached, so restoring access applies at once.
+ expect(lookup).toHaveBeenCalledTimes(2);
+ lookup.mockRestore();
+ },
+ );
+
+ it.each([Role.AI_GATEWAY, Role.MEMBER])(
+ "accepts user-bound keys whose owner is %s",
+ async (role) => {
+ const { caller, org, user } = await prepare();
+ const key = await caller.aiGateway.createApiKey({
+ orgId: org.id,
+ name: "User key",
+ metadata: {},
+ });
+ await setOwnerRole(org.id, user.id, role);
+
+ await expect(authenticateKey(key.secretKey)).resolves.toMatchObject({
+ apiKeyId: key.id,
+ userId: user.id,
+ });
+ },
+ );
+
+ it("accepts service-account keys regardless of the creator's role", async () => {
+ const { caller, org, user } = await prepare();
+ const key = await caller.aiGateway.createApiKey({
+ orgId: org.id,
+ name: "Service key",
+ metadata: {},
+ issuedAs: "serviceAccount",
+ });
+ await setOwnerRole(org.id, user.id, Role.NONE);
+
+ await expect(authenticateKey(key.secretKey)).resolves.toMatchObject({
+ apiKeyId: key.id,
+ userId: null,
+ });
+ });
+
+ it("serves an out-of-band demotion from cache until the entry expires", async () => {
+ const { caller, org, user } = await prepare();
+ const key = await caller.aiGateway.createApiKey({
+ orgId: org.id,
+ name: "User key",
+ metadata: {},
+ });
+ const fastHashedSecretKey = createShaHash(key.secretKey, env.SALT);
+ await expect(authenticateKey(key.secretKey)).resolves.toMatchObject({
+ apiKeyId: key.id,
+ });
+ await setOwnerRole(org.id, user.id, Role.VIEWER);
+
+ // The resolve cache TTL bounds how long the cached grant survives.
+ await expect(authenticateKey(key.secretKey)).resolves.toMatchObject({
+ apiKeyId: key.id,
+ });
+ await invalidateGatewayResolveCacheForApiKey(fastHashedSecretKey);
+ await expect(authenticateKey(key.secretKey)).rejects.toMatchObject(
+ ownerNotAllowed,
+ );
+ });
+ });
+
it("accepts organization keys granted gateway:invoke without a gateway association", async () => {
const originalCache = env.LANGFUSE_AI_GATEWAY_CACHE_RESOLVE_ENABLED;
Object.assign(env, { LANGFUSE_AI_GATEWAY_CACHE_RESOLVE_ENABLED: "false" });
diff --git a/web/src/features/ai-gateway/server/auth/gatewayApiKeyAuthenticator.ts b/web/src/features/ai-gateway/server/auth/gatewayApiKeyAuthenticator.ts
index 9bc899af4d..a235604666 100644
--- a/web/src/features/ai-gateway/server/auth/gatewayApiKeyAuthenticator.ts
+++ b/web/src/features/ai-gateway/server/auth/gatewayApiKeyAuthenticator.ts
@@ -3,6 +3,7 @@ import { ApiKeyId, OrganizationId } from "@langfuse/shared/rbac";
import { authorize } from "@/src/features/rbac/authorize";
import { getRolesForPrincipal } from "@/src/features/rbac/getRolesForPrincipal";
+import { hasOrganizationAccess } from "@/src/features/rbac/utils/checkOrganizationAccess";
import { isGatewayEnabledForOrganization } from "@/src/features/ai-gateway/server/availability";
import { GatewayControlPlaneError } from "@/src/features/ai-gateway/server/gatewayControlPlaneError";
import {
@@ -163,6 +164,24 @@ export class GatewayApiKeyAuthenticator {
})),
};
+ // Runs on cache misses only, so a role change that does not revoke the key
+ // takes effect within LANGFUSE_AI_GATEWAY_CACHE_RESOLVE_TTL_SECONDS.
+ // Demotions through the app revoke user-bound keys and evict them at once.
+ if (row.userId) {
+ const ownerRole = row.user?.organizationMemberships.find(
+ (membership) => membership.orgId === organizationId,
+ )?.role;
+ if (
+ !ownerRole ||
+ !hasOrganizationAccess({ role: ownerRole, scope: "gateway:invoke" })
+ ) {
+ throw new GatewayControlPlaneError(
+ "API key owner is not allowed to invoke the gateway",
+ 403,
+ );
+ }
+ }
+
// Any organization key may call the gateway when its roles grant
// gateway:invoke, so this check must not depend on API_AUTH_MIGRATION.
await assertCanInvokeGateway(this.prisma, row.id, organizationId);
diff --git a/web/src/features/ai-gateway/server/resolve/gatewayResolveRepository.ts b/web/src/features/ai-gateway/server/resolve/gatewayResolveRepository.ts
index 02761f7eb3..45d5f49dce 100644
--- a/web/src/features/ai-gateway/server/resolve/gatewayResolveRepository.ts
+++ b/web/src/features/ai-gateway/server/resolve/gatewayResolveRepository.ts
@@ -22,7 +22,13 @@ export class GatewayResolveRepository {
userId: true,
expiresAt: true,
note: true,
- user: { select: { name: true, email: true } },
+ user: {
+ select: {
+ name: true,
+ email: true,
+ organizationMemberships: { select: { orgId: true, role: true } },
+ },
+ },
gatewayAssociation: { select: { metadata: true } },
organization: {
select: {
diff --git a/web/src/features/ai-gateway/server/resolve/resolveService.servertest.ts b/web/src/features/ai-gateway/server/resolve/resolveService.servertest.ts
index cbe1d88602..bce5000049 100644
--- a/web/src/features/ai-gateway/server/resolve/resolveService.servertest.ts
+++ b/web/src/features/ai-gateway/server/resolve/resolveService.servertest.ts
@@ -75,9 +75,12 @@ const row = (overrides: {
expiresAt: null,
note: overrides.note === undefined ? "CI key" : overrides.note,
user:
- overrides.user === undefined
- ? { name: "Alice", email: "[email protected]" }
- : overrides.user,
+ overrides.user === null
+ ? null
+ : {
+ ...(overrides.user ?? { name: "Alice", email: "[email protected]" }),
+ organizationMemberships: [{ orgId: "org-1", role: "MEMBER" }],
+ },
gatewayAssociation:
overrides.metadata === undefined ? null : { metadata: overrides.metadata },
organization: {