Skip to content

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

fix(api-auth): return 404 when mutating a missing API key (#17677)

Max Deichmannda578c281d13
diff --git a/web/src/__tests__/server/organizations-api-key-trpc.servertest.ts b/web/src/__tests__/server/organizations-api-key-trpc.servertest.ts
index f28881360e..7fcc15fadf 100644
--- a/web/src/__tests__/server/organizations-api-key-trpc.servertest.ts
+++ b/web/src/__tests__/server/organizations-api-key-trpc.servertest.ts
@@ -354,6 +354,16 @@ describe("organization API keys trpc", () => {
expect(updatedKey?.note).toBe("Updated Note");
});
+ it("returns NOT_FOUND for a missing API key", async () => {
+ await expect(
+ ownerCaller.organizationApiKeys.updateNote({
+ orgId: organizationId,
+ keyId: randomUUID(),
+ note: "Updated Note",
+ }),
+ ).rejects.toMatchObject({ code: "NOT_FOUND" });
+ });
+
it("does not update in-app agent API keys", async () => {
const inAppAgentKey = await createAndAddApiKeysToDb({
prisma,
@@ -435,6 +445,15 @@ describe("organization API keys trpc", () => {
expect(deletedKey).toBeUndefined();
});
+ it("returns NOT_FOUND for a missing API key", async () => {
+ await expect(
+ ownerCaller.organizationApiKeys.delete({
+ orgId: organizationId,
+ id: randomUUID(),
+ }),
+ ).rejects.toMatchObject({ code: "NOT_FOUND" });
+ });
+
it("does not delete in-app agent API keys", async () => {
const inAppAgentKey = await createAndAddApiKeysToDb({
prisma,
diff --git a/web/src/__tests__/server/project-api-key-trpc.servertest.ts b/web/src/__tests__/server/project-api-key-trpc.servertest.ts
index 0f2f9f7b14..663d734053 100644
--- a/web/src/__tests__/server/project-api-key-trpc.servertest.ts
+++ b/web/src/__tests__/server/project-api-key-trpc.servertest.ts
@@ -7,6 +7,7 @@ import {
createAndAddApiKeysToDb,
createOrgProjectAndApiKey,
} from "@langfuse/shared/src/server";
+import { randomUUID } from "crypto";
describe("project API keys trpc", () => {
// The session user is persisted as the API key creator, so it must exist
@@ -148,6 +149,18 @@ describe("project API keys trpc", () => {
});
describe("projectApiKeys.updateNote", () => {
+ it("returns NOT_FOUND for a missing API key", async () => {
+ const { caller, projectId } = await createProjectCaller();
+
+ await expect(
+ caller.projectApiKeys.updateNote({
+ projectId,
+ keyId: randomUUID(),
+ note: "Updated Note",
+ }),
+ ).rejects.toMatchObject({ code: "NOT_FOUND" });
+ });
+
it("does not update in-app agent API keys", async () => {
const { caller, projectId } = await createProjectCaller();
const inAppAgentKey = await createAndAddApiKeysToDb({
@@ -174,6 +187,17 @@ describe("project API keys trpc", () => {
});
describe("projectApiKeys.delete", () => {
+ it("returns NOT_FOUND for a missing API key", async () => {
+ const { caller, projectId } = await createProjectCaller();
+
+ await expect(
+ caller.projectApiKeys.delete({
+ projectId,
+ id: randomUUID(),
+ }),
+ ).rejects.toMatchObject({ code: "NOT_FOUND" });
+ });
+
it("does not delete in-app agent API keys", async () => {
const { caller, projectId } = await createProjectCaller();
const inAppAgentKey = await createAndAddApiKeysToDb({
diff --git a/web/src/features/public-api/server/organizationApiKeyRouter.ts b/web/src/features/public-api/server/organizationApiKeyRouter.ts
index ef8b61b76b..0fb701ae46 100644
--- a/web/src/features/public-api/server/organizationApiKeyRouter.ts
+++ b/web/src/features/public-api/server/organizationApiKeyRouter.ts
@@ -8,6 +8,7 @@ import {
import * as z from "zod";
import { ApiAuthService } from "@/src/features/public-api/server/apiAuth";
import { redis } from "@langfuse/shared/src/server";
+import { LangfuseNotFoundError } from "@langfuse/shared";
import { createAndAddApiKeysToDb } from "@langfuse/shared/src/server/auth/apiKeys";
export const organizationApiKeysRouter = createTRPCRouter({
@@ -112,7 +113,7 @@ export const organizationApiKeysRouter = createTRPCRouter({
scope: "organization:CRUD_apiKeys",
});
- await ctx.prisma.apiKey.findFirstOrThrow({
+ const apiKey = await ctx.prisma.apiKey.findFirst({
where: {
id: input.keyId,
orgId: input.orgId,
@@ -120,6 +121,10 @@ export const organizationApiKeysRouter = createTRPCRouter({
},
});
+ if (!apiKey) {
+ throw new LangfuseNotFoundError("API key not found");
+ }
+
await auditLog({
session: ctx.session,
resourceType: "apiKey",
@@ -154,7 +159,7 @@ export const organizationApiKeysRouter = createTRPCRouter({
organizationId: input.orgId,
scope: "organization:CRUD_apiKeys",
});
- const apiKey = await ctx.prisma.apiKey.findFirstOrThrow({
+ const apiKey = await ctx.prisma.apiKey.findFirst({
where: {
id: input.id,
orgId: input.orgId,
@@ -162,6 +167,10 @@ export const organizationApiKeysRouter = createTRPCRouter({
},
});
+ if (!apiKey) {
+ throw new LangfuseNotFoundError("API key not found");
+ }
+
if (apiKey.isInAppAgentKey) return false;
await auditLog({
diff --git a/web/src/features/public-api/server/projectApiKeyRouter.ts b/web/src/features/public-api/server/projectApiKeyRouter.ts
index d1fed03937..dbed8a5fde 100644
--- a/web/src/features/public-api/server/projectApiKeyRouter.ts
+++ b/web/src/features/public-api/server/projectApiKeyRouter.ts
@@ -8,7 +8,7 @@ import * as z from "zod";
import { ApiAuthService } from "@/src/features/public-api/server/apiAuth";
import { redis } from "@langfuse/shared/src/server";
import { createAndAddApiKeysToDb } from "@langfuse/shared/src/server/auth/apiKeys";
-import { StringNoHTML } from "@langfuse/shared";
+import { LangfuseNotFoundError, StringNoHTML } from "@langfuse/shared";
export const projectApiKeysRouter = createTRPCRouter({
byProjectId: protectedProjectProcedure
@@ -104,7 +104,7 @@ export const projectApiKeysRouter = createTRPCRouter({
scope: "apiKeys:CUD",
});
- await ctx.prisma.apiKey.findFirstOrThrow({
+ const apiKey = await ctx.prisma.apiKey.findFirst({
where: {
id: input.keyId,
projectId: input.projectId,
@@ -112,6 +112,10 @@ export const projectApiKeysRouter = createTRPCRouter({
},
});
+ if (!apiKey) {
+ throw new LangfuseNotFoundError("API key not found");
+ }
+
await auditLog({
session: ctx.session,
resourceType: "apiKey",
@@ -146,7 +150,7 @@ export const projectApiKeysRouter = createTRPCRouter({
projectId: input.projectId,
scope: "apiKeys:CUD",
});
- const apiKey = await ctx.prisma.apiKey.findFirstOrThrow({
+ const apiKey = await ctx.prisma.apiKey.findFirst({
where: {
id: input.id,
projectId: input.projectId,
@@ -154,6 +158,10 @@ export const projectApiKeysRouter = createTRPCRouter({
},
});
+ if (!apiKey) {
+ throw new LangfuseNotFoundError("API key not found");
+ }
+
if (apiKey.isInAppAgentKey) return false;
await auditLog({