diff --git a/litellm/proxy/middleware/per_request_root_path_middleware.py b/litellm/proxy/middleware/per_request_root_path_middleware.py index d5df91458d2..77b14a44a32 100644 --- a/litellm/proxy/middleware/per_request_root_path_middleware.py +++ b/litellm/proxy/middleware/per_request_root_path_middleware.py @@ -22,6 +22,22 @@ from litellm._logging import verbose_proxy_logger SERVER_ROOT_PATHS_ENV: Final = "SERVER_ROOT_PATHS" + +class RootPathMiddleware: + """Normalize requests from proxies that strip the configured root path.""" + + def __init__(self, app: ASGIApp, root_path: str) -> None: + self.app = app + self.root_path: Final = root_path.rstrip("/") + + async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None: + if self.root_path and scope["type"] in ("http", "websocket"): + path: Final = scope.get("path", "") + if path != self.root_path and not path.startswith(self.root_path + "/"): + scope["path"] = f"{self.root_path}{path}" # rebind-ok: ASGI middleware contract + await self.app(scope, receive, send) + + # The effective ``root_path`` for the currently-handled request. Populated by # ``PerRequestRootPathMiddleware`` from the (possibly-mutated) scope so code # that emits URLs off the request path — the 401 challenges' resource_metadata diff --git a/litellm/proxy/proxy_server.py b/litellm/proxy/proxy_server.py index 7791f034fba..cae20fdf734 100644 --- a/litellm/proxy/proxy_server.py +++ b/litellm/proxy/proxy_server.py @@ -656,6 +656,7 @@ from litellm.proxy.middleware.in_flight_requests_middleware import ( ) from litellm.proxy.middleware.per_request_root_path_middleware import ( PerRequestRootPathMiddleware, + RootPathMiddleware, get_server_root_paths, ) from litellm.proxy.middleware.prometheus_auth_middleware import PrometheusAuthMiddleware @@ -19035,10 +19036,10 @@ app.add_middleware( get_settings=lambda: get_admission_control_settings(general_settings), state=admission_control_state, ) +app.add_middleware(RootPathMiddleware, root_path=server_root_path) # Added last on purpose - last-added is outermost, and the client-visible URL -# prefix must be resolved into scope["root_path"] before any inner middleware -# or the router inspects the path. Only added when SERVER_ROOT_PATHS is -# configured, so the default deployment's middleware stack is unchanged. +# prefix must be resolved before any inner middleware or the router inspects the +# path. The scalar middleware also handles ingress controllers that strip it. _server_root_paths: Final = get_server_root_paths() if _server_root_paths: if server_root_path and server_root_path != "/": diff --git a/tests/proxy_unit_tests/test_server_root_path.py b/tests/proxy_unit_tests/test_server_root_path.py index 4b39558e15a..f66d289ff73 100644 --- a/tests/proxy_unit_tests/test_server_root_path.py +++ b/tests/proxy_unit_tests/test_server_root_path.py @@ -1,5 +1,6 @@ import os from unittest import mock + from litellm.proxy import utils @@ -14,9 +15,7 @@ def test_get_server_root_path_unset(): del os.environ["SERVER_ROOT_PATH"] root_path = utils.get_server_root_path() - assert ( - root_path == "" - ), "Should return empty string when unset to allow X-Forwarded-Prefix" + assert root_path == "", "Should return empty string when unset to allow X-Forwarded-Prefix" def test_get_server_root_path_set(): @@ -34,9 +33,7 @@ def test_get_server_root_path_empty_string(): """ with mock.patch.dict(os.environ, {"SERVER_ROOT_PATH": ""}, clear=True): root_path = utils.get_server_root_path() - assert ( - root_path == "" - ), "Should return empty string when explicitly set to empty" + assert root_path == "", "Should return empty string when explicitly set to empty" # Integration test simulation for FastAPI app initialization @@ -62,3 +59,25 @@ def test_fastapi_app_initialization_mock(): server_root_path = utils.get_server_root_path() app = FastAPI(root_path=server_root_path) assert app.root_path == "/custom-root" + + +def test_root_path_middleware_handles_prefix_stripped_by_proxy(tmp_path): + from fastapi import FastAPI + from fastapi.staticfiles import StaticFiles + from fastapi.testclient import TestClient + + from litellm.proxy.middleware.per_request_root_path_middleware import RootPathMiddleware + + (tmp_path / "index.html").write_text("dashboard") + app = FastAPI(root_path="/genai-platform/llm-gateway") + app.mount("/ui", StaticFiles(directory=tmp_path, html=True), name="ui") + app.add_middleware(RootPathMiddleware, root_path=app.root_path) + + client = TestClient(app, root_path=app.root_path) + redirect = client.get("/ui", follow_redirects=False) + response = client.get("/ui/") + + assert redirect.status_code == 307 + assert redirect.headers["location"].endswith("/genai-platform/llm-gateway/ui/") + assert response.status_code == 200 + assert response.text == "dashboard"