Skip to content

Commit 90de65e

Browse files
fix iter
1 parent e711dea commit 90de65e

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

services/web/server/src/simcore_service_webserver/utils_aiohttp.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,20 @@ def iter_originating_hosts(request: web.Request) -> Iterator[str]:
134134
#
135135
# SEE https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Forwarded-Host
136136
# SEE https://doc.traefik.io/traefik/getting-started/faq/#what-are-the-forwarded-headers-when-proxying-http-requests
137-
for host in (
138-
request.headers.get("X-Forwarded-Host"),
139-
request.host,
140-
):
141-
if host is not None:
142-
yield host.partition(":")[0] # exclude port, if any
137+
seen = set()
138+
139+
forwarded = request.headers.get("X-Forwarded-Host")
140+
if forwarded:
141+
# X-Forwarded-Host can contain a comma-separated list of hosts
142+
# (when the request passes through multiple proxies)
143+
for host in forwarded.split(","):
144+
stripped_host = host.strip().partition(":")[0]
145+
if stripped_host and stripped_host not in seen:
146+
seen.add(stripped_host)
147+
yield host
148+
149+
# Fallback to request.host
150+
if request.host:
151+
host = request.host.partition(":")[0]
152+
if host not in seen:
153+
yield host

0 commit comments

Comments
 (0)