safehttpx

0.1.7 · active · verified Thu Apr 09

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.

Warnings

Install

Imports

Quickstart

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())

view raw JSON →