Skip to main content

Drop-in SSRF protection for httpx

Project description

httpx-secure

PyPI - Python Version

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

  1. Cache Lookup: First checks if the host has been recently validated and cached
  2. DNS Resolution: If not cached, resolves the hostname to an IP address
  3. Validation: Verifies the IP is globally routable, blocking private/internal addresses
  4. Custom Validation: If provided, your custom validator is called for additional checks
  5. 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

httpx_secure-1.3.0.tar.gz (9.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

httpx_secure-1.3.0-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

File details

Details for the file httpx_secure-1.3.0.tar.gz.

File metadata

  • Download URL: httpx_secure-1.3.0.tar.gz
  • Upload date:
  • Size: 9.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.13

File hashes

Hashes for httpx_secure-1.3.0.tar.gz
Algorithm Hash digest
SHA256 b076ab3551c5e7f7b92d092c234e010e230832800ec4142d4186666fb88bb449
MD5 c85b2825daf7bf224f1013df0e74138a
BLAKE2b-256 ee112586dd9ce949839544416b8e047b87e1ab4349e5a460711c396a61e74f4a

See more details on using hashes here.

Provenance

The following attestation bundles were made for httpx_secure-1.3.0.tar.gz:

Publisher: ci.yaml on Zaczero/pkgs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file httpx_secure-1.3.0-py3-none-any.whl.

File metadata

  • Download URL: httpx_secure-1.3.0-py3-none-any.whl
  • Upload date:
  • Size: 5.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.13

File hashes

Hashes for httpx_secure-1.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 913478890e086a2fd14b80e603116ace22e229d88562b82314a3be76d5c38279
MD5 e1347bb940bcdb8d1fbee5eddfed3d69
BLAKE2b-256 5e44c14ee9449b1c09e0d5e1014a90ad2cf3b6feeb3d56887acde5370ce3821e

See more details on using hashes here.

Provenance

The following attestation bundles were made for httpx_secure-1.3.0-py3-none-any.whl:

Publisher: ci.yaml on Zaczero/pkgs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page