-
-
Notifications
You must be signed in to change notification settings - Fork 754
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve ProxyHeadersMiddleware #1611
Conversation
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. |
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. |
except ValueError: | ||
self.trusted_literals.add(host) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we have a test case for this?
# See https://github.com/encode/uvicorn/issues/1068#issuecomment-855371576 | ||
if host in self: | ||
return x_forwarded_for_hosts[0] | ||
return host |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can this be reached? What's the scenario?
I think we may need to add more strategic comments on this PR, since it's not trivial to follow. 😅 Ref.: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Forwarded-For |
@pypae 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. |
👋 🏃 💨 |
@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. :) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
# Try parsing the trusted host as an IPv4Network | ||
# to allow checking a whole range. | ||
# https://github.com/encode/uvicorn/issues/1068 | ||
self.trusted_networks.add(ipaddress.IPv4Network(host)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a reason for using IPv4Network
instead of ipaddress.ip_network
which is version agnostic?
return True | ||
|
||
try: | ||
ip = ipaddress.IPv4Address(item) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a reason we're using IPv4Address
instead of ipaddress.ip_address
which is version agnostic?
Nothing to add. Go ahead. 🙏 |
For those subscribed - I've started a new PR #2231 |
|
Thanks @Kludex |
Summary
This PR addresses multiple issues mentioned in #1068 to improve the
ProxyHeadersMiddleware
.host
for requests from clients running on the proxy server itself. (The main issue)x-forwarded-for
headers. (Mentioned by @b0g3r)