🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.
TypeScript analyticsautogenevaluationlangchainlarge-language-modelsllama-indexllmllm-evaluationllm-observabilityllmopsmonitoringobservabilityopen-sourceopenaiplaygroundprompt-engineeringprompt-managementself-hostedycombinator
fix(api): validate authorUserId on public comment creation (LFINT-1987) (#15900)
Steffen Schmitz89956fd0ac67
diff --git a/fern/apis/server/definition/comments.yml b/fern/apis/server/definition/comments.ymlindex e9baaa7dbe..0378536456 100644--- a/fern/apis/server/definition/comments.yml+++ b/fern/apis/server/definition/comments.yml@@ -61,7 +61,7 @@ types:docs: The content of the comment. May include markdown. Currently limited to 5000 characters.authorUserId:type: optional<string>- docs: The id of the user who created the comment.+ docs: The id of the user who created the comment. Must be a member of the organization that owns the project, otherwise an error will be thrown.CreateCommentResponse:properties:id:diff --git a/web/public/generated/api/openapi.yml b/web/public/generated/api/openapi.ymlindex 50c874edca..347afcbb5f 100644--- a/web/public/generated/api/openapi.yml+++ b/web/public/generated/api/openapi.yml@@ -8972,7 +8972,10 @@ components:authorUserId:type: stringnullable: true- description: The id of the user who created the comment.+ description: >-+ The id of the user who created the comment. Must be a member of the+ organization that owns the project, otherwise an error will be+ thrown.required:- projectId- objectTypediff --git a/web/src/__tests__/server/comments-api.servertest.ts b/web/src/__tests__/server/comments-api.servertest.tsindex a97e1e6392..84c888937c 100644--- a/web/src/__tests__/server/comments-api.servertest.ts+++ b/web/src/__tests__/server/comments-api.servertest.ts@@ -1,4 +1,8 @@-import { makeZodVerifiedAPICall } from "@/src/__tests__/test-utils";+import { randomUUID } from "crypto";+import {+ makeAPICall,+ makeZodVerifiedAPICall,+} from "@/src/__tests__/test-utils";import {GetCommentsV1Response,GetCommentV1Response,@@ -12,6 +16,41 @@ import {} from "@langfuse/shared/src/server";import { createObservation, createTrace } from "@langfuse/shared/src/server";+const seedProjectId = "7a88fb47-b4e2-43b8-a06c-a5ce950dc53a";++// POST /api/public/comments only accepts an authorUserId that belongs to a+// member of the project's organization. CI provisions the seed project through+// LANGFUSE_INIT_* instead of the Postgres seeder, so seeded ids such as+// "user-1" do not exist there. Create a member of the project's organization+// and use its id wherever a valid comment author is needed.+let orgMemberUserId: string;++beforeAll(async () => {+ const project = await prisma.project.findUniqueOrThrow({+ where: { id: seedProjectId },+ select: { orgId: true },+ });++ const user = await prisma.user.create({+ data: {+ name: "Comments API Author",+ email: `comments-api-author-${randomUUID()}@langfuse.com`,+ },+ });+ orgMemberUserId = user.id;++ await prisma.organizationMembership.create({+ data: { orgId: project.orgId, userId: orgMemberUserId, role: "MEMBER" },+ });+});++afterAll(async () => {+ await prisma.organizationMembership.deleteMany({+ where: { userId: orgMemberUserId },+ });+ await prisma.user.delete({ where: { id: orgMemberUserId } });+});+describe("Create and get comments", () => {beforeAll(async () => {const traces = [@@ -35,7 +74,7 @@ describe("Create and get comments", () => {objectId: "1234",objectType: "TRACE",projectId: "7a88fb47-b4e2-43b8-a06c-a5ce950dc53a",- authorUserId: "user-1",+ authorUserId: orgMemberUserId,},);@@ -54,7 +93,7 @@ describe("Create and get comments", () => {objectId: "1234",objectType: "TRACE",content: "hello",- authorUserId: "user-1",+ authorUserId: orgMemberUserId,});});@@ -366,7 +405,7 @@ describe("Public API does NOT process mentions", () => {objectId: "no-mention-processing-trace",objectType: "TRACE",projectId: "7a88fb47-b4e2-43b8-a06c-a5ce950dc53a",- authorUserId: "user-1",+ authorUserId: orgMemberUserId,},);@@ -385,3 +424,140 @@ describe("Public API does NOT process mentions", () => {);});});++describe("POST /api/public/comments authorUserId scoping", () => {+ const projectId = seedProjectId;+ const objectId = "author-scoping-trace";+ let otherOrgId: string;+ let otherOrgUserId: string;++ beforeAll(async () => {+ await createTracesCh([+ createTrace({+ name: "trace-for-author-scoping",+ project_id: projectId,+ id: objectId,+ }),+ ]);++ const otherOrg = await prisma.organization.create({+ data: { name: `Comment Author Scoping Org ${randomUUID()}` },+ });+ otherOrgId = otherOrg.id;++ const otherOrgUser = await prisma.user.create({+ data: {+ name: "Other Org User",+ email: `comment-author-scoping-${randomUUID()}@langfuse.com`,+ },+ });+ otherOrgUserId = otherOrgUser.id;++ await prisma.organizationMembership.create({+ data: { orgId: otherOrgId, userId: otherOrgUserId, role: "MEMBER" },+ });+ });++ afterAll(async () => {+ await prisma.comment.deleteMany({ where: { projectId, objectId } });+ await prisma.organizationMembership.deleteMany({+ where: { userId: otherOrgUserId },+ });+ await prisma.organization.delete({ where: { id: otherOrgId } });+ await prisma.user.delete({ where: { id: otherOrgUserId } });+ });++ it("should reject an authorUserId that is not a member of the project's organization", async () => {+ const response = await makeAPICall<{ message: string; error: string }>(+ "POST",+ "/api/public/comments",+ {+ content: "spoofed comment",+ objectId,+ objectType: "TRACE",+ projectId,+ authorUserId: otherOrgUserId,+ },+ );++ expect(response.status).toBe(400);+ expect(response.body.error).toBe("InvalidRequestError");++ const comments = await prisma.comment.findMany({+ where: { projectId, authorUserId: otherOrgUserId },+ });+ expect(comments).toHaveLength(0);+ });++ it("should not disclose whether a rejected authorUserId exists", async () => {+ const crossOrgResponse = await makeAPICall<{ message: string }>(+ "POST",+ "/api/public/comments",+ {+ content: "spoofed comment",+ objectId,+ objectType: "TRACE",+ projectId,+ authorUserId: otherOrgUserId,+ },+ );++ const unknownUserResponse = await makeAPICall<{ message: string }>(+ "POST",+ "/api/public/comments",+ {+ content: "spoofed comment",+ objectId,+ objectType: "TRACE",+ projectId,+ authorUserId: `does-not-exist-${randomUUID()}`,+ },+ );++ expect(crossOrgResponse.status).toBe(400);+ expect(unknownUserResponse.status).toBe(400);+ expect(crossOrgResponse.body.message).toBe(+ unknownUserResponse.body.message,+ );+ });++ it("should accept an authorUserId of an organization member without project-level ownership", async () => {+ // orgMemberUserId has an organization membership but no project membership.+ const commentResponse = await makeZodVerifiedAPICall(+ PostCommentsV1Response,+ "POST",+ "/api/public/comments",+ {+ content: "comment by org member",+ objectId,+ objectType: "TRACE",+ projectId,+ authorUserId: orgMemberUserId,+ },+ );++ const comment = await prisma.comment.findUnique({+ where: { id: commentResponse.body.id },+ });+ expect(comment?.authorUserId).toBe(orgMemberUserId);+ });++ it("should still create comments without an authorUserId", async () => {+ const commentResponse = await makeZodVerifiedAPICall(+ PostCommentsV1Response,+ "POST",+ "/api/public/comments",+ {+ content: "comment without author",+ objectId,+ objectType: "TRACE",+ projectId,+ },+ );++ const comment = await prisma.comment.findUnique({+ where: { id: commentResponse.body.id },+ });+ expect(comment?.authorUserId).toBeNull();+ });+});diff --git a/web/src/features/comments/server/publicCommentService.ts b/web/src/features/comments/server/publicCommentService.tsindex 1fdab291a9..03ad73131d 100644--- a/web/src/features/comments/server/publicCommentService.ts+++ b/web/src/features/comments/server/publicCommentService.ts@@ -8,7 +8,7 @@ import type {GetCommentsV1Query,PostCommentsV1Body,} from "@/src/features/public-api/types/comments";-import { LangfuseNotFoundError } from "@langfuse/shared";+import { InvalidRequestError, LangfuseNotFoundError } from "@langfuse/shared";import { prisma } from "@langfuse/shared/src/db";type CommentAuditScope = {@@ -71,10 +71,41 @@ export const getCommentRecordOrThrow = async ({return comment;};+// The public API accepts a client-supplied authorUserId, so we must verify that+// the author is a member of the authenticated project's organization. Otherwise+// any project write key could attribute comments to arbitrary users.+// The message is identical for unknown and out-of-scope users so that the+// endpoint does not disclose whether a given user id exists.+const throwIfAuthorNotInOrganization = async ({+ authorUserId,+ orgId,+}: {+ authorUserId: string;+ orgId: string;+}) => {+ const membership = await prisma.organizationMembership.findFirst({+ where: { orgId, userId: authorUserId },+ select: { id: true },+ });++ if (!membership) {+ throw new InvalidRequestError(+ "authorUserId must belong to a member of the organization that owns the authenticated project.",+ );+ }+};+export const createCommentForApi = async ({input,auditScope,}: CreateCommentInput) => {+ if (input.authorUserId != null) {+ await throwIfAuthorNotInOrganization({+ authorUserId: input.authorUserId,+ orgId: auditScope.orgId,+ });+ }+const result = await validateCommentReferenceObject({ctx: { prisma, auth: { scope: { projectId: auditScope.projectId } } },input: {