Skip to content

Commit

Permalink
Fix Forwarded header for IPv4 with dual stack sockets (#2509)
Browse files Browse the repository at this point in the history
* prefer IPv4 addresses

* Handling remote and local IPs

* nit: formatting

* Code style

---------

Co-authored-by: Ryan Karg <rkarg@blizzard.com>
Co-authored-by: Miha Zupan <mihazupan.zupan1@gmail.com>
  • Loading branch information
3 people authored May 20, 2024
1 parent d3e38a1 commit ef089b0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,12 @@ private void AppendBy(HttpContext context, ref ValueStringBuilder builder)
// https://tools.ietf.org/html/rfc7239#section-6
private void AppendNode(IPAddress? ipAddress, int port, NodeFormat format, ref ValueStringBuilder builder)
{
// Prefer IPv4 formatting
if (ipAddress is { IsIPv4MappedToIPv6: true })
{
ipAddress = ipAddress.MapToIPv4();
}

// "It is important to note that an IPv6 address and any nodename with
// node-port specified MUST be quoted, since ":" is not an allowed
// character in "token"."
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 Expand Up @@ -139,6 +142,9 @@ await transform.ApplyAsync(new RequestTransformContext()
[InlineData("", "127.0.0.1", 2, NodeFormat.Ip, ForwardedTransformActions.Set, "by=127.0.0.1")]
[InlineData("", "127.0.0.1", 2, NodeFormat.IpAndPort, ForwardedTransformActions.Append, "by=\"127.0.0.1:2\"")]
[InlineData("", "127.0.0.1", 2, NodeFormat.IpAndRandomPort, ForwardedTransformActions.Append, "by=\"127.0.0.1:_abcdefghi\"")]
[InlineData("", "::ffff:127.0.0.1", 2, NodeFormat.Ip, ForwardedTransformActions.Set, "by=127.0.0.1")]
[InlineData("", "::ffff:127.0.0.1", 2, NodeFormat.IpAndPort, ForwardedTransformActions.Append, "by=\"127.0.0.1:2\"")]
[InlineData("", "::ffff:127.0.0.1", 2, NodeFormat.IpAndRandomPort, ForwardedTransformActions.Append, "by=\"127.0.0.1:_abcdefghi\"")]
[InlineData("", "::1", 2, NodeFormat.Random, ForwardedTransformActions.Set, "by=_abcdefghi")]
[InlineData("", "::1", 2, NodeFormat.RandomAndPort, ForwardedTransformActions.Append, "by=\"_abcdefghi:2\"")]
[InlineData("", "::1", 2, NodeFormat.RandomAndRandomPort, ForwardedTransformActions.Append, "by=\"_abcdefghi:_jklmnopqr\"")]
Expand Down

0 comments on commit ef089b0

Please sign in to comment.