uvicorn
uvicorn copied to clipboard
Improve ProxyHeadersMiddleware
Summary
This PR addresses multiple issues mentioned in https://github.com/encode/uvicorn/issues/1068 to improve the ProxyHeadersMiddleware.
- :bug: Fix the
hostfor requests from clients running on the proxy server itself. (The main issue) - :bug: Fallback to host that was already set for empty
x-forwarded-forheaders. (Mentioned by @b0g3r) - ✨ Also allow to specify IP ranges in CIDR notation as trusted hosts. (Mentioned by @jalaziz) This greatly simplifies deployments on docker swarm / kubernetes, where the reverse proxy might have a dynamic IP.
@b0g3r I don't fully grasp your third issue:
- It doesn't check request_addr like it is one of the proxies. It means that hacker who has direct network access to the server can impersonate IPs (example)
Isn't this covered by the following line? https://github.com/encode/uvicorn/blob/6947d19ff39286dc1d126eb5299f1c58c321dad3/uvicorn/middleware/proxy_headers.py#L81
Is this a feature that you would like to see merged? Can we do anything on our side?
For the time being, we're using this middleware outside of uvicorn.
For anyone coming across the same issue: Just copy the code, manually add it as a middleware and disable proxy headers in uvicorn. (For example by setting the environment variable UVICORN_PROXY_HEADERS=False)
As a side note -- would it be reasonable to add support for X-Forwarded-Host and X-Forwarded-Port, like Werkzeug does? (There's also X-Forwarded-Prefix, but I'm not familiar enough with Quart to know if that would be the right place to handle it)
UPD: Actually, that would be #965. Also, docs mention X-Forwarded-Port but it doesn't seem to be handled anywhere in the code, as far as I can see.
I think we may need to add more strategic comments on this PR, since it's not trivial to follow. :sweat_smile:
Ref.: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Forwarded-For
@pypae Are you still interested in this PR?
Are you still interested in this PR?
Yes, I still think this feature could be of use for many users. I'll have another look at it later this week and add some strategic comments.
:wave: :runner: :dash:
@pypae Are you still interested in this PR?
Hello @Kludex @pypae I hope you're doing well. I'm also interested in this functionality. Currently in our code we have to do this manually, and it would be very nice to have the out-of-the-box mechanism in uvicorn.
@pypae are you planning to further work on this functionality?
If not, are you @Kludex planning to take over and finish this pull request? Or are you going to close it without merging?
I'm waiting for the author here. If someone wants to take over, feel free to do it. I'm not closing this PR anyway.
I need my review comments to be replied.
It will be nice to have it. :)
Nothing to add. Go ahead. 🙏
For those subscribed - I've started a new PR #2231
- Let's continue on https://github.com/encode/uvicorn/pull/2231.
Thanks @Kludex