Skip to content

Commit

Permalink
Simplify X-Forwarded-For handling
Browse files Browse the repository at this point in the history
This happens in asp.net middleware now

Closes #2033
Closes #2034

(cherry picked from commit 16e2d130e6a2e7239bcfe92187a7f990f93eff00)
Co-Authored-By: ta264 <ta264@users.noreply.github.com>
  • Loading branch information
Qstick committed Jan 24, 2023
1 parent 13dad33 commit e62d001
Showing 1 changed file with 1 addition and 33 deletions.
34 changes: 1 addition & 33 deletions src/Readarr.Http/Extensions/RequestExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,39 +162,7 @@ public static string GetRemoteIP(this HttpRequest request)
remoteIP = remoteIP.MapToIPv4();
}

var remoteAddress = remoteIP.ToString();

// Only check if forwarded by a local network reverse proxy
if (remoteIP.IsLocalAddress())
{
var realIPHeader = request.Headers["X-Real-IP"];
if (realIPHeader.Any())
{
return realIPHeader.First().ToString();
}

var forwardedForHeader = request.Headers["X-Forwarded-For"];
if (forwardedForHeader.Any())
{
// Get the first address that was forwarded by a local IP to prevent remote clients faking another proxy
foreach (var forwardedForAddress in forwardedForHeader.SelectMany(v => v.Split(',')).Select(v => v.Trim()).Reverse())
{
if (!IPAddress.TryParse(forwardedForAddress, out remoteIP))
{
return remoteAddress;
}

if (!remoteIP.IsLocalAddress())
{
return forwardedForAddress;
}

remoteAddress = forwardedForAddress;
}
}
}

return remoteAddress;
return remoteIP.ToString();
}

public static void DisableCache(this IHeaderDictionary headers)
Expand Down

0 comments on commit e62d001

Please sign in to comment.