Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Forwarded header for IPv4 with dual stack sockets #2509

Merged
merged 4 commits into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/ReverseProxy/Transforms/RequestHeaderForwardedTransform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,16 @@ private void AppendFor(HttpContext context, ref ValueStringBuilder builder)
builder.Append(';');
}
builder.Append("for=");
AppendNode(context.Connection.RemoteIpAddress, context.Connection.RemotePort, ForFormat, ref builder);

IPAddress? remoteIp = null;
var remoteIpAddress = context.Connection.RemoteIpAddress;
if (remoteIpAddress is not null)
{
remoteIp = remoteIpAddress.IsIPv4MappedToIPv6 ?
remoteIpAddress.MapToIPv4() :
remoteIpAddress;
}
AppendNode(remoteIp, context.Connection.RemotePort, ForFormat, ref builder);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ await transform.ApplyAsync(new RequestTransformContext()
[InlineData("", "127.0.0.1", 2, NodeFormat.Ip, ForwardedTransformActions.Set, "for=127.0.0.1")]
[InlineData("", "127.0.0.1", 2, NodeFormat.IpAndPort, ForwardedTransformActions.Append, "for=\"127.0.0.1:2\"")]
[InlineData("", "127.0.0.1", 2, NodeFormat.IpAndRandomPort, ForwardedTransformActions.Append, "for=\"127.0.0.1:_abcdefghi\"")]
[InlineData("", "::ffff:127.0.0.1", 2, NodeFormat.Ip, ForwardedTransformActions.Set, "for=127.0.0.1")]
[InlineData("", "::ffff:127.0.0.1", 2, NodeFormat.IpAndPort, ForwardedTransformActions.Append, "for=\"127.0.0.1:2\"")]
[InlineData("", "::ffff:127.0.0.1", 2, NodeFormat.IpAndRandomPort, ForwardedTransformActions.Append, "for=\"127.0.0.1:_abcdefghi\"")]
[InlineData("", "::1", 2, NodeFormat.Random, ForwardedTransformActions.Set, "for=_abcdefghi")]
[InlineData("", "::1", 2, NodeFormat.RandomAndPort, ForwardedTransformActions.Append, "for=\"_abcdefghi:2\"")]
[InlineData("", "::1", 2, NodeFormat.RandomAndRandomPort, ForwardedTransformActions.Append, "for=\"_abcdefghi:_jklmnopqr\"")]
Expand Down
Loading