Modal logo

OutboundPolicy

class OutboundPolicy(object)

Routes that control where outbound network traffic from a Sandbox goes.

A policy is an ordered list of OutboundRoutes. Each outbound connection is handled by the first route that matches it, and goes to its original destination. The last route must be OutboundRoute.any(), which matches everything no earlier route matched and either lets it through or, with .block(), blocks it.

Usage

import modal

secret = modal.Secret.from_name("api-token")

policy = modal.OutboundPolicy(
    allow_tls_decryption=True,
    routes=[
        # Inject a secret-backed header into HTTPS requests.
        modal.OutboundRoute.https(
            domains=["api.example.com"],
            header_replacements=[{"secret": secret, "headers": {"Authorization": "Bearer $API_TOKEN"}}],
        ),
        # Allow TLS connections to these domains.
        modal.OutboundRoute.tls(domains=["github.com", "*.github.com", "pypi.org"]),
        # Allow all traffic to a CIDR range.
        modal.OutboundRoute.ip(cidrs=["203.0.113.0/24"]),
        # Block everything else.
        modal.OutboundRoute.any().block(),
    ],
)

sb = modal.Sandbox.create(app=app, outbound_policy=policy)
__init__(self, *, allow_tls_decryption=False, routes)

Create an outbound policy.

Parameters

allow_tls_decryption bool
Allow Modal to decrypt the Sandbox's TLS traffic on routes that need it, by generating a certificate authority for the Sandbox and attempting to make the Sandbox trust it. Decrypts nothing on its own. Required by HTTPS routes. Fixed when the Sandbox is created. Sandboxes that allow TLS decryption don't support memory snapshots. (Default is False)
routes Sequence[_OutboundRoute]
Ordered routes; the first route that matches a connection handles it. Must end with OutboundRoute.any().