Skip to content

Commit

Permalink
Fix X-Forwarded-For for IPv4 with dual stack sockets #2399 (#2400)
Browse files Browse the repository at this point in the history
* Fix X-Forwarded-For for IPv4 with dual stack sockets #2399

* Address review feedback
  • Loading branch information
hacst authored Feb 15, 2024
1 parent b12b34b commit d918126
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,14 @@ public override ValueTask ApplyAsync(RequestTransformContext context)
throw new ArgumentNullException(nameof(context));
}

var remoteIp = context.HttpContext.Connection.RemoteIpAddress?.ToString();
string? remoteIp = null;
var remoteIpAddress = context.HttpContext.Connection.RemoteIpAddress;
if (remoteIpAddress is not null)
{
remoteIp = remoteIpAddress.IsIPv4MappedToIPv6 ?
remoteIpAddress.MapToIPv4().ToString() :
remoteIpAddress.ToString();
}

switch (TransformAction)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ public class RequestHeaderXForwardedForTransformTests
[InlineData("", "", ForwardedTransformActions.Remove, "")]
[InlineData("", "::1", ForwardedTransformActions.Set, "::1")]
[InlineData("", "127.0.0.1", ForwardedTransformActions.Set, "127.0.0.1")]
[InlineData("", "::ffff:127.0.0.1", ForwardedTransformActions.Set, "127.0.0.1")]
[InlineData("", "127.0.0.1", ForwardedTransformActions.Append, "127.0.0.1")]
[InlineData("", "::ffff:127.0.0.1", ForwardedTransformActions.Append, "127.0.0.1")]
[InlineData("", "127.0.0.1", ForwardedTransformActions.Remove, "")]
[InlineData("existing,Header", "", ForwardedTransformActions.Set, "")]
[InlineData("existing;Header", "", ForwardedTransformActions.Set, "")]
Expand Down

0 comments on commit d918126

Please sign in to comment.