The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
fix(ui): hide model write affordances from view-only admin sessions
mateo-berri74fb398f9bad
diff --git a/ui/litellm-dashboard/src/app/(dashboard)/models-and-endpoints/components/AutoRouters/AutoRoutersPanel.test.tsx b/ui/litellm-dashboard/src/app/(dashboard)/models-and-endpoints/components/AutoRouters/AutoRoutersPanel.test.tsxindex 8c683f230e0..f460b77c2c7 100644--- a/ui/litellm-dashboard/src/app/(dashboard)/models-and-endpoints/components/AutoRouters/AutoRoutersPanel.test.tsx+++ b/ui/litellm-dashboard/src/app/(dashboard)/models-and-endpoints/components/AutoRouters/AutoRoutersPanel.test.tsx@@ -140,6 +140,7 @@ const renderPanel = (canModify = true) =>accessToken="token"userRole="Admin"userID="u-admin"+ isViewOnly={false}teams={null}createScope={canModify ? "unscoped-ok" : "forbidden"}/>,diff --git a/ui/litellm-dashboard/src/app/(dashboard)/models-and-endpoints/components/AutoRouters/AutoRoutersPanel.tsx b/ui/litellm-dashboard/src/app/(dashboard)/models-and-endpoints/components/AutoRouters/AutoRoutersPanel.tsxindex db5120cebce..5b53217f9c1 100644--- a/ui/litellm-dashboard/src/app/(dashboard)/models-and-endpoints/components/AutoRouters/AutoRoutersPanel.tsx+++ b/ui/litellm-dashboard/src/app/(dashboard)/models-and-endpoints/components/AutoRouters/AutoRoutersPanel.tsx@@ -21,12 +21,20 @@ interface AutoRoutersPanelProps {accessToken: string;userRole: string;userID: string | null;+ isViewOnly: boolean;teams: Team[] | null;/** Owned by the page, which knows how this caller must scope what they create. */createScope: ModelWriteScope;}-export function AutoRoutersPanel({ accessToken, userRole, userID, teams, createScope }: AutoRoutersPanelProps) {+export function AutoRoutersPanel({+ accessToken,+ userRole,+ userID,+ isViewOnly,+ teams,+ createScope,+}: AutoRoutersPanelProps) {const canCreate = createScope !== "forbidden";const { data: deployments, isLoading } = useAutoRouters();const invalidateAutoRouters = useInvalidateAutoRouters();@@ -39,8 +47,8 @@ export function AutoRoutersPanel({ accessToken, userRole, userID, teams, createSconst [isDeleting, setIsDeleting] = useState(false);const routers = useMemo(- () => toAutoRouterRows(deployments ?? [], { userRole, userID }, teams),- [deployments, userRole, userID, teams],+ () => toAutoRouterRows(deployments ?? [], { userRole, userID, isViewOnly }, teams),+ [deployments, userRole, userID, isViewOnly, teams],);const handleCreated = () => {diff --git a/ui/litellm-dashboard/src/app/(dashboard)/models-and-endpoints/components/AutoRouters/autoRouterRows.test.ts b/ui/litellm-dashboard/src/app/(dashboard)/models-and-endpoints/components/AutoRouters/autoRouterRows.test.tsindex 9944653b638..23585f6c110 100644--- a/ui/litellm-dashboard/src/app/(dashboard)/models-and-endpoints/components/AutoRouters/autoRouterRows.test.ts+++ b/ui/litellm-dashboard/src/app/(dashboard)/models-and-endpoints/components/AutoRouters/autoRouterRows.test.ts@@ -5,8 +5,9 @@ import { toAutoRouterRow, toAutoRouterRows } from "./autoRouterRows";// Existing cases assert resource classification, so they run as a proxy admin: the actor// gate is then a pass-through and canEdit/canDelete still reflect the row itself.-const ADMIN = { userRole: "Admin", userID: "u-admin" };-const TEAM_ADMIN = { userRole: "Internal User", userID: "u-team-admin" };+const ADMIN = { userRole: "Admin", userID: "u-admin", isViewOnly: false };+const TEAM_ADMIN = { userRole: "Internal User", userID: "u-team-admin", isViewOnly: false };+const VIEW_ONLY_ADMIN = { userRole: "Admin", userID: "u-viewer", isViewOnly: true };const complexityDeployment = {model_name: "tri-tier-router",@@ -224,7 +225,7 @@ describe("autoRouterRows actor gating", () => {{ team_id: "team-1", members_with_roles: [{ user_id: "u-team-admin", user_email: "t@t", role: "admin" }] },] as never;- const rowIn = (actor: { userRole: string; userID: string }, teamId: string | null) =>+ const rowIn = (actor: { userRole: string; userID: string; isViewOnly: boolean }, teamId: string | null) =>toAutoRouterRow({ ...complexityDeployment, model_info: { id: "cid-1", db_model: true, team_id: teamId } },0,@@ -259,4 +260,12 @@ describe("autoRouterRows actor gating", () => {expect(row.canEdit).toBe(true);expect(row.canDelete).toBe(true);});++ // A proxy_admin_viewer session reads "Admin" through the masquerade, but PATCH and+ // DELETE both 403 it, so its rows must not offer the affordances.+ it("hides write affordances from a view-only admin session", () => {+ const row = rowIn(VIEW_ONLY_ADMIN, null);+ expect(row.canEdit).toBe(false);+ expect(row.canDelete).toBe(false);+ });});diff --git a/ui/litellm-dashboard/src/app/(dashboard)/models-and-endpoints/page.test.tsx b/ui/litellm-dashboard/src/app/(dashboard)/models-and-endpoints/page.test.tsxindex 521f89a39f2..8bf3f6db8dd 100644--- a/ui/litellm-dashboard/src/app/(dashboard)/models-and-endpoints/page.test.tsx+++ b/ui/litellm-dashboard/src/app/(dashboard)/models-and-endpoints/page.test.tsx@@ -38,8 +38,17 @@ vi.mock("./useModelDashboardData", () => ({useModelDashboardData: () => ({ availableModelAccessGroups: [], allModelsOnProxy: [], availableModelGroups: [] }),}));-const ADMIN = { accessToken: "at", token: "t", userRole: "Admin", userId: "u1", premiumUser: false };-const NON_ADMIN = { accessToken: "at", token: "t", userRole: "Internal User", userId: "u1", premiumUser: false };+const ADMIN = { accessToken: "at", token: "t", userRole: "Admin", userId: "u1", premiumUser: false, isViewOnly: false };+const NON_ADMIN = {+ accessToken: "at",+ token: "t",+ userRole: "Internal User",+ userId: "u1",+ premiumUser: false,+ isViewOnly: false,+};+// A proxy_admin_viewer session: effectiveSessionRole masquerades the role as "Admin".+const VIEW_ONLY_ADMIN = { ...ADMIN, isViewOnly: true };const renderPage = () => {const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false, gcTime: 0 } } });@@ -99,6 +108,22 @@ describe("ModelsAndEndpointsPage", () => {expect(queryByRole("tab", { name: "Health Status" })).not.toBeInTheDocument();});+ // POST /model/new 403s a proxy_admin_viewer, so the form's tab must not render for one.+ it("hides the Add Model tab for a view-only admin session", () => {+ mockUseAuthorized.mockReturnValue(VIEW_ONLY_ADMIN);+ const { getByRole, queryByRole } = renderPage();+ expect(queryByRole("tab", { name: "Add Model" })).not.toBeInTheDocument();+ expect(getByRole("tab", { name: "All Models" })).toBeInTheDocument();+ });++ // Read parity: the Auto-Routers list stays reachable for a view-only admin; only the+ // create affordance inside it is withheld, which AutoRoutersTabPanel decides.+ it("keeps the Auto-Routers tab for a view-only admin session", () => {+ mockUseAuthorized.mockReturnValue(VIEW_ONLY_ADMIN);+ const { getByRole } = renderPage();+ expect(getByRole("tab", { name: /Auto-Routers/ })).toBeInTheDocument();+ });+// Auto-routers are excluded from the All Models table, so this tab is their home: the only// place in the product to list, create, edit or delete one.describe("Auto-Routers tab", () => {diff --git a/ui/litellm-dashboard/src/app/(dashboard)/models-and-endpoints/page.tsx b/ui/litellm-dashboard/src/app/(dashboard)/models-and-endpoints/page.tsxindex 9ae7dc12f81..34c9d87004e 100644--- a/ui/litellm-dashboard/src/app/(dashboard)/models-and-endpoints/page.tsx+++ b/ui/litellm-dashboard/src/app/(dashboard)/models-and-endpoints/page.tsx@@ -80,7 +80,7 @@ const renderPanel = (key: string) => {};export default function ModelsAndEndpointsPage() {- const { accessToken, userRole, userId: userID, premiumUser } = useAuthorized();+ const { accessToken, userRole, userId: userID, premiumUser, isViewOnly } = useAuthorized();const { data: teams } = useTeams();const { data: uiSettings } = useUISettings();const queryClient = useQueryClient();@@ -92,7 +92,7 @@ export default function ModelsAndEndpointsPage() {const isInternalUser = userRole && internalUserRoles.includes(userRole);const canCreate = canCreateModels(- { userRole, userID },+ { userRole, userID, isViewOnly },{teams: teams ?? null,disabledForInternalUsers:@@ -182,6 +182,7 @@ export default function ModelsAndEndpointsPage() {accessToken={accessToken}userID={userID}userRole={userRole}+ isViewOnly={isViewOnly}onModelUpdate={invalidateModels}modelAccessGroups={availableModelAccessGroups}/>diff --git a/ui/litellm-dashboard/src/app/(dashboard)/models-and-endpoints/panels/AutoRoutersTabPanel.test.tsx b/ui/litellm-dashboard/src/app/(dashboard)/models-and-endpoints/panels/AutoRoutersTabPanel.test.tsxnew file mode 100644index 00000000000..12f0b95bf13--- /dev/null+++ b/ui/litellm-dashboard/src/app/(dashboard)/models-and-endpoints/panels/AutoRoutersTabPanel.test.tsx@@ -0,0 +1,39 @@+/* @vitest-environment jsdom */+import { render } from "@testing-library/react";+import { describe, expect, it, vi } from "vitest";+import AutoRoutersTabPanel from "./AutoRoutersTabPanel";++const panelProps = vi.fn();+vi.mock("../components/AutoRouters/AutoRoutersPanel", () => ({+ AutoRoutersPanel: (props: Record<string, unknown>) => {+ panelProps(props);+ return <div data-testid="auto-routers-panel" />;+ },+}));++const mockUseAuthorized = vi.fn();+vi.mock("@/app/(dashboard)/hooks/useAuthorized", () => ({ default: () => mockUseAuthorized() }));+vi.mock("@/app/(dashboard)/hooks/teams/useTeams", () => ({ useTeams: () => ({ data: [] }) }));+vi.mock("@/app/(dashboard)/hooks/uiSettings/useUISettings", () => ({+ useUISettings: () => ({ data: { values: {} } }),+}));++const SESSION = { accessToken: "at", userRole: "Admin", userId: "u1", isViewOnly: false };++const lastProps = () => panelProps.mock.calls.at(-1)?.[0] as { createScope: string };++describe("AutoRoutersTabPanel", () => {+ it("grants an unscoped create to a real proxy admin", () => {+ mockUseAuthorized.mockReturnValue(SESSION);+ render(<AutoRoutersTabPanel />);+ expect(lastProps().createScope).toBe("unscoped-ok");+ });++ // The masqueraded "Admin" a proxy_admin_viewer session carries: POST /model/new 403s it,+ // so the panel must not be told it may create.+ it("withholds the create affordance from a view-only admin session", () => {+ mockUseAuthorized.mockReturnValue({ ...SESSION, isViewOnly: true });+ render(<AutoRoutersTabPanel />);+ expect(lastProps().createScope).toBe("forbidden");+ });+});diff --git a/ui/litellm-dashboard/src/app/(dashboard)/models-and-endpoints/panels/AutoRoutersTabPanel.tsx b/ui/litellm-dashboard/src/app/(dashboard)/models-and-endpoints/panels/AutoRoutersTabPanel.tsxindex 5f7d56e8e33..69b442da09b 100644--- a/ui/litellm-dashboard/src/app/(dashboard)/models-and-endpoints/panels/AutoRoutersTabPanel.tsx+++ b/ui/litellm-dashboard/src/app/(dashboard)/models-and-endpoints/panels/AutoRoutersTabPanel.tsx@@ -15,13 +15,13 @@ import { AutoRoutersPanel } from "../components/AutoRouters/AutoRoutersPanel";* Viewer roles reach the list without write affordances.*/export default function AutoRoutersTabPanel() {- const { accessToken, userRole, userId: userID } = useAuthorized();+ const { accessToken, userRole, userId: userID, isViewOnly } = useAuthorized();const { data: teams } = useTeams();const { data: uiSettings } = useUISettings();const isInternalUser = userRole != null && internalUserRoles.includes(userRole);const scope = modelCreationScope(- { userRole, userID },+ { userRole, userID, isViewOnly },{teams: teams ?? null,disabledForInternalUsers: isInternalUser && uiSettings?.values?.disable_model_add_for_internal_users === true,@@ -33,6 +33,7 @@ export default function AutoRoutersTabPanel() {accessToken={accessToken}userRole={userRole ?? ""}userID={userID ?? null}+ isViewOnly={isViewOnly}teams={teams ?? null}createScope={scope}/>diff --git a/ui/litellm-dashboard/src/components/add_model/AddModelForm.tsx b/ui/litellm-dashboard/src/components/add_model/AddModelForm.tsxindex ad0f749b189..92951b68fd5 100644--- a/ui/litellm-dashboard/src/components/add_model/AddModelForm.tsx+++ b/ui/litellm-dashboard/src/components/add_model/AddModelForm.tsx@@ -82,7 +82,7 @@ const AddModelForm: React.FC<AddModelFormProps> = ({// Using a unique ID to force the ConnectionErrorDisplay to remount and run a fresh testconst [connectionTestId, setConnectionTestId] = useState<string>("");- const { accessToken, userRole, premiumUser, userId } = useAuthorized();+ const { accessToken, userRole, premiumUser, userId, isViewOnly } = useAuthorized();const {data: providerMetadata,isLoading: isProviderMetadataLoading,@@ -157,7 +157,10 @@ const AddModelForm: React.FC<AddModelFormProps> = ({const isTeamAdmin = isUserTeamAdminForAnyTeam(teams, userId);// Same owner the Auto-Routers tab uses, so the two creation forms cannot disagree about// who has to name a team. This form is only reachable when creation is allowed at all.- const createScope = modelCreationScope({ userRole, userID: userId }, { teams, disabledForInternalUsers: false });+ const createScope = modelCreationScope(+ { userRole, userID: userId, isViewOnly },+ { teams, disabledForInternalUsers: false },+ );const requiresTeamScope = createScope === "team-required";return (diff --git a/ui/litellm-dashboard/src/components/model_info_view.test.tsx b/ui/litellm-dashboard/src/components/model_info_view.test.tsxindex 742d2593ade..768183907db 100644--- a/ui/litellm-dashboard/src/components/model_info_view.test.tsx+++ b/ui/litellm-dashboard/src/components/model_info_view.test.tsx@@ -87,6 +87,7 @@ describe("ModelInfoView", () => {accessToken: "test-token",userID: "123",userRole: "Admin",+ isViewOnly: false,onModelUpdate: vi.fn(),modelAccessGroups: ["group1", "group2"],};@@ -328,6 +329,16 @@ describe("ModelInfoView", () => {});});+ // A proxy_admin_viewer session reads "Admin" through effectiveSessionRole, but the update+ // and delete endpoints 403 it, so the write buttons must not be offered.+ it("should disable delete and update buttons for a view-only admin session", async () => {+ render(<ModelInfoView {...DEFAULT_ADMIN_PROPS} isViewOnly={true} />, { wrapper });+ await waitFor(() => {+ expect(screen.getByTestId("delete-model-button")).toBeDisabled();+ });+ expect(screen.getByTestId("update-api-key-button")).toBeDisabled();+ });+it("should disable delete button when model is not a DB model", async () => {const nonDbModelData = {...defaultModelData,diff --git a/ui/litellm-dashboard/src/components/model_info_view.tsx b/ui/litellm-dashboard/src/components/model_info_view.tsxindex f21e98e084c..35afcdb2985 100644--- a/ui/litellm-dashboard/src/components/model_info_view.tsx+++ b/ui/litellm-dashboard/src/components/model_info_view.tsx@@ -53,6 +53,7 @@ interface ModelInfoViewProps {accessToken: string | null;userID: string | null;userRole: string | null;+ isViewOnly: boolean;onModelUpdate?: (updatedModel: any) => void;modelAccessGroups: string[] | null;}@@ -117,6 +118,7 @@ export default function ModelInfoView({accessToken,userID,userRole,+ isViewOnly,onModelUpdate,modelAccessGroups,}: ModelInfoViewProps) {@@ -167,7 +169,7 @@ export default function ModelInfoView({// Keep modelData variable name for backwards compatibilityconst modelData = transformedModelData;- const canEditModel = canModifyModel({ userRole, userID }, teams ?? null, {+ const canEditModel = canModifyModel({ userRole, userID, isViewOnly }, teams ?? null, {teamId: modelData?.model_info?.team_id,isDbModel: modelData?.model_info?.db_model === true,});diff --git a/ui/litellm-dashboard/src/utils/modelPermissions.test.ts b/ui/litellm-dashboard/src/utils/modelPermissions.test.tsindex 179a9b7933a..6778c92a864 100644--- a/ui/litellm-dashboard/src/utils/modelPermissions.test.ts+++ b/ui/litellm-dashboard/src/utils/modelPermissions.test.ts@@ -1,14 +1,16 @@import { describe, expect, it } from "vitest";import { Team } from "@/components/networking";-import { canModifyModel, modelCreationScope } from "./modelPermissions";+import { canCreateModels, canModifyModel, modelCreationScope } from "./modelPermissions";const teamWhere = (userId: string, role: string, teamId = "team-1"): Team[] =>[{ team_id: teamId, members_with_roles: [{ user_id: userId, user_email: "[email protected]", role }] }] as unknown as Team[];-const PROXY_ADMIN = { userRole: "Admin", userID: "u-admin" };-const TEAM_ADMIN = { userRole: "Internal User", userID: "u-team-admin" };-const MEMBER = { userRole: "Internal User", userID: "u-member" };+const PROXY_ADMIN = { userRole: "Admin", userID: "u-admin", isViewOnly: false };+const TEAM_ADMIN = { userRole: "Internal User", userID: "u-team-admin", isViewOnly: false };+const MEMBER = { userRole: "Internal User", userID: "u-member", isViewOnly: false };+// proxy_admin_viewer sessions: effectiveSessionRole masquerades the role as "Admin".+const VIEW_ONLY_ADMIN = { userRole: "Admin", userID: "u-viewer", isViewOnly: true };const noLimits = { disabledForInternalUsers: false };@@ -40,9 +42,24 @@ describe("modelCreationScope", () => {// an unscoped create from them 403s. Treating them as admins here is what let a form submit// a payload the backend always rejected.it("does not treat an org admin as able to create unscoped", () => {- const orgAdmin = { userRole: "org_admin", userID: "u-org" };+ const orgAdmin = { userRole: "org_admin", userID: "u-org", isViewOnly: false };expect(modelCreationScope(orgAdmin, { teams: teamWhere("u-org", "admin"), ...noLimits })).toBe("team-required");});++ // Server-side, POST /model/new 403s the viewer roles, so the "Admin" the masquerade+ // reports must not read as a proxy admin here.+ it("forbids a view-only admin session despite the masqueraded Admin role", () => {+ expect(modelCreationScope(VIEW_ONLY_ADMIN, { teams: [], ...noLimits })).toBe("forbidden");+ expect(canCreateModels(VIEW_ONLY_ADMIN, { teams: [], ...noLimits })).toBe(false);+ });++ // A blunt view-only gate would fail this: team-admin membership legitimately grants+ // team-scoped creation, whatever the session role says.+ it("still requires a team from a view-only admin who admins a team", () => {+ expect(modelCreationScope(VIEW_ONLY_ADMIN, { teams: teamWhere("u-viewer", "admin"), ...noLimits })).toBe(+ "team-required",+ );+ });});describe("canModifyModel", () => {@@ -80,6 +97,15 @@ describe("canModifyModel", () => {});it("does not treat two absent identities as a match", () => {- expect(canModifyModel({ userRole: "Internal User", userID: null }, null, teamRow)).toBe(false);+ expect(canModifyModel({ userRole: "Internal User", userID: null, isViewOnly: false }, null, teamRow)).toBe(false);+ });++ // PATCH /model/{id}/update and POST /model/delete 403 the viewer roles like /model/new does.+ it("refuses a view-only admin session on a DB row", () => {+ expect(canModifyModel(VIEW_ONLY_ADMIN, null, teamRow)).toBe(false);+ });++ it("lets a view-only user who admins the owning team act on its row", () => {+ expect(canModifyModel(VIEW_ONLY_ADMIN, teamWhere("u-viewer", "admin"), teamRow)).toBe(true);});});diff --git a/ui/litellm-dashboard/src/utils/modelPermissions.ts b/ui/litellm-dashboard/src/utils/modelPermissions.tsindex b5914f9d7ea..9815ac9f0e2 100644--- a/ui/litellm-dashboard/src/utils/modelPermissions.ts+++ b/ui/litellm-dashboard/src/utils/modelPermissions.ts@@ -15,8 +15,17 @@ import { isProxyAdminRole, isUserTeamAdminForAnyTeam, isUserTeamAdminForSingleTeexport interface ModelActor {userRole: string | null;userID: string | null;+ /**+ * From useAuthorized(). A proxy_admin_viewer session masquerades as "Admin" in userRole+ * (effectiveSessionRole, for read parity), yet every management write 403s it, so the role+ * alone cannot answer a write question.+ */+ isViewOnly: boolean;}+const isWritableProxyAdmin = ({ userRole, isViewOnly }: ModelActor): boolean =>+ !isViewOnly && userRole != null && isProxyAdminRole(userRole);+/** How this actor must scope a deployment they create, or that they may not create one. */export type ModelWriteScope = "forbidden" | "unscoped-ok" | "team-required";@@ -37,16 +46,16 @@ const isTeamAdminOf = (teams: Team[] | null, userID: string, teamId: string): bo* pair of booleans keeps "may not create" and "may create unscoped" from being confused.*/export const modelCreationScope = (- { userRole, userID }: ModelActor,+ actor: ModelActor,{ teams, disabledForInternalUsers }: ModelCreationLimits,): ModelWriteScope => {- if (userRole != null && isProxyAdminRole(userRole)) {+ if (isWritableProxyAdmin(actor)) {return "unscoped-ok";}if (disabledForInternalUsers) {return "forbidden";}- if (userID != null && isUserTeamAdminForAnyTeam(teams, userID)) {+ if (actor.userID != null && isUserTeamAdminForAnyTeam(teams, actor.userID)) {return "team-required";}return "forbidden";@@ -63,18 +72,18 @@ export interface ModelRowOrigin {/** May this actor edit or delete this specific deployment? */export const canModifyModel = (- { userRole, userID }: ModelActor,+ actor: ModelActor,teams: Team[] | null,{ teamId, isDbModel }: ModelRowOrigin,): boolean => {if (!isDbModel) {return false;}- if (userRole != null && isProxyAdminRole(userRole)) {+ if (isWritableProxyAdmin(actor)) {return true;}- if (userID == null || teamId == null) {+ if (actor.userID == null || teamId == null) {return false;}- return isTeamAdminOf(teams, userID, teamId);+ return isTeamAdminOf(teams, actor.userID, teamId);};