Skip to content
Python

gateway: links are https on a public host even without X-Forwarded-Proto

neuregex13f8b25fe8f6
diff --git a/infra/gateway.py b/infra/gateway.py
index af23e3c..1e849f1 100644
--- a/infra/gateway.py
+++ b/infra/gateway.py
@@ -96,10 +96,20 @@ async def status(request: web.Request) -> web.Response:
return web.json_response({"app": pantry.app, "models": pantry.models, "frees": pantry.frees, "comfyui": stats})
+LOCAL_HOSTS = ("127.0.0.1", "localhost", "0.0.0.0", "[::1]")
+
+
def external_base(request: web.Request) -> str:
- """Where the client reached us: Modal terminates TLS and says so in X-Forwarded-Proto."""
- scheme = request.headers.get("X-Forwarded-Proto", request.scheme).split(",")[0].strip()
- return f"{scheme}://{request.host}"
+ """Where the client reached us. Modal terminates TLS and talks plain http to the container
+ without an X-Forwarded-Proto header (first deploy, 23 oct: links came out as http://…modal.run,
+ the SDK saw another origin and sent no token, 401), so a public host is https unless a proxy
+ says otherwise; only a local or private address keeps the scheme it was reached on."""
+ forwarded = request.headers.get("X-Forwarded-Proto", "").split(",")[0].strip()
+ host = request.host
+ name = host.rsplit(":", 1)[0] if not host.startswith("[") else host.split("]")[0] + "]"
+ private = name in LOCAL_HOSTS or name.startswith(("10.", "192.168.", "172.16.", "172.17.", "172.18.", "172.19.", "172.2", "172.30.", "172.31."))
+ scheme = forwarded or (request.scheme if private else "https")
+ return f"{scheme}://{host}"
def relink(body: bytes, request: web.Request) -> bytes:
diff --git a/test/gateway_test.py b/test/gateway_test.py
index 1cc9e7e..5509fb4 100644
--- a/test/gateway_test.py
+++ b/test/gateway_test.py
@@ -36,6 +36,13 @@ async def main():
async with s.get("http://127.0.0.1:18000/api/v2/jobs/j1", headers={**h, "X-Forwarded-Proto": "https", "Host": "gw.example.test"}) as r:
body = await r.json()
assert body["urls"]["self"] == "https://gw.example.test/api/v2/jobs/j1", body["urls"]
+ # a public host with no forwarded header (Modal) is https; a local one keeps http
+ async with s.get("http://127.0.0.1:18000/api/v2/jobs/j1", headers={**h, "Host": "x--serve.us-east.modal.run"}) as r:
+ body = await r.json()
+ assert body["urls"]["self"] == "https://x--serve.us-east.modal.run/api/v2/jobs/j1", body["urls"]
+ async with s.get("http://127.0.0.1:18000/api/v2/jobs/j1", headers={**h, "Host": "192.168.1.20:8000"}) as r:
+ body = await r.json()
+ assert body["urls"]["self"] == "http://192.168.1.20:8000/api/v2/jobs/j1", body["urls"]
async with s.get("http://127.0.0.1:18000/api/v2/jobs/j1/events", headers=h) as r:
text = await r.text(); assert r.headers["Content-Type"].startswith("text/event-stream")
assert "http://127.0.0.1:18189/x" in text, "streams are passed through untouched"