A small Python library created to help developers protect their applications from Server Side Request Forgery (SSRF) attacks.
Maintained. Last release 2 months ago, commits in 1 of the last 26 weeks. Weaker on commit activity and release cadence.
pip install safehttpxsafehttpx
A small Python library created to help developers protect their applications from Server Side Request Forgery (SSRF) attacks. It implements an asynchronous GET method called safehttpx.get(), which is a wrapper around httpx2.AsyncClient.get() (HTTPX2, the maintained fork of httpx) while performing DNS validation on the supplied URL using Google DNS.
It also implements mitigation for DNS rebinding attacks.
Why?
Server Side Request Forgery (SSRF) attacks can be particularly dangerous as they allow attackers to make arbitrary HTTP requests from your server, potentially accessing sensitive internal services that are normally unreachable from the internet. This could enable attackers to scan internal networks, access metadata services in cloud environments (like "AWS Instance Metadata Service"), or hit internal APIs - all while appearing to come from your trusted server. By validating URLs against public DNS servers and implementing protections against DNS rebinding, safehttpx helps prevent attackers from coercing your application into making requests to internal or otherwise restricted network resources.
Usage
Installation
$ pip install safehttpx
Also avalaible through Conda
$ conda install safehttpx -c conda-forge
or
$ mamba install safehttpx -c conda-forge
[!NOTE] Starting with v0.2.0,
safehttpxis built onhttpx2: it depends onhttpx2,safehttpx.get()returns anhttpx2.Response, and custom transports must behttpx2transports. If you need to usehttpxinstead, pinsafehttpxto v0.1.7 or earlier:$ pip install "safehttpx<=0.1.7"
Basic Usage
import safehttpx as sh
await sh.get("https://huggingface.co")
>>> <Response [200 OK]>
await sh.get("http://127.0.0.1")
>>> ValueError: Hostname 127.0.0.1 failed validation
Note on Async Usage: