Skip to content

Commit 2fda276

Browse files
committed
fix: update reverse proxy to utilize forwarded headers for client, protocol, host, and path
1 parent 5a2cae0 commit 2fda276

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

src/stac_auth_proxy/handlers/reverse_proxy.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,18 @@ def _prepare_headers(self, request: Request) -> MutableHeaders:
3737
headers = MutableHeaders(request.headers)
3838
headers.setdefault("Via", f"1.1 {self.proxy_name}")
3939

40-
proxy_client = request.client.host if request.client else "unknown"
41-
proxy_proto = request.url.scheme
42-
proxy_host = request.url.netloc
43-
proxy_path = request.base_url.path
40+
proxy_client = headers.get(
41+
"X-Forwarded-For", request.client.host if request.client else "unknown"
42+
)
43+
proxy_proto = headers.get("X-Forwarded-Proto", request.url.scheme)
44+
proxy_host = headers.get("X-Forwarded-Host", request.url.netloc)
45+
proxy_path = headers.get("X-Forwarded-Path", request.base_url.path)
4446
headers.setdefault(
4547
"Forwarded",
4648
f"for={proxy_client};host={proxy_host};proto={proxy_proto};path={proxy_path}",
4749
)
50+
51+
# NOTE: This is useful if the upstream API does not support the Forwarded header
4852
if self.legacy_forwarded_headers:
4953
headers.setdefault("X-Forwarded-For", proxy_client)
5054
headers.setdefault("X-Forwarded-Host", proxy_host)

0 commit comments

Comments
 (0)