Modal logo

OutboundRoute

class OutboundRoute(object)

A route in an OutboundPolicy: which outbound traffic it matches.

Create routes with the OutboundRoute.tls, OutboundRoute.https, OutboundRoute.ip and OutboundRoute.any factories. Traffic a route matches goes to its original destination, except that the any route can block what it matches. Routes are immutable: block() returns a new route.

Usage

import modal

# Allow TLS connections to a domain and its subdomains.
modal.OutboundRoute.tls(domains=["example.com", "*.example.com"])

# Block everything no earlier route matched.
modal.OutboundRoute.any().block()

tls

tls(cls, *, domains)

Match connections that carry TLS from the first byte to one of domains, on tcp/443.

Connections are matched by the domain the client requests during the TLS handshake (SNI). TLS routes never decrypt traffic: the connection's contents are passed through as-is. Traffic to these domains on other ports or transports isn't matched by this route.

Parameters

domains Sequence[str]
Domains to match. A *. prefix matches subdomains at any depth but not the domain itself: *.example.com matches a.example.com and a.b.example.com, not example.com. A bare "*" matches all domains.

https

https(cls, *, domains, header_replacements=None)

Match HTTPS connections to one of domains, on tcp/443.

HTTPS routes always decrypt traffic, which requires the policy to be created with allow_tls_decryption=True, even when the route has no header replacements. Requests whose Host header doesn't match the domain requested for the connection are refused. Traffic to these domains on other ports or transports isn't matched by this route.

Header values support templating with keys in a replacement's secret: a $-prefixed key name in the secret is replaced with the secret value. Literal $ characters are written $$. Secret values never enter the Sandbox: they are injected into matching requests outside the container.

Parameters

domains Sequence[str]
Domains to match. A *. prefix matches subdomains at any depth but not the domain itself: *.example.com matches a.example.com and a.b.example.com, not example.com. A bare "*" matches all domains.
header_replacements Sequence[OutboundHeaderReplacement] | None
Headers to set on matching requests, in public beta. Each entry is a dict with a headers key (header name -> value template) and an optional secret key: a named Secret (e.g. from Secret.from_name) whose keys the header values may reference. Each header is set on every matching request, so the request has exactly one header with that name and the given value. Copies the request already has are removed, whatever their case, and the header is added if the request has none. A route can set each header name only once, ignoring case.

Usage

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

route = modal.OutboundRoute.https(
    domains=["api.example.com"],
    header_replacements=[
        {"secret": secret, "headers": {"Authorization": "Bearer $API_TOKEN"}},
    ],
)

ip

ip(cls, *, cidrs)

Match all traffic to one of cidrs, on any transport and port.

Parameters

cidrs Sequence[str]
CIDR ranges to match, e.g. "203.0.113.0/24".

any

any(cls)

Match all traffic that no earlier route in the policy matched.

Every OutboundPolicy needs exactly one any route, as its last route. Without a redirect, the traffic it matches goes to its original destination; call .block() on the route to block it instead.

block

block(self)

Return a new route that blocks the traffic it matches.

Only OutboundRoute.any() supports block().