# How to Configure Network Policies with Per-Sandbox Traffic Tokens in CubeSandbox

> Configure network policies and per-sandbox traffic tokens in CubeSandbox. Restrict egress traffic and public URL access simultaneously for enhanced security.

- Repository: [Tencent Cloud/CubeSandbox](https://github.com/TencentCloud/CubeSandbox)
- Tags: how-to-guide
- Published: 2026-07-09

---

**CubeSandbox separates outbound network policies (CIDR-based eBPF rules) from inbound protection (per-sandbox traffic tokens), allowing operators to restrict both egress traffic and public URL access simultaneously.**

CubeSandbox provides a dual-layer security model for sandbox networking that combines kernel-level traffic filtering with request-level authentication. By leveraging the `CubeNetworkConfig` struct and traffic token mechanisms implemented in the TencentCloud/CubeSandbox repository, administrators can enforce granular control over both outbound connections and inbound public access. This guide demonstrates how to configure these policies using actual source code paths and working SDK examples.

## Understanding Outbound Network Policies

Outbound network policies in CubeSandbox are defined through the **`CubeNetworkConfig`** struct, specifically utilizing the `allow_out`, `deny_out`, and `allow_internet_access` fields. These configurations are compiled into **eBPF LPM-trie maps** by the network agent located in [`CubeNet/cubevs/netpolicy.go`](https://github.com/TencentCloud/CubeSandbox/blob/main/CubeNet/cubevs/netpolicy.go). The maps are attached directly to the sandbox's TAP device, enforcing CIDR-based rules for every packet leaving the sandbox at the kernel level.

### Configuring CIDR Allow and Deny Lists

To restrict outbound traffic, populate the `AllowOut` field with specific CIDR blocks representing trusted SaaS endpoints or APIs. Simultaneously, set `DenyOut` to `["0.0.0.0/0"]` to block all other outbound connections. This creates an explicit allowlist model where only destinations matching the specified CIDR ranges can receive traffic from the sandbox.

## Enabling Per-Sandbox Traffic Tokens

Per-sandbox traffic tokens provide inbound protection by requiring authentication for all public URL requests. When creating a sandbox, set `network.allow_public_traffic` to `false` in the request. The master service, implemented in [`CubeMaster/pkg/service/sandbox/sandbox_run.go`](https://github.com/TencentCloud/CubeSandbox/blob/main/CubeMaster/pkg/service/sandbox/sandbox_run.go), generates a random opaque token stored in the **`TrafficAccessToken`** field and returns it in the create-sandbox response defined in [`CubeMaster/pkg/service/sandbox/types/types.go`](https://github.com/TencentCloud/CubeSandbox/blob/main/CubeMaster/pkg/service/sandbox/types/types.go).

### Token Validation in CubeProxy

When inbound traffic arrives at the sandbox's public URL, **`CubeProxy`** validates the request by checking for the `e2b-traffic-access-token` header (E2B-compatible) or the `cube-traffic-access-token` header (native alias). If the header is missing or the value does not match the stored token, `CubeProxy` rejects the request with **HTTP 403** before any data reaches the sandbox, as documented in [`docs/guide/restrict-public-access.md`](https://github.com/TencentCloud/CubeSandbox/blob/main/docs/guide/restrict-public-access.md).

## Combining Network Policies with Traffic Tokens

The two security mechanisms operate independently and can be combined without conflict. Outbound eBPF maps control what the sandbox can **send**, while the traffic token header controls who can **receive** responses from the public URL. This separation allows operators to implement defense-in-depth strategies, restricting both the sandbox's network footprint and its exposure to unauthorized inbound requests.

## Implementation Examples

The following examples demonstrate how to configure network policies with per-sandbox traffic tokens using the CubeSandbox SDKs and direct API calls.

### Python SDK Configuration

```python
from cubesandbox import Sandbox

sandbox = Sandbox.create(
    template="your-template-id",
    network={"allow_public_traffic": False}   # disables public access

)

token = sandbox.traffic_access_token
url = f"http://{sandbox.get_host(80)}/"

# 403 without token

assert requests.get(url).status_code == 403

# 200 with E2B‑compatible header

assert requests.get(url, headers={"e2b-traffic-access-token": token}).status_code == 200

# 200 with CubeSandbox‑native header

assert requests.get(url, headers={"cube-traffic-access-token": token}).status_code == 200

```

### Go SDK Configuration

```go
import "github.com/TencentCloud/CubeSandbox/sdk/go"

cfg := &sdk.NetworkConfig{
    AllowPublicTraffic: sdk.Bool(false),      // generate traffic token
    AllowOut: []string{
        "203.0.113.0/24",   // allowed SaaS CIDR
    },
    DenyOut: []string{
        "0.0.0.0/0",        // block everything else
    },
}

rsp, err := sdk.CreateSandbox(context.Background(), sdk.CreateRequest{
    TemplateID: "your-template-id",
    Network:    cfg,
})
if err != nil { /* handle */ }

token := rsp.TrafficAccessToken   // send this token with every inbound request

```

### cURL Requests with Token Headers

```bash
curl -H "e2b-traffic-access-token: $TOKEN" \
     "http://80-$SANDBOX_ID.cube.app/"

```

## Summary

- Outbound policies use eBPF LPM-trie maps in [`CubeNet/cubevs/netpolicy.go`](https://github.com/TencentCloud/CubeSandbox/blob/main/CubeNet/cubevs/netpolicy.go) to enforce CIDR-based rules at the kernel level.
- Traffic tokens are generated when `AllowPublicTraffic` is set to `false` and returned in the `TrafficAccessToken` field.
- `CubeProxy` validates `e2b-traffic-access-token` or `cube-traffic-access-token` headers, rejecting unauthorized requests with **HTTP 403**.
- Both mechanisms can be combined to create comprehensive network security policies that control both egress and ingress traffic.

## Frequently Asked Questions

### How do traffic tokens differ from outbound network policies?

Traffic tokens control **inbound** access by requiring authentication headers on public URL requests, while outbound network policies control **egress** traffic through eBPF-based CIDR filtering. Tokens operate at the application layer (HTTP headers), whereas outbound policies operate at the network layer (eBPF/XDP) within [`CubeNet/cubevs/netpolicy.go`](https://github.com/TencentCloud/CubeSandbox/blob/main/CubeNet/cubevs/netpolicy.go).

### Can I use traffic tokens with full internet access enabled?

Yes. Since traffic tokens only restrict inbound public access and outbound policies only restrict egress traffic, you can configure a sandbox with `allow_internet_access: true` while still requiring tokens for incoming requests. This is useful when sandboxes need to reach external APIs but should only respond to authenticated callers.

### What happens if I don't include the traffic token header?

`CubeProxy` rejects the request with **HTTP 403 Forbidden** before the traffic reaches the sandbox container. The connection is terminated at the proxy layer, providing zero-trust inbound security regardless of the application running inside the sandbox.

### Are the token headers compatible with E2B standards?

Yes. CubeSandbox supports the `e2b-traffic-access-token` header for compatibility with E2B standards, while also accepting `cube-traffic-access-token` as a native alias. Both headers validate against the same `TrafficAccessToken` value stored for the sandbox according to the types defined in [`CubeMaster/pkg/service/sandbox/types/types.go`](https://github.com/TencentCloud/CubeSandbox/blob/main/CubeMaster/pkg/service/sandbox/types/types.go).