-
Notifications
You must be signed in to change notification settings - Fork 26
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
Walk backwards through the ip list to avoid mocking by the client #6
Comments
Hi, I already thought about this. You can have a setup where you chain proxy, so the real public IP of the client is the one from the first proxy, not the last one. what do you think? |
Making that configurable would be great! I haven't run into the issue itself yet so the only source I have is that blogpost. We just started to use your middleware and it works perfectly so far. What do you mean with "Only accept header from trusted IP's"? In our case we only use it for logging purposes and obviously validate the values before storing any of it. Having said that, I suppose it matters from use-case to use-case what you want to do with it. |
Only accept header from trusted IP's: this header is set by a proxy/lb that is in front of you app, you should only accept this header if it comes from a proxy/lb that you manage (identified by its IP). For your usecase it does not really matter if you use it for logging purpose, but let's imagine that you were using the value for authentication or to do some geoip lookup and then allow access based on country: you'll have to make sure that the end user cannot provide a fake IP through this header, so you proxy/lb should ignore the XFF header, get the IP of the client and set the XFF header with this value, in you application code, you can add the IP of the proxy/lb in the list of the trusted IP. If you want to read more about the topic, this blog post describe an attack that was used againt stackoverflow using XFF: http://blog.ircmaxell.com/2012/11/anatomy-of-attack-how-i-hacked.html |
It looks like best way to get client IP is by making two changes to current implementation:
|
why not iterate through them then? If there are multiple proxies - then just check whether the next rightmost address is in the trusted network - then go next. |
Why get both XFF and X real both isnt that enought to iterate through XFF and I dont understand the logic behind that |
Not having this as the default is a huge security risk! Please regard how NGINX is parsing the XFF in realip. That is the only safe way IMO: you can only prune the rightmost value if the current RemoteAddr is in your allowed list. Then recurse, until you have an untrusted address. By fully trusting the RemoteAddr you're placing your bets on any intermediate to not screw up. Example:
the result will be:
ergo, this library trusts the RemoteAddr is 127.0.0.1. A better implementation is to take 80.0.0.1 as the resulting IP, as that is the last known safe one. I hate having to learn this by browsing the code, instead of from the readme. |
The current untrusted-IP behaviour can lead to rate limiter evasion, memory exhaustion, access control bypass, etc. It should be clearly documented at the very least and probably changed to be a configurable rightmost-ish strategy (or strategies). I'm working on a library to help with this (but it's not quite done): https://github.com/realclientip/realclientip-go |
https://husobee.github.io/golang/ip-address/2015/12/17/remote-ip-go.html mentions:
So the code that he/she suggests is the following:
Thoughts?
The text was updated successfully, but these errors were encountered: