RollbarSharp icon indicating copy to clipboard operation
RollbarSharp copied to clipboard

Issue with User IP and X-Forwarded-By

Open olaj opened this issue 9 years ago • 2 comments

Not sure how to fix this but this method.

private static string IpFromXForwardedFor(HttpRequest request)
{
    var forwardedFor = request.Headers["X-Forwarded-For"];
    if (!string.IsNullOrEmpty(forwardedFor) && forwardedFor.Contains(","))
    {
        forwardedFor = forwardedFor.Split(',').Last().Trim();
    }
    return forwardedFor;
}

Didn't work well with my setup with Cloudflare. My header was like this.

X_FORWARDED_FOR: 196.215.208.37, 197.214.212.59:57956

Where the first one is the "good ip" and the second one is from Cloudflare. RollbarSharp was taking the last one and Rollbar didn't even accept that as an IP (because of the port i guess) and well it's the Cloudflare IP anyway so not that interesting. Just wanted to let you know. I run with a fork now where i take the first IP instead.

olaj avatar Feb 23 '16 10:02 olaj

@olaj I use AWS, and AWS does use this pattern

X-Forwarded-For: OriginatingClientIPAddress, proxy1-IPAddress, proxy2-IPAddress

Source: X-Forwarded Headers for Elastic Load Balancing

In this case I guess that cloudflare use the same pattern, so we could use the first IP address instead the last one.

Could you fix that?

If yes, doesn't forget the unit tests :stuck_out_tongue:

AlbertoMonteiro avatar Feb 23 '16 12:02 AlbertoMonteiro

actually CloudFlare passes the correct user IP Address in it's own header.

so, just to include CF the code should also have:

var ipCloudFlare = request.Headers["CF-Connecting-IP"];
if (!string.IsNullOrEmpty(ipCloudFlare))
    return ipCloudFlare;

balexandre avatar Dec 19 '16 09:12 balexandre