Skip to main content

outbound-guard

CI Codecov PyPI version Docs

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:port or [IPv6]:port, host names are case-insensitive. Anything else, like URLs, CIDR ranges or wildcards, is rejected: parse_destination(entry) raises ValueError, so you can use it to validate user input
  • a connection to host:port that resolves to an internal address is allowed if host or address is in the allowlist, either with that port or without a port
  • a host name entry trusts DNS for that name: api.internal allows whatever api.internal resolves 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 (C) 2026 MrNaif2018

Licensed under the MIT license

Metadata

Release files for outbound-guard 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for outbound-guard 0.1.0
File Size Uploaded Provenance Actions
outbound_guard-0.1.0.tar.gz 17.2 kB With provenance Details for outbound_guard-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for outbound-guard 0.1.0
File Interpreter ABI Platform Actions
outbound_guard-0.1.0-py3-none-any.whl Python 3 none any Details for outbound_guard-0.1.0-py3-none-any.whl

Total release size: 28.5 kB

Release files / outbound_guard-0.1.0.tar.gz

Download URL outbound_guard-0.1.0.tar.gz
Size 17.2 kB
Tags Source
SHA-256 checksum
How to use checksums
d0b37e05db0adbc342856ceafc8ef13ab00f2c2584af369b05d0816631053b6a
BLAKE2b-256 checksum
How to use checksums
c5d23a831474e7a31dc063522e97c3f17355ecd2dc851b5735d5aedf2bfee55f
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 27, 2026.

Transparency log

Release files / outbound_guard-0.1.0-py3-none-any.whl

Download URL outbound_guard-0.1.0-py3-none-any.whl
Size 11.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
69c3e180bd92dc4770fc6d975613b6a351f84d75ed1699a69c13c6304f878a2e
BLAKE2b-256 checksum
How to use checksums
7db05e181229380960a657ba0c289a79d6f042233d9fa8b7953e8a0561d32d6e
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 27, 2026.

Transparency log

Security for outbound-guard 0.1.0

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.

Select from 2 files

Attestation for outbound_guard-0.1.0.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 27, 2026.

Transparency log

A 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.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page