Behaviour of the ForwardedHeaderHandler with X-Forwarded-For
I have an issue wth the behaviour of ForwardedHeaderHandler with X-Forwarded-For .
According to the spec, the leftmost value of the header is the original source ip.
X-Forwarded-For: client, proxy1, proxy2 (source wikipedia)
However, I noticed that when every proxy is trusted, play reports the host address as the remote address when it should really use the header leftmost value. It creates problems, for example, in my case, when play runs inside a container, where relying on forwarded-for always gives a useless ip.
Can you please proide some debugging information? You can obfuscate the IP addresses if you like.
Suggested information:
- information about which Play version you're using
X-Forwarded-ForandX-Forwarded-Protoheader values- values for the
remoteAddressandsecureproperties of the request - snippet of the code where you're dumping the header values and request properties so we can check which calls you make
- explanations about which IP addresses correspond to the host, proxy, browser, etc
- configuration values
Hello, We use Play 2.4.2 .
Example with unexpected value (case described in the issue)
- In application.conf :
play.http.forwarded.trustedProxies=["0.0.0.0/0"]
- Request Header (send from 172.2.3.4) :
X-Forwarded-For: 65.78.45.55, 52.67.67.55
- Result :
remoteAddress = 172.2.3.4 (we expected 65.78.45.55)
Example with expected value
- In application.conf :
play.http.forwarded.trustedProxies=["172.0.0.0/8"]
- Request (send from 172.2.3.4) :
X-Forwarded-For: 65.78.45.55, 52.67.67.55
- Result :
remoteAddress = 52.67.67.55 (expected behavior)
This example come from an elastic beanstalk usecase : 172.2.3.4 is a docker host ip, 52.67.67.55 is an amazon elastic load balancer. After looking into the play source, in the first example remoteAddress is 172.2.3.4 instead of 65.78.45.55 because 65.78.45.55 is on the trusted subnet (0.0.0.0/0).
Good catch.
Is this still an issue with Play 2.5?
seems to be ok. I did two tests here: https://github.com/martinpallmann/playframework/tree/issue-4906
@martinpallmann Could you please provide a pull request with your tests?
How should this be tested correctly? My test setup:
- Play running on 127.0.0.1:9000 (assigned internal IP adres: 15.0.8.15), with nginx running in front on port 8090
- In my HomeController I log the IP: Logger.debug(request().host());
When I connect from another computer (ip 15.0.8.30), on the same network I would still see the following output:
[debug] application - 15.0.8.15:8090
I have configured my nginx just like the provided example: https://www.playframework.com/documentation/2.5.x/HTTPServer#Set-up-with-nginx
- I am not using HTTPS locally
@adis-me could you try to adding a proxy_set_header X-Real-IP $remote_addr;?
We have the following:
nginx <-> nginx <-> play
and that works and gives the correct ip with play.http.forwarded.trustedProxies=["0.0.0.0/0", "::/0"]. (Our play instance is behind a firewall so no problem)
However we are migrating to nginx<->haproxy<->play while our nginx only handles the HTTP2 stuff, etc.
This is my nginx configuration (partly):
location / {
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_buffering off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Scheme $scheme;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_pass http://my-backend;
}
What I do not have is a trustedProxies key in my application.conf. But I did not find any documentation about this setting. Although when I try with play.http.forwarded.trustedProxies=["0.0.0.0/0"] it gives the same output..
@adis-me unfortunatly it's request().remoteAddress() not request().host()
@schmitch Yes, sorry my bad! Then I can confirm too, that this problem is not occurring with 2.5.3. Thanks for your help!
@mkurz I created a pull request: https://github.com/playframework/playframework/pull/6186 Strangely it is not linked here.
@gmethvin this issue can be closed then?
I think this can be closed now.