Skip to main content

A wrapper around the requests library for safely making HTTP requests on behalf of a third party

Project description


Advocate is a set of tools based around the requests library for safely making HTTP requests on behalf of a third party. Specifically, it aims to prevent common techniques that enable SSRF attacks.

Advocate was inspired by fin1te’s SafeCurl project.


pip install advocate

Advocate is officially supported on CPython 2.7+, CPython 3.4+ and PyPy 2. PyPy 3 may work as well, but you’ll need a copy of the ipaddress module from elsewhere. Python 3 users may also need to install netifaces from their HG repository, as the version currently on PyPi won’t install.


Advocate is more-or-less a drop-in replacement for requests. In most cases you can just replace “requests” with “advocate” where necessary and be good to go:

>>> import advocate
>>> print advocate.get("")
<Response [200]>

Advocate also provides a subclassed requests.Session with sane defaults for blacklisting already set up:

>>> import advocate
>>> sess = advocate.Session()
>>> print sess.get("")
<Response [200]>
>>> print sess.get("http://localhost/")
advocate.exceptions.UnacceptableAddressException: ('localhost', 80)

If you have more nuanced rules, but still want a drop-in replacement for requests, there’s RequestsAPIWrapper :

>>> from advocate import Blacklist, RequestsAPIWrapper
>>> from advocate.packages import ipaddress
>>> dougs_advocate = RequestsAPIWrapper(Blacklist(ip_blacklist={
...     # Contains data incomprehensible to mere mortals
...     ipaddress.ip_network("")
... }))
>>> print dougs_advocate.get("")
advocate.exceptions.UnacceptableAddressException: ('', 80)

Other than that, you can do just about everything with Advocate that you can with an unwrapped requests. Advocate passes requests’ test suite with the exception of tests that require Session.mount().

This seems like it’s been done before

There’ve been a few similar projects, but in my opinion Advocate’s approach is the best because:

It sees URLs the same as the underlying HTTP library

Parsing URLs is hard, and no two URL parsers seem to behave exactly the same. The tiniest differences in parsing between your validator and the underlying HTTP library can lead to vulnerabilities. For example, differences between PHP’s parse_url and cURL’s URL parser allowed a blacklist bypass in SafeCurl.

Advocate doesn’t do URL parsing at all, and lets requests handle it. Advocate only looks at the address requests actually tries to open a socket to.

It deals with DNS rebinding

Two consecutive calls to socket.getaddrinfo aren’t guaranteed to return the same info, depending on the system configuration. If the “safe” looking record TTLs between the verification lookup and the lookup for actually opening the socket, we may end up connecting to a very different server than the one we OK’d!

Advocate gets around this by only using one getaddrinfo call for both verification and connecting the socket. In pseudocode:

def connect_socket(host, port):
    for res in socket.getaddrinfo(host, port):
        # where `res` will be a tuple containing the IP for the host
        if not is_blacklisted(res):
            # ... connect the socket using `res`

See Wikipedia’s article on DNS rebinding attacks for more info.

It handles redirects sanely

Most of the other SSRF-prevention libs cover this, but I’ve seen a lot of sample code online that doesn’t. Advocate will catch it since it inspects every connection attempt the underlying HTTP lib makes.


Proper IPv6 Support?

Advocate’s IPv6 support is still a work-in-progress, since I’m not that familiar with the spec, and there are so many ways to tunnel IPv4 over IPv6, as well as other things we’d rather avoid. IPv6 records are ignored by default for now, but you can enable them with allow_ipv6=True.

It should mostly work as expected, but Advocate’s approach might not even make sense with most IPv6 deployments, see Issue #3 for more info.

If you can think of any improvements to the IPv6 handling, please submit an issue or PR!


  • This is beta-quality software, the API might change without warning!
  • mount() ing other adapters is disallowed to prevent Advocate’s blacklisting adapters from being clobbered.
  • Advocate does not (yet) support the use of HTTP proxies.
  • Proper IPv6 support is still a WIP as noted above.


Project details

Download files

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

Files for advocate, version 0.3.2
Filename, size File type Python version Upload date Hashes
Filename, size advocate-0.3.2.tar.gz (29.2 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page