Skip to content

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

fix(web): require audit log access for audit-log batch export downloads (#16567)

Max Deichmann0317d5d2ae71
diff --git a/web/src/__tests__/server/batchExport-trpc.servertest.ts b/web/src/__tests__/server/batchExport-trpc.servertest.ts
index 1a28959b20..5b536dfa90 100644
--- a/web/src/__tests__/server/batchExport-trpc.servertest.ts
+++ b/web/src/__tests__/server/batchExport-trpc.servertest.ts
@@ -5,6 +5,7 @@ import { createOrgProjectAndApiKey } from "@langfuse/shared/src/server";
import type { Session } from "next-auth";
import {
BatchExportFileFormat,
+ BatchExportStatus,
BatchTableNames,
type Plan,
} from "@langfuse/shared";
@@ -165,6 +166,232 @@ describe("batchExport tRPC – audit_logs table authorization", () => {
);
});
+const seedCompletedExport = (opts: {
+ projectId: string;
+ name: string;
+ tableName: BatchTableNames;
+}) =>
+ prisma.batchExport.create({
+ data: {
+ projectId: opts.projectId,
+ userId: "user-test",
+ status: BatchExportStatus.COMPLETED,
+ name: opts.name,
+ format: BatchExportFileFormat.CSV,
+ // Non-URL so downloadUrl cannot parse an object key and must fall back
+ // to the stored value instead of signing against the test bucket.
+ url: "not-a-valid-url",
+ finishedAt: new Date(),
+ expiresAt: new Date(Date.now() + 60 * 60 * 1000),
+ query: {
+ tableName: opts.tableName,
+ filter: null,
+ orderBy: null,
+ },
+ },
+ });
+
+describe("batchExport tRPC – audit_logs download and list authorization", () => {
+ afterAll(async () => {
+ await prisma.organization.deleteMany({
+ where: { id: { in: __orgIds } },
+ });
+ });
+
+ it("blocks a MEMBER from downloadUrl of an OWNER-created audit_logs export", async () => {
+ const { project, org } = await createOrgProjectAndApiKey();
+ __orgIds.push(org.id);
+
+ const exportJob = await seedCompletedExport({
+ projectId: project.id,
+ name: "owner audit export",
+ tableName: BatchTableNames.AuditLogs,
+ });
+
+ const memberCaller = appRouter.createCaller({
+ ...createInnerTRPCContext({
+ session: makeSession(org.id, org.name, project.id, project.name, {
+ plan: "cloud:team",
+ projectRole: "MEMBER",
+ }),
+ headers: {},
+ }),
+ prisma,
+ });
+
+ await expect(
+ memberCaller.batchExport.downloadUrl({
+ projectId: project.id,
+ batchExportId: exportJob.id,
+ }),
+ ).rejects.toMatchObject({ code: "FORBIDDEN" });
+ });
+
+ it("blocks an OWNER on cloud:hobby from downloadUrl of an audit_logs export", async () => {
+ const { project, org } = await createOrgProjectAndApiKey();
+ __orgIds.push(org.id);
+
+ const exportJob = await seedCompletedExport({
+ projectId: project.id,
+ name: "hobby audit export",
+ tableName: BatchTableNames.AuditLogs,
+ });
+
+ const caller = appRouter.createCaller({
+ ...createInnerTRPCContext({
+ session: makeSession(org.id, org.name, project.id, project.name, {
+ plan: "cloud:hobby",
+ projectRole: "OWNER",
+ }),
+ headers: {},
+ }),
+ prisma,
+ });
+
+ await expect(
+ caller.batchExport.downloadUrl({
+ projectId: project.id,
+ batchExportId: exportJob.id,
+ }),
+ ).rejects.toMatchObject({ code: "FORBIDDEN" });
+ });
+
+ it("allows an OWNER with audit-logs entitlement to downloadUrl an audit_logs export", async () => {
+ const { project, org } = await createOrgProjectAndApiKey();
+ __orgIds.push(org.id);
+
+ const exportJob = await seedCompletedExport({
+ projectId: project.id,
+ name: "entitled audit export",
+ tableName: BatchTableNames.AuditLogs,
+ });
+
+ const caller = appRouter.createCaller({
+ ...createInnerTRPCContext({
+ session: makeSession(org.id, org.name, project.id, project.name, {
+ plan: "cloud:team",
+ projectRole: "OWNER",
+ }),
+ headers: {},
+ }),
+ prisma,
+ });
+
+ await expect(
+ caller.batchExport.downloadUrl({
+ projectId: project.id,
+ batchExportId: exportJob.id,
+ }),
+ ).resolves.toEqual({ url: "not-a-valid-url" });
+ });
+
+ it("still allows a MEMBER to downloadUrl a traces export", async () => {
+ const { project, org } = await createOrgProjectAndApiKey();
+ __orgIds.push(org.id);
+
+ const exportJob = await seedCompletedExport({
+ projectId: project.id,
+ name: "traces export",
+ tableName: BatchTableNames.Traces,
+ });
+
+ const memberCaller = appRouter.createCaller({
+ ...createInnerTRPCContext({
+ session: makeSession(org.id, org.name, project.id, project.name, {
+ plan: "cloud:team",
+ projectRole: "MEMBER",
+ }),
+ headers: {},
+ }),
+ prisma,
+ });
+
+ await expect(
+ memberCaller.batchExport.downloadUrl({
+ projectId: project.id,
+ batchExportId: exportJob.id,
+ }),
+ ).resolves.toEqual({ url: "not-a-valid-url" });
+ });
+
+ it("hides audit_logs exports from all for a MEMBER without auditLogs:read", async () => {
+ const { project, org } = await createOrgProjectAndApiKey();
+ __orgIds.push(org.id);
+
+ await seedCompletedExport({
+ projectId: project.id,
+ name: "audit export hidden",
+ tableName: BatchTableNames.AuditLogs,
+ });
+ await seedCompletedExport({
+ projectId: project.id,
+ name: "traces export visible",
+ tableName: BatchTableNames.Traces,
+ });
+
+ const memberCaller = appRouter.createCaller({
+ ...createInnerTRPCContext({
+ session: makeSession(org.id, org.name, project.id, project.name, {
+ plan: "cloud:team",
+ projectRole: "MEMBER",
+ }),
+ headers: {},
+ }),
+ prisma,
+ });
+
+ const result = await memberCaller.batchExport.all({
+ projectId: project.id,
+ page: 0,
+ limit: 50,
+ });
+
+ expect(result.exports.map((e) => e.name)).toEqual([
+ "traces export visible",
+ ]);
+ expect(result.totalCount).toBe(1);
+ });
+
+ it("includes audit_logs exports in all for an OWNER with entitlement", async () => {
+ const { project, org } = await createOrgProjectAndApiKey();
+ __orgIds.push(org.id);
+
+ await seedCompletedExport({
+ projectId: project.id,
+ name: "audit export listed",
+ tableName: BatchTableNames.AuditLogs,
+ });
+ await seedCompletedExport({
+ projectId: project.id,
+ name: "traces export listed",
+ tableName: BatchTableNames.Traces,
+ });
+
+ const caller = appRouter.createCaller({
+ ...createInnerTRPCContext({
+ session: makeSession(org.id, org.name, project.id, project.name, {
+ plan: "cloud:team",
+ projectRole: "OWNER",
+ }),
+ headers: {},
+ }),
+ prisma,
+ });
+
+ const result = await caller.batchExport.all({
+ projectId: project.id,
+ page: 0,
+ limit: 50,
+ });
+
+ expect(result.exports.map((e) => e.name).sort()).toEqual([
+ "audit export listed",
+ "traces export listed",
+ ]);
+ expect(result.totalCount).toBe(2);
+ });
+});
+
describe("batchExport tRPC – useEventsTable snapshot", () => {
afterAll(async () => {
await prisma.organization.deleteMany({
diff --git a/web/src/features/batch-exports/server/batchExport.ts b/web/src/features/batch-exports/server/batchExport.ts
index d4d2b98fb6..1ab9b7723b 100644
--- a/web/src/features/batch-exports/server/batchExport.ts
+++ b/web/src/features/batch-exports/server/batchExport.ts
@@ -2,9 +2,16 @@ import { auditLog } from "@/src/features/audit-logs/auditLog";
import { env } from "@/src/env.mjs";
import { parseBatchExportFileKeyFromUrl } from "@/src/features/batch-exports/server/batchExportFileKey";
import { getBatchExportStorageServiceClient } from "@/src/features/batch-exports/server/getBatchExportStorageClient";
-import { throwIfNoEntitlement } from "@/src/features/entitlements/server/hasEntitlement";
-import { throwIfNoProjectAccess } from "@/src/features/rbac/utils/checkProjectAccess";
import {
+ hasEntitlement,
+ throwIfNoEntitlement,
+} from "@/src/features/entitlements/server/hasEntitlement";
+import {
+ hasProjectAccess,
+ throwIfNoProjectAccess,
+} from "@/src/features/rbac/utils/checkProjectAccess";
+import {
+ type AuthedSession,
createTRPCRouter,
protectedProjectProcedure,
} from "@/src/server/api/trpc";
@@ -16,6 +23,7 @@ import {
LangfuseNotFoundError,
paginationZod,
} from "@langfuse/shared";
+import { type Prisma } from "@langfuse/shared/src/db";
import {
BatchExportQueue,
logger,
@@ -33,6 +41,33 @@ const LEGACY_DOWNLOAD_WINDOW_MS = 24 * 60 * 60 * 1000;
// signature expiry when the request starts, not while the download streams.
const FRESH_DOWNLOAD_URL_TTL_SECONDS = 10 * 60;
+// A persisted export's query is a Json column, so its table name is only
+// known at runtime. On create the query is still the validated input, where
+// `tableName` is typed and compared directly.
+const isAuditLogExport = (query: Prisma.JsonValue): boolean =>
+ typeof query === "object" &&
+ query !== null &&
+ !Array.isArray(query) &&
+ query.tableName === BatchExportTableName.AuditLogs;
+
+const canReadAuditLogs = (session: AuthedSession, projectId: string) =>
+ hasEntitlement({
+ entitlement: "audit-logs",
+ sessionUser: session.user,
+ projectId,
+ }) && hasProjectAccess({ session, projectId, scope: "auditLogs: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.
+const assertCanReadAuditLogs = (session: AuthedSession, projectId: string) => {
+ throwIfNoEntitlement({
+ entitlement: "audit-logs",
+ sessionUser: session.user,
+ projectId,
+ });
+ throwIfNoProjectAccess({ session, projectId, scope: "auditLogs:read" });
+};
+
const isDownloadWindowExpired = (batchExport: {
finishedAt: Date | null;
expiresAt: Date | null;
@@ -68,16 +103,7 @@ export const batchExportRouter = createTRPCRouter({
};
if (query.tableName === BatchExportTableName.AuditLogs) {
- throwIfNoEntitlement({
- entitlement: "audit-logs",
- sessionUser: ctx.session.user,
- projectId,
- });
- throwIfNoProjectAccess({
- session: ctx.session,
- projectId,
- scope: "auditLogs:read",
- });
+ assertCanReadAuditLogs(ctx.session, projectId);
}
assertLegacyTracingIoSearchCanCreateBatchJob({
@@ -171,6 +197,11 @@ export const batchExportRouter = createTRPCRouter({
if (!batchExport) {
throw new LangfuseNotFoundError("Batch export not found");
}
+
+ if (isAuditLogExport(batchExport.query)) {
+ assertCanReadAuditLogs(ctx.session, input.projectId);
+ }
+
if (
batchExport.status !== BatchExportStatus.COMPLETED ||
!batchExport.url
@@ -234,11 +265,23 @@ export const batchExportRouter = createTRPCRouter({
scope: "batchExports:read",
});
+ const where = {
+ projectId: input.projectId,
+ ...(canReadAuditLogs(ctx.session, input.projectId)
+ ? {}
+ : {
+ NOT: {
+ query: {
+ path: ["tableName"],
+ equals: BatchExportTableName.AuditLogs,
+ },
+ },
+ }),
+ };
+
const [exports, totalCount] = await Promise.all([
ctx.prisma.batchExport.findMany({
- where: {
- projectId: input.projectId,
- },
+ where,
take: input.limit,
skip: input.page * input.limit,
orderBy: {
@@ -246,9 +289,7 @@ export const batchExportRouter = createTRPCRouter({
},
}),
ctx.prisma.batchExport.count({
- where: {
- projectId: input.projectId,
- },
+ where,
}),
]);