AntiSSRFPolicy.AddAllowedAddresses Method
Definition
Adds IP networks to be explicitly allowed by the policy.
public void AddAllowedAddresses(string[] networks)
AllowedAddressestakes precedence overDeniedAddresses. If an IP address matches both, it will be considered allowed by the policy.
Parameters
networks: string[]
The list of IP networks to be explicitly allowed by the policy.
Exceptions
ArgumentNullException
- The
networksparameter isnullor containsnullvalues.
FormatException
- A network string is not in valid CIDR format.
AntiSSRFException
- Attempted to edit the policy after it has been used to create a handler via
GetHandler().
Examples
using Microsoft.Security.AntiSSRF;
using System;
using System.Net.Http;
using System.Threading.Tasks;
// Customize the policy
var policy = new AntiSSRFPolicy(PolicyConfigOptions.None);
policy.DenyAllUnspecifiedIPs = true;
policy.AddAllowedAddresses(new[] { "1.2.3.4" });
// Create HttpClient with the policy handler
using var httpClient = new HttpClient(policy.GetHandler());
try
{
// If the untrusted hostname directs to 1.2.3.4,
// the request will succeed here
var response = await httpClient.GetAsync("https://<some_untrusted_hostname>/public/data");
}
catch (AntiSSRFException ex)
{
// If untrusted hostname directs to anything besides 1.2.3.4,
// the request will fail here with an AntiSSRFException
}