Network
A sandbox’s network mode controls where its outbound connections can go. Inbound connections are never allowed; sandboxes publish no ports.
| Mode | Allowed destinations |
|---|---|
public (default) |
the internet |
none |
nothing, not even a DNS resolver |
allowlist |
a DNS resolver and only the entries you list |
In every mode, a sandbox is blocked from reaching the host machine, the host’s local network, and link-local addresses such as cloud metadata services.
Allowlist mode
Section titled “Allowlist mode”Each allowlist entry is one of:
- a hostname, such as
pypi.org; - a wildcard subdomain, such as
*.pythonhosted.org; - an IPv4 address, such as
8.8.8.8; - an IPv4 CIDR range, such as
8.8.8.0/24.
A hostname permits the internet addresses returned by the DNS resolver. An allowlist holds at most 256 entries. IPv6 is not supported, and any address or network inside the blocked ranges above is rejected.
In Python and TypeScript, passing allow selects allowlist mode
automatically. In Rust, pass SandboxNetwork::Allowlist { allow }.
with Sandbox.create("python:3.12", allow=["pypi.org", "*.pythonhosted.org"]) as sandbox: installed = sandbox.run(["pip", "install", "--quiet", "six"]) print(installed.exit_code)
sandbox.set_network("none") refused = sandbox.run(["pip", "download", "--quiet", "--no-deps", "--dest", "/tmp", "idna"]) print(refused.exit_code) print(sandbox.spec["network"])await using sandbox = await Sandbox.create("python:3.12", { allow: ["pypi.org", "*.pythonhosted.org"],});const installed = await sandbox.run(["pip", "install", "--quiet", "six"]);console.log(installed.exitCode);
await sandbox.setNetwork("none");const refused = await sandbox.run(["pip", "download", "--quiet", "--no-deps", "--dest", "/tmp", "idna"]);console.log(refused.exitCode);console.log(sandbox.spec.network);let allow = vec!["pypi.org".to_owned(), "*.pythonhosted.org".to_owned()];let sandbox = client .create_sandbox("python:3.12") .network(SandboxNetwork::Allowlist { allow }) .await?;sandbox .scope(async |sandbox| { let installed = sandbox.run(["pip", "install", "--quiet", "six"]).await?; println!("{}", installed.exit_code);
sandbox.set_network(&SandboxNetwork::None).await?; let refused = sandbox .run([ "pip", "download", "--quiet", "--no-deps", "--dest", "/tmp", "idna", ]) .await?; println!("{}", refused.exit_code); println!("{:?}", sandbox.spec().network()); Ok(()) }) .await?;No network
Section titled “No network”Block all outbound traffic, including DNS:
with Sandbox.create("alpine:3.20", network="none") as sandbox: print(sandbox.run("wget -q -T 5 -O /dev/null http://example.com").exit_code)await using sandbox = await Sandbox.create("alpine:3.20", { network: "none" });console.log((await sandbox.run("wget -q -T 5 -O /dev/null http://example.com")).exitCode);let sandbox = client .create_sandbox("alpine:3.20") .network(SandboxNetwork::None) .await?;sandbox .scope(async |sandbox| { let fetched = sandbox .run("wget -q -T 5 -O /dev/null http://example.com") .await?; println!("{}", fetched.exit_code); Ok(()) }) .await?;Change the network while running
Section titled “Change the network while running”Call set_network (setNetwork in TypeScript) to change where new
connections can go from that point onward. Already open connections remain
open, and the sandbox’s spec reflects the active network setting. Calling it
on a sandbox that has ended raises the error for how it ended.
A common pattern is to install dependencies with public or an allowlist,
then switch to none before the part you want sealed, as shown above.
dvm records the bytes transferred on every connection as part of the run (What a recording holds).