# Network

> Control where a sandbox can connect (the internet, nowhere, or an allowlist) and change it while the sandbox runs.

This is the Markdown of https://docs.dvm.run/guides/network/. Every page of the dvm.run docs is served as Markdown at its address plus `.md`; the list of pages is https://docs.dvm.run/llms.txt.

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

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 }`.

* Python

  `python/examples/network.py`

  ```python
  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"])
  ```

* TypeScript

  `js/examples/network.ts`

  ```ts
  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);
  ```

* Rust

  `rust/dvm-run/examples/network.rs`

  ```rust
  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

Block all outbound traffic, including DNS:

* Python

  `python/examples/network.py`

  ```python
  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)
  ```

* TypeScript

  `js/examples/network.ts`

  ```ts
  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);
  ```

* Rust

  `rust/dvm-run/examples/network.rs`

  ```rust
  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

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](/concepts/recording/)).
