On Heroku Cedar, I wanted to get the client's IP. First attempt:
ENV['REMOTE_ADDR']
This does not work, of course, because all requests are passed through proxies. So the alternative was to use:
ENV['HTTP_X_FORWARDED_FOR']
But this is not quite safe, is it?
If it contains only one value, I take this. If it contains more than one value (comma-separated), I could take the first one.
But what if someone manipulates this value? I cannot trust ENV['HTTP_X_FORWARDED_FOR']
as I could with ENV['REMOTE_ADDR']
. And there is no list of trusted proxies that I could use, either.
But there must be some way to reliably get the client's IP address, always. Do you know one?
In their docs, Heroku describes that X-Forwarded-For
is "the originating IP address of the client connecting to the Heroku router".
This sounds as if Heroku could be overwriting the X-Forwarded-For
with the originating remote IP. This would prevent spoofing, right? Can someone verify this?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…