Network Access Control

Updated at:

Use allowInternetAccess, allowOut, and denyOut to control the destinations that a sandbox actively accesses, and verify the egress rules from inside the sandbox.

Prerequisites

The following examples are provided in both Node.js and Python, and read connection information from environment variables. Before you run them, install the SDK and configure the following environment variables (fill in E2B_API_URL and E2B_DOMAIN for the destination region, where <region> is for example cn-shenzhen):

# Connection info (replace with your actual values)
export E2B_API_KEY="<your-api-key>"
export E2B_API_URL="https://api.<region>.sandbox.aliyuncs.com"
export E2B_DOMAIN="<region>.sandbox.aliyuncs.com"

Node.js (the example is written in TypeScript; save it as demo.ts):

npm install e2b@^2.31.0
npx tsx demo.ts

Python (save the example as demo.py, requires Python 3.10 or later):

python3 --version        # confirm 3.10 or later
pip install e2b==2.31.0
python demo.py

Runtime updates. The Python examples require Python 3.10 or later and E2B Python SDK 2.31.0. If the SDK you have installed does not provide update_network, upgrade it by running the preceding command, or call PUT /sandboxes/{id}/network directly. For a complete example, see Update egress configuration at runtime.

Parameter overview

The network configuration is passed in when you create the sandbox through the top-level allowInternetAccess and network objects.

Field naming is not fully consistent across the three interfaces (Node.js SDK, Python SDK, and OpenAPI), so take care to tell them apart: the following examples are provided in both Node.js and Python. Use the style that matches the SDK you work with.

Parameter (SDK / API)

Purpose

Supported

Modifiable after creation

allowInternetAccess / allow_internet_access

Top-level egress switch that allowOut can still override. false is equivalent to denyOut: ["0.0.0.0/0"].

Yes

Yes (updateNetwork)

network.allowOut / network.allow_out

Egress allow list. Supports IP addresses, CIDR (Classless Inter-Domain Routing) blocks, and domain names.

Yes

Yes (updateNetwork)

network.denyOut / network.deny_out

Egress deny list. Supports IP addresses, CIDR blocks, and domain names.

Yes

Yes (updateNetwork)

network.egressProxy

SOCKS5 egress proxy

Not yet supported

—

network.rules

Transforms egress requests by domain name, such as injecting HTTP headers

Yes

Yes (updateNetwork)

The table lists the Node.js and Python SDK field names side by side: network.rules uses the same name in both SDKs, and network.egressProxy is not yet supported and is not provided by the Python SDK.

egressProxy is not yet supported. A create or update request that carries a non-null value for this field returns 400. Some SDK versions omit this field before the request is sent. For more information about network.rules, see Credential injection.

Public access authentication

For the authentication parameters, request headers, and examples of service ports, see Public access authentication.

When the token is returned

For details, see the When the token is returned section of Public Access.

Difference from envdAccessToken

For details, see the Difference from envdAccessToken section of Public Access.

Egress access control

Egress access control (also referred to as egress traffic control) restricts the destinations that a sandbox actively accesses on external networks. It is jointly determined by allowInternetAccess, allowOut, and denyOut.

Important

If an AgenticFS Volume or an OSS Volume is mounted in the sandbox, you must allow the network targets required to access that storage service. Do this before you enable or adjust an egress access control policy. Otherwise, the storage network connection is interrupted, which affects mounting and file read and write operations. Configure the required egress allow rules before you make the change, and fully assess the impact of the change on your workloads.

Master switch: allowInternetAccess

  • true (default): allows access to the internet.

  • false: equivalent to appending "0.0.0.0/0" to denyOut. This establishes a "deny all by default" baseline, but not a hard switch that overrides allowOut. Because allowOut takes priority over denyOut (see Priority and matching rules below), it forbids all egress only when used alone (without allowOut). Once it is combined with allowOut, destinations matched by allowOut are still allowed, which gives you a "deny all + allow list" policy.

    When allowInternetAccess: false is set and no allow entries are configured, the denied scope covers IPv4 destinations on the internet and in a Virtual Private Cloud (VPC). To turn off the internet egress of the platform while you keep routes to private resources in a VPC, configure vpc_config in the template and set internet_access to false. For more information, see Access resources in a VPC.

import { Sandbox } from "e2b";

// Fully isolate the sandbox from external networks
const sandbox = await Sandbox.create({
  apiKey: process.env.E2B_API_KEY,
  apiUrl: process.env.E2B_API_URL,
  domain: process.env.E2B_DOMAIN,
  allowInternetAccess: false,
});
console.log(`[create] sandbox created (network isolated): ${sandbox.sandboxId}`);

try {
  // Egress is forbidden, so rely on the application-layer result. The || echo fallback prevents a non-zero exit code from raising an exception.
  const probe = await sandbox.commands.run(
    'curl -sS -o /dev/null -w "%{http_code}" -m 5 https://api.github.com || echo blocked'
  );
  console.log(`[probe] access api.github.com -> ${probe.stdout.trim()}`); // blocked
} finally {
  // Destroy the sandbox when you finish to avoid leftover usage and billing
  await sandbox.kill();
  console.log("[cleanup] sandbox destroyed");
}
import os

from e2b import Sandbox

# Fully isolate the sandbox from external networks
sandbox = Sandbox.create(
    api_key=os.environ["E2B_API_KEY"],
    api_url=os.environ["E2B_API_URL"],
    domain=os.environ["E2B_DOMAIN"],
    allow_internet_access=False,
)
print(f"[create] sandbox created (network isolated): {sandbox.sandbox_id}")

try:
    # Egress is forbidden, so rely on the application-layer result. The || echo fallback prevents a non-zero exit code from raising an exception.
    probe = sandbox.commands.run(
        'curl -sS -o /dev/null -w "%{http_code}" -m 5 https://api.github.com || echo blocked'
    )
    print(f"[probe] access api.github.com -> {probe.stdout.strip()}")  # blocked
finally:
    # Destroy the sandbox when you finish to avoid leftover usage and billing
    sandbox.kill()
    print("[cleanup] sandbox destroyed")

Allow list allowOut and deny list denyOut

Each entry in allowOut can be a CIDR block, a bare IP address, or a domain name ("8.8.8.8", "8.8.0.0/16", "example.com", "*.example.com"). denyOut also supports IP addresses, CIDR blocks, and domain names.

import { Sandbox } from "e2b";

// Allowlist mode: deny everything, allow only the specified destinations
const sandbox = await Sandbox.create({
  apiKey: process.env.E2B_API_KEY,
  apiUrl: process.env.E2B_API_URL,
  domain: process.env.E2B_DOMAIN,
  network: {
    denyOut: ["0.0.0.0/0"],
    allowOut: ["8.8.8.8", "8.8.8.0/24", "api.example.com", "*.github.com"],
  },
});
console.log(`[create] sandbox created (allowlisted egress): ${sandbox.sandboxId}`);

try {
  // Matches *.github.com, so the request is allowed
  const allowed = await sandbox.commands.run(
    'curl -sS -o /dev/null -w "%{http_code}" -m 5 https://api.github.com || echo blocked'
  );
  console.log(`[probe] api.github.com (in allowlist) -> ${allowed.stdout.trim()}`);

  // Not in the allowlist, so the request is denied
  const denied = await sandbox.commands.run(
    'curl -sS -o /dev/null -w "%{http_code}" -m 5 https://pypi.org || echo blocked'
  );
  console.log(`[probe] pypi.org (not in allowlist) -> ${denied.stdout.trim()}`); // blocked
} finally {
  // Destroy the sandbox when you finish to avoid leftover usage and billing
  await sandbox.kill();
  console.log("[cleanup] sandbox destroyed");
}
import os

from e2b import Sandbox

sandbox = Sandbox.create(
    api_key=os.environ["E2B_API_KEY"],
    api_url=os.environ["E2B_API_URL"],
    domain=os.environ["E2B_DOMAIN"],
    network={
        "deny_out": ["0.0.0.0/0"],
        "allow_out": ["8.8.8.8", "8.8.8.0/24", "api.example.com", "*.github.com"],
    },
)
print(f"[create] sandbox created (allowlisted egress): {sandbox.sandbox_id}")

try:
    # Matches *.github.com, so the request is allowed
    allowed = sandbox.commands.run(
        'curl -sS -o /dev/null -w "%{http_code}" -m 5 https://api.github.com || echo blocked'
    )
    print(f"[probe] api.github.com (in allowlist) -> {allowed.stdout.strip()}")

    # Not in the allowlist, so the request is denied
    denied = sandbox.commands.run(
        'curl -sS -o /dev/null -w "%{http_code}" -m 5 https://pypi.org || echo blocked'
    )
    print(f"[probe] pypi.org (not in allowlist) -> {denied.stdout.strip()}")  # blocked
finally:
    # Destroy the sandbox when you finish to avoid leftover usage and billing
    sandbox.kill()
    print("[cleanup] sandbox destroyed")

In the SDK, you can also express "all traffic" with a selector callback, which is the officially recommended approach (the ALL_TRAFFIC constant is still kept for backward compatibility). The relationship among the three forms is as follows. ALL_TRAFFIC is a constant exported by both the Node.js and Python SDKs. The input parameter of the Node.js selector callback is allTraffic, and the input parameter of the Python selector callback is ctx.all_traffic. Both of them resolve to 0.0.0.0/0. The following example shows the two styles:

import { Sandbox } from "e2b";

const sandbox = await Sandbox.create({
  apiKey: process.env.E2B_API_KEY,
  apiUrl: process.env.E2B_API_URL,
  domain: process.env.E2B_DOMAIN,
  network: {
    denyOut: ({ allTraffic }) => [allTraffic], // allTraffic === "0.0.0.0/0"
    allowOut: ["1.1.1.1", "8.8.8.0/24"],
  },
});
console.log(`[create] sandbox created: ${sandbox.sandboxId}`);

try {
  // ... Business logic ...
} finally {
  // Destroy the sandbox when you finish to avoid leftover usage and billing
  await sandbox.kill();
  console.log("[cleanup] sandbox destroyed");
}
import os

from e2b import Sandbox

sandbox = Sandbox.create(
    api_key=os.environ["E2B_API_KEY"],
    api_url=os.environ["E2B_API_URL"],
    domain=os.environ["E2B_DOMAIN"],
    network={
        "deny_out": lambda ctx: [ctx.all_traffic],  // ctx.all_traffic == "0.0.0.0/0"
        "allow_out": ["1.1.1.1", "8.8.8.0/24"],
    },
)
print(f"[create] sandbox created: {sandbox.sandbox_id}")

try:
    # ... Business logic ...
    pass
finally:
    # Destroy the sandbox when you finish to avoid leftover usage and billing
    sandbox.kill()
    print("[cleanup] sandbox destroyed")

Scope and limitations of domain filtering

When you filter by domain name, take note of the following behaviors:

  • Default behavior when only an allow list is configured: if allowOut is not empty and denyOut is omitted or set to an empty array, the service automatically adds denyOut: ["0.0.0.0/0"]. If you explicitly provide a non-empty denyOut, the deny entries you specify are used for matching. Setting allowInternetAccess: false also appends the preceding IPv4 range.

  • Covers only HTTP (port 80) and TLS (port 443): domain matching relies on the Host header on port 80 and the TLS Server Name Indication (SNI) on port 443. Other ports can only be filtered by IP addresses or CIDR blocks. UDP protocols such as QUIC and HTTP/3 do not support domain filtering.

  • DNS resolution. After you configure domain name rules, the platform processes the DNS queries that the sandbox sends through the default DNS server based on those rules. If a domain name is denied by an egress rule, the DNS query is also denied, and your application may report a name resolution failure. Verify whether a destination is reachable based on the results of application-layer requests.

  • The wildcard *.example.com is supported and matches all subdomains.

How to avoid affecting storage access

To restrict egress access, add the domain names of the storage services that you use to the egress allow list allowOut (allow_out in the Python SDK).

For an OSS Volume mount

Combine the bucket name with the endpoint domain name in the mount configuration. Example:

  • Bucket name: your-bucket

  • Endpoint: https://oss-cn-hangzhou.aliyuncs.com

  • Add to the allow list: your-bucket.oss-cn-hangzhou.aliyuncs.com

For an AgenticFS Volume mount

Copy the domain name that precedes :/ from the mount target address. Example:

  • Mount target address: ap-xxxxx.xxxxx-yyyyy.cn-hangzhou.nas.aliyuncs.com:/data

  • Add to the allow list: ap-xxxxx.xxxxx-yyyyy.cn-hangzhou.nas.aliyuncs.com

If you use multiple buckets or access points, add the domain name of each one before you enable egress restrictions.

Priority and matching rules

When multiple parameters are present at the same time, the following rules determine whether an egress request is allowed:

  1. Allow takes priority over deny: allowOut always takes priority over denyOut. If a destination matches both the allow list and the deny list, it is allowed.

  2. allowInternetAccess: false is only equivalent to denyOut += ["0.0.0.0/0"]: it establishes a "deny all by default" baseline, but is not a hard switch. Because allow takes priority over deny, destinations matched by allowOut still override this baseline and are allowed. Therefore, allowInternetAccess: false plus allowOut gives you a "deny all + allow list" policy, and all egress is truly forbidden only when no allowOut is configured.

  3. IPv4 allow list: if the allow list is not empty and denyOut is empty, IPv4 destinations that do not match the allow list are automatically denied.

Effects of common combinations:

Configuration

Effect

No egress parameters configured

Allows access to the entire internet (default)

allowInternetAccess: false (without allowOut)

Forbids all egress (equivalent to denyOut: ["0.0.0.0/0"])

allowInternetAccess: false + allowOut: ["8.8.8.8"]

Denies all by default, but allows 8.8.8.8 matched by allowOut (allow takes priority, which is the same as denyOut: ["0.0.0.0/0"] + allowOut)

denyOut: ["10.0.0.0/8"]

Denies only that CIDR block and allows the rest (deny list)

denyOut: ["0.0.0.0/0"] + allowOut: ["8.8.8.8"]

Allows only 8.8.8.8 and denies everything else (allow list)

allowOut: ["example.com"] (without denyOut)

Allows the specified domain name, automatically adds denyOut: ["0.0.0.0/0"], and denies IPv4 destinations that do not match.

Behavior of denied connections

Because of the way the egress firewall is implemented, a denied TCP connection may appear to connect successfully from inside the sandbox: the firewall must accept the connection before it can decide whether the destination is allowed, so the socket may show as established even though no packets actually reach the destination.

To determine whether traffic actually reaches a destination, check the application-layer response, such as the HTTP status code, the TLS handshake, and the expected protocol bytes. Do not rely on whether the TCP connection is established.

Updating egress configuration at runtime

For complete examples of modifying the allow list of a running sandbox, blocking a single IP address, and turning off egress, see Update egress configuration at runtime.

Limits

  • egressProxy is not yet supported. A create or update request that carries a non-null value for this field returns 400. Some SDK versions omit this field before the request is sent.

  • trafficAccessToken is returned when you create or connect to a sandbox for which service port authentication is enabled. Neither getInfo nor list returns the token. A valid token is required when you access a protected service port over the internet or PrivateLink.

  • denyOut supports IP addresses, CIDR blocks, and domain names.

  • Domain filtering covers only HTTP (port 80) and TLS (port 443). Other ports are filtered by IP addresses or CIDR blocks, and UDP protocols such as QUIC and HTTP/3 do not support domain filtering.

  • A denied connection may appear as "connected successfully" inside the sandbox. Rely on the application-layer response.

  • Egress control (allowOut / denyOut) and public inbound authentication (allowPublicTraffic) are independent of each other.

Recommendations

  • For untrusted code or third-party tasks, tighten egress by default: denyOut: ["0.0.0.0/0"] plus an allowOut allow list as needed.

  • When you use a domain name allow list, you can explicitly set denyOut: ["0.0.0.0/0"], and confirm that the destination uses port 80 or 443.

  • Do not write trafficAccessToken or envdAccessToken into logs or command output.

Update and audit

When you change the egress configuration of a running sandbox, you must also submit the credential injection rules that you want to keep. For more information, see Update network configuration. To view the accessed destinations and the policy decision results, see View egress audit logs.