Closed SebastianC closed 6 years ago
One trouble with this is that people have been adding the IPv6 versions as a workaround. Changing it internally will break them unless you also change the KnownNetworks values for them.
I had originally considered updating another property to ForwardedHeadersOptions
to indicate that this conversion should be turned "on", and default it to "off". Would that resolve your concern?
If it's a KnownProxy, I can see how adding the mapped-value would work, but i was unable to create a KnownNetwork that would be matched appropriately.
My apologies for the spam, but another option would be to check both variations:
private bool CheckKnownAddress(IPAddress address)
{
if (address.IsIPv4MappedToIPv6)
{
var ipv4Address = address.MapToIPv4();
if (_options.KnownProxies.Contains(ipv4Address))
{
return true;
}
foreach (var network in _options.KnownNetworks)
{
if (network.Contains(ipv4Address))
{
return true;
}
}
}
if (_options.KnownProxies.Contains(address))
{
return true;
}
foreach (var network in _options.KnownNetworks)
{
if (network.Contains(address))
{
return true;
}
}
return false;
}
Checking both varians is a good mitigation.
How's this for a simplification?
private bool CheckKnownAddress(IPAddress address)
{
if (address.IsIPv4MappedToIPv6)
{
var ipv4Address = address.MapToIPv4();
if (CheckKnownAddress(ipv4Address))
{
return true;
}
}
if (_options.KnownProxies.Contains(address))
{
return true;
}
foreach (var network in _options.KnownNetworks)
{
if (network.Contains(address))
{
return true;
}
}
return false;
}
Better than mine. Will update the PR.
My scenario:
My problem:
Proposed fix:
Microsoft.AspNetCore.HttpOverrides.ForwardedHeadersMiddleware
functionCheckKnownAddress
, check ifaddress.IsIPv4MappedToIPv6
, and if it is, map it back to the IPv4 address before continuing. Pull request coming shortly.This solution seems like it would also resolve another issue: #341 - "::ffff:127.0.0.1 not recognized as trusted local proxy"