-
Notifications
You must be signed in to change notification settings - Fork 38.3k
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
Add support for X-Forwarded-For and Forwarded for #23582
Conversation
@molassar Please sign the Contributor License Agreement! Click here to manually synchronize the status of this Pull Request. See the FAQ for frequently asked questions. |
@molassar Thank you for signing the Contributor License Agreement! |
@molassar, currently |
As far as I have understood, UriComponents is just more powerful alternative of URI. Will it be semantically correct to put remote host and port in it? Since, remote host has nothing to do with uri, it seems that UriComponents is not a proper place for it. |
Indeed it wouldn't make sense to add anything to do with "remote" host or port. |
Ok, now I see your point |
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.
Thanks for the revision. I've added a couple of minor performance related comments, but overall this looks like the right approach to me.
.port(remoteAddressO.map(InetSocketAddress::getPort).orElse(-1)) | ||
.adaptFromForwardedForHeader(request.getHeaders()) | ||
.build(); | ||
String remoteHost = remoteUriComponents.getHost(); |
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.
Likewise as for ForwardedHeaderFilter, it is worth applying a check if this even needs to be done.
.host(request.getRemoteHost()) | ||
.port(request.getRemotePort()) | ||
.adaptFromForwardedForHeader(httpRequest.getHeaders()) | ||
.build(); |
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.
It is worth applying some sort of check if this even needs to be done, e.g. "X-Forwarded-For" and/or an indexOf scan to see if "Forward" has a "for" attribute. In hot paths especially we need to reduce Object creation. The same applies for the use of Optional.
@molassar I've merged this and made some further updates. Notably I removed the logic related to obfuscated host. My reading of the spec is that this is something proxies might do but we only read the first remote address which should be the client address. |
Would have been nice to add a warning that having this enabled makes you vulnerable to IP spoofing (client can just send X-Forwarded-For header himself). Tomcat has a nice implementation with it's RemoteIpFilter where you can define which IPs you trust and which does not use the leftmost value (if untrusted). |
The "Forwarded" section in the reference documentation mentions:
Is there anything missing here? Also, the As you've said already, this header can be easily manipulated if not properly cleared by the proxy, so I don't think the |
Ah, I didn't know there is documentation for the Filter on the web. I was referring to the Javadoc of the class. That's where I always look first and I didn't expect that there is additional documentation on the web. I would still suggest to have a warning in the class as this topic is not so easy to understand at the first glance. I think you missunderstood how the Say we have a client ( There are now two cases:
So no matter what the client sends, it won't be picked up by the |
@T3rm1 Interesting, what happens if the client sends a request directly to the backend (so not going through any proxy) with the following header: |
Looking at RemoteIpFilter it updates the forwarded header so that only trusted proxies remain. This is the sort of cleanup we expect to occur before the request gets to the |
Interesting case, although unlikely because the client would need to know that |
Closes #23260