Skip to content

Commit 0d2fdb9

Browse files
committed
Rm base_path from requests proxied upstream
1 parent 06ac646 commit 0d2fdb9

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

src/stac_auth_proxy/app.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
BuildCql2FilterMiddleware,
2222
EnforceAuthMiddleware,
2323
OpenApiMiddleware,
24+
RemoveRootPathMiddleware,
2425
)
2526
from .utils.lifespan import check_conformance, check_server_health
2627

@@ -75,6 +76,7 @@ async def lifespan(app: FastAPI):
7576
#
7677
# Handlers (place catch-all proxy handler last)
7778
#
79+
7880
if settings.healthz_prefix:
7981
app.include_router(
8082
HealthzHandler(upstream_url=str(settings.upstream_url)).router,
@@ -90,6 +92,7 @@ async def lifespan(app: FastAPI):
9092
#
9193
# Middleware (order is important, last added = first to run)
9294
#
95+
9396
if settings.enable_authentication_extension:
9497
app.add_middleware(
9598
AuthenticationExtensionMiddleware,
@@ -135,4 +138,10 @@ async def lifespan(app: FastAPI):
135138
oidc_config_url=settings.oidc_discovery_internal_url,
136139
)
137140

141+
if settings.root_path:
142+
app.add_middleware(
143+
RemoveRootPathMiddleware,
144+
base_path=settings.root_path,
145+
)
146+
138147
return app
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
"""Middleware to remove BASE_PATH from incoming requests."""
2+
3+
from dataclasses import dataclass
4+
5+
from starlette.types import ASGIApp, Receive, Scope, Send
6+
7+
8+
@dataclass
9+
class RemoveRootPathMiddleware:
10+
"""
11+
Middleware to remove BASE_PATH from incoming requests.
12+
13+
IMPORTANT: This middleware must be the first middleware in the chain (ie last in the
14+
order of declaration) so that it trims the base_path from the request path before
15+
other middleware review the request.
16+
"""
17+
18+
app: ASGIApp
19+
base_path: str
20+
21+
async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
22+
"""Remove BASE_PATH from the request path if it exists."""
23+
if scope["type"] != "http":
24+
return await self.app(scope, receive, send)
25+
26+
path = scope["path"]
27+
28+
# Remove base_path if it exists at the start of the path
29+
if path.startswith(self.base_path):
30+
scope["raw_path"] = scope["path"].encode()
31+
scope["path"] = path[len(self.base_path) :] or "/"
32+
33+
return await self.app(scope, receive, send)

src/stac_auth_proxy/middleware/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from .AddProcessTimeHeaderMiddleware import AddProcessTimeHeaderMiddleware
44
from .ApplyCql2FilterMiddleware import ApplyCql2FilterMiddleware
55
from .AuthenticationExtensionMiddleware import AuthenticationExtensionMiddleware
6+
from .BasePathMiddleware import RemoveRootPathMiddleware
67
from .BuildCql2FilterMiddleware import BuildCql2FilterMiddleware
78
from .EnforceAuthMiddleware import EnforceAuthMiddleware
89
from .UpdateOpenApiMiddleware import OpenApiMiddleware
@@ -11,6 +12,7 @@
1112
"AddProcessTimeHeaderMiddleware",
1213
"ApplyCql2FilterMiddleware",
1314
"AuthenticationExtensionMiddleware",
15+
"RemoveRootPathMiddleware",
1416
"BuildCql2FilterMiddleware",
1517
"EnforceAuthMiddleware",
1618
"OpenApiMiddleware",

0 commit comments

Comments
 (0)