SSRF prevention library: validates URLs, blocks private IPs, supports allow-lists and custom DNS resolvers
npx @tessl/cli install tessl/npm-url-sheriff@1.0.0URL Sheriff is a security library that validates URLs and prevents Server-Side Request Forgery (SSRF) attacks by detecting and blocking requests to private or internal network addresses.
npm install url-sheriffPrimary Use Case: Validate URLs before making HTTP requests to prevent SSRF attacks.
Core Method: isSafeURL(url: string | URL): Promise<boolean> - Main validation method that throws Error if URL is unsafe.
Key Capabilities:
When to Use:
Error Handling: All methods throw standard JavaScript Error objects. Always wrap isSafeURL() calls in try-catch blocks.
import URLSheriff from 'url-sheriff';For CommonJS:
const URLSheriff = require('url-sheriff');import URLSheriff from 'url-sheriff';
const sheriff = new URLSheriff();
try {
await sheriff.isSafeURL('https://example.com'); // Returns true
await sheriff.isSafeURL('http://127.0.0.1:3000'); // Throws Error
} catch (error) {
console.error('URL is unsafe:', error.message);
}See Quick Start Guide for detailed getting started instructions.
URL Sheriff performs multi-layered URL validation:
The library supports custom DNS resolvers, runtime allow-list management, and debug logging via Node.js debuglog.
| Method | Description | Returns |
|---|---|---|
isSafeURL(url) | Validates if URL is safe | Promise<boolean> (throws Error if unsafe) |
isPrivateIPAddress(ip) | Checks if IP is private | boolean |
hostnameLookup(hostname) | DNS lookup (system resolver) | Promise<string[]> (IPv4 and IPv6) |
resolveHostnameViaServers(hostname) | DNS lookup (custom resolvers) | Promise<string[]> (IPv4 only) |
addToAllowList(entries) | Add allow-list entries | void |
removeFromAllowList(entries) | Remove allow-list entries | void |
getAllowList() | Get current allow-list | Array<string | RegExp> |
setAllowedSchemes(schemes) | Set allowed schemes | string[] | null |
getAllowedSchemes() | Get allowed schemes | string[] | null |
clearSchemeRestrictions() | Remove scheme restrictions | void |
| Option | Type | Description |
|---|---|---|
dnsResolvers | string[]? | Custom DNS resolver IP addresses |
allowList | Array<string | RegExp>? | Trusted hostnames/patterns (bypass private IP checks) |
allowedSchemes | string[]? | Allowed URL schemes/protocols (case-insensitive) |
| Error Message | Meaning |
|---|---|
| "Invalid URL provided" | URL string cannot be parsed |
| "URL scheme '{scheme}' is not allowed" | Protocol not in allowed schemes |
| "URL uses a private hostname" | Hostname is private IP or resolves to private IPs |
| DNS error messages | DNS resolution failed (ENOTFOUND, ETIMEDOUT, etc.) |
| "DNS resolver is not defined" | resolveHostnameViaServers called without dnsResolvers config |
Real-World Scenarios - Comprehensive usage examples
Edge Cases - Advanced scenarios and corner cases
API Reference - Complete API documentation
Configuration Reference - Detailed configuration options
URL Sheriff protects against SSRF attacks by:
See Real-World Scenarios for security best practices.
Enable debug logging with Node.js util.debuglog:
NODE_DEBUG=url-sheriff node your-app.jsDebug logs include initialization, URL parsing, DNS resolution, allow-list checking, IP validation, and scheme validation.
url, node:dns/promises, node:util