🪢 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(web): log the public key when an API key lookup fails (#16375)
Ben Langfeld91c96e30bf54
diff --git a/packages/shared/src/server/auth/apiKeys.test.ts b/packages/shared/src/server/auth/apiKeys.test.tsindex 06757b4f44..d03c9150fa 100644--- a/packages/shared/src/server/auth/apiKeys.test.ts+++ b/packages/shared/src/server/auth/apiKeys.test.ts@@ -32,21 +32,44 @@ describe("redactLangfuseSecretKeys", () => {});describe("formatSubmittedPublicKeyForLog", () => {- it("passes a public key through verbatim", () => {- expect(formatSubmittedPublicKeyForLog(PUBLIC_KEY)).toBe(PUBLIC_KEY);+ it("echoes a public key, quoted so it is delimited in the log line", () => {+ expect(formatSubmittedPublicKeyForLog(PUBLIC_KEY)).toBe(`"${PUBLIC_KEY}"`);});it("masks a secret key to its display form", () => {- expect(formatSubmittedPublicKeyForLog(SECRET_KEY)).toBe("sk-lf-...9b0c");+ expect(formatSubmittedPublicKeyForLog(SECRET_KEY)).toBe('"sk-lf-...9b0c"');});it("masks non-Langfuse secrets without revealing their middle", () => {const openAiStyle = "sk-proj-ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";- expect(formatSubmittedPublicKeyForLog(openAiStyle)).toBe("sk-pro...6789");+ expect(formatSubmittedPublicKeyForLog(openAiStyle)).toBe('"sk-pro...6789"');});it("does not echo short values whose head and tail would overlap", () => {- expect(formatSubmittedPublicKeyForLog("None")).toBe("****");- expect(formatSubmittedPublicKeyForLog("")).toBe("****");+ expect(formatSubmittedPublicKeyForLog("None")).toBe('"****"');+ expect(formatSubmittedPublicKeyForLog("")).toBe('"****"');+ });++ it("replaces characters outside printable ASCII", () => {+ expect(formatSubmittedPublicKeyForLog("pk-lf-a\nERROR forged")).toBe(+ '"pk-lf-a\uFFFDERROR forged"',+ );+ expect(formatSubmittedPublicKeyForLog("pk-lf-a\u001b[2Jb")).toBe(+ '"pk-lf-a\uFFFD[2Jb"',+ );+ expect(formatSubmittedPublicKeyForLog("pk-lf-a\u009bb")).toBe(+ '"pk-lf-a\uFFFDb"',+ );+ });++ it("escapes a quote or backslash so the value stays one token", () => {+ expect(formatSubmittedPublicKeyForLog('pk-lf-a" b\\c')).toBe(+ '"pk-lf-a\\" b\\\\c"',+ );+ });++ it("bounds the logged length", () => {+ const long = `pk-lf-${"a".repeat(500)}`;+ expect(formatSubmittedPublicKeyForLog(long)).toBe(`"${long.slice(0, 64)}"`);});});diff --git a/packages/shared/src/server/auth/apiKeys.ts b/packages/shared/src/server/auth/apiKeys.tsindex 7fe0bcf568..f0489d9a71 100644--- a/packages/shared/src/server/auth/apiKeys.ts+++ b/packages/shared/src/server/auth/apiKeys.ts@@ -24,15 +24,37 @@ export function redactLangfuseSecretKeys(value: string): string {);}+const MAX_LOGGED_PUBLIC_KEY_LENGTH = 64;+/*** Formats a client-submitted public key for logging. Only a value that is- * actually a Langfuse public key is echoed verbatim; anything else is masked to- * its display form because it may be a secret placed in the wrong slot.+ * actually a Langfuse public key is echoed; anything else is masked to its+ * display form because it may be a secret placed in the wrong slot.+ *+ * The value reaches us straight from a client-controlled Authorization header,+ * and the callers that log it are the ones where the key was not found, so it+ * may be arbitrary bytes of arbitrary length. Log formatters interpolate a+ * message verbatim, so echoing the value as-is would let a caller forge log+ * lines with a newline, or drive an operator's terminal with an escape+ * sequence (CWE-117).+ *+ * A real public key is printable ASCII (`pk-lf-` plus a UUID), so allow-list+ * that range rather than enumerating the dangerous one: a single rule covers C0+ * controls, DEL and the C1 range, which `JSON.stringify` leaves unescaped.+ * `JSON.stringify` then quotes the result, delimiting it within the log line+ * and escaping any embedded quote or backslash.*/export function formatSubmittedPublicKeyForLog(value: string): string {- if (value.startsWith("pk-lf-")) return value;- if (value.length < 12) return "****";- return getDisplaySecretKey(value);+ let formatted: string;+ if (value.startsWith("pk-lf-")) formatted = value;+ else if (value.length < 12) formatted = "****";+ else formatted = getDisplaySecretKey(value);++ return JSON.stringify(+ formatted+ .slice(0, MAX_LOGGED_PUBLIC_KEY_LENGTH)+ .replace(/[^\x20-\x7e]/g, "\uFFFD"),+ );}export async function hashSecretKey(key: string) {diff --git a/web/src/features/public-api/server/apiAuth.ts b/web/src/features/public-api/server/apiAuth.tsindex f8ed67acc6..f5de4b4e03 100644--- a/web/src/features/public-api/server/apiAuth.ts+++ b/web/src/features/public-api/server/apiAuth.ts@@ -115,8 +115,7 @@ export class ApiAuthService {if (!slowKey) {logger.error(- "No key found for public key",- formatSubmittedPublicKeyForLog(publicKey),+ `No key found for public key: ${formatSubmittedPublicKeyForLog(publicKey)}`,);if (this.redis) {logger.info(@@ -158,15 +157,14 @@ export class ApiAuthService {if (!finalApiKey) {logger.info(- "No project id found for key",- formatSubmittedPublicKeyForLog(publicKey),+ `No project id found for key: ${formatSubmittedPublicKeyForLog(publicKey)}`,);throw new Error("Invalid credentials");}const plan = finalApiKey.plan;if (!isPlan(plan)) {- logger.error("Invalid plan type for key", finalApiKey.plan);+ logger.error(`Invalid plan type for key: ${finalApiKey.plan}`);throw new Error("Invalid credentials");}@@ -325,7 +323,9 @@ export class ApiAuthService {},});if (!dbKey) {- logger.info("No api key found for public key:", publicKey);+ logger.info(+ `No api key found for public key: ${formatSubmittedPublicKeyForLog(publicKey)}`,+ );throw new Error("Invalid public key");}return dbKey;