Skip to main content

Runtime network egress control for Python

Project description

tethered

Runtime network egress control for Python

One function call. Zero dependencies. No infrastructure changes.

PyPI Python License: MIT
coverage CI CodeQL security: bandit
Ruff uv Checked with pyright

tethered is a lightweight, in-process policy check that hooks into Python's own socket layer to enforce your allow list before any packet leaves the machine. Everything runs locally within your process — works with requests, httpx, aiohttp, Django, Flask, FastAPI, and any library built on Python sockets.

import tethered

tethered.activate(allow=["*.stripe.com:443", "db.internal:5432"])

import urllib.request
urllib.request.urlopen("https://api.stripe.com/v1/charges")  # works — matches *.stripe.com:443
urllib.request.urlopen("https://evil.com/exfil")             # raises tethered.EgressBlocked

Why tethered?

Python has no built-in way to restrict outbound network access at runtime. Infrastructure-level controls (firewalls, network policies, proxies) require platform teams, separate services, or admin privileges. None of them give you a single line of Python that says "this process may only talk to these hosts."

tethered fills this gap at the application layer. No proxies, no sidecars, no admin privileges — just a function call. It's complementary to infrastructure controls, not a replacement.

Use cases

  • Supply chain defense. A compromised dependency can't phone home if egress is locked to your known services.
  • AI agent guardrails. Constrain LLM-powered agents to only the APIs they need.
  • Test isolation. Ensure your test suite never accidentally hits production endpoints.
  • Least-privilege networking. Declare your app's network surface the same way you declare its dependencies.

Install

uv add tethered

Or with pip:

pip install tethered

Requires Python 3.10+. Zero runtime dependencies.

Getting started

Call activate() as early as possible — before any library makes network connections:

# manage.py, wsgi.py, main.py, or your entrypoint
import tethered
tethered.activate(allow=["*.stripe.com:443", "db.internal:5432"])

# Then import and run your app
from myapp import create_app
app = create_app()

This pattern works the same for Django, Flask, FastAPI, scripts, and AI agents — activate tethered before your application and its dependencies start making connections.

Existing connections (e.g., connection pools) established before activate() will continue to work — tethered intercepts at connect time, not at read/write time.

Allow list syntax

Pattern Example Matches
Exact hostname "api.stripe.com" api.stripe.com only
Wildcard subdomain "*.stripe.com" api.stripe.com, dashboard.stripe.com (not stripe.com)
Hostname + port "api.stripe.com:443" api.stripe.com on port 443 only
IPv4 address "198.51.100.1" That IP only
IPv4 CIDR range "10.0.0.0/8" Any IP in 10.x.x.x
CIDR + port "10.0.0.0/8:5432" Any IP in 10.x.x.x on port 5432
IPv6 address "2001:db8::1" or "[2001:db8::1]" That IPv6 address
IPv6 + port "[2001:db8::1]:443" That IPv6 address on port 443 only
IPv6 CIDR "[2001:db8::]/32" Any IP in that IPv6 prefix

Wildcard matching: Uses Python's fnmatch syntax. * matches any characters including dots, so *.stripe.com matches both api.stripe.com and a.b.stripe.com. This differs from TLS certificate wildcards. The characters ? (single character) and [seq] (character set) are also supported.

Localhost (127.0.0.0/8, ::1) is always allowed by default. The addresses 0.0.0.0 and :: (INADDR_ANY) are also treated as localhost.

Malformed hostnames containing whitespace, control characters, or invisible Unicode are rejected and never matched by wildcard rules.

API

tethered.activate()

tethered.activate(
    *,
    allow: list[str],
    log_only: bool = False,
    fail_closed: bool = False,
    allow_localhost: bool = True,
    on_blocked: Callable[[str, int | None], None] | None = None,
    locked: bool = False,
    lock_token: object | None = None,
)
Parameter Description
allow Required. Allowed destinations — see Allow list syntax. Pass [] to block all non-localhost connections.
log_only Log blocked connections instead of raising EgressBlocked. Default False.
fail_closed Block when the policy check itself errors, instead of failing open. Default False.
allow_localhost Allow loopback addresses (127.0.0.0/8, ::1). Default True.
on_blocked Callback (host, port) -> None invoked on every blocked connection, including in log-only mode.
locked Prevent deactivate() and activate() without the correct lock_token. Default False.
lock_token Opaque token required when locked=True. Also required to replace an existing locked policy. Compared by identity (is), not equality.

Can be called multiple times to replace the active policy — calling activate() again does not require deactivate() first. If the current policy is locked, the correct lock_token must be provided. Each call creates a completely new policy; no parameters or state carry over from previous calls.

Log-only mode

Monitor without blocking — useful for rollout or auditing:

tethered.activate(
    allow=["*.stripe.com"],
    log_only=True,
    on_blocked=lambda host, port: print(f"would block: {host}:{port}"),
)

tethered logs to the "tethered" logger via stdlib logging. To see log-only warnings, ensure your application has logging configured (e.g., logging.basicConfig()).

Locked mode

Raise the bar against accidental or casual disabling of enforcement:

secret = object()
tethered.activate(allow=["*.stripe.com:443"], locked=True, lock_token=secret)

# Both deactivate() and activate() require the correct token
tethered.deactivate(lock_token=secret)

Calling activate(locked=True) without a lock_token raises ValueError. Calling activate() or deactivate() when a locked policy is active without the correct token raises TetheredLocked.

tethered.deactivate()

tethered.deactivate(*, lock_token: object | None = None)

Disable enforcement. All connections are allowed again. Internal state (IP-to-hostname mappings, callback references) is fully cleared — a subsequent activate() starts fresh.

If activated with locked=True, the matching lock_token must be provided or TetheredLocked is raised.

tethered.EgressBlocked

Raised when a connection is blocked. Subclass of RuntimeError.

try:
    urllib.request.urlopen("https://evil.com")
except tethered.EgressBlocked as e:
    print(e.host)           # "evil.com"
    print(e.port)           # 443
    print(e.resolved_from)  # original hostname if connecting by resolved IP

tethered.TetheredLocked

Raised when deactivate() or activate() is called on a locked policy without the correct token. Subclass of RuntimeError.

How it works

tethered uses sys.addaudithook (PEP 578) to intercept socket operations at the interpreter level:

  • socket.getaddrinfo — blocks DNS resolution for disallowed hostnames and records IP-to-hostname mappings for allowed hosts.
  • socket.gethostbyname / socket.gethostbyaddr — intercept alternative DNS resolution paths, including reverse-DNS lookups of raw IPs.
  • socket.connect (including connect_ex, which raises the socket.connect audit event in CPython) — enforces the allow list on TCP connections.
  • socket.sendto / socket.sendmsg — enforces the allow list on UDP datagrams.

When getaddrinfo resolves a hostname, tethered records the IP-to-hostname mapping in a bounded LRU cache. When a subsequent connect() targets that IP, tethered looks up the original hostname and checks it against the allow list. If denied, EgressBlocked is raised before any packet leaves the machine.

This works with libraries built on CPython sockets (requests, httpx, urllib3, aiohttp) and frameworks like Django, Flask, and FastAPI — they all call socket.getaddrinfo and socket.connect under the hood. Asyncio and async libraries using CPython sockets are supported: audit hooks fire at the C socket level, so asyncio, aiohttp, and httpx async use the same enforcement path as synchronous code.

The per-connection overhead is a Python function call with hostname normalization, a dictionary lookup, and pattern matching — designed to add minimal overhead relative to actual network I/O.

Security model

tethered is a defense-in-depth guardrail, not a security sandbox. It intercepts Python-level socket operations. Code that uses ctypes, cffi, subprocesses, or C extensions with direct syscalls can bypass it. For full process isolation, combine tethered with OS-level controls (containers, seccomp, network namespaces).

What tethered protects against

Trusted-but-buggy code and supply chain threats: dependencies that use Python's standard socket module (directly or through libraries like requests, urllib3, httpx, aiohttp). tethered prevents these from connecting to destinations not in your allow list.

What tethered does NOT protect against

  • ctypes / cffi / direct syscalls. Native code can call libc's connect() directly, bypassing the audit hook.
  • Subprocesses. subprocess.Popen, os.system, and os.exec* create new processes without the audit hook.
  • C extensions with raw socket calls. Extensions calling C-level socket functions are not intercepted.
  • In-process disabling. Code in the same interpreter can call deactivate() or activate() unless locked=True is used. Even locked mode can be bypassed by code that modifies module state — Python has no true encapsulation.

Design trade-offs

  • Fail-open by default. If tethered's matching logic raises an unexpected exception, the connection is allowed and a warning is logged. A bug in tethered should not break your application. Use fail_closed=True for stricter environments.
  • Audit hooks are irremovable. sys.addaudithook has no remove function (by design — PEP 578). deactivate() makes the hook a no-op but cannot unregister it. This is per-process only — no persistent state, no system changes, everything is gone when the process exits.
  • IP-to-hostname mapping is bounded. The LRU cache holds up to 4096 entries. In long-running processes with many unique DNS lookups, older mappings are evicted. A connection to an evicted IP is checked against IP/CIDR rules only.
  • Direct IP connections skip hostname matching. Connecting to a raw IP without prior DNS resolution means only IP/CIDR rules apply — hostname wildcards won't match. On shared-IP infrastructure (CDNs, cloud hosting), multiple hostnames may resolve to the same IP. If an allowed hostname shares an IP with a disallowed one, a raw-IP connection to that address will pass hostname policy via the cached mapping. This is inherent to any system that cannot bind a socket to a specific hostname identity.
  • Localhost allows local relays. With the default allow_localhost=True, any proxy, tunnel, or forwarding agent listening on 127.0.0.1 or ::1 can relay traffic to external destinations, bypassing the intent of the egress policy. In high-security environments where local relays are a concern, set allow_localhost=False and explicitly allow only the loopback addresses and ports your application needs.

Recommendations

For defense-in-depth, combine tethered with:

  • OS-level sandboxing (containers, seccomp-bpf, network namespaces) for hard isolation.
  • Subprocess restrictions (audit hooks on subprocess.Popen events, or seccomp filters).
  • Import restrictions to prevent ctypes/cffi loading in untrusted code paths.

Handling blocked connections

EgressBlocked is a RuntimeError, not an OSError. This is intentional — a policy violation is not a network error and should not be silently caught by HTTP libraries or retry logic. You'll want to handle it explicitly at your application boundaries.

Django / FastAPI middleware

# middleware.py
import tethered

class EgressBlockedMiddleware:
    def __init__(self, get_response):
        self.get_response = get_response

    def __call__(self, request):
        try:
            return self.get_response(request)
        except tethered.EgressBlocked as e:
            logger.error("Egress blocked: %s:%s (resolved_from=%s)", e.host, e.port, e.resolved_from)
            return HttpResponse("Service unavailable", status=503)

Celery tasks

# EgressBlocked is a RuntimeError, so autoretry_for=(ConnectionError, TimeoutError)
# already won't retry it — the task fails immediately on a policy violation.
@app.task(autoretry_for=(ConnectionError, TimeoutError))
def sync_data():
    requests.post("https://api.stripe.com/v1/charges", ...)

Retry decorators

# Catch EgressBlocked before your retry logic — retrying a policy block is pointless
try:
    response = retry_with_backoff(make_request)
except tethered.EgressBlocked:
    raise  # don't retry policy violations
except ConnectionError:
    handle_network_failure()

Badge

Using tethered in your project? Add the badge to your README:

[![egress: tethered](https://img.shields.io/badge/egress-tethered-orange?labelColor=4B8BBE)](https://github.com/shcherbak-ai/tethered)

egress: tethered

Contributing

See CONTRIBUTING.md for development setup and guidelines.

Security

See SECURITY.md for reporting vulnerabilities.

License

MIT

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

tethered-0.2.0.tar.gz (641.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tethered-0.2.0-py3-none-any.whl (17.0 kB view details)

Uploaded Python 3

File details

Details for the file tethered-0.2.0.tar.gz.

File metadata

  • Download URL: tethered-0.2.0.tar.gz
  • Upload date:
  • Size: 641.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for tethered-0.2.0.tar.gz
Algorithm Hash digest
SHA256 fa8db3192bed175ef6b57d100965058f0cb99bbe06090e7d01643e43903bef64
MD5 660a3c44d3adb33fddba970b77de4fec
BLAKE2b-256 43783bf9d99766313fee1f39367ce6f85977c4c0031550ad79057562ba59e8a5

See more details on using hashes here.

Provenance

The following attestation bundles were made for tethered-0.2.0.tar.gz:

Publisher: publish.yml on shcherbak-ai/tethered

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file tethered-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: tethered-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 17.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for tethered-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4d524983957ed6a1dda2816c8b56890a7fd8e8763218e37c248caf4137fa730f
MD5 0794772fe2125c6a484503c1414b444a
BLAKE2b-256 b83551220da44e79ca8af9b5ae0418e1b57c4eff45bddc981c07227e6a7aed5b

See more details on using hashes here.

Provenance

The following attestation bundles were made for tethered-0.2.0-py3-none-any.whl:

Publisher: publish.yml on shcherbak-ai/tethered

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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