Skip to content

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

feat(auth): gate public-API project routes through the policy seam in all migration modes (#16661)

Mark Salpetere8ff49d283b6
diff --git a/.env.prod.example b/.env.prod.example
index c0ffff562e..548b531e7c 100644
--- a/.env.prod.example
+++ b/.env.prod.example
@@ -243,6 +243,9 @@ LANGFUSE_ENABLE_BLOB_STORAGE_FILE_LOG=true
# Cache configuration
# LANGFUSE_CACHE_API_KEY_ENABLED=
# LANGFUSE_CACHE_API_KEY_TTL_SECONDS=
+
+# Auth migration: legacy (default) | shadow | enforce
+# API_AUTH_MIGRATION=
# LANGFUSE_CACHE_PROMPT_ENABLED=
# LANGFUSE_CACHE_PROMPT_TTL_SECONDS=
diff --git a/packages/shared/src/features/rbac/projectAccessRights.ts b/packages/shared/src/features/rbac/projectAccessRights.ts
index 21c6883cbb..43011dbc10 100644
--- a/packages/shared/src/features/rbac/projectAccessRights.ts
+++ b/packages/shared/src/features/rbac/projectAccessRights.ts
@@ -71,7 +71,7 @@ export const projectScopes = [
"promptExperiments:CUD",
"promptExperiments:read",
- "auditLogs:read",
+ "projectAuditLogs:read",
"TableViewPresets:CUD",
"TableViewPresets:read",
@@ -81,6 +81,19 @@ export const projectScopes = [
"alerts:read",
"alerts:CUD",
+
+ // Public-API action tokens; not granted to any UI role.
+ "traces:read",
+ "traces:create",
+ "scores:read",
+ "scores:create",
+ "media:create",
+ "sessions:read",
+ "metrics:read",
+ "models:read",
+ "experiments:read",
+ "mcp:access",
+ "feedback:create",
] as const;
// type string of all Resource:Action, e.g. "members:read"
@@ -135,7 +148,7 @@ export const projectRoleAccessRights: Record<Role, ProjectScope[]> = {
"annotationQueueAssignments:CUD",
"promptExperiments:CUD",
"promptExperiments:read",
- "auditLogs:read",
+ "projectAuditLogs:read",
"dashboards:read",
"dashboards:CUD",
"TableViewPresets:CUD",
@@ -192,7 +205,7 @@ export const projectRoleAccessRights: Record<Role, ProjectScope[]> = {
"annotationQueueAssignments:CUD",
"promptExperiments:CUD",
"promptExperiments:read",
- "auditLogs:read",
+ "projectAuditLogs:read",
"dashboards:read",
"dashboards:CUD",
"TableViewPresets:CUD",
diff --git a/packages/shared/src/server/auth/apiKeyCache.ts b/packages/shared/src/server/auth/apiKeyCache.ts
index 4f2c89061c..d5651e90dc 100644
--- a/packages/shared/src/server/auth/apiKeyCache.ts
+++ b/packages/shared/src/server/auth/apiKeyCache.ts
@@ -4,3 +4,11 @@ export const createApiKeyCacheKey = (hash: string) =>
`${API_KEY_CACHE_KEY_PREFIX}${hash}`;
export const API_KEY_CACHE_PATTERN = createApiKeyCacheKey("*");
+
+/** AUTHZ_CONTEXT_CACHE_KEY_PREFIX namespaces the policy-core context cache, disjoint from the legacy `api-key:` cache so both coexist in shadow mode. */
+export const AUTHZ_CONTEXT_CACHE_KEY_PREFIX = "authz:context:";
+
+export const createAuthzContextCacheKey = (hash: string) =>
+ `${AUTHZ_CONTEXT_CACHE_KEY_PREFIX}${hash}`;
+
+export const AUTHZ_CONTEXT_CACHE_PATTERN = createAuthzContextCacheKey("*");
diff --git a/packages/shared/src/server/auth/invalidateApiKeys.ts b/packages/shared/src/server/auth/invalidateApiKeys.ts
index 0f95c0a13e..592a6c8cff 100644
--- a/packages/shared/src/server/auth/invalidateApiKeys.ts
+++ b/packages/shared/src/server/auth/invalidateApiKeys.ts
@@ -1,10 +1,38 @@
import { prisma } from "../../db";
import { redis, safeMultiDel, scanKeys } from "..";
import { logger } from "../logger";
+import { env } from "../../env";
import type { Cluster, Redis } from "ioredis";
import { type ApiKey } from "../../db";
-import { API_KEY_CACHE_PATTERN, createApiKeyCacheKey } from "./apiKeyCache";
+import {
+ API_KEY_CACHE_PATTERN,
+ AUTHZ_CONTEXT_CACHE_PATTERN,
+ createApiKeyCacheKey,
+ createAuthzContextCacheKey,
+} from "./apiKeyCache";
+import { createShaHash } from "./apiKeys";
+
+/**
+ * Redis keys to delete per API key row, across both cache namespaces:
+ * legacy `api-key:<fastHash>` plus the policy-core `authz:context:` keys for
+ * every presentation of the key — `sha(secret+salt)` (== fastHash) and
+ * `sha(publicKey+salt)`. Public-key rows are covered even without a fast hash.
+ */
+function cacheKeysForRows(apiKeys: ApiKey[]): string[] {
+ const salt = env.SALT;
+ const keys: string[] = [];
+ for (const key of apiKeys) {
+ if (key.fastHashedSecretKey) {
+ keys.push(createApiKeyCacheKey(key.fastHashedSecretKey));
+ keys.push(createAuthzContextCacheKey(key.fastHashedSecretKey));
+ }
+ if (salt && key.publicKey) {
+ keys.push(createAuthzContextCacheKey(createShaHash(key.publicKey, salt)));
+ }
+ }
+ return keys;
+}
/**
* Invalidate cached API keys from Redis cache
@@ -26,19 +54,14 @@ export async function invalidateCachedApiKeys(
identifier: string,
redisClient: Redis | Cluster | null = redis,
) {
- const hashKeys = apiKeys.map((key) => key.fastHashedSecretKey);
-
- const filteredHashKeys = hashKeys.filter((hash): hash is string =>
- Boolean(hash),
- );
- if (filteredHashKeys.length === 0) {
+ const keysToDelete = cacheKeysForRows(apiKeys);
+ if (keysToDelete.length === 0) {
logger.info("No valid keys to invalidate");
return;
}
if (redisClient) {
logger.info(`Invalidating API keys in redis for ${identifier}`);
- const keysToDelete = filteredHashKeys.map(createApiKeyCacheKey);
await safeMultiDel(redisClient, keysToDelete);
}
}
@@ -73,18 +96,14 @@ export async function invalidateCachedOrgApiKeys(
},
});
- const hashKeys = apiKeys
- .map((key) => key.fastHashedSecretKey)
- .filter((hash): hash is string => Boolean(hash));
-
- if (hashKeys.length === 0) {
+ const keysToDelete = cacheKeysForRows(apiKeys);
+ if (keysToDelete.length === 0) {
logger.info(`No valid API keys to invalidate for org ${orgId}`);
return;
}
if (redisClient) {
logger.info(`Invalidating API keys in redis for org ${orgId}`);
- const keysToDelete = hashKeys.map(createApiKeyCacheKey);
await safeMultiDel(redisClient, keysToDelete);
}
}
@@ -109,18 +128,14 @@ export async function invalidateCachedProjectApiKeys(
},
});
- const hashKeys = apiKeys
- .map((key) => key.fastHashedSecretKey)
- .filter((hash): hash is string => Boolean(hash));
-
- if (hashKeys.length === 0) {
+ const keysToDelete = cacheKeysForRows(apiKeys);
+ if (keysToDelete.length === 0) {
logger.info(`No valid API keys to invalidate for project ${projectId}`);
return;
}
if (redisClient) {
logger.info(`Invalidating API keys in redis for project ${projectId}`);
- const keysToDelete = hashKeys.map(createApiKeyCacheKey);
await safeMultiDel(redisClient, keysToDelete);
}
}
@@ -141,7 +156,11 @@ export async function invalidateAllCachedApiKeys(
return 0;
}
- const keysToDelete = await scanKeys(redisClient, API_KEY_CACHE_PATTERN);
+ const [legacyKeys, contextKeys] = await Promise.all([
+ scanKeys(redisClient, API_KEY_CACHE_PATTERN),
+ scanKeys(redisClient, AUTHZ_CONTEXT_CACHE_PATTERN),
+ ]);
+ const keysToDelete = [...legacyKeys, ...contextKeys];
if (keysToDelete.length === 0) {
logger.info("No cached API keys to invalidate");
diff --git a/packages/shared/src/server/test-utils/org-factory.ts b/packages/shared/src/server/test-utils/org-factory.ts
index 58e164b240..cc1e1ac0b3 100644
--- a/packages/shared/src/server/test-utils/org-factory.ts
+++ b/packages/shared/src/server/test-utils/org-factory.ts
@@ -39,7 +39,7 @@ export const createOrgProjectAndApiKey = async (
orgId: org.id,
},
});
- const publicKey = v4();
+ const publicKey = `pk-lf-${v4()}`;
const secretKey = `sk-lf-${v4()}`;
const salt = env.SALT;
if (!salt) {
diff --git a/web/src/__tests__/server/__snapshots__/project-route-auth-parity.servertest.ts.snap b/web/src/__tests__/server/__snapshots__/project-route-auth-parity.servertest.ts.snap
new file mode 100644
index 0000000000..2f0c41a22b
--- /dev/null
+++ b/web/src/__tests__/server/__snapshots__/project-route-auth-parity.servertest.ts.snap
@@ -0,0 +1,734 @@
+// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
+
+exports[`project-route auth parity > legacy matches the main-captured baseline 1`] = `
+{
+ "DELETE annotation-queues/[queueId]/assignments | admin/admin": 401,
+ "DELETE annotation-queues/[queueId]/assignments | agent/basic": 401,
+ "DELETE annotation-queues/[queueId]/assignments | agent/bearer": 401,
+ "DELETE annotation-queues/[queueId]/assignments | org/basic": 403,
+ "DELETE annotation-queues/[queueId]/assignments | org/bearer": 401,
+ "DELETE annotation-queues/[queueId]/assignments | project/basic": 400,
+ "DELETE annotation-queues/[queueId]/assignments | project/bearer": 403,
+ "DELETE annotation-queues/[queueId]/items/[itemId] | admin/admin": 401,
+ "DELETE annotation-queues/[queueId]/items/[itemId] | agent/basic": 401,
+ "DELETE annotation-queues/[queueId]/items/[itemId] | agent/bearer": 401,
+ "DELETE annotation-queues/[queueId]/items/[itemId] | org/basic": 403,
+ "DELETE annotation-queues/[queueId]/items/[itemId] | org/bearer": 401,
+ "DELETE annotation-queues/[queueId]/items/[itemId] | project/basic": 404,
+ "DELETE annotation-queues/[queueId]/items/[itemId] | project/bearer": 403,
+ "DELETE dataset-items/[datasetItemId] | admin/admin": 401,
+ "DELETE dataset-items/[datasetItemId] | agent/basic": 401,
+ "DELETE dataset-items/[datasetItemId] | agent/bearer": 401,
+ "DELETE dataset-items/[datasetItemId] | org/basic": 403,
+ "DELETE dataset-items/[datasetItemId] | org/bearer": 401,
+ "DELETE dataset-items/[datasetItemId] | project/basic": 404,
+ "DELETE dataset-items/[datasetItemId] | project/bearer": 403,
+ "DELETE datasets/[name]/runs/[runName] | admin/admin": 401,
+ "DELETE datasets/[name]/runs/[runName] | agent/basic": 401,
+ "DELETE datasets/[name]/runs/[runName] | agent/bearer": 401,
+ "DELETE datasets/[name]/runs/[runName] | org/basic": 403,
+ "DELETE datasets/[name]/runs/[runName] | org/bearer": 401,
+ "DELETE datasets/[name]/runs/[runName] | project/basic": 404,
+ "DELETE datasets/[name]/runs/[runName] | project/bearer": 403,
+ "DELETE llm-connections/[id] | admin/admin": 403,
+ "DELETE llm-connections/[id] | agent/basic": 401,
+ "DELETE llm-connections/[id] | agent/bearer": 401,
+ "DELETE llm-connections/[id] | org/basic": 403,
+ "DELETE llm-connections/[id] | org/bearer": 401,
+ "DELETE llm-connections/[id] | project/basic": 404,
+ "DELETE llm-connections/[id] | project/bearer": 403,
+ "DELETE models/[modelId]/index | admin/admin": 403,
+ "DELETE models/[modelId]/index | agent/basic": 401,
+ "DELETE models/[modelId]/index | agent/bearer": 401,
+ "DELETE models/[modelId]/index | org/basic": 403,
+ "DELETE models/[modelId]/index | org/bearer": 401,
+ "DELETE models/[modelId]/index | project/basic": 404,
+ "DELETE models/[modelId]/index | project/bearer": 403,
+ "DELETE scores/[scoreId] | admin/admin": 401,
+ "DELETE scores/[scoreId] | agent/basic": 401,
+ "DELETE scores/[scoreId] | agent/bearer": 401,
+ "DELETE scores/[scoreId] | org/basic": 403,
+ "DELETE scores/[scoreId] | org/bearer": 401,
+ "DELETE scores/[scoreId] | project/basic": 202,
+ "DELETE scores/[scoreId] | project/bearer": 403,
+ "DELETE traces/[traceId] | admin/admin": 401,
+ "DELETE traces/[traceId] | agent/basic": 401,
+ "DELETE traces/[traceId] | agent/bearer": 401,
+ "DELETE traces/[traceId] | org/basic": 403,
+ "DELETE traces/[traceId] | org/bearer": 401,
+ "DELETE traces/[traceId] | project/basic": 200,
+ "DELETE traces/[traceId] | project/bearer": 403,
+ "DELETE traces/index | admin/admin": 401,
+ "DELETE traces/index | agent/basic": 401,
+ "DELETE traces/index | agent/bearer": 401,
+ "DELETE traces/index | org/basic": 403,
+ "DELETE traces/index | org/bearer": 401,
+ "DELETE traces/index | project/basic": 400,
+ "DELETE traces/index | project/bearer": 403,
+ "DELETE unstable/dashboard-widgets/[widgetId] | admin/admin": 401,
+ "DELETE unstable/dashboard-widgets/[widgetId] | agent/basic": 401,
+ "DELETE unstable/dashboard-widgets/[widgetId] | agent/bearer": 401,
+ "DELETE unstable/dashboard-widgets/[widgetId] | org/basic": 403,
+ "DELETE unstable/dashboard-widgets/[widgetId] | org/bearer": 401,
+ "DELETE unstable/dashboard-widgets/[widgetId] | project/basic": 404,
+ "DELETE unstable/dashboard-widgets/[widgetId] | project/bearer": 403,
+ "DELETE unstable/dashboards/[dashboardId] | admin/admin": 401,
+ "DELETE unstable/dashboards/[dashboardId] | agent/basic": 401,
+ "DELETE unstable/dashboards/[dashboardId] | agent/bearer": 401,
+ "DELETE unstable/dashboards/[dashboardId] | org/basic": 403,
+ "DELETE unstable/dashboards/[dashboardId] | org/bearer": 401,
+ "DELETE unstable/dashboards/[dashboardId] | project/basic": 404,
+ "DELETE unstable/dashboards/[dashboardId] | project/bearer": 403,
+ "DELETE unstable/dashboards/[dashboardId]/placements/[placementId] | admin/admin": 401,
+ "DELETE unstable/dashboards/[dashboardId]/placements/[placementId] | agent/basic": 401,
+ "DELETE unstable/dashboards/[dashboardId]/placements/[placementId] | agent/bearer": 401,
+ "DELETE unstable/dashboards/[dashboardId]/placements/[placementId] | org/basic": 403,
+ "DELETE unstable/dashboards/[dashboardId]/placements/[placementId] | org/bearer": 401,
+ "DELETE unstable/dashboards/[dashboardId]/placements/[placementId] | project/basic": 404,
+ "DELETE unstable/dashboards/[dashboardId]/placements/[placementId] | project/bearer": 403,
+ "DELETE unstable/evaluation-rules/[evaluationRuleId] | admin/admin": 401,
+ "DELETE unstable/evaluation-rules/[evaluationRuleId] | agent/basic": 401,
+ "DELETE unstable/evaluation-rules/[evaluationRuleId] | agent/bearer": 401,
+ "DELETE unstable/evaluation-rules/[evaluationRuleId] | org/basic": 403,
+ "DELETE unstable/evaluation-rules/[evaluationRuleId] | org/bearer": 401,
+ "DELETE unstable/evaluation-rules/[evaluationRuleId] | project/basic": 404,
+ "DELETE unstable/evaluation-rules/[evaluationRuleId] | project/bearer": 403,
+ "DELETE unstable/evaluators/[evaluatorId] | admin/admin": 401,
+ "DELETE unstable/evaluators/[evaluatorId] | agent/basic": 401,
+ "DELETE unstable/evaluators/[evaluatorId] | agent/bearer": 401,
+ "DELETE unstable/evaluators/[evaluatorId] | org/basic": 403,
+ "DELETE unstable/evaluators/[evaluatorId] | org/bearer": 401,
+ "DELETE unstable/evaluators/[evaluatorId] | project/basic": 404,
+ "DELETE unstable/evaluators/[evaluatorId] | project/bearer": 403,
+ "DELETE v2/evaluation-rules/[evaluationRuleId] | admin/admin": 401,
+ "DELETE v2/evaluation-rules/[evaluationRuleId] | agent/basic": 401,
+ "DELETE v2/evaluation-rules/[evaluationRuleId] | agent/bearer": 401,
+ "DELETE v2/evaluation-rules/[evaluationRuleId] | org/basic": 403,
+ "DELETE v2/evaluation-rules/[evaluationRuleId] | org/bearer": 401,
+ "DELETE v2/evaluation-rules/[evaluationRuleId] | project/basic": 404,
+ "DELETE v2/evaluation-rules/[evaluationRuleId] | project/bearer": 403,
+ "DELETE v2/evaluators/[evaluatorId] | admin/admin": 401,
+ "DELETE v2/evaluators/[evaluatorId] | agent/basic": 401,
+ "DELETE v2/evaluators/[evaluatorId] | agent/bearer": 401,
+ "DELETE v2/evaluators/[evaluatorId] | org/basic": 403,
+ "DELETE v2/evaluators/[evaluatorId] | org/bearer": 401,
+ "DELETE v2/evaluators/[evaluatorId] | project/basic": 404,
+ "DELETE v2/evaluators/[evaluatorId] | project/bearer": 403,
+ "GET annotation-queues/[queueId]/index | admin/admin": 401,
+ "GET annotation-queues/[queueId]/index | agent/basic": 401,
+ "GET annotation-queues/[queueId]/index | agent/bearer": 401,
+ "GET annotation-queues/[queueId]/index | org/basic": 403,
+ "GET annotation-queues/[queueId]/index | org/bearer": 401,
+ "GET annotation-queues/[queueId]/index | project/basic": 404,
+ "GET annotation-queues/[queueId]/index | project/bearer": 403,
+ "GET annotation-queues/[queueId]/items/[itemId] | admin/admin": 401,
+ "GET annotation-queues/[queueId]/items/[itemId] | agent/basic": 401,
+ "GET annotation-queues/[queueId]/items/[itemId] | agent/bearer": 401,
+ "GET annotation-queues/[queueId]/items/[itemId] | org/basic": 403,
+ "GET annotation-queues/[queueId]/items/[itemId] | org/bearer": 401,
+ "GET annotation-queues/[queueId]/items/[itemId] | project/basic": 404,
+ "GET annotation-queues/[queueId]/items/[itemId] | project/bearer": 403,
+ "GET annotation-queues/[queueId]/items/index | admin/admin": 401,
+ "GET annotation-queues/[queueId]/items/index | agent/basic": 401,
+ "GET annotation-queues/[queueId]/items/index | agent/bearer": 401,
+ "GET annotation-queues/[queueId]/items/index | org/basic": 403,
+ "GET annotation-queues/[queueId]/items/index | org/bearer": 401,
+ "GET annotation-queues/[queueId]/items/index | project/basic": 404,
+ "GET annotation-queues/[queueId]/items/index | project/bearer": 403,
+ "GET annotation-queues/index | admin/admin": 401,
+ "GET annotation-queues/index | agent/basic": 401,
+ "GET annotation-queues/index | agent/bearer": 401,
+ "GET annotation-queues/index | org/basic": 403,
+ "GET annotation-queues/index | org/bearer": 401,
+ "GET annotation-queues/index | project/basic": 200,
+ "GET annotation-queues/index | project/bearer": 403,
+ "GET comments/[commentId] | admin/admin": 401,
+ "GET comments/[commentId] | agent/basic": 401,
+ "GET comments/[commentId] | agent/bearer": 401,
+ "GET comments/[commentId] | org/basic": 403,
+ "GET comments/[commentId] | org/bearer": 401,
+ "GET comments/[commentId] | project/basic": 404,
+ "GET comments/[commentId] | project/bearer": 403,
+ "GET comments/index | admin/admin": 401,
+ "GET comments/index | agent/basic": 401,
+ "GET comments/index | agent/bearer": 401,
+ "GET comments/index | org/basic": 403,
+ "GET comments/index | org/bearer": 401,
+ "GET comments/index | project/basic": 200,
+ "GET comments/index | project/bearer": 403,
+ "GET dataset-items/[datasetItemId] | admin/admin": 401,
+ "GET dataset-items/[datasetItemId] | agent/basic": 401,
+ "GET dataset-items/[datasetItemId] | agent/bearer": 401,
+ "GET dataset-items/[datasetItemId] | org/basic": 403,
+ "GET dataset-items/[datasetItemId] | org/bearer": 401,
+ "GET dataset-items/[datasetItemId] | project/basic": 404,
+ "GET dataset-items/[datasetItemId] | project/bearer": 403,
+ "GET dataset-items/index | admin/admin": 401,
+ "GET dataset-items/index | agent/basic": 401,
+ "GET dataset-items/index | agent/bearer": 401,
+ "GET dataset-items/index | org/basic": 403,
+ "GET dataset-items/index | org/bearer": 401,
+ "GET dataset-items/index | project/basic": 200,
+ "GET dataset-items/index | project/bearer": 403,
+ "GET dataset-run-items | admin/admin": 401,
+ "GET dataset-run-items | agent/basic": 401,
+ "GET dataset-run-items | agent/bearer": 401,
+ "GET dataset-run-items | org/basic": 403,
+ "GET dataset-run-items | org/bearer": 401,
+ "GET dataset-run-items | project/basic": 400,
+ "GET dataset-run-items | project/bearer": 403,
+ "GET datasets/[name]/index | admin/admin": 401,
+ "GET datasets/[name]/index | agent/basic": 401,
+ "GET datasets/[name]/index | agent/bearer": 401,
+ "GET datasets/[name]/index | org/basic": 403,
+ "GET datasets/[name]/index | org/bearer": 401,
+ "GET datasets/[name]/index | project/basic": 404,
+ "GET datasets/[name]/index | project/bearer": 403,
+ "GET datasets/[name]/runs/[runName] | admin/admin": 401,
+ "GET datasets/[name]/runs/[runName] | agent/basic": 401,
+ "GET datasets/[name]/runs/[runName] | agent/bearer": 401,
+ "GET datasets/[name]/runs/[runName] | org/basic": 403,
+ "GET datasets/[name]/runs/[runName] | org/bearer": 401,
+ "GET datasets/[name]/runs/[runName] | project/basic": 404,
+ "GET datasets/[name]/runs/[runName] | project/bearer": 403,
+ "GET datasets/[name]/runs/index | admin/admin": 401,
+ "GET datasets/[name]/runs/index | agent/basic": 401,
+ "GET datasets/[name]/runs/index | agent/bearer": 401,
+ "GET datasets/[name]/runs/index | org/basic": 403,
+ "GET datasets/[name]/runs/index | org/bearer": 401,
+ "GET datasets/[name]/runs/index | project/basic": 404,
+ "GET datasets/[name]/runs/index | project/bearer": 403,
+ "GET datasets/index | admin/admin": 401,
+ "GET datasets/index | agent/basic": 401,
+ "GET datasets/index | agent/bearer": 401,
+ "GET datasets/index | org/basic": 403,
+ "GET datasets/index | org/bearer": 401,
+ "GET datasets/index | project/basic": 200,
+ "GET datasets/index | project/bearer": 403,
+ "GET experiment-items/index | admin/admin": 401,
+ "GET experiment-items/index | agent/basic": 400,
+ "GET experiment-items/index | agent/bearer": 403,
+ "GET experiment-items/index | org/basic": 403,
+ "GET experiment-items/index | org/bearer": 401,
+ "GET experiment-items/index | project/basic": 400,
+ "GET experiment-items/index | project/bearer": 403,
+ "GET experiments/index | admin/admin": 401,
+ "GET experiments/index | agent/basic": 400,
+ "GET experiments/index | agent/bearer": 403,
+ "GET experiments/index | org/basic": 403,
+ "GET experiments/index | org/bearer": 401,
+ "GET experiments/index | project/basic": 400,
+ "GET experiments/index | project/bearer": 403,
+ "GET llm-connections/index | admin/admin": 403,
+ "GET llm-connections/index | agent/basic": 401,
+ "GET llm-connections/index | agent/bearer": 401,
+ "GET llm-connections/index | org/basic": 403,
+ "GET llm-connections/index | org/bearer": 401,
+ "GET llm-connections/index | project/basic": 200,
+ "GET llm-connections/index | project/bearer": 403,
+ "GET media/[mediaId] | admin/admin": 401,
+ "GET media/[mediaId] | agent/basic": 401,
+ "GET media/[mediaId] | agent/bearer": 401,
+ "GET media/[mediaId] | org/basic": 403,
+ "GET media/[mediaId] | org/bearer": 401,
+ "GET media/[mediaId] | project/basic": 404,
+ "GET media/[mediaId] | project/bearer": 403,
+ "GET metrics/daily | admin/admin": 401,
+ "GET metrics/daily | agent/basic": 401,
+ "GET metrics/daily | agent/bearer": 401,
+ "GET metrics/daily | org/basic": 403,
+ "GET metrics/daily | org/bearer": 401,
+ "GET metrics/daily | project/basic": 200,
+ "GET metrics/daily | project/bearer": 403,
+ "GET metrics/index | admin/admin": 401,
+ "GET metrics/index | agent/basic": 401,
+ "GET metrics/index | agent/bearer": 401,
+ "GET metrics/index | org/basic": 403,
+ "GET metrics/index | org/bearer": 401,
+ "GET metrics/index | project/basic": 400,
+ "GET metrics/index | project/bearer": 403,
+ "GET models/[modelId]/index | admin/admin": 403,
+ "GET models/[modelId]/index | agent/basic": 401,
+ "GET models/[modelId]/index | agent/bearer": 401,
+ "GET models/[modelId]/index | org/basic": 403,
+ "GET models/[modelId]/index | org/bearer": 401,
+ "GET models/[modelId]/index | project/basic": 404,
+ "GET models/[modelId]/index | project/bearer": 403,
+ "GET models/index | admin/admin": 403,
+ "GET models/index | agent/basic": 401,
+ "GET models/index | agent/bearer": 401,
+ "GET models/index | org/basic": 403,
+ "GET models/index | org/bearer": 401,
+ "GET models/index | project/basic": 200,
+ "GET models/index | project/bearer": 403,
+ "GET observations/[observationId] | admin/admin": 401,
+ "GET observations/[observationId] | agent/basic": 404,
+ "GET observations/[observationId] | agent/bearer": 403,
+ "GET observations/[observationId] | org/basic": 403,
+ "GET observations/[observationId] | org/bearer": 401,
+ "GET observations/[observationId] | project/basic": 404,
+ "GET observations/[observationId] | project/bearer": 403,
+ "GET observations/index | admin/admin": 401,
+ "GET observations/index | agent/basic": 200,
+ "GET observations/index | agent/bearer": 403,
+ "GET observations/index | org/basic": 403,
+ "GET observations/index | org/bearer": 401,
+ "GET observations/index | project/basic": 200,
+ "GET observations/index | project/bearer": 403,
+ "GET score-configs/[configId] | admin/admin": 401,
+ "GET score-configs/[configId] | agent/basic": 401,
+ "GET score-configs/[configId] | agent/bearer": 401,
+ "GET score-configs/[configId] | org/basic": 403,
+ "GET score-configs/[configId] | org/bearer": 401,
+ "GET score-configs/[configId] | project/basic": 404,
+ "GET score-configs/[configId] | project/bearer": 403,
+ "GET score-configs/index | admin/admin": 401,
+ "GET score-configs/index | agent/basic": 401,
+ "GET score-configs/index | agent/bearer": 401,
+ "GET score-configs/index | org/basic": 403,
+ "GET score-configs/index | org/bearer": 401,
+ "GET score-configs/index | project/basic": 200,
+ "GET score-configs/index | project/bearer": 403,
+ "GET scores/[scoreId] | admin/admin": 401,
+ "GET scores/[scoreId] | agent/basic": 401,
+ "GET scores/[scoreId] | agent/bearer": 401,
+ "GET scores/[scoreId] | org/basic": 403,
+ "GET scores/[scoreId] | org/bearer": 401,
+ "GET scores/[scoreId] | project/basic": 404,
+ "GET scores/[scoreId] | project/bearer": 403,
+ "GET scores/index | admin/admin": 401,
+ "GET scores/index | agent/basic": 401,
+ "GET scores/index | agent/bearer": 401,
+ "GET scores/index | org/basic": 403,
+ "GET scores/index | org/bearer": 401,
+ "GET scores/index | project/basic": 200,
+ "GET scores/index | project/bearer": 403,
+ "GET sessions/[sessionId] | admin/admin": 401,
+ "GET sessions/[sessionId] | agent/basic": 401,
+ "GET sessions/[sessionId] | agent/bearer": 401,
+ "GET sessions/[sessionId] | org/basic": 403,
+ "GET sessions/[sessionId] | org/bearer": 401,
+ "GET sessions/[sessionId] | project/basic": 404,
+ "GET sessions/[sessionId] | project/bearer": 403,
+ "GET sessions/index | admin/admin": 401,
+ "GET sessions/index | agent/basic": 401,
+ "GET sessions/index | agent/bearer": 401,
+ "GET sessions/index | org/basic": 403,
+ "GET sessions/index | org/bearer": 401,
+ "GET sessions/index | project/basic": 200,
+ "GET sessions/index | project/bearer": 403,
+ "GET traces/[traceId] | admin/admin": 401,
+ "GET traces/[traceId] | agent/basic": 401,
+ "GET traces/[traceId] | agent/bearer": 401,
+ "GET traces/[traceId] | org/basic": 403,
+ "GET traces/[traceId] | org/bearer": 401,
+ "GET traces/[traceId] | project/basic": 404,
+ "GET traces/[traceId] | project/bearer": 403,
+ "GET traces/index | admin/admin": 401,
+ "GET traces/index | agent/basic": 401,
+ "GET traces/index | agent/bearer": 401,
+ "GET traces/index | org/basic": 403,
+ "GET traces/index | org/bearer": 401,
+ "GET traces/index | project/basic": 200,
+ "GET traces/index | project/bearer": 403,
+ "GET unstable/dashboard-widgets/[widgetId] | admin/admin": 401,
+ "GET unstable/dashboard-widgets/[widgetId] | agent/basic": 401,
+ "GET unstable/dashboard-widgets/[widgetId] | agent/bearer": 401,
+ "GET unstable/dashboard-widgets/[widgetId] | org/basic": 403,
+ "GET unstable/dashboard-widgets/[widgetId] | org/bearer": 401,
+ "GET unstable/dashboard-widgets/[widgetId] | project/basic": 404,
+ "GET unstable/dashboard-widgets/[widgetId] | project/bearer": 403,
+ "GET unstable/dashboard-widgets/index | admin/admin": 401,
+ "GET unstable/dashboard-widgets/index | agent/basic": 401,
+ "GET unstable/dashboard-widgets/index | agent/bearer": 401,
+ "GET unstable/dashboard-widgets/index | org/basic": 403,
+ "GET unstable/dashboard-widgets/index | org/bearer": 401,
+ "GET unstable/dashboard-widgets/index | project/basic": 200,
+ "GET unstable/dashboard-widgets/index | project/bearer": 403,
+ "GET unstable/dashboards/[dashboardId] | admin/admin": 401,
+ "GET unstable/dashboards/[dashboardId] | agent/basic": 401,
+ "GET unstable/dashboards/[dashboardId] | agent/bearer": 401,
+ "GET unstable/dashboards/[dashboardId] | org/basic": 403,
+ "GET unstable/dashboards/[dashboardId] | org/bearer": 401,
+ "GET unstable/dashboards/[dashboardId] | project/basic": 404,
+ "GET unstable/dashboards/[dashboardId] | project/bearer": 403,
+ "GET unstable/dashboards/index | admin/admin": 401,
+ "GET unstable/dashboards/index | agent/basic": 401,
+ "GET unstable/dashboards/index | agent/bearer": 401,
+ "GET unstable/dashboards/index | org/basic": 403,
+ "GET unstable/dashboards/index | org/bearer": 401,
+ "GET unstable/dashboards/index | project/basic": 200,
+ "GET unstable/dashboards/index | project/bearer": 403,
+ "GET unstable/evaluation-rules/[evaluationRuleId] | admin/admin": 401,
+ "GET unstable/evaluation-rules/[evaluationRuleId] | agent/basic": 401,
+ "GET unstable/evaluation-rules/[evaluationRuleId] | agent/bearer": 401,
+ "GET unstable/evaluation-rules/[evaluationRuleId] | org/basic": 403,
+ "GET unstable/evaluation-rules/[evaluationRuleId] | org/bearer": 401,
+ "GET unstable/evaluation-rules/[evaluationRuleId] | project/basic": 404,
+ "GET unstable/evaluation-rules/[evaluationRuleId] | project/bearer": 403,
+ "GET unstable/evaluation-rules/index | admin/admin": 401,
+ "GET unstable/evaluation-rules/index | agent/basic": 401,
+ "GET unstable/evaluation-rules/index | agent/bearer": 401,
+ "GET unstable/evaluation-rules/index | org/basic": 403,
+ "GET unstable/evaluation-rules/index | org/bearer": 401,
+ "GET unstable/evaluation-rules/index | project/basic": 200,
+ "GET unstable/evaluation-rules/index | project/bearer": 403,
+ "GET unstable/evaluators/[evaluatorId] | admin/admin": 401,
+ "GET unstable/evaluators/[evaluatorId] | agent/basic": 401,
+ "GET unstable/evaluators/[evaluatorId] | agent/bearer": 401,
+ "GET unstable/evaluators/[evaluatorId] | org/basic": 403,
+ "GET unstable/evaluators/[evaluatorId] | org/bearer": 401,
+ "GET unstable/evaluators/[evaluatorId] | project/basic": 404,
+ "GET unstable/evaluators/[evaluatorId] | project/bearer": 403,
+ "GET unstable/evaluators/index | admin/admin": 401,
+ "GET unstable/evaluators/index | agent/basic": 401,
+ "GET unstable/evaluators/index | agent/bearer": 401,
+ "GET unstable/evaluators/index | org/basic": 403,
+ "GET unstable/evaluators/index | org/bearer": 401,
+ "GET unstable/evaluators/index | project/basic": 200,
+ "GET unstable/evaluators/index | project/bearer": 403,
+ "GET v2/datasets/[datasetName]/index | admin/admin": 401,
+ "GET v2/datasets/[datasetName]/index | agent/basic": 401,
+ "GET v2/datasets/[datasetName]/index | agent/bearer": 401,
+ "GET v2/datasets/[datasetName]/index | org/basic": 403,
+ "GET v2/datasets/[datasetName]/index | org/bearer": 401,
+ "GET v2/datasets/[datasetName]/index | project/basic": 404,
+ "GET v2/datasets/[datasetName]/index | project/bearer": 403,
+ "GET v2/datasets/index | admin/admin": 401,
+ "GET v2/datasets/index | agent/basic": 401,
+ "GET v2/datasets/index | agent/bearer": 401,
+ "GET v2/datasets/index | org/basic": 403,
+ "GET v2/datasets/index | org/bearer": 401,
+ "GET v2/datasets/index | project/basic": 200,
+ "GET v2/datasets/index | project/bearer": 403,
+ "GET v2/evaluation-rules/[evaluationRuleId] | admin/admin": 401,
+ "GET v2/evaluation-rules/[evaluationRuleId] | agent/basic": 401,
+ "GET v2/evaluation-rules/[evaluationRuleId] | agent/bearer": 401,
+ "GET v2/evaluation-rules/[evaluationRuleId] | org/basic": 403,
+ "GET v2/evaluation-rules/[evaluationRuleId] | org/bearer": 401,
+ "GET v2/evaluation-rules/[evaluationRuleId] | project/basic": 404,
+ "GET v2/evaluation-rules/[evaluationRuleId] | project/bearer": 403,
+ "GET v2/evaluation-rules/index | admin/admin": 401,
+ "GET v2/evaluation-rules/index | agent/basic": 401,
+ "GET v2/evaluation-rules/index | agent/bearer": 401,
+ "GET v2/evaluation-rules/index | org/basic": 403,
+ "GET v2/evaluation-rules/index | org/bearer": 401,
+ "GET v2/evaluation-rules/index | project/basic": 200,
+ "GET v2/evaluation-rules/index | project/bearer": 403,
+ "GET v2/evaluators/[evaluatorId] | admin/admin": 401,
+ "GET v2/evaluators/[evaluatorId] | agent/basic": 401,
+ "GET v2/evaluators/[evaluatorId] | agent/bearer": 401,
+ "GET v2/evaluators/[evaluatorId] | org/basic": 403,
+ "GET v2/evaluators/[evaluatorId] | org/bearer": 401,
+ "GET v2/evaluators/[evaluatorId] | project/basic": 404,
+ "GET v2/evaluators/[evaluatorId] | project/bearer": 403,
+ "GET v2/evaluators/[evaluatorId]/versions | admin/admin": 401,
+ "GET v2/evaluators/[evaluatorId]/versions | agent/basic": 401,
+ "GET v2/evaluators/[evaluatorId]/versions | agent/bearer": 401,
+ "GET v2/evaluators/[evaluatorId]/versions | org/basic": 403,
+ "GET v2/evaluators/[evaluatorId]/versions | org/bearer": 401,
+ "GET v2/evaluators/[evaluatorId]/versions | project/basic": 404,
+ "GET v2/evaluators/[evaluatorId]/versions | project/bearer": 403,
+ "GET v2/evaluators/index | admin/admin": 401,
+ "GET v2/evaluators/index | agent/basic": 401,
+ "GET v2/evaluators/index | agent/bearer": 401,
+ "GET v2/evaluators/index | org/basic": 403,
+ "GET v2/evaluators/index | org/bearer": 401,
+ "GET v2/evaluators/index | project/basic": 200,
+ "GET v2/evaluators/index | project/bearer": 403,
+ "GET v2/metrics | admin/admin": 401,
+ "GET v2/metrics | agent/basic": 401,
+ "GET v2/metrics | agent/bearer": 401,
+ "GET v2/metrics | org/basic": 403,
+ "GET v2/metrics | org/bearer": 401,
+ "GET v2/metrics | project/basic": 400,
+ "GET v2/metrics | project/bearer": 403,
+ "GET v2/observations/index | admin/admin": 401,
+ "GET v2/observations/index | agent/basic": 200,
+ "GET v2/observations/index | agent/bearer": 403,
+ "GET v2/observations/index | org/basic": 403,
+ "GET v2/observations/index | org/bearer": 401,
+ "GET v2/observations/index | project/basic": 200,
+ "GET v2/observations/index | project/bearer": 403,
+ "GET v2/scores/[scoreId] | admin/admin": 401,
+ "GET v2/scores/[scoreId] | agent/basic": 401,
+ "GET v2/scores/[scoreId] | agent/bearer": 401,
+ "GET v2/scores/[scoreId] | org/basic": 403,
+ "GET v2/scores/[scoreId] | org/bearer": 401,
+ "GET v2/scores/[scoreId] | project/basic": 404,
+ "GET v2/scores/[scoreId] | project/bearer": 403,
+ "GET v2/scores/index | admin/admin": 401,
+ "GET v2/scores/index | agent/basic": 401,
+ "GET v2/scores/index | agent/bearer": 401,
+ "GET v2/scores/index | org/basic": 403,
+ "GET v2/scores/index | org/bearer": 401,
+ "GET v2/scores/index | project/basic": 200,
+ "GET v2/scores/index | project/bearer": 403,
+ "GET v3/scores/index | admin/admin": 401,
+ "GET v3/scores/index | agent/basic": 401,
+ "GET v3/scores/index | agent/bearer": 401,
+ "GET v3/scores/index | org/basic": 403,
+ "GET v3/scores/index | org/bearer": 401,
+ "GET v3/scores/index | project/basic": 200,
+ "GET v3/scores/index | project/bearer": 403,
+ "PATCH annotation-queues/[queueId]/items/[itemId] | admin/admin": 401,
+ "PATCH annotation-queues/[queueId]/items/[itemId] | agent/basic": 401,
+ "PATCH annotation-queues/[queueId]/items/[itemId] | agent/bearer": 401,
+ "PATCH annotation-queues/[queueId]/items/[itemId] | org/basic": 403,
+ "PATCH annotation-queues/[queueId]/items/[itemId] | org/bearer": 401,
+ "PATCH annotation-queues/[queueId]/items/[itemId] | project/basic": 404,
+ "PATCH annotation-queues/[queueId]/items/[itemId] | project/bearer": 403,
+ "PATCH generations | admin/admin": 401,
+ "PATCH generations | agent/basic": 401,
+ "PATCH generations | agent/bearer": 401,
+ "PATCH generations | org/basic": 403,
+ "PATCH generations | org/bearer": 401,
+ "PATCH generations | project/basic": 400,
+ "PATCH generations | project/bearer": 403,
+ "PATCH media/[mediaId] | admin/admin": 401,
+ "PATCH media/[mediaId] | agent/basic": 401,
+ "PATCH media/[mediaId] | agent/bearer": 401,
+ "PATCH media/[mediaId] | org/basic": 403,
+ "PATCH media/[mediaId] | org/bearer": 401,
+ "PATCH media/[mediaId] | project/basic": 400,
+ "PATCH media/[mediaId] | project/bearer": 403,
+ "PATCH score-configs/[configId] | admin/admin": 401,
+ "PATCH score-configs/[configId] | agent/basic": 401,
+ "PATCH score-configs/[configId] | agent/bearer": 401,
+ "PATCH score-configs/[configId] | org/basic": 403,
+ "PATCH score-configs/[configId] | org/bearer": 401,
+ "PATCH score-configs/[configId] | project/basic": 400,
+ "PATCH score-configs/[configId] | project/bearer": 403,
+ "PATCH spans | admin/admin": 401,
+ "PATCH spans | agent/basic": 401,
+ "PATCH spans | agent/bearer": 401,
+ "PATCH spans | org/basic": 403,
+ "PATCH spans | org/bearer": 401,
+ "PATCH spans | project/basic": 400,
+ "PATCH spans | project/bearer": 403,
+ "PATCH unstable/dashboard-widgets/[widgetId] | admin/admin": 401,
+ "PATCH unstable/dashboard-widgets/[widgetId] | agent/basic": 401,
+ "PATCH unstable/dashboard-widgets/[widgetId] | agent/bearer": 401,
+ "PATCH unstable/dashboard-widgets/[widgetId] | org/basic": 403,
+ "PATCH unstable/dashboard-widgets/[widgetId] | org/bearer": 401,
+ "PATCH unstable/dashboard-widgets/[widgetId] | project/basic": 400,
+ "PATCH unstable/dashboard-widgets/[widgetId] | project/bearer": 403,
+ "PATCH unstable/dashboards/[dashboardId] | admin/admin": 401,
+ "PATCH unstable/dashboards/[dashboardId] | agent/basic": 401,
+ "PATCH unstable/dashboards/[dashboardId] | agent/bearer": 401,
+ "PATCH unstable/dashboards/[dashboardId] | org/basic": 403,
+ "PATCH unstable/dashboards/[dashboardId] | org/bearer": 401,
+ "PATCH unstable/dashboards/[dashboardId] | project/basic": 400,
+ "PATCH unstable/dashboards/[dashboardId] | project/bearer": 403,
+ "PATCH unstable/dashboards/[dashboardId]/placements/[placementId] | admin/admin": 401,
+ "PATCH unstable/dashboards/[dashboardId]/placements/[placementId] | agent/basic": 401,
+ "PATCH unstable/dashboards/[dashboardId]/placements/[placementId] | agent/bearer": 401,
+ "PATCH unstable/dashboards/[dashboardId]/placements/[placementId] | org/basic": 403,
+ "PATCH unstable/dashboards/[dashboardId]/placements/[placementId] | org/bearer": 401,
+ "PATCH unstable/dashboards/[dashboardId]/placements/[placementId] | project/basic": 400,
+ "PATCH unstable/dashboards/[dashboardId]/placements/[placementId] | project/bearer": 403,
+ "PATCH unstable/evaluation-rules/[evaluationRuleId] | admin/admin": 401,
+ "PATCH unstable/evaluation-rules/[evaluationRuleId] | agent/basic": 401,
+ "PATCH unstable/evaluation-rules/[evaluationRuleId] | agent/bearer": 401,
+ "PATCH unstable/evaluation-rules/[evaluationRuleId] | org/basic": 403,
+ "PATCH unstable/evaluation-rules/[evaluationRuleId] | org/bearer": 401,
+ "PATCH unstable/evaluation-rules/[evaluationRuleId] | project/basic": 400,
+ "PATCH unstable/evaluation-rules/[evaluationRuleId] | project/bearer": 403,
+ "PATCH v2/evaluation-rules/[evaluationRuleId] | admin/admin": 401,
+ "PATCH v2/evaluation-rules/[evaluationRuleId] | agent/basic": 401,
+ "PATCH v2/evaluation-rules/[evaluationRuleId] | agent/bearer": 401,
+ "PATCH v2/evaluation-rules/[evaluationRuleId] | org/basic": 403,
+ "PATCH v2/evaluation-rules/[evaluationRuleId] | org/bearer": 401,
+ "PATCH v2/evaluation-rules/[evaluationRuleId] | project/basic": 400,
+ "PATCH v2/evaluation-rules/[evaluationRuleId] | project/bearer": 403,
+ "PATCH v2/evaluators/[evaluatorId] | admin/admin": 401,
+ "PATCH v2/evaluators/[evaluatorId] | agent/basic": 401,
+ "PATCH v2/evaluators/[evaluatorId] | agent/bearer": 401,
+ "PATCH v2/evaluators/[evaluatorId] | org/basic": 403,
+ "PATCH v2/evaluators/[evaluatorId] | org/bearer": 401,
+ "PATCH v2/evaluators/[evaluatorId] | project/basic": 400,
+ "PATCH v2/evaluators/[evaluatorId] | project/bearer": 403,
+ "POST annotation-queues/[queueId]/assignments | admin/admin": 401,
+ "POST annotation-queues/[queueId]/assignments | agent/basic": 401,
+ "POST annotation-queues/[queueId]/assignments | agent/bearer": 401,
+ "POST annotation-queues/[queueId]/assignments | org/basic": 403,
+ "POST annotation-queues/[queueId]/assignments | org/bearer": 401,
+ "POST annotation-queues/[queueId]/assignments | project/basic": 400,
+ "POST annotation-queues/[queueId]/assignments | project/bearer": 403,
+ "POST annotation-queues/[queueId]/items/index | admin/admin": 401,
+ "POST annotation-queues/[queueId]/items/index | agent/basic": 401,
+ "POST annotation-queues/[queueId]/items/index | agent/bearer": 401,
+ "POST annotation-queues/[queueId]/items/index | org/basic": 403,
+ "POST annotation-queues/[queueId]/items/index | org/bearer": 401,
+ "POST annotation-queues/[queueId]/items/index | project/basic": 400,
+ "POST annotation-queues/[queueId]/items/index | project/bearer": 403,
+ "POST annotation-queues/index | admin/admin": 401,
+ "POST annotation-queues/index | agent/basic": 401,
+ "POST annotation-queues/index | agent/bearer": 401,
+ "POST annotation-queues/index | org/basic": 403,
+ "POST annotation-queues/index | org/bearer": 401,
+ "POST annotation-queues/index | project/basic": 400,
+ "POST annotation-queues/index | project/bearer": 403,
+ "POST comments/index | admin/admin": 401,
+ "POST comments/index | agent/basic": 401,
+ "POST comments/index | agent/bearer": 401,
+ "POST comments/index | org/basic": 403,
+ "POST comments/index | org/bearer": 401,
+ "POST comments/index | project/basic": 400,
+ "POST comments/index | project/bearer": 403,
+ "POST dataset-items/index | admin/admin": 401,
+ "POST dataset-items/index | agent/basic": 401,
+ "POST dataset-items/index | agent/bearer": 401,
+ "POST dataset-items/index | org/basic": 403,
+ "POST dataset-items/index | org/bearer": 401,
+ "POST dataset-items/index | project/basic": 400,
+ "POST dataset-items/index | project/bearer": 403,
+ "POST dataset-run-items | admin/admin": 401,
+ "POST dataset-run-items | agent/basic": 401,
+ "POST dataset-run-items | agent/bearer": 401,
+ "POST dataset-run-items | org/basic": 403,
+ "POST dataset-run-items | org/bearer": 401,
+ "POST dataset-run-items | project/basic": 400,
+ "POST dataset-run-items | project/bearer": 403,
+ "POST datasets/index | admin/admin": 401,
+ "POST datasets/index | agent/basic": 401,
+ "POST datasets/index | agent/bearer": 401,
+ "POST datasets/index | org/basic": 403,
+ "POST datasets/index | org/bearer": 401,
+ "POST datasets/index | project/basic": 400,
+ "POST datasets/index | project/bearer": 403,
+ "POST events | admin/admin": 401,
+ "POST events | agent/basic": 401,
+ "POST events | agent/bearer": 401,
+ "POST events | org/basic": 403,
+ "POST events | org/bearer": 401,
+ "POST events | project/basic": 400,
+ "POST events | project/bearer": 403,
+ "POST feedback | admin/admin": 401,
+ "POST feedback | agent/basic": 401,
+ "POST feedback | agent/bearer": 401,
+ "POST feedback | org/basic": 403,
+ "POST feedback | org/bearer": 401,
+ "POST feedback | project/basic": 400,
+ "POST feedback | project/bearer": 403,
+ "POST generations | admin/admin": 401,
+ "POST generations | agent/basic": 401,
+ "POST generations | agent/bearer": 401,
+ "POST generations | org/basic": 403,
+ "POST generations | org/bearer": 401,
+ "POST generations | project/basic": 200,
+ "POST generations | project/bearer": 403,
+ "POST media/index | admin/admin": 401,
+ "POST media/index | agent/basic": 401,
+ "POST media/index | agent/bearer": 401,
+ "POST media/index | org/basic": 403,
+ "POST media/index | org/bearer": 401,
+ "POST media/index | project/basic": 400,
+ "POST media/index | project/bearer": 403,
+ "POST models/index | admin/admin": 403,
+ "POST models/index | agent/basic": 401,
+ "POST models/index | agent/bearer": 401,
+ "POST models/index | org/basic": 403,
+ "POST models/index | org/bearer": 401,
+ "POST models/index | project/basic": 400,
+ "POST models/index | project/bearer": 403,
+ "POST score-configs/index | admin/admin": 401,
+ "POST score-configs/index | agent/basic": 401,
+ "POST score-configs/index | agent/bearer": 401,
+ "POST score-configs/index | org/basic": 403,
+ "POST score-configs/index | org/bearer": 401,
+ "POST score-configs/index | project/basic": 400,
+ "POST score-configs/index | project/bearer": 403,
+ "POST scores/index | admin/admin": 401,
+ "POST scores/index | agent/basic": 401,
+ "POST scores/index | agent/bearer": 401,
+ "POST scores/index | org/basic": 403,
+ "POST scores/index | org/bearer": 401,
+ "POST scores/index | project/basic": 400,
+ "POST scores/index | project/bearer": 400,
+ "POST spans | admin/admin": 401,
+ "POST spans | agent/basic": 401,
+ "POST spans | agent/bearer": 401,
+ "POST spans | org/basic": 403,
+ "POST spans | org/bearer": 401,
+ "POST spans | project/basic": 200,
+ "POST spans | project/bearer": 403,
+ "POST traces/index | admin/admin": 401,
+ "POST traces/index | agent/basic": 401,
+ "POST traces/index | agent/bearer": 401,
+ "POST traces/index | org/basic": 403,
+ "POST traces/index | org/bearer": 401,
+ "POST traces/index | project/basic": 200,
+ "POST traces/index | project/bearer": 403,
+ "POST unstable/dashboard-widgets/index | admin/admin": 401,
+ "POST unstable/dashboard-widgets/index | agent/basic": 401,
+ "POST unstable/dashboard-widgets/index | agent/bearer": 401,
+ "POST unstable/dashboard-widgets/index | org/basic": 403,
+ "POST unstable/dashboard-widgets/index | org/bearer": 401,
+ "POST unstable/dashboard-widgets/index | project/basic": 400,
+ "POST unstable/dashboard-widgets/index | project/bearer": 403,
+ "POST unstable/dashboards/[dashboardId]/placements/index | admin/admin": 401,
+ "POST unstable/dashboards/[dashboardId]/placements/index | agent/basic": 401,
+ "POST unstable/dashboards/[dashboardId]/placements/index | agent/bearer": 401,
+ "POST unstable/dashboards/[dashboardId]/placements/index | org/basic": 403,
+ "POST unstable/dashboards/[dashboardId]/placements/index | org/bearer": 401,
+ "POST unstable/dashboards/[dashboardId]/placements/index | project/basic": 400,
+ "POST unstable/dashboards/[dashboardId]/placements/index | project/bearer": 403,
+ "POST unstable/dashboards/index | admin/admin": 401,
+ "POST unstable/dashboards/index | agent/basic": 401,
+ "POST unstable/dashboards/index | agent/bearer": 401,
+ "POST unstable/dashboards/index | org/basic": 403,
+ "POST unstable/dashboards/index | org/bearer": 401,
+ "POST unstable/dashboards/index | project/basic": 400,
+ "POST unstable/dashboards/index | project/bearer": 403,
+ "POST unstable/evaluation-rules/index | admin/admin": 401,
+ "POST unstable/evaluation-rules/index | agent/basic": 401,
+ "POST unstable/evaluation-rules/index | agent/bearer": 401,
+ "POST unstable/evaluation-rules/index | org/basic": 403,
+ "POST unstable/evaluation-rules/index | org/bearer": 401,
+ "POST unstable/evaluation-rules/index | project/basic": 400,
+ "POST unstable/evaluation-rules/index | project/bearer": 403,
+ "POST unstable/evaluators/index | admin/admin": 401,
+ "POST unstable/evaluators/index | agent/basic": 401,
+ "POST unstable/evaluators/index | agent/bearer": 401,
+ "POST unstable/evaluators/index | org/basic": 403,
+ "POST unstable/evaluators/index | org/bearer": 401,
+ "POST unstable/evaluators/index | project/basic": 400,
+ "POST unstable/evaluators/index | project/bearer": 403,
+ "POST v2/datasets/index | admin/admin": 401,
+ "POST v2/datasets/index | agent/basic": 401,
+ "POST v2/datasets/index | agent/bearer": 401,
+ "POST v2/datasets/index | org/basic": 403,
+ "POST v2/datasets/index | org/bearer": 401,
+ "POST v2/datasets/index | project/basic": 400,
+ "POST v2/datasets/index | project/bearer": 403,
+ "POST v2/evaluation-rules/index | admin/admin": 401,
+ "POST v2/evaluation-rules/index | agent/basic": 401,
+ "POST v2/evaluation-rules/index | agent/bearer": 401,
+ "POST v2/evaluation-rules/index | org/basic": 403,
+ "POST v2/evaluation-rules/index | org/bearer": 401,
+ "POST v2/evaluation-rules/index | project/basic": 400,
+ "POST v2/evaluation-rules/index | project/bearer": 403,
+ "POST v2/evaluators/index | admin/admin": 401,
+ "POST v2/evaluators/index | agent/basic": 401,
+ "POST v2/evaluators/index | agent/bearer": 401,
+ "POST v2/evaluators/index | org/basic": 403,
+ "POST v2/evaluators/index | org/bearer": 401,
+ "POST v2/evaluators/index | project/basic": 400,
+ "POST v2/evaluators/index | project/bearer": 403,
+ "PUT llm-connections/index | admin/admin": 403,
+ "PUT llm-connections/index | agent/basic": 401,
+ "PUT llm-connections/index | agent/bearer": 401,
+ "PUT llm-connections/index | org/basic": 403,
+ "PUT llm-connections/index | org/bearer": 401,
+ "PUT llm-connections/index | project/basic": 400,
+ "PUT llm-connections/index | project/bearer": 403,
+ "PUT models/[modelId]/index | admin/admin": 403,
+ "PUT models/[modelId]/index | agent/basic": 401,
+ "PUT models/[modelId]/index | agent/bearer": 401,
+ "PUT models/[modelId]/index | org/basic": 403,
+ "PUT models/[modelId]/index | org/bearer": 401,
+ "PUT models/[modelId]/index | project/basic": 400,
+ "PUT models/[modelId]/index | project/bearer": 403,
+}
+`;
diff --git a/web/src/__tests__/server/admin-api-key-auth.servertest.ts b/web/src/__tests__/server/admin-api-key-auth.servertest.ts
index d407b8b1f3..354c74e75c 100644
--- a/web/src/__tests__/server/admin-api-key-auth.servertest.ts
+++ b/web/src/__tests__/server/admin-api-key-auth.servertest.ts
@@ -1,5 +1,5 @@
import { type NextApiRequest } from "next";
-import { verifyAuth } from "@/src/features/public-api/server/createAuthedProjectAPIRoute";
+import { verifyAuth } from "@/src/features/public-api/server/verifyProjectApiKeyAuth";
import { prisma } from "@langfuse/shared/src/db";
import { env } from "@/src/env.mjs";
import {
diff --git a/web/src/__tests__/server/apiKeyCacheInvalidation.servertest.ts b/web/src/__tests__/server/apiKeyCacheInvalidation.servertest.ts
new file mode 100644
index 0000000000..ad6a7a9d23
--- /dev/null
+++ b/web/src/__tests__/server/apiKeyCacheInvalidation.servertest.ts
@@ -0,0 +1,274 @@
+import { type Plan, Role } from "@langfuse/shared";
+import { prisma } from "@langfuse/shared/src/db";
+import {
+ createApiKeyCacheKey,
+ createAuthzContextCacheKey,
+ createShaHash,
+} from "@langfuse/shared/src/server";
+import type { Session } from "next-auth";
+import { v4 as uuidv4 } from "uuid";
+import {
+ afterAll,
+ beforeAll,
+ beforeEach,
+ describe,
+ expect,
+ it,
+ vi,
+} from "vitest";
+
+// organizations.delete cancels Stripe before deleting; the test env has a cloud
+// region but no Stripe, so force the self-hosted path to reach the eviction.
+vi.mock("@/src/ee/features/billing/utils/isCloudBilling", () => ({
+ isCloudBillingEnabled: () => false,
+ useIsCloudBillingAvailable: () => false,
+}));
+
+import { appRouter } from "@/src/server/api/root";
+import { createInnerTRPCContext } from "@/src/server/api/trpc";
+import { env } from "@/src/env.mjs";
+import { handleDeleteProject } from "@/src/ee/features/admin-api/server/projects/projectById";
+import { handleDeleteOrganization } from "@/src/ee/features/admin-api/server/organizations/organizationById";
+import {
+ createRedisTestClient,
+ ensureRedisReady,
+ getRedisKeysByPattern,
+ setRedisValue,
+ type RedisTestClient,
+} from "@/src/__tests__/server/redis-test-utils";
+
+let redis: RedisTestClient;
+
+beforeAll(async () => {
+ redis = createRedisTestClient();
+ await ensureRedisReady(redis);
+});
+
+afterAll(() => {
+ redis.disconnect();
+});
+
+async function createOrg() {
+ const orgId = uuidv4();
+ await prisma.organization.create({
+ data: { id: orgId, name: `Org ${orgId.slice(0, 8)}` },
+ });
+ return orgId;
+}
+
+async function createProject(orgId: string) {
+ const projectId = uuidv4();
+ await prisma.project.create({
+ data: { id: projectId, name: `Project ${projectId.slice(0, 8)}`, orgId },
+ });
+ return projectId;
+}
+
+function cacheKeysFor({
+ fastHash,
+ publicKey,
+}: {
+ fastHash: string;
+ publicKey: string;
+}) {
+ return [
+ createApiKeyCacheKey(fastHash),
+ createAuthzContextCacheKey(fastHash),
+ createAuthzContextCacheKey(createShaHash(publicKey, env.SALT!)),
+ ];
+}
+
+async function seedOrgScopedKey(orgId: string) {
+ const suffix = uuidv4().slice(0, 8);
+ const fastHash = `fh-${suffix}`;
+ const publicKey = `pk-lf-${suffix}`;
+ await prisma.apiKey.create({
+ data: {
+ orgId,
+ scope: "ORGANIZATION",
+ publicKey,
+ hashedSecretKey: `hsk-${suffix}`,
+ fastHashedSecretKey: fastHash,
+ displaySecretKey: `sk-...${suffix}`,
+ },
+ });
+ const keys = cacheKeysFor({ fastHash, publicKey });
+ for (const key of keys) {
+ await setRedisValue(redis, key, "1");
+ }
+ return keys;
+}
+
+async function survivingKeys(keys: string[]) {
+ const present: string[] = [];
+ for (const key of keys) {
+ const [hit] = await getRedisKeysByPattern(redis, key);
+ if (hit) present.push(hit);
+ }
+ return present;
+}
+
+async function createUserInOrgs(orgIds: string[]) {
+ const userId = uuidv4();
+ const user = await prisma.user.create({
+ data: {
+ id: userId,
+ email: `cache-inval-${userId.slice(0, 8)}@test.com`,
+ name: "Test User",
+ },
+ });
+ for (const orgId of orgIds) {
+ await prisma.organizationMembership.create({
+ data: { userId: user.id, orgId, role: Role.OWNER },
+ });
+ }
+ return user;
+}
+
+function makeCaller({
+ userId,
+ orgIds,
+ projectId,
+ plan = "cloud:pro",
+}: {
+ userId: string;
+ orgIds: string[];
+ projectId?: string;
+ plan?: Plan;
+}) {
+ const session: Session = {
+ expires: "1",
+ user: {
+ id: userId,
+ canCreateOrganizations: true,
+ name: "Test User",
+ email: "[email protected]",
+ organizations: orgIds.map((orgId) => ({
+ id: orgId,
+ name: "Test Organization",
+ role: Role.OWNER,
+ plan,
+ cloudConfig: undefined,
+ metadata: {},
+ aiFeaturesEnabled: false,
+ aiTelemetryEnabled: true,
+ projects: projectId
+ ? [
+ {
+ id: projectId,
+ role: Role.OWNER,
+ retentionDays: 0,
+ deletedAt: null,
+ hasTraces: false,
+ name: "Test Project",
+ metadata: {},
+ createdAt: new Date().toISOString(),
+ },
+ ]
+ : [],
+ })),
+ featureFlags: {
+ searchBar: false,
+ excludeClickhouseRead: false,
+ templateFlag: true,
+ v4BetaToggleVisible: false,
+ observationEvals: false,
+ experimentsV4Enabled: false,
+ },
+ admin: false,
+ },
+ environment: {} as any,
+ };
+ const ctx = createInnerTRPCContext({ session, headers: {} });
+ return appRouter.createCaller({ ...ctx, prisma, session });
+}
+
+function makeRes() {
+ const res: any = {};
+ res.statusCode = 200;
+ res.status = (code: number) => {
+ res.statusCode = code;
+ return res;
+ };
+ res.json = (body: unknown) => {
+ res.body = body;
+ return res;
+ };
+ return res;
+}
+
+describe("API-key cache invalidation on project/org lifecycle", () => {
+ beforeEach(async () => {
+ await ensureRedisReady(redis);
+ });
+
+ it("projects.delete evicts the org's cached keys", async () => {
+ const orgId = await createOrg();
+ const projectId = await createProject(orgId);
+ const user = await createUserInOrgs([orgId]);
+ const keys = await seedOrgScopedKey(orgId);
+
+ const caller = makeCaller({ userId: user.id, orgIds: [orgId], projectId });
+ await caller.projects.delete({ projectId });
+
+ expect(await survivingKeys(keys)).toEqual([]);
+ });
+
+ it("projects.transfer evicts cached keys for both source and target orgs", async () => {
+ const sourceOrgId = await createOrg();
+ const targetOrgId = await createOrg();
+ const projectId = await createProject(sourceOrgId);
+ const user = await createUserInOrgs([sourceOrgId, targetOrgId]);
+ const sourceKeys = await seedOrgScopedKey(sourceOrgId);
+ const targetKeys = await seedOrgScopedKey(targetOrgId);
+
+ const caller = makeCaller({
+ userId: user.id,
+ orgIds: [sourceOrgId, targetOrgId],
+ projectId,
+ });
+ await caller.projects.transfer({ projectId, targetOrgId });
+
+ expect(await survivingKeys([...sourceKeys, ...targetKeys])).toEqual([]);
+ });
+
+ it("organizations.delete evicts cached keys before the cascade removes the rows", async () => {
+ const orgId = await createOrg();
+ const user = await createUserInOrgs([orgId]);
+ const keys = await seedOrgScopedKey(orgId);
+
+ const caller = makeCaller({ userId: user.id, orgIds: [orgId] });
+ await caller.organizations.delete({ orgId });
+
+ expect(await survivingKeys(keys)).toEqual([]);
+ });
+
+ it("admin handleDeleteProject evicts the org's cached keys", async () => {
+ const orgId = await createOrg();
+ const projectId = await createProject(orgId);
+ const keys = await seedOrgScopedKey(orgId);
+
+ const res = makeRes();
+ await handleDeleteProject({} as any, res, projectId, {
+ orgId,
+ apiKeyId: "ADMIN_KEY",
+ } as any);
+
+ expect(res.statusCode).toBe(202);
+ expect(await survivingKeys(keys)).toEqual([]);
+ });
+
+ it("admin handleDeleteOrganization evicts cached keys before the cascade removes the rows", async () => {
+ const orgId = await createOrg();
+ const keys = await seedOrgScopedKey(orgId);
+
+ const res = makeRes();
+ await handleDeleteOrganization(
+ { query: { organizationId: orgId } } as any,
+ res,
+ );
+
+ expect(res.statusCode).toBe(200);
+ expect(await survivingKeys(keys)).toEqual([]);
+ });
+});
diff --git a/web/src/__tests__/server/batchExport-trpc.servertest.ts b/web/src/__tests__/server/batchExport-trpc.servertest.ts
index 5b536dfa90..d0c64c6fe8 100644
--- a/web/src/__tests__/server/batchExport-trpc.servertest.ts
+++ b/web/src/__tests__/server/batchExport-trpc.servertest.ts
@@ -94,7 +94,7 @@ describe("batchExport tRPC – audit_logs table authorization", () => {
});
});
- it("blocks a MEMBER (no auditLogs:read) from exporting audit_logs even on cloud:team plan", async () => {
+ it("blocks a MEMBER (no projectAuditLogs:read) from exporting audit_logs even on cloud:team plan", async () => {
const { project, org } = await createOrgProjectAndApiKey();
__orgIds.push(org.id);
@@ -139,7 +139,7 @@ describe("batchExport tRPC – audit_logs table authorization", () => {
["cloud:team", "ADMIN"],
["self-hosted:enterprise", "OWNER"],
])(
- "allows plan=%s role=%s (both entitlement and auditLogs:read) to export audit_logs",
+ "allows plan=%s role=%s (both entitlement and projectAuditLogs:read) to export audit_logs",
async (plan, projectRole) => {
const { project, org } = await createOrgProjectAndApiKey();
__orgIds.push(org.id);
@@ -314,7 +314,7 @@ describe("batchExport tRPC – audit_logs download and list authorization", () =
).resolves.toEqual({ url: "not-a-valid-url" });
});
- it("hides audit_logs exports from all for a MEMBER without auditLogs:read", async () => {
+ it("hides audit_logs exports from all for a MEMBER without projectAuditLogs:read", async () => {
const { project, org } = await createOrgProjectAndApiKey();
__orgIds.push(org.id);
diff --git a/web/src/__tests__/server/in-app-agent-api-route-auth.servertest.ts b/web/src/__tests__/server/in-app-agent-api-route-auth.servertest.ts
index d5ed98fb14..5ec5523d74 100644
--- a/web/src/__tests__/server/in-app-agent-api-route-auth.servertest.ts
+++ b/web/src/__tests__/server/in-app-agent-api-route-auth.servertest.ts
@@ -54,6 +54,7 @@ describe("in-app agent public API route auth", () => {
async function callRoute(params: { allowInAppAgentKey?: boolean }) {
const handler = createAuthedProjectAPIRoute({
name: "Test Route",
+ action: "project:read",
...(params.allowInAppAgentKey === undefined
? {}
: { allowInAppAgentKey: params.allowInAppAgentKey }),
diff --git a/web/src/__tests__/server/policy-auth.servertest.ts b/web/src/__tests__/server/policy-auth.servertest.ts
new file mode 100644
index 0000000000..df9da63395
--- /dev/null
+++ b/web/src/__tests__/server/policy-auth.servertest.ts
@@ -0,0 +1,86 @@
+import {
+ createBasicAuthHeader,
+ createOrgProjectAndApiKey,
+ hashSecretKey,
+} from "@langfuse/shared/src/server";
+import { prisma } from "@langfuse/shared/src/db";
+
+import { Authenticator } from "@/src/features/apiKey/authenticator";
+import { authorize } from "@/src/features/auth/policy/authorize";
+import { ApiAuthService } from "@/src/features/public-api/server/apiAuth";
+
+describe("policy authenticate() composition", () => {
+ let fixture: Awaited<ReturnType<typeof createOrgProjectAndApiKey>>;
+
+ const basicHeader = () =>
+ createBasicAuthHeader(fixture.publicKey, fixture.secretKey);
+
+ const backfillFastHash = async () => {
+ await new ApiAuthService(prisma, null).verifyAuthHeaderAndReturnScope(
+ basicHeader(),
+ );
+ };
+
+ const auth = (authorization: string) =>
+ new Authenticator().authenticate({ headers: { authorization } });
+
+ beforeEach(async () => {
+ fixture = await createOrgProjectAndApiKey({ plan: "Hobby" });
+ });
+
+ it("resolves a project context from Basic auth", async () => {
+ const result = await auth(basicHeader());
+ expect(result.success).toBe(true);
+ if (!result.success) return;
+ expect(result.context.principal.kind).toBe("apiKey");
+ expect(
+ authorize(result.context, "traces:read", {
+ projectId: fixture.projectId,
+ }).success,
+ ).toBe(true);
+ });
+
+ it("authenticates a private key over Bearer with full project scope", async () => {
+ await backfillFastHash();
+ const result = await auth(`Bearer ${fixture.secretKey}`);
+ expect(result.success).toBe(true);
+ if (!result.success) return;
+ expect(
+ authorize(result.context, "traces:read", {
+ projectId: fixture.projectId,
+ }).success,
+ ).toBe(true);
+ });
+
+ it("authenticates a public key over Bearer with scores-only scope", async () => {
+ const result = await auth(`Bearer ${fixture.publicKey}`);
+ expect(result.success).toBe(true);
+ if (!result.success) return;
+ expect(
+ authorize(result.context, "scores:create", {
+ projectId: fixture.projectId,
+ }).success,
+ ).toBe(true);
+ expect(
+ authorize(result.context, "traces:read", {
+ projectId: fixture.projectId,
+ }).success,
+ ).toBe(false);
+ });
+
+ it("401s an unknown token", async () => {
+ const result = await auth("Bearer not-a-real-key");
+ expect(result.success).toBe(false);
+ });
+
+ it("authenticates a NULL fast-hash key over Basic via the slow-hash path", async () => {
+ await prisma.apiKey.update({
+ where: { publicKey: fixture.publicKey },
+ data: {
+ hashedSecretKey: await hashSecretKey(fixture.secretKey),
+ fastHashedSecretKey: null,
+ },
+ });
+ expect((await auth(basicHeader())).success).toBe(true);
+ });
+});
diff --git a/web/src/__tests__/server/project-route-auth-enforce-scope.servertest.ts b/web/src/__tests__/server/project-route-auth-enforce-scope.servertest.ts
new file mode 100644
index 0000000000..934f9774e6
--- /dev/null
+++ b/web/src/__tests__/server/project-route-auth-enforce-scope.servertest.ts
@@ -0,0 +1,138 @@
+import { type NextApiRequest } from "next";
+
+import {
+ createBasicAuthHeader,
+ createOrgProjectAndApiKey,
+} from "@langfuse/shared/src/server";
+
+import { env } from "@/src/env.mjs";
+import { type VerifyAuthParams } from "@/src/features/public-api/server/createAuthedProjectAPIRoute";
+
+// Proves the enforce mapper returns a scope byte-identical to legacy's across
+// credential kinds. The parity matrix asserts status; this asserts scope
+// fields. verifyAuth is imported dynamically so the authenticator singleton
+// captures the admin key set in beforeAll.
+
+type VerifyAuth = (
+ params: VerifyAuthParams,
+) => Promise<{ validKey: true; scope: Record<string, unknown> }>;
+
+const adminApiKey = "test-admin-api-key-enforce-scope";
+
+let verifyAuth: VerifyAuth;
+let projectId = "";
+let publicKey = "";
+let secretKey = "";
+
+let originalMigration: string | undefined;
+let originalAdminApiKey: string | undefined;
+let originalCloudRegion: string | undefined;
+
+const reqWith = (headers: Record<string, string | undefined>): NextApiRequest =>
+ ({ headers, method: "GET" }) as unknown as NextApiRequest;
+
+const setMode = (mode: string) => {
+ (env as any).API_AUTH_MIGRATION = mode;
+};
+
+const dropScopeKey = ({
+ scope: _apiKeyScope,
+ ...rest
+}: Record<string, unknown>) => rest;
+
+const scopeUnderModes = async (params: VerifyAuthParams) => {
+ setMode("legacy");
+ const legacy = await verifyAuth(params);
+ setMode("enforce");
+ const enforce = await verifyAuth(params);
+ return { legacy: legacy.scope, enforce: enforce.scope };
+};
+
+describe("enforce maps principals to legacy-identical scopes", () => {
+ beforeAll(async () => {
+ originalMigration = (env as any).API_AUTH_MIGRATION;
+ originalAdminApiKey = (env as any).ADMIN_API_KEY;
+ originalCloudRegion = (env as any).NEXT_PUBLIC_LANGFUSE_CLOUD_REGION;
+ (env as any).ADMIN_API_KEY = adminApiKey;
+
+ ({ verifyAuth } =
+ (await import("@/src/features/public-api/server/createAuthedProjectAPIRoute")) as unknown as {
+ verifyAuth: VerifyAuth;
+ });
+
+ const base = await createOrgProjectAndApiKey();
+ projectId = base.projectId;
+ publicKey = base.publicKey;
+ secretKey = base.secretKey;
+ });
+
+ afterAll(() => {
+ (env as any).API_AUTH_MIGRATION = originalMigration;
+ (env as any).ADMIN_API_KEY = originalAdminApiKey;
+ (env as any).NEXT_PUBLIC_LANGFUSE_CLOUD_REGION = originalCloudRegion;
+ });
+
+ it("private-key basic yields a legacy-identical project scope", async () => {
+ const { legacy, enforce } = await scopeUnderModes({
+ req: reqWith({
+ authorization: createBasicAuthHeader(publicKey, secretKey),
+ }),
+ name: "Get Traces",
+ action: "traces:read",
+ allowedAccessLevels: ["project"],
+ });
+ expect(enforce.accessLevel).toBe("project");
+ expect(enforce.projectId).toBe(projectId);
+ expect(dropScopeKey(enforce)).toEqual(dropScopeKey(legacy));
+ });
+
+ it("public-key bearer on a score-ingest route yields the scores access level", async () => {
+ const { legacy, enforce } = await scopeUnderModes({
+ req: reqWith({ authorization: `Bearer ${publicKey}` }),
+ name: "Create Score",
+ action: "scores:create",
+ allowedAccessLevels: ["project", "scores"],
+ });
+ expect(enforce.accessLevel).toBe("scores");
+ expect(dropScopeKey(enforce)).toEqual(dropScopeKey(legacy));
+ });
+
+ it("admin key on self-host yields the synthesized admin scope", async () => {
+ (env as any).NEXT_PUBLIC_LANGFUSE_CLOUD_REGION = undefined;
+ const { legacy, enforce } = await scopeUnderModes({
+ req: reqWith({
+ authorization: `Bearer ${adminApiKey}`,
+ "x-langfuse-admin-api-key": adminApiKey,
+ "x-langfuse-project-id": projectId,
+ }),
+ name: "Get Models",
+ action: "models:read",
+ isAdminApiKeyAuthAllowed: true,
+ });
+ expect(enforce.apiKeyId).toBe("ADMIN_API_KEY");
+ expect(enforce.projectId).toBe(projectId);
+ expect(dropScopeKey(enforce)).toEqual(dropScopeKey(legacy));
+ });
+
+ it("admin key on Langfuse Cloud is refused in legacy and enforce", async () => {
+ (env as any).NEXT_PUBLIC_LANGFUSE_CLOUD_REGION = "us";
+ const params: VerifyAuthParams = {
+ req: reqWith({
+ authorization: `Bearer ${adminApiKey}`,
+ "x-langfuse-admin-api-key": adminApiKey,
+ "x-langfuse-project-id": projectId,
+ }),
+ name: "Get Models",
+ action: "models:read",
+ isAdminApiKeyAuthAllowed: true,
+ };
+ const cloudDenial = {
+ status: 403,
+ message: "Admin API key auth is not available on Langfuse Cloud",
+ };
+ setMode("legacy");
+ await expect(verifyAuth(params)).rejects.toEqual(cloudDenial);
+ setMode("enforce");
+ await expect(verifyAuth(params)).rejects.toEqual(cloudDenial);
+ });
+});
diff --git a/web/src/__tests__/server/project-route-auth-parity.servertest.ts b/web/src/__tests__/server/project-route-auth-parity.servertest.ts
new file mode 100644
index 0000000000..b92cc1dc03
--- /dev/null
+++ b/web/src/__tests__/server/project-route-auth-parity.servertest.ts
@@ -0,0 +1,351 @@
+import { readdirSync } from "node:fs";
+import path from "node:path";
+
+import type { NextApiRequest, NextApiResponse } from "next";
+import { createMocks } from "node-mocks-http";
+
+import { env } from "@/src/env.mjs";
+import { prisma } from "@langfuse/shared/src/db";
+import {
+ createAndAddApiKeysToDb,
+ createBasicAuthHeader,
+ createOrgProjectAndApiKey,
+} from "@langfuse/shared/src/server";
+
+// Pins the project-route authorization seam to legacy: sweeps every project
+// public-API route across migration modes and key kinds, recording each cell's
+// status. Shadow/enforce must equal legacy (cross-mode); a main-captured
+// snapshot pins legacy across the refactor (cross-branch). Value is status only.
+
+type HttpMethod = "GET" | "POST" | "PUT" | "PATCH" | "DELETE";
+
+type Route = { route: string; methods: HttpMethod[] };
+
+// Project routes under test, grouped by domain. `route` is the path under
+// pages/api/public/ (import path + completeness key); `methods` are its real
+// supported methods.
+const projectRoutes: Route[] = [
+ { route: "traces/index", methods: ["GET", "POST", "DELETE"] },
+ { route: "traces/[traceId]", methods: ["GET", "DELETE"] },
+ { route: "observations/index", methods: ["GET"] },
+ { route: "observations/[observationId]", methods: ["GET"] },
+ { route: "v2/observations/index", methods: ["GET"] },
+ { route: "sessions/index", methods: ["GET"] },
+ { route: "sessions/[sessionId]", methods: ["GET"] },
+ { route: "scores/index", methods: ["GET", "POST"] },
+ { route: "scores/[scoreId]", methods: ["GET", "DELETE"] },
+ { route: "v2/scores/index", methods: ["GET"] },
+ { route: "v2/scores/[scoreId]", methods: ["GET"] },
+ { route: "v3/scores/index", methods: ["GET"] },
+ { route: "score-configs/index", methods: ["GET", "POST"] },
+ { route: "score-configs/[configId]", methods: ["GET", "PATCH"] },
+ { route: "comments/index", methods: ["GET", "POST"] },
+ { route: "comments/[commentId]", methods: ["GET"] },
+ { route: "annotation-queues/index", methods: ["GET", "POST"] },
+ { route: "annotation-queues/[queueId]/index", methods: ["GET"] },
+ {
+ route: "annotation-queues/[queueId]/assignments",
+ methods: ["POST", "DELETE"],
+ },
+ {
+ route: "annotation-queues/[queueId]/items/index",
+ methods: ["GET", "POST"],
+ },
+ {
+ route: "annotation-queues/[queueId]/items/[itemId]",
+ methods: ["GET", "PATCH", "DELETE"],
+ },
+ { route: "datasets/index", methods: ["GET", "POST"] },
+ { route: "datasets/[name]/index", methods: ["GET"] },
+ { route: "datasets/[name]/runs/index", methods: ["GET"] },
+ { route: "datasets/[name]/runs/[runName]", methods: ["GET", "DELETE"] },
+ { route: "v2/datasets/index", methods: ["GET", "POST"] },
+ { route: "v2/datasets/[datasetName]/index", methods: ["GET"] },
+ { route: "dataset-items/index", methods: ["GET", "POST"] },
+ { route: "dataset-items/[datasetItemId]", methods: ["GET", "DELETE"] },
+ { route: "dataset-run-items", methods: ["GET", "POST"] },
+ { route: "experiments/index", methods: ["GET"] },
+ { route: "experiment-items/index", methods: ["GET"] },
+ { route: "models/index", methods: ["GET", "POST"] },
+ { route: "models/[modelId]/index", methods: ["GET", "PUT", "DELETE"] },
+ { route: "media/index", methods: ["POST"] },
+ { route: "media/[mediaId]", methods: ["GET", "PATCH"] },
+ { route: "metrics/index", methods: ["GET"] },
+ { route: "metrics/daily", methods: ["GET"] },
+ { route: "v2/metrics", methods: ["GET"] },
+ { route: "events", methods: ["POST"] },
+ { route: "feedback", methods: ["POST"] },
+ { route: "generations", methods: ["POST", "PATCH"] },
+ { route: "spans", methods: ["POST", "PATCH"] },
+ { route: "llm-connections/index", methods: ["GET", "PUT"] },
+ { route: "llm-connections/[id]", methods: ["DELETE"] },
+ { route: "unstable/dashboards/index", methods: ["GET", "POST"] },
+ {
+ route: "unstable/dashboards/[dashboardId]",
+ methods: ["GET", "PATCH", "DELETE"],
+ },
+ {
+ route: "unstable/dashboards/[dashboardId]/placements/index",
+ methods: ["POST"],
+ },
+ {
+ route: "unstable/dashboards/[dashboardId]/placements/[placementId]",
+ methods: ["PATCH", "DELETE"],
+ },
+ { route: "unstable/dashboard-widgets/index", methods: ["GET", "POST"] },
+ {
+ route: "unstable/dashboard-widgets/[widgetId]",
+ methods: ["GET", "PATCH", "DELETE"],
+ },
+ { route: "unstable/evaluators/index", methods: ["GET", "POST"] },
+ { route: "unstable/evaluators/[evaluatorId]", methods: ["GET", "DELETE"] },
+ { route: "unstable/evaluation-rules/index", methods: ["GET", "POST"] },
+ {
+ route: "unstable/evaluation-rules/[evaluationRuleId]",
+ methods: ["GET", "PATCH", "DELETE"],
+ },
+ { route: "v2/evaluators/index", methods: ["GET", "POST"] },
+ {
+ route: "v2/evaluators/[evaluatorId]",
+ methods: ["GET", "PATCH", "DELETE"],
+ },
+ { route: "v2/evaluators/[evaluatorId]/versions", methods: ["GET"] },
+ { route: "v2/evaluation-rules/index", methods: ["GET", "POST"] },
+ {
+ route: "v2/evaluation-rules/[evaluationRuleId]",
+ methods: ["GET", "PATCH", "DELETE"],
+ },
+];
+
+// Non-project public files, subtracted from the filesystem walk before the
+// completeness guard compares it to the table.
+const denylistPrefixes = [
+ "health", // liveness probe
+ "ready", // readiness probe
+ "ingestion", // batch ingestion, own auth path
+ "prompts", // prompt handlers, own auth path
+ "v2/prompts", // prompt list/name handlers, own auth path
+ "mcp", // MCP server, own auth path
+ "otel", // ingestion handlers read the raw request stream, not drivable via node-mocks-http
+ "integrations", // blob-storage integration, own auth path
+ "organizations", // organization-scoped, not project seam
+ "projects", // organization-scoped, not project seam
+ "scim", // SCIM, own auth path
+ "slack", // Slack OAuth, own auth path
+];
+
+type ApiKeyKind = "org" | "project" | "agent" | "admin";
+const apiKeyKinds: ApiKeyKind[] = ["org", "project", "agent", "admin"];
+
+type MigrationMode = "legacy" | "shadow" | "enforce";
+
+const adminApiKey = "test-admin-api-key-project-route-parity";
+// Valid-shaped id that does not exist, so a good key lands past auth (404/400).
+const nonexistentId = "00000000-0000-0000-0000-000000000000";
+
+type KeyPair = { publicKey: string; secretKey: string };
+
+const keys: Record<Exclude<ApiKeyKind, "admin">, KeyPair> = {
+ org: { publicKey: "", secretKey: "" },
+ project: { publicKey: "", secretKey: "" },
+ agent: { publicKey: "", secretKey: "" },
+};
+let fixtureProjectId = "";
+
+let originalMigration: string | undefined;
+let originalAdminApiKey: string | undefined;
+let matrices: Record<MigrationMode, Record<string, number>>;
+
+/** queryForRoute maps each [param] path segment to a fixed nonexistent id, as Next populates req.query. */
+function queryForRoute(route: string): Record<string, string> {
+ const query: Record<string, string> = {};
+ for (const match of route.matchAll(/\[([^\]]+)\]/g)) {
+ query[match[1]!] = nonexistentId;
+ }
+ return query;
+}
+
+/** getHeaders derives a key kind's header sets: org/project/agent send basic + bearer, admin sends its single triple. */
+function getHeaders(
+ apiKeyKind: ApiKeyKind,
+): { headerKind: string; headers: Record<string, string> }[] {
+ if (apiKeyKind === "admin") {
+ return [
+ {
+ headerKind: "admin",
+ headers: {
+ authorization: `Bearer ${adminApiKey}`,
+ "x-langfuse-admin-api-key": adminApiKey,
+ "x-langfuse-project-id": fixtureProjectId,
+ },
+ },
+ ];
+ }
+ const { publicKey, secretKey } = keys[apiKeyKind];
+ // An org key has no bound project, so a realistic org request names its target
+ // via header; project/agent keys resolve their project from the key itself.
+ const target =
+ apiKeyKind === "org"
+ ? { "x-langfuse-project-id": fixtureProjectId }
+ : undefined;
+ return [
+ {
+ headerKind: "basic",
+ headers: {
+ authorization: createBasicAuthHeader(publicKey, secretKey),
+ ...target,
+ },
+ },
+ {
+ headerKind: "bearer",
+ headers: { authorization: `Bearer ${publicKey}`, ...target },
+ },
+ ];
+}
+
+/** callRoute imports the route by convention and invokes its handler with a mocked request, returning the status code. */
+async function callRoute(
+ route: string,
+ method: HttpMethod,
+ headers: Record<string, string>,
+): Promise<number> {
+ const mod = await import(
+ /* @vite-ignore */ `@/src/pages/api/public/${route}`
+ );
+ const handler = mod.default as (
+ req: NextApiRequest,
+ res: NextApiResponse,
+ ) => Promise<void>;
+ const { req, res } = createMocks<NextApiRequest, NextApiResponse>({
+ method,
+ query: queryForRoute(route),
+ body: method === "GET" ? undefined : {},
+ headers,
+ });
+ await handler(req, res);
+ return res._getStatusCode();
+}
+
+type Cell = { key: string; run: () => Promise<number> };
+
+/** matrixCells lists every route × method × key kind × header kind cell in source order. */
+function matrixCells(): Cell[] {
+ const cells: Cell[] = [];
+ for (const { route, methods } of projectRoutes) {
+ for (const method of methods) {
+ for (const apiKeyKind of apiKeyKinds) {
+ for (const { headerKind, headers } of getHeaders(apiKeyKind)) {
+ cells.push({
+ key: `${method} ${route} | ${apiKeyKind}/${headerKind}`,
+ run: () => callRoute(route, method, headers),
+ });
+ }
+ }
+ }
+ }
+ return cells;
+}
+
+/** runMatrix runs every cell in one mode with bounded concurrency, returning a flat status-by-cell map in source order. */
+async function runMatrix(mode: MigrationMode): Promise<Record<string, number>> {
+ (env as any).API_AUTH_MIGRATION = mode;
+ const cells = matrixCells();
+ const statuses = new Array<number>(cells.length);
+ let next = 0;
+ const worker = async () => {
+ while (next < cells.length) {
+ const index = next++;
+ statuses[index] = await cells[index]!.run();
+ }
+ };
+ await Promise.all(Array.from({ length: 16 }, worker));
+ const result: Record<string, number> = {};
+ cells.forEach((cell, index) => {
+ result[cell.key] = statuses[index]!;
+ });
+ return result;
+}
+
+/** walkRoutes lists every route file under pages/api/public/ as a table-style route key. */
+function walkRoutes(): string[] {
+ const base = path.resolve(process.cwd(), "src/pages/api/public");
+ const routes: string[] = [];
+ const walk = (dir: string) => {
+ for (const entry of readdirSync(dir, { withFileTypes: true })) {
+ const full = path.join(dir, entry.name);
+ if (entry.isDirectory()) {
+ walk(full);
+ } else if (entry.name.endsWith(".ts")) {
+ routes.push(
+ path
+ .relative(base, full)
+ .replace(/\.ts$/, "")
+ .split(path.sep)
+ .join("/"),
+ );
+ }
+ }
+ };
+ walk(base);
+ return routes;
+}
+
+describe("project-route auth parity", () => {
+ beforeAll(async () => {
+ originalMigration = (env as any).API_AUTH_MIGRATION;
+ originalAdminApiKey = (env as any).ADMIN_API_KEY;
+ (env as any).ADMIN_API_KEY = adminApiKey;
+
+ const base = await createOrgProjectAndApiKey();
+ fixtureProjectId = base.projectId;
+ keys.project = { publicKey: base.publicKey, secretKey: base.secretKey };
+
+ const org = await createAndAddApiKeysToDb({
+ prisma,
+ entityId: base.orgId,
+ scope: "ORGANIZATION",
+ });
+ keys.org = { publicKey: org.publicKey, secretKey: org.secretKey };
+
+ const agent = await createAndAddApiKeysToDb({
+ prisma,
+ entityId: base.projectId,
+ scope: "PROJECT",
+ isInAppAgentKey: true,
+ });
+ keys.agent = { publicKey: agent.publicKey, secretKey: agent.secretKey };
+
+ matrices = {
+ legacy: await runMatrix("legacy"),
+ shadow: await runMatrix("shadow"),
+ enforce: await runMatrix("enforce"),
+ };
+ }, 120_000);
+
+ afterAll(() => {
+ (env as any).API_AUTH_MIGRATION = originalMigration;
+ (env as any).ADMIN_API_KEY = originalAdminApiKey;
+ });
+
+ it("legacy matches the main-captured baseline", () => {
+ expect(matrices.legacy).toMatchSnapshot();
+ });
+
+ it("shadow is byte-identical to legacy", () => {
+ expect(matrices.shadow).toEqual(matrices.legacy);
+ });
+
+ it("enforce is byte-identical to legacy", () => {
+ expect(matrices.enforce).toEqual(matrices.legacy);
+ });
+
+ it("covers every project route", () => {
+ const denied = (route: string) =>
+ denylistPrefixes.some(
+ (prefix) => route === prefix || route.startsWith(`${prefix}/`),
+ );
+ const walked = new Set(walkRoutes().filter((route) => !denied(route)));
+ const tabled = new Set(projectRoutes.map((r) => r.route));
+ expect(walked).toEqual(tabled);
+ });
+});
diff --git a/web/src/__tests__/server/unit/apiKey/authenticator.servertest.ts b/web/src/__tests__/server/unit/apiKey/authenticator.servertest.ts
new file mode 100644
index 0000000000..d032444169
--- /dev/null
+++ b/web/src/__tests__/server/unit/apiKey/authenticator.servertest.ts
@@ -0,0 +1,323 @@
+import { type Redis } from "ioredis";
+import { beforeEach, describe, expect, it, vi } from "vitest";
+
+import { type ApiKey, type PrismaClient } from "@langfuse/shared/src/db";
+import { InternalServerError, UnauthorizedError } from "@langfuse/shared";
+import {
+ AUTHZ_CONTEXT_CACHE_KEY_PREFIX,
+ API_KEY_CACHE_KEY_PREFIX,
+ createShaHash,
+} from "@langfuse/shared/src/server";
+
+import { Authenticator } from "@/src/features/apiKey/authenticator";
+import { AuthenticatorCache } from "@/src/features/apiKey/authenticatorCache";
+import {
+ OrganizationRepository,
+ type OrganizationWithProjects,
+} from "@/src/features/auth/policy/organizationRepository";
+import { ContextResolver } from "@/src/features/auth/policy/contextResolver";
+import { Verifier } from "@/src/features/apiKey/verifier";
+import { type ApiKeyRepository } from "@/src/features/apiKey/apiKeyRepository";
+
+const SALT = "test-salt";
+const ORG = "org_1";
+const PRJ = "prj_1";
+const KNOWN_SECRET = "sk-lf-known";
+const UNKNOWN_SECRET = "sk-lf-unknown";
+
+const knownHash = createShaHash(KNOWN_SECRET, SALT);
+
+const apiKey = (over: Partial<ApiKey> = {}): ApiKey => ({
+ id: "key_p",
+ createdAt: new Date(0),
+ note: null,
+ publicKey: "pk-lf-1",
+ hashedSecretKey: "hsk",
+ fastHashedSecretKey: knownHash,
+ displaySecretKey: "sk-...abc",
+ lastUsedAt: null,
+ expiresAt: null,
+ isInAppAgentKey: false,
+ projectId: PRJ,
+ orgId: ORG,
+ scope: "PROJECT",
+ createdByUserId: "user_1",
+ createdByApiKeyId: null,
+ ...over,
+});
+
+/** store resolves the known secret via the fast-hash index and misses everything else. */
+const store = (key: ApiKey): ApiKeyRepository =>
+ ({
+ findByFastHash: async (hash: string) => ({
+ success: true,
+ apiKey: hash === key.fastHashedSecretKey ? key : null,
+ }),
+ findByPublicKey: async () => ({ success: true, apiKey: null }),
+ verifySlow: async () => ({ success: true, valid: false }),
+ backfillFastHash: async () => {},
+ }) as unknown as ApiKeyRepository;
+
+const orgRow = {
+ id: ORG,
+ cloudConfig: null,
+ cloudFreeTierUsageThresholdState: null,
+ projects: [{ id: PRJ }],
+} as unknown as OrganizationWithProjects;
+
+const resolver = new ContextResolver(
+ new OrganizationRepository({
+ organization: {
+ findUnique: async () => orgRow,
+ findFirst: async () => orgRow,
+ },
+ } as unknown as PrismaClient),
+);
+
+/** fakeRedis is an in-memory get/set store; overrides let a test make either op throw. */
+function fakeRedis(
+ overrides: Partial<{
+ get: Redis["get"];
+ set: Redis["set"];
+ }> = {},
+) {
+ const map = new Map<string, string>();
+ const redis = {
+ map,
+ get: overrides.get ?? (async (k: string) => map.get(k) ?? null),
+ set:
+ overrides.set ??
+ (async (k: string, v: string) => {
+ map.set(k, v);
+ return "OK";
+ }),
+ };
+ return redis as unknown as Redis & { map: Map<string, string> };
+}
+
+const bearer = (token: string) => ({
+ headers: { authorization: `Bearer ${token}` },
+});
+
+describe("Authenticator consolidated context cache", () => {
+ let verifier: Verifier;
+
+ beforeEach(() => {
+ verifier = new Verifier(store(apiKey()), SALT);
+ });
+
+ it("miss then hit: first call resolves via verify and caches; second call serves from cache without verifying", async () => {
+ const redis = fakeRedis();
+ const auth = new Authenticator(
+ verifier,
+ resolver,
+ new AuthenticatorCache(redis, SALT),
+ );
+
+ const first = await auth.authenticate(bearer(KNOWN_SECRET));
+ expect(first.success).toBe(true);
+ expect([...redis.map.keys()]).toEqual([
+ `${AUTHZ_CONTEXT_CACHE_KEY_PREFIX}${knownHash}`,
+ ]);
+
+ const verifySpy = vi.spyOn(verifier, "verify");
+ const second = await auth.authenticate(bearer(KNOWN_SECRET));
+ expect(second.success).toBe(true);
+ expect(verifySpy).not.toHaveBeenCalled();
+ if (first.success && second.success) {
+ expect(second.context).toStrictEqual(first.context);
+ }
+ });
+
+ it("uses the authz:context: namespace, never the legacy api-key: prefix", async () => {
+ const redis = fakeRedis();
+ const auth = new Authenticator(
+ verifier,
+ resolver,
+ new AuthenticatorCache(redis, SALT),
+ );
+ await auth.authenticate(bearer(KNOWN_SECRET));
+ const key = [...redis.map.keys()][0];
+ expect(key.startsWith(AUTHZ_CONTEXT_CACHE_KEY_PREFIX)).toBe(true);
+ expect(key.startsWith(API_KEY_CACHE_KEY_PREFIX)).toBe(false);
+ });
+
+ it("negative caching: an unknown credential's 401 is stored and replayed without verifying", async () => {
+ const redis = fakeRedis();
+ const auth = new Authenticator(
+ verifier,
+ resolver,
+ new AuthenticatorCache(redis, SALT),
+ );
+
+ const first = await auth.authenticate(bearer(UNKNOWN_SECRET));
+ expect(first.success).toBe(false);
+ expect(first.error).toBeInstanceOf(UnauthorizedError);
+ const stored = redis.map.get(
+ `${AUTHZ_CONTEXT_CACHE_KEY_PREFIX}${createShaHash(UNKNOWN_SECRET, SALT)}`,
+ );
+ expect(stored).toBeDefined();
+
+ const verifySpy = vi.spyOn(verifier, "verify");
+ const second = await auth.authenticate(bearer(UNKNOWN_SECRET));
+ expect(verifySpy).not.toHaveBeenCalled();
+ expect(second.success).toBe(false);
+ if (!first.success && !second.success) {
+ expect(second.error.message).toBe(first.error.message);
+ }
+ });
+
+ it("fails open on a read error: falls through to verify and still authenticates", async () => {
+ const redis = fakeRedis({
+ get: (async () => {
+ throw new Error("redis down");
+ }) as unknown as Redis["get"],
+ });
+ const auth = new Authenticator(
+ verifier,
+ resolver,
+ new AuthenticatorCache(redis, SALT),
+ );
+ const result = await auth.authenticate(bearer(KNOWN_SECRET));
+ expect(result.success).toBe(true);
+ });
+
+ it("fails open on a write error: swallows it and still authenticates", async () => {
+ const redis = fakeRedis({
+ set: (async () => {
+ throw new Error("redis down");
+ }) as unknown as Redis["set"],
+ });
+ const auth = new Authenticator(
+ verifier,
+ resolver,
+ new AuthenticatorCache(redis, SALT),
+ );
+ const result = await auth.authenticate(bearer(KNOWN_SECRET));
+ expect(result.success).toBe(true);
+ });
+
+ it("in-app-agent key: cached and still gated off a cache hit", async () => {
+ const agentKey = apiKey({ isInAppAgentKey: true });
+ const agentVerifier = new Verifier(store(agentKey), SALT);
+ const redis = fakeRedis();
+ const auth = new Authenticator(
+ agentVerifier,
+ resolver,
+ new AuthenticatorCache(redis, SALT),
+ );
+
+ const allowed = await auth.authenticate({
+ headers: { authorization: `Bearer ${KNOWN_SECRET}` },
+ allowInAppAgentKey: true,
+ });
+ expect(allowed.success).toBe(true);
+ expect(redis.map.size).toBe(1);
+
+ const verifySpy = vi.spyOn(agentVerifier, "verify");
+ const gated = await auth.authenticate(bearer(KNOWN_SECRET));
+ expect(verifySpy).not.toHaveBeenCalled();
+ expect(gated.success).toBe(false);
+ if (!gated.success) {
+ expect(gated.error).toBeInstanceOf(UnauthorizedError);
+ }
+ });
+
+ it("admin key, route disallows: gated off a cache hit", async () => {
+ const redis = fakeRedis();
+ const adminVerifier = new Verifier(store(apiKey()), SALT, KNOWN_SECRET);
+ const auth = new Authenticator(
+ adminVerifier,
+ resolver,
+ new AuthenticatorCache(redis, SALT),
+ );
+
+ const allowed = await auth.authenticate({
+ ...bearer(KNOWN_SECRET),
+ isAdminApiKeyAuthAllowed: true,
+ });
+ expect(allowed.success).toBe(true);
+ expect(redis.map.size).toBe(1);
+
+ const verifySpy = vi.spyOn(adminVerifier, "verify");
+ const gated = await auth.authenticate(bearer(KNOWN_SECRET));
+ expect(verifySpy).not.toHaveBeenCalled();
+ expect(gated.success).toBe(false);
+ if (!gated.success) {
+ expect(gated.error).toBeInstanceOf(UnauthorizedError);
+ }
+ });
+
+ it("no-ops the cache when disabled", async () => {
+ const nullCacheAuth = new Authenticator(
+ verifier,
+ resolver,
+ new AuthenticatorCache(null, SALT),
+ );
+ const result = await nullCacheAuth.authenticate(bearer(KNOWN_SECRET));
+ expect(result.success).toBe(true);
+ });
+
+ it("does not negatively cache a 500: a later call re-runs verify", async () => {
+ const failing = {
+ findByFastHash: async () => ({
+ success: false,
+ error: new InternalServerError("db down"),
+ }),
+ findByPublicKey: async () => ({ success: true, apiKey: null }),
+ verifySlow: async () => ({ success: true, valid: false }),
+ backfillFastHash: async () => {},
+ } as unknown as ApiKeyRepository;
+ const failingVerifier = new Verifier(failing, SALT);
+ const redis = fakeRedis();
+ const auth = new Authenticator(
+ failingVerifier,
+ resolver,
+ new AuthenticatorCache(redis, SALT),
+ );
+
+ const first = await auth.authenticate(bearer(KNOWN_SECRET));
+ expect(first.success).toBe(false);
+ if (!first.success) expect(first.error).toBeInstanceOf(InternalServerError);
+ expect(redis.map.size).toBe(0);
+
+ const verifySpy = vi.spyOn(failingVerifier, "verify");
+ const second = await auth.authenticate(bearer(KNOWN_SECRET));
+ expect(verifySpy).toHaveBeenCalledTimes(1);
+ expect(second.success).toBe(false);
+ });
+
+ it("does not refresh the TTL on a read: a cache hit never writes", async () => {
+ const redis = fakeRedis();
+ const setSpy = vi.spyOn(redis, "set");
+ const auth = new Authenticator(
+ verifier,
+ resolver,
+ new AuthenticatorCache(redis, SALT),
+ );
+
+ await auth.authenticate(bearer(KNOWN_SECRET));
+ expect(setSpy).toHaveBeenCalledTimes(1);
+
+ await auth.authenticate(bearer(KNOWN_SECRET));
+ expect(setSpy).toHaveBeenCalledTimes(1);
+ });
+
+ it("get returns null on a miss, distinct from a replayed 401", async () => {
+ const redis = fakeRedis();
+ const cache = new AuthenticatorCache(redis, SALT);
+ const credential = { kind: "bearer", token: UNKNOWN_SECRET } as const;
+
+ expect(await cache.get(credential)).toBeNull();
+
+ await cache.set(credential, {
+ success: false,
+ error: new UnauthorizedError("nope"),
+ });
+ const hit = await cache.get(credential);
+ expect(hit?.success).toBe(false);
+ if (hit && !hit.success) {
+ expect(hit.error).toBeInstanceOf(UnauthorizedError);
+ }
+ });
+});
diff --git a/web/src/__tests__/server/unit/apiKey/invalidateApiKeys.servertest.ts b/web/src/__tests__/server/unit/apiKey/invalidateApiKeys.servertest.ts
new file mode 100644
index 0000000000..5f63e977ad
--- /dev/null
+++ b/web/src/__tests__/server/unit/apiKey/invalidateApiKeys.servertest.ts
@@ -0,0 +1,70 @@
+import { type Redis } from "ioredis";
+import { describe, expect, it, vi } from "vitest";
+
+import { type ApiKey } from "@langfuse/shared/src/db";
+import {
+ API_KEY_CACHE_KEY_PREFIX,
+ AUTHZ_CONTEXT_CACHE_KEY_PREFIX,
+ createShaHash,
+ invalidateCachedApiKeys,
+} from "@langfuse/shared/src/server";
+
+import { env } from "@/src/env.mjs";
+
+const apiKey = (over: Partial<ApiKey> = {}): ApiKey => ({
+ id: "key_p",
+ createdAt: new Date(0),
+ note: null,
+ publicKey: "pk-lf-1",
+ hashedSecretKey: "hsk",
+ fastHashedSecretKey: "fh1",
+ displaySecretKey: "sk-...abc",
+ lastUsedAt: null,
+ expiresAt: null,
+ isInAppAgentKey: false,
+ projectId: "prj_1",
+ orgId: "org_1",
+ scope: "PROJECT",
+ createdByUserId: "user_1",
+ createdByApiKeyId: null,
+ ...over,
+});
+
+/** delSpy captures the flat set of keys handed to safeMultiDel across single- or multi-key deletes. */
+function delSpy() {
+ const del = vi.fn((_keys: string[]): Promise<number> => Promise.resolve(1));
+ const redis = { del } as unknown as Redis;
+ const deletedKeys = () => del.mock.calls.flatMap((c) => c[0]);
+ return { redis, deletedKeys };
+}
+
+describe("invalidateCachedApiKeys clears both cache namespaces", () => {
+ it("deletes the legacy key plus both authz:context presentations per row", async () => {
+ const { redis, deletedKeys } = delSpy();
+ const key = apiKey({ fastHashedSecretKey: "fh1", publicKey: "pk-lf-1" });
+
+ await invalidateCachedApiKeys([key], "test", redis);
+
+ const keys = deletedKeys();
+ expect(keys).toContain(`${API_KEY_CACHE_KEY_PREFIX}fh1`);
+ expect(keys).toContain(`${AUTHZ_CONTEXT_CACHE_KEY_PREFIX}fh1`);
+ expect(keys).toContain(
+ `${AUTHZ_CONTEXT_CACHE_KEY_PREFIX}${createShaHash("pk-lf-1", env.SALT!)}`,
+ );
+ });
+
+ it("covers a public-key-only row that never got a fast hash", async () => {
+ const { redis, deletedKeys } = delSpy();
+ const key = apiKey({ fastHashedSecretKey: null, publicKey: "pk-lf-2" });
+
+ await invalidateCachedApiKeys([key], "test", redis);
+
+ const keys = deletedKeys();
+ expect(keys).toEqual([
+ `${AUTHZ_CONTEXT_CACHE_KEY_PREFIX}${createShaHash("pk-lf-2", env.SALT!)}`,
+ ]);
+ expect(keys.some((k) => k.startsWith(API_KEY_CACHE_KEY_PREFIX))).toBe(
+ false,
+ );
+ });
+});
diff --git a/web/src/__tests__/server/unit/apiKey/parseAuthorizationHeader.servertest.ts b/web/src/__tests__/server/unit/apiKey/parseAuthorizationHeader.servertest.ts
new file mode 100644
index 0000000000..4a5fd03bcc
--- /dev/null
+++ b/web/src/__tests__/server/unit/apiKey/parseAuthorizationHeader.servertest.ts
@@ -0,0 +1,45 @@
+import { describe, expect, it } from "vitest";
+
+import { parseAuthorizationHeader } from "@/src/features/apiKey/helpers/parseAuthorizationHeader";
+
+const basicHeader = (pub: string, secret: string) =>
+ `Basic ${btoa(`${pub}:${secret}`)}`;
+
+describe("parseAuthorizationHeader", () => {
+ it("decodes a Basic public:secret pair", () => {
+ expect(parseAuthorizationHeader(basicHeader("pk-lf-1", "sk-lf-1"))).toEqual(
+ {
+ kind: "basic",
+ publicKey: "pk-lf-1",
+ secretKey: "sk-lf-1",
+ },
+ );
+ });
+
+ it("reads a Bearer token", () => {
+ expect(parseAuthorizationHeader("Bearer sk-secret")).toEqual({
+ kind: "bearer",
+ token: "sk-secret",
+ });
+ });
+
+ it("a missing header is malformed", () => {
+ expect(parseAuthorizationHeader(undefined)).toEqual({ kind: "malformed" });
+ });
+
+ it("an unknown scheme is malformed", () => {
+ expect(parseAuthorizationHeader("Digest x")).toEqual({ kind: "malformed" });
+ });
+
+ it("a Basic payload without a colon is malformed", () => {
+ expect(parseAuthorizationHeader(`Basic ${btoa("no-colon")}`)).toEqual({
+ kind: "malformed",
+ });
+ });
+
+ it("an invalid base64 Basic payload is malformed", () => {
+ expect(parseAuthorizationHeader("Basic !!!invalid!!!")).toEqual({
+ kind: "malformed",
+ });
+ });
+});
diff --git a/web/src/__tests__/server/unit/create-authed-project-api-route-auth-errors.servertest.ts b/web/src/__tests__/server/unit/create-authed-project-api-route-auth-errors.servertest.ts
index ad7ee6b232..8a117ed182 100644
--- a/web/src/__tests__/server/unit/create-authed-project-api-route-auth-errors.servertest.ts
+++ b/web/src/__tests__/server/unit/create-authed-project-api-route-auth-errors.servertest.ts
@@ -123,6 +123,7 @@ describe("createAuthedProjectAPIRoute auth error handling", () => {
}) {
const handler = createAuthedProjectAPIRoute({
name: "Test Route",
+ action: "project:read",
querySchema: z.object({}),
responseSchema: z.object({ ok: z.literal(true) }),
errorContract: options?.useStructuredErrorContract
@@ -268,6 +269,7 @@ describe("createAuthedProjectAPIRoute auth error handling", () => {
const handler = createAuthedProjectAPIRoute({
name: "Sensitive Route",
+ action: "project:read",
querySchema: z.object({ token: z.string() }),
bodySchema: z.object({
secretKey: z.string(),
diff --git a/web/src/__tests__/server/unit/policy/authorize.servertest.ts b/web/src/__tests__/server/unit/policy/authorize.servertest.ts
new file mode 100644
index 0000000000..f7025354c8
--- /dev/null
+++ b/web/src/__tests__/server/unit/policy/authorize.servertest.ts
@@ -0,0 +1,168 @@
+import { describe, expect, it } from "vitest";
+
+import { ForbiddenError } from "@langfuse/shared";
+
+import { authorize } from "@/src/features/auth/policy/authorize";
+import {
+ allOrganizationActions,
+ allProjectActions,
+ wildcard,
+ type AuthorizationContext,
+ type Policy,
+} from "@/src/features/auth/policy/types";
+
+const ORG = "org_1";
+const PRJ = "prj_1";
+const OTHER_PRJ = "prj_2";
+
+const allowProject = (
+ actions: readonly string[],
+ resources: Policy["resources"],
+): Policy => ({
+ kind: "project",
+ source: { kind: "role", id: "OWNER" },
+ actions: actions as never,
+ resources,
+ effect: "allow",
+});
+const allowOrg = (
+ actions: readonly string[],
+ resources: Policy["resources"],
+): Policy => ({
+ kind: "organization",
+ source: { kind: "role", id: "OWNER" },
+ actions: actions as never,
+ resources,
+ effect: "allow",
+});
+const denyProject = (
+ actions: readonly string[],
+ resources: Policy["resources"],
+): Policy => ({
+ kind: "project",
+ source: { kind: "grant" },
+ actions: actions as never,
+ resources,
+ effect: "deny",
+});
+const ctx = (policies: Policy[]): AuthorizationContext => ({
+ principal: {
+ kind: "apiKey",
+ apiKeyId: "key_1",
+ userId: null,
+ isInAppAgentKey: false,
+ publicKey: "pk-lf-1",
+ scope: "PROJECT",
+ presentation: "privateKey",
+ organizations: [],
+ },
+ policies,
+});
+
+describe("authorize — project coverage", () => {
+ const grant = ctx([allowProject(["prompts:read"], [PRJ])]);
+ it.each([
+ ["a grant covers its project", { projectId: PRJ }, true],
+ ["a grant does not cover another project", { projectId: OTHER_PRJ }, false],
+ ] as const)("%s", (_name, resource, expected) => {
+ expect(authorize(grant, "prompts:read", resource).success).toBe(expected);
+ });
+ it("no project ref grants nothing", () => {
+ const empty = ctx([allowProject(["prompts:read"], [])]);
+ expect(authorize(empty, "prompts:read", { projectId: PRJ }).success).toBe(
+ false,
+ );
+ });
+});
+
+describe("authorize — org-level actions", () => {
+ const orgAdmin = ctx([allowOrg(["projects:create"], [ORG])]);
+ it("an org ref covers org-level actions", () => {
+ expect(authorize(orgAdmin, "projects:create", { orgId: ORG }).success).toBe(
+ true,
+ );
+ });
+ it("org grant does not leak to another org", () => {
+ expect(
+ authorize(orgAdmin, "projects:create", { orgId: "org_x" }).success,
+ ).toBe(false);
+ });
+});
+
+describe("authorize — kind-gating", () => {
+ it("a project wildcard never satisfies an org action", () => {
+ const projectWildcard = ctx([allowProject(["prompts:read"], wildcard)]);
+ expect(
+ authorize(projectWildcard, "project:read", { orgId: ORG }).success,
+ ).toBe(false);
+ });
+ it("an org wildcard never satisfies a project action", () => {
+ const orgWildcard = ctx([allowOrg(["project:read"], wildcard)]);
+ expect(
+ authorize(orgWildcard, "prompts:read", { projectId: PRJ }).success,
+ ).toBe(false);
+ });
+ it("a project audit grant does not satisfy an org-level audit check", () => {
+ const projectAudit = ctx([allowProject(["projectAuditLogs:read"], [PRJ])]);
+ expect(
+ authorize(projectAudit, "projectAuditLogs:read", { projectId: PRJ })
+ .success,
+ ).toBe(true);
+ expect(
+ authorize(projectAudit, "projectAuditLogs:read", { orgId: ORG }).success,
+ ).toBe(false);
+ });
+});
+
+describe("authorize — admin wildcard", () => {
+ const admin = ctx([
+ allowOrg(allOrganizationActions, wildcard),
+ allowProject(allProjectActions, wildcard),
+ ]);
+ it("admin allows any project action", () => {
+ expect(authorize(admin, "prompts:read", { projectId: "y" }).success).toBe(
+ true,
+ );
+ });
+ it("admin allows any org action", () => {
+ expect(authorize(admin, "projects:create", { orgId: "x" }).success).toBe(
+ true,
+ );
+ });
+});
+
+describe("authorize — deny-overrides and deny-by-default", () => {
+ it("denies by default when nothing matches", () => {
+ expect(authorize(ctx([]), "prompts:read", { projectId: PRJ }).success).toBe(
+ false,
+ );
+ });
+ it("a matching deny beats a matching allow", () => {
+ const suspended = ctx([
+ allowProject(["traces:create"], [PRJ]),
+ denyProject(["traces:create"], [PRJ]),
+ ]);
+ expect(
+ authorize(suspended, "traces:create", { projectId: PRJ }).success,
+ ).toBe(false);
+ });
+ it("a deny of the checked project beats an allow that also covers others", () => {
+ const c = ctx([
+ allowProject(["prompts:read"], [PRJ, OTHER_PRJ]),
+ denyProject(["prompts:read"], [PRJ]),
+ ]);
+ expect(authorize(c, "prompts:read", { projectId: PRJ }).success).toBe(
+ false,
+ );
+ expect(authorize(c, "prompts:read", { projectId: OTHER_PRJ }).success).toBe(
+ true,
+ );
+ });
+ it("a deny carries the generic Forbidden message", () => {
+ const roleDeny = denyProject(["traces:create"], [PRJ]);
+ const decision = authorize(ctx([roleDeny]), "traces:create", {
+ projectId: PRJ,
+ });
+ expect(decision.error?.message).toBe(new ForbiddenError().message);
+ });
+});
diff --git a/web/src/__tests__/server/unit/policy/contextResolver.servertest.ts b/web/src/__tests__/server/unit/policy/contextResolver.servertest.ts
new file mode 100644
index 0000000000..8619698b87
--- /dev/null
+++ b/web/src/__tests__/server/unit/policy/contextResolver.servertest.ts
@@ -0,0 +1,213 @@
+import { describe, expect, it } from "vitest";
+
+import { type ApiKey, type PrismaClient } from "@langfuse/shared/src/db";
+import { InternalServerError } from "@langfuse/shared";
+
+import { authorize } from "@/src/features/auth/policy/authorize";
+import {
+ OrganizationRepository,
+ type OrganizationWithProjects,
+} from "@/src/features/auth/policy/organizationRepository";
+import {
+ ContextResolver,
+ type ResolveContextParams,
+} from "@/src/features/auth/policy/contextResolver";
+import { type AuthorizationContext } from "@/src/features/auth/policy/types";
+
+const ORG = "org_1";
+const PRJ = "prj_1";
+const OTHER_PRJ = "prj_2";
+const USER = "user_1";
+
+const orgRow = (
+ over: Partial<OrganizationWithProjects> = {},
+): OrganizationWithProjects =>
+ ({
+ id: ORG,
+ cloudConfig: null,
+ cloudFreeTierUsageThresholdState: null,
+ projects: [{ id: PRJ }, { id: OTHER_PRJ }],
+ ...over,
+ }) as unknown as OrganizationWithProjects;
+
+const resolverFor = (row: OrganizationWithProjects | null): ContextResolver =>
+ new ContextResolver(
+ new OrganizationRepository({
+ organization: {
+ findUnique: async () => row,
+ findFirst: async () => row,
+ },
+ } as unknown as PrismaClient),
+ );
+
+const contextFor = async (
+ params: ResolveContextParams,
+ row: OrganizationWithProjects | null = orgRow(),
+): Promise<AuthorizationContext> => {
+ const resolved = await resolverFor(row).resolve(params);
+ if (!resolved.success) throw resolved.error;
+ return resolved.context;
+};
+
+const apiKey = (over: Partial<ApiKey> = {}): ApiKey => ({
+ id: "key_p",
+ createdAt: new Date(0),
+ note: null,
+ publicKey: "pk-lf-1",
+ hashedSecretKey: "hsk",
+ fastHashedSecretKey: "fhsk",
+ displaySecretKey: "sk-...abc",
+ lastUsedAt: null,
+ expiresAt: null,
+ isInAppAgentKey: false,
+ projectId: PRJ,
+ orgId: ORG,
+ scope: "PROJECT",
+ createdByUserId: USER,
+ createdByApiKeyId: null,
+ ...over,
+});
+const orgKey = (over: Partial<ApiKey> = {}): ApiKey =>
+ apiKey({ id: "key_o", scope: "ORGANIZATION", projectId: null, ...over });
+
+describe("resolves the admin key", () => {
+ it("grants admin over any project and org", async () => {
+ const ctx = await contextFor({ authorization: "admin" });
+ expect(ctx.principal.kind).toBe("admin");
+ expect(authorize(ctx, "prompts:read", { projectId: "any" }).success).toBe(
+ true,
+ );
+ expect(authorize(ctx, "projects:create", { orgId: "any" }).success).toBe(
+ true,
+ );
+ });
+});
+
+describe("presentation rides in the input", () => {
+ it("the same row is full-access under privateKey and scores-only under publicKey", async () => {
+ const priv = await contextFor({
+ authorization: "privateKey",
+ apiKey: apiKey(),
+ });
+ const pub = await contextFor({
+ authorization: "publicKey",
+ apiKey: apiKey(),
+ });
+ expect(authorize(priv, "traces:read", { projectId: PRJ }).success).toBe(
+ true,
+ );
+ expect(authorize(pub, "scores:create", { projectId: PRJ }).success).toBe(
+ true,
+ );
+ expect(authorize(pub, "traces:read", { projectId: PRJ }).success).toBe(
+ false,
+ );
+ });
+});
+
+describe("expansion table: scope PROJECT, privateKey", () => {
+ it("grants the full project vocabulary over the bound project only", async () => {
+ const ctx = await contextFor({
+ authorization: "privateKey",
+ apiKey: apiKey(),
+ });
+ expect(authorize(ctx, "prompts:read", { projectId: PRJ }).success).toBe(
+ true,
+ );
+ expect(
+ authorize(ctx, "prompts:read", { projectId: OTHER_PRJ }).success,
+ ).toBe(false);
+ });
+ it("does not satisfy org-level actions", async () => {
+ const ctx = await contextFor({
+ authorization: "privateKey",
+ apiKey: apiKey(),
+ });
+ expect(authorize(ctx, "project:read", { orgId: ORG }).success).toBe(false);
+ });
+});
+
+describe("expansion table: scope ORGANIZATION, privateKey", () => {
+ it("grants the full org vocabulary and project:read across org projects", async () => {
+ const ctx = await contextFor({
+ authorization: "privateKey",
+ apiKey: orgKey(),
+ });
+ expect(authorize(ctx, "project:read", { projectId: PRJ }).success).toBe(
+ true,
+ );
+ expect(authorize(ctx, "traces:read", { projectId: PRJ }).success).toBe(
+ false,
+ );
+ });
+});
+
+describe("attribution", () => {
+ it("carries createdByUserId to principal.userId", async () => {
+ const ctx = await contextFor({
+ authorization: "privateKey",
+ apiKey: apiKey(),
+ });
+ expect(ctx.principal.kind === "apiKey" && ctx.principal.userId).toBe(USER);
+ });
+
+ it("apiKey principal: carries isInAppAgentKey", async () => {
+ const ctx = await contextFor({
+ authorization: "privateKey",
+ apiKey: apiKey({ isInAppAgentKey: true }),
+ });
+ expect(
+ ctx.principal.kind === "apiKey" && ctx.principal.isInAppAgentKey,
+ ).toBe(true);
+ });
+});
+
+describe("org suspension rides as a boolean, not a policy", () => {
+ it("carries isIngestionSuspended while the PDP still grants creation", async () => {
+ const ctx = await contextFor(
+ { authorization: "privateKey", apiKey: apiKey() },
+ orgRow({ cloudFreeTierUsageThresholdState: "BLOCKED" }),
+ );
+ const org =
+ ctx.principal.kind === "apiKey" ? ctx.principal.organizations[0] : null;
+ expect(org?.isIngestionSuspended).toBe(true);
+ expect(authorize(ctx, "traces:create", { projectId: PRJ }).success).toBe(
+ true,
+ );
+ });
+});
+
+describe("a verified key with no org is a 500 invariant break", () => {
+ it("collapses a missing org to an InternalServerError", async () => {
+ const resolved = await resolverFor(null).resolve({
+ authorization: "privateKey",
+ apiKey: apiKey(),
+ });
+ expect(resolved.success).toBe(false);
+ if (!resolved.success) {
+ expect(resolved.error).toBeInstanceOf(InternalServerError);
+ }
+ });
+
+ it("collapses an org key with a null orgId to an InternalServerError", async () => {
+ const resolved = await resolverFor(orgRow()).resolve({
+ authorization: "privateKey",
+ apiKey: orgKey({ orgId: null }),
+ });
+ expect(resolved.success).toBe(false);
+ if (!resolved.success) {
+ expect(resolved.error).toBeInstanceOf(InternalServerError);
+ }
+ });
+
+ it("collapses a project key with a null projectId to an InternalServerError", async () => {
+ const resolved = await resolverFor(orgRow()).resolve({
+ authorization: "privateKey",
+ apiKey: apiKey({ projectId: null }),
+ });
+ expect(resolved.success).toBe(false);
+ if (!resolved.success) {
+ expect(resolved.error).toBeInstanceOf(InternalServerError);
+ }
+ });
+});
diff --git a/web/src/__tests__/server/unit/policy/enforceProjectAuth.servertest.ts b/web/src/__tests__/server/unit/policy/enforceProjectAuth.servertest.ts
new file mode 100644
index 0000000000..a007070180
--- /dev/null
+++ b/web/src/__tests__/server/unit/policy/enforceProjectAuth.servertest.ts
@@ -0,0 +1,74 @@
+import { describe, expect, it } from "vitest";
+
+import { InvalidRequestError } from "@langfuse/shared";
+
+import { __test } from "@/src/features/auth/policy/enforceProjectAuth";
+import { type AuthorizationContext } from "@/src/features/auth/policy/types";
+
+const { getProjectId } = __test;
+
+const projectIdHeader = "x-langfuse-project-id";
+
+const PRJ = "prj_1";
+
+const projectKey = (): AuthorizationContext => ({
+ principal: {
+ kind: "apiKey",
+ apiKeyId: "key_1",
+ userId: null,
+ isInAppAgentKey: false,
+ publicKey: "pk-lf-1",
+ scope: "PROJECT",
+ presentation: "privateKey",
+ organizations: [],
+ boundResource: { projectId: PRJ },
+ },
+ policies: [],
+});
+
+const orgKey = (): AuthorizationContext => ({
+ principal: {
+ kind: "apiKey",
+ apiKeyId: "key_2",
+ userId: null,
+ isInAppAgentKey: false,
+ publicKey: "pk-lf-2",
+ scope: "ORGANIZATION",
+ presentation: "privateKey",
+ organizations: [],
+ boundResource: { orgId: "org_1" },
+ },
+ policies: [],
+});
+
+describe("getProjectId", () => {
+ it("resolves the bound project without a header", () => {
+ expect(getProjectId(projectKey(), {})).toEqual({
+ success: true,
+ projectId: PRJ,
+ });
+ });
+ it("resolves the bound project when the header is blank", () => {
+ expect(getProjectId(projectKey(), { [projectIdHeader]: "" })).toEqual({
+ success: true,
+ projectId: PRJ,
+ });
+ });
+ it("resolves an unbound principal from the header", () => {
+ expect(getProjectId(orgKey(), { [projectIdHeader]: PRJ })).toEqual({
+ success: true,
+ projectId: PRJ,
+ });
+ });
+ it("400s a header disagreeing with the bound project", () => {
+ expect(
+ getProjectId(projectKey(), { [projectIdHeader]: "prj_2" }),
+ ).toMatchObject({ success: false, error: expect.any(InvalidRequestError) });
+ });
+ it("400s when neither header nor bound project exists", () => {
+ expect(getProjectId(orgKey(), {})).toMatchObject({
+ success: false,
+ error: expect.any(InvalidRequestError),
+ });
+ });
+});
diff --git a/web/src/__tests__/server/unit/policy/shadow.servertest.ts b/web/src/__tests__/server/unit/policy/shadow.servertest.ts
new file mode 100644
index 0000000000..83e6f4a3ae
--- /dev/null
+++ b/web/src/__tests__/server/unit/policy/shadow.servertest.ts
@@ -0,0 +1,77 @@
+import { describe, expect, it } from "vitest";
+
+import {
+ diffResults,
+ newVerdict,
+ recordCoverage,
+ type LegacyDecision,
+ type NewResult,
+ type ParitySink,
+} from "@/src/features/auth/policy/shadow";
+
+const capture = () => {
+ const calls: { stat: string; tags: Record<string, string | number> }[] = [];
+ const sink: ParitySink = {
+ increment: (stat, tags) => calls.push({ stat, tags }),
+ span: () => undefined,
+ };
+ return { calls, sink };
+};
+
+const deny: NewResult = { success: false, error: { httpCode: 403 } };
+const allow: NewResult = { success: true };
+
+describe("diffResults — the ship-gate signal", () => {
+ it.each([
+ ["both allow", allow, { ok: true } as LegacyDecision, "match"],
+ ["both deny", deny, { ok: false, code: 403 } as LegacyDecision, "match"],
+ [
+ "new denies what legacy allows",
+ deny,
+ { ok: true } as LegacyDecision,
+ "new_denies",
+ ],
+ [
+ "new allows what legacy denies",
+ allow,
+ { ok: false, code: 403 } as LegacyDecision,
+ "new_allows",
+ ],
+ ["legacy has no gate", deny, { absent: true } as LegacyDecision, "net_new"],
+ ] as const)("%s", (_name, neu, legacy, result) => {
+ const { calls, sink } = capture();
+ expect(
+ diffResults(
+ neu,
+ legacy,
+ { seam: "project_route", action: "traces:read" },
+ sink,
+ ),
+ ).toBe(result);
+ expect(calls[0].tags.result).toBe(result);
+ });
+});
+
+describe("newVerdict maps the new result to a verdict and code", () => {
+ it.each([
+ [allow, "allow", 200],
+ [deny, "deny", 403],
+ [{ success: false, error: { httpCode: 401 } } as NewResult, "deny", 401],
+ [{ success: false, error: { httpCode: 400 } } as NewResult, "deny", 400],
+ ] as const)("%o", (result, verdict, code) => {
+ expect(newVerdict(result)).toEqual({ verdict, code });
+ });
+});
+
+describe("recordCoverage", () => {
+ it("counts a request against its operation", () => {
+ const { calls, sink } = capture();
+ recordCoverage("GET /api/public/v2/prompts", sink);
+ expect(calls).toEqual([
+ {
+ stat: "langfuse.authz.coverage",
+ tags: { operation: "GET /api/public/v2/prompts" },
+ },
+ ]);
+ });
+});
diff --git a/web/src/__tests__/server/unit/policy/verifier.servertest.ts b/web/src/__tests__/server/unit/policy/verifier.servertest.ts
new file mode 100644
index 0000000000..7900fe1d83
--- /dev/null
+++ b/web/src/__tests__/server/unit/policy/verifier.servertest.ts
@@ -0,0 +1,201 @@
+import { describe, expect, it, vi } from "vitest";
+
+import { type ApiKey } from "@langfuse/shared/src/db";
+import { InternalServerError } from "@langfuse/shared";
+import { hashSecretKey } from "@langfuse/shared/src/server";
+
+import { Verifier } from "@/src/features/apiKey/verifier";
+import {
+ type FindApiKeyResult,
+ type ApiKeyRepository,
+} from "@/src/features/apiKey/apiKeyRepository";
+import { parseAuthorizationHeader } from "@/src/features/apiKey/helpers/parseAuthorizationHeader";
+
+const SALT = "salt";
+const ADMIN = "admin-secret";
+
+const apiKey = (over: Partial<ApiKey> = {}): ApiKey =>
+ ({
+ id: "key_1",
+ createdAt: new Date(0),
+ note: null,
+ publicKey: "pk-lf-1",
+ hashedSecretKey: "hsk",
+ fastHashedSecretKey: "fhsk",
+ displaySecretKey: "sk-...abc",
+ lastUsedAt: null,
+ expiresAt: null,
+ isInAppAgentKey: false,
+ projectId: "prj_1",
+ orgId: "org_1",
+ scope: "PROJECT",
+ createdByUserId: null,
+ createdByApiKeyId: null,
+ ...over,
+ }) as ApiKey;
+
+const basicHeader = (pub: string, secret: string) =>
+ `Basic ${btoa(`${pub}:${secret}`)}`;
+
+const lookup = (apiKey: ApiKey | null): FindApiKeyResult => ({
+ success: true,
+ apiKey,
+});
+
+const stubStore = (over: Partial<ApiKeyRepository> = {}): ApiKeyRepository =>
+ ({
+ findByFastHash: vi.fn(async () => lookup(null)),
+ findByPublicKey: vi.fn(async () => lookup(null)),
+ backfillFastHash: vi.fn(async () => {}),
+ ...over,
+ }) as unknown as ApiKeyRepository;
+
+const verifier = (store: ApiKeyRepository) => new Verifier(store, SALT, ADMIN);
+
+describe("scheme dispatch", () => {
+ it("a missing header is a 401", async () => {
+ const result = await verifier(stubStore()).verify(
+ parseAuthorizationHeader(undefined),
+ );
+ expect(result.success).toBe(false);
+ });
+ it("an unknown scheme is a 401", async () => {
+ const result = await verifier(stubStore()).verify(
+ parseAuthorizationHeader("Digest x"),
+ );
+ expect(result.success).toBe(false);
+ });
+});
+
+describe("Basic authenticates the secret as privateKey", () => {
+ it("resolves a key found by its fast hash", async () => {
+ const key = apiKey();
+ const store = stubStore({
+ findByFastHash: vi.fn(async () => lookup(key)),
+ });
+ const result = await verifier(store).verify(
+ parseAuthorizationHeader(basicHeader("pk-lf-1", "sk")),
+ );
+ expect(result).toMatchObject({
+ success: true,
+ authorization: "privateKey",
+ apiKey: key,
+ });
+ });
+ it("falls back to bcrypt and backfills the fast hash for a NULL-hash key", async () => {
+ const key = apiKey({
+ fastHashedSecretKey: null,
+ hashedSecretKey: await hashSecretKey("sk"),
+ });
+ const backfill = vi.fn(async () => {});
+ const store = stubStore({
+ findByPublicKey: vi.fn(async () => lookup(key)),
+ backfillFastHash: backfill,
+ });
+ const result = await verifier(store).verify(
+ parseAuthorizationHeader(basicHeader("pk-lf-1", "sk")),
+ );
+ expect(result.success).toBe(true);
+ expect(backfill).toHaveBeenCalledOnce();
+ });
+ it("401s when neither index nor bcrypt matches", async () => {
+ const result = await verifier(stubStore()).verify(
+ parseAuthorizationHeader(basicHeader("pk-lf-1", "sk")),
+ );
+ expect(result.success).toBe(false);
+ });
+});
+
+describe("Bearer chains admin then private then public", () => {
+ it("resolves the admin key first", async () => {
+ const result = await verifier(stubStore()).verify(
+ parseAuthorizationHeader(`Bearer ${ADMIN}`),
+ );
+ expect(result).toMatchObject({ success: true, authorization: "admin" });
+ });
+ it("private-first: a token matching the fast hash is privateKey", async () => {
+ const key = apiKey();
+ const findByPublicKey = vi.fn(async () => lookup(key));
+ const store = stubStore({
+ findByFastHash: vi.fn(async () => lookup(key)),
+ findByPublicKey,
+ });
+ const result = await verifier(store).verify(
+ parseAuthorizationHeader("Bearer sk-secret"),
+ );
+ expect(result).toMatchObject({
+ success: true,
+ authorization: "privateKey",
+ });
+ expect(findByPublicKey).not.toHaveBeenCalled();
+ });
+ it("falls to publicKey when no fast hash matches", async () => {
+ const key = apiKey();
+ const store = stubStore({
+ findByPublicKey: vi.fn(async () => lookup(key)),
+ });
+ const result = await verifier(store).verify(
+ parseAuthorizationHeader("Bearer pk-lf-1"),
+ );
+ expect(result).toMatchObject({
+ success: true,
+ authorization: "publicKey",
+ apiKey: key,
+ });
+ });
+ it("rejects an org-scoped key presented as a public-key bearer", async () => {
+ const key = apiKey({ scope: "ORGANIZATION", projectId: null });
+ const store = stubStore({
+ findByPublicKey: vi.fn(async () => lookup(key)),
+ });
+ const result = await verifier(store).verify(
+ parseAuthorizationHeader("Bearer pk-lf-1"),
+ );
+ expect(result.success).toBe(false);
+ });
+ it("a NULL-hash key is Basic-only, not Bearer-private", async () => {
+ const key = apiKey({ fastHashedSecretKey: null });
+ const store = stubStore({
+ findByFastHash: vi.fn(async () => lookup(key)),
+ findByPublicKey: vi.fn(async () => lookup(null)),
+ });
+ const result = await verifier(store).verify(
+ parseAuthorizationHeader("Bearer sk-secret"),
+ );
+ expect(result.success).toBe(false);
+ });
+});
+
+describe("admin key needs a set, non-empty configured key", () => {
+ it("is ignored when the configured key is unset", async () => {
+ const noKey = new Verifier(stubStore(), SALT, undefined);
+ const result = await noKey.verify(parseAuthorizationHeader("Bearer "));
+ expect(result.success).toBe(false);
+ });
+ it("is ignored when the configured key is whitespace only", async () => {
+ const blankKey = new Verifier(stubStore(), SALT, " ");
+ const result = await blankKey.verify(
+ parseAuthorizationHeader("Bearer "),
+ );
+ expect(result.success).toBe(false);
+ });
+});
+
+describe("infra failures surface as typed errors, not throws", () => {
+ it("propagates an api key repo failure as an InternalServerError result", async () => {
+ const store = stubStore({
+ findByFastHash: vi.fn(
+ async (): Promise<FindApiKeyResult> => ({
+ success: false,
+ error: new InternalServerError("db down"),
+ }),
+ ),
+ });
+ const result = await verifier(store).verify(
+ parseAuthorizationHeader(basicHeader("pk-lf-1", "sk")),
+ );
+ expect(result.success).toBe(false);
+ if (result.success) return;
+ expect(result.error).toBeInstanceOf(InternalServerError);
+ });
+});
diff --git a/web/src/__tests__/server/unit/policy/verifyAuth.servertest.ts b/web/src/__tests__/server/unit/policy/verifyAuth.servertest.ts
new file mode 100644
index 0000000000..16a91d972f
--- /dev/null
+++ b/web/src/__tests__/server/unit/policy/verifyAuth.servertest.ts
@@ -0,0 +1,195 @@
+import { type NextApiRequest } from "next";
+import { beforeEach, describe, expect, it, vi } from "vitest";
+
+import { ForbiddenError } from "@langfuse/shared";
+
+const {
+ env,
+ mockLegacyVerifyAuth,
+ mockEnforceProjectAuth,
+ mockDiffResults,
+ mockRecordCoverage,
+} = vi.hoisted(() => ({
+ env: { API_AUTH_MIGRATION: "legacy" as string },
+ mockLegacyVerifyAuth: vi.fn(),
+ mockEnforceProjectAuth: vi.fn(),
+ mockDiffResults: vi.fn(),
+ mockRecordCoverage: vi.fn(),
+}));
+
+vi.mock("@/src/env.mjs", () => ({ env }));
+
+vi.mock("@/src/features/public-api/server/verifyProjectApiKeyAuth", () => ({
+ verifyAuth: mockLegacyVerifyAuth,
+}));
+
+vi.mock("@/src/features/auth/policy/enforceProjectAuth", () => ({
+ enforceProjectAuth: mockEnforceProjectAuth,
+}));
+
+vi.mock("@/src/features/auth/policy/shadow", async (importOriginal) => ({
+ ...(await importOriginal<object>()),
+ diffResults: mockDiffResults,
+ recordCoverage: mockRecordCoverage,
+}));
+
+import { verifyAuth } from "@/src/features/public-api/server/createAuthedProjectAPIRoute";
+
+describe("project seam verifyAuth", () => {
+ const legacyScope = { scope: { projectId: "p1", accessLevel: "project" } };
+ const req = { headers: {}, method: "GET" } as unknown as NextApiRequest;
+
+ const call = () =>
+ verifyAuth({ req, name: "Get Traces", action: "traces:read" });
+
+ const legacyAllows = () =>
+ mockLegacyVerifyAuth.mockResolvedValue(legacyScope);
+ const legacyDenies = (status: number) =>
+ mockLegacyVerifyAuth.mockRejectedValue({ status, message: "legacy" });
+ const authzAllows = () =>
+ mockEnforceProjectAuth.mockResolvedValue({ success: true });
+ const authzDenies = () =>
+ mockEnforceProjectAuth.mockResolvedValue({
+ success: false,
+ error: new ForbiddenError("nope"),
+ });
+
+ const principalOrg = {
+ orgId: "o1",
+ plan: "Team" as const,
+ rateLimitOverrides: [],
+ projectIds: ["p1"],
+ isIngestionSuspended: false,
+ };
+ const apiKeyContextResult = (presentation: "privateKey" | "publicKey") => ({
+ success: true as const,
+ projectId: "p1",
+ context: {
+ principal: {
+ kind: "apiKey" as const,
+ apiKeyId: "ak1",
+ userId: null,
+ isInAppAgentKey: false,
+ scope: "PROJECT" as const,
+ publicKey: "pk-lf-1",
+ presentation,
+ organizations: [principalOrg],
+ boundResource: { projectId: "p1" },
+ },
+ policies: [],
+ },
+ });
+ const authzAllowsApiKey = (presentation: "privateKey" | "publicKey") =>
+ mockEnforceProjectAuth.mockResolvedValue(apiKeyContextResult(presentation));
+
+ beforeEach(() => {
+ vi.clearAllMocks();
+ env.API_AUTH_MIGRATION = "legacy";
+ });
+
+ describe("legacy mode never runs the new pipeline", () => {
+ it("returns the legacy scope and skips enforceProjectAuth", async () => {
+ legacyAllows();
+ expect(await call()).toBe(legacyScope);
+ expect(mockEnforceProjectAuth).not.toHaveBeenCalled();
+ });
+
+ it("rethrows the legacy error unchanged", async () => {
+ legacyDenies(403);
+ await expect(call()).rejects.toEqual({ status: 403, message: "legacy" });
+ expect(mockEnforceProjectAuth).not.toHaveBeenCalled();
+ });
+ });
+
+ describe("an unrecognized mode fails safe to legacy", () => {
+ beforeEach(() => {
+ env.API_AUTH_MIGRATION = "";
+ });
+
+ it("runs only legacy and skips the new pipeline and parity telemetry", async () => {
+ legacyAllows();
+ authzDenies();
+ expect(await call()).toBe(legacyScope);
+ expect(mockEnforceProjectAuth).not.toHaveBeenCalled();
+ expect(mockDiffResults).not.toHaveBeenCalled();
+ expect(mockRecordCoverage).not.toHaveBeenCalled();
+ });
+ });
+
+ describe("shadow mode keeps responses byte-identical to legacy", () => {
+ beforeEach(() => {
+ env.API_AUTH_MIGRATION = "shadow";
+ });
+
+ it("returns the legacy scope even when the new pipeline denies", async () => {
+ legacyAllows();
+ authzDenies();
+ expect(await call()).toBe(legacyScope);
+ });
+
+ it("rethrows the legacy denial even when the new pipeline allows", async () => {
+ legacyDenies(401);
+ authzAllows();
+ await expect(call()).rejects.toEqual({ status: 401, message: "legacy" });
+ });
+
+ it("records the parity cell and coverage counter", async () => {
+ legacyAllows();
+ authzDenies();
+ await call();
+ expect(mockRecordCoverage).toHaveBeenCalledWith("Get Traces");
+ expect(mockDiffResults).toHaveBeenCalledWith(
+ { success: false, error: expect.any(ForbiddenError) },
+ { ok: true },
+ { seam: "project_route", action: "traces:read" },
+ );
+ });
+ });
+
+ describe("enforce mode is the new pipeline's sole authority", () => {
+ beforeEach(() => {
+ env.API_AUTH_MIGRATION = "enforce";
+ });
+
+ it("maps a private-key principal to a legacy-shaped project scope", async () => {
+ authzAllowsApiKey("privateKey");
+ expect(await call()).toEqual({
+ validKey: true,
+ scope: {
+ projectId: "p1",
+ accessLevel: "project",
+ orgId: "o1",
+ plan: "Team",
+ rateLimitOverrides: [],
+ apiKeyId: "ak1",
+ publicKey: "pk-lf-1",
+ isIngestionSuspended: false,
+ isInAppAgentKey: false,
+ },
+ });
+ });
+
+ it("maps a public-key presentation to the scores access level", async () => {
+ authzAllowsApiKey("publicKey");
+ expect((await call()).scope.accessLevel).toBe("scores");
+ });
+
+ it("never runs legacy verify", async () => {
+ authzAllowsApiKey("privateKey");
+ await call();
+ expect(mockLegacyVerifyAuth).not.toHaveBeenCalled();
+ });
+
+ it("throws the new pipeline's 403 when it denies", async () => {
+ authzDenies();
+ await expect(call()).rejects.toEqual({ status: 403, message: "nope" });
+ });
+
+ it("does not record parity telemetry", async () => {
+ authzAllowsApiKey("privateKey");
+ await call();
+ expect(mockDiffResults).not.toHaveBeenCalled();
+ expect(mockRecordCoverage).not.toHaveBeenCalled();
+ });
+ });
+});
diff --git a/web/src/ee/features/admin-api/server/organizations/organizationById.ts b/web/src/ee/features/admin-api/server/organizations/organizationById.ts
index fc2c16f8e3..1a39b3786f 100644
--- a/web/src/ee/features/admin-api/server/organizations/organizationById.ts
+++ b/web/src/ee/features/admin-api/server/organizations/organizationById.ts
@@ -1,6 +1,9 @@
import { type NextApiRequest, type NextApiResponse } from "next";
import { prisma } from "@langfuse/shared/src/db";
-import { logger } from "@langfuse/shared/src/server";
+import {
+ invalidateCachedOrgApiKeys,
+ logger,
+} from "@langfuse/shared/src/server";
import { organizationNameSchema } from "@/src/features/organizations/utils/organizationNameSchema";
import { auditLog } from "@/src/features/audit-logs/auditLog";
import { z } from "zod";
@@ -196,6 +199,10 @@ export async function handleDeleteOrganization(
});
}
+ // Evict before the delete: ApiKey.organization cascades, so keys are gone
+ // by the time a post-delete eviction would run and it would find nothing.
+ await invalidateCachedOrgApiKeys(organizationId);
+
// Delete the organization
const deletedOrganization = await prisma.organization.delete({
where: { id: organizationId },
diff --git a/web/src/ee/features/admin-api/server/projects/createProject.ts b/web/src/ee/features/admin-api/server/projects/createProject.ts
index b217616ad1..5df988294a 100644
--- a/web/src/ee/features/admin-api/server/projects/createProject.ts
+++ b/web/src/ee/features/admin-api/server/projects/createProject.ts
@@ -1,6 +1,10 @@
import { type NextApiRequest, type NextApiResponse } from "next";
import { prisma } from "@langfuse/shared/src/db";
-import { logger, type ApiAccessScope } from "@langfuse/shared/src/server";
+import {
+ logger,
+ type ApiAccessScope,
+ invalidateCachedOrgApiKeys,
+} from "@langfuse/shared/src/server";
import { projectNameSchema } from "@/src/features/auth/lib/projectNameSchema";
import { projectRetentionSchema } from "@/src/features/auth/lib/projectRetentionSchema";
import { hasEntitlementBasedOnPlan } from "@/src/features/entitlements/server/hasEntitlement";
@@ -95,6 +99,9 @@ export async function handleCreateProject(
},
});
+ // Refresh org-scoped keys' baked projectIds now that a project exists.
+ await invalidateCachedOrgApiKeys(scope.orgId);
+
// Best-effort CHB metering signal; no-op unless the org is CHB-billed
emitChbProjectEvent({
type: "LANGFUSE_PROJECT_CREATED",
diff --git a/web/src/ee/features/admin-api/server/projects/projectById/index.ts b/web/src/ee/features/admin-api/server/projects/projectById/index.ts
index cb4ce97fc9..bccf0ca7cc 100644
--- a/web/src/ee/features/admin-api/server/projects/projectById/index.ts
+++ b/web/src/ee/features/admin-api/server/projects/projectById/index.ts
@@ -155,6 +155,11 @@ export async function handleDeleteProject(
action: "delete",
});
+ // Refresh org-scoped keys' baked projectIds now that a project is gone.
+ await new ApiAuthService(prisma, redis).invalidateCachedOrgApiKeys(
+ scope.orgId,
+ );
+
// Soft-delete is the billing-relevant moment: the customer stops being
// billable now, not when the async hard-delete worker finishes.
emitChbProjectEvent({
diff --git a/web/src/ee/features/audit-log-viewer/AuditLogsSettingsPage.tsx b/web/src/ee/features/audit-log-viewer/AuditLogsSettingsPage.tsx
index 2380a90bc8..01711e8423 100644
--- a/web/src/ee/features/audit-log-viewer/AuditLogsSettingsPage.tsx
+++ b/web/src/ee/features/audit-log-viewer/AuditLogsSettingsPage.tsx
@@ -7,7 +7,7 @@ import { useHasProjectAccess } from "@/src/features/rbac";
export function AuditLogsSettingsPage(props: { projectId: string }) {
const hasAccess = useHasProjectAccess({
projectId: props.projectId,
- scope: "auditLogs:read",
+ scope: "projectAuditLogs:read",
});
const hasEntitlement = useHasEntitlement("audit-logs");
diff --git a/web/src/ee/features/audit-log-viewer/OrgAuditLogsSettingsPage.tsx b/web/src/ee/features/audit-log-viewer/OrgAuditLogsSettingsPage.tsx
index 330809992d..33970cd996 100644
--- a/web/src/ee/features/audit-log-viewer/OrgAuditLogsSettingsPage.tsx
+++ b/web/src/ee/features/audit-log-viewer/OrgAuditLogsSettingsPage.tsx
@@ -7,7 +7,7 @@ import { useHasOrganizationAccess } from "@/src/features/rbac";
export function OrgAuditLogsSettingsPage(props: { orgId: string }) {
const hasAccess = useHasOrganizationAccess({
organizationId: props.orgId,
- scope: "auditLogs:read",
+ scope: "orgAuditLogs:read",
});
const hasEntitlement = useHasEntitlement("audit-logs");
diff --git a/web/src/env.mjs b/web/src/env.mjs
index 18c2eafb2e..0b52f15422 100644
--- a/web/src/env.mjs
+++ b/web/src/env.mjs
@@ -359,6 +359,9 @@ export const env = createEnv({
LANGFUSE_CACHE_API_KEY_ENABLED: z.enum(["true", "false"]).default("true"),
LANGFUSE_CACHE_API_KEY_TTL_SECONDS: z.coerce.number().default(300),
+ // auth migration; self-host and default stay legacy
+ API_AUTH_MIGRATION: z.enum(["legacy", "shadow", "enforce"]).default("legacy"),
+
// Multimodal media upload to S3
LANGFUSE_S3_MEDIA_MAX_CONTENT_LENGTH: z.coerce
.number()
@@ -1028,6 +1031,7 @@ export const env = createEnv({
LANGFUSE_CACHE_API_KEY_ENABLED: process.env.LANGFUSE_CACHE_API_KEY_ENABLED,
LANGFUSE_CACHE_API_KEY_TTL_SECONDS:
process.env.LANGFUSE_CACHE_API_KEY_TTL_SECONDS,
+ API_AUTH_MIGRATION: process.env.API_AUTH_MIGRATION,
LANGFUSE_ALLOWED_ORGANIZATION_CREATORS:
process.env.LANGFUSE_ALLOWED_ORGANIZATION_CREATORS,
STRIPE_SECRET_KEY: process.env.STRIPE_SECRET_KEY,
diff --git a/web/src/features/apiKey/apiKeyRepository.ts b/web/src/features/apiKey/apiKeyRepository.ts
new file mode 100644
index 0000000000..5948653be6
--- /dev/null
+++ b/web/src/features/apiKey/apiKeyRepository.ts
@@ -0,0 +1,68 @@
+import {
+ type ApiKey,
+ type PrismaClient,
+ prisma as defaultPrisma,
+} from "@langfuse/shared/src/db";
+import { InternalServerError } from "@langfuse/shared";
+import { logger } from "@langfuse/shared/src/server";
+
+import {
+ type ErrorResult,
+ type Success,
+} from "@/src/features/auth/policy/types";
+
+/** ApiKeyRepository reads `ApiKey` rows by the two disjoint unique indexes and backfills the fast hash, returning infra failures as values; caching lives at the Authenticator. */
+export class ApiKeyRepository {
+ constructor(private readonly prisma: PrismaClient = defaultPrisma) {}
+
+ /** findByFastHash looks a key up by its fast-hash unique index. */
+ async findByFastHash(fastHashedSecretKey: string): Promise<FindApiKeyResult> {
+ try {
+ const apiKey = await this.prisma.apiKey.findUnique({
+ where: { fastHashedSecretKey },
+ });
+ return { success: true, apiKey };
+ } catch (error) {
+ return {
+ success: false,
+ error: new InternalServerError(
+ `api key lookup by fast hash failed: ${String(error)}`,
+ ),
+ };
+ }
+ }
+
+ /** findByPublicKey looks a key up by its public-key unique index. */
+ async findByPublicKey(publicKey: string): Promise<FindApiKeyResult> {
+ try {
+ const apiKey = await this.prisma.apiKey.findUnique({
+ where: { publicKey },
+ });
+ return { success: true, apiKey };
+ } catch (error) {
+ return {
+ success: false,
+ error: new InternalServerError(
+ `api key lookup by public key failed: ${String(error)}`,
+ ),
+ };
+ }
+ }
+
+ /** backfillFastHash writes a key's fast hash, swallowing failures. */
+ async backfillFastHash(apiKeyId: string, hash: string): Promise<void> {
+ try {
+ await this.prisma.apiKey.update({
+ where: { id: apiKeyId },
+ data: { fastHashedSecretKey: hash },
+ });
+ } catch (error) {
+ logger.error("authz api key fast-hash backfill failed", error);
+ }
+ }
+}
+
+/** FindApiKeyResult is a hit, a miss (null), or an infra failure; a miss is normal control flow, not an error. */
+export type FindApiKeyResult =
+ | (Success & { apiKey: ApiKey | null })
+ | ErrorResult<InternalServerError>;
diff --git a/web/src/features/apiKey/authenticator.ts b/web/src/features/apiKey/authenticator.ts
new file mode 100644
index 0000000000..ebb1a4d4ab
--- /dev/null
+++ b/web/src/features/apiKey/authenticator.ts
@@ -0,0 +1,104 @@
+import { type IncomingHttpHeaders } from "http";
+
+import { type InternalServerError, UnauthorizedError } from "@langfuse/shared";
+
+import { ContextResolver } from "@/src/features/auth/policy/contextResolver";
+import {
+ parseAuthorizationHeader,
+ type Credential,
+} from "@/src/features/apiKey/helpers/parseAuthorizationHeader";
+import { AuthenticatorCache } from "@/src/features/apiKey/authenticatorCache";
+import { Verifier, invalidCredentials } from "@/src/features/apiKey/verifier";
+import {
+ type AuthorizationContext,
+ type ErrorResult,
+ type Principal,
+ type Success,
+} from "@/src/features/auth/policy/types";
+
+/** Authenticator resolves a request's credential into an `AuthorizationContext`: cache → verify → resolve → enforce route settings. */
+export class Authenticator {
+ constructor(
+ private readonly authn: Verifier = new Verifier(),
+ private readonly authz: ContextResolver = new ContextResolver(),
+ private readonly cache: AuthenticatorCache = new AuthenticatorCache(),
+ ) {}
+
+ /** authenticate runs the full pipeline read-through the context cache and enforces route settings on the resolved principal on every path, returning a typed failure rather than throwing. */
+ async authenticate(params: ApiKeyAuthParams): Promise<ApiKeyAuthResults> {
+ const credential = parseAuthorizationHeader(params.headers.authorization);
+ if (credential.kind === "malformed") {
+ return {
+ success: false,
+ error: new UnauthorizedError(invalidCredentials),
+ };
+ }
+
+ const result =
+ (await this.cache.get(credential)) ??
+ (await this.verifyAndResolve(credential));
+ if (result.success) {
+ const denied = enforceRouteSettings(result.context.principal, params);
+ if (denied) return denied;
+ }
+ return result;
+ }
+
+ /** verifyAndResolve authenticates and materializes on a cache miss, writing every cacheable outcome back to the cache. */
+ private async verifyAndResolve(
+ credential: Credential,
+ ): Promise<ApiKeyAuthResults> {
+ const verified = await this.authn.verify(credential);
+ if (!verified.success) {
+ await this.cache.set(credential, verified);
+ return verified;
+ }
+ const resolved = await this.authz.resolve(verified);
+ await this.cache.set(credential, resolved);
+ return resolved;
+ }
+}
+
+/** authenticator is the Authenticator on its default prisma/redis collaborators. */
+export const authenticator = new Authenticator();
+
+/** enforceRouteSettings rejects key kinds a route does not opt into — in-app-agent and admin — reading the resolved principal so it reruns on every cache path. */
+function enforceRouteSettings(
+ principal: Principal,
+ params: ApiKeyAuthParams,
+): ErrorResult<UnauthorizedError> | null {
+ if (principal.kind === "admin" && !params.isAdminApiKeyAuthAllowed) {
+ return {
+ success: false,
+ error: new UnauthorizedError("Admin API key auth is not allowed here"),
+ };
+ }
+ if (
+ principal.kind === "apiKey" &&
+ principal.isInAppAgentKey &&
+ !params.allowInAppAgentKey
+ ) {
+ return {
+ success: false,
+ error: new UnauthorizedError(
+ "Access denied - in-app agent keys are not allowed for this endpoint",
+ ),
+ };
+ }
+ return null;
+}
+
+/** ApiKeyAuthParams is the request headers plus the route's key-kind opt-ins. */
+export type ApiKeyAuthParams = {
+ headers: IncomingHttpHeaders;
+ allowInAppAgentKey?: boolean;
+ isAdminApiKeyAuthAllowed?: boolean;
+};
+
+/** ApiKeyAuthResults is the pipeline's outcome: the resolved context, or a typed failure. */
+export type ApiKeyAuthResults =
+ | Authenticated
+ | ErrorResult<UnauthorizedError | InternalServerError>;
+
+/** Authenticated is the pipeline's success outcome: the resolved authorization context. */
+export type Authenticated = Success & { context: AuthorizationContext };
diff --git a/web/src/features/apiKey/authenticatorCache.ts b/web/src/features/apiKey/authenticatorCache.ts
new file mode 100644
index 0000000000..6e0ba8a576
--- /dev/null
+++ b/web/src/features/apiKey/authenticatorCache.ts
@@ -0,0 +1,109 @@
+import { type Redis, type Cluster } from "ioredis";
+
+import { UnauthorizedError } from "@langfuse/shared";
+import {
+ redis as defaultRedis,
+ createShaHash,
+ createAuthzContextCacheKey,
+ logger,
+} from "@langfuse/shared/src/server";
+
+import { env } from "@/src/env.mjs";
+import { type Credential } from "@/src/features/apiKey/helpers/parseAuthorizationHeader";
+import {
+ type ApiKeyAuthResults,
+ type Authenticated,
+} from "@/src/features/apiKey/authenticator";
+import {
+ type AuthorizationContext,
+ type ErrorResult,
+} from "@/src/features/auth/policy/types";
+
+/** AuthenticatorCache is the read-through context cache keyed by credential; it owns key derivation and stores the Authenticator's resolved result. */
+export class AuthenticatorCache {
+ constructor(
+ private readonly redis: Redis | Cluster | null = defaultRedis,
+ private readonly salt: string = env.SALT,
+ ) {}
+
+ /** get returns the cached result for a credential, or null on a miss — distinct from a cached 401. */
+ async get(credential: Credential): Promise<ResolveContextResult | null> {
+ const key = this.keyFor(credential);
+ const redis = this.redis;
+ if (!key || !cacheEnabled(redis)) return null;
+ try {
+ const raw = await redis.get(key);
+ if (!raw) return null;
+ return deserialize(JSON.parse(raw) as CachedEntry);
+ } catch (error) {
+ logger.error("authz context cache read failed, falling open", error);
+ return null;
+ }
+ }
+
+ /** set persists a success context or a 401 under a fixed TTL, skips a 500 (fail open), and returns whether it wrote. */
+ async set(
+ credential: Credential,
+ result: ApiKeyAuthResults,
+ ): Promise<boolean> {
+ const key = this.keyFor(credential);
+ const entry = toEntry(result);
+ const redis = this.redis;
+ if (!key || !entry || !cacheEnabled(redis)) return false;
+ try {
+ await redis.set(
+ key,
+ JSON.stringify(entry),
+ "EX",
+ env.LANGFUSE_CACHE_API_KEY_TTL_SECONDS,
+ );
+ return true;
+ } catch (error) {
+ logger.error("authz context cache write failed", error);
+ return false;
+ }
+ }
+
+ /** keyFor derives the namespaced cache key from a credential's secret material, or null when uncacheable. */
+ private keyFor(credential: Credential): string | null {
+ if (credential.kind === "basic") {
+ return createAuthzContextCacheKey(
+ createShaHash(credential.secretKey, this.salt),
+ );
+ }
+ if (credential.kind === "bearer") {
+ return createAuthzContextCacheKey(
+ createShaHash(credential.token, this.salt),
+ );
+ }
+ return null;
+ }
+}
+
+/** cacheEnabled is the shared on/off switch for the context cache, reusing the legacy api-key cache flag. */
+function cacheEnabled(redis: Redis | Cluster | null): redis is Redis | Cluster {
+ return Boolean(redis) && env.LANGFUSE_CACHE_API_KEY_ENABLED === "true";
+}
+
+/** toEntry maps a resolved result to its cache row, yielding null for a 500 so it is never negatively cached. */
+function toEntry(result: ApiKeyAuthResults): CachedEntry | null {
+ if (result.success) return { context: result.context };
+ if (result.error instanceof UnauthorizedError) {
+ return { unauthorized: result.error.message };
+ }
+ return null;
+}
+
+/** deserialize reconstructs a cache hit from its stored row. */
+function deserialize(entry: CachedEntry): ResolveContextResult {
+ if ("context" in entry) return { success: true, context: entry.context };
+ return { success: false, error: new UnauthorizedError(entry.unauthorized) };
+}
+
+/** ResolveContextResult is a cache hit: a resolved context or a replayed 401, never a 500. */
+export type ResolveContextResult =
+ | Authenticated
+ | ErrorResult<UnauthorizedError>;
+
+/** CachedEntry is the redis-serialized cache value: a materialized context, or a negative 401 body. */
+type CachedEntry = { context: AuthorizationContext } | { unauthorized: string };
diff --git a/web/src/features/apiKey/helpers/parseAuthorizationHeader.ts b/web/src/features/apiKey/helpers/parseAuthorizationHeader.ts
new file mode 100644
index 0000000000..2d5f703e33
--- /dev/null
+++ b/web/src/features/apiKey/helpers/parseAuthorizationHeader.ts
@@ -0,0 +1,41 @@
+/** basicPrefix is the HTTP Basic auth scheme prefix. */
+const basicPrefix = "Basic ";
+
+/** bearerPrefix is the HTTP Bearer auth scheme prefix. */
+const bearerPrefix = "Bearer ";
+
+/** Credential is the parsed Authorization wire format — a scheme, never a verification outcome. */
+export type Credential =
+ | { kind: "basic"; publicKey: string; secretKey: string }
+ | { kind: "bearer"; token: string }
+ | { kind: "malformed" };
+
+/** parseAuthorizationHeader decodes the Authorization header into its scheme, or `malformed` for anything unparsable. */
+export function parseAuthorizationHeader(
+ header: string | undefined,
+): Credential {
+ if (header?.startsWith(basicPrefix)) {
+ const decoded = decodeBasic(header.slice(basicPrefix.length));
+ if (!decoded) return { kind: "malformed" };
+ return { kind: "basic", ...decoded };
+ }
+ if (header?.startsWith(bearerPrefix)) {
+ return { kind: "bearer", token: header.slice(bearerPrefix.length) };
+ }
+ return { kind: "malformed" };
+}
+
+/** decodeBasic decodes a base64 `public:secret` payload, or undefined if malformed. */
+function decodeBasic(
+ encoded: string,
+): { publicKey: string; secretKey: string } | undefined {
+ let decoded: string;
+ try {
+ decoded = atob(encoded);
+ } catch {
+ return undefined;
+ }
+ const [publicKey, secretKey] = decoded.split(":");
+ if (!publicKey || !secretKey) return undefined;
+ return { publicKey, secretKey };
+}
diff --git a/web/src/features/apiKey/verifier.ts b/web/src/features/apiKey/verifier.ts
new file mode 100644
index 0000000000..377f14af78
--- /dev/null
+++ b/web/src/features/apiKey/verifier.ts
@@ -0,0 +1,157 @@
+import crypto from "node:crypto";
+
+import { type ApiKey } from "@langfuse/shared/src/db";
+import { InternalServerError, UnauthorizedError } from "@langfuse/shared";
+import { createShaHash, verifySecretKey } from "@langfuse/shared/src/server";
+
+import { env } from "@/src/env.mjs";
+import { type Credential } from "@/src/features/apiKey/helpers/parseAuthorizationHeader";
+import { ApiKeyRepository } from "@/src/features/apiKey/apiKeyRepository";
+import {
+ type ErrorResult,
+ type Success,
+} from "@/src/features/auth/policy/types";
+
+/** invalidCredentials is today's single 401 body for any unknown or malformed token. */
+export const invalidCredentials =
+ "Invalid credentials. Confirm that you've configured the correct host.";
+
+/** publicKeyPrefix is the prefix every Langfuse public key carries. */
+const publicKeyPrefix = "pk-lf-";
+
+/** Verifier authenticates a request credential into a resolvable presentation, dispatching Basic vs Bearer and never throwing. */
+export class Verifier {
+ constructor(
+ private readonly apiKeyRepo: ApiKeyRepository = new ApiKeyRepository(),
+ private readonly salt: string = env.SALT,
+ private readonly adminApiKey: string | undefined = env.ADMIN_API_KEY,
+ ) {}
+
+ /** verify resolves a parsed credential to a presentation, or a typed failure. */
+ async verify(credential: Credential): Promise<VerifyApiKeyResult> {
+ if (credential.kind === "basic") {
+ return this.verifyBasic(credential.publicKey, credential.secretKey);
+ }
+ if (credential.kind === "bearer") {
+ return this.verifyBearer(credential.token);
+ }
+ return unauthorized();
+ }
+
+ /** verifyBasic authenticates a public:secret pair as the privateKey presentation, private key first then a slow bcrypt backfill. */
+ private async verifyBasic(
+ publicKey: string,
+ secretKey: string,
+ ): Promise<VerifyApiKeyResult> {
+ const byPrivateKey = await this.verifyPrivateKey(secretKey);
+ if (byPrivateKey) return byPrivateKey;
+
+ const bySlowHash = await this.backfillSlowHash(publicKey, secretKey);
+ if (bySlowHash) return bySlowHash;
+
+ return unauthorized();
+ }
+
+ /** verifyBearer chains admin, then public (public key), then private (fast hash). */
+ private async verifyBearer(token: string): Promise<VerifyApiKeyResult> {
+ const admin = this.verifyAdminKey(token);
+ if (admin) return admin;
+
+ const byPublicKey = await this.verifyPublicKey(token);
+ if (byPublicKey) return byPublicKey;
+
+ const byPrivateKey = await this.verifyPrivateKey(token);
+ if (byPrivateKey) return byPrivateKey;
+
+ return unauthorized();
+ }
+
+ /** verifyPrivateKey resolves a secret to its privateKey presentation via the fast-hash index, or null when it is not indexed there. */
+ private async verifyPrivateKey(
+ secretKey: string,
+ ): Promise<VerifyApiKeyResult | null> {
+ const found = await this.apiKeyRepo.findByFastHash(
+ createShaHash(secretKey, this.salt),
+ );
+ if (!found.success) return found;
+ if (found.apiKey?.fastHashedSecretKey) return privateKey(found.apiKey);
+ return null;
+ }
+
+ /** backfillSlowHash bcrypt-verifies a secret against the public key's row, backfilling the fast hash on a match, or null on a miss. */
+ private async backfillSlowHash(
+ publicKey: string,
+ secretKey: string,
+ ): Promise<VerifyApiKeyResult | null> {
+ const found = await this.apiKeyRepo.findByPublicKey(publicKey);
+ if (!found.success) return found;
+ if (!found.apiKey) return null;
+
+ let valid: boolean;
+ try {
+ valid = await verifySecretKey(secretKey, found.apiKey.hashedSecretKey);
+ } catch (error) {
+ return {
+ success: false,
+ error: new InternalServerError(`slow verify failed: ${String(error)}`),
+ };
+ }
+ if (!valid) return null;
+
+ await this.apiKeyRepo.backfillFastHash(
+ found.apiKey.id,
+ createShaHash(secretKey, this.salt),
+ );
+ return privateKey(found.apiKey);
+ }
+
+ /** verifyAdminKey resolves a token that timing-safe matches the admin key, or null; the key is ignored unless set and non-empty after trimming. */
+ private verifyAdminKey(token: string): VerifyApiKeyResult | null {
+ const adminApiKey = this.adminApiKey?.trim();
+ if (!adminApiKey) return null;
+ try {
+ if (
+ crypto.timingSafeEqual(Buffer.from(token), Buffer.from(adminApiKey))
+ ) {
+ return { success: true, authorization: "admin" };
+ }
+ } catch {
+ return null;
+ }
+ return null;
+ }
+
+ /** verifyPublicKey resolves a public-key token to its scores-only presentation, or null when it is not a public key or is unknown. */
+ private async verifyPublicKey(
+ token: string,
+ ): Promise<VerifyApiKeyResult | null> {
+ if (!token.startsWith(publicKeyPrefix)) return null;
+ const found = await this.apiKeyRepo.findByPublicKey(token);
+ if (!found.success) return found;
+ if (!found.apiKey) return null;
+ // Public-key (bearer) auth is project-scoped score ingest only; an org key
+ // must authenticate with its secret over basic or private bearer.
+ if (found.apiKey.scope !== "PROJECT") return null;
+ return { success: true, authorization: "publicKey", apiKey: found.apiKey };
+ }
+}
+
+/** privateKey wraps an ApiKey row as the full-access privateKey presentation. */
+function privateKey(apiKey: ApiKey): VerifyApiKeyResult {
+ return { success: true, authorization: "privateKey", apiKey };
+}
+
+/** unauthorized is the single 401 outcome for any unknown or malformed credential. */
+function unauthorized(): ErrorResult<UnauthorizedError> {
+ return { success: false, error: new UnauthorizedError(invalidCredentials) };
+}
+
+/** VerifiedCredential is the presentation the resolver consumes: an api key with how it was presented, or the admin key. */
+type VerifiedCredential =
+ | { authorization: "publicKey" | "privateKey"; apiKey: ApiKey }
+ | { authorization: "admin" };
+
+/** VerifyApiKeyResult is the verified credential, or a typed failure; verify returns, never throws. */
+export type VerifyApiKeyResult =
+ | (Success & VerifiedCredential)
+ | ErrorResult<UnauthorizedError | InternalServerError>;
diff --git a/web/src/features/auth/policy/authorize.ts b/web/src/features/auth/policy/authorize.ts
new file mode 100644
index 0000000000..a67a17c748
--- /dev/null
+++ b/web/src/features/auth/policy/authorize.ts
@@ -0,0 +1,55 @@
+import { ForbiddenError } from "@langfuse/shared";
+
+import {
+ wildcard,
+ type Action,
+ type AuthorizationContext,
+ type Decision,
+ type ErrorResult,
+ type Policy,
+ type Resource,
+} from "./types";
+
+/** authorize evaluates the policies for action on resource: a matching deny 403s, else a matching allow succeeds, else implicit-deny 403. */
+export function authorize(
+ ctx: AuthorizationContext,
+ action: Action,
+ resource: Resource,
+): Decision {
+ const matches = ctx.policies
+ .filter(hasResourceKind(resource))
+ .filter(hasAction(action))
+ .filter(hasResourceId(resource));
+
+ if (matches.some(hasEffect("deny"))) {
+ return forbidden();
+ }
+ if (matches.some(hasEffect("allow"))) {
+ return { success: true };
+ }
+ return forbidden();
+}
+
+/** hasResourceKind matches a policy of the kind that governs the checked resource. */
+const hasResourceKind = (resource: Resource) => (p: Policy) =>
+ p.kind === ("projectId" in resource ? "project" : "organization");
+
+/** hasResourceId matches a policy whose resources cover the checked resource's id, by wildcard or listing. */
+const hasResourceId = (resource: Resource) => (p: Policy) =>
+ p.resources === wildcard ||
+ p.resources.includes(
+ "projectId" in resource ? resource.projectId : resource.orgId,
+ );
+
+/** hasAction matches a policy granting the action explicitly; actions are never wildcarded. */
+const hasAction = (action: Action) => (p: Policy) =>
+ (p.actions as readonly string[]).includes(action);
+
+/** hasEffect matches a policy of the given effect. */
+const hasEffect = (effect: Policy["effect"]) => (p: Policy) =>
+ p.effect === effect;
+
+/** forbidden builds a 403 Decision carrying the generic ForbiddenError. */
+function forbidden(): ErrorResult<ForbiddenError> {
+ return { success: false, error: new ForbiddenError() };
+}
diff --git a/web/src/features/auth/policy/contextResolver.ts b/web/src/features/auth/policy/contextResolver.ts
new file mode 100644
index 0000000000..14ec97f6d3
--- /dev/null
+++ b/web/src/features/auth/policy/contextResolver.ts
@@ -0,0 +1,199 @@
+import { type ApiKey } from "@langfuse/shared/src/db";
+import { CloudConfigSchema, InternalServerError } from "@langfuse/shared";
+
+import { apiKeyAccessRights } from "@/src/features/rbac/constants/apiKeyAccessRights";
+import { getOrganizationPlanServerSide } from "@/src/features/entitlements/server/getPlan";
+import {
+ OrganizationRepository,
+ type GetOrganizationResult,
+ type OrganizationWithProjects,
+} from "./organizationRepository";
+import {
+ wildcard,
+ type AuthorizationContext,
+ type ErrorResult,
+ type Policy,
+ type Principal,
+ type PrincipalOrganization,
+ type Resource,
+ type Success,
+ type SystemPolicy,
+} from "./types";
+
+/** ContextResolver materializes an authenticated credential into its `AuthorizationContext`, loading and enriching the org it implies. */
+export class ContextResolver {
+ constructor(
+ private readonly orgs: OrganizationRepository = new OrganizationRepository(),
+ ) {}
+
+ /** resolve turns a verified credential into its context, collapsing a missing org to a 500 invariant break. */
+ async resolve(params: ResolveContextParams): Promise<Resolved> {
+ if (params.authorization === "admin") {
+ return { success: true, context: adminContext() };
+ }
+ const org = await this.getPrincipalOrganization(params.apiKey);
+ if (!org.success) return org;
+ return {
+ success: true,
+ context: materialize(
+ params.apiKey,
+ params.authorization,
+ org.organization,
+ ),
+ };
+ }
+
+ /** getPrincipalOrganization loads and derives the key's `PrincipalOrganization`, collapsing a null miss to a 500 invariant break. */
+ private async getPrincipalOrganization(
+ apiKey: ApiKey,
+ ): Promise<
+ | (Success & { organization: PrincipalOrganization })
+ | ErrorResult<InternalServerError>
+ > {
+ const found = await this.loadOrganization(apiKey);
+ if (!found.success) return found;
+ if (!found.organization) {
+ return {
+ success: false,
+ error: new InternalServerError(
+ `verified key ${apiKey.id} resolved to no organization`,
+ ),
+ };
+ }
+ return {
+ success: true,
+ organization: toPrincipalOrganization(found.organization),
+ };
+ }
+
+ /** loadOrganization loads the key's org by its scope: an org key by orgId, a project key by its bound projectId. */
+ private async loadOrganization(
+ apiKey: ApiKey,
+ ): Promise<GetOrganizationResult> {
+ if (apiKey.scope === "ORGANIZATION") {
+ if (apiKey.orgId === null) {
+ return {
+ success: false,
+ error: new InternalServerError(`org key ${apiKey.id} has no orgId`),
+ };
+ }
+ return this.orgs.getOrganizationByOrgId(apiKey.orgId);
+ }
+ if (apiKey.projectId === null) {
+ return {
+ success: false,
+ error: new InternalServerError(
+ `project key ${apiKey.id} has no projectId`,
+ ),
+ };
+ }
+ return this.orgs.getOrganizationByProjectId(apiKey.projectId);
+ }
+}
+
+/** materialize expands the `ApiKey` row and its presentation into the policies the credential implies. */
+function materialize(
+ apiKey: ApiKey,
+ authorization: "publicKey" | "privateKey",
+ org: PrincipalOrganization,
+): AuthorizationContext {
+ const principal: Principal = {
+ kind: "apiKey",
+ apiKeyId: apiKey.id,
+ userId: apiKey.createdByUserId,
+ isInAppAgentKey: apiKey.isInAppAgentKey,
+ publicKey: apiKey.publicKey,
+ scope: apiKey.scope,
+ presentation: authorization,
+ organizations: [org],
+ boundResource: boundResourceFor(apiKey),
+ };
+
+ const grants =
+ authorization === "publicKey"
+ ? apiKeyAccessRights.SCORES_INGEST
+ : apiKeyAccessRights[apiKey.scope];
+ const policies = grants.map((p) => bind(p, principal));
+ return { principal, policies };
+}
+
+/** adminContext is the admin context: no key row, the ADMIN role bound to the wildcard resource. */
+function adminContext(): AuthorizationContext {
+ const principal: Principal = { kind: "admin", userId: null };
+ return {
+ principal,
+ policies: apiKeyAccessRights.ADMIN.map((policy) => bind(policy, principal)),
+ };
+}
+
+/** toPrincipalOrganization derives an org's `PrincipalOrganization` caps and liveness from its raw row. */
+function toPrincipalOrganization(
+ org: OrganizationWithProjects,
+): PrincipalOrganization {
+ const cloudConfig = getCloudConfig(org);
+ return {
+ orgId: org.id,
+ plan: getOrganizationPlanServerSide(cloudConfig),
+ rateLimitOverrides: cloudConfig?.rateLimitOverrides ?? [],
+ projectIds: org.projects.map((p) => p.id),
+ isIngestionSuspended: org.cloudFreeTierUsageThresholdState === "BLOCKED",
+ };
+}
+
+/** getCloudConfig parses an org's raw cloud-config json, or undefined when unset. */
+function getCloudConfig(
+ org: OrganizationWithProjects,
+): CloudConfigSchema | undefined {
+ return org.cloudConfig ? CloudConfigSchema.parse(org.cloudConfig) : undefined;
+}
+
+/** bind fixes a resource-less SystemPolicy to the resources the principal covers, by the policy's kind. */
+function bind(policy: SystemPolicy, principal: Principal): Policy {
+ return policy.kind === "organization"
+ ? { ...policy, resources: orgResources(principal) }
+ : { ...policy, resources: projectResources(principal) };
+}
+
+/** projectResources are the project ids a project-kind policy binds to: the bound project, the bound org's projects, or the wildcard for admin. */
+function projectResources(principal: Principal): Policy["resources"] {
+ if (principal.kind === "admin") return wildcard;
+ const bound =
+ principal.kind === "apiKey" ? principal.boundResource : undefined;
+ if (bound && "projectId" in bound) return [bound.projectId];
+ const orgs =
+ bound && "orgId" in bound
+ ? principal.organizations.filter((o) => o.orgId === bound.orgId)
+ : principal.organizations;
+ return orgs.flatMap((o) => o.projectIds);
+}
+
+/** orgResources are the org ids an org-kind policy binds to: the bound org, or the wildcard for admin. */
+function orgResources(principal: Principal): Policy["resources"] {
+ if (principal.kind === "admin") return wildcard;
+ const bound =
+ principal.kind === "apiKey" ? principal.boundResource : undefined;
+ const orgs =
+ bound && "orgId" in bound
+ ? principal.organizations.filter((o) => o.orgId === bound.orgId)
+ : principal.organizations;
+ return orgs.map((o) => o.orgId);
+}
+
+/** boundResourceFor is the legacy single-target a request resolves against with no header. */
+function boundResourceFor(apiKey: ApiKey): Resource {
+ if (apiKey.scope === "ORGANIZATION") return { orgId: apiKey.orgId! };
+ return { projectId: apiKey.projectId! };
+}
+
+/** ResolveContextParams is a verified credential: an api key with how it was presented, or the admin key. */
+export type ResolveContextParams =
+ | {
+ authorization: "publicKey" | "privateKey";
+ apiKey: ApiKey;
+ }
+ | { authorization: "admin" };
+
+/** Resolved is the materialized context, or a 500 when a verified key's org is missing. */
+export type Resolved =
+ | (Success & { context: AuthorizationContext })
+ | ErrorResult<InternalServerError>;
diff --git a/web/src/features/auth/policy/enforceProjectAuth.ts b/web/src/features/auth/policy/enforceProjectAuth.ts
new file mode 100644
index 0000000000..34956cc31d
--- /dev/null
+++ b/web/src/features/auth/policy/enforceProjectAuth.ts
@@ -0,0 +1,113 @@
+import { type IncomingHttpHeaders } from "http";
+
+import {
+ type ForbiddenError,
+ type InternalServerError,
+ InvalidRequestError,
+ type UnauthorizedError,
+} from "@langfuse/shared";
+
+import { authorize } from "./authorize";
+import { authenticator } from "@/src/features/apiKey/authenticator";
+import {
+ type AuthorizationContext,
+ type ErrorResult,
+ type ProjectAction,
+ type Success,
+} from "./types";
+
+/** projectIdHeader selects the target project for keys without a bound project. */
+const projectIdHeader = "x-langfuse-project-id";
+
+/** headerValue normalizes a possibly-repeated header to its first value. */
+const headerValue = (
+ value: string | string[] | undefined,
+): string | undefined => (Array.isArray(value) ? value[0] : value);
+
+/** enforceProjectAuth runs the new project pipeline — authenticate, its own target resolution, authorize — returning every outcome as a value; it never throws one. */
+export async function enforceProjectAuth(
+ params: EnforceProjectAuthParams,
+): Promise<ProjectAccessResult | ErrorResult<AuthError>> {
+ const authn = await authenticator.authenticate({
+ headers: params.headers,
+ allowInAppAgentKey: params.allowInAppAgentKey,
+ isAdminApiKeyAuthAllowed: params.isAdminApiKeyAuthAllowed,
+ });
+ if (!authn.success) return authn;
+
+ const context = authn.context;
+ const target = getProjectId(context, params.headers);
+ if (!target.success) return target;
+
+ if (params.action === undefined) {
+ return { success: true, context, projectId: target.projectId };
+ }
+ const decision = authorize(context, params.action, {
+ projectId: target.projectId,
+ });
+ if (!decision.success) {
+ return { success: false, error: decision.error };
+ }
+ return { success: true, context, projectId: target.projectId };
+}
+
+/** getProjectId resolves the target project as `header ?? boundResource ?? 400`; a header disagreeing with the bound project 400s. */
+function getProjectId(
+ context: AuthorizationContext,
+ headers: IncomingHttpHeaders,
+): ResolvedProject | ErrorResult<InvalidRequestError> {
+ const boundProjectId = boundProjectIdOf(context);
+ const header = headerValue(headers[projectIdHeader]) || undefined;
+ if (header && boundProjectId && header !== boundProjectId) {
+ return {
+ success: false,
+ error: new InvalidRequestError(
+ `${projectIdHeader} disagrees with the API key's project`,
+ ),
+ };
+ }
+ const projectId = header ?? boundProjectId;
+ if (!projectId) {
+ return {
+ success: false,
+ error: new InvalidRequestError(
+ `No project target: send ${projectIdHeader} or use a project-scoped API key`,
+ ),
+ };
+ }
+ return { success: true, projectId };
+}
+
+/** boundProjectIdOf returns the project an api key is bound to, when any. */
+function boundProjectIdOf(context: AuthorizationContext): string | undefined {
+ if (context.principal.kind !== "apiKey") return undefined;
+ const bound = context.principal.boundResource;
+ return bound && "projectId" in bound ? bound.projectId : undefined;
+}
+
+/** EnforceProjectAuthParams is the request headers, the checked action, and the route's key-kind opt-ins. */
+export type EnforceProjectAuthParams = {
+ headers: IncomingHttpHeaders;
+ action?: ProjectAction;
+ allowInAppAgentKey?: boolean;
+ isAdminApiKeyAuthAllowed?: boolean;
+};
+
+/** ProjectAccessResult is the project seam's success outcome: the resolved context and target. */
+export type ProjectAccessResult = Success & {
+ context: AuthorizationContext;
+ projectId: string;
+};
+
+/** AuthError is any typed failure the project pipeline surfaces. */
+export type AuthError =
+ | UnauthorizedError
+ | InvalidRequestError
+ | InternalServerError
+ | ForbiddenError;
+
+/** ResolvedProject is project target resolution's success outcome. */
+type ResolvedProject = Success & { projectId: string };
+
+/** __test exposes module-private helpers for the colocated unit test. */
+export const __test = { getProjectId };
diff --git a/web/src/features/auth/policy/organizationRepository.ts b/web/src/features/auth/policy/organizationRepository.ts
new file mode 100644
index 0000000000..9948e4b9a9
--- /dev/null
+++ b/web/src/features/auth/policy/organizationRepository.ts
@@ -0,0 +1,67 @@
+import {
+ type Organization,
+ type PrismaClient,
+ prisma as defaultPrisma,
+} from "@langfuse/shared/src/db";
+import { InternalServerError } from "@langfuse/shared";
+
+import { type ErrorResult, type Success } from "./types";
+
+/** OrganizationRepository reads an org with its live projects, returning a null miss or an infra failure as a value. */
+export class OrganizationRepository {
+ constructor(private readonly prisma: PrismaClient = defaultPrisma) {}
+
+ /** getOrganizationByOrgId loads an org and all its live project ids by org id, carrying a null miss on success. */
+ async getOrganizationByOrgId(orgId: string): Promise<GetOrganizationResult> {
+ try {
+ const organization = await this.prisma.organization.findUnique({
+ where: { id: orgId },
+ include: {
+ projects: { where: { deletedAt: null }, select: { id: true } },
+ },
+ });
+ return { success: true, organization };
+ } catch (error) {
+ return {
+ success: false,
+ error: new InternalServerError(
+ `failed to load org ${orgId}: ${String(error)}`,
+ ),
+ };
+ }
+ }
+
+ /** getOrganizationByProjectId loads the org owning a live project, carrying only that project id, or a null miss on success for a soft-deleted or unknown project. */
+ async getOrganizationByProjectId(
+ projectId: string,
+ ): Promise<GetOrganizationResult> {
+ try {
+ const organization = await this.prisma.organization.findFirst({
+ where: { projects: { some: { id: projectId, deletedAt: null } } },
+ });
+ return {
+ success: true,
+ organization: organization
+ ? { ...organization, projects: [{ id: projectId }] }
+ : null,
+ };
+ } catch (error) {
+ return {
+ success: false,
+ error: new InternalServerError(
+ `failed to load org for project ${projectId}: ${String(error)}`,
+ ),
+ };
+ }
+ }
+}
+
+/** OrganizationWithProjects is the raw org row plus its live project ids. */
+export type OrganizationWithProjects = Organization & {
+ projects: { id: string }[];
+};
+
+/** GetOrganizationResult is the loaded org or a null miss, or an infra failure. */
+export type GetOrganizationResult =
+ | (Success & { organization: OrganizationWithProjects | null })
+ | ErrorResult<InternalServerError>;
diff --git a/web/src/features/auth/policy/shadow.ts b/web/src/features/auth/policy/shadow.ts
new file mode 100644
index 0000000000..e4624e7862
--- /dev/null
+++ b/web/src/features/auth/policy/shadow.ts
@@ -0,0 +1,113 @@
+import { getCurrentSpan, recordIncrement } from "@langfuse/shared/src/server";
+
+/** parityStat counts one authorization decision. */
+const parityStat = "langfuse.authz.parity";
+
+/** coverageStat counts one request against its operation. */
+const coverageStat = "langfuse.authz.coverage";
+
+/** defaultSink writes to dogstatsd and the active span; tests inject a capturing fake. */
+const defaultSink: ParitySink = {
+ increment: (stat, tags) => recordIncrement(stat, 1, tags),
+ span: () => getCurrentSpan(),
+};
+
+/** diffResults classifies the new pipeline's decision against legacy's, emits `langfuse.authz.parity`, and returns the result. */
+export function diffResults(
+ neu: NewResult,
+ legacy: LegacyDecision,
+ meta: { seam: Seam; action: string },
+ sink: ParitySink = defaultSink,
+): ParityResult {
+ const n = newVerdict(neu);
+ const l = legacyVerdict(legacy);
+ const result = classify(l.verdict, n.verdict);
+ const tags = {
+ seam: meta.seam,
+ action: meta.action,
+ result,
+ legacy_code: l.code,
+ new_code: n.code,
+ } satisfies Record<string, string | number>;
+ sink.increment(parityStat, tags);
+ sink.span()?.setAttribute(`${parityStat}.result`, result);
+ return result;
+}
+
+/** recordCoverage counts a request against its operation so parity zeros can be told from untested routes. */
+export function recordCoverage(
+ operation: string,
+ sink: ParitySink = defaultSink,
+): void {
+ sink.increment(coverageStat, { operation });
+}
+
+/** newVerdict reads the new pipeline's result: success allows (200), else denies with its http code. */
+export function newVerdict(result: NewResult): {
+ verdict: Verdict;
+ code: number;
+} {
+ return result.success
+ ? { verdict: "allow", code: 200 }
+ : { verdict: "deny", code: result.error.httpCode };
+}
+
+/** verdictFromStatus reads a legacy chokepoint's http status: under 400 allows, else denies. */
+function verdictFromStatus(status: number): Verdict {
+ return status < 400 ? "allow" : "deny";
+}
+
+/** legacyFromStatus lifts a legacy chokepoint's http status into a LegacyDecision. */
+export function legacyFromStatus(status: number): LegacyDecision {
+ return status < 400 ? { ok: true } : { ok: false, code: status };
+}
+
+/** legacyVerdict reads a legacy decision into a verdict + code. */
+function legacyVerdict(legacy: LegacyDecision): {
+ verdict: Verdict;
+ code: number;
+} {
+ if ("absent" in legacy) return { verdict: "absent", code: 0 };
+ const code = legacy.ok ? 200 : legacy.code;
+ return { verdict: verdictFromStatus(code), code };
+}
+
+/** classify names the disagreement: legacy without a gate is `net_new`, agreement is `match`, else which path is stricter. */
+function classify(legacy: Verdict, neu: Verdict): ParityResult {
+ if (legacy === "absent") return "net_new";
+ if (legacy === neu) return "match";
+ return neu === "deny" ? "new_denies" : "new_allows";
+}
+
+/** Verdict is one path's decision at one enforcement point; legacy is `absent` only where it runs no gate. */
+export type Verdict = "allow" | "deny" | "absent";
+
+/** ParityResult is the signal: agreement, which path is stricter, or net-new enforcement legacy never gated. */
+export type ParityResult = "match" | "new_denies" | "new_allows" | "net_new";
+
+/** Seam is the enforcement point a decision came from. */
+export type Seam =
+ | "project_route"
+ | "org_route"
+ | "ingestion_event"
+ | "mcp_access"
+ | "mcp_tool";
+
+/** LegacyDecision is the legacy path's outcome: it allowed, it denied with an http code, or it ran no gate. */
+export type LegacyDecision =
+ | { ok: true }
+ | { ok: false; code: number }
+ | { absent: true };
+
+/** ParitySink is the telemetry surface, injectable so tests capture without a collector. */
+export type ParitySink = {
+ increment: (stat: string, tags: Record<string, string | number>) => void;
+ span: () =>
+ | { setAttribute: (key: string, value: string | number) => void }
+ | undefined;
+};
+
+/** NewResult is the new pipeline's outcome at a decision point: a success or a typed error carrying its http code. */
+export type NewResult =
+ | { success: true }
+ | { success: false; error: { httpCode: number } };
diff --git a/web/src/features/auth/policy/types.ts b/web/src/features/auth/policy/types.ts
new file mode 100644
index 0000000000..2cd0805c32
--- /dev/null
+++ b/web/src/features/auth/policy/types.ts
@@ -0,0 +1,113 @@
+import { type z } from "zod";
+
+import {
+ projectScopes,
+ type ApiKeyScope,
+ type CloudConfigRateLimit,
+ type ForbiddenError,
+ type Plan,
+ type ProjectScope,
+} from "@langfuse/shared";
+import {
+ organizationScopes,
+ type OrganizationScope,
+} from "@/src/features/rbac/constants/organizationAccessRights";
+
+/** wildcard matches any resource in a policy. */
+export const wildcard = "*" as const;
+
+/** allProjectActions is the full project action vocabulary. */
+export const allProjectActions: ProjectAction[] = [...projectScopes];
+
+/** allOrganizationActions is the full organization action vocabulary. */
+export const allOrganizationActions: OrganizationAction[] = [
+ ...organizationScopes,
+];
+
+/** Wildcard is the type of the wildcard resource matcher literal. */
+type Wildcard = typeof wildcard;
+
+/** ProjectAction is an action assignable to a project policy. */
+export type ProjectAction = ProjectScope;
+
+/** OrganizationAction is an action assignable to an organization policy. */
+export type OrganizationAction = OrganizationScope;
+
+/** Action is any checkable action. */
+export type Action = ProjectAction | OrganizationAction;
+
+/** PrincipalOrganization carries an org's static caps and its ingestion-suspension liveness state, enforced at the seam not the PDP. */
+export type PrincipalOrganization = {
+ orgId: string;
+ plan: Plan;
+ rateLimitOverrides: z.infer<typeof CloudConfigRateLimit>;
+ projectIds: string[];
+ isIngestionSuspended: boolean;
+};
+
+/** Principal is an authorized admin, user, or api key, discriminated on kind. */
+export type Principal =
+ | { kind: "admin"; userId: string | null }
+ | { kind: "user"; userId: string; organizations: PrincipalOrganization[] }
+ | {
+ kind: "apiKey";
+ apiKeyId: string;
+ userId: string | null;
+ isInAppAgentKey: boolean;
+ publicKey: string;
+ scope: ApiKeyScope;
+ presentation: "publicKey" | "privateKey";
+ organizations: PrincipalOrganization[];
+ boundResource?: Resource;
+ };
+
+/** Source describes where a policy came from: a role or an explicit grant. */
+type Source = { kind: "role"; id: string } | { kind: "grant" };
+
+/** ProjectResource identifies a project by its globally-unique id. */
+type ProjectResource = { projectId: string };
+
+/** OrgResource identifies an org node. */
+type OrgResource = { orgId: string };
+
+/** Resource is the thing being checked: a bare project or an org node. */
+export type Resource = ProjectResource | OrgResource;
+
+/** BasePolicy carries the origin and effect every policy shares. */
+type BasePolicy = {
+ source: Source;
+ effect: "allow" | "deny";
+};
+
+/** OrganizationSystemPolicy is a resource-less org-level policy. */
+type OrganizationSystemPolicy = BasePolicy & {
+ kind: "organization";
+ actions: OrganizationAction[];
+};
+
+/** ProjectSystemPolicy is a resource-less project-level policy. */
+type ProjectSystemPolicy = BasePolicy & {
+ kind: "project";
+ actions: ProjectAction[];
+};
+
+/** SystemPolicy is a policy before its resource is bound. */
+export type SystemPolicy = OrganizationSystemPolicy | ProjectSystemPolicy;
+
+/** Policy is a SystemPolicy bound to the flat ids its kind targets, or the wildcard. */
+export type Policy = SystemPolicy & { resources: string[] | Wildcard };
+
+/** AuthorizationContext is the PIP output and PDP input for one principal. */
+export type AuthorizationContext = {
+ principal: Principal;
+ policies: Policy[];
+};
+
+/** Success is a successful outcome, disjoint from ErrorResult on `success`. */
+export type Success = { success: true; error?: never };
+
+/** ErrorResult is a failed outcome carrying the typed error. */
+export type ErrorResult<E> = { success: false; error: E };
+
+/** Decision is a PDP outcome: a boolean success, or a typed 403. */
+export type Decision = Success | ErrorResult<ForbiddenError>;
diff --git a/web/src/features/batch-exports/server/batchExport.ts b/web/src/features/batch-exports/server/batchExport.ts
index be62a6802b..b9ae479446 100644
--- a/web/src/features/batch-exports/server/batchExport.ts
+++ b/web/src/features/batch-exports/server/batchExport.ts
@@ -52,7 +52,8 @@ const canReadAuditLogs = (session: AuthedSession, projectId: string) =>
entitlement: "audit-logs",
sessionUser: session.user,
projectId,
- }) && hasProjectAccess({ session, projectId, scope: "auditLogs:read" });
+ }) &&
+ hasProjectAccess({ session, projectId, scope: "projectAuditLogs:read" });
// An audit-log export holds actor identifiers and admin actions, so reading
// one needs the audit-log gates too, not just the batch-export ones.
@@ -62,7 +63,11 @@ const assertCanReadAuditLogs = (session: AuthedSession, projectId: string) => {
sessionUser: session.user,
projectId,
});
- throwIfNoProjectAccess({ session, projectId, scope: "auditLogs:read" });
+ throwIfNoProjectAccess({
+ session,
+ projectId,
+ scope: "projectAuditLogs:read",
+ });
};
const isDownloadWindowExpired = (batchExport: {
diff --git a/web/src/features/onboarding/server/onboardingService.ts b/web/src/features/onboarding/server/onboardingService.ts
index 8909ad4694..27b58347b7 100644
--- a/web/src/features/onboarding/server/onboardingService.ts
+++ b/web/src/features/onboarding/server/onboardingService.ts
@@ -4,7 +4,10 @@ import {
Role,
SurveyName,
} from "@langfuse/shared/src/db";
-import { resolveProjectRole } from "@langfuse/shared/src/server";
+import {
+ resolveProjectRole,
+ invalidateCachedOrgApiKeys,
+} from "@langfuse/shared/src/server";
import { env } from "@/src/env.mjs";
import { auditLog } from "@/src/features/audit-logs/auditLog";
import {
@@ -432,5 +435,9 @@ export const provisionStarterOrganizationForNewUser = async ({
prisma,
);
+ // After the tx commits: refresh org-scoped keys' baked projectIds for the
+ // new starter project.
+ await invalidateCachedOrgApiKeys(createdResources.organization.id);
+
return createdResources;
};
diff --git a/web/src/features/organizations/server/organizationRouter.ts b/web/src/features/organizations/server/organizationRouter.ts
index 8594620bf1..6efa93e2e2 100644
--- a/web/src/features/organizations/server/organizationRouter.ts
+++ b/web/src/features/organizations/server/organizationRouter.ts
@@ -421,17 +421,18 @@ export const organizationsRouter = createTRPCRouter({
}
}
+ // Evict before the delete: ApiKey.organization cascades, so keys are gone
+ // by the time a post-delete eviction would run and it would find nothing.
+ await new ApiAuthService(ctx.prisma, redis).invalidateCachedOrgApiKeys(
+ input.orgId,
+ );
+
const organization = await ctx.prisma.organization.delete({
where: {
id: input.orgId,
},
});
- // the api keys contain which org they belong to, so we need to remove them from Redis
- await new ApiAuthService(ctx.prisma, redis).invalidateCachedOrgApiKeys(
- input.orgId,
- );
-
await auditLog({
session: ctx.session,
resourceType: "organization",
diff --git a/web/src/features/projects/server/projectsRouter.ts b/web/src/features/projects/server/projectsRouter.ts
index 5e454fcf31..a05ca62cde 100644
--- a/web/src/features/projects/server/projectsRouter.ts
+++ b/web/src/features/projects/server/projectsRouter.ts
@@ -18,6 +18,7 @@ import {
redis,
ProjectDeleteQueue,
getEnvironmentsForProject,
+ invalidateCachedOrgApiKeys,
} from "@langfuse/shared/src/server";
import { randomUUID } from "crypto";
import { StringNoHTMLNonEmpty } from "@langfuse/shared";
@@ -69,6 +70,9 @@ export const projectsRouter = createTRPCRouter({
after: project,
});
+ // Refresh org-scoped keys' baked projectIds now that a project exists.
+ await invalidateCachedOrgApiKeys(input.orgId);
+
// Best-effort CHB metering signal; no-op unless the org is CHB-billed
emitChbProjectEvent({
type: "LANGFUSE_PROJECT_CREATED",
@@ -220,6 +224,9 @@ export const projectsRouter = createTRPCRouter({
action: "delete",
});
+ // Refresh org-scoped keys' baked projectIds now that a project is gone.
+ await invalidateCachedOrgApiKeys(ctx.session.orgId);
+
// Soft-delete is the billing-relevant moment: the customer stops being
// billable now, not when the async hard-delete worker finishes.
emitChbProjectEvent({
@@ -317,6 +324,10 @@ export const projectsRouter = createTRPCRouter({
redis,
).invalidateCachedProjectApiKeys(input.projectId);
+ // Both orgs' org-scoped keys bake a projectId set that just changed.
+ await invalidateCachedOrgApiKeys(ctx.session.orgId);
+ await invalidateCachedOrgApiKeys(input.targetOrgId);
+
// A transfer is a delete for the source org and a create for the
// destination one. CHB's registry is keyed by (organizationId,
// projectId), so emitting only one side leaves the source org billed for
diff --git a/web/src/features/prompts/server/handlers/promptVersionHandler.ts b/web/src/features/prompts/server/handlers/promptVersionHandler.ts
index 32d66853c3..a5e4421cc8 100644
--- a/web/src/features/prompts/server/handlers/promptVersionHandler.ts
+++ b/web/src/features/prompts/server/handlers/promptVersionHandler.ts
@@ -18,6 +18,7 @@ const UpdatePromptBodySchema = z.object({
export const promptVersionHandler = withMiddlewares({
PATCH: createAuthedProjectAPIRoute({
name: "Update Prompt",
+ action: "prompts:CUD",
bodySchema: UpdatePromptBodySchema,
responseSchema: z.any(),
fn: async ({ body, req, auth }) => {
diff --git a/web/src/features/public-api/server/createAuthedProjectAPIRoute.ts b/web/src/features/public-api/server/createAuthedProjectAPIRoute.ts
index 5d1261932c..098ba459b5 100644
--- a/web/src/features/public-api/server/createAuthedProjectAPIRoute.ts
+++ b/web/src/features/public-api/server/createAuthedProjectAPIRoute.ts
@@ -1,12 +1,7 @@
-import crypto from "node:crypto";
import { type NextApiRequest, type NextApiResponse } from "next";
import { type ZodType, type z } from "zod";
-import { ApiAuthService } from "@/src/features/public-api/server/apiAuth";
-import { prisma } from "@langfuse/shared/src/db";
import {
- redis,
type AuthHeaderValidVerificationResult,
- type ApiAccessLevel,
traceException,
logger,
contextWithLangfuseProps,
@@ -31,9 +26,24 @@ import {
} from "./structuredPublicApiErrorContract";
import { clickHouseRouteForRequest } from "@/src/features/public-api/server/clickHouseRequestTags";
import { attachDeprecation } from "@/src/features/public-api/server/deprecations";
-
-/** Access levels that can be accepted by project-scoped API routes. */
-type RouteAccessLevel = Exclude<ApiAccessLevel, "organization">;
+import {
+ verifyAuth as verifyLegacyAuth,
+ type RouteAccessLevel,
+} from "@/src/features/public-api/server/verifyProjectApiKeyAuth";
+import {
+ enforceProjectAuth,
+ type ProjectAccessResult,
+} from "@/src/features/auth/policy/enforceProjectAuth";
+import {
+ diffResults,
+ legacyFromStatus,
+ recordCoverage,
+} from "@/src/features/auth/policy/shadow";
+import {
+ type Principal,
+ type ProjectAction,
+} from "@/src/features/auth/policy/types";
+import { prisma } from "@langfuse/shared/src/db";
// Next's res.json uses JSON.stringify; V8 throws this when the JSON string
// exceeds the engine limit. Keep this check scoped to the response write.
@@ -46,6 +56,11 @@ export type AuthedProjectAPIRouteConfig<
TResponse extends ZodType<any>,
> = {
name: string;
+ /**
+ * The project action this route authorizes through the policy core. Required
+ * so a route cannot ship with no authorization.
+ */
+ action: ProjectAction;
querySchema?: TQuery;
bodySchema?: TBody;
responseSchema: TResponse;
@@ -99,158 +114,145 @@ export type AuthedProjectAPIRouteConfig<
}) => Promise<z.infer<TResponse>>;
};
-/**
- * Verifies API key authentication (Basic or Bearer) using ApiAuthService.
- *
- * Delegates to ApiAuthService.verifyAuthHeaderAndReturnScope which handles
- * both Basic auth (public + secret key) and Bearer auth (public key only).
- * The caller controls which access levels are accepted via allowedAccessLevels.
- *
- * @param authHeader - The Authorization header from the request
- * @param allowedAccessLevels - Access levels to accept (default: ["project"])
- * @returns An auth scope object with the verified access level
- * @throws Error with appropriate message if authentication fails
- */
-async function verifyApiKeyAuth(
- authHeader: string | undefined,
- allowedAccessLevels: RouteAccessLevel[] = ["project"],
- allowInAppAgentKey = false,
-): Promise<
- AuthHeaderValidVerificationResult & {
- scope: { projectId: string; accessLevel: RouteAccessLevel };
- }
-> {
- const regularAuth = await new ApiAuthService(
- prisma,
- redis,
- ).verifyAuthHeaderAndReturnScope(authHeader, { allowInAppAgentKey });
-
- if (!regularAuth.validKey) {
- throw { status: 401, message: regularAuth.error };
+/** verifyAuth is the project seam: the new pipeline decides alone in enforce, both run for parity in shadow (byte-identical to legacy), and legacy decides alone otherwise (the default). */
+export async function verifyAuth(
+ params: VerifyAuthParams,
+): Promise<VerifyAuthResult> {
+ // enforce mode runs only the new pipeline, which is the sole authority.
+ if (env.API_AUTH_MIGRATION === "enforce") {
+ const authz = await runNewAuth(params);
+ if (!authz.success) {
+ throw { status: authz.error.httpCode, message: authz.error.message };
+ }
+ return toVerifyAuthResult(authz);
}
- if (
- !(allowedAccessLevels as ApiAccessLevel[]).includes(
- regularAuth.scope.accessLevel,
- )
- ) {
- throw {
- status: 403,
- message: "Access denied - insufficient permissions for this endpoint",
- };
+ // shadow mode runs both: legacy decides, the new pipeline records parity.
+ if (env.API_AUTH_MIGRATION === "shadow") {
+ const legacy = await runLegacyAuth(params);
+ const authz = await runNewAuth(params);
+ recordCoverage(params.name);
+ diffResults(authz, legacyFromStatus(legacy.status), {
+ seam: "project_route",
+ action: params.action,
+ });
+ if (!legacy.ok) throw legacy.error;
+ return legacy.auth;
}
- if (!regularAuth.scope.projectId) {
- throw {
- status: 403,
- message:
- "Project ID not found for API token. Are you using an organization key?",
+ // legacy is the default: any other value (including a blank one) fails safe
+ // to the legacy path, so self-host does no new auth work.
+ const legacy = await runLegacyAuth(params);
+ if (!legacy.ok) throw legacy.error;
+ return legacy.auth;
+}
+
+/** runLegacyAuth runs the legacy verify and captures its throw as a value with the status it reported. */
+async function runLegacyAuth(
+ params: VerifyAuthParams,
+): Promise<LegacyDecision> {
+ try {
+ const auth = await verifyLegacyAuth(
+ params.req,
+ params.isAdminApiKeyAuthAllowed ?? false,
+ params.allowedAccessLevels ?? ["project"],
+ params.allowInAppAgentKey ?? false,
+ );
+ return { ok: true, status: 200, auth };
+ } catch (error) {
+ const status = (error as { status?: unknown }).status;
+ return {
+ ok: false,
+ status: typeof status === "number" ? status : 500,
+ error,
};
}
+}
- return regularAuth as AuthHeaderValidVerificationResult & {
- scope: { projectId: string; accessLevel: RouteAccessLevel };
- };
+/** runNewAuth runs the new project pipeline for the request's action and route opt-ins. */
+function runNewAuth(params: VerifyAuthParams) {
+ return enforceProjectAuth({
+ headers: params.req.headers,
+ action: params.action,
+ allowInAppAgentKey: params.allowInAppAgentKey,
+ isAdminApiKeyAuthAllowed: params.isAdminApiKeyAuthAllowed,
+ });
}
-/**
- * Verifies admin API key authentication for self-hosted instances.
- *
- * This function checks if the request contains valid admin API key credentials:
- * 1. Authorization header must be Bearer token format with ADMIN_API_KEY value
- * 2. x-langfuse-admin-api-key header must match ADMIN_API_KEY env var exactly (for redundancy)
- * 3. x-langfuse-project-id header must be present and specify a valid project ID
- * 4. NEXT_PUBLIC_LANGFUSE_CLOUD_REGION must NOT be set (self-hosted instances only)
- *
- * The ADMIN_API_KEY must be set as an environment variable on the server.
- * This authentication method is intended for administrative operations on self-hosted instances.
- *
- * @param req - The Next.js API request
- * @returns An auth scope object if successful, null if admin auth is not being attempted
- * @throws Error with appropriate status code if admin auth fails
- */
-async function verifyAdminApiKeyAuth(req: NextApiRequest): Promise<
- | (AuthHeaderValidVerificationResult & {
- scope: { projectId: string; accessLevel: "project" };
- })
- | null
-> {
- const authHeader = req.headers.authorization;
- const adminApiKeyHeader = req.headers["x-langfuse-admin-api-key"];
- const projectIdHeader = req.headers["x-langfuse-project-id"];
-
- // If not attempting admin auth, return null to proceed with regular auth
- if (!authHeader?.startsWith("Bearer ") || !adminApiKeyHeader) return null;
-
- // Verify this is a self-hosted instance (not Langfuse Cloud)
- if (env.NEXT_PUBLIC_LANGFUSE_CLOUD_REGION) {
+/** toVerifyAuthResult maps the new pipeline's principal to the legacy-shaped verified scope. */
+async function toVerifyAuthResult(
+ authz: ProjectAccessResult,
+): Promise<VerifyAuthResult> {
+ const { principal } = authz.context;
+ if (principal.kind === "admin") return adminScope(authz.projectId);
+ if (principal.kind !== "apiKey") {
throw {
- status: 403,
- message: "Admin API key auth is not available on Langfuse Cloud",
+ status: 500,
+ message: `unexpected principal kind on the project seam: ${principal.kind}`,
};
}
+ return apiKeyScope(principal, authz.projectId);
+}
- // Verify ADMIN_API_KEY is configured
- const adminApiKey = env.ADMIN_API_KEY;
- if (!adminApiKey) {
+/** apiKeyScope maps an api-key principal to its verified scope; an org key here is an invariant break. */
+function apiKeyScope(
+ principal: ApiKeyPrincipal,
+ projectId: string,
+): VerifyAuthResult {
+ // The project seam denies org keys at the PDP, so one reaching here means a
+ // project route granted an org-satisfiable action.
+ if (principal.scope !== "PROJECT") {
throw {
status: 500,
- message: "Admin API key is not configured on this instance",
+ message: `org-scoped key ${principal.apiKeyId} reached the project mapper`,
};
}
-
- // Extract Bearer token
- const bearerToken = authHeader.replace("Bearer ", "");
-
- // 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(
- Buffer.from(bearerToken),
- Buffer.from(adminApiKey),
- );
- const headerEqual = crypto.timingSafeEqual(
- Buffer.from(String(adminApiKeyHeader)),
- Buffer.from(adminApiKey),
- );
- const isEqual = bearerTokenEqual && headerEqual;
-
- if (!isEqual) throw Error();
- } catch {
- throw { status: 401, message: "Invalid admin API key" };
+ const org = principal.organizations[0];
+ if (!org) {
+ throw {
+ status: 500,
+ message: `api key ${principal.apiKeyId} resolved to no organization`,
+ };
}
+ return {
+ validKey: true,
+ scope: {
+ projectId,
+ accessLevel:
+ principal.presentation === "publicKey" ? "scores" : "project",
+ orgId: org.orgId,
+ plan: org.plan,
+ rateLimitOverrides: org.rateLimitOverrides,
+ apiKeyId: principal.apiKeyId,
+ publicKey: principal.publicKey,
+ isIngestionSuspended: org.isIngestionSuspended,
+ isInAppAgentKey: principal.isInAppAgentKey,
+ },
+ };
+}
- // Verify project ID header is present
- if (!projectIdHeader || typeof projectIdHeader !== "string") {
+/** adminScope synthesizes the legacy self-host admin scope, loading the target project's org (404 on miss); admin key auth never applies on Langfuse Cloud. */
+async function adminScope(projectId: string): Promise<VerifyAuthResult> {
+ if (env.NEXT_PUBLIC_LANGFUSE_CLOUD_REGION) {
throw {
- status: 400,
- message:
- "x-langfuse-project-id header is required for admin API key authentication",
+ status: 403,
+ message: "Admin API key auth is not available on Langfuse Cloud",
};
}
-
- // Verify project exists
const project = await prisma.project.findUnique({
- where: { id: projectIdHeader, deletedAt: null },
+ where: { id: projectId, deletedAt: null },
select: { id: true, orgId: true },
});
-
- if (!project) {
- throw { status: 404, message: "Project not found" };
- }
-
- // Return auth scope matching the regular auth structure
+ if (!project) throw { status: 404, message: "Project not found" };
return {
validKey: true,
scope: {
projectId: project.id,
- accessLevel: "project" as const,
+ accessLevel: "project",
orgId: project.orgId,
plan: "oss",
rateLimitOverrides: [],
- apiKeyId: "ADMIN_API_KEY", // Special identifier for audit logging
+ apiKeyId: "ADMIN_API_KEY",
publicKey: "ADMIN_API_KEY",
isIngestionSuspended: false,
isInAppAgentKey: false,
@@ -258,51 +260,28 @@ async function verifyAdminApiKeyAuth(req: NextApiRequest): Promise<
};
}
-/**
- * Verifies authentication for API routes with support for both regular API key
- * auth (Basic or Bearer) and admin API key auth.
- *
- * This is the main authentication entry point that delegates to either admin
- * or regular API key auth based on the configuration and request headers.
- *
- * @param req - The Next.js API request
- * @param isAdminApiKeyAuthAllowed - Whether to allow admin API key authentication
- * @param allowedAccessLevels - Access levels to accept for regular API key auth
- * @returns An auth scope object with the verified access level
- * @throws Error with appropriate status code if authentication fails
- */
-export async function verifyAuth(
- req: NextApiRequest,
- isAdminApiKeyAuthAllowed: boolean,
- allowedAccessLevels: RouteAccessLevel[] = ["project"],
- allowInAppAgentKey = false,
-): Promise<
- AuthHeaderValidVerificationResult & {
- scope: { projectId: string; accessLevel: RouteAccessLevel };
- }
-> {
- if (isAdminApiKeyAuthAllowed) {
- // Try admin API key authentication first
- const adminAuth = await verifyAdminApiKeyAuth(req);
- if (adminAuth) {
- // Admin auth succeeded
- return adminAuth;
- }
- // Admin auth not attempted, fall back to regular API key auth
- return await verifyApiKeyAuth(
- req.headers.authorization,
- allowedAccessLevels,
- allowInAppAgentKey,
- );
- }
+/** VerifyAuthParams is the request plus the route's action and legacy auth options. */
+export type VerifyAuthParams = {
+ req: NextApiRequest;
+ name: string;
+ action: ProjectAction;
+ isAdminApiKeyAuthAllowed?: boolean;
+ allowedAccessLevels?: RouteAccessLevel[];
+ allowInAppAgentKey?: boolean;
+};
- // Only regular API key auth is allowed
- return await verifyApiKeyAuth(
- req.headers.authorization,
- allowedAccessLevels,
- allowInAppAgentKey,
- );
-}
+/** VerifyAuthResult is the verified project scope the route handler receives. */
+type VerifyAuthResult = AuthHeaderValidVerificationResult & {
+ scope: { projectId: string; accessLevel: RouteAccessLevel };
+};
+
+/** ApiKeyPrincipal is the api-key variant of `Principal` the mapper consumes. */
+type ApiKeyPrincipal = Extract<Principal, { kind: "apiKey" }>;
+
+/** LegacyDecision is the legacy verify captured as a value: the verified scope, or the status + error to re-throw. */
+type LegacyDecision =
+ | { ok: true; status: 200; auth: VerifyAuthResult }
+ | { ok: false; status: number; error: unknown };
export const createAuthedProjectAPIRoute = <
TQuery extends ZodType<any>,
@@ -343,12 +322,14 @@ export const createAuthedProjectAPIRoute = <
// Verify authentication (API key or admin API key)
try {
- auth = await verifyAuth(
+ auth = await verifyAuth({
req,
- routeConfig.isAdminApiKeyAuthAllowed || false,
- routeConfig.allowedAccessLevels || ["project"],
- routeConfig.allowInAppAgentKey === true,
- );
+ name: routeConfig.name,
+ action: routeConfig.action,
+ isAdminApiKeyAuthAllowed: routeConfig.isAdminApiKeyAuthAllowed || false,
+ allowedAccessLevels: routeConfig.allowedAccessLevels || ["project"],
+ allowInAppAgentKey: routeConfig.allowInAppAgentKey === true,
+ });
} catch (error: any) {
if (isPrismaException(error)) {
traceException(error);
diff --git a/web/src/features/public-api/server/evaluation/evaluationRuleApiHandler.ts b/web/src/features/public-api/server/evaluation/evaluationRuleApiHandler.ts
index f8a087d6e1..9d032770a9 100644
--- a/web/src/features/public-api/server/evaluation/evaluationRuleApiHandler.ts
+++ b/web/src/features/public-api/server/evaluation/evaluationRuleApiHandler.ts
@@ -17,6 +17,7 @@ import {
export const evaluationRuleApiHandler = withStablePublicApiMiddlewares({
GET: createStablePublicApiRoute({
name: "Get evaluation rule",
+ action: "evaluationRule:read",
querySchema: EvaluationRuleIdQuery,
responseSchema: EvaluationRule,
fn: ({ query, auth }) =>
@@ -28,6 +29,7 @@ export const evaluationRuleApiHandler = withStablePublicApiMiddlewares({
}),
PATCH: createStablePublicApiRoute({
name: "Update evaluation rule",
+ action: "evaluationRule:CUD",
querySchema: EvaluationRuleIdQuery,
bodySchema: UpdateEvaluationRuleBody,
responseSchema: EvaluationRule,
@@ -41,6 +43,7 @@ export const evaluationRuleApiHandler = withStablePublicApiMiddlewares({
}),
DELETE: createStablePublicApiRoute({
name: "Delete evaluation rule",
+ action: "evaluationRule:CUD",
querySchema: EvaluationRuleIdQuery,
responseSchema: DeleteEvaluationRuleResponse,
fn: ({ query, auth }) =>
diff --git a/web/src/features/public-api/server/evaluation/evaluationRulesApiHandler.ts b/web/src/features/public-api/server/evaluation/evaluationRulesApiHandler.ts
index ec8910e300..46095a8ef4 100644
--- a/web/src/features/public-api/server/evaluation/evaluationRulesApiHandler.ts
+++ b/web/src/features/public-api/server/evaluation/evaluationRulesApiHandler.ts
@@ -16,6 +16,7 @@ import {
export const evaluationRulesApiHandler = withStablePublicApiMiddlewares({
GET: createStablePublicApiRoute({
name: "List evaluation rules",
+ action: "evaluationRule:read",
querySchema: ListEvaluationRulesQuery,
responseSchema: ListEvaluationRulesResponse,
fn: ({ query, auth }) =>
@@ -28,6 +29,7 @@ export const evaluationRulesApiHandler = withStablePublicApiMiddlewares({
}),
POST: createStablePublicApiRoute({
name: "Create evaluation rule",
+ action: "evaluationRule:CUD",
bodySchema: CreateEvaluationRuleBody,
responseSchema: EvaluationRule,
successStatusCode: 201,
diff --git a/web/src/features/public-api/server/evaluation/evaluatorApiHandler.ts b/web/src/features/public-api/server/evaluation/evaluatorApiHandler.ts
index 310b204b6e..b303cdf550 100644
--- a/web/src/features/public-api/server/evaluation/evaluatorApiHandler.ts
+++ b/web/src/features/public-api/server/evaluation/evaluatorApiHandler.ts
@@ -17,6 +17,7 @@ import {
export const evaluatorApiHandler = withStablePublicApiMiddlewares({
GET: createStablePublicApiRoute({
name: "Get evaluator",
+ action: "evaluator:read",
querySchema: EvaluatorIdQuery,
responseSchema: Evaluator,
fn: ({ query, auth }) =>
@@ -28,6 +29,7 @@ export const evaluatorApiHandler = withStablePublicApiMiddlewares({
}),
PATCH: createStablePublicApiRoute({
name: "Update evaluator",
+ action: "evaluator:CUD",
querySchema: EvaluatorIdQuery,
bodySchema: UpdateEvaluatorBody,
responseSchema: Evaluator,
@@ -41,6 +43,7 @@ export const evaluatorApiHandler = withStablePublicApiMiddlewares({
}),
DELETE: createStablePublicApiRoute({
name: "Delete evaluator",
+ action: "evaluator:CUD",
querySchema: EvaluatorIdQuery,
responseSchema: DeleteEvaluatorResponse,
fn: ({ query, auth }) =>
diff --git a/web/src/features/public-api/server/evaluation/evaluatorVersionsApiHandler.ts b/web/src/features/public-api/server/evaluation/evaluatorVersionsApiHandler.ts
index 77650d8acc..191894413b 100644
--- a/web/src/features/public-api/server/evaluation/evaluatorVersionsApiHandler.ts
+++ b/web/src/features/public-api/server/evaluation/evaluatorVersionsApiHandler.ts
@@ -11,6 +11,7 @@ import {
export const evaluatorVersionsApiHandler = withStablePublicApiMiddlewares({
GET: createStablePublicApiRoute({
name: "List evaluator versions",
+ action: "evaluator:read",
querySchema: ListEvaluatorVersionsQuery,
responseSchema: ListEvaluatorVersionsResponse,
fn: ({ query, auth }) =>
diff --git a/web/src/features/public-api/server/evaluation/evaluatorsApiHandler.ts b/web/src/features/public-api/server/evaluation/evaluatorsApiHandler.ts
index 5a6cceac47..75780cfb38 100644
--- a/web/src/features/public-api/server/evaluation/evaluatorsApiHandler.ts
+++ b/web/src/features/public-api/server/evaluation/evaluatorsApiHandler.ts
@@ -16,6 +16,7 @@ import {
export const evaluatorsApiHandler = withStablePublicApiMiddlewares({
GET: createStablePublicApiRoute({
name: "List evaluators",
+ action: "evaluator:read",
querySchema: ListEvaluatorsQuery,
responseSchema: ListEvaluatorsResponse,
fn: ({ query, auth }) =>
@@ -28,6 +29,7 @@ export const evaluatorsApiHandler = withStablePublicApiMiddlewares({
}),
POST: createStablePublicApiRoute({
name: "Create evaluator",
+ action: "evaluator:CUD",
bodySchema: CreateEvaluatorBody,
responseSchema: Evaluator,
successStatusCode: 201,
diff --git a/web/src/features/public-api/server/verifyProjectApiKeyAuth.ts b/web/src/features/public-api/server/verifyProjectApiKeyAuth.ts
new file mode 100644
index 0000000000..e13ce4f0e9
--- /dev/null
+++ b/web/src/features/public-api/server/verifyProjectApiKeyAuth.ts
@@ -0,0 +1,218 @@
+import crypto from "node:crypto";
+import { type NextApiRequest } from "next";
+import { ApiAuthService } from "@/src/features/public-api/server/apiAuth";
+import { prisma } from "@langfuse/shared/src/db";
+import {
+ redis,
+ type AuthHeaderValidVerificationResult,
+ type ApiAccessLevel,
+} from "@langfuse/shared/src/server";
+import { env } from "@/src/env.mjs";
+
+/** Access levels that can be accepted by project-scoped API routes. */
+export type RouteAccessLevel = Exclude<ApiAccessLevel, "organization">;
+
+/**
+ * Verifies API key authentication (Basic or Bearer) using ApiAuthService.
+ *
+ * Delegates to ApiAuthService.verifyAuthHeaderAndReturnScope which handles
+ * both Basic auth (public + secret key) and Bearer auth (public key only).
+ * The caller controls which access levels are accepted via allowedAccessLevels.
+ *
+ * @param authHeader - The Authorization header from the request
+ * @param allowedAccessLevels - Access levels to accept (default: ["project"])
+ * @returns An auth scope object with the verified access level
+ * @throws Error with appropriate message if authentication fails
+ */
+async function verifyApiKeyAuth(
+ authHeader: string | undefined,
+ allowedAccessLevels: RouteAccessLevel[] = ["project"],
+ allowInAppAgentKey = false,
+): Promise<
+ AuthHeaderValidVerificationResult & {
+ scope: { projectId: string; accessLevel: RouteAccessLevel };
+ }
+> {
+ const regularAuth = await new ApiAuthService(
+ prisma,
+ redis,
+ ).verifyAuthHeaderAndReturnScope(authHeader, { allowInAppAgentKey });
+
+ if (!regularAuth.validKey) {
+ throw { status: 401, message: regularAuth.error };
+ }
+
+ if (
+ !(allowedAccessLevels as ApiAccessLevel[]).includes(
+ regularAuth.scope.accessLevel,
+ )
+ ) {
+ throw {
+ status: 403,
+ message: "Access denied - insufficient permissions for this endpoint",
+ };
+ }
+
+ if (!regularAuth.scope.projectId) {
+ throw {
+ status: 403,
+ message:
+ "Project ID not found for API token. Are you using an organization key?",
+ };
+ }
+
+ return regularAuth as AuthHeaderValidVerificationResult & {
+ scope: { projectId: string; accessLevel: RouteAccessLevel };
+ };
+}
+
+/**
+ * Verifies admin API key authentication for self-hosted instances.
+ *
+ * This function checks if the request contains valid admin API key credentials:
+ * 1. Authorization header must be Bearer token format with ADMIN_API_KEY value
+ * 2. x-langfuse-admin-api-key header must match ADMIN_API_KEY env var exactly (for redundancy)
+ * 3. x-langfuse-project-id header must be present and specify a valid project ID
+ * 4. NEXT_PUBLIC_LANGFUSE_CLOUD_REGION must NOT be set (self-hosted instances only)
+ *
+ * The ADMIN_API_KEY must be set as an environment variable on the server.
+ * This authentication method is intended for administrative operations on self-hosted instances.
+ *
+ * @param req - The Next.js API request
+ * @returns An auth scope object if successful, null if admin auth is not being attempted
+ * @throws Error with appropriate status code if admin auth fails
+ */
+async function verifyAdminApiKeyAuth(req: NextApiRequest): Promise<
+ | (AuthHeaderValidVerificationResult & {
+ scope: { projectId: string; accessLevel: "project" };
+ })
+ | null
+> {
+ const authHeader = req.headers.authorization;
+ const adminApiKeyHeader = req.headers["x-langfuse-admin-api-key"];
+ const projectIdHeader = req.headers["x-langfuse-project-id"];
+
+ // If not attempting admin auth, return null to proceed with regular auth
+ if (!authHeader?.startsWith("Bearer ") || !adminApiKeyHeader) return null;
+
+ // Verify this is a self-hosted instance (not Langfuse Cloud)
+ if (env.NEXT_PUBLIC_LANGFUSE_CLOUD_REGION) {
+ throw {
+ status: 403,
+ message: "Admin API key auth is not available on Langfuse Cloud",
+ };
+ }
+
+ // Verify ADMIN_API_KEY is configured
+ const adminApiKey = env.ADMIN_API_KEY;
+ if (!adminApiKey) {
+ throw {
+ status: 500,
+ message: "Admin API key is not configured on this instance",
+ };
+ }
+
+ // Extract Bearer token
+ const bearerToken = authHeader.replace("Bearer ", "");
+
+ // 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(
+ Buffer.from(bearerToken),
+ Buffer.from(adminApiKey),
+ );
+ const headerEqual = crypto.timingSafeEqual(
+ Buffer.from(String(adminApiKeyHeader)),
+ Buffer.from(adminApiKey),
+ );
+ const isEqual = bearerTokenEqual && headerEqual;
+
+ if (!isEqual) throw Error();
+ } catch {
+ throw { status: 401, message: "Invalid admin API key" };
+ }
+
+ // Verify project ID header is present
+ if (!projectIdHeader || typeof projectIdHeader !== "string") {
+ throw {
+ status: 400,
+ message:
+ "x-langfuse-project-id header is required for admin API key authentication",
+ };
+ }
+
+ // Verify project exists
+ const project = await prisma.project.findUnique({
+ where: { id: projectIdHeader, deletedAt: null },
+ select: { id: true, orgId: true },
+ });
+
+ if (!project) {
+ throw { status: 404, message: "Project not found" };
+ }
+
+ // Return auth scope matching the regular auth structure
+ return {
+ validKey: true,
+ scope: {
+ projectId: project.id,
+ accessLevel: "project" as const,
+ orgId: project.orgId,
+ plan: "oss",
+ rateLimitOverrides: [],
+ apiKeyId: "ADMIN_API_KEY", // Special identifier for audit logging
+ publicKey: "ADMIN_API_KEY",
+ isIngestionSuspended: false,
+ isInAppAgentKey: false,
+ },
+ };
+}
+
+/**
+ * Verifies authentication for API routes with support for both regular API key
+ * auth (Basic or Bearer) and admin API key auth.
+ *
+ * This is the main authentication entry point that delegates to either admin
+ * or regular API key auth based on the configuration and request headers.
+ *
+ * @param req - The Next.js API request
+ * @param isAdminApiKeyAuthAllowed - Whether to allow admin API key authentication
+ * @param allowedAccessLevels - Access levels to accept for regular API key auth
+ * @returns An auth scope object with the verified access level
+ * @throws Error with appropriate status code if authentication fails
+ */
+export async function verifyAuth(
+ req: NextApiRequest,
+ isAdminApiKeyAuthAllowed: boolean,
+ allowedAccessLevels: RouteAccessLevel[] = ["project"],
+ allowInAppAgentKey = false,
+): Promise<
+ AuthHeaderValidVerificationResult & {
+ scope: { projectId: string; accessLevel: RouteAccessLevel };
+ }
+> {
+ if (isAdminApiKeyAuthAllowed) {
+ // Try admin API key authentication first
+ const adminAuth = await verifyAdminApiKeyAuth(req);
+ if (adminAuth) {
+ // Admin auth succeeded
+ return adminAuth;
+ }
+ // Admin auth not attempted, fall back to regular API key auth
+ return await verifyApiKeyAuth(
+ req.headers.authorization,
+ allowedAccessLevels,
+ allowInAppAgentKey,
+ );
+ }
+
+ // Only regular API key auth is allowed
+ return await verifyApiKeyAuth(
+ req.headers.authorization,
+ allowedAccessLevels,
+ allowInAppAgentKey,
+ );
+}
diff --git a/web/src/features/rbac/constants/apiKeyAccessRights.ts b/web/src/features/rbac/constants/apiKeyAccessRights.ts
new file mode 100644
index 0000000000..481359d36d
--- /dev/null
+++ b/web/src/features/rbac/constants/apiKeyAccessRights.ts
@@ -0,0 +1,56 @@
+import {
+ allOrganizationActions,
+ allProjectActions,
+ type SystemPolicy,
+} from "@/src/features/auth/policy/types";
+
+/** ApiKeyRole is the policy layer's role vocabulary for an API key, decoupled from the DB `ApiKeyScope` enum. */
+export type ApiKeyRole = "PROJECT" | "ORGANIZATION" | "SCORES_INGEST" | "ADMIN";
+
+/** apiKeyAccessRights maps each `ApiKeyRole` to its resource-less grants; the resolver binds each to the key's project/org, or the wildcard for ADMIN. */
+export const apiKeyAccessRights: Record<ApiKeyRole, SystemPolicy[]> = {
+ PROJECT: [
+ {
+ kind: "project",
+ source: { kind: "role", id: "PROJECT" },
+ effect: "allow",
+ actions: allProjectActions,
+ },
+ ],
+ ORGANIZATION: [
+ {
+ kind: "organization",
+ source: { kind: "role", id: "ORGANIZATION" },
+ effect: "allow",
+ actions: allOrganizationActions,
+ },
+ {
+ kind: "project",
+ source: { kind: "role", id: "ORGANIZATION" },
+ effect: "allow",
+ actions: ["project:read"],
+ },
+ ],
+ SCORES_INGEST: [
+ {
+ kind: "project",
+ source: { kind: "role", id: "SCORES_INGEST" },
+ effect: "allow",
+ actions: ["scores:create"],
+ },
+ ],
+ ADMIN: [
+ {
+ kind: "project",
+ source: { kind: "role", id: "ADMIN" },
+ effect: "allow",
+ actions: allProjectActions,
+ },
+ {
+ kind: "organization",
+ source: { kind: "role", id: "ADMIN" },
+ effect: "allow",
+ actions: allOrganizationActions,
+ },
+ ],
+};
diff --git a/web/src/features/rbac/constants/organizationAccessRights.ts b/web/src/features/rbac/constants/organizationAccessRights.ts
index 8563263087..a4ba13012d 100644
--- a/web/src/features/rbac/constants/organizationAccessRights.ts
+++ b/web/src/features/rbac/constants/organizationAccessRights.ts
@@ -1,7 +1,6 @@
import { type Role } from "@langfuse/shared/src/db";
-// eslint-disable-next-line @typescript-eslint/no-unused-vars -- Used via typeof
-const organizationScopes = [
+export const organizationScopes = [
"projects:create",
"projects:transfer_org",
"organization:CRUD_apiKeys",
@@ -10,7 +9,7 @@ const organizationScopes = [
"organizationMembers:read",
"organizationMembers:CUD",
"langfuseCloudBilling:CRUD",
- "auditLogs:read",
+ "orgAuditLogs:read",
] as const;
// type string of all Resource:Action, e.g. "organizationMembers:read"
@@ -26,7 +25,7 @@ export const organizationRoleAccessRights: Record<Role, OrganizationScope[]> = {
"organizationMembers:CUD",
"organizationMembers:read",
"langfuseCloudBilling:CRUD",
- "auditLogs:read",
+ "orgAuditLogs:read",
],
ADMIN: [
"projects:create",
@@ -34,7 +33,7 @@ export const organizationRoleAccessRights: Record<Role, OrganizationScope[]> = {
"organization:update",
"organizationMembers:CUD",
"organizationMembers:read",
- "auditLogs:read",
+ "orgAuditLogs:read",
],
MEMBER: ["organizationMembers:read"],
VIEWER: [],
diff --git a/web/src/pages/api/public/annotation-queues/[queueId]/assignments.ts b/web/src/pages/api/public/annotation-queues/[queueId]/assignments.ts
index 74aa7c8bea..7ed3c25a98 100644
--- a/web/src/pages/api/public/annotation-queues/[queueId]/assignments.ts
+++ b/web/src/pages/api/public/annotation-queues/[queueId]/assignments.ts
@@ -15,6 +15,7 @@ import {
export default withMiddlewares({
POST: createAuthedProjectAPIRoute({
name: "Create Annotation Queue Assignment",
+ action: "annotationQueueAssignments:CUD",
bodySchema: CreateAnnotationQueueAssignmentBody,
querySchema: AnnotationQueueAssignmentQuery,
responseSchema: CreateAnnotationQueueAssignmentResponse,
@@ -34,6 +35,7 @@ export default withMiddlewares({
DELETE: createAuthedProjectAPIRoute({
name: "Delete Annotation Queue Assignment",
+ action: "annotationQueueAssignments:CUD",
querySchema: AnnotationQueueAssignmentQuery,
bodySchema: DeleteAnnotationQueueAssignmentBody,
responseSchema: DeleteAnnotationQueueAssignmentResponse,
diff --git a/web/src/pages/api/public/annotation-queues/[queueId]/index.ts b/web/src/pages/api/public/annotation-queues/[queueId]/index.ts
index ef5f5f36c2..51c72db06a 100644
--- a/web/src/pages/api/public/annotation-queues/[queueId]/index.ts
+++ b/web/src/pages/api/public/annotation-queues/[queueId]/index.ts
@@ -9,6 +9,7 @@ import { getAnnotationQueueForApi } from "@/src/features/annotation-queues/serve
export default withMiddlewares({
GET: createAuthedProjectAPIRoute({
name: "Get annotation queue by ID",
+ action: "annotationQueues:read",
querySchema: GetAnnotationQueueByIdQuery,
responseSchema: GetAnnotationQueueByIdResponse,
rateLimitResource: "annotation-queues",
diff --git a/web/src/pages/api/public/annotation-queues/[queueId]/items/[itemId].ts b/web/src/pages/api/public/annotation-queues/[queueId]/items/[itemId].ts
index d13866aa56..16d35b2fa0 100644
--- a/web/src/pages/api/public/annotation-queues/[queueId]/items/[itemId].ts
+++ b/web/src/pages/api/public/annotation-queues/[queueId]/items/[itemId].ts
@@ -17,6 +17,7 @@ import {
export default withMiddlewares({
GET: createAuthedProjectAPIRoute({
name: "Get annotation queue item by ID",
+ action: "annotationQueues:read",
querySchema: GetAnnotationQueueItemByIdQuery,
responseSchema: GetAnnotationQueueItemByIdResponse,
rateLimitResource: "annotation-queues",
@@ -29,6 +30,7 @@ export default withMiddlewares({
}),
PATCH: createAuthedProjectAPIRoute({
name: "Update annotation queue item",
+ action: "annotationQueues:CUD",
querySchema: GetAnnotationQueueItemByIdQuery,
bodySchema: UpdateAnnotationQueueItemBody,
responseSchema: UpdateAnnotationQueueItemResponse,
@@ -44,6 +46,7 @@ export default withMiddlewares({
}),
DELETE: createAuthedProjectAPIRoute({
name: "Delete annotation queue item",
+ action: "annotationQueues:CUD",
querySchema: DeleteAnnotationQueueItemQuery,
responseSchema: DeleteAnnotationQueueItemResponse,
rateLimitResource: "annotation-queues",
diff --git a/web/src/pages/api/public/annotation-queues/[queueId]/items/index.ts b/web/src/pages/api/public/annotation-queues/[queueId]/items/index.ts
index 0343b76d65..872f2763eb 100644
--- a/web/src/pages/api/public/annotation-queues/[queueId]/items/index.ts
+++ b/web/src/pages/api/public/annotation-queues/[queueId]/items/index.ts
@@ -14,6 +14,7 @@ import {
export default withMiddlewares({
GET: createAuthedProjectAPIRoute({
name: "Get annotation queue items",
+ action: "annotationQueues:read",
querySchema: GetAnnotationQueueItemsQuery,
responseSchema: GetAnnotationQueueItemsResponse,
rateLimitResource: "annotation-queues",
@@ -28,6 +29,7 @@ export default withMiddlewares({
}),
POST: createAuthedProjectAPIRoute({
name: "Create annotation queue item",
+ action: "annotationQueues:CUD",
querySchema: GetAnnotationQueueItemsQuery,
bodySchema: CreateAnnotationQueueItemBody,
responseSchema: CreateAnnotationQueueItemResponse,
diff --git a/web/src/pages/api/public/annotation-queues/index.ts b/web/src/pages/api/public/annotation-queues/index.ts
index b806fdc973..93eebd0b36 100644
--- a/web/src/pages/api/public/annotation-queues/index.ts
+++ b/web/src/pages/api/public/annotation-queues/index.ts
@@ -14,6 +14,7 @@ import {
export default withMiddlewares({
GET: createAuthedProjectAPIRoute({
name: "Get annotation queues",
+ action: "annotationQueues:read",
querySchema: GetAnnotationQueuesQuery,
responseSchema: GetAnnotationQueuesResponse,
rateLimitResource: "annotation-queues",
@@ -27,6 +28,7 @@ export default withMiddlewares({
POST: createAuthedProjectAPIRoute({
name: "Create annotation queue",
+ action: "annotationQueues:CUD",
bodySchema: CreateAnnotationQueueBody,
responseSchema: CreateAnnotationQueueResponse,
rateLimitResource: "annotation-queues",
diff --git a/web/src/pages/api/public/comments/[commentId].ts b/web/src/pages/api/public/comments/[commentId].ts
index ff15415938..c91e02fa69 100644
--- a/web/src/pages/api/public/comments/[commentId].ts
+++ b/web/src/pages/api/public/comments/[commentId].ts
@@ -9,6 +9,7 @@ import {
export default withMiddlewares({
GET: createAuthedProjectAPIRoute({
name: "Get Comment",
+ action: "comments:read",
querySchema: GetCommentV1Query,
responseSchema: GetCommentV1Response,
fn: async ({ query, auth }) =>
diff --git a/web/src/pages/api/public/comments/index.ts b/web/src/pages/api/public/comments/index.ts
index 06cccd5e6c..b044223662 100644
--- a/web/src/pages/api/public/comments/index.ts
+++ b/web/src/pages/api/public/comments/index.ts
@@ -14,6 +14,7 @@ import {
export default withMiddlewares({
POST: createAuthedProjectAPIRoute({
name: "Create Comment",
+ action: "comments:CUD",
bodySchema: PostCommentsV1Body,
responseSchema: PostCommentsV1Response,
fn: async ({ body, auth }) =>
@@ -21,6 +22,7 @@ export default withMiddlewares({
}),
GET: createAuthedProjectAPIRoute({
name: "Get Comments",
+ action: "comments:read",
querySchema: GetCommentsV1Query,
responseSchema: GetCommentsV1Response,
fn: async ({ query, auth }) =>
diff --git a/web/src/pages/api/public/dataset-items/[datasetItemId].ts b/web/src/pages/api/public/dataset-items/[datasetItemId].ts
index 5c274c34e9..2c1181b241 100644
--- a/web/src/pages/api/public/dataset-items/[datasetItemId].ts
+++ b/web/src/pages/api/public/dataset-items/[datasetItemId].ts
@@ -14,6 +14,7 @@ import {
export default withMiddlewares({
GET: createAuthedProjectAPIRoute({
name: "Get Dataset Item",
+ action: "datasets:read",
querySchema: GetDatasetItemV1Query,
responseSchema: GetDatasetItemV1Response,
rateLimitResource: "datasets",
@@ -25,6 +26,7 @@ export default withMiddlewares({
}),
DELETE: createAuthedProjectAPIRoute({
name: "Delete Dataset Item",
+ action: "datasets:CUD",
querySchema: DeleteDatasetItemV1Query,
responseSchema: DeleteDatasetItemV1Response,
rateLimitResource: "datasets",
diff --git a/web/src/pages/api/public/dataset-items/index.ts b/web/src/pages/api/public/dataset-items/index.ts
index e5f706e001..d6afde1b15 100644
--- a/web/src/pages/api/public/dataset-items/index.ts
+++ b/web/src/pages/api/public/dataset-items/index.ts
@@ -22,6 +22,7 @@ export const config = {
export default withMiddlewares({
POST: createAuthedProjectAPIRoute({
name: "Create Dataset Item",
+ action: "datasets:CUD",
bodySchema: PostDatasetItemsV1Body,
responseSchema: PostDatasetItemsV1Response,
rateLimitResource: "datasets",
@@ -34,6 +35,7 @@ export default withMiddlewares({
}),
GET: createAuthedProjectAPIRoute({
name: "Get Dataset Items",
+ action: "datasets:read",
querySchema: GetDatasetItemsV1Query,
responseSchema: GetDatasetItemsV1Response,
rateLimitResource: "datasets",
diff --git a/web/src/pages/api/public/dataset-run-items.ts b/web/src/pages/api/public/dataset-run-items.ts
index 10c89eedaa..2f88a37a69 100644
--- a/web/src/pages/api/public/dataset-run-items.ts
+++ b/web/src/pages/api/public/dataset-run-items.ts
@@ -17,6 +17,7 @@ import { env } from "@/src/env.mjs";
export default withMiddlewares({
POST: createAuthedProjectAPIRoute({
name: "Create Dataset Run Item",
+ action: "datasets:CUD",
bodySchema: PostDatasetRunItemsV1Body,
responseSchema: PostDatasetRunItemsV1Response,
rateLimitResource: "datasets",
@@ -38,6 +39,7 @@ export default withMiddlewares({
}),
GET: createAuthedProjectAPIRoute({
name: "Get Dataset Run Items",
+ action: "datasets:read",
querySchema: GetDatasetRunItemsV1Query,
responseSchema: GetDatasetRunItemsV1Response,
deprecation: DATASET_RUN_ITEMS_DEPRECATION,
diff --git a/web/src/pages/api/public/datasets/[name]/index.ts b/web/src/pages/api/public/datasets/[name]/index.ts
index 396c95f7b2..c2e5522a6d 100644
--- a/web/src/pages/api/public/datasets/[name]/index.ts
+++ b/web/src/pages/api/public/datasets/[name]/index.ts
@@ -9,6 +9,7 @@ import { getDatasetByNameForApi } from "@/src/features/datasets/server/publicDat
export default withMiddlewares({
GET: createAuthedProjectAPIRoute({
name: "Get Dataset",
+ action: "datasets:read",
querySchema: GetDatasetV1Query,
responseSchema: GetDatasetV1Response,
rateLimitResource: "datasets",
diff --git a/web/src/pages/api/public/datasets/[name]/runs/[runName].ts b/web/src/pages/api/public/datasets/[name]/runs/[runName].ts
index 66d2cd8142..b2e0d32310 100644
--- a/web/src/pages/api/public/datasets/[name]/runs/[runName].ts
+++ b/web/src/pages/api/public/datasets/[name]/runs/[runName].ts
@@ -15,6 +15,7 @@ import { DATASET_RUNS_DEPRECATION } from "@/src/features/public-api/server/depre
export default withMiddlewares({
GET: createAuthedProjectAPIRoute({
name: "get-dataset-run",
+ action: "datasets:read",
querySchema: GetDatasetRunV1Query,
responseSchema: GetDatasetRunV1Response,
deprecation: DATASET_RUNS_DEPRECATION,
@@ -32,6 +33,7 @@ export default withMiddlewares({
}),
DELETE: createAuthedProjectAPIRoute({
name: "delete-dataset-run",
+ action: "datasets:CUD",
querySchema: DeleteDatasetRunV1Query,
responseSchema: DeleteDatasetRunV1Response,
rateLimitResource: "datasets",
diff --git a/web/src/pages/api/public/datasets/[name]/runs/index.ts b/web/src/pages/api/public/datasets/[name]/runs/index.ts
index 34815963a5..17048749ba 100644
--- a/web/src/pages/api/public/datasets/[name]/runs/index.ts
+++ b/web/src/pages/api/public/datasets/[name]/runs/index.ts
@@ -10,6 +10,7 @@ import { DATASET_RUNS_DEPRECATION } from "@/src/features/public-api/server/depre
export default withMiddlewares({
GET: createAuthedProjectAPIRoute({
name: "get-dataset-runs",
+ action: "datasets:read",
querySchema: GetDatasetRunsV1Query,
responseSchema: GetDatasetRunsV1Response,
deprecation: DATASET_RUNS_DEPRECATION,
diff --git a/web/src/pages/api/public/datasets/index.ts b/web/src/pages/api/public/datasets/index.ts
index 49cee11b1f..9b80654b5d 100644
--- a/web/src/pages/api/public/datasets/index.ts
+++ b/web/src/pages/api/public/datasets/index.ts
@@ -14,6 +14,7 @@ import {
export default withMiddlewares({
POST: createAuthedProjectAPIRoute({
name: "Create or Update Dataset",
+ action: "datasets:CUD",
bodySchema: PostDatasetsV1Body,
responseSchema: PostDatasetsV1Response,
rateLimitResource: "datasets",
@@ -33,6 +34,7 @@ export default withMiddlewares({
}),
GET: createAuthedProjectAPIRoute({
name: "Get Datasets",
+ action: "datasets:read",
querySchema: GetDatasetsV1Query,
responseSchema: GetDatasetsV1Response,
rateLimitResource: "datasets",
diff --git a/web/src/pages/api/public/events.ts b/web/src/pages/api/public/events.ts
index 50be748710..70c07f3b4a 100644
--- a/web/src/pages/api/public/events.ts
+++ b/web/src/pages/api/public/events.ts
@@ -15,6 +15,7 @@ import { v4 } from "uuid";
export default withMiddlewares({
POST: createAuthedProjectAPIRoute({
name: "Create Event",
+ action: "traces:create",
bodySchema: PostEventsV1Body,
responseSchema: PostEventsV1Response,
// Writes an observation-create event that lands in the legacy observations
diff --git a/web/src/pages/api/public/experiment-items/index.ts b/web/src/pages/api/public/experiment-items/index.ts
index 2de2ae2ded..c246ca15a0 100644
--- a/web/src/pages/api/public/experiment-items/index.ts
+++ b/web/src/pages/api/public/experiment-items/index.ts
@@ -13,6 +13,7 @@ import { clampToDataAccessDays } from "@/src/features/entitlements/server/hasEnt
export default withMiddlewares({
GET: createAuthedProjectAPIRoute({
name: "Get Experiment Items",
+ action: "experiments:read",
querySchema: GetExperimentItemsV1Query,
responseSchema: GetExperimentItemsV1Response,
allowInAppAgentKey: true,
diff --git a/web/src/pages/api/public/experiments/index.ts b/web/src/pages/api/public/experiments/index.ts
index 6aea69ec5c..674d797862 100644
--- a/web/src/pages/api/public/experiments/index.ts
+++ b/web/src/pages/api/public/experiments/index.ts
@@ -13,6 +13,7 @@ import { clampToDataAccessDays } from "@/src/features/entitlements/server/hasEnt
export default withMiddlewares({
GET: createAuthedProjectAPIRoute({
name: "Get Experiments",
+ action: "experiments:read",
querySchema: GetExperimentsV1Query,
responseSchema: GetExperimentsV1Response,
allowInAppAgentKey: true,
diff --git a/web/src/pages/api/public/feedback.ts b/web/src/pages/api/public/feedback.ts
index 964a6b49a0..33655253c5 100644
--- a/web/src/pages/api/public/feedback.ts
+++ b/web/src/pages/api/public/feedback.ts
@@ -9,6 +9,7 @@ import {
export default withMiddlewares({
POST: createAuthedProjectAPIRoute({
name: "Submit Feedback",
+ action: "feedback:create",
bodySchema: PostFeedbackBody,
responseSchema: PostFeedbackResponse,
successStatusCode: 201,
diff --git a/web/src/pages/api/public/generations.ts b/web/src/pages/api/public/generations.ts
index 0c6074a6a6..55f639f3f9 100644
--- a/web/src/pages/api/public/generations.ts
+++ b/web/src/pages/api/public/generations.ts
@@ -17,6 +17,7 @@ import { v4 } from "uuid";
export default withMiddlewares({
POST: createAuthedProjectAPIRoute({
name: "Create Generation (Legacy)",
+ action: "traces:create",
bodySchema: PostGenerationsV1Body,
responseSchema: PostGenerationsV1Response,
rateLimitResource: "legacy-ingestion",
@@ -61,6 +62,7 @@ export default withMiddlewares({
}),
PATCH: createAuthedProjectAPIRoute({
name: "Patch Generation (Legacy)",
+ action: "traces:create",
bodySchema: PatchGenerationsV1Body,
responseSchema: PatchGenerationsV1Response,
rateLimitResource: "legacy-ingestion",
diff --git a/web/src/pages/api/public/llm-connections/[id].ts b/web/src/pages/api/public/llm-connections/[id].ts
index 4e6ec91015..12ebe3cf51 100644
--- a/web/src/pages/api/public/llm-connections/[id].ts
+++ b/web/src/pages/api/public/llm-connections/[id].ts
@@ -18,6 +18,7 @@ import {
export default withMiddlewares({
DELETE: createAuthedProjectAPIRoute({
name: "Delete LLM Connection",
+ action: "llmApiKeys:delete",
querySchema: DeleteLlmConnectionV1Query,
responseSchema: DeleteLlmConnectionV1Response,
isAdminApiKeyAuthAllowed: true,
diff --git a/web/src/pages/api/public/llm-connections/index.ts b/web/src/pages/api/public/llm-connections/index.ts
index bdff918cd9..231bdbcb96 100644
--- a/web/src/pages/api/public/llm-connections/index.ts
+++ b/web/src/pages/api/public/llm-connections/index.ts
@@ -22,6 +22,7 @@ import { env } from "@/src/env.mjs";
export default withMiddlewares({
GET: createAuthedProjectAPIRoute({
name: "Get LLM Connections",
+ action: "llmApiKeys:read",
querySchema: GetLlmConnectionsV1Query,
responseSchema: GetLlmConnectionsV1Response,
isAdminApiKeyAuthAllowed: true,
@@ -79,6 +80,7 @@ export default withMiddlewares({
PUT: createAuthedProjectAPIRoute({
name: "Upsert LLM Connection",
+ action: "llmApiKeys:create",
bodySchema: PutLlmConnectionV1Body,
responseSchema: PutLlmConnectionV1Response,
isAdminApiKeyAuthAllowed: true,
diff --git a/web/src/pages/api/public/media/[mediaId].ts b/web/src/pages/api/public/media/[mediaId].ts
index cd0e317718..e2f765086d 100644
--- a/web/src/pages/api/public/media/[mediaId].ts
+++ b/web/src/pages/api/public/media/[mediaId].ts
@@ -16,6 +16,7 @@ import { ForbiddenError } from "@langfuse/shared";
export default withMiddlewares({
GET: createAuthedProjectAPIRoute({
name: "Get Media data",
+ action: "media:create",
querySchema: GetMediaQuerySchema,
responseSchema: GetMediaResponseSchema,
fn: async ({ query, auth }) => {
@@ -30,6 +31,7 @@ export default withMiddlewares({
PATCH: createAuthedProjectAPIRoute({
name: "Update Media Uploaded At",
+ action: "media:create",
querySchema: z.object({
mediaId: z.string(),
}),
diff --git a/web/src/pages/api/public/media/index.ts b/web/src/pages/api/public/media/index.ts
index ca514c664d..001f986f00 100644
--- a/web/src/pages/api/public/media/index.ts
+++ b/web/src/pages/api/public/media/index.ts
@@ -12,6 +12,7 @@ import { instrumentAsync } from "@langfuse/shared/src/server";
export default withMiddlewares({
POST: createAuthedProjectAPIRoute({
name: "Get Media Upload URL",
+ action: "media:create",
bodySchema: GetMediaUploadUrlQuerySchema,
responseSchema: GetMediaUploadUrlResponseSchema,
successStatusCode: 201,
diff --git a/web/src/pages/api/public/metrics/daily.ts b/web/src/pages/api/public/metrics/daily.ts
index 6ef9b6116e..86b626841d 100644
--- a/web/src/pages/api/public/metrics/daily.ts
+++ b/web/src/pages/api/public/metrics/daily.ts
@@ -14,6 +14,7 @@ import { clampToDataAccessDays } from "@/src/features/entitlements/server/hasEnt
export default withMiddlewares({
GET: createAuthedProjectAPIRoute({
name: "Get Daily Metrics",
+ action: "metrics:read",
querySchema: GetMetricsDailyV1Query,
responseSchema: GetMetricsDailyV1Response,
deprecation: METRICS_DEPRECATION,
diff --git a/web/src/pages/api/public/metrics/index.ts b/web/src/pages/api/public/metrics/index.ts
index 96c3fd2981..c4ebfee92d 100644
--- a/web/src/pages/api/public/metrics/index.ts
+++ b/web/src/pages/api/public/metrics/index.ts
@@ -15,6 +15,7 @@ export default withMiddlewares(
{
GET: createAuthedProjectAPIRoute({
name: "Get Metrics",
+ action: "metrics:read",
rateLimitResource: "public-api-metrics",
querySchema: GetMetricsV1Query,
responseSchema: GetMetricsV1Response,
diff --git a/web/src/pages/api/public/models/[modelId]/index.ts b/web/src/pages/api/public/models/[modelId]/index.ts
index 544928ab10..e0a8faba90 100644
--- a/web/src/pages/api/public/models/[modelId]/index.ts
+++ b/web/src/pages/api/public/models/[modelId]/index.ts
@@ -17,6 +17,7 @@ import {
export default withMiddlewares({
GET: createAuthedProjectAPIRoute({
name: "Get model definitions",
+ action: "models:read",
isAdminApiKeyAuthAllowed: true,
querySchema: GetModelV1Query,
responseSchema: GetModelV1Response,
@@ -30,6 +31,7 @@ export default withMiddlewares({
PUT: createAuthedProjectAPIRoute({
name: "Upsert custom model definition",
+ action: "models:CUD",
isAdminApiKeyAuthAllowed: true,
querySchema: GetModelV1Query,
bodySchema: PostModelsV1Body,
@@ -46,6 +48,7 @@ export default withMiddlewares({
DELETE: createAuthedProjectAPIRoute({
name: "Delete model",
+ action: "models:CUD",
isAdminApiKeyAuthAllowed: true,
querySchema: DeleteModelV1Query,
responseSchema: DeleteModelV1Response,
diff --git a/web/src/pages/api/public/models/index.ts b/web/src/pages/api/public/models/index.ts
index fc91c35049..b6900dbbc8 100644
--- a/web/src/pages/api/public/models/index.ts
+++ b/web/src/pages/api/public/models/index.ts
@@ -14,6 +14,7 @@ import {
export default withMiddlewares({
GET: createAuthedProjectAPIRoute({
name: "Get model definitions",
+ action: "models:read",
isAdminApiKeyAuthAllowed: true,
querySchema: GetModelsV1Query,
responseSchema: GetModelsV1Response,
@@ -28,6 +29,7 @@ export default withMiddlewares({
POST: createAuthedProjectAPIRoute({
name: "Create custom model definition",
+ action: "models:CUD",
isAdminApiKeyAuthAllowed: true,
bodySchema: PostModelsV1Body,
responseSchema: PostModelsV1Response,
diff --git a/web/src/pages/api/public/observations/[observationId].ts b/web/src/pages/api/public/observations/[observationId].ts
index 84e5b3350e..f40b4bffb4 100644
--- a/web/src/pages/api/public/observations/[observationId].ts
+++ b/web/src/pages/api/public/observations/[observationId].ts
@@ -22,6 +22,7 @@ export default withMiddlewares(
{
GET: createAuthedProjectAPIRoute({
name: "Get Observation",
+ action: "traces:read",
allowInAppAgentKey: true,
rateLimitResource: "public-api-legacy",
querySchema: GetObservationV1Query,
diff --git a/web/src/pages/api/public/observations/index.ts b/web/src/pages/api/public/observations/index.ts
index 12e6cf600f..4ba7bcb30c 100644
--- a/web/src/pages/api/public/observations/index.ts
+++ b/web/src/pages/api/public/observations/index.ts
@@ -27,6 +27,7 @@ export default withMiddlewares(
{
GET: createAuthedProjectAPIRoute({
name: "Get Observations",
+ action: "traces:read",
allowInAppAgentKey: true,
rateLimitResource: "public-api-legacy",
querySchema: GetObservationsV1Query,
diff --git a/web/src/pages/api/public/organizations/projects/index.ts b/web/src/pages/api/public/organizations/projects/index.ts
index 7af7d53ab3..114f6b9c90 100644
--- a/web/src/pages/api/public/organizations/projects/index.ts
+++ b/web/src/pages/api/public/organizations/projects/index.ts
@@ -1,3 +1,5 @@
+import { type IncomingHttpHeaders } from "http";
+
import { ApiAuthService } from "@/src/features/public-api/server/apiAuth";
import { cors, runMiddleware } from "@/src/features/public-api/server/cors";
import { prisma } from "@langfuse/shared/src/db";
@@ -7,6 +9,15 @@ import { handleGetProjects } from "@/src/ee/features/admin-api/server/projects";
import { type NextApiRequest, type NextApiResponse } from "next";
import { hasEntitlementBasedOnPlan } from "@/src/features/entitlements/server/hasEntitlement";
+import { env } from "@/src/env.mjs";
+import { authenticator } from "@/src/features/apiKey/authenticator";
+import { authorize } from "@/src/features/auth/policy/authorize";
+import {
+ diffResults,
+ legacyFromStatus,
+ recordCoverage,
+ type NewResult,
+} from "@/src/features/auth/policy/shadow";
export default async function handler(
req: NextApiRequest,
@@ -57,6 +68,25 @@ export default async function handler(
});
}
+ // Route the project:read decision through the policy core. Legacy skips the
+ // pipeline; shadow records parity without changing behavior; enforce gates.
+ if (env.API_AUTH_MIGRATION !== "legacy") {
+ const authz = await authorizeOrgProjectsRead(req.headers);
+ if (env.API_AUTH_MIGRATION === "shadow") {
+ recordCoverage("List Projects");
+ diffResults(authz, legacyFromStatus(200), {
+ seam: "org_route",
+ action: "project:read",
+ });
+ }
+ if (env.API_AUTH_MIGRATION === "enforce" && !authz.success) {
+ return res.status(403).json({
+ error:
+ "Invalid API key. Organization-scoped API key required for this operation.",
+ });
+ }
+ }
+
const rateLimitCheck = await RateLimitService.getInstance().rateLimitRequest(
authCheck.scope,
"public-api",
@@ -78,3 +108,26 @@ export default async function handler(
});
}
}
+
+/** authorizeOrgProjectsRead resolves the request's context and checks project:read across the org's projects (vacuously allowing an org with none). */
+async function authorizeOrgProjectsRead(
+ headers: IncomingHttpHeaders,
+): Promise<NewResult> {
+ const authn = await authenticator.authenticate({ headers });
+ if (!authn.success) {
+ return { success: false, error: { httpCode: authn.error.httpCode } };
+ }
+ const context = authn.context;
+ const orgProjectIds =
+ context.principal.kind === "apiKey"
+ ? context.principal.organizations.flatMap((o) => o.projectIds)
+ : [];
+ const allowed =
+ orgProjectIds.length === 0 ||
+ orgProjectIds.some(
+ (projectId) => authorize(context, "project:read", { projectId }).success,
+ );
+ return allowed
+ ? { success: true }
+ : { success: false, error: { httpCode: 403 } };
+}
diff --git a/web/src/pages/api/public/otel/v1/metrics/index.ts b/web/src/pages/api/public/otel/v1/metrics/index.ts
index 652460ad12..f9570f1b46 100644
--- a/web/src/pages/api/public/otel/v1/metrics/index.ts
+++ b/web/src/pages/api/public/otel/v1/metrics/index.ts
@@ -11,6 +11,7 @@ export const config = {
export default withMiddlewares({
POST: createAuthedProjectAPIRoute({
name: "OTel Metrics",
+ action: "traces:create",
querySchema: z.any(),
responseSchema: z.any(),
rateLimitResource: "ingestion",
diff --git a/web/src/pages/api/public/otel/v1/traces/index.ts b/web/src/pages/api/public/otel/v1/traces/index.ts
index be98cc8b55..59e4bf8fe2 100644
--- a/web/src/pages/api/public/otel/v1/traces/index.ts
+++ b/web/src/pages/api/public/otel/v1/traces/index.ts
@@ -26,6 +26,7 @@ export const config = {
export default withMiddlewares({
POST: createAuthedProjectAPIRoute({
name: "OTel Traces",
+ action: "traces:create",
querySchema: z.any(),
responseSchema: z.any(),
rateLimitResource: "ingestion",
diff --git a/web/src/pages/api/public/score-configs/[configId].ts b/web/src/pages/api/public/score-configs/[configId].ts
index e037b43dc1..0ddae667e1 100644
--- a/web/src/pages/api/public/score-configs/[configId].ts
+++ b/web/src/pages/api/public/score-configs/[configId].ts
@@ -15,6 +15,7 @@ import {
export default withMiddlewares({
GET: createAuthedProjectAPIRoute({
name: "Get a Score Config",
+ action: "scoreConfigs:read",
querySchema: GetScoreConfigQuery,
responseSchema: GetScoreConfigResponse,
fn: async ({ query, auth }) => {
@@ -26,6 +27,7 @@ export default withMiddlewares({
}),
PATCH: createAuthedProjectAPIRoute({
name: "Update a Score Config",
+ action: "scoreConfigs:CUD",
querySchema: PatchScoreConfigQuery,
bodySchema: PatchScoreConfigBody,
responseSchema: PatchScoreConfigResponse,
diff --git a/web/src/pages/api/public/score-configs/index.ts b/web/src/pages/api/public/score-configs/index.ts
index 1b4c6614a9..bde23eb19f 100644
--- a/web/src/pages/api/public/score-configs/index.ts
+++ b/web/src/pages/api/public/score-configs/index.ts
@@ -14,6 +14,7 @@ import {
export default withMiddlewares({
POST: createAuthedProjectAPIRoute({
name: "Create Score Config",
+ action: "scoreConfigs:CUD",
bodySchema: PostScoreConfigBody,
responseSchema: PostScoreConfigResponse,
fn: async ({ body, auth }) => {
@@ -25,6 +26,7 @@ export default withMiddlewares({
}),
GET: createAuthedProjectAPIRoute({
name: "Get Score Configs",
+ action: "scoreConfigs:read",
querySchema: GetScoreConfigsQuery,
responseSchema: GetScoreConfigsResponse,
fn: async ({ query, auth }) => {
diff --git a/web/src/pages/api/public/scores/[scoreId].ts b/web/src/pages/api/public/scores/[scoreId].ts
index 84b26ad471..87ae7c8047 100644
--- a/web/src/pages/api/public/scores/[scoreId].ts
+++ b/web/src/pages/api/public/scores/[scoreId].ts
@@ -15,6 +15,7 @@ import { SCORES_DEPRECATION } from "@/src/features/public-api/server/deprecation
export default withMiddlewares({
GET: createAuthedProjectAPIRoute({
name: "Get Score",
+ action: "scores:read",
querySchema: GetScoreQueryV1,
responseSchema: GetScoreResponseV1,
deprecation: SCORES_DEPRECATION,
@@ -43,6 +44,7 @@ export default withMiddlewares({
}),
DELETE: createAuthedProjectAPIRoute({
name: "Delete Score",
+ action: "scores:CUD",
querySchema: DeleteScoreQueryV1,
responseSchema: DeleteScoreResponseV1,
rateLimitResource: "score-delete",
diff --git a/web/src/pages/api/public/scores/index.ts b/web/src/pages/api/public/scores/index.ts
index 3c72c1a9ee..eebef065b1 100644
--- a/web/src/pages/api/public/scores/index.ts
+++ b/web/src/pages/api/public/scores/index.ts
@@ -20,6 +20,7 @@ import { clampToDataAccessDays } from "@/src/features/entitlements/server/hasEnt
export default withMiddlewares({
POST: createAuthedProjectAPIRoute({
name: "Create Score",
+ action: "scores:create",
bodySchema: PostScoresBodyV1,
responseSchema: PostScoresResponseV1,
allowedAccessLevels: ["project", "scores"],
@@ -63,6 +64,7 @@ export default withMiddlewares({
}),
GET: createAuthedProjectAPIRoute({
name: "/api/public/scores",
+ action: "scores:read",
querySchema: GetScoresQueryV1,
responseSchema: GetScoresResponseV1,
deprecation: SCORES_DEPRECATION,
diff --git a/web/src/pages/api/public/sessions/[sessionId].ts b/web/src/pages/api/public/sessions/[sessionId].ts
index 716bba36ba..b7530f45b3 100644
--- a/web/src/pages/api/public/sessions/[sessionId].ts
+++ b/web/src/pages/api/public/sessions/[sessionId].ts
@@ -13,6 +13,7 @@ import { SESSIONS_DEPRECATION } from "@/src/features/public-api/server/deprecati
export default withMiddlewares({
GET: createAuthedProjectAPIRoute({
name: "Get Session",
+ action: "sessions:read",
deprecation: SESSIONS_DEPRECATION,
rateLimitResource: "public-api-legacy",
querySchema: GetSessionV1Query,
diff --git a/web/src/pages/api/public/sessions/index.ts b/web/src/pages/api/public/sessions/index.ts
index 2732e3834f..0e68d9b1e8 100644
--- a/web/src/pages/api/public/sessions/index.ts
+++ b/web/src/pages/api/public/sessions/index.ts
@@ -12,6 +12,7 @@ import { clampToDataAccessDays } from "@/src/features/entitlements/server/hasEnt
export default withMiddlewares({
GET: createAuthedProjectAPIRoute({
name: "Get Sessions",
+ action: "sessions:read",
deprecation: SESSIONS_DEPRECATION,
rateLimitResource: "public-api-legacy",
querySchema: GetSessionsV1Query,
diff --git a/web/src/pages/api/public/spans.ts b/web/src/pages/api/public/spans.ts
index 46fdc849c6..e1b522053a 100644
--- a/web/src/pages/api/public/spans.ts
+++ b/web/src/pages/api/public/spans.ts
@@ -17,6 +17,7 @@ import { v4 } from "uuid";
export default withMiddlewares({
POST: createAuthedProjectAPIRoute({
name: "Create Span (Legacy)",
+ action: "traces:create",
bodySchema: PostSpansV1Body,
responseSchema: PostSpansV1Response,
// Writes an observation-create event that lands in the legacy observations
@@ -57,6 +58,7 @@ export default withMiddlewares({
}),
PATCH: createAuthedProjectAPIRoute({
name: "Update Span (Legacy)",
+ action: "traces:create",
bodySchema: PatchSpansV1Body,
responseSchema: PatchSpansV1Response,
rejectInEventsOnlyMode: true,
diff --git a/web/src/pages/api/public/traces/[traceId].ts b/web/src/pages/api/public/traces/[traceId].ts
index b1fddf60f9..f4e7988c59 100644
--- a/web/src/pages/api/public/traces/[traceId].ts
+++ b/web/src/pages/api/public/traces/[traceId].ts
@@ -34,6 +34,7 @@ export default withMiddlewares(
{
GET: createAuthedProjectAPIRoute({
name: "Get Single Trace",
+ action: "traces:read",
deprecation: TRACES_DEPRECATION,
rateLimitResource: "public-api-legacy",
querySchema: GetTraceV1Query,
@@ -198,6 +199,7 @@ export default withMiddlewares(
DELETE: createAuthedProjectAPIRoute({
name: "Delete Single Trace",
+ action: "traces:delete",
querySchema: DeleteTraceV1Query,
responseSchema: DeleteTraceV1Response,
rateLimitResource: "trace-delete",
diff --git a/web/src/pages/api/public/traces/index.ts b/web/src/pages/api/public/traces/index.ts
index 2fa2b6ad28..cc739c482e 100644
--- a/web/src/pages/api/public/traces/index.ts
+++ b/web/src/pages/api/public/traces/index.ts
@@ -39,6 +39,7 @@ export default withMiddlewares(
{
POST: createAuthedProjectAPIRoute({
name: "Create Trace (Legacy)",
+ action: "traces:create",
bodySchema: PostTracesV1Body,
responseSchema: PostTracesV1Response, // Adjust this if you have a specific response schema
rateLimitResource: "legacy-ingestion",
@@ -79,6 +80,7 @@ export default withMiddlewares(
GET: createAuthedProjectAPIRoute({
name: "Get Traces",
+ action: "traces:read",
rateLimitResource: "public-api-legacy",
querySchema: GetTracesV1Query,
responseSchema: GetTracesV1Response,
@@ -226,6 +228,7 @@ export default withMiddlewares(
DELETE: createAuthedProjectAPIRoute({
name: "Delete Multiple Traces",
+ action: "traces:delete",
bodySchema: DeleteTracesV1Body,
responseSchema: DeleteTracesV1Response,
rateLimitResource: "trace-delete",
diff --git a/web/src/pages/api/public/unstable/dashboard-widgets/[widgetId].ts b/web/src/pages/api/public/unstable/dashboard-widgets/[widgetId].ts
index 4d5a0a245e..a1ef48ac2c 100644
--- a/web/src/pages/api/public/unstable/dashboard-widgets/[widgetId].ts
+++ b/web/src/pages/api/public/unstable/dashboard-widgets/[widgetId].ts
@@ -18,6 +18,7 @@ import {
export default withUnstablePublicApiMiddlewares({
GET: createUnstablePublicApiRoute({
name: "Get Unstable Dashboard Widget",
+ action: "dashboards:read",
querySchema: DashboardWidgetIdQuery,
responseSchema: GetUnstableDashboardWidgetResponse,
fn: ({ query, auth }) =>
@@ -28,6 +29,7 @@ export default withUnstablePublicApiMiddlewares({
}),
PATCH: createUnstablePublicApiRoute({
name: "Update Unstable Dashboard Widget",
+ action: "dashboards:CUD",
querySchema: DashboardWidgetIdQuery,
bodySchema: PatchUnstableDashboardWidgetBody,
responseSchema: PatchUnstableDashboardWidgetResponse,
@@ -41,6 +43,7 @@ export default withUnstablePublicApiMiddlewares({
}),
DELETE: createUnstablePublicApiRoute({
name: "Delete Unstable Dashboard Widget",
+ action: "dashboards:CUD",
querySchema: DashboardWidgetIdQuery,
responseSchema: DeleteUnstableDashboardWidgetResponse,
fn: async ({ query, auth }) => {
diff --git a/web/src/pages/api/public/unstable/dashboard-widgets/index.ts b/web/src/pages/api/public/unstable/dashboard-widgets/index.ts
index a7aaf39c2a..5cae24f2d4 100644
--- a/web/src/pages/api/public/unstable/dashboard-widgets/index.ts
+++ b/web/src/pages/api/public/unstable/dashboard-widgets/index.ts
@@ -16,6 +16,7 @@ import {
export default withUnstablePublicApiMiddlewares({
GET: createUnstablePublicApiRoute({
name: "List Unstable Dashboard Widgets",
+ action: "dashboards:read",
querySchema: GetUnstableDashboardWidgetsQuery,
responseSchema: GetUnstableDashboardWidgetsResponse,
fn: ({ query, auth }) =>
@@ -23,6 +24,7 @@ export default withUnstablePublicApiMiddlewares({
}),
POST: createUnstablePublicApiRoute({
name: "Create Unstable Dashboard Widget",
+ action: "dashboards:CUD",
bodySchema: PostUnstableDashboardWidgetBody,
responseSchema: PostUnstableDashboardWidgetResponse,
fn: async ({ body, auth }) =>
diff --git a/web/src/pages/api/public/unstable/dashboards/[dashboardId].ts b/web/src/pages/api/public/unstable/dashboards/[dashboardId].ts
index 7154e403a9..22ce67209f 100644
--- a/web/src/pages/api/public/unstable/dashboards/[dashboardId].ts
+++ b/web/src/pages/api/public/unstable/dashboards/[dashboardId].ts
@@ -18,6 +18,7 @@ import {
export default withUnstablePublicApiMiddlewares({
GET: createUnstablePublicApiRoute({
name: "Get Unstable Dashboard",
+ action: "dashboards:read",
querySchema: DashboardIdQuery,
responseSchema: GetUnstableDashboardResponse,
fn: ({ query, auth }) =>
@@ -28,6 +29,7 @@ export default withUnstablePublicApiMiddlewares({
}),
PATCH: createUnstablePublicApiRoute({
name: "Update Unstable Dashboard",
+ action: "dashboards:CUD",
querySchema: DashboardIdQuery,
bodySchema: PatchUnstableDashboardBody,
responseSchema: PatchUnstableDashboardResponse,
@@ -41,6 +43,7 @@ export default withUnstablePublicApiMiddlewares({
}),
DELETE: createUnstablePublicApiRoute({
name: "Delete Unstable Dashboard",
+ action: "dashboards:CUD",
querySchema: DashboardIdQuery,
responseSchema: DeleteUnstableDashboardResponse,
fn: async ({ query, auth }) => {
diff --git a/web/src/pages/api/public/unstable/dashboards/[dashboardId]/placements/[placementId].ts b/web/src/pages/api/public/unstable/dashboards/[dashboardId]/placements/[placementId].ts
index c409a86bab..c075887b72 100644
--- a/web/src/pages/api/public/unstable/dashboards/[dashboardId]/placements/[placementId].ts
+++ b/web/src/pages/api/public/unstable/dashboards/[dashboardId]/placements/[placementId].ts
@@ -16,6 +16,7 @@ import {
export default withUnstablePublicApiMiddlewares({
PATCH: createUnstablePublicApiRoute({
name: "Update Unstable Dashboard Placement",
+ action: "dashboards:CUD",
querySchema: DashboardPlacementQuery,
bodySchema: PatchDashboardPlacementBody,
responseSchema: PatchDashboardPlacementResponse,
@@ -29,6 +30,7 @@ export default withUnstablePublicApiMiddlewares({
}),
DELETE: createUnstablePublicApiRoute({
name: "Delete Unstable Dashboard Placement",
+ action: "dashboards:CUD",
querySchema: DashboardPlacementQuery,
responseSchema: DeleteDashboardPlacementResponse,
fn: ({ query, auth }) =>
diff --git a/web/src/pages/api/public/unstable/dashboards/[dashboardId]/placements/index.ts b/web/src/pages/api/public/unstable/dashboards/[dashboardId]/placements/index.ts
index e8198be2fd..2189ce47a7 100644
--- a/web/src/pages/api/public/unstable/dashboards/[dashboardId]/placements/index.ts
+++ b/web/src/pages/api/public/unstable/dashboards/[dashboardId]/placements/index.ts
@@ -12,6 +12,7 @@ import { addPublicDashboardPlacement } from "@/src/features/dashboard/server/pub
export default withUnstablePublicApiMiddlewares({
POST: createUnstablePublicApiRoute({
name: "Add Unstable Dashboard Placement",
+ action: "dashboards:CUD",
querySchema: DashboardIdQuery,
bodySchema: PostDashboardPlacementBody,
responseSchema: PostDashboardPlacementResponse,
diff --git a/web/src/pages/api/public/unstable/dashboards/index.ts b/web/src/pages/api/public/unstable/dashboards/index.ts
index f03808f646..da3b2e550f 100644
--- a/web/src/pages/api/public/unstable/dashboards/index.ts
+++ b/web/src/pages/api/public/unstable/dashboards/index.ts
@@ -16,6 +16,7 @@ import {
export default withUnstablePublicApiMiddlewares({
GET: createUnstablePublicApiRoute({
name: "List Unstable Dashboards",
+ action: "dashboards:read",
querySchema: GetUnstableDashboardsQuery,
responseSchema: GetUnstableDashboardsResponse,
fn: ({ query, auth }) =>
@@ -23,6 +24,7 @@ export default withUnstablePublicApiMiddlewares({
}),
POST: createUnstablePublicApiRoute({
name: "Create Unstable Dashboard",
+ action: "dashboards:CUD",
bodySchema: PostUnstableDashboardBody,
responseSchema: PostUnstableDashboardResponse,
fn: ({ body, auth }) =>
diff --git a/web/src/pages/api/public/unstable/evaluation-rules/[evaluationRuleId].ts b/web/src/pages/api/public/unstable/evaluation-rules/[evaluationRuleId].ts
index 74f4c94951..a0c9571d32 100644
--- a/web/src/pages/api/public/unstable/evaluation-rules/[evaluationRuleId].ts
+++ b/web/src/pages/api/public/unstable/evaluation-rules/[evaluationRuleId].ts
@@ -20,6 +20,7 @@ import {
export default withUnstablePublicApiMiddlewares({
GET: createUnstablePublicApiRoute({
name: "Get Unstable Evaluation Rule",
+ action: "evaluationRule:read",
querySchema: GetUnstableEvaluationRuleQuery,
responseSchema: GetUnstableEvaluationRuleResponse,
fn: async ({ query, auth }) =>
@@ -30,6 +31,7 @@ export default withUnstablePublicApiMiddlewares({
}),
PATCH: createUnstablePublicApiRoute({
name: "Update Unstable Evaluation Rule",
+ action: "evaluationRule:CUD",
querySchema: PatchUnstableEvaluationRuleQuery,
bodySchema: PatchUnstableEvaluationRuleBody,
responseSchema: PatchUnstableEvaluationRuleResponse,
@@ -44,6 +46,7 @@ export default withUnstablePublicApiMiddlewares({
}),
DELETE: createUnstablePublicApiRoute({
name: "Delete Unstable Evaluation Rule",
+ action: "evaluationRule:CUD",
querySchema: DeleteUnstableEvaluationRuleQuery,
responseSchema: DeleteUnstableEvaluationRuleResponse,
fn: async ({ query, auth }) => {
diff --git a/web/src/pages/api/public/unstable/evaluation-rules/index.ts b/web/src/pages/api/public/unstable/evaluation-rules/index.ts
index cd80772165..fde221ab97 100644
--- a/web/src/pages/api/public/unstable/evaluation-rules/index.ts
+++ b/web/src/pages/api/public/unstable/evaluation-rules/index.ts
@@ -16,6 +16,7 @@ import {
export default withUnstablePublicApiMiddlewares({
GET: createUnstablePublicApiRoute({
name: "List Unstable Evaluation Rules",
+ action: "evaluationRule:read",
querySchema: GetUnstableEvaluationRulesQuery,
responseSchema: GetUnstableEvaluationRulesResponse,
fn: async ({ query, auth }) =>
@@ -27,6 +28,7 @@ export default withUnstablePublicApiMiddlewares({
}),
POST: createUnstablePublicApiRoute({
name: "Create Unstable Evaluation Rule",
+ action: "evaluationRule:CUD",
bodySchema: PostUnstableEvaluationRuleBody,
responseSchema: PostUnstableEvaluationRuleResponse,
fn: async ({ body, auth }) =>
diff --git a/web/src/pages/api/public/unstable/evaluators/[evaluatorId].ts b/web/src/pages/api/public/unstable/evaluators/[evaluatorId].ts
index 43ab658f59..a3ca669476 100644
--- a/web/src/pages/api/public/unstable/evaluators/[evaluatorId].ts
+++ b/web/src/pages/api/public/unstable/evaluators/[evaluatorId].ts
@@ -16,6 +16,7 @@ import {
export default withUnstablePublicApiMiddlewares({
GET: createUnstablePublicApiRoute({
name: "Get Unstable Evaluator",
+ action: "evaluator:read",
querySchema: GetUnstableEvaluatorQuery,
responseSchema: GetUnstableEvaluatorResponse,
fn: async ({ query, auth }) =>
@@ -26,6 +27,7 @@ export default withUnstablePublicApiMiddlewares({
}),
DELETE: createUnstablePublicApiRoute({
name: "Delete Unstable Evaluator",
+ action: "evaluator:CUD",
querySchema: DeleteUnstableEvaluatorQuery,
responseSchema: DeleteUnstableEvaluatorResponse,
fn: async ({ query, auth }) => {
diff --git a/web/src/pages/api/public/unstable/evaluators/index.ts b/web/src/pages/api/public/unstable/evaluators/index.ts
index 073b4c4d16..711e4718c9 100644
--- a/web/src/pages/api/public/unstable/evaluators/index.ts
+++ b/web/src/pages/api/public/unstable/evaluators/index.ts
@@ -16,6 +16,7 @@ import {
export default withUnstablePublicApiMiddlewares({
GET: createUnstablePublicApiRoute({
name: "List Unstable Evaluators",
+ action: "evaluator:read",
querySchema: GetUnstableEvaluatorsQuery,
responseSchema: GetUnstableEvaluatorsResponse,
fn: async ({ query, auth }) =>
@@ -27,6 +28,7 @@ export default withUnstablePublicApiMiddlewares({
}),
POST: createUnstablePublicApiRoute({
name: "Create Unstable Evaluator",
+ action: "evaluator:CUD",
bodySchema: PostUnstableEvaluatorBody,
responseSchema: PostUnstableEvaluatorResponse,
fn: async ({ body, auth }) =>
diff --git a/web/src/pages/api/public/v2/datasets/[datasetName]/index.ts b/web/src/pages/api/public/v2/datasets/[datasetName]/index.ts
index a8708753b2..f9ef1ce128 100644
--- a/web/src/pages/api/public/v2/datasets/[datasetName]/index.ts
+++ b/web/src/pages/api/public/v2/datasets/[datasetName]/index.ts
@@ -11,6 +11,7 @@ import { LangfuseNotFoundError } from "@langfuse/shared";
export default withMiddlewares({
GET: createAuthedProjectAPIRoute({
name: "get-dataset",
+ action: "datasets:read",
querySchema: GetDatasetV2Query,
responseSchema: GetDatasetV2Response,
rateLimitResource: "datasets",
diff --git a/web/src/pages/api/public/v2/datasets/index.ts b/web/src/pages/api/public/v2/datasets/index.ts
index f554f23d3d..90cb3379c9 100644
--- a/web/src/pages/api/public/v2/datasets/index.ts
+++ b/web/src/pages/api/public/v2/datasets/index.ts
@@ -12,6 +12,7 @@ import { createDatasetForApi } from "@/src/features/datasets/server/publicDatase
export default withMiddlewares({
POST: createAuthedProjectAPIRoute({
name: "Create Dataset",
+ action: "datasets:CUD",
bodySchema: PostDatasetsV2Body,
responseSchema: PostDatasetsV2Response,
rateLimitResource: "datasets",
@@ -27,6 +28,7 @@ export default withMiddlewares({
}),
GET: createAuthedProjectAPIRoute({
name: "Get Datasets",
+ action: "datasets:read",
querySchema: GetDatasetsV2Query,
responseSchema: GetDatasetsV2Response,
rateLimitResource: "datasets",
diff --git a/web/src/pages/api/public/v2/metrics.ts b/web/src/pages/api/public/v2/metrics.ts
index e5dfb843b9..6ba1e4c491 100644
--- a/web/src/pages/api/public/v2/metrics.ts
+++ b/web/src/pages/api/public/v2/metrics.ts
@@ -22,6 +22,7 @@ export function isMetricsV2Available(): boolean {
export default withMiddlewares({
GET: createAuthedProjectAPIRoute({
name: "Get Metrics V2",
+ action: "metrics:read",
rateLimitResource: "public-api-v2-metrics",
querySchema: GetMetricsV2Query,
responseSchema: GetMetricsV2Response,
diff --git a/web/src/pages/api/public/v2/observations/index.ts b/web/src/pages/api/public/v2/observations/index.ts
index 981f34e892..486af8374a 100644
--- a/web/src/pages/api/public/v2/observations/index.ts
+++ b/web/src/pages/api/public/v2/observations/index.ts
@@ -15,6 +15,7 @@ import { clampToDataAccessDays } from "@/src/features/entitlements/server/hasEnt
export default withMiddlewares({
GET: createAuthedProjectAPIRoute({
name: "Get Observations V2",
+ action: "traces:read",
allowInAppAgentKey: true,
querySchema: GetObservationsV2Query,
responseSchema: GetObservationsV2Response,
diff --git a/web/src/pages/api/public/v2/scores/[scoreId].ts b/web/src/pages/api/public/v2/scores/[scoreId].ts
index 435161a807..5e7ec0fe9c 100644
--- a/web/src/pages/api/public/v2/scores/[scoreId].ts
+++ b/web/src/pages/api/public/v2/scores/[scoreId].ts
@@ -13,6 +13,7 @@ import { SCORES_DEPRECATION } from "@/src/features/public-api/server/deprecation
export default withMiddlewares({
GET: createAuthedProjectAPIRoute({
name: "Get Score",
+ action: "scores:read",
querySchema: GetScoreQueryV2,
responseSchema: GetScoreResponseV2,
deprecation: SCORES_DEPRECATION,
diff --git a/web/src/pages/api/public/v2/scores/index.ts b/web/src/pages/api/public/v2/scores/index.ts
index 1bc37f4249..dc42e20236 100644
--- a/web/src/pages/api/public/v2/scores/index.ts
+++ b/web/src/pages/api/public/v2/scores/index.ts
@@ -13,6 +13,7 @@ import { clampToDataAccessDays } from "@/src/features/entitlements/server/hasEnt
export default withMiddlewares({
GET: createAuthedProjectAPIRoute({
name: "/api/public/scores",
+ action: "scores:read",
querySchema: GetScoresQueryV2,
responseSchema: GetScoresResponseV2,
deprecation: SCORES_DEPRECATION,
diff --git a/web/src/pages/api/public/v3/scores/index.ts b/web/src/pages/api/public/v3/scores/index.ts
index 8dec6347c2..aa2cab6f48 100644
--- a/web/src/pages/api/public/v3/scores/index.ts
+++ b/web/src/pages/api/public/v3/scores/index.ts
@@ -100,6 +100,7 @@ const GetScoresV3Query = GetScoresQueryV3.extend({
export default withMiddlewares({
GET: createAuthedProjectAPIRoute({
name: "Get Scores V3",
+ action: "scores:read",
querySchema: GetScoresV3Query,
responseSchema: GetScoresResponseV3,
fn: async ({ query, auth }) => {
diff --git a/web/src/server/api/routers/auditLogs.ts b/web/src/server/api/routers/auditLogs.ts
index f34bfb76d0..7e93c51042 100644
--- a/web/src/server/api/routers/auditLogs.ts
+++ b/web/src/server/api/routers/auditLogs.ts
@@ -95,7 +95,7 @@ export const auditLogsRouter = createTRPCRouter({
throwIfNoProjectAccess({
session: ctx.session,
projectId: input.projectId,
- scope: "auditLogs:read",
+ scope: "projectAuditLogs:read",
});
const [auditLogs, totalCount] = await Promise.all([
@@ -195,7 +195,7 @@ export const auditLogsRouter = createTRPCRouter({
throwIfNoOrganizationAccess({
session: ctx.session,
organizationId: input.orgId,
- scope: "auditLogs:read",
+ scope: "orgAuditLogs:read",
});
// Fetch organization-level audit logs (where projectId is null)