Skip to content

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]

fix(ui): gate the Old Usage page behind a proxy-admin capability

Yuneng Jiang5096fc792742
diff --git a/ui/litellm-dashboard/src/app/(dashboard)/old-usage/_components/usage.test.tsx b/ui/litellm-dashboard/src/app/(dashboard)/old-usage/_components/usage.test.tsx
index e3db50b7300..4cf210c6e38 100644
--- a/ui/litellm-dashboard/src/app/(dashboard)/old-usage/_components/usage.test.tsx
+++ b/ui/litellm-dashboard/src/app/(dashboard)/old-usage/_components/usage.test.tsx
@@ -1,6 +1,6 @@
import React from "react";
import { describe, it, expect, vi, beforeEach } from "vitest";
-import { screen, waitFor, within } from "@testing-library/react";
+import { act, screen, waitFor, within } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { renderWithProviders } from "../../../../../tests/test-utils";
import UsagePage from "./usage";
@@ -49,6 +49,17 @@ const renderUsage = (overrides: Partial<React.ComponentProps<typeof UsagePage>>
/>,
);
+// Mount fires two effects whose requests sit behind a promise chain
+// (proxy settings, then the spend query). "proves the flush window is wide
+// enough" below keeps this honest: it asserts the same flush surfaces those
+// requests for an admin, so a denied role's silence means the gate held.
+const flushPendingRequests = async () => {
+ await act(async () => {
+ await new Promise((resolve) => setTimeout(resolve, 0));
+ await new Promise((resolve) => setTimeout(resolve, 0));
+ });
+};
+
beforeEach(() => {
vi.clearAllMocks();
networking.getProxyUISettings.mockResolvedValue(UNLIMITED_SETTINGS);
@@ -185,18 +196,67 @@ describe("old usage page", () => {
});
});
- describe("as a non-admin", () => {
- it("renders only the All Up tab and skips admin-only queries", async () => {
- renderUsage({ userRole: "Internal User" });
+ // Every role below is served 401 on /global/spend/* by the proxy. Org admins
+ // and team admins reach the UI as "Internal User" — `org_admin` is an
+ // organization membership role, never a top-level user_role.
+ describe.each(["Internal User", "Internal Viewer", "internal_user", "internal_user_viewer", "Org Admin"])(
+ "as %s",
+ (userRole) => {
+ it("shows the admin-only notice instead of the usage dashboard", async () => {
+ renderUsage({ userRole });
+
+ expect(await screen.findByText(/Proxy-wide usage is only available to admin users/i)).toBeInTheDocument();
+ expect(screen.queryByRole("tab", { name: "All Up" })).not.toBeInTheDocument();
+ });
- expect(await screen.findByRole("tab", { name: "All Up" })).toBeInTheDocument();
- expect(screen.queryByRole("tab", { name: "Team Based Usage" })).not.toBeInTheDocument();
- expect(screen.queryByRole("tab", { name: "Customer Usage" })).not.toBeInTheDocument();
- expect(screen.queryByRole("tab", { name: "Tag Based Usage" })).not.toBeInTheDocument();
+ it("fires no /global/spend or /global/activity request", async () => {
+ renderUsage({ userRole });
+
+ await screen.findByText(/Proxy-wide usage is only available to admin users/i);
+ await flushPendingRequests();
+
+ expect(networking.getProxyUISettings).not.toHaveBeenCalled();
+ expect(networking.adminSpendLogsCall).not.toHaveBeenCalled();
+ expect(networking.adminTopKeysCall).not.toHaveBeenCalled();
+ expect(networking.adminTopModelsCall).not.toHaveBeenCalled();
+ expect(networking.adminTopEndUsersCall).not.toHaveBeenCalled();
+ expect(networking.teamSpendLogsCall).not.toHaveBeenCalled();
+ expect(networking.tagsSpendLogsCall).not.toHaveBeenCalled();
+ expect(networking.allTagNamesCall).not.toHaveBeenCalled();
+ expect(networking.adminspendByProvider).not.toHaveBeenCalled();
+ expect(networking.adminGlobalActivity).not.toHaveBeenCalled();
+ expect(networking.adminGlobalActivityPerModel).not.toHaveBeenCalled();
+ });
+ },
+ );
+
+ describe("the admin-only gate", () => {
+ it("proves the flush window is wide enough to catch a leaked request", async () => {
+ renderUsage({ userRole: "Admin" });
+ await flushPendingRequests();
+
+ expect(networking.getProxyUISettings).toHaveBeenCalled();
+ expect(networking.adminSpendLogsCall).toHaveBeenCalled();
+ expect(networking.tagsSpendLogsCall).toHaveBeenCalled();
+ expect(networking.adminGlobalActivity).toHaveBeenCalled();
+ });
+
+ it("still lets an admin through, so the notice is a real gate and not a dead branch", async () => {
+ renderUsage({ userRole: "Admin" });
+
+ expect(await screen.findByRole("tab", { name: "All Up" })).toBeInTheDocument();
+ expect(screen.queryByText(/Proxy-wide usage is only available to admin users/i)).not.toBeInTheDocument();
await waitFor(() => expect(networking.adminSpendLogsCall).toHaveBeenCalled());
- expect(networking.teamSpendLogsCall).not.toHaveBeenCalled();
- expect(networking.adminTopEndUsersCall).not.toHaveBeenCalled();
+ });
+
+ it("does not put the session token in the provider spend query", async () => {
+ renderUsage({ userRole: "Admin", token: "session-jwt-value" });
+
+ await waitFor(() => expect(networking.adminspendByProvider).toHaveBeenCalled());
+ const callArgs = networking.adminspendByProvider.mock.calls[0];
+ expect(callArgs).not.toContain("session-jwt-value");
+ expect(callArgs[0]).toBe("sk-test");
});
});
});
diff --git a/ui/litellm-dashboard/src/app/(dashboard)/old-usage/_components/usage.tsx b/ui/litellm-dashboard/src/app/(dashboard)/old-usage/_components/usage.tsx
index 3d55f9bb698..5b2f8547822 100644
--- a/ui/litellm-dashboard/src/app/(dashboard)/old-usage/_components/usage.tsx
+++ b/ui/litellm-dashboard/src/app/(dashboard)/old-usage/_components/usage.tsx
@@ -37,6 +37,7 @@ import {
} from "@/components/networking";
import TopKeyView from "@/components/UsagePage/components/EntityUsage/TopKeyView";
import { MoneyCell } from "@/components/shared/table_cells";
+import { hasCapability } from "@/utils/capabilities";
import { formatNumberWithCommas } from "@/utils/dataUtils";
interface UsagePageProps {
@@ -90,6 +91,7 @@ const TeamSpendBarList: React.FC<{ data: TeamSpendTotal[] }> = ({ data }) => {
};
const UsagePage: React.FC<UsagePageProps> = ({ accessToken, token, userRole, userID, keys, premiumUser }) => {
+ const canViewGlobalSpend = hasCapability(userRole, "viewGlobalSpend");
const currentDate = new Date();
const [keySpendData, setKeySpendData] = useState<any[]>([]);
const [topKeys, setTopKeys] = useState<any[]>([]);
@@ -155,8 +157,11 @@ const UsagePage: React.FC<UsagePageProps> = ({ accessToken, token, userRole, use
};
useEffect(() => {
+ if (!canViewGlobalSpend) {
+ return;
+ }
updateTagSpendData(dateValue.from, dateValue.to);
- }, [dateValue, selectedTags]);
+ }, [canViewGlobalSpend, dateValue, selectedTags]);
const updateEndUserData = async (
startTime: Date | undefined,
@@ -319,10 +324,7 @@ const UsagePage: React.FC<UsagePageProps> = ({ accessToken, token, userRole, use
const fetchProviderSpend = () =>
fetchAndSetData(
- () =>
- accessToken && token
- ? adminspendByProvider(accessToken, token, startTime, endTime)
- : Promise.reject("No access token or token"),
+ () => (accessToken ? adminspendByProvider(accessToken, startTime, endTime) : Promise.reject("No access token")),
setSpendByProvider,
"Error fetching provider spend",
);
@@ -467,6 +469,9 @@ const UsagePage: React.FC<UsagePageProps> = ({ accessToken, token, userRole, use
useEffect(() => {
const initlizeUsageData = async () => {
+ if (!canViewGlobalSpend) {
+ return;
+ }
if (accessToken && token && userRole && userID) {
const proxy_settings: ProxySettings | undefined = await fetchProxySettings();
if (proxy_settings) {
@@ -493,7 +498,24 @@ const UsagePage: React.FC<UsagePageProps> = ({ accessToken, token, userRole, use
};
initlizeUsageData();
- }, [accessToken, token, userRole, userID, startTime, endTime]);
+ }, [canViewGlobalSpend, accessToken, token, userRole, userID, startTime, endTime]);
+
+ if (!canViewGlobalSpend) {
+ return (
+ <div className="w-full p-8">
+ <Card>
+ <CardHeader>
+ <CardTitle>Usage</CardTitle>
+ </CardHeader>
+ <CardContent>
+ <p className="text-sm text-muted-foreground">
+ Proxy-wide usage is only available to admin users. Your own usage is on the Usage page.
+ </p>
+ </CardContent>
+ </Card>
+ </div>
+ );
+ }
if (proxySettings?.DISABLE_EXPENSIVE_DB_QUERIES) {
return (
diff --git a/ui/litellm-dashboard/src/components/leftnav.test.tsx b/ui/litellm-dashboard/src/components/leftnav.test.tsx
index f795076ff03..04aae64c768 100644
--- a/ui/litellm-dashboard/src/components/leftnav.test.tsx
+++ b/ui/litellm-dashboard/src/components/leftnav.test.tsx
@@ -6,6 +6,7 @@ import Sidebar, { menuGroups, getBreadcrumb } from "./leftnav";
vi.mock("../utils/roles", () => {
return {
all_admin_roles: ["admin", "admin_viewer"],
+ old_admin_roles: ["admin", "admin_viewer"],
internalUserRoles: ["internal"],
rolesWithWriteAccess: ["admin", "internal"],
rolesAllowedToViewWriteScopedPages: ["admin", "internal", "admin_viewer"],
@@ -266,6 +267,30 @@ describe("Sidebar (leftnav)", () => {
});
expect(screen.queryByText("Prompts")).not.toBeInTheDocument();
});
+
+ it("should hide Old Usage from internal users while keeping other Experimental children", async () => {
+ mockUseAuthorized.mockReturnValue(internalAuth);
+ renderWithProviders(<Sidebar {...defaultProps} />);
+
+ act(() => {
+ fireEvent.click(screen.getByText("Experimental"));
+ });
+ await waitFor(() => {
+ expect(screen.getByText("API Playground")).toBeInTheDocument();
+ });
+ expect(screen.queryByText("Old Usage")).not.toBeInTheDocument();
+ });
+
+ it("should show Old Usage to admins", async () => {
+ renderWithProviders(<Sidebar {...defaultProps} />);
+
+ act(() => {
+ fireEvent.click(screen.getByText("Experimental"));
+ });
+ await waitFor(() => {
+ expect(screen.getByText("Old Usage")).toBeInTheDocument();
+ });
+ });
});
it("should show Organizations tab for organization admins", () => {
diff --git a/ui/litellm-dashboard/src/components/leftnav.tsx b/ui/litellm-dashboard/src/components/leftnav.tsx
index 12d124b059e..805428f8cd8 100644
--- a/ui/litellm-dashboard/src/components/leftnav.tsx
+++ b/ui/litellm-dashboard/src/components/leftnav.tsx
@@ -289,7 +289,13 @@ const menuGroups: MenuGroup[] = [
icon: <Tags {...ICON} />,
roles: all_admin_roles,
},
- { key: "4", page: "usage", label: "Old Usage", icon: <BarChart3 {...ICON} /> },
+ {
+ key: "4",
+ page: "usage",
+ label: "Old Usage",
+ icon: <BarChart3 {...ICON} />,
+ roles: rolesWithCapability("viewGlobalSpend"),
+ },
],
},
],
diff --git a/ui/litellm-dashboard/src/components/networking.tsx b/ui/litellm-dashboard/src/components/networking.tsx
index 17a5ca37990..25c87560cbd 100644
--- a/ui/litellm-dashboard/src/components/networking.tsx
+++ b/ui/litellm-dashboard/src/components/networking.tsx
@@ -2098,7 +2098,6 @@ export const adminTopEndUsersCall = async (
export const adminspendByProvider = async (
accessToken: string,
- keyToken: string | null,
startTime: string | undefined,
endTime: string | undefined,
) => {
@@ -2107,7 +2106,6 @@ export const adminspendByProvider = async (
accessToken,
query: {
...(startTime && endTime ? { start_date: startTime, end_date: endTime } : {}),
- ...(keyToken ? { api_key: keyToken } : {}),
},
});
return data;
diff --git a/ui/litellm-dashboard/src/utils/capabilities.test.ts b/ui/litellm-dashboard/src/utils/capabilities.test.ts
index 858658309ec..ab3358b80ac 100644
--- a/ui/litellm-dashboard/src/utils/capabilities.test.ts
+++ b/ui/litellm-dashboard/src/utils/capabilities.test.ts
@@ -1,6 +1,7 @@
import { describe, expect, it } from "vitest";
import { hasCapability, rolesWithCapability } from "./capabilities";
+import { effectiveSessionRole } from "./roles";
describe("hasCapability", () => {
it.each(["Admin", "Admin Viewer", "proxy_admin", "proxy_admin_viewer"])(
@@ -67,6 +68,51 @@ describe.each(["viewAuditLogs", "viewDeletedTeams"] as const)("hasCapability - %
);
});
+// Backend truth table for the `/global/spend/*` routes the Old Usage page calls
+// (verified against a live proxy): only proxy_admin and proxy_admin_viewer are
+// served. Org admins and team admins carry `internal_user` as their top-level
+// user_role, so `effectiveSessionRole` renders them "Internal User" — an org
+// admin never reaches the UI as "Org Admin" or `org_admin`.
+describe("hasCapability - viewGlobalSpend", () => {
+ it.each(["Admin", "Admin Viewer", "proxy_admin", "proxy_admin_viewer"])("should grant it to %s", (role) => {
+ expect(hasCapability(role, "viewGlobalSpend")).toBe(true);
+ });
+
+ it.each([
+ "Internal User",
+ "Internal Viewer",
+ "internal_user",
+ "internal_user_viewer",
+ "Org Admin",
+ "org_admin",
+ "App User",
+ "Unknown Role",
+ "",
+ null,
+ undefined,
+ ])("should deny it to %s", (role) => {
+ expect(hasCapability(role, "viewGlobalSpend")).toBe(false);
+ });
+
+ it("should deny it to every role an org admin or team admin can present at runtime", () => {
+ const orgAdminSessionRole = effectiveSessionRole("internal_user");
+ const teamAdminSessionRole = effectiveSessionRole("internal_user");
+
+ expect(orgAdminSessionRole).toBe("Internal User");
+ expect(hasCapability(orgAdminSessionRole, "viewGlobalSpend")).toBe(false);
+ expect(hasCapability(teamAdminSessionRole, "viewGlobalSpend")).toBe(false);
+ });
+
+ it.each([
+ ["proxy_admin", true],
+ ["proxy_admin_viewer", true],
+ ["internal_user", false],
+ ["internal_user_viewer", false],
+ ] as const)("should match the backend for a %s session", (rawRole, expected) => {
+ expect(hasCapability(effectiveSessionRole(rawRole), "viewGlobalSpend")).toBe(expected);
+ });
+});
+
describe("rolesWithCapability", () => {
it("should return a copy so callers cannot mutate the capability map", () => {
const roles = rolesWithCapability("viewToolPolicies");
diff --git a/ui/litellm-dashboard/src/utils/capabilities.ts b/ui/litellm-dashboard/src/utils/capabilities.ts
index 8171ef9a512..014bf8530f4 100644
--- a/ui/litellm-dashboard/src/utils/capabilities.ts
+++ b/ui/litellm-dashboard/src/utils/capabilities.ts
@@ -1,4 +1,6 @@
-import { all_admin_roles } from "./roles";
+import { all_admin_roles, old_admin_roles } from "./roles";
+
+const proxyAdminOnlyRoles = [...old_admin_roles, "proxy_admin", "proxy_admin_viewer"];
const CAPABILITY_ROLES = {
viewToolPolicies: all_admin_roles,
@@ -6,6 +8,7 @@ const CAPABILITY_ROLES = {
viewDeletedTeams: all_admin_roles,
viewPolicies: all_admin_roles,
viewPrompts: all_admin_roles,
+ viewGlobalSpend: proxyAdminOnlyRoles,
} as const satisfies Record<string, readonly string[]>;
export type Capability = keyof typeof CAPABILITY_ROLES;