🪢 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): redact secret keys submitted as public keys from logs and spans (#17478)
Steffen Schmitz74a52e4cf7f3
diff --git a/packages/shared/src/server/auth/apiKeys.test.ts b/packages/shared/src/server/auth/apiKeys.test.tsnew file mode 100644index 0000000000..06757b4f44--- /dev/null+++ b/packages/shared/src/server/auth/apiKeys.test.ts@@ -0,0 +1,52 @@+import { describe, expect, it } from "vitest";+import {+ formatSubmittedPublicKeyForLog,+ redactLangfuseSecretKeys,+} from "./apiKeys";++const SECRET_KEY = "sk-lf-0f2a4c6e-8b1d-4e3f-9a7c-5d6e7f8a9b0c";+const PUBLIC_KEY = "pk-lf-1a2b3c4d-5e6f-4a7b-8c9d-0e1f2a3b4c5d";++describe("redactLangfuseSecretKeys", () => {+ it("masks a bare secret key to its display form", () => {+ expect(redactLangfuseSecretKeys(SECRET_KEY)).toBe("sk-lf-...9b0c");+ });++ it("masks every secret key embedded in a larger string", () => {+ const input = JSON.stringify([+ SECRET_KEY,+ PUBLIC_KEY,+ "sk-lf-gw-abcdef1234",+ ]);+ expect(redactLangfuseSecretKeys(input)).toBe(+ JSON.stringify(["sk-lf-...9b0c", PUBLIC_KEY, "sk-lf-...1234"]),+ );+ });++ it("leaves strings without a secret key unchanged", () => {+ expect(redactLangfuseSecretKeys(PUBLIC_KEY)).toBe(PUBLIC_KEY);+ expect(redactLangfuseSecretKeys("python-sdk 3.1.0")).toBe(+ "python-sdk 3.1.0",+ );+ });+});++describe("formatSubmittedPublicKeyForLog", () => {+ it("passes a public key through verbatim", () => {+ expect(formatSubmittedPublicKeyForLog(PUBLIC_KEY)).toBe(PUBLIC_KEY);+ });++ it("masks a secret key to its display form", () => {+ 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");+ });++ it("does not echo short values whose head and tail would overlap", () => {+ expect(formatSubmittedPublicKeyForLog("None")).toBe("****");+ expect(formatSubmittedPublicKeyForLog("")).toBe("****");+ });+});diff --git a/packages/shared/src/server/auth/apiKeys.ts b/packages/shared/src/server/auth/apiKeys.tsindex 10eecd6a92..1429a3a0b8 100644--- a/packages/shared/src/server/auth/apiKeys.ts+++ b/packages/shared/src/server/auth/apiKeys.ts@@ -11,6 +11,30 @@ export function getDisplaySecretKey(secretKey: string) {return secretKey.slice(0, 6) + "..." + secretKey.slice(-4);}+const LANGFUSE_SECRET_KEY_PATTERN = /sk-lf-[A-Za-z0-9_-]+/g;++/**+ * Replaces every Langfuse secret key inside a user-controlled string with its+ * display form (`sk-lf-...abcd`), so the value can be logged or attached to a+ * span without exposing the secret.+ */+export function redactLangfuseSecretKeys(value: string): string {+ return value.replace(LANGFUSE_SECRET_KEY_PATTERN, (match) =>+ getDisplaySecretKey(match),+ );+}++/**+ * 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.+ */+export function formatSubmittedPublicKeyForLog(value: string): string {+ if (value.startsWith("pk-lf-")) return value;+ if (value.length < 12) return "****";+ return getDisplaySecretKey(value);+}+export async function hashSecretKey(key: string) {// legacy, uses bcrypt, transformed into hashed key upon first useconst hashedKey = await hash(key, 11);diff --git a/packages/shared/src/server/headerPropagation.ts b/packages/shared/src/server/headerPropagation.tsindex e76756b924..ca80a54168 100644--- a/packages/shared/src/server/headerPropagation.ts+++ b/packages/shared/src/server/headerPropagation.ts@@ -6,6 +6,7 @@ import {type ClickHouseQuerySurface,} from "./clickhouse/queryTags";import { extractPublicApiCallerAttribution } from "./ingestion/ingestionAttribution";+import { redactLangfuseSecretKeys } from "./auth/apiKeys";export type LangfuseContextProps = {headers?: IncomingHttpHeaders;@@ -45,7 +46,9 @@ export const contextWithLangfuseProps = ((env.LANGFUSE_LOG_PROPAGATED_HEADERS as string[]).forEach((name) => {const value = props.headers![name];if (!value) return;- const strValue = Array.isArray(value) ? JSON.stringify(value) : value;+ const strValue = redactLangfuseSecretKeys(+ Array.isArray(value) ? JSON.stringify(value) : value,+ );baggage = baggage.setEntry(`langfuse.header.${name}`, {value: strValue,});@@ -59,7 +62,9 @@ export const contextWithLangfuseProps = () {const value = props.headers![name];if (!value) return;- const strValue = Array.isArray(value) ? JSON.stringify(value) : value;+ const strValue = redactLangfuseSecretKeys(+ Array.isArray(value) ? JSON.stringify(value) : value,+ );baggage = baggage.setEntry(`langfuse.header.${name}`, {value: strValue,});diff --git a/web/src/__tests__/server/api-auth.servertest.ts b/web/src/__tests__/server/api-auth.servertest.tsindex 3673894347..ff5dcd2168 100644--- a/web/src/__tests__/server/api-auth.servertest.ts+++ b/web/src/__tests__/server/api-auth.servertest.ts@@ -367,6 +367,35 @@ describe("Authenticate API calls", () => {warnSpy.mockRestore();});+ it("redacts a secret key submitted in the public key slot from the mismatch warning", async () => {+ await new ApiAuthService(prisma, null).verifyAuthHeaderAndReturnScope(+ getValidAuthHeader(),+ );++ const warnSpy = vi.spyOn(logger, "warn");++ const auth = await new ApiAuthService(+ prisma,+ null,+ ).verifyAuthHeaderAndReturnScope(+ createBasicAuthHeader(testApiKey.secretKey, testApiKey.secretKey),+ );++ expect(auth.validKey).toBe(true);++ const mismatchWarning = warnSpy.mock.calls+ .map((call) => String(call[0]))+ .find((message) => message.includes("Public key mismatch"));+ expect(mismatchWarning).toBeDefined();+ expect(mismatchWarning).not.toContain(testApiKey.secretKey);+ expect(mismatchWarning).toContain(+ getDisplaySecretKey(testApiKey.secretKey),+ );+ expect(mismatchWarning).toContain(testApiKey.publicKey);++ warnSpy.mockRestore();+ });+it("does not warn when the submitted public key matches", async () => {await new ApiAuthService(prisma, null).verifyAuthHeaderAndReturnScope(getValidAuthHeader(),diff --git a/web/src/features/public-api/server/apiAuth.ts b/web/src/features/public-api/server/apiAuth.tsindex 5a79334377..3cc70fa025 100644--- a/web/src/features/public-api/server/apiAuth.ts+++ b/web/src/features/public-api/server/apiAuth.ts@@ -2,6 +2,7 @@ import { env } from "@/src/env.mjs";import {createShaHash,deleteApiKeyFromDb,+ formatSubmittedPublicKeyForLog,recordIncrement,verifySecretKey,type AuthHeaderVerificationResult,@@ -113,7 +114,10 @@ export class ApiAuthService {});if (!slowKey) {- logger.error("No key found for public key", publicKey);+ logger.error(+ "No key found for public key",+ formatSubmittedPublicKeyForLog(publicKey),+ );if (this.redis) {logger.info(`No key found, storing ${API_KEY_NON_EXISTENT} in redis`,@@ -132,7 +136,9 @@ export class ApiAuthService {);if (!isValid) {- logger.debug(`Old key is invalid: ${publicKey}`);+ logger.debug(+ `Old key is invalid: ${formatSubmittedPublicKeyForLog(publicKey)}`,+ );throw new Error("Invalid credentials");}@@ -151,7 +157,10 @@ export class ApiAuthService {}if (!finalApiKey) {- logger.info("No project id found for key", publicKey);+ logger.info(+ "No project id found for key",+ formatSubmittedPublicKeyForLog(publicKey),+ );throw new Error("Invalid credentials");}const plan = finalApiKey.plan;@@ -182,7 +191,7 @@ export class ApiAuthService {// 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})`,+ `Public key mismatch on basic auth: submitted public key ${formatSubmittedPublicKeyForLog(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})`,);}diff --git a/web/src/pages/api/public/ingestion.ts b/web/src/pages/api/public/ingestion.tsindex 802a0f3f70..2ef3af1198 100644--- a/web/src/pages/api/public/ingestion.ts+++ b/web/src/pages/api/public/ingestion.ts@@ -11,6 +11,7 @@ import {markProjectIngestFailure,createIngestionAttribution,processEventBatch,+ redactLangfuseSecretKeys,} from "@langfuse/shared/src/server";import { telemetry } from "@/src/features/telemetry";import { clickHouseRouteForRequest } from "@/src/features/public-api/server/clickHouseRequestTags";@@ -81,9 +82,13 @@ export default async function handler(header.toLowerCase().startsWith("x-langfuse") ||header.toLowerCase().startsWith("x_langfuse")) {+ const value = req.headers[header];+ if (value === undefined) return;currentSpan?.setAttributes({[`langfuse.header.${header.slice(11).toLowerCase().replaceAll("_", "-")}`]:- req.headers[header],+ Array.isArray(value)+ ? value.map(redactLangfuseSecretKeys)+ : redactLangfuseSecretKeys(value),});}});