Drop-in SSRF protection for httpx
Project description
httpx-secure
Drop-in SSRF protection for httpx.
Why Use This?
- SSRF Protection: Block requests to private/internal IP addresses
- Custom Validation: Extend with your own validation logic
- Minimal Overhead: Efficient implementation with built-in DNS caching
- Broad Python Support: Compatible with Python 3.9+
- Semantic Versioning: Predictable, reliable updates
- Zero-Clause BSD: Public domain, use freely anywhere
Installation
pip install httpx-secure
Quick Start
import httpx
from httpx_secure import httpx_ssrf_protection
client = httpx_ssrf_protection(
httpx.AsyncClient(),
dns_cache_size=1000, # Cache up to 1000 DNS resolutions
dns_cache_ttl=600, # Cache for 10 minutes
)
await client.get("https://public.domain") # Allowed
await client.get("https://private.domain") # Blocked
Custom Validation
For example, implement a simple domain whitelist to restrict requests to specific hosts:
import httpx
from httpx_secure import httpx_ssrf_protection
from ipaddress import IPv4Address, IPv6Address
def custom_validator(
hostname: str,
ip: IPv4Address | IPv6Address,
port: int
) -> bool:
return hostname in {
"whitelisted.domain",
"webhook.partner.com",
}
client = httpx_ssrf_protection(
httpx.AsyncClient(),
custom_validator=custom_validator,
)
await client.get("https://whitelisted.domain") # Allowed
await client.get("https://unknown.domain") # Blocked
How It Works
- Cache Lookup: First checks if the host has been recently validated and cached
- DNS Resolution: If not cached, resolves the hostname to an IP address
- Validation: Verifies the IP is globally routable, blocking private/internal addresses
- Custom Validation: If provided, your custom validator is called for additional checks
- Request Modification: Rewrites the request to use the validated IP directly
The DNS cache significantly reduces latency for repeated requests, while per-host locking ensures efficient concurrent resolution of parallel requests.
[!TIP] The SSRF protection applies to all HTTP methods (GET, POST, PUT, DELETE, etc.) and automatically validates redirects to prevent SSRF attacks through redirect chains.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file httpx_secure-1.2.0.tar.gz.
File metadata
- Download URL: httpx_secure-1.2.0.tar.gz
- Upload date:
- Size: 18.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6e4ec4fede8d19094e29312312d82e3468092c3cd8d53647d58c0f865cf8ec81
|
|
| MD5 |
ef9ebbdd0918ac51ebf090b6ee8edc94
|
|
| BLAKE2b-256 |
09c1a06452c56e05f2b5cb4771e7424099ba4c855466fecacaae894324c8f4b1
|
Provenance
The following attestation bundles were made for httpx_secure-1.2.0.tar.gz:
Publisher:
workflow.yaml on Zaczero/httpx-secure
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
httpx_secure-1.2.0.tar.gz -
Subject digest:
6e4ec4fede8d19094e29312312d82e3468092c3cd8d53647d58c0f865cf8ec81 - Sigstore transparency entry: 401985729
- Sigstore integration time:
-
Permalink:
Zaczero/httpx-secure@57ca9733bd7fb0cf547c3a1f6c17b17db038625e -
Branch / Tag:
refs/tags/1.2.0 - Owner: https://github.com/Zaczero
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
workflow.yaml@57ca9733bd7fb0cf547c3a1f6c17b17db038625e -
Trigger Event:
push
-
Statement type:
File details
Details for the file httpx_secure-1.2.0-py3-none-any.whl.
File metadata
- Download URL: httpx_secure-1.2.0-py3-none-any.whl
- Upload date:
- Size: 5.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d97c806e4ba115c7b6e3f91c8e7f37d83823fae5d80f5e8f2fd86bf09f2888a7
|
|
| MD5 |
ded9f2c46c94a1f44e21993e25dd1e24
|
|
| BLAKE2b-256 |
aec838966b87dbb7e1f2dffcbb9fc6168580eb970c336c810ff8bf3554a33699
|
Provenance
The following attestation bundles were made for httpx_secure-1.2.0-py3-none-any.whl:
Publisher:
workflow.yaml on Zaczero/httpx-secure
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
httpx_secure-1.2.0-py3-none-any.whl -
Subject digest:
d97c806e4ba115c7b6e3f91c8e7f37d83823fae5d80f5e8f2fd86bf09f2888a7 - Sigstore transparency entry: 401985757
- Sigstore integration time:
-
Permalink:
Zaczero/httpx-secure@57ca9733bd7fb0cf547c3a1f6c17b17db038625e -
Branch / Tag:
refs/tags/1.2.0 - Owner: https://github.com/Zaczero
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
workflow.yaml@57ca9733bd7fb0cf547c3a1f6c17b17db038625e -
Trigger Event:
push
-
Statement type: