mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-18 09:45:23 +00:00
fix(proxy): preserve UI routes with stripped root paths
This commit is contained in:
parent
078a60478f
commit
bafc906fd2
3 changed files with 45 additions and 9 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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 != "/":
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue