Skip to content

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

feat(auth): authorize every ingestion event through the policy core (#16715)

Mark Salpeter3679ef9ff666
diff --git a/web/src/__tests__/server/__snapshots__/public-api-auth-parity.servertest.ts.snap b/web/src/__tests__/server/__snapshots__/public-api-auth-parity.servertest.ts.snap
index c59708ef73..5723f7f1ae 100644
--- a/web/src/__tests__/server/__snapshots__/public-api-auth-parity.servertest.ts.snap
+++ b/web/src/__tests__/server/__snapshots__/public-api-auth-parity.servertest.ts.snap
@@ -1,6 +1,6 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
-exports[`public-api auth parity > legacy matches the main-captured baseline 1`] = `
+exports[`public-api auth parity > legacy matches the captured baseline except documented divergences 1`] = `
{
"DELETE annotation-queues/[queueId]/assignments | admin/admin": 401,
"DELETE annotation-queues/[queueId]/assignments | agent/basic": 401,
@@ -709,6 +709,12 @@ exports[`public-api auth parity > legacy matches the main-captured baseline 1`]
"POST generations | org/bearer": 401,
"POST generations | project/basic": 200,
"POST generations | project/bearer": 403,
+ "POST ingestion | admin/admin": 401,
+ "POST ingestion | agent/basic": 401,
+ "POST ingestion | agent/bearer": 401,
+ "POST ingestion | org/bearer": 401,
+ "POST ingestion | project/basic": 400,
+ "POST ingestion | project/bearer": 400,
"POST media/index | admin/admin": 401,
"POST media/index | agent/basic": 401,
"POST media/index | agent/bearer": 401,
@@ -723,6 +729,18 @@ exports[`public-api auth parity > legacy matches the main-captured baseline 1`]
"POST models/index | org/bearer": 401,
"POST models/index | project/basic": 400,
"POST models/index | project/bearer": 403,
+ "POST otel/v1/metrics/index | admin/admin": 401,
+ "POST otel/v1/metrics/index | agent/basic": 401,
+ "POST otel/v1/metrics/index | agent/bearer": 401,
+ "POST otel/v1/metrics/index | org/basic": 403,
+ "POST otel/v1/metrics/index | org/bearer": 401,
+ "POST otel/v1/metrics/index | project/basic": 200,
+ "POST otel/v1/metrics/index | project/bearer": 403,
+ "POST otel/v1/traces/index | admin/admin": 401,
+ "POST otel/v1/traces/index | agent/basic": 401,
+ "POST otel/v1/traces/index | agent/bearer": 401,
+ "POST otel/v1/traces/index | org/basic": 403,
+ "POST otel/v1/traces/index | org/bearer": 401,
"POST projects/[projectId]/apiKeys/index | admin/admin": 401,
"POST projects/[projectId]/apiKeys/index | agent/basic": 401,
"POST projects/[projectId]/apiKeys/index | agent/bearer": 401,
diff --git a/web/src/__tests__/server/ingestion-unit.servertest.ts b/web/src/__tests__/server/ingestion-unit.servertest.ts
index 7839b482bd..7a483c1907 100644
--- a/web/src/__tests__/server/ingestion-unit.servertest.ts
+++ b/web/src/__tests__/server/ingestion-unit.servertest.ts
@@ -82,7 +82,7 @@ vi.mock("@langfuse/shared/src/db", async () => {
});
describe("/api/public/ingestion API Endpoint", () => {
- it(`should return 500 for prisma exception`, async () => {
+ it(`should return 503 for prisma exception`, async () => {
const { req, res } = createMocks({
method: "POST",
headers: {
@@ -97,6 +97,6 @@ describe("/api/public/ingestion API Endpoint", () => {
const extendedRes = res as unknown as NextApiResponse;
await handler(extendedReq, extendedRes);
- expect(res._getStatusCode()).toBe(500);
+ expect(res._getStatusCode()).toBe(503);
});
});
diff --git a/web/src/__tests__/server/public-api-auth-parity.servertest.ts b/web/src/__tests__/server/public-api-auth-parity.servertest.ts
index a7b771015e..7abc76d0fa 100644
--- a/web/src/__tests__/server/public-api-auth-parity.servertest.ts
+++ b/web/src/__tests__/server/public-api-auth-parity.servertest.ts
@@ -14,8 +14,8 @@ import {
// Pins the public-API authorization seam to legacy: sweeps every route across
// migration modes and key kinds, recording each cell's status. Shadow and
-// enforce must equal legacy (cross-mode); a main-captured snapshot pins legacy
-// across the refactor (cross-branch). Value is status only.
+// enforce must equal legacy, save documented enforce divergences; a snapshot
+// pins legacy itself. Value is status only.
type HttpMethod = "GET" | "POST" | "PUT" | "PATCH" | "DELETE";
@@ -108,6 +108,9 @@ const projectRoutes: Route[] = [
route: "v2/evaluation-rules/[evaluationRuleId]",
methods: ["GET", "PATCH", "DELETE"],
},
+ { route: "ingestion", methods: ["POST"] },
+ { route: "otel/v1/traces/index", methods: ["POST"] },
+ { route: "otel/v1/metrics/index", methods: ["POST"] },
];
// Org and misc routes call shadowAuth directly from the handler body.
@@ -140,11 +143,10 @@ const orgRoutes: Route[] = [
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
+ "otel/otlp-proto", // generated protobuf, not a route
"slack", // Slack OAuth, own auth path
];
@@ -244,6 +246,12 @@ async function callRoute(
type Cell = { key: string; run: () => Promise<number> };
+// node-mocks-http can't supply the raw request stream otel/v1/traces reads, so these cells hang; only the project key passes, identically in every mode.
+const streamReadingCells = new Set([
+ "POST otel/v1/traces/index | project/basic",
+ "POST otel/v1/traces/index | project/bearer",
+]);
+
/** matrixCells lists every route × method × key kind × header kind cell in source order. */
function matrixCells(routes: Route[]): Cell[] {
const cells: Cell[] = [];
@@ -251,10 +259,9 @@ function matrixCells(routes: Route[]): Cell[] {
for (const method of methods) {
for (const apiKeyKind of apiKeyKinds) {
for (const { headerKind, headers } of getHeaders(apiKeyKind, route)) {
- cells.push({
- key: `${method} ${route} | ${apiKeyKind}/${headerKind}`,
- run: () => callRoute(route, method, headers),
- });
+ const key = `${method} ${route} | ${apiKeyKind}/${headerKind}`;
+ if (streamReadingCells.has(key)) continue;
+ cells.push({ key, run: () => callRoute(route, method, headers) });
}
}
}
@@ -306,10 +313,12 @@ function walkRoutes(): string[] {
return routes;
}
-// Enforce cells that intentionally diverge from legacy; every other cell must match.
-const enforceDivergences: Record<string, number> = {
- // org keys hold project:read, which legacy's ["project"] tier gate refused
- "GET projects/index | org/basic": 200,
+// divergences lists cells whose status differs from the main baseline the
+// snapshot records; `legacy`/`enforce` give that mode's actual status.
+const divergences: Record<string, { legacy?: number; enforce?: number }> = {
+ "GET projects/index | org/basic": { enforce: 200 },
+ // ingestion had no tier gate on main (org key -> 401); shadowAuth adds one -> 403.
+ "POST ingestion | org/basic": { legacy: 403, enforce: 400 },
};
describe("public-api auth parity", () => {
@@ -349,8 +358,14 @@ describe("public-api auth parity", () => {
(env as any).ADMIN_API_KEY = originalAdminApiKey;
});
- it("legacy matches the main-captured baseline", () => {
- expect(matrices.legacy).toMatchSnapshot();
+ it("legacy matches the captured baseline except documented divergences", () => {
+ const baseline = { ...matrices.legacy };
+ for (const [cell, { legacy }] of Object.entries(divergences)) {
+ if (legacy === undefined) continue;
+ expect(baseline[cell]).toBe(legacy);
+ delete baseline[cell];
+ }
+ expect(baseline).toMatchSnapshot();
});
it("shadow is byte-identical to legacy", () => {
@@ -358,10 +373,11 @@ describe("public-api auth parity", () => {
});
it("enforce matches legacy except documented divergences", () => {
- expect(matrices.enforce).toEqual({
- ...matrices.legacy,
- ...enforceDivergences,
- });
+ const expected = { ...matrices.legacy };
+ for (const [cell, { enforce }] of Object.entries(divergences)) {
+ if (enforce !== undefined) expected[cell] = enforce;
+ }
+ expect(matrices.enforce).toEqual(expected);
});
it("covers every public route", () => {
diff --git a/web/src/__tests__/server/unit/assertToolAuthorized.servertest.ts b/web/src/__tests__/server/unit/assertToolAuthorized.servertest.ts
index dd824e7568..e8e2804d7b 100644
--- a/web/src/__tests__/server/unit/assertToolAuthorized.servertest.ts
+++ b/web/src/__tests__/server/unit/assertToolAuthorized.servertest.ts
@@ -9,6 +9,12 @@ const { env } = vi.hoisted(() => ({
vi.mock("@/src/env.mjs", () => ({ env }));
+const { shadowAuthDiff } = vi.hoisted(() => ({ shadowAuthDiff: vi.fn() }));
+
+vi.mock("@/src/features/public-api/server/shadowAuthDiff", () => ({
+ shadowAuthDiff,
+}));
+
import { __test } from "@/src/features/mcp/server/mcpServer";
import type { ToolDefinition } from "@/src/features/mcp/core/define-tool";
import {
@@ -70,6 +76,7 @@ const tool = (action: ApiAction): ToolDefinition => ({
describe("assertToolAuthorized", () => {
beforeEach(() => {
env.API_AUTH_MIGRATION = "enforce";
+ shadowAuthDiff.mockReset();
});
it("throws a formatted InvalidRequest error when the context lacks the tool's action", () => {
@@ -113,15 +120,13 @@ describe("assertToolAuthorized", () => {
});
it("diffs a denied action without throwing", () => {
- const diff = vi.fn();
expect(() =>
assertToolAuthorized(
tool("prompts:CUD"),
serverContext(authContext([allowPrompts])),
- diff,
),
).not.toThrow();
- expect(diff).toHaveBeenCalledWith(
+ expect(shadowAuthDiff).toHaveBeenCalledWith(
{ success: false, error: expect.any(ForbiddenError) },
{ success: true, scope: { accessLevel: "project" } },
"prompts:CUD",
@@ -129,13 +134,11 @@ describe("assertToolAuthorized", () => {
});
it("diffs an allowed action", () => {
- const diff = vi.fn();
assertToolAuthorized(
tool("prompts:read"),
serverContext(authContext([allowPrompts])),
- diff,
);
- expect(diff).toHaveBeenCalledWith(
+ expect(shadowAuthDiff).toHaveBeenCalledWith(
{ success: true },
{ success: true, scope: { accessLevel: "project" } },
"prompts:read",
diff --git a/web/src/__tests__/server/unit/public-api/shadowAuthorize.servertest.ts b/web/src/__tests__/server/unit/public-api/shadowAuthorize.servertest.ts
new file mode 100644
index 0000000000..f298635e15
--- /dev/null
+++ b/web/src/__tests__/server/unit/public-api/shadowAuthorize.servertest.ts
@@ -0,0 +1,122 @@
+import { beforeEach, describe, expect, it, vi } from "vitest";
+
+import { ForbiddenError } from "@langfuse/shared";
+
+const { env } = vi.hoisted(() => ({
+ env: { API_AUTH_MIGRATION: "enforce" as string },
+}));
+
+vi.mock("@/src/env.mjs", () => ({ env }));
+
+const { shadowAuthDiff } = vi.hoisted(() => ({ shadowAuthDiff: vi.fn() }));
+
+vi.mock("@/src/features/public-api/server/shadowAuthDiff", () => ({
+ shadowAuthDiff,
+}));
+
+import { shadowAuthorize } from "@/src/features/public-api/server/shadowAuth";
+import {
+ __dangerouslySkipAuthz,
+ type ApiAction,
+} from "@/src/features/public-api/server/enforceAuth";
+import {
+ type AuthorizationContext,
+ type Policy,
+ type ProjectAction,
+} from "@/src/features/auth/policy/types";
+
+const PRJ = "prj_1";
+
+const allowPrompts: Policy = {
+ kind: "project",
+ source: { kind: "role", id: "PROJECT" },
+ actions: ["prompts:read"] as ProjectAction[] as never,
+ resources: [PRJ],
+ effect: "allow",
+};
+
+const authContext = (policies: Policy[]): AuthorizationContext => ({
+ principal: {
+ kind: "apiKey",
+ apiKeyId: "key_1",
+ userId: null,
+ isInAppAgentKey: false,
+ publicKey: "pk-lf-1",
+ scope: "PROJECT",
+ presentation: "privateKey",
+ organizations: [],
+ boundResource: { orgId: "org_1", projectId: PRJ },
+ },
+ policies,
+});
+
+const params = (action: ApiAction, ctx: AuthorizationContext | undefined) => ({
+ ctx,
+ action,
+ resource: { projectId: PRJ },
+ accessLevel: "project" as const,
+});
+
+describe("shadowAuthorize", () => {
+ beforeEach(() => {
+ env.API_AUTH_MIGRATION = "enforce";
+ shadowAuthDiff.mockClear();
+ });
+
+ it("denies when the context lacks the item's action", () => {
+ const decision = shadowAuthorize(
+ params("prompts:CUD", authContext([allowPrompts])),
+ );
+ expect(decision).toMatchObject({
+ success: false,
+ error: expect.any(ForbiddenError),
+ });
+ });
+
+ it("allows when the context holds the item's action", () => {
+ expect(
+ shadowAuthorize(params("prompts:read", authContext([allowPrompts]))),
+ ).toEqual({ success: true });
+ });
+
+ it("passes an ungated item", () => {
+ expect(
+ shadowAuthorize(params(__dangerouslySkipAuthz, authContext([]))),
+ ).toEqual({
+ success: true,
+ });
+ });
+
+ it("passes when no context resolved (legacy)", () => {
+ expect(shadowAuthorize(params("prompts:CUD", undefined))).toEqual({
+ success: true,
+ });
+ });
+
+ describe("shadow mode", () => {
+ beforeEach(() => {
+ env.API_AUTH_MIGRATION = "shadow";
+ });
+
+ it("diffs a denied item against legacy's implicit allow without denying", () => {
+ const decision = shadowAuthorize(
+ params("prompts:CUD", authContext([allowPrompts])),
+ );
+ expect(decision).toEqual({ success: true });
+ expect(shadowAuthDiff).toHaveBeenCalledWith(
+ { success: false, error: expect.any(ForbiddenError) },
+ { success: true, scope: { accessLevel: "project" } },
+ "prompts:CUD",
+ );
+ });
+
+ it("diffs an allowed item", () => {
+ shadowAuthorize(params("prompts:read", authContext([allowPrompts])));
+ expect(shadowAuthDiff).toHaveBeenCalledWith(
+ { success: true },
+ { success: true, scope: { accessLevel: "project" } },
+ "prompts:read",
+ );
+ });
+ });
+});
diff --git a/web/src/features/mcp/server/mcpServer.ts b/web/src/features/mcp/server/mcpServer.ts
index 132243fbd6..ffe4129f0c 100644
--- a/web/src/features/mcp/server/mcpServer.ts
+++ b/web/src/features/mcp/server/mcpServer.ts
@@ -18,10 +18,7 @@ import {
CallToolRequestSchema,
} from "@modelcontextprotocol/sdk/types.js";
import { isProductFeedbackAvailable } from "@/src/features/feedback/server/FeedbackService";
-import { env } from "@/src/env.mjs";
-import { authorize } from "@/src/features/auth/policy/authorize";
-import { shadowAuthDiff } from "@/src/features/public-api/server/shadowAuthDiff";
-import { __dangerouslySkipAuthz } from "@/src/features/public-api/server/enforceAuth";
+import { shadowAuthorize } from "@/src/features/public-api/server/shadowAuth";
import { formatErrorForUser } from "../core/error-formatting";
import type { ServerContext } from "../types";
import type { ToolDefinition } from "../core/define-tool";
@@ -140,24 +137,17 @@ export function createMcpServer(context: ServerContext): Server {
return server;
}
-/** assertToolAuthorized checks the resolved context against a tool's action: enforce throws on deny, shadow only diffs, ungated tools and unresolved contexts (legacy) pass. */
+/** assertToolAuthorized authorizes a tool call through the per-item seam, throwing an enforce-mode deny as an MCP error. */
function assertToolAuthorized(
definition: ToolDefinition,
context: ServerContext,
- diff = shadowAuthDiff,
): void {
- if (definition.action === __dangerouslySkipAuthz || !context.auth) return;
- const decision = authorize(context.auth, definition.action, {
- projectId: context.projectId,
+ const decision = shadowAuthorize({
+ ctx: context.auth,
+ action: definition.action,
+ resource: { projectId: context.projectId },
+ accessLevel: context.accessLevel,
});
- if (env.API_AUTH_MIGRATION === "shadow") {
- diff(
- decision,
- { success: true, scope: { accessLevel: context.accessLevel } },
- definition.action,
- );
- return;
- }
if (!decision.success) throw formatErrorForUser(decision.error);
}
diff --git a/web/src/features/public-api/server/shadowAuth.ts b/web/src/features/public-api/server/shadowAuth.ts
index d2b10ffc14..ff764d7e63 100644
--- a/web/src/features/public-api/server/shadowAuth.ts
+++ b/web/src/features/public-api/server/shadowAuth.ts
@@ -16,17 +16,22 @@ import {
type RouteAccessLevel,
} from "@/src/features/public-api/server/verifyProjectApiKeyAuth";
import {
+ __dangerouslySkipAuthz,
enforceAuth,
+ type ApiAction,
type EnforceAuthParams,
type EnforceAuthResult,
} from "@/src/features/public-api/server/enforceAuth";
import { shadowAuthDiff } from "@/src/features/public-api/server/shadowAuthDiff";
+import { authorize } from "@/src/features/auth/policy/authorize";
import {
forbiddenError,
serviceUnavailableError,
unauthorizedError,
type AuthorizationContext,
+ type Decision,
type ErrorResult,
+ type Resource,
type Success,
} from "@/src/features/auth/policy/types";
import { isPrismaException } from "@/src/utils/exceptions";
@@ -136,11 +141,36 @@ function isOrgFamily(allowedAccessLevels: ApiAccessLevel[]): boolean {
);
}
+/** shadowAuthorize authorizes one item against a shadowAuth-resolved context for the active migration mode: legacy and ungated items pass, shadow only diffs against legacy's implicit allow, and enforce returns the decision the caller disposes of. */
+export function shadowAuthorize(params: ShadowAuthorizeParams): Decision {
+ if (params.action === __dangerouslySkipAuthz || !params.ctx) {
+ return { success: true };
+ }
+ const decision = authorize(params.ctx, params.action, params.resource);
+ if (env.API_AUTH_MIGRATION === "shadow") {
+ shadowAuthDiff(
+ decision,
+ { success: true, scope: { accessLevel: params.accessLevel } },
+ params.action,
+ );
+ return { success: true };
+ }
+ return decision;
+}
+
/** ShadowAuthParams is enforceAuth's params plus the access levels the legacy verify gates on. */
export type ShadowAuthParams = EnforceAuthParams & {
allowedAccessLevels: ApiAccessLevel[];
};
+/** ShadowAuthorizeParams is one per-item authorization: the resolved context, the action the item asserts or the explicit opt-out, the resource it targets, and the access level the shadow diff records. */
+export type ShadowAuthorizeParams = {
+ ctx: AuthorizationContext | undefined;
+ action: ApiAction;
+ resource: Resource;
+ accessLevel: ApiAccessLevel;
+};
+
/** ShadowAuthAccessResult is a verified scope; the authorizing context rides along only when the new pipeline produced it. */
export type ShadowAuthAccessResult = Success & {
scope: ApiAccessScope;
diff --git a/web/src/pages/api/public/ingestion.ts b/web/src/pages/api/public/ingestion.ts
index c3ac2adab3..b5134fd6b0 100644
--- a/web/src/pages/api/public/ingestion.ts
+++ b/web/src/pages/api/public/ingestion.ts
@@ -3,7 +3,6 @@ import { type NextApiRequest, type NextApiResponse } from "next";
import { z } from "zod";
import {
traceException,
- redis,
logger,
getCurrentSpan,
contextWithLangfuseProps,
@@ -11,6 +10,7 @@ import {
markProjectIngestFailure,
createIngestionAttribution,
processEventBatch,
+ type ApiAccessLevel,
redactLangfuseSecretKeys,
} from "@langfuse/shared/src/server";
import { telemetry } from "@/src/features/telemetry";
@@ -19,12 +19,10 @@ import {
jsonSchema,
MethodNotAllowedError,
BaseError,
- UnauthorizedError,
+ InternalServerError,
ForbiddenError,
} from "@langfuse/shared";
import { isPrismaException } from "@/src/utils/exceptions";
-import { prisma } from "@langfuse/shared/src/db";
-import { ApiAuthService } from "@/src/features/public-api/server/apiAuth";
import { RateLimitService } from "@/src/features/public-api/server/RateLimitService";
import * as opentelemetry from "@opentelemetry/api";
import { env } from "@/src/env.mjs";
@@ -37,6 +35,15 @@ import {
SDK_VERSION_ATTRIBUTE,
extractSdkAttributes,
} from "@langfuse/shared/instrumentation/bootstrap";
+import {
+ shadowAuth,
+ shadowAuthorize,
+} from "@/src/features/public-api/server/shadowAuth";
+import { __dangerouslySkipAuthz } from "@/src/features/public-api/server/enforceAuth";
+import {
+ type AuthorizationContext,
+ type ProjectAction,
+} from "@/src/features/auth/policy/types";
export const config = {
api: {
@@ -103,28 +110,26 @@ export default async function handler(
if (req.method !== "POST") throw new MethodNotAllowedError();
- // CHECK AUTH FOR ALL EVENTS
- const authCheck = await new ApiAuthService(
- prisma,
- redis,
- ).verifyAuthHeaderAndReturnScope(req.headers.authorization);
-
- if (!authCheck.validKey) {
- throw new UnauthorizedError(authCheck.error);
- }
- if (!authCheck.scope.projectId) {
- throw new UnauthorizedError(
- "Missing projectId in scope. Are you using an organization key?",
- );
+ // CHECK AUTH FOR ALL EVENTS; each event authorizes its own action below.
+ const authResult = await shadowAuth({
+ req,
+ action: __dangerouslySkipAuthz,
+ allowedAccessLevels: ["project", "scores"],
+ });
+ if (!authResult.success) throw authResult.error;
+ const { scope, ctx: authCtx } = authResult;
+ // shadowAuth's project/scores gating guarantees a projectId; narrow the invariant.
+ if (!scope.projectId) {
+ throw new InternalServerError("Missing projectId on an authorized scope");
}
- const projectId = authCheck.scope.projectId;
+ const projectId = scope.projectId;
projectIdForIngestFailure = projectId;
-
- if (authCheck.scope.isIngestionSuspended) {
+ if (scope.isIngestionSuspended) {
throw new ForbiddenError(
"Ingestion suspended: Usage threshold exceeded. Please upgrade your plan.",
);
}
+ const authCheck = { validKey: true as const, scope };
const ctx = contextWithLangfuseProps({
headers: req.headers,
@@ -197,12 +202,23 @@ export default async function handler(
);
}
- const result = await processEventBatch(batchForProcessing, authCheck, {
- attribution,
- });
- if (rejectedErrors.length > 0) {
- result.errors = [...result.errors, ...rejectedErrors];
- }
+ const authorized = authorizeIngestionBatch(
+ batchForProcessing,
+ authCtx,
+ scope.accessLevel,
+ projectId,
+ );
+
+ const result = await processEventBatch(
+ authorized.batchForProcessing,
+ authCheck,
+ { attribution },
+ );
+ result.errors = [
+ ...result.errors,
+ ...rejectedErrors,
+ ...authorized.rejectedErrors,
+ ];
// Cloud-only: a 207 with every event 201 is how agents conclude the
// legacy write path is healthy. Stamp when the original batch asked
@@ -301,26 +317,13 @@ const EVENTS_ONLY_INGESTION_REMEDIATION = [
export function filterBatchForEventsOnly(
batch: unknown[],
isEventsOnlyMode: boolean,
-): {
- batchForProcessing: unknown[];
- rejectedErrors: {
- id: string;
- status: number;
- message: string;
- error: string;
- }[];
-} {
+): IngestionBatchFilter {
if (!isEventsOnlyMode) {
return { batchForProcessing: batch, rejectedErrors: [] };
}
const batchForProcessing: unknown[] = [];
- const rejectedErrors: {
- id: string;
- status: number;
- message: string;
- error: string;
- }[] = [];
+ const rejectedErrors: IngestionEventRejection[] = [];
for (const event of batch) {
const eventObj =
@@ -347,6 +350,56 @@ export function filterBatchForEventsOnly(
return { batchForProcessing, rejectedErrors };
}
+/** authorizeIngestionBatch authorizes each event against the resolved context, dropping enforce-mode denials as 207 rejections; legacy and shadow keep every event. */
+function authorizeIngestionBatch(
+ batch: unknown[],
+ ctx: AuthorizationContext | undefined,
+ accessLevel: ApiAccessLevel,
+ projectId: string,
+): IngestionBatchFilter {
+ const batchForProcessing: unknown[] = [];
+ const rejectedErrors: IngestionEventRejection[] = [];
+
+ for (const event of batch) {
+ const decision = shadowAuthorize({
+ ctx,
+ action: ingestionActionForEventType(eventTypeOf(event)),
+ resource: { projectId },
+ accessLevel,
+ });
+ if (!decision.success) {
+ rejectedErrors.push({
+ id: idOf(event),
+ status: 401,
+ message: "Authentication error",
+ error: "Access Scope Denied",
+ });
+ continue;
+ }
+ batchForProcessing.push(event);
+ }
+ return { batchForProcessing, rejectedErrors };
+}
+
+/** ingestionActionForEventType maps an event type to the project action its write asserts; SDK logs skip authz. */
+function ingestionActionForEventType(
+ type: string | null,
+): ProjectAction | typeof __dangerouslySkipAuthz {
+ if (type === eventTypes.SDK_LOG) return __dangerouslySkipAuthz;
+ if (type === eventTypes.SCORE_CREATE) return "scores:create";
+ return "traces:create";
+}
+
+/** idOf reads an event's `id`, defaulting to `unknown` for a malformed event. */
+function idOf(event: unknown): string {
+ return typeof event === "object" &&
+ event !== null &&
+ "id" in event &&
+ typeof (event as { id: unknown }).id === "string"
+ ? (event as { id: string }).id
+ : "unknown";
+}
+
function eventTypeOf(event: unknown): string | null {
if (typeof event !== "object" || event === null) {
return null;
@@ -361,3 +414,17 @@ function batchContainsTraceOrObservationEvent(batch: unknown[]): boolean {
return type !== null && TRACE_OR_OBSERVATION_EVENT_TYPES.has(type);
});
}
+
+/** IngestionEventRejection is one event dropped from a batch, rendered in the 207 result's errors. */
+type IngestionEventRejection = {
+ id: string;
+ status: number;
+ message: string;
+ error: string;
+};
+
+/** IngestionBatchFilter is a batch split into the events to process and the events rejected. */
+type IngestionBatchFilter = {
+ batchForProcessing: unknown[];
+ rejectedErrors: IngestionEventRejection[];
+};