Detailed configuration options for URL Sheriff.
Configuration interface for initializing URLSheriff.
interface URLSheriffConfig {
/**
* Optional array of custom DNS resolver IP addresses (e.g., ['1.1.1.1', '8.8.8.8'])
* When provided, these resolvers are used instead of system DNS
* Resolvers are tried in order until one succeeds
*/
dnsResolvers?: string[];
/**
* Optional array of allowed hostnames or patterns
* Entries can be:
* - String literals: exact hostname matches (e.g., 'localhost', '127.0.0.1')
* - Case-sensitive matching
* - Matches against hostname only, not resolved IP addresses
* - RegExp patterns: pattern-based matching (e.g., /^.*\.internal\.company\.com$/)
* - Case-sensitive by default (use /i flag for case-insensitive)
* - Matches against hostname only
* URLs matching allow-list entries bypass private IP checks
*/
allowList?: Array<string | RegExp>;
/**
* Optional array of allowed URL schemes/protocols (e.g., ['http', 'https', 'ftp'])
* When provided, only URLs with these schemes are allowed
* Schemes are case-insensitive and automatically normalized to lowercase
* When omitted or null, all schemes are allowed
*/
allowedSchemes?: string[];
}Type: string[]? (optional)
Description: Array of custom DNS resolver IP addresses. When provided, these resolvers are used instead of system DNS. Resolvers are tried in order until one succeeds.
Example:
const sheriff = new URLSheriff({
dnsResolvers: ['1.1.1.1', '8.8.8.8'] // Cloudflare and Google DNS
});Behavior:
resolveHostnameViaServers() method becomes availablehostnameLookup())Type: Array<string | RegExp>? (optional)
Description: Array of allowed hostnames or patterns. URLs matching allow-list entries bypass private IP checks.
Entry Types:
String literals: Exact hostname matches
'localhost', '127.0.0.1'RegExp patterns: Pattern-based matching
/i flag for case-insensitive)/^.*\.internal\.company\.com$/Example:
const sheriff = new URLSheriff({
allowList: [
'localhost', // Exact string match
'127.0.0.1', // IP address match
/^.*\.internal\.company\.com$/, // Regex pattern match
/^api-\d+\.staging\.example\.org$/ // Dynamic subdomain pattern
]
});Important Notes:
/i flag for case-insensitive)Type: string[]? (optional)
Description: Array of allowed URL schemes/protocols. When provided, only URLs with these schemes are allowed. Schemes are case-insensitive and automatically normalized to lowercase.
Example:
const sheriff = new URLSheriff({
allowedSchemes: ['https', 'http']
});Behavior:
'HTTPS', 'https', 'Https' all work)null, all schemes are allowed[]) clears restrictions (same as null)setAllowedSchemes()Common Use Cases:
['https'] - Only HTTPS allowed['http', 'https'] - HTTP and HTTPS allowedundefined or null - No restrictionsNo restrictions, blocks all private IPs:
const sheriff = new URLSheriff();HTTPS-only with minimal allow-list:
const sheriff = new URLSheriff({
allowedSchemes: ['https'],
allowList: ['trusted-api.internal.com']
});Allow HTTP/HTTPS and localhost:
const sheriff = new URLSheriff({
allowedSchemes: ['http', 'https'],
allowList: ['localhost', '127.0.0.1']
});Use specific DNS resolvers:
const sheriff = new URLSheriff({
dnsResolvers: ['1.1.1.1', '8.8.8.8', '9.9.9.9']
});Mix of string literals and regex patterns:
const sheriff = new URLSheriff({
allowList: [
'localhost',
'127.0.0.1',
/^.*\.internal\.company\.com$/,
/^api-\d+\.staging\.example\.org$/,
/^service-[a-z]+\.dev\.company\.com:\d+$/
]
});Configuration can be modified at runtime using instance methods:
const sheriff = new URLSheriff({
allowedSchemes: ['https']
});
// Update allowed schemes
sheriff.setAllowedSchemes(['https', 'http']);
// Add to allow-list
sheriff.addToAllowList(['new-trusted-domain.com']);
// Remove from allow-list
sheriff.removeFromAllowList(['localhost']);
// Clear scheme restrictions
sheriff.clearSchemeRestrictions();
// Get current configuration
const schemes = sheriff.getAllowedSchemes();
const allowList = sheriff.getAllowList();Note: dnsResolvers cannot be changed after instantiation. Create a new instance if different DNS resolvers are needed.