Should HTTPSRedirectMiddleware take x-forwarded-proto: "https", into consideration?
#3197
-
|
As an "always-worrying" user (and one trying to put multiple safeguards on stuff), I noticed that, even though "everything works" when I use The browser does go via HTTPS (and the service handles certificates etc.), but Starlette/FastAPI sees the plain HTTP traffic after TLS termination. However, the headers correctly show Is this something that |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
|
I would not make Starlette's middleware checks the ASGI So the middleware order/config should be:
If the app sees |
Beta Was this translation helpful? Give feedback.
-
|
Classic TLS-terminating-proxy setup. The redirect loop comes from |
Beta Was this translation helpful? Give feedback.
Classic TLS-terminating-proxy setup. The redirect loop comes from
scope["scheme"]beinghttpinside the container even though the client arrived over HTTPS. Fix is to normalize the ASGI scope beforeHTTPSRedirectMiddlewareruns:uvicorn app:app --proxy-headers --forwarded-allow-ips '*'(or a specific CIDR), or wrap manually withProxyHeadersMiddlewarefromuvicorn.middleware.proxy_headers. Folding x-forwarded-proto handling directly into HTTPSRedirectMiddleware would force trusted-proxy config into every header-aware middleware, fixing it once at the edge is cleaner.