Skip to main content
See Networking for network policies, DNS, TLS, and port publishing.

Network

Used by Sandbox.create(network=…)

Sandbox network configuration.

network.tcp_accept_queue_size

int \| None · Default: None (1,024) How many not-yet-accepted connections each published TCP port’s host listener queues, from 1 to 2,147,483,647. Connections arriving while the queue is full never reach the sandbox, so raise this when a burst of parallel connections, such as a reverse proxy fanning out one page load, exceeds it. The host kernel caps the effective depth at its own somaxconn (4,096 by default on Linux, 128 on macOS). Sandbox.restore(..., tcp_accept_queue_size=n) applies it to the listeners a restored child publishes.

network.nat64_prefixes

tuple[str, ...] · Default: ("64:ff9b::/96",) NAT64 /96 prefixes for policy classification. Destinations inside these prefixes are also evaluated by their embedded IPv4 address. Pass this field by keyword. Custom or empty prefix lists are supported only for local sandboxes; cloud creation rejects them.

max_udp_connections

Network(max_udp_connections=512) sets the runtime UDP relay session limit. Omission is unlimited in single-tenant mode and defaults to 1,024 in multi-tenant mode; zero explicitly selects unlimited. At a finite limit, a new session evicts the least recently active session. Sessions expire after 60 seconds of inactivity.

network.max_tcp_connections

max_connections remains a deprecated TCP-only alias. Specifying both names is an error. Either name can be combined with the UDP limit. int \| None · Default: None Maximum concurrent TCP connections. Zero selects unlimited. The same limits can be selected when restoring, without changing the captured guest interface:
Omitted restore limits use destination defaults. restore_with_progress() accepts the same options; max_connections remains a deprecated TCP-only alias, and supplying both TCP names is an error.

network.http

HttpConfig | None · Default: None Denial responses are disabled by default. Set HttpConfig(deny_response=True) to enable readable 403 responses. Optionally set deny_message to customize the body; {host} names the blocked hostname. Setting a message alone does not enable responses. When enabled, None uses the built-in message and an empty string sends no body. Requires a supporting local runtime; cloud rejects enabling it.

Network.none()

Deny all traffic in both directions. The network interface remains present; exec and fs still work since they use the host-guest channel, not the network.

Returns

Network configuration with deny defaults in both directions.

Network.from_profiles()

Build a deny-by-default network configuration from PUBLIC, PRIVATE, and HOST profiles. Duplicate profiles are ignored, generated rules use canonical order, and gateway DNS is added automatically for every non-empty profile set.

Returns

Network configuration containing the composed profiles.

Network.allow_all()

Unrestricted network access, including to private addresses and the host machine.

Returns

Unrestricted network configuration.

Rule

Used by NetworkPolicy(rules=…)

Frozen dataclass for a single network policy rule. Prefer the Rule.allow() / Rule.deny() class methods over the positional constructor.
Ingress rules carrying ICMP protocols are rejected at sandbox creation; the host has no inbound ICMP path. Use Direction.EGRESS for ICMP allow/deny. A NetworkPolicy is an ordered list of Rule values plus two per-direction defaults, evaluated first-match-wins per direction. The class methods below build rules; assemble them into NetworkPolicy(rules=(...)) and pass it as Network(policy=...).

Rule order matters

The first matching rule wins, so a broad rule placed before a narrow one swallows it:
Put specific rules before general ones.

Rule.allow()

Create a rule that permits matching traffic. All filters are keyword-only.

Parameters

directionDirection
Which evaluator considers the rule. Defaults to EGRESS.
Protocol filter.
portint | str | None
Single port (443) or range (“8000-9000”).
Target filter. Prefer the typed Destination helpers; string shorthand is also accepted.

Returns

An allow rule.

Rule.deny()

Create a rule that blocks matching traffic. Same keyword-only filters as allow().

Parameters

directionDirection
Which evaluator considers the rule. Defaults to EGRESS.
Protocol filter.
portint | str | None
Single port (0–65535) or inclusive port range such as 8000-9000. Invalid values and reversed ranges raise ValueError; omit the filter to match any port.
Target filter.

Returns

A deny rule.

Rule.allow_dns()

Allow plain DNS (UDP/53 and TCP/53) to the sandbox gateway, i.e. the in-process DNS forwarder. The standard one-liner for opening DNS under a deny-by-default policy. See DNS as egress for the underlying semantics. Returns the pair (udp_rule, tcp_rule) since this SDK’s Rule shape carries a single protocol; splat into NetworkPolicy.rules. DoT (TCP/853) is intentionally not included; add an explicit Rule.allow(destination=Destination.group(DestGroup.HOST), protocol=Protocol.TCP, port=853) if needed (and pair with TLS interception).

Rule.deny_dns()

Deny gateway UDP/53 and TCP/53. Place these rules before profile-generated rules to override their automatic DNS access.

Returns

(udp_rule, tcp_rule) for DestGroup.HOST on port 53.

Destination

Returns NetworkDestination · used by Rule.allow() / Rule.deny()

Factory for typed NetworkDestination values.

Destination.any()

Match any destination.

Destination.ip()

Match an exact IPv4 or IPv6 address. Stored as /32 for IPv4 or /128 for IPv6.

Parameters

ipstr
IPv4 or IPv6 address.

Destination.cidr()

Match a CIDR range.

Parameters

cidrstr
CIDR notation, e.g. “10.0.0.0/8”.

Destination.domain()

Match an exact domain. Domain strings are validated at sandbox creation; invalid names raise ValueError.

Parameters

domainstr
Fully qualified domain name.

Destination.domain_suffix()

Match the apex domain and all subdomains.

Parameters

suffixstr
Domain suffix, e.g. “.example.com”.

Destination.group()

Match a well-known DestGroup address group.

Parameters

Group keyword.

PortBinding

Used by Network(ports=…)

Frozen dataclass for a published host-to-guest port with an optional host bind address. Prefer the PortBinding.tcp() / PortBinding.udp() class methods.

binding.protocol

PortProtocol · Default: TCP Published port protocol PortBinding is a frozen dataclass for published ports that need an explicit host bind address or UDP. Prefer the protocol-specific constructors over building one by hand.
Pass them to Network(ports=(...)). A plain dict[int, int] is also accepted for the common case, binding TCP to 127.0.0.1.

PortBinding.tcp()

Publish a TCP port from the sandbox to the host.

Parameters

host_portint
Port on the host.
guest_portint
Port inside the sandbox.
bindstr
Host bind address. Defaults to 127.0.0.1; use 0.0.0.0 for all IPv4 interfaces.

Returns

A TCP port binding.

PortBinding.udp()

Publish a UDP port from the sandbox to the host.

Parameters

host_portint
Port on the host.
guest_portint
Port inside the sandbox.
bindstr
Host bind address. Defaults to 127.0.0.1.

Returns

A UDP port binding.

NetworkPolicy

Used by Network(policy=…)

Ordered rules with per-direction defaults.
Class methods none() and allow_all() construct terminal whole policies. from_profiles(profiles) composes NetworkProfile values with canonical ordering and automatic gateway DNS.

NetworkPolicy.none()

Deny all ingress and egress traffic

Returns

NetworkPolicy

NetworkPolicy.allow_all()

Allow all ingress and egress traffic

Returns

NetworkPolicy

NetworkPolicy.from_profiles()

Compose the selected canonical network profiles

Returns

NetworkPolicy

Types

NetworkProfile

NetworkDestination

Produced by Destination helpers

Frozen dataclass produced by Destination helpers.

NetworkDestinationKind

Returned in NetworkDestination.kind

Network destination variant.

DnsConfig

Used by Network(dns=…)

Frozen dataclass for DNS interception settings. The value type of Network.dns; import it from microsandbox.types.

TlsConfig

Used by Network(tls=…)

Frozen dataclass for TLS interception settings within Network.

ScopedUpstreamCACert

dataclass

Used by TlsConfig(scoped_upstream_ca_certs=…)

A CA bundle trusted only for upstream hosts matching a pattern.

ScopedVerifyUpstream

dataclass

Used by TlsConfig(scoped_verify_upstream=…)

A per-host override for upstream certificate verification.

NetworkRateLimiter

dataclass

Used by Network.rate_limiter

Groups local network limits by traffic direction. An omitted direction is unlimited.

RateLimiter

dataclass

Held by NetworkRateLimiter

Limits bandwidth and packet rate for one traffic direction.

TokenBucket

dataclass

Used by RateLimiter

Token-bucket configuration for one rate-limiter dimension.

Action

Used by NetworkPolicy · Rule

Policy action.

Direction

Used by Rule

String enum for traffic direction.

Protocol

Used by Rule

String enum for network protocols in policy rules.

PortProtocol

Used by PortBinding

String enum for port-level protocol selection.

DestGroup

Used by Destination.group()

String enum for well-known destination groups used in Destination.group() or string-shorthand Rule.destination.