Skip to content

Commit

Permalink
Improve logic for parsing IPNetworks in AuthorizationPolicyBuilderExt…
Browse files Browse the repository at this point in the history
…ensions
  • Loading branch information
mburumaxwell committed Nov 22, 2023
1 parent 678b0ba commit 62c571f
Showing 1 changed file with 4 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,12 @@ public static AuthorizationPolicyBuilder RequireApprovedNetworks(this Authorizat
if (!networks.Any()) return builder;

// reduce the networks where possible (referred to as supernetting)
#if NET8_0_OR_GREATER
var reduced = networks;
#else
var reduced = IPNetwork.Supernet(networks.ToArray());
#if !NET8_0_OR_GREATER
networks = IPNetwork.Supernet([.. networks]);
#endif

// add the requirement
return builder.AddRequirements(new ApprovedIPNetworkRequirement(reduced));
return builder.AddRequirements(new ApprovedIPNetworkRequirement(networks));
}

/// <summary>
Expand Down Expand Up @@ -138,11 +136,7 @@ public static AuthorizationPolicyBuilder RequireNetworkFromDns(this Authorizatio
var ips = Dns.GetHostAddresses(f);

// parse the IP addresses into IP networks
#if NET8_0_OR_GREATER
var rawNetworks = ips?.Select(ip => IPNetwork.Parse($"{ip}/32")) ?? Array.Empty<IPNetwork>();
#else
var rawNetworks = ips?.Select(ip => IPNetwork.Parse(ip.ToString(), CidrGuess.ClassLess)) ?? Array.Empty<IPNetwork>();
#endif
var rawNetworks = ips?.Select(ip => new IPNetwork(ip, (byte)(ip.AddressFamily is AddressFamily.InterNetwork ? 32 : 128)));

// add networks into the list if there are any
if (rawNetworks?.Any() ?? false)
Expand Down

0 comments on commit 62c571f

Please sign in to comment.