AntiSSRF-rs
Rust implementation of Microsoft AntiSSRF — a security library for preventing Server-Side Request Forgery (SSRF) attacks.
Languages
Overview
AntiSSRF-rs is a Rust version of Microsoft's AntiSSRF library, designed to prevent Server-Side Request Forgery (SSRF) attacks by validating every outbound network request against a configurable security policy. It integrates seamlessly with reqwest and reqwest-middleware to provide DNS-level IP blocking, header validation, protocol enforcement, and redirect-chain re-validation.
Features
| Feature | Description |
|---|---|
| IP Blocking | Block internal/sensitive IP addresses (localhost, private networks, cloud metadata services) using CIDR ranges |
| IPv6 Normalization | Automatically maps IPv4 to IPv6-mapped addresses (::ffff:) for consistent CIDR matching |
| Header Enforcement | Require specific headers (e.g., X-Forwarded-For) and deny dangerous headers |
| Protocol Control | Reject plaintext HTTP to untrusted endpoints |
| Redirect Validation | Re-validate every hop in a redirect chain against the same policy |
| Domain Validation | Check URLs against trusted domains (with subdomain support) and Azure service endpoints |
| Immutable Policy | Policy locks after first use to prevent runtime tampering |
| Optional Networking | Core library has zero network dependencies; reqwest-integration feature adds HTTP client support |
Quick Start
1. Add to Cargo.toml
[]
= "0.1"
Or with explicit feature control:
[]
# Core only (no HTTP client dependencies)
= { = "0.1", = false }
# Full reqwest integration (default)
= { = "0.1", = ["reqwest-integration"] }
2. Basic Policy
use ;
// Block all known dangerous IPs (IMDS, WireServer, private networks, etc.)
let policy = new;
3. With reqwest (DNS-level IP validation)
use ;
use AntiSSRFClientBuilder;
let policy = new;
let client = new
.timeout
.build
.expect;
4. With reqwest_middleware (header + protocol validation)
use ;
use AntiSSRFMiddleware;
use ClientBuilder;
let policy = new;
let middleware = new;
let client = new
.with
.build;
5. Fine-Grained Policy Configuration
use ;
let mut policy = new;
// Allowlist takes precedence over denylist
policy.add_allowed_addresses?;
// Require specific headers
policy.add_required_headers?;
// Deny dangerous headers
policy.add_denied_headers?;
// Block plaintext HTTP
policy.set_allow_plaintext_http?;
// Validate an outgoing request
let mut headers = vec!;
let allowed = policy.validate_request?;
assert!;
6. Domain Validation
use uri_validator;
// Check if URL is in a trusted domain (supports subdomains)
assert!;
// Azure Key Vault domain validation
assert!;
// Rejects hostnames containing '--' (Azure naming restriction)
assert!;
PolicyConfigOptions
| Option | Behavior |
|---|---|
None |
No restrictions — allow all requests |
InternalOnly |
Block external IPs (deny all unspecified) |
ExternalOnlyV1 |
Block IMDS + WireServer + special ranges (v1 blocklist) |
ExternalOnlyLatest |
Same as V1 — alias for the latest recommended blocklist |
Modules
| Module | Purpose |
|---|---|
error |
Error types (AntiSSRFError) |
cidr |
CIDR block parsing and IP containment |
ip_address_ranges |
Predefined special IP ranges (IMDS, WireServer, loopback, etc.) |
domains |
Azure service domain lists (Key Vault, Storage) |
uri_validator |
URL and domain validation utilities |
policy |
Policy configuration and runtime enforcement |
network |
reqwest integration (resolver + middleware + client builder) |
Running Tests
# Core tests (no network dependencies)
# With reqwest integration (default features)
# All tests with output
# Documentation tests
Security Design
- Evaluation Order: Allowlist →
deny_all_unspecified_ips→ Denylist. An allowlisted IP is always permitted, even if it also appears in a denylist. - IPv6 Normalization: All IPv4 addresses are mapped to
::ffff:<ipv4>before CIDR checks. IPv4/24becomes IPv6-mapped/120(add 96 to prefix length). - Case-Insensitive Headers: Header names are matched case-insensitively; header values are matched case-sensitively.
- Edit Lock: Once a policy is used for validation, it becomes immutable to prevent runtime tampering.
- Redirect Re-Validation: Every redirect hop is re-validated against the same policy, preventing open-redirect bypasses.
License
MIT — See LICENSE for details.