feat(proxy): resolve root_path per request from SERVER_ROOT_PATHS

One deployment can encode exactly one client-visible URL path prefix
today: SERVER_ROOT_PATH is a scalar stamped onto the app at startup, so
a pod fronting several ingress prefixes 404s every prefix but one before
any handler runs, and MCP OAuth discovery can emit only one prefix's
URLs (RFC 9728 section 3 exact-match fails for the rest).

Add an opt-in outermost ASGI middleware that matches the request path
against a configured prefix list (SERVER_ROOT_PATHS, comma-separated) on
a segment boundary and sets scope["root_path"] for that request only.
Everything downstream is stock Starlette: route matching strips
root_path so routes stay registered root-relative, and request.base_url
re-includes it, so the discovery documents' resource and the 401
challenges' resource_metadata land under the prefix the client actually
called — with no discovery-builder changes.

LazyFeatureMiddleware now strips the scope root_path (falling back to
the cached SERVER_ROOT_PATH scalar) before feature prefix matching, so
lazily-registered routers — the MCP OAuth discovery router among them —
load under per-request prefixes.

Follow-up to the routing discussion on #35226; composes with, but does
not depend on, #35576.
This commit is contained in:
gym-cmd 2026-08-05 11:50:55 +01:00
parent b735578822
commit 0ec70ed693
6 changed files with 595 additions and 9 deletions

View file

@ -292,15 +292,22 @@ class LazyFeatureMiddleware:
# Short-circuit once every feature has loaded.
if scope["type"] in ("http", "websocket") and len(self._loaded) < len(self._features):
path = scope.get("path", "")
# Strip SERVER_ROOT_PATH so prefix matching works under a server
# root path. Without this, requests like /api/v1/policies/... never
# match the registered prefixes (/policies/...) and lazy features
# stay unloaded — every endpoint under them returns 404. The
# `+ "/"` boundary prevents false-positive matches (e.g. /apiv2
# against root /api). If the path doesn't start with the prefix
# (e.g. a reverse proxy already stripped it), we leave it alone.
if self._root_path and path.startswith(self._root_path + "/"):
path = path[len(self._root_path) :]
# Strip the request's root_path so prefix matching works under a
# server root path. Without this, requests like
# /api/v1/policies/... never match the registered prefixes
# (/policies/...) and lazy features stay unloaded — every endpoint
# under them returns 404. scope["root_path"] is authoritative when
# set: the scalar SERVER_ROOT_PATH lands there via
# FastAPI(root_path=...), and PerRequestRootPathMiddleware
# (SERVER_ROOT_PATHS) resolves a per-request prefix there; the
# cached env scalar is the fallback for scopes built without it.
# The `+ "/"` boundary prevents false-positive matches (e.g.
# /apiv2 against root /api). If the path doesn't start with the
# prefix (e.g. a reverse proxy already stripped it), we leave it
# alone.
root_path = str(scope.get("root_path", "")).rstrip("/") or self._root_path
if root_path and path.startswith(root_path + "/"):
path = path[len(root_path) :]
for feat in self._features:
if feat.module_path in self._loaded:
continue

View file

@ -0,0 +1,105 @@
"""
Per-request ``root_path`` resolution for a proxy fronting several
client-visible URL path prefixes from one deployment (``SERVER_ROOT_PATHS``).
``SERVER_ROOT_PATH`` is a single scalar stamped onto the app at startup, so
one deployment serves exactly one client-visible prefix: Starlette strips that
prefix during route matching and rebuilds it into ``request.base_url``, and a
request under any other prefix 404s before a handler runs. A pod fronting
several ingress path prefixes (e.g. ``/tenant-a/*`` and ``/tenant-b/*`` both
terminating at the same LiteLLM) is therefore forced into one deployment per
prefix purely to encode it.
This middleware generalizes the same mechanism to N prefixes: the operator
lists the prefixes the ingress preserves, and the one matching an incoming
request becomes that request's ``scope["root_path"]``. Everything downstream
is unchanged Starlette behavior — route matching strips ``root_path``
(``starlette._utils.get_route_path``) so routes stay registered
root-relative, and ``request.base_url`` re-includes it, so every emitted URL
(the MCP OAuth discovery documents' RFC 9728 §3 ``resource`` and the 401
challenges' ``resource_metadata`` among them) lands under the prefix the
client actually called. A path under no configured prefix passes through
untouched: it routes root-relative exactly as today, and 404s otherwise.
Opt-in by design: LiteLLM cannot detect whether the proxy in front strips or
preserves the client-visible prefix. Setting ``SERVER_ROOT_PATHS`` is the
operator affirming the prefix reaches the pod intact (path-based ALB /
ingress routing); a topology that rewrites the path away must keep using the
scalar ``SERVER_ROOT_PATH`` / path-carrying ``PROXY_BASE_URL``.
"""
import os
from typing import Final, Sequence
from starlette.types import ASGIApp, Receive, Scope, Send
from litellm._logging import verbose_proxy_logger
SERVER_ROOT_PATHS_ENV: Final = "SERVER_ROOT_PATHS"
def normalize_root_paths(raw_paths: Sequence[str]) -> tuple[str, ...]:
"""Canonicalize configured prefixes: strip whitespace and trailing
slashes, dedupe, order longest-first so the most-specific nested prefix
wins at match time. Entries without a leading ``/`` and the bare root are
dropped with a warning — a typo'd prefix silently matching nothing would
surface only as unexplained 404s at request time."""
kept: list[str] = []
for entry in raw_paths:
candidate = entry.strip()
if not candidate:
continue
if not candidate.startswith("/"):
verbose_proxy_logger.warning(
"%s entry %r does not start with '/' and will be ignored.",
SERVER_ROOT_PATHS_ENV,
entry,
)
continue
candidate = candidate.rstrip("/")
if not candidate:
verbose_proxy_logger.warning(
"%s entry %r is the bare root and will be ignored; a root-mounted deployment needs no entry.",
SERVER_ROOT_PATHS_ENV,
entry,
)
continue
if candidate not in kept:
kept.append(candidate)
return tuple(sorted(kept, key=len, reverse=True))
def get_server_root_paths() -> tuple[str, ...]:
"""The normalized ``SERVER_ROOT_PATHS`` prefixes, empty when unset."""
configured: Final = os.getenv(SERVER_ROOT_PATHS_ENV, "")
if not configured.strip():
return ()
return normalize_root_paths(configured.split(","))
class PerRequestRootPathMiddleware:
"""ASGI middleware that sets ``scope["root_path"]`` to the configured
prefix matching the request path on a whole-segment boundary, leaving
``scope["path"]`` untouched (Starlette strips ``root_path`` from the path
at route-match time; stripping here too would double-strip).
Must be the outermost middleware (added last): inner middlewares and the
router need ``root_path`` already resolved — LazyFeatureMiddleware strips
it before feature prefix matching, Starlette strips it during route
matching. A matched prefix overrides a scalar ``root_path``
(``SERVER_ROOT_PATH``) for that request; combining both mechanisms is
warned about at startup.
"""
def __init__(self, app: ASGIApp, root_paths: Sequence[str]) -> None:
self.app = app
self.root_paths: Final = normalize_root_paths(root_paths)
async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
if scope["type"] in ("http", "websocket"):
path: Final = scope.get("path", "")
for prefix in self.root_paths:
if path == prefix or path.startswith(prefix + "/"):
scope["root_path"] = prefix
break
await self.app(scope, receive, send)

View file

@ -510,6 +510,10 @@ except ImportError:
from litellm.proxy.middleware.in_flight_requests_middleware import (
InFlightRequestsMiddleware,
)
from litellm.proxy.middleware.per_request_root_path_middleware import (
PerRequestRootPathMiddleware,
get_server_root_paths,
)
from litellm.proxy.middleware.prometheus_auth_middleware import PrometheusAuthMiddleware
from litellm.proxy.middleware.request_size_limit_middleware import (
RequestSizeLimitMiddleware,
@ -16482,6 +16486,25 @@ app.add_middleware(
get_max_request_size_mb=lambda: general_settings.get("max_request_size_mb"),
is_request_size_limit_enabled=lambda: premium_user is True,
)
# 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: LazyFeatureMiddleware strips it for feature
# prefix matching, Starlette strips it for route matching, and
# request.base_url re-includes it in every emitted URL (the MCP OAuth
# discovery documents among them). Only added when SERVER_ROOT_PATHS is
# configured, so the default deployment's middleware stack is unchanged.
_server_root_paths: Final = get_server_root_paths()
if _server_root_paths:
if server_root_path and server_root_path != "/":
verbose_proxy_logger.warning(
"Both SERVER_ROOT_PATH=%r and SERVER_ROOT_PATHS=%r are set. A request "
"matching a SERVER_ROOT_PATHS prefix overrides the scalar root_path for "
"that request; unmatched requests keep SERVER_ROOT_PATH. Configure one "
"mechanism or the other.",
server_root_path,
_server_root_paths,
)
app.add_middleware(PerRequestRootPathMiddleware, root_paths=_server_root_paths)
async def _stream_mcp_asgi_response(handle_fn, scope: dict, receive) -> "StreamingResponse":

View file

@ -9241,3 +9241,176 @@ async def test_upstream_resource_sent_on_dcr_bridge_relay_authorize():
query = await _authorize_query(server)
assert query["resource"] == ["https://mcp.example.com/mcp"]
assert query["client_id"] == ["caller-client"]
# ---------------------------------------------------------------------------
# Per-request root_path (SERVER_ROOT_PATHS / PerRequestRootPathMiddleware):
# one app fronting several client-visible URL path prefixes, each prefix's
# discovery documents emitting URLs under the prefix the client called
# (RFC 9728 §3 exact-match). A scalar PROXY_BASE_URL / SERVER_ROOT_PATH can
# encode at most one prefix per pod; these tests pin the N-prefix case.
# ---------------------------------------------------------------------------
@pytest.fixture
def _no_proxy_base_url(monkeypatch):
"""Discovery must derive URLs from the request in these tests, so the
scalar env overrides are cleared."""
monkeypatch.delenv("PROXY_BASE_URL", raising=False)
monkeypatch.delenv("SERVER_ROOT_PATH", raising=False)
def _prefixed_discovery_client(prefixes):
from fastapi import FastAPI
from fastapi.testclient import TestClient
from litellm.proxy._experimental.mcp_server.discoverable_endpoints import router
from litellm.proxy.middleware.per_request_root_path_middleware import (
PerRequestRootPathMiddleware,
)
app = FastAPI()
app.include_router(router)
app.add_middleware(PerRequestRootPathMiddleware, root_paths=prefixes)
return TestClient(app)
class TestPerRequestRootPathDiscovery:
def test_prefixed_wellknown_not_routable_without_middleware(self):
"""Control: on a plain app (the only shape a scalar root_path can
express), a prefixed well-known request 404s before any discovery
builder runs — the routing gap this feature exists to close."""
from fastapi import FastAPI
from fastapi.testclient import TestClient
from litellm.proxy._experimental.mcp_server.discoverable_endpoints import router
from litellm.proxy._experimental.mcp_server.mcp_server_manager import (
global_mcp_server_manager,
)
global_mcp_server_manager.registry.clear()
server = _create_oauth2_server(server_id="srv_a", name="server_a", server_name="server_a", alias="server_a")
global_mcp_server_manager.registry[server.server_id] = server
app = FastAPI()
app.include_router(router)
client = TestClient(app)
try:
resp = client.get("/tenant-a/.well-known/oauth-protected-resource/mcp/server_a")
assert resp.status_code == 404
finally:
global_mcp_server_manager.registry.clear()
def test_two_prefixes_one_app_each_resource_matches_the_called_url(self, _no_proxy_base_url):
"""The multi-origin case itself: two prefixes served by the same app,
each per-server document's ``resource`` equal to the URL its client
called — including the prefix."""
from litellm.proxy._experimental.mcp_server.mcp_server_manager import (
global_mcp_server_manager,
)
global_mcp_server_manager.registry.clear()
for sid, name in (("srv_a", "server_a"), ("srv_b", "server_b")):
global_mcp_server_manager.registry[sid] = _create_oauth2_server(
server_id=sid, name=name, server_name=name, alias=name
)
client = _prefixed_discovery_client(["/tenant-a", "/tenant-b"])
try:
resp_a = client.get("/tenant-a/.well-known/oauth-protected-resource/mcp/server_a")
resp_b = client.get("/tenant-b/.well-known/oauth-protected-resource/mcp/server_b")
assert resp_a.status_code == 200
assert resp_a.json()["resource"] == "http://testserver/tenant-a/mcp/server_a"
assert resp_b.status_code == 200
assert resp_b.json()["resource"] == "http://testserver/tenant-b/mcp/server_b"
# Every URL the document advertises stays under the request's
# prefix, so it resolves on this same app.
for auth_server in resp_a.json()["authorization_servers"]:
assert auth_server.startswith("http://testserver/tenant-a/")
finally:
global_mcp_server_manager.registry.clear()
def test_unprefixed_requests_unchanged_on_the_same_app(self, _no_proxy_base_url):
"""Backward compat on the very same app: a root request emits the
document byte-identical to a deployment without the middleware."""
from litellm.proxy._experimental.mcp_server.mcp_server_manager import (
global_mcp_server_manager,
)
global_mcp_server_manager.registry.clear()
server = _create_oauth2_server(server_id="srv_a", name="server_a", server_name="server_a", alias="server_a")
global_mcp_server_manager.registry[server.server_id] = server
client = _prefixed_discovery_client(["/tenant-a"])
try:
resp = client.get("/.well-known/oauth-protected-resource/mcp/server_a")
assert resp.status_code == 200
assert resp.json()["resource"] == "http://testserver/mcp/server_a"
finally:
global_mcp_server_manager.registry.clear()
def test_unlisted_prefix_404s(self, _no_proxy_base_url):
client = _prefixed_discovery_client(["/tenant-a"])
assert client.get("/tenant-c/.well-known/oauth-protected-resource/mcp/server_a").status_code == 404
def test_aggregate_documents_and_as_endpoints_under_prefix(self, _no_proxy_base_url):
"""Aggregate PRM/AS documents carry the prefix, and the advertised
authorize endpoint actually resolves under it — the 404 trap that
invalidated prefixing discovery URLs without per-request routing
(#35226 review round 1)."""
from litellm.proxy._experimental.mcp_server.mcp_server_manager import (
global_mcp_server_manager,
)
global_mcp_server_manager.registry.clear()
client = _prefixed_discovery_client(["/tenant-a"])
prm = client.get("/tenant-a/.well-known/oauth-protected-resource/mcp")
asm = client.get("/tenant-a/.well-known/oauth-authorization-server/mcp")
assert prm.status_code == 200
assert prm.json()["resource"] == "http://testserver/tenant-a/mcp"
assert prm.json()["authorization_servers"] == ["http://testserver/tenant-a/mcp"]
assert asm.status_code == 200
assert asm.json()["issuer"] == "http://testserver/tenant-a/mcp"
assert asm.json()["authorization_endpoint"] == "http://testserver/tenant-a/authorize"
# The prefixed authorize URL routes to the real handler (not 404):
# under per-request root_path the whole app is reachable per-prefix,
# so discovery may advertise prefixed AS endpoints safely.
assert client.get("/tenant-a/authorize").status_code != 404
def test_passthrough_challenge_metadata_url_carries_prefix(self, _no_proxy_base_url):
"""The WWW-Authenticate resource_metadata URL a 401 advertises must
land under the request's prefix, or the client is bounced to a
document whose ``resource`` cannot match the URL it called."""
from litellm.proxy._experimental.mcp_server.oauth_utils import (
get_passthrough_resource_metadata_url,
)
def _scope(path, root_path=None):
scope = {
"type": "http",
"method": "POST",
"path": path,
"headers": [],
"scheme": "http",
"server": ("testserver", 80),
"client": ("1.2.3.4", 4444),
}
if root_path is not None:
scope["root_path"] = root_path
return scope
prefixed = get_passthrough_resource_metadata_url(
scope=_scope("/tenant-a/mcp/github", root_path="/tenant-a"),
server_name="github",
)
assert prefixed == "http://testserver/tenant-a/.well-known/oauth-protected-resource/mcp/github"
# Regression guard: no root_path → today's URL, unchanged.
bare = get_passthrough_resource_metadata_url(
scope=_scope("/mcp/github"),
server_name="github",
)
assert bare == "http://testserver/.well-known/oauth-protected-resource/mcp/github"

View file

@ -0,0 +1,202 @@
"""Tests for PerRequestRootPathMiddleware (``SERVER_ROOT_PATHS``).
One deployment fronting several client-visible URL path prefixes: the matched
prefix becomes that request's ``root_path``, so Starlette route matching and
``request.base_url`` — and therefore every URL the proxy emits, the MCP OAuth
discovery documents among them — resolve under the prefix the client called.
"""
import pytest
from fastapi import FastAPI, Request
from fastapi.testclient import TestClient
from litellm.proxy.middleware.per_request_root_path_middleware import (
PerRequestRootPathMiddleware,
get_server_root_paths,
normalize_root_paths,
)
class TestNormalizeRootPaths:
def test_strips_whitespace_and_trailing_slash(self):
assert normalize_root_paths([" /tenant-a/ ", "/tenant-b"]) == (
"/tenant-a",
"/tenant-b",
)
def test_drops_empty_entries(self):
assert normalize_root_paths(["", " ", "/tenant-a"]) == ("/tenant-a",)
def test_drops_entries_without_leading_slash(self):
# A typo'd entry must not silently match nothing at request time.
assert normalize_root_paths(["tenant-a", "/tenant-b"]) == ("/tenant-b",)
def test_drops_bare_root(self):
# "/" would turn every request into a root_path rewrite; a
# root-mounted deployment needs no entry at all.
assert normalize_root_paths(["/", "/tenant-a"]) == ("/tenant-a",)
def test_dedupes(self):
assert normalize_root_paths(["/t", "/t/", " /t "]) == ("/t",)
def test_longest_first_for_nested_prefixes(self):
# Longest-first ordering is what makes the most-specific nested
# prefix win at match time.
assert normalize_root_paths(["/t", "/t/deep"]) == ("/t/deep", "/t")
class TestGetServerRootPaths:
def test_unset_env_is_empty(self, monkeypatch):
monkeypatch.delenv("SERVER_ROOT_PATHS", raising=False)
assert get_server_root_paths() == ()
def test_empty_env_is_empty(self, monkeypatch):
monkeypatch.setenv("SERVER_ROOT_PATHS", "")
assert get_server_root_paths() == ()
def test_comma_separated_entries(self, monkeypatch):
monkeypatch.setenv("SERVER_ROOT_PATHS", "/tenant-a, /tenant-b/")
assert get_server_root_paths() == ("/tenant-a", "/tenant-b")
def _capture_scope_middleware(root_paths):
"""Middleware wired to a downstream that records the scope it received."""
captured = {}
async def downstream(scope, receive, send):
captured.update(scope)
await send({"type": "http.response.start", "status": 200, "headers": []})
await send({"type": "http.response.body", "body": b""})
return PerRequestRootPathMiddleware(downstream, root_paths=root_paths), captured
async def _run(mw, scope):
async def receive():
return {"type": "http.request", "body": b"", "more_body": False}
async def send(message):
pass
await mw(scope, receive, send)
class TestPerRequestRootPathMiddleware:
@pytest.mark.asyncio
async def test_matched_prefix_becomes_root_path_path_untouched(self):
# Starlette's router strips root_path from the (unmodified) path at
# match time, so the middleware must NOT rewrite scope["path"].
mw, captured = _capture_scope_middleware(["/tenant-a"])
await _run(mw, {"type": "http", "path": "/tenant-a/mcp/x", "method": "GET", "headers": []})
assert captured["root_path"] == "/tenant-a"
assert captured["path"] == "/tenant-a/mcp/x"
@pytest.mark.asyncio
async def test_exact_prefix_matches(self):
mw, captured = _capture_scope_middleware(["/tenant-a"])
await _run(mw, {"type": "http", "path": "/tenant-a", "method": "GET", "headers": []})
assert captured["root_path"] == "/tenant-a"
@pytest.mark.asyncio
async def test_segment_boundary_prevents_sibling_match(self):
# /tenant-ab must not match the /tenant-a prefix.
mw, captured = _capture_scope_middleware(["/tenant-a"])
await _run(mw, {"type": "http", "path": "/tenant-ab/mcp", "method": "GET", "headers": []})
assert "root_path" not in captured
@pytest.mark.asyncio
async def test_unmatched_path_untouched(self):
mw, captured = _capture_scope_middleware(["/tenant-a"])
await _run(mw, {"type": "http", "path": "/chat/completions", "method": "GET", "headers": []})
assert "root_path" not in captured
assert captured["path"] == "/chat/completions"
@pytest.mark.asyncio
async def test_longest_nested_prefix_wins(self):
mw, captured = _capture_scope_middleware(["/t", "/t/deep"])
await _run(mw, {"type": "http", "path": "/t/deep/mcp", "method": "GET", "headers": []})
assert captured["root_path"] == "/t/deep"
@pytest.mark.asyncio
async def test_matched_prefix_overrides_scalar_root_path(self):
# FastAPI(root_path=SERVER_ROOT_PATH) stamps the scalar before the
# middleware stack runs; a matched dynamic prefix wins for that
# request (combining both mechanisms is warned about at startup).
mw, captured = _capture_scope_middleware(["/tenant-a"])
await _run(
mw,
{"type": "http", "path": "/tenant-a/mcp", "root_path": "/legacy", "method": "GET", "headers": []},
)
assert captured["root_path"] == "/tenant-a"
@pytest.mark.asyncio
async def test_unmatched_request_keeps_scalar_root_path(self):
mw, captured = _capture_scope_middleware(["/tenant-a"])
await _run(
mw,
{"type": "http", "path": "/legacy/mcp", "root_path": "/legacy", "method": "GET", "headers": []},
)
assert captured["root_path"] == "/legacy"
@pytest.mark.asyncio
async def test_websocket_scope_matched(self):
mw, captured = _capture_scope_middleware(["/tenant-a"])
await _run(mw, {"type": "websocket", "path": "/tenant-a/ws", "headers": []})
assert captured["root_path"] == "/tenant-a"
@pytest.mark.asyncio
async def test_lifespan_scope_passes_through(self):
called = {}
async def downstream(scope, receive, send):
called["scope"] = scope
mw = PerRequestRootPathMiddleware(downstream, root_paths=["/tenant-a"])
await _run(mw, {"type": "lifespan"})
assert called["scope"] == {"type": "lifespan"}
def _routed_client(prefixes):
app = FastAPI()
@app.get("/where")
def where(request: Request):
return {
"base_url": str(request.base_url),
"root_path": request.scope.get("root_path", ""),
}
app.add_middleware(PerRequestRootPathMiddleware, root_paths=prefixes)
return TestClient(app)
class TestEndToEndRouting:
def test_two_prefixes_route_on_one_app(self):
# The property a scalar SERVER_ROOT_PATH cannot provide: two
# client-visible prefixes served by the same app, each request
# reconstructing its own base URL.
client = _routed_client(["/tenant-a", "/tenant-b"])
resp_a = client.get("/tenant-a/where")
resp_b = client.get("/tenant-b/where")
assert resp_a.status_code == 200
assert resp_a.json() == {
"base_url": "http://testserver/tenant-a/",
"root_path": "/tenant-a",
}
assert resp_b.status_code == 200
assert resp_b.json() == {
"base_url": "http://testserver/tenant-b/",
"root_path": "/tenant-b",
}
def test_unprefixed_route_still_served(self):
client = _routed_client(["/tenant-a"])
resp = client.get("/where")
assert resp.status_code == 200
assert resp.json()["base_url"] == "http://testserver/"
def test_unlisted_prefix_404s(self):
client = _routed_client(["/tenant-a"])
assert client.get("/tenant-c/where").status_code == 404

View file

@ -8995,6 +8995,82 @@ class TestLazyFeatureMiddleware:
else:
assert loads == [], f"{case}: feature must not load"
@pytest.mark.asyncio
@pytest.mark.parametrize(
"env_root_path,scope_root_path,request_path,should_load,case",
[
# Per-request root_path (PerRequestRootPathMiddleware under
# SERVER_ROOT_PATHS) with no scalar env: strip and match.
("", "/tenant-a", "/tenant-a/dummy/x", True, "per-request root_path strip"),
# scope root_path is authoritative over the cached env scalar.
("/api/v1", "/tenant-a", "/tenant-a/dummy/x", True, "scope wins over env scalar"),
# Boundary check still applies to the per-request value.
("", "/tenant-a", "/tenant-ab/dummy/x", False, "boundary check on scope root_path"),
# Empty scope root_path falls back to the env scalar.
("/api/v1", "", "/api/v1/dummy/x", True, "empty scope falls back to env"),
],
)
async def test_per_request_root_path_handling(
self, monkeypatch, env_root_path, scope_root_path, request_path, should_load, case
):
"""
``scope["root_path"]`` must be stripped before prefix matching when
set — the scalar SERVER_ROOT_PATH lands there via
``FastAPI(root_path=...)``, and PerRequestRootPathMiddleware
(SERVER_ROOT_PATHS) resolves a per-request prefix there. Otherwise
lazily-registered features — the MCP OAuth discovery router among
them — stay unloaded under a client-visible prefix and 404.
"""
from fastapi import FastAPI
from litellm.proxy._lazy_features import (
LazyFeature,
LazyFeatureMiddleware,
)
monkeypatch.setenv("SERVER_ROOT_PATH", env_root_path)
loads = []
def fake_register(app, module):
loads.append(getattr(module, "__name__", "?"))
feat = LazyFeature(
name=f"dummy_prr_{case}",
module_path="json",
path_prefixes=("/dummy",),
register_fn=fake_register,
)
async def downstream(scope, receive, send):
await send({"type": "http.response.start", "status": 200, "headers": []})
await send({"type": "http.response.body", "body": b""})
target_app = FastAPI()
mw = LazyFeatureMiddleware(downstream, fastapi_app=target_app, features=(feat,))
async def receive():
return {"type": "http.request", "body": b"", "more_body": False}
async def send(message):
pass
await mw(
{
"type": "http",
"path": request_path,
"root_path": scope_root_path,
"method": "GET",
"headers": [],
},
receive,
send,
)
if should_load:
assert loads == ["json"], f"{case}: expected feature to load"
else:
assert loads == [], f"{case}: feature must not load"
@pytest.mark.asyncio
async def test_concurrent_first_requests_only_register_once(self):
"""