outbound-guard
Keep outbound connections to user-supplied destinations away from internal networks (SSRF protection)
Overview
Apps that send webhooks, fetch user-supplied URLs or connect to user-configured SMTP and SSH servers can be made to reach their own internal network: databases, admin panels, cloud metadata endpoints. With outbound-guard, only public addresses and the internal destinations you allow are reachable:
- DNS tricks and unusual ways of writing IP addresses don't get around it
- with aiohttp, redirects are checked too, proxies are refused, responses are size limited and requests time out by default
- the allowlist of internal destinations can be a fixed list or loaded from an async source like a database
The core has no dependencies. Integrations for aiohttp, SMTP (aiosmtplib) and SSH (paramiko) are optional extras.
For API reference see the docs
Installation
pip install "outbound-guard[aiohttp,smtp,ssh]"
Pick only the extras you need: aiohttp, smtp, ssh. Python 3.12.4 or newer is required.
Usage
Create one guard and share it:
from outbound_guard import OutboundGuard
guard = OutboundGuard() # public addresses only
guard = OutboundGuard(["api.internal:8080", "192.168.1.5"], denial_hint=". Allow it in the outbound settings")
aiohttp
import aiohttp
from outbound_guard.aiohttp import session
async with session(guard) as client, client.post(webhook_url, json=payload) as response:
data = await response.json()
session() returns an aiohttp.ClientSession with a 15 second total timeout and a 16 MiB response size limit
(timeout= and max_response_size= change them). Other arguments are passed to ClientSession. A denied connection
raises aiohttp.ClientOSError with errno.EACCES. A response over the limit raises aiohttp.ClientPayloadError from
read(), text() and json(); iterating response.content directly is not limited.
GuardedConnector(guard, **kwargs) and limited_response_class(max_size) are available for building sessions yourself.
SMTP
from outbound_guard.smtp import connect_smtp
async with await connect_smtp(guard, "smtp.example.com", 465, use_tls=True, username=user, password=password) as smtp:
await smtp.send_message(message)
connect_smtp() returns a connected aiosmtplib.SMTP. Other arguments are passed to aiosmtplib.SMTP.
SSH
import paramiko
from outbound_guard.ssh import connect_ssh
client = paramiko.SSHClient()
client.load_system_host_keys()
await connect_ssh(guard, client, "server.example.com", username=user, password=password)
Other arguments are passed to SSHClient.connect.
Other protocols
sock = await guard.connect(host, port, timeout=10) # a connected blocking socket.socket
addr_infos = await guard.resolve(host, port) # getaddrinfo results with approved addresses only
Both raise OutboundDenied (an OSError with errno.EACCES) if no resolved address is approved.
Allowlist
Public addresses are always allowed. Anything else is allowed only if it matches an allowlist entry:
- entries are
host,host:port,IP,IP:portor[IPv6]:port, host names are case-insensitive. Anything else, like URLs, CIDR ranges or wildcards, is rejected:parse_destination(entry)raisesValueError, so you can use it to validate user input - a connection to
host:portthat resolves to an internaladdressis allowed ifhostoraddressis in the allowlist, either with that port or without a port - a host name entry trusts DNS for that name:
api.internalallows whateverapi.internalresolves to - allowed destinations stay reachable through redirects
The allowlist is either a collection of entries, validated when the guard is created, or an async function returning one:
async def allowed_hosts() -> list[str]:
return (await settings.load()).allowed_hosts
guard = OutboundGuard(allowed_hosts)
Changes apply to the next connection. If the function raises or returns an invalid entry, no internal destination is allowed and the error is logged.
Logging
Denials are logged as warnings to the outbound_guard logger with the host, port, address and reason, followed by
denial_hint. The hint is not included in exceptions, so they are safe to show to the users who supplied the
destination. Credentials from URLs are not logged.
Limitations
- only connections made through the guard are checked. Other clients, libraries and subprocesses aren't, so a network level firewall is still a good idea as defense in depth
- the server's own public IP addresses are public: block them at the firewall if services listen on them
Copyright and License
Copyright (C) 2026 MrNaif2018
Licensed under the MIT license
Metadata
Release files for outbound-guard 0.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| outbound_guard-0.1.1.tar.gz | 18.1 kB | With provenance | Details for outbound_guard-0.1.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| outbound_guard-0.1.1-py3-none-any.whl | Python 3 | none | any | Details for outbound_guard-0.1.1-py3-none-any.whl |
Total release size: 29.7 kB
Release files / outbound_guard-0.1.1.tar.gz
| Download URL | outbound_guard-0.1.1.tar.gz |
|---|---|
| Size | 18.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
3ea83f7647819f88e4058a0b787553982d12dc944e0b57999fe8476d915aaada
|
|
BLAKE2b-256 checksum How to use checksums |
f3ae20c0424e12a1f9ef978ac2d553ae3b0d7b8271b3a60f95d1c7f82acf6185
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 28, 2026.
Transparency logRelease files / outbound_guard-0.1.1-py3-none-any.whl
| Download URL | outbound_guard-0.1.1-py3-none-any.whl |
|---|---|
| Size | 11.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
960e31af700600b4d86e2e3a3d18486e92e3b61e0a08294afb1b43067bdeebe7
|
|
BLAKE2b-256 checksum How to use checksums |
933addafd4f5fed80f3d8f29f131025bfbd0fc544d80e4abc2143062599028fd
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 28, 2026.
Transparency logSecurity for outbound-guard 0.1.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for outbound_guard-0.1.1.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 28, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.