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): resolve login redirect loop when reverse proxy adds HttpOnly to cookies (#23532)
Hendrik Jaksf233520c4406
diff --git a/litellm/proxy/proxy_server.py b/litellm/proxy/proxy_server.pyindex 9738ae4f1a2..accf669f2e3 100644--- a/litellm/proxy/proxy_server.py+++ b/litellm/proxy/proxy_server.py@@ -11506,8 +11506,11 @@ async def login_v2(request: Request): # noqa: PLR0915litellm_dashboard_ui += "/ui/"litellm_dashboard_ui += "?login=success"+ # Token is included in the response body so the UI can set a JS-accessible+ # cookie even when a reverse proxy (e.g. nginx-ingress) adds HttpOnly to the+ # server-set cookie, which would otherwise cause an infinite login redirect.json_response = JSONResponse(- content={"redirect_url": litellm_dashboard_ui},+ content={"redirect_url": litellm_dashboard_ui, "token": jwt_token},status_code=status.HTTP_200_OK,)json_response.set_cookie(key="token", value=jwt_token)diff --git a/ui/litellm-dashboard/src/app/page.tsx b/ui/litellm-dashboard/src/app/page.tsxindex 44df1b5bd41..73c27c00724 100644--- a/ui/litellm-dashboard/src/app/page.tsx+++ b/ui/litellm-dashboard/src/app/page.tsx@@ -42,6 +42,7 @@ import ToolPoliciesView from "@/components/ToolPoliciesView";import SpendLogsTable from "@/components/view_logs";import ViewUserDashboard from "@/components/view_users";import { ThemeProvider } from "@/contexts/ThemeContext";+import { clearTokenCookies, getCookie } from "@/utils/cookieUtils";import { isJwtExpired } from "@/utils/jwtUtils";import { buildLoginUrlWithReturn, consumeReturnUrl, isValidReturnUrl, normalizeUrlForCompare, storeReturnUrl } from "@/utils/returnUrlUtils";import { formatUserRole, isAdminRole } from "@/utils/roles";@@ -51,21 +52,12 @@ import { useRouter, useSearchParams } from "next/navigation";import { Suspense, useEffect, useMemo, useRef, useState } from "react";import { ConfigProvider, theme } from "antd";-function getCookie(name: string) {- // Safer cookie read + decoding; handles '=' inside values- const match = document.cookie.split("; ").find((row) => row.startsWith(name + "="));- if (!match) return null;- const value = match.slice(name.length + 1);- try {- return decodeURIComponent(value);- } catch {- return value;- }-}-function deleteCookie(name: string, path = "/") {// Best-effort client-side clear (works for non-HttpOnly cookies without Domain)document.cookie = `${name}=; Max-Age=0; Path=${path}`;+ if (name === "token") {+ clearTokenCookies();+ }}interface ProxySettings {diff --git a/ui/litellm-dashboard/src/components/networking.test.ts b/ui/litellm-dashboard/src/components/networking.test.tsindex c57dcb97eb9..3c107fa586f 100644--- a/ui/litellm-dashboard/src/components/networking.test.ts+++ b/ui/litellm-dashboard/src/components/networking.test.ts@@ -5,6 +5,7 @@ import * as Networking from "./networking";vi.mock("@/utils/cookieUtils", () => ({clearTokenCookies: vi.fn(),getCookie: vi.fn(),+ storeLoginToken: vi.fn(),}));vi.mock("./molecules/notifications_manager", () => ({@@ -79,6 +80,38 @@ describe("networking - expired session handling", () => {});});+describe("loginCall - storeLoginToken integration", () => {+ const originalFetch = global.fetch;++ beforeEach(() => {+ vi.clearAllMocks();+ });++ afterEach(() => {+ global.fetch = originalFetch;+ });++ it("calls storeLoginToken when response includes token", async () => {+ global.fetch = vi.fn().mockResolvedValue({+ ok: true,+ json: async () => ({ redirect_url: "/ui/?login=success", token: "my-jwt" }),+ }) as any;+ const { storeLoginToken } = await import("@/utils/cookieUtils");+ await Networking.loginCall("admin", "pass");+ expect(storeLoginToken).toHaveBeenCalledWith("my-jwt");+ });++ it("does not call storeLoginToken when response has no token", async () => {+ global.fetch = vi.fn().mockResolvedValue({+ ok: true,+ json: async () => ({ redirect_url: "/ui/?login=success" }),+ }) as any;+ const { storeLoginToken } = await import("@/utils/cookieUtils");+ await Networking.loginCall("admin", "pass");+ expect(storeLoginToken).not.toHaveBeenCalled();+ });+});+describe("daily activity helpers", () => {const startTime = new Date("2025-02-12T00:00:00.000Z");const endTime = new Date("2025-02-19T00:00:00.000Z");diff --git a/ui/litellm-dashboard/src/components/networking.tsx b/ui/litellm-dashboard/src/components/networking.tsxindex 28f8d308de7..16f35605877 100644--- a/ui/litellm-dashboard/src/components/networking.tsx+++ b/ui/litellm-dashboard/src/components/networking.tsx@@ -69,7 +69,7 @@ export const getInProductNudgesCall = async (accessToken: string) => {* Helper file for calls being made to proxy*/import MessageManager from "@/components/molecules/message_manager";-import { clearTokenCookies } from "@/utils/cookieUtils";+import { clearTokenCookies, storeLoginToken } from "@/utils/cookieUtils";import { TagNewRequest, TagUpdateRequest, TagListResponse, TagInfoResponse } from "./tag_management/types";import { Team } from "./key_team_helpers/key_list";import { UserInfo } from "./view_users/types";@@ -9255,14 +9255,14 @@ export const loginCall = async (username: string, password: string, useV3?: boolconst exchangeData: LoginResponse = await exchangeResponse.json();if (exchangeData.token) {- document.cookie = `token=${exchangeData.token}; path=/; SameSite=Lax`;+ storeLoginToken(exchangeData.token);}return exchangeData;}// Backwards compatibility: v2 or old v3 returns token directlyif (data.token) {- document.cookie = `token=${data.token}; path=/; SameSite=Lax`;+ storeLoginToken(data.token);}return data;diff --git a/ui/litellm-dashboard/src/components/user_dashboard.tsx b/ui/litellm-dashboard/src/components/user_dashboard.tsxindex f97d8ffab04..90eac56540d 100644--- a/ui/litellm-dashboard/src/components/user_dashboard.tsx+++ b/ui/litellm-dashboard/src/components/user_dashboard.tsx@@ -1,5 +1,5 @@"use client";-import { clearTokenCookies } from "@/utils/cookieUtils";+import { clearTokenCookies, getCookie } from "@/utils/cookieUtils";import { Col, Grid } from "@tremor/react";import { Typography } from "antd";import { jwtDecode } from "jwt-decode";@@ -35,12 +35,6 @@ export type UserInfo = {spend: number;};-function getCookie(name: string) {- console.log("COOKIES", document.cookie);- const cookieValue = document.cookie.split("; ").find((row) => row.startsWith(name + "="));- return cookieValue ? cookieValue.split("=")[1] : null;-}-interface UserDashboardProps {userID: string | null;userRole: string | null;@@ -103,7 +97,11 @@ const UserDashboard: React.FC<UserDashboardProps> = ({// They are only cleared on logoutuseEffect(() => {const handleBeforeUnload = () => {+ const token = sessionStorage.getItem("token");sessionStorage.clear();+ if (token) {+ sessionStorage.setItem("token", token);+ }};window.addEventListener("beforeunload", handleBeforeUnload);return () => window.removeEventListener("beforeunload", handleBeforeUnload);diff --git a/ui/litellm-dashboard/src/utils/cookieUtils.test.ts b/ui/litellm-dashboard/src/utils/cookieUtils.test.tsindex 8b066e6a8ea..c7bd27a6a85 100644--- a/ui/litellm-dashboard/src/utils/cookieUtils.test.ts+++ b/ui/litellm-dashboard/src/utils/cookieUtils.test.ts@@ -1,11 +1,12 @@import { describe, it, expect, beforeEach, vi } from "vitest";-import { clearTokenCookies, getCookie } from "./cookieUtils";+import { clearTokenCookies, getCookie, storeLoginToken } from "./cookieUtils";describe("cookieUtils", () => {beforeEach(() => {document.cookie.split(";").forEach((c) => {document.cookie = c.replace(/^ +/, "").replace(/=.*/, "=;expires=" + new Date().toUTCString() + ";path=/");});+ sessionStorage.clear();vi.spyOn(console, "log").mockImplementation(() => {});});@@ -116,6 +117,55 @@ describe("cookieUtils", () => {vi.restoreAllMocks();});++ it("should clear sessionStorage token", () => {+ sessionStorage.setItem("token", "stored-token");+ clearTokenCookies();+ expect(sessionStorage.getItem("token")).toBeNull();+ });+ });++ describe("storeLoginToken", () => {+ it("should store the token in sessionStorage", () => {+ storeLoginToken("my-jwt-token");+ expect(sessionStorage.getItem("token")).toBe("my-jwt-token");+ });++ it("should overwrite an existing token in sessionStorage", () => {+ storeLoginToken("old-token");+ expect(sessionStorage.getItem("token")).toBe("old-token");++ storeLoginToken("new-token");+ expect(sessionStorage.getItem("token")).toBe("new-token");+ });++ it("should not throw when window is undefined (server-side rendering)", () => {+ const originalWindow = global.window;+ delete (global as any).window;++ expect(() => storeLoginToken("token")).not.toThrow();++ global.window = originalWindow;+ });++ it("should not store empty string token", () => {+ storeLoginToken("");+ expect(sessionStorage.getItem("token")).toBeNull();+ });++ it("should not store whitespace-only token", () => {+ storeLoginToken(" ");+ expect(sessionStorage.getItem("token")).toBeNull();+ });++ it("should set a JS-accessible cookie at /ui path", () => {+ const cookieSpy = vi.spyOn(document, "cookie", "set");+ storeLoginToken("my-jwt-token");+ expect(cookieSpy).toHaveBeenCalledWith(+ expect.stringContaining("path=/ui")+ );+ vi.restoreAllMocks();+ });});describe("getCookie", () => {@@ -141,5 +191,26 @@ describe("cookieUtils", () => {expect(getCookie("token")).toBe("token-value");expect(getCookie("other")).toBe("other-value");});++ it("should handle values containing '=' characters", () => {+ document.cookie = "token=abc=def=ghi; path=/";+ expect(getCookie("token")).toBe("abc=def=ghi");+ });++ it("should fall back to sessionStorage when cookie is not found", () => {+ sessionStorage.setItem("token", "session-stored-jwt");+ expect(getCookie("token")).toBe("session-stored-jwt");+ });++ it("should prefer cookie over sessionStorage", () => {+ document.cookie = "token=cookie-value; path=/";+ sessionStorage.setItem("token", "session-value");+ expect(getCookie("token")).toBe("cookie-value");+ });++ it("should not fall back to sessionStorage for non-token keys", () => {+ sessionStorage.setItem("other", "other-value");+ expect(getCookie("other")).toBeNull();+ });});});diff --git a/ui/litellm-dashboard/src/utils/cookieUtils.ts b/ui/litellm-dashboard/src/utils/cookieUtils.tsindex 01add36542c..b4493744ad4 100644--- a/ui/litellm-dashboard/src/utils/cookieUtils.ts+++ b/ui/litellm-dashboard/src/utils/cookieUtils.ts@@ -2,6 +2,23 @@* Utility functions for managing cookies*/+/**+ * Returns the cookie path for the UI.+ * Derives the path from window.location.pathname so it works when+ * LiteLLM is deployed behind a subpath (e.g. /myapp/ui instead of /ui).+ * No imports from networking.tsx to avoid circular dependencies.+ */+function getUiCookiePath(): string {+ if (typeof window === "undefined") return "/ui";+ // Match "/ui" only as a full path segment (followed by "/" or end of string)+ // to avoid false matches like "/my-ui-tool/login" → "/my-ui".+ const match = window.location.pathname.match(/\/ui(?=\/|$)/);+ if (match && match.index !== undefined) {+ return window.location.pathname.substring(0, match.index + 3);+ }+ return "/ui";+}+/*** Clears the token cookie from both root and /ui paths*/@@ -16,7 +33,8 @@ export function clearTokenCookies() {// Clear with various combinations of path and SameSite// Include current path in case of custom server root pathconst currentPath = window.location.pathname;- const paths = ["/", "/ui"];+ const uiCookiePath = getUiCookiePath();+ const paths = ["/", uiCookiePath];// Add the current path directory if it's different from root and /uiif (currentPath && currentPath !== "/" && !currentPath.startsWith("/ui")) {@@ -43,7 +61,45 @@ export function clearTokenCookies() {});});- console.log("After clearing cookies:", document.cookie);+ try {+ sessionStorage.removeItem("token");+ } catch {+ // sessionStorage may be unavailable+ }++}++/**+ * Stores the login token so the UI can read it even when a reverse proxy+ * (e.g. nginx-ingress) adds HttpOnly to the server-set cookie.+ *+ * Strategy:+ * 1. Set a JS-accessible cookie at path "/ui". Because nginx only modifies+ * server-set Set-Cookie headers, a cookie created via document.cookie will+ * never carry HttpOnly. Using path "/ui" avoids colliding with the+ * server-set HttpOnly cookie at path "/".+ * 2. Also store in sessionStorage as a secondary fallback.+ */+export function storeLoginToken(token: string) {+ if (typeof window === "undefined") return;+ if (!token || !token.trim()) return;++ // 1. JS-accessible cookie at /ui — survives same-tab navigations and+ // is readable by getCookie() via document.cookie.+ try {+ const secure = window.location.protocol === "https:" ? "; Secure" : "";+ const cookiePath = getUiCookiePath();+ document.cookie = `token=${encodeURIComponent(token)}; path=${cookiePath}; SameSite=Lax${secure}`;+ } catch {+ // cookie setting may fail in restrictive environments+ }++ // 2. sessionStorage backup+ try {+ sessionStorage.setItem("token", token);+ } catch {+ // sessionStorage may be unavailable (e.g. private browsing quota exceeded)+ }}/**@@ -53,6 +109,23 @@ export function clearTokenCookies() {*/export function getCookie(name: string) {if (typeof document === "undefined") return null;- const cookieValue = document.cookie.split("; ").find((row) => row.startsWith(name + "="));- return cookieValue ? cookieValue.split("=")[1] : null;+ const row = document.cookie.split("; ").find((r) => r.startsWith(name + "="));+ if (row) {+ const raw = row.split("=").slice(1).join("=");+ try {+ return decodeURIComponent(raw);+ } catch {+ return raw;+ }+ }+ // Fallback to sessionStorage — covers the case where a reverse proxy+ // added HttpOnly to the server-set cookie, making it invisible to JS.+ if (name === "token" && typeof window !== "undefined") {+ try {+ return sessionStorage.getItem(name);+ } catch {+ return null;+ }+ }+ return null;}