Registry / http-networking / safehttpx

safehttpx

JSON →
library0.1.7pypypi✓ verified 24d ago

safehttpx is a small Python library designed to protect applications from Server Side Request Forgery (SSRF) attacks. It provides an asynchronous `safehttpx.get()` method, which wraps `httpx.AsyncClient.get()` while performing DNS validation using Google DNS and implementing mitigation for DNS rebinding attacks. The current version is 0.1.7, and releases are irregular, driven primarily by security updates and the needs of its primary consumer, Gradio.

pip install safehttpx
INSTALL
IMPORT
SIG · SAFEHTTPX
S
safehttpx
http-networkingpythonv0.1.7
Install
2.1s avg
Import
354ms
Disk
20MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.1.7 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.298s · 21.9MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 2.1s · import 0.268s · 22MB
20MB installed
● package 20MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

get
from safehttpx import get
import safehttpx as sh await sh.get(...)

This quickstart demonstrates how to use `safehttpx.get()` asynchronously. It attempts to fetch a valid external URL and then demonstrates how an attempt to access a local IP address (a common SSRF target) is blocked by default, raising a `ValueError`. Remember to run this in an async context.

import asyncio import safehttpx as sh async def fetch_safe_url(): try: response = await sh.get("https://huggingface.co") response.raise_for_status() # Raise an exception for HTTP errors print(f"Success: {response.status_code} - {response.url}") # Example of blocked internal IP await sh.get("http://127.0.0.1") except ValueError as e: print(f"Validation Error: {e}") except Exception as e: print(f"An unexpected error occurred: {e}") if __name__ == "__main__": asyncio.run(fetch_safe_url())
Debug
Known issues
gotchaSetting `_transport=False` in `safehttpx.get()` explicitly bypasses all SSRF protections, effectively making the call equivalent to a raw `httpx.AsyncClient.get()`. This should only be used if you fully understand and accept the security implications.
fix
Avoid using `_transport=False` unless absolutely necessary and with a clear understanding of the risks. Ensure all URLs passed to `safehttpx.get()` are intended for public access.
affects: All versions
gotcha`safehttpx.get()` is an asynchronous function. It must be `await`-ed within an `async` function. In a script, you'll need to use `asyncio.run()` to execute the async call.
fix
Always use `await safehttpx.get(...)` within an `async` function. For top-level calls in scripts, wrap the async function call with `asyncio.run(your_async_function())`.
affects: All versions
gotchasafehttpx's DNS validation relies on Google DNS by default. While generally robust, highly restrictive network environments or specific corporate policies might require custom DNS resolvers. This could lead to legitimate external domains being blocked if Google DNS is unreachable or if local DNS records differ.
fix
Ensure that Google DNS (8.8.8.8, 8.8.4.4) is reachable from your application's environment. If not, investigate network configuration or consider contributing to the library for custom DNS resolver options if needed.
affects: All versions
gotchasafehttpx primarily focuses on securing `GET` requests. While it wraps `httpx`, users who directly instantiate `httpx.AsyncClient` or use other HTTP methods (like `POST`, `PUT`) without explicit safehttpx wrappers might inadvertently bypass the SSRF protections for those requests. The `safehttpx` module currently only exposes a `get` method.
fix
Ensure all network requests that could be susceptible to SSRF are routed through `safehttpx.get()`. Do not assume other HTTP methods or direct `httpx` client usage inherit `safehttpx`'s protections.
affects: All versions
breakingAs `safehttpx` is a wrapper around `httpx`, it is implicitly affected by breaking changes in `httpx` (e.g., changes in redirect handling or SSL configuration). While `safehttpx` may pin `httpx` versions, custom environment setups could lead to mismatches.
fix
Refer to the `httpx` changelog (e.g., v0.20.0 for redirect changes, v0.28.0 for SSL config changes) if unexpected behavior occurs. Ensure your `httpx` version is compatible with the `safehttpx` version you are using.
affects: Potentially all versions, depending on `httpx` version pinning and user environment.
Upgrade
Version history
0.1.7latest on PyPI · released Oct 24, 2025
Audit
Dependencies
httpxrequiredCore dependency, safehttpx wraps httpx.AsyncClient.
Agent activity
18 hits · last 30 days
node
16
OpenAI (training)
1
Resources
safehttpx — pip install safehttpx · libregistry