Skip to main content

A Python package for replicating Gradio applications

Project description

safehttpx

A small utility Python 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 httpx.AsyncClient.get() while performing DNS validation on 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

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:

The example snippets above will work in environments like IPython or Jupyter notebooks where an asyncio event loop is already running. For regular Python scripts, you'll need to explicitly create and run an asyncio event loop. Here's how you can structure your code to use safehttpx in a standard Python script:

import asyncio
import safehttpx as sh

async def main():
    response = await sh.get("https://huggingface.co")
    print(response)

if __name__ == "__main__":
    asyncio.run(main())

Whitelisted Domains

You may want to whitelist certain domains from being validated. For example, if you are running code on a server that implements DNS splitting, then even public URLs may appear as internal URLs. You can whitelist domains like this:

import safehttpx as sh

PUBLIC_HOSTNAME_WHITELIST = ["hf.co", "huggingface.co"]

await sh.get("https://huggingface.co", domain_whitelist=PUBLIC_HOSTNAME_WHITELIST)
>>> <Response [200 OK]>

Custom Transports (Advanced)

If you know what you are doing, and what to pass in a custom instance of httpx.AsyncBaseTransport, you can use the _transport parameter in sh.get(). Setting this to False explicitly will use no secure transport (effectively making sh.get equivalent to httpx.AsyncClient.get()).

More Information

This library was created as a result of Trail of Bits' security audit of Gradio 5 (Hugging Face), and is used in the Gradio library to make secure requests to custom, user-specified URLs. We are releasing this as a standalone library so that other developers can benefit from our learnings.

If you find a security issue in this library, please email the Gradio team at gradio-team@huggingface.co. Thanks!

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

safehttpx-0.1.0.tar.gz (9.1 kB view details)

Uploaded Source

Built Distribution

safehttpx-0.1.0-py3-none-any.whl (8.4 kB view details)

Uploaded Python 3

File details

Details for the file safehttpx-0.1.0.tar.gz.

File metadata

  • Download URL: safehttpx-0.1.0.tar.gz
  • Upload date:
  • Size: 9.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.12.2

File hashes

Hashes for safehttpx-0.1.0.tar.gz
Algorithm Hash digest
SHA256 488f47c41d3c100c15573afc9555bb5ae3e40eadd927e073952fb3b294fdade3
MD5 c282e1adb4cf51869e71707432afdfa0
BLAKE2b-256 66b2ba8a1e4de6639d1d0aa359fc95840aaf861756a1e09d9687518e4f9f4dbb

See more details on using hashes here.

File details

Details for the file safehttpx-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: safehttpx-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 8.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.12.2

File hashes

Hashes for safehttpx-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 eeceb62a05999e00bb5f2043c8ba6f7ec96abb0673f4573ce115a30e12f726fc
MD5 6460b8e804a59019264ee0a2a2c3ece5
BLAKE2b-256 55c7f0cfa621d5a736c7a2b410d3819a260d8d8ac334dcbe3714db3d37155b4e

See more details on using hashes here.

Supported by

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