🪢 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(scim): scope Users/{id} reads and mutations to the caller's org (#17828)
Steffen Reuthere6d58f6de424
diff --git a/web/src/__tests__/server/scim-api.servertest.ts b/web/src/__tests__/server/scim-api.servertest.tsindex 2fbc607897..389e012696 100644--- a/web/src/__tests__/server/scim-api.servertest.ts+++ b/web/src/__tests__/server/scim-api.servertest.ts@@ -1494,6 +1494,134 @@ describe("SCIM API", () => {});});+ describe("Cross-organization isolation (Users/{id})", () => {+ const createdUserIds: string[] = [];++ // A user that exists on the instance but is NOT a member of the caller's+ // organization (seed-org-id) — i.e. a user belonging to some other org.+ const createNonMemberUser = async () => {+ const user = await prisma.user.create({+ data: {+ email: `outsider.${randomUUID().substring(0, 8)}@othercorp.example`,+ name: "Outside Org User",+ },+ });+ createdUserIds.push(user.id);+ return user;+ };++ afterEach(async () => {+ if (createdUserIds.length > 0) {+ await prisma.organizationMembership.deleteMany({+ where: { userId: { in: createdUserIds } },+ });+ await prisma.user.deleteMany({+ where: { id: { in: createdUserIds } },+ });+ createdUserIds.length = 0;+ }+ });++ it("GET on an out-of-org user is a 404", async () => {+ const outsider = await createNonMemberUser();++ const nonMember = await makeAPICall<{+ detail: string;+ userName?: string;+ name?: unknown;+ emails?: unknown;+ meta?: unknown;+ }>(+ "GET",+ `/api/public/scim/Users/${outsider.id}`,+ undefined,+ createBasicAuthHeader(orgApiKey, orgSecretKey),+ );++ expect(nonMember.status).toBe(404);+ // None of the out-of-org user's attributes may cross the org boundary.+ expect(nonMember.body.userName).toBeUndefined();+ expect(nonMember.body.name).toBeUndefined();+ expect(nonMember.body.emails).toBeUndefined();+ expect(nonMember.body.meta).toBeUndefined();+ });++ it("PUT with active omitted returns 404 for an out-of-org user", async () => {+ const outsider = await createNonMemberUser();++ const result = await makeAPICall<{ detail: string; userName?: string }>(+ "PUT",+ `/api/public/scim/Users/${outsider.id}`,+ { schemas: ["urn:ietf:params:scim:schemas:core:2.0:User"] },+ createBasicAuthHeader(orgApiKey, orgSecretKey),+ );++ expect(result.status).toBe(404);+ expect(result.body.userName).toBeUndefined();+ expect(result.body.detail).toContain("User not found");++ // No membership was created as a side effect.+ const memberships = await prisma.organizationMembership.findMany({+ where: { userId: outsider.id, orgId },+ });+ expect(memberships.length).toBe(0);+ });++ it("PUT active:false on an out-of-org user is a 404, not a silent noop", async () => {+ const outsider = await createNonMemberUser();++ const result = await makeAPICall<{ detail: string; userName?: string }>(+ "PUT",+ `/api/public/scim/Users/${outsider.id}`,+ {+ schemas: ["urn:ietf:params:scim:schemas:core:2.0:User"],+ active: false,+ },+ createBasicAuthHeader(orgApiKey, orgSecretKey),+ );++ expect(result.status).toBe(404);+ expect(result.body.userName).toBeUndefined();+ });++ it("PUT active:true provisions a user who is not yet a member", async () => {+ const outsider = await createNonMemberUser();++ const result = await makeZodVerifiedAPICall(+ ScimUserSchema,+ "PUT",+ `/api/public/scim/Users/${outsider.id}`,+ {+ schemas: ["urn:ietf:params:scim:schemas:core:2.0:User"],+ active: true,+ roles: ["MEMBER"],+ },+ createBasicAuthHeader(orgApiKey, orgSecretKey),+ 200,+ );++ expect(result.status).toBe(200);+ const memberships = await prisma.organizationMembership.findMany({+ where: { userId: outsider.id, orgId },+ });+ expect(memberships.length).toBe(1);+ expect(memberships[0].role).toBe("MEMBER");+ });++ it("DELETE on an out-of-org user is a 404", async () => {+ const outsider = await createNonMemberUser();++ const result = await makeAPICall<{ detail: string }>(+ "DELETE",+ `/api/public/scim/Users/${outsider.id}`,+ undefined,+ createBasicAuthHeader(orgApiKey, orgSecretKey),+ );++ expect(result.status).toBe(404);+ });+ });+// SCIM provisioning is gated behind the `admin-api` entitlement, matching the// sibling organization admin REST endpoints (memberships, projects, apiKeys).// Plans without that entitlement (e.g. Hobby) must be rejected before anydiff --git a/web/src/pages/api/public/scim/Users/[id].ts b/web/src/pages/api/public/scim/Users/[id].tsindex d9fcefd3ce..22fcd50b24 100644--- a/web/src/pages/api/public/scim/Users/[id].ts+++ b/web/src/pages/api/public/scim/Users/[id].ts@@ -455,27 +455,38 @@ export default async function handler(}}-// GET - Retrieve a specific user-async function handleGet(- req: NextApiRequest,+// Resolve the caller organization's membership for `userId`. When the user is+// not a member, write a 404 that never echoes any of the user's attributes and+// return null.+async function requireOrgMembership(res: NextApiResponse,- user: User,orgId: string,+ userId: string,) {- // For GET operations, verify the user is a member of the organizationconst orgMembership = await prisma.organizationMembership.findFirst({- where: {- orgId: orgId,- userId: user.id,- },+ where: { orgId, userId },});-if (!orgMembership) {- return res.status(404).json({+ res.status(404).json({schemas: ["urn:ietf:params:scim:api:messages:2.0:Error"],detail: "User not found in organization",status: 404,});+ return null;+ }+ return orgMembership;+}++// GET - Retrieve a specific user+async function handleGet(+ req: NextApiRequest,+ res: NextApiResponse,+ user: User,+ orgId: string,+) {+ // For GET operations, verify the user is a member of the organization.+ if (!(await requireOrgMembership(res, orgId, user.id))) {+ return;}// Transform to SCIM format@@ -656,6 +667,14 @@ async function handlePut(});}+ // A caller may only observe or mutate a user already tied to their+ // organization. The sole exception is provisioning (active:true), which+ // legitimately references a global user id in order to add the user.+ const isProvisioning = body.active === true;+ if (!isProvisioning && !(await requireOrgMembership(res, orgId, user.id))) {+ return;+ }+// Handle active status for provisioning/deprovisioning.//// `active: true` ensures the user is provisioned. An explicit `roles` value is@@ -717,6 +736,9 @@ async function handleDelete(orgId: string,apiKeyId: string,) {+ if (!(await requireOrgMembership(res, orgId, user.id))) {+ return;+ }// Deprovision atomically: check + delete in one Serializable txn.if (!(await deprovisionOrReject(res, user.id, orgId, apiKeyId, user.email))) {return;