Skip to content

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

fix(security): use constant-time comparison for admin API key auth (#13208)

Steffen Schmitzb8b5c6012aa8
diff --git a/web/src/ee/features/admin-api/server/adminApiAuth.ts b/web/src/ee/features/admin-api/server/adminApiAuth.ts
index 5350511926..90cdb2d090 100644
--- a/web/src/ee/features/admin-api/server/adminApiAuth.ts
+++ b/web/src/ee/features/admin-api/server/adminApiAuth.ts
@@ -1,3 +1,5 @@
+import crypto from "crypto";
+
import { env } from "@/src/env.mjs";
import { logger } from "@langfuse/shared/src/server";
import { type IncomingHttpHeaders } from "http";
@@ -41,7 +43,29 @@ export class AdminApiAuthService {
}
const [scheme, token] = authString.split(" ");
- if (scheme !== "Bearer" || !token || token !== env.ADMIN_API_KEY) {
+ if (scheme !== "Bearer" || !token) {
+ return {
+ isAuthorized: false,
+ error: "Unauthorized: Invalid token",
+ };
+ }
+
+ // Keep this comparison in sync with the project-scoped admin-key check in
+ // web/src/features/public-api/server/createAuthedProjectAPIRoute.ts.
+ try {
+ // timingSafeEqual throws on different input lengths, handle accordingly
+ if (
+ !crypto.timingSafeEqual(
+ Buffer.from(token),
+ Buffer.from(env.ADMIN_API_KEY),
+ )
+ ) {
+ return {
+ isAuthorized: false,
+ error: "Unauthorized: Invalid token",
+ };
+ }
+ } catch {
return {
isAuthorized: false,
error: "Unauthorized: Invalid token",
diff --git a/web/src/ee/features/multi-tenant-sso/createNewSsoConfigHandler.ts b/web/src/ee/features/multi-tenant-sso/createNewSsoConfigHandler.ts
index 236e009d32..d9ee657249 100644
--- a/web/src/ee/features/multi-tenant-sso/createNewSsoConfigHandler.ts
+++ b/web/src/ee/features/multi-tenant-sso/createNewSsoConfigHandler.ts
@@ -5,6 +5,7 @@ import { type NextApiRequest, type NextApiResponse } from "next";
import { env } from "@/src/env.mjs";
import { logger } from "@langfuse/shared/src/server";
import { multiTenantSsoAvailable } from "@/src/ee/features/multi-tenant-sso/multiTenantSsoAvailable";
+import { AdminApiAuthService } from "@/src/ee/features/admin-api/server/adminApiAuth";
export async function createNewSsoConfigHandler(
req: NextApiRequest,
@@ -22,26 +23,18 @@ export async function createNewSsoConfigHandler(
res.status(405).json({ error: "Method Not Allowed" });
return;
}
- // check if ADMIN_API_KEY is set
- if (!env.ADMIN_API_KEY) {
- res.status(500).json({ error: "ADMIN_API_KEY is not set" });
- return;
- }
if (!env.ENCRYPTION_KEY) {
res.status(500).json({ error: "ENCRYPTION_KEY is not set" });
return;
}
- // check bearer token
- const { authorization } = req.headers;
- if (!authorization) {
- res
- .status(401)
- .json({ error: "Unauthorized: No authorization header provided" });
- return;
- }
- const [scheme, token] = authorization.split(" ");
- if (scheme !== "Bearer" || !token || token !== env.ADMIN_API_KEY) {
- res.status(401).json({ error: "Unauthorized: Invalid token" });
+ // Authenticate via ADMIN_API_KEY with timing-safe comparison.
+ // multiTenantSsoAvailable already restricts this endpoint to Langfuse
+ // Cloud, so explicitly opt in with isAllowedOnLangfuseCloud.
+ if (
+ !AdminApiAuthService.handleAdminAuth(req, res, {
+ isAllowedOnLangfuseCloud: true,
+ })
+ ) {
return;
}
diff --git a/web/src/features/public-api/server/createAuthedProjectAPIRoute.ts b/web/src/features/public-api/server/createAuthedProjectAPIRoute.ts
index fcc0504466..3473c9d7c1 100644
--- a/web/src/features/public-api/server/createAuthedProjectAPIRoute.ts
+++ b/web/src/features/public-api/server/createAuthedProjectAPIRoute.ts
@@ -163,7 +163,9 @@ async function verifyAdminApiKeyAuth(req: NextApiRequest): Promise<
// Extract Bearer token
const bearerToken = authHeader.replace("Bearer ", "");
- // Verify both the Bearer token and header match the ADMIN_API_KEY
+ // Verify both the Bearer token and header match the ADMIN_API_KEY.
+ // Keep this comparison in sync with the admin-key check in
+ // web/src/ee/features/admin-api/server/adminApiAuth.ts.
try {
// timingSafeEqual throws on different input lengths, handle accordingly
const bearerTokenEqual = crypto.timingSafeEqual(