Skip to content

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

test(api-auth): assert API keys never contain colons (#12653)

Max Deichmann53dc98220852
diff --git a/packages/shared/src/server/auth/apiKeys.ts b/packages/shared/src/server/auth/apiKeys.ts
index f6f4bba41f..65345f69a6 100644
--- a/packages/shared/src/server/auth/apiKeys.ts
+++ b/packages/shared/src/server/auth/apiKeys.ts
@@ -14,7 +14,7 @@ export async function hashSecretKey(key: string) {
return hashedKey;
}
-async function generateKeySet() {
+export async function generateKeySet() {
return {
pk: `pk-lf-${randomUUID()}`,
sk: `sk-lf-${randomUUID()}`,
diff --git a/web/src/__tests__/server/api-auth.servertest.ts b/web/src/__tests__/server/api-auth.servertest.ts
index 7160a0e820..f46b93c66d 100644
--- a/web/src/__tests__/server/api-auth.servertest.ts
+++ b/web/src/__tests__/server/api-auth.servertest.ts
@@ -2,6 +2,7 @@ import {
OrgEnrichedApiKey,
createBasicAuthHeader,
createOrgProjectAndApiKey,
+ generateKeySet,
} from "@langfuse/shared/src/server";
import { Prisma, type PrismaClient, prisma } from "@langfuse/shared/src/db";
import { env } from "@/src/env.mjs";
@@ -849,4 +850,19 @@ describe("Authenticate API calls", () => {
expect(keys.length).toBe(0);
});
});
+
+ describe("API key format", () => {
+ it("generated public and secret keys never contain a colon (30 iterations)", async () => {
+ // Keys are built as `pk-lf-<uuid>` / `sk-lf-<uuid>`.
+ // UUIDs only use hex digits (0-9, a-f) and hyphens, so colons are
+ // structurally impossible – but we assert it explicitly so any future
+ // change to the generation logic gets caught before it can break
+ // Basic-Auth header parsing (which splits on ':').
+ for (let i = 0; i < 30; i++) {
+ const { pk, sk } = await generateKeySet();
+ expect(pk).not.toContain(":");
+ expect(sk).not.toContain(":");
+ }
+ });
+ });
});