🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.
TypeScript analyticsautogenevaluationlangchainlarge-language-modelsllama-indexllmllm-evaluationllm-observabilityllmopsmonitoringobservabilityopen-sourceopenaiplaygroundprompt-engineeringprompt-managementself-hostedycombinator
fix(rbac): let project members view the API keys page (#16312)
Steffen Schmitze70e14e96754
diff --git a/web/src/__tests__/server/project-api-key-trpc.servertest.ts b/web/src/__tests__/server/project-api-key-trpc.servertest.tsindex cc10c6ffef..46d2da09a3 100644--- a/web/src/__tests__/server/project-api-key-trpc.servertest.ts+++ b/web/src/__tests__/server/project-api-key-trpc.servertest.ts@@ -28,7 +28,7 @@ describe("project API keys trpc", () => {async function createProjectCaller(projectRole: "ADMIN" | "MEMBER" = "ADMIN",) {- const { projectId, orgId } = await createOrgProjectAndApiKey();+ const { projectId, orgId, publicKey } = await createOrgProjectAndApiKey();const session: Session = {expires: "1",@@ -76,7 +76,7 @@ describe("project API keys trpc", () => {const ctx = createInnerTRPCContext({ session, headers: {} });const caller = appRouter.createCaller({ ...ctx, prisma });- return { caller, projectId };+ return { caller, projectId, publicKey };}describe("projectApiKeys.byProjectId", () => {@@ -98,6 +98,17 @@ describe("project API keys trpc", () => {"In-app agent key hidden from project UI",);});++ // The settings page gates the list view on apiKeys:read, which MEMBERs+ // hold without apiKeys:CUD. Pin that this read path stays open to them.+ it("lists keys for MEMBER callers, who only hold apiKeys:read", async () => {+ const { caller, projectId, publicKey } =+ await createProjectCaller("MEMBER");++ const apiKeys = await caller.projectApiKeys.byProjectId({ projectId });++ expect(apiKeys.map((key) => key.publicKey)).toContain(publicKey);+ });});describe("projectApiKeys.create", () => {diff --git a/web/src/features/public-api/components/ApiKeyList.clienttest.tsx b/web/src/features/public-api/components/ApiKeyList.clienttest.tsxnew file mode 100644index 0000000000..f2fb13c076--- /dev/null+++ b/web/src/features/public-api/components/ApiKeyList.clienttest.tsx@@ -0,0 +1,116 @@+import { render, screen } from "@testing-library/react";+import { type Role } from "@langfuse/shared/src/db";++const { projectApiKeys, mockSession } = vi.hoisted(() => ({+ projectApiKeys: [+ {+ id: "key-1",+ createdAt: new Date("2026-01-02T03:04:05.000Z"),+ expiresAt: null,+ lastUsedAt: null,+ note: "Production key",+ publicKey: "pk-lf-1234",+ displaySecretKey: "sk-lf-...5678",+ createdByUser: null,+ createdByApiKey: null,+ },+ ],+ mockSession: { data: null as unknown },+}));++vi.mock("next-auth/react", () => ({+ useSession: () => mockSession,+}));++vi.mock("@/src/utils/api", () => {+ const noopMutation = () => ({+ mutate: vi.fn(),+ mutateAsync: vi.fn(),+ isPending: false,+ });+ return {+ api: {+ projectApiKeys: {+ byProjectId: {+ useQuery: (+ _input: unknown,+ options?: { enabled?: boolean },+ ): { data?: typeof projectApiKeys } =>+ options?.enabled ? { data: projectApiKeys } : {},+ },+ create: { useMutation: noopMutation },+ updateNote: { useMutation: noopMutation },+ delete: { useMutation: noopMutation },+ },+ organizationApiKeys: {+ byOrganizationId: { useQuery: () => ({}) },+ create: { useMutation: noopMutation },+ updateNote: { useMutation: noopMutation },+ delete: { useMutation: noopMutation },+ },+ useUtils: () => ({+ projectApiKeys: { invalidate: vi.fn() },+ organizationApiKeys: { invalidate: vi.fn() },+ }),+ },+ reportNonTrpcError: vi.fn(),+ };+});++vi.mock("@/src/features/public-api/hooks/useLangfuseEnvCode", () => ({+ useLangfuseEnvCode: () => 'LANGFUSE_PUBLIC_KEY="pk-lf-..."',+ useLangfuseBaseUrl: () => "http://localhost:3000",+}));++vi.mock("@/src/features/posthog-analytics/usePostHogClientCapture", () => ({+ usePostHogClientCapture: () => vi.fn(),+}));++import { ApiKeyList } from "./ApiKeyList";++const PROJECT_ID = "project-1";++function renderAsProjectRole(role: Role) {+ mockSession.data = {+ user: {+ admin: false,+ organizations: [{ id: "org-1", projects: [{ id: PROJECT_ID, role }] }],+ },+ };++ return render(<ApiKeyList entityId={PROJECT_ID} scope="project" />);+}++describe("ApiKeyList project access gating", () => {+ it("lists keys read-only for a MEMBER, who holds apiKeys:read but not apiKeys:CUD", () => {+ renderAsProjectRole("MEMBER");++ expect(screen.queryByText("Access Denied")).not.toBeInTheDocument();+ expect(screen.getByTitle("pk-lf-1234")).toBeInTheDocument();+ expect(screen.getByText("sk-lf-...5678")).toBeInTheDocument();+ // Write controls stay behind apiKeys:CUD: no create button, and the note+ // renders as plain text instead of the click-to-edit affordance.+ expect(+ screen.queryByRole("button", { name: /create new api keys/i }),+ ).not.toBeInTheDocument();+ expect(screen.getByText("Production key")).toBeInTheDocument();+ expect(screen.queryByText("Click to add note")).not.toBeInTheDocument();+ });++ it("shows write controls for an ADMIN, who holds apiKeys:CUD", () => {+ renderAsProjectRole("ADMIN");++ expect(screen.queryByText("Access Denied")).not.toBeInTheDocument();+ expect(screen.getByTitle("pk-lf-1234")).toBeInTheDocument();+ expect(+ screen.getByRole("button", { name: /create new api keys/i }),+ ).toBeInTheDocument();+ });++ it("denies access to a VIEWER, who holds neither api key scope", () => {+ renderAsProjectRole("VIEWER");++ expect(screen.getByText("Access Denied")).toBeInTheDocument();+ expect(screen.queryByTitle("pk-lf-1234")).not.toBeInTheDocument();+ });+});diff --git a/web/src/features/public-api/components/ApiKeyList.tsx b/web/src/features/public-api/components/ApiKeyList.tsxindex 2d94282557..94cc9db5c6 100644--- a/web/src/features/public-api/components/ApiKeyList.tsx+++ b/web/src/features/public-api/components/ApiKeyList.tsx@@ -45,9 +45,12 @@ export function ApiKeyList(props: { entityId: string; scope: ApiKeyScope }) {);}- const hasProjectAccess = useHasProjectAccess({+ // Viewing the list only needs apiKeys:read, which project MEMBERs hold.+ // Create, delete, and note editing stay behind apiKeys:CUD and are gated+ // individually by CreateApiKeyButton, DeleteApiKeyButton, and ApiKeyNote.+ const hasProjectReadAccess = useHasProjectAccess({projectId: props.entityId,- scope: "apiKeys:CUD",+ scope: "apiKeys:read",});const hasOrganizationAccess = useHasOrganizationAccess({organizationId: props.entityId,@@ -55,11 +58,11 @@ export function ApiKeyList(props: { entityId: string; scope: ApiKeyScope }) {});const hasAccess =- props.scope === "project" ? hasProjectAccess : hasOrganizationAccess;+ props.scope === "project" ? hasProjectReadAccess : hasOrganizationAccess;const projectApiKeysQuery = api.projectApiKeys.byProjectId.useQuery({ projectId: entityId },- { enabled: hasProjectAccess && props.scope === "project" },+ { enabled: hasProjectReadAccess && props.scope === "project" },);const organizationApiKeysQuery =api.organizationApiKeys.byOrganizationId.useQuery(