Skip to content

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

fix(web): prevent chain (#12222)

Max Deichmann2548665e0a81
diff --git a/web/src/features/rbac/utils/checkAccess.clienttest.ts b/web/src/features/rbac/utils/checkAccess.clienttest.ts
new file mode 100644
index 0000000000..573cae50aa
--- /dev/null
+++ b/web/src/features/rbac/utils/checkAccess.clienttest.ts
@@ -0,0 +1,72 @@
+import { hasOrganizationAccess } from "./checkOrganizationAccess";
+import { hasProjectAccess } from "./checkProjectAccess";
+
+describe("RBAC access checks", () => {
+ it("ignores inherited role/admin properties for organization access", () => {
+ const params = Object.assign(
+ Object.create({
+ role: "OWNER",
+ admin: true,
+ }),
+ {
+ session: {
+ user: {
+ admin: false,
+ organizations: [{ id: "org-1", role: "MEMBER" }],
+ },
+ },
+ organizationId: "org-1",
+ scope: "organizationMembers:CUD",
+ },
+ );
+
+ expect(
+ hasOrganizationAccess(
+ params as Parameters<typeof hasOrganizationAccess>[0],
+ ),
+ ).toBe(false);
+ });
+
+ it("ignores inherited role/admin properties for project access", () => {
+ const params = Object.assign(
+ Object.create({
+ role: "OWNER",
+ admin: true,
+ }),
+ {
+ session: {
+ user: {
+ admin: false,
+ organizations: [
+ {
+ projects: [{ id: "project-1", role: "MEMBER" }],
+ },
+ ],
+ },
+ },
+ projectId: "project-1",
+ scope: "project:update",
+ },
+ );
+
+ expect(
+ hasProjectAccess(params as Parameters<typeof hasProjectAccess>[0]),
+ ).toBe(false);
+ });
+
+ it("still supports own role-based checks", () => {
+ expect(
+ hasOrganizationAccess({
+ role: "OWNER",
+ scope: "organizationMembers:CUD",
+ }),
+ ).toBe(true);
+
+ expect(
+ hasProjectAccess({
+ role: "ADMIN",
+ scope: "project:update",
+ }),
+ ).toBe(true);
+ });
+});
diff --git a/web/src/features/rbac/utils/checkOrganizationAccess.ts b/web/src/features/rbac/utils/checkOrganizationAccess.ts
index 5e81002087..7f25e29a3e 100644
--- a/web/src/features/rbac/utils/checkOrganizationAccess.ts
+++ b/web/src/features/rbac/utils/checkOrganizationAccess.ts
@@ -6,6 +6,7 @@ import { type Role } from "@langfuse/shared/src/db";
import { TRPCError } from "@trpc/server";
import { type Session } from "next-auth";
import { useSession } from "next-auth/react";
+import { hasOwnRole } from "./hasOwnRole";
type HasOrganizationAccessParams =
| {
@@ -55,15 +56,13 @@ export const useHasOrganizationAccess = (p: {
// For use in UI components as function, if session is already available
export function hasOrganizationAccess(p: HasOrganizationAccessParams): boolean {
- const isAdmin = "role" in p ? p.admin : p.session?.user?.admin;
+ const isAdmin = hasOwnRole(p) ? p.admin : p.session?.user?.admin;
if (isAdmin) return true;
- const organizationRole: Role | undefined =
- "role" in p
- ? p.role
- : p.session?.user?.organizations.find(
- (org) => org.id === p.organizationId,
- )?.role;
+ const organizationRole: Role | undefined = hasOwnRole(p)
+ ? p.role
+ : p.session?.user?.organizations.find((org) => org.id === p.organizationId)
+ ?.role;
if (organizationRole === undefined) return false;
return organizationRoleAccessRights[organizationRole].includes(p.scope);
diff --git a/web/src/features/rbac/utils/checkProjectAccess.ts b/web/src/features/rbac/utils/checkProjectAccess.ts
index 71444d8b61..31e23d3ffe 100644
--- a/web/src/features/rbac/utils/checkProjectAccess.ts
+++ b/web/src/features/rbac/utils/checkProjectAccess.ts
@@ -6,6 +6,7 @@ import { type Role } from "@langfuse/shared/src/db";
import { TRPCError } from "@trpc/server";
import { type Session } from "next-auth";
import { useSession } from "next-auth/react";
+import { hasOwnRole } from "./hasOwnRole";
type HasProjectAccessParams = (
| {
@@ -53,15 +54,14 @@ export const useHasProjectAccess = (p: {
// For use in UI components as function, if session is already available
export function hasProjectAccess(p: HasProjectAccessParams): boolean {
- const isAdmin = "role" in p ? p.admin : p.session?.user?.admin;
+ const isAdmin = hasOwnRole(p) ? p.admin : p.session?.user?.admin;
if (isAdmin) return true;
- const projectRole: Role | undefined =
- "role" in p
- ? p.role
- : p.session?.user?.organizations
- .flatMap((org) => org.projects)
- .find((project) => project.id === p.projectId)?.role;
+ const projectRole: Role | undefined = hasOwnRole(p)
+ ? p.role
+ : p.session?.user?.organizations
+ .flatMap((org) => org.projects)
+ .find((project) => project.id === p.projectId)?.role;
if (projectRole === undefined) return false;
return projectRoleAccessRights[projectRole].includes(p.scope);
diff --git a/web/src/features/rbac/utils/hasOwnRole.ts b/web/src/features/rbac/utils/hasOwnRole.ts
new file mode 100644
index 0000000000..1c7fd99d71
--- /dev/null
+++ b/web/src/features/rbac/utils/hasOwnRole.ts
@@ -0,0 +1,6 @@
+import { type Role } from "@langfuse/shared/src/db";
+
+export const hasOwnRole = <T extends object>(
+ p: T,
+): p is Extract<T, { role: Role }> =>
+ Object.prototype.hasOwnProperty.call(p, "role");