🪢 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(auth): resolve API key scope publicKey from the verified key, not submitted pk (#15456)
Steffen Schmitz77909f75db48
diff --git a/web/src/__tests__/server/api-auth.servertest.ts b/web/src/__tests__/server/api-auth.servertest.tsindex f8d0874252..45dd820825 100644--- a/web/src/__tests__/server/api-auth.servertest.ts+++ b/web/src/__tests__/server/api-auth.servertest.ts@@ -1,3 +1,4 @@+import { vi } from "vitest";import {OrgEnrichedApiKey,createAndAddApiKeysToDb,@@ -7,6 +8,7 @@ import {generateKeySet,getDisplaySecretKey,hashSecretKey,+ logger,} from "@langfuse/shared/src/server";import { Prisma, type PrismaClient, prisma } from "@langfuse/shared/src/db";import { env } from "@/src/env.mjs";@@ -329,6 +331,64 @@ describe("Authenticate API calls", () => {expect(auth.scope.isInAppAgentKey).toBe(true);}});++ it("returns the API key's actual public key in scope and warns when the submitted public key does not match", async () => {+ // first use sets fastHashedSecretKey so the secret-hash lookup path is taken+ await new ApiAuthService(prisma, null).verifyAuthHeaderAndReturnScope(+ getValidAuthHeader(),+ );++ const warnSpy = vi.spyOn(logger, "warn");+ const submittedPublicKey = `pk-lf-mismatch-${v4()}`;++ const auth = await new ApiAuthService(+ prisma,+ null,+ ).verifyAuthHeaderAndReturnScope(+ createBasicAuthHeader(submittedPublicKey, testApiKey.secretKey),+ );++ expect(auth.validKey).toBe(true);+ if (auth.validKey) {+ expect(auth.scope.publicKey).toBe(testApiKey.publicKey);+ expect(auth.scope.projectId).toBe(testApiKey.projectId);+ }++ const mismatchWarning = warnSpy.mock.calls+ .map((call) => String(call[0]))+ .find((message) => message.includes("Public key mismatch"));+ expect(mismatchWarning).toBeDefined();+ expect(mismatchWarning).toContain(submittedPublicKey);+ expect(mismatchWarning).toContain(testApiKey.publicKey);+ expect(mismatchWarning).toContain(testApiKey.projectId);++ warnSpy.mockRestore();+ });++ it("does not warn when the submitted public key matches", async () => {+ await new ApiAuthService(prisma, null).verifyAuthHeaderAndReturnScope(+ getValidAuthHeader(),+ );++ const warnSpy = vi.spyOn(logger, "warn");++ const auth = await new ApiAuthService(+ prisma,+ null,+ ).verifyAuthHeaderAndReturnScope(getValidAuthHeader());++ expect(auth.validKey).toBe(true);+ if (auth.validKey) {+ expect(auth.scope.publicKey).toBe(testApiKey.publicKey);+ }+ expect(+ warnSpy.mock.calls.filter((call) =>+ String(call[0]).includes("Public key mismatch"),+ ),+ ).toHaveLength(0);++ warnSpy.mockRestore();+ });});describe("validates with redis", () => {diff --git a/web/src/features/public-api/server/apiAuth.ts b/web/src/features/public-api/server/apiAuth.tsindex c56ae4ff5b..516dba79fa 100644--- a/web/src/features/public-api/server/apiAuth.ts+++ b/web/src/features/public-api/server/apiAuth.ts@@ -178,6 +178,15 @@ export class ApiAuthService {? ("organization" as const): ("project" as const);+ // The API key is resolved via the hash of the secret key, so the+ // submitted public key may not belong to it, e.g. after a partial+ // credential rotation on the client.+ if (publicKey !== finalApiKey.publicKey) {+ logger.warn(+ `Public key mismatch on basic auth: submitted public key ${publicKey} does not match public key ${finalApiKey.publicKey} of the API key resolved via the secret key (apiKeyId ${finalApiKey.id}, projectId ${finalApiKey.projectId}, orgId ${finalApiKey.orgId})`,+ );+ }+const result = {validKey: true as const,scope: {@@ -188,7 +197,7 @@ export class ApiAuthService {rateLimitOverrides: finalApiKey.rateLimitOverrides ?? [],apiKeyId: finalApiKey.id,scope: finalApiKey.scope,- publicKey,+ publicKey: finalApiKey.publicKey,isIngestionSuspended: finalApiKey.isIngestionSuspended,isInAppAgentKey: finalApiKey.isInAppAgentKey,},