Rails: `request.remote_ip` appears to return the wrong IP address

Created on 18 Oct 2012  ·  3Comments  ·  Source: rails/rails

The last commit to the remote IP middleware appears to have deliberately reversed the algorithm that picks the remote IP out of a list of proxies. Unfortunately, the previous algorithm (which I wrote, and used in production with a few hundred thousand clients :P) is the one that agrees with the written spec for how X-Forwarded-For is supposed to work.

tl;dr plz to revert that part of the commit, I would like to be able to upgrade Rails and still have remote_ip return the right IP address.

actionpack

Most helpful comment

This is still an issue. From stack overflow posts: https://stackoverflow.com/questions/5636032/request-remote-ip-returns-wrong-ip
https://stackoverflow.com/questions/4465476/rails-get-client-ip-address
https://stackoverflow.com/questions/19317255/rails-how-to-obtain-visitors-ip-address
https://stackoverflow.com/questions/1226959/how-to-get-client-ip-and-server-ip-using-rails

All of these questions point to using request.remote_ip. But that returns an incorrect IP address sometimes. I use the following code to get a correct IP address.

      response = open('https://jsonip.com/').read
      data = JSON.parse(response)
      ip_address = data['ip']
    rescue
      ip_address = request.remote_ip
    end

Is there any possibility to fix this in a future release?

All 3 comments

This is still an issue. From stack overflow posts: https://stackoverflow.com/questions/5636032/request-remote-ip-returns-wrong-ip
https://stackoverflow.com/questions/4465476/rails-get-client-ip-address
https://stackoverflow.com/questions/19317255/rails-how-to-obtain-visitors-ip-address
https://stackoverflow.com/questions/1226959/how-to-get-client-ip-and-server-ip-using-rails

All of these questions point to using request.remote_ip. But that returns an incorrect IP address sometimes. I use the following code to get a correct IP address.

      response = open('https://jsonip.com/').read
      data = JSON.parse(response)
      ip_address = data['ip']
    rescue
      ip_address = request.remote_ip
    end

Is there any possibility to fix this in a future release?

Looks like #24521 is to fix this issue? Will any of you be willing to move that PR forward?

Was this page helpful?
0 / 5 - 0 ratings