playframework icon indicating copy to clipboard operation
playframework copied to clipboard

Behaviour of the ForwardedHeaderHandler with X-Forwarded-For

Open fredoche opened this issue 11 years ago • 14 comments

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.

fredoche avatar Jul 29 '15 11:07 fredoche

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-For and X-Forwarded-Proto header values
  • values for the remoteAddress and secure properties 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

richdougherty avatar Jul 30 '15 01:07 richdougherty

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).

jdauphant avatar Jul 30 '15 08:07 jdauphant

Good catch.

jroper avatar Aug 03 '15 06:08 jroper

Is this still an issue with Play 2.5?

mkurz avatar Mar 11 '16 10:03 mkurz

seems to be ok. I did two tests here: https://github.com/martinpallmann/playframework/tree/issue-4906

martinpallmann avatar May 23 '16 15:05 martinpallmann

@martinpallmann Could you please provide a pull request with your tests?

mkurz avatar May 23 '16 22:05 mkurz

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 avatar May 24 '16 08:05 adis-me

@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.

schmitch avatar May 24 '16 08:05 schmitch

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 avatar May 24 '16 08:05 adis-me

@adis-me unfortunatly it's request().remoteAddress() not request().host()

schmitch avatar May 24 '16 08:05 schmitch

@schmitch Yes, sorry my bad! Then I can confirm too, that this problem is not occurring with 2.5.3. Thanks for your help!

adis-me avatar May 24 '16 08:05 adis-me

@mkurz I created a pull request: https://github.com/playframework/playframework/pull/6186 Strangely it is not linked here.

martinpallmann avatar May 24 '16 10:05 martinpallmann

@gmethvin this issue can be closed then?

martinpallmann avatar May 25 '16 11:05 martinpallmann

I think this can be closed now.

mkurz avatar Nov 18 '16 07:11 mkurz