Skip to main content
Configure sandbox networking. See Networking for usage and policy concepts.

Network

Used by Sandbox.create(network=…)

Sandbox network configuration.

network.policy

NetworkPolicy \| None · Default: None Concrete network policy

network.ports

Mapping[int, int] \| Sequence[PortBinding] · Default: {} Port mappings from host to guest. Mapping form binds TCP to 127.0.0.1; PortBinding can set an explicit bind address or UDP

network.deny_domains

tuple[str, ...] · Default: () Deny egress to these exact domains. Each entry adds a deny Domain("...") policy rule that fires at DNS resolution (NXDOMAIN), TLS first-flight (SNI), and TCP egress (cache fallback). Prepended onto the policy so it takes precedence over later allow rules

network.deny_domain_suffixes

tuple[str, ...] · Default: () Deny egress to all subdomains of these suffixes. Adds deny DomainSuffix("...") rules; same enforcement layers as deny_domains

network.dns

DnsConfig \| None · Default: None DNS interception configuration

network.tls

TlsConfig \| None · Default: None TLS interception configuration

network.ipv4_pool

str \| None · Default: None IPv4 pool used for per-sandbox /30 guest subnets. Defaults to 172.16.0.0/12

network.ipv6_pool

str \| None · Default: None IPv6 pool used for per-sandbox /64 guest prefixes. Defaults to fd42:6d73:62::/48

network.max_connections

int \| None · Default: None Maximum concurrent connections

network.rate_limiter

NetworkRateLimiter \| None · Default: None Ingress and egress rate limits

network.on_secret_violation

ViolationAction\|ViolationPolicy · Default: BLOCK_AND_LOG Sandbox-wide action when a secret placeholder reaches a disallowed host

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.

rule.action

Action What to do when this rule matches

rule.direction

Direction · Default: EGRESS Which evaluator considers this rule. Direction.ANY matches in either direction

rule.destination

str \| NetworkDestination \| None · Default: None Target filter. Prefer typed Destination helpers; string shorthand also works (DestGroup values, exact IPs, domains, CIDR ranges, domain suffixes prefixed with ".", or "*" for any). Domain and suffix strings are validated at sandbox creation; invalid names raise ValueError

rule.protocol

Protocol \| None · Default: None Protocol filter

rule.port

int \| str \| None · Default: None Single port (443) or range ("8000-9000") 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 or port range.
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.host_port

int Port on the host

binding.guest_port

int Port inside the sandbox

binding.bind

str · Default: "127.0.0.1" Host address to bind. Use 0.0.0.0 for all IPv4 interfaces

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.

policy.default_egress

Action · Default: DENY Action when no egress-applicable rule matches

policy.default_ingress

Action · Default: ALLOW Action when no ingress-applicable rule matches

policy.rules

tuple[Rule, ...] · Default: () Rules evaluated first-match-wins per direction 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.