Skip to content

Commit 218a0e0

Browse files
mishushakovclaude
andauthored
docs: replace outdated ALL_TRAFFIC syntax with selector callbacks (#231)
* made it nicer * docs: fix updateNetwork wording and add ALL_TRAFFIC backward-compat note Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
1 parent d9148d3 commit 218a0e0

1 file changed

Lines changed: 48 additions & 99 deletions

File tree

docs/sandbox/internet-access.mdx

Lines changed: 48 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Every sandbox has access to the internet and can be reached by a public URL.
66

77
## Controlling internet access
88

9-
You can control whether a sandbox has access to the internet by using the `allowInternetAccess` parameter when creating a sandbox. By default, internet access is enabled (`true`), but you can disable it for security-sensitive workloads.
9+
You can control whether a sandbox has access to the internet by using the `allowInternetAccess` / `allow_internet_access` parameter when creating a sandbox. By default, internet access is enabled, but you can disable it for security-sensitive workloads.
1010

1111
<CodeGroup>
1212
```js JavaScript & TypeScript
@@ -32,25 +32,25 @@ isolated_sandbox = Sandbox.create(allow_internet_access=False)
3232
When internet access is disabled, the sandbox cannot make outbound network connections, which provides an additional layer of security for sensitive code execution.
3333

3434
<Note>
35-
Setting `allowInternetAccess` to `false` is equivalent to setting `network.denyOut` to `['0.0.0.0/0']` (denying all traffic).
35+
Setting `allowInternetAccess` / `allow_internet_access` to a falsy value is equivalent to setting `network.denyOut` / `network.deny_out` to `['0.0.0.0/0']` (denying all traffic).
3636
</Note>
3737

3838
## Fine-grained network control
3939

40-
For more granular control over network access, you can use the `network` configuration option to specify allow and deny lists for outbound traffic.
40+
For more granular control over network access, you can use the network configuration option to specify allow and deny lists for outbound traffic.
4141

4242
### Allow and deny lists
4343

4444
You can specify IP addresses, CIDR blocks, or domain names that the sandbox is allowed to use:
4545

4646
<CodeGroup>
4747
```js JavaScript & TypeScript
48-
import { Sandbox, ALL_TRAFFIC } from 'e2b'
48+
import { Sandbox } from 'e2b'
4949

5050
// Deny all traffic except specific IPs
5151
const sandbox = await Sandbox.create({
5252
network: {
53-
denyOut: [ALL_TRAFFIC],
53+
denyOut: ({ allTraffic }) => [allTraffic], // allTraffic === '0.0.0.0/0'
5454
allowOut: ['1.1.1.1', '8.8.8.0/24']
5555
}
5656
})
@@ -63,12 +63,12 @@ const restrictedSandbox = await Sandbox.create({
6363
})
6464
```
6565
```python Python
66-
from e2b import Sandbox, ALL_TRAFFIC
66+
from e2b import Sandbox
6767

6868
# Deny all traffic except specific IPs
6969
sandbox = Sandbox.create(
7070
network={
71-
"deny_out": [ALL_TRAFFIC],
71+
"deny_out": lambda ctx: [ctx.all_traffic], # ctx.all_traffic == "0.0.0.0/0"
7272
"allow_out": ["1.1.1.1", "8.8.8.0/24"]
7373
}
7474
)
@@ -82,30 +82,34 @@ restricted_sandbox = Sandbox.create(
8282
```
8383
</CodeGroup>
8484

85+
<Note>
86+
The selector callback (`({ allTraffic }) => [allTraffic]` / `lambda ctx: [ctx.all_traffic]`) is the recommended way to express "all traffic" (`0.0.0.0/0`). The `ALL_TRAFFIC` constant remains exported for backward compatibility.
87+
</Note>
88+
8589
### Domain-based filtering
8690

87-
You can allow traffic to specific domains by specifying hostnames in `allow out`. When using domain-based filtering, you must include `ALL_TRAFFIC` in `deny out` to block all other traffic. Domains are not supported in the `deny out` list.
91+
You can allow traffic to specific domains by specifying hostnames in `allowOut` / `allow_out`. When using domain-based filtering, you must deny all other traffic in `denyOut` / `deny_out`. Domains are not supported in the deny lists.
8892

8993
<CodeGroup>
9094
```js JavaScript & TypeScript
91-
import { Sandbox, ALL_TRAFFIC } from 'e2b'
95+
import { Sandbox } from 'e2b'
9296

9397
// Allow only traffic to google.com
9498
const sandbox = await Sandbox.create({
9599
network: {
96100
allowOut: ['google.com'],
97-
denyOut: [ALL_TRAFFIC]
101+
denyOut: ({ allTraffic }) => [allTraffic]
98102
}
99103
})
100104
```
101105
```python Python
102-
from e2b import Sandbox, ALL_TRAFFIC
106+
from e2b import Sandbox
103107

104108
# Allow only traffic to google.com
105109
sandbox = Sandbox.create(
106110
network={
107111
"allow_out": ["google.com"],
108-
"deny_out": [ALL_TRAFFIC]
112+
"deny_out": lambda ctx: [ctx.all_traffic]
109113
}
110114
)
111115
```
@@ -119,24 +123,24 @@ You can also use wildcards to allow all subdomains of a domain:
119123

120124
<CodeGroup>
121125
```js JavaScript & TypeScript
122-
import { Sandbox, ALL_TRAFFIC } from 'e2b'
126+
import { Sandbox } from 'e2b'
123127

124128
// Allow traffic to any subdomain of mydomain.com
125129
const sandbox = await Sandbox.create({
126130
network: {
127131
allowOut: ['*.mydomain.com'],
128-
denyOut: [ALL_TRAFFIC]
132+
denyOut: ({ allTraffic }) => [allTraffic]
129133
}
130134
})
131135
```
132136
```python Python
133-
from e2b import Sandbox, ALL_TRAFFIC
137+
from e2b import Sandbox
134138

135139
# Allow traffic to any subdomain of mydomain.com
136140
sandbox = Sandbox.create(
137141
network={
138142
"allow_out": ["*.mydomain.com"],
139-
"deny_out": [ALL_TRAFFIC]
143+
"deny_out": lambda ctx: [ctx.all_traffic]
140144
}
141145
)
142146
```
@@ -146,24 +150,24 @@ You can combine domain names with IP addresses and CIDR blocks:
146150

147151
<CodeGroup>
148152
```js JavaScript & TypeScript
149-
import { Sandbox, ALL_TRAFFIC } from 'e2b'
153+
import { Sandbox } from 'e2b'
150154

151155
// Allow traffic to specific domains and IPs
152156
const sandbox = await Sandbox.create({
153157
network: {
154158
allowOut: ['api.example.com', '*.github.com', '8.8.8.8'],
155-
denyOut: [ALL_TRAFFIC]
159+
denyOut: ({ allTraffic }) => [allTraffic]
156160
}
157161
})
158162
```
159163
```python Python
160-
from e2b import Sandbox, ALL_TRAFFIC
164+
from e2b import Sandbox
161165

162166
# Allow traffic to specific domains and IPs
163167
sandbox = Sandbox.create(
164168
network={
165169
"allow_out": ["api.example.com", "*.github.com", "8.8.8.8"],
166-
"deny_out": [ALL_TRAFFIC]
170+
"deny_out": lambda ctx: [ctx.all_traffic]
167171
}
168172
)
169173
```
@@ -185,68 +189,41 @@ This is a limitation of how outbound traffic is currently routed from the sandbo
185189

186190
### Priority rules
187191

188-
When both `allow out` and `deny out` are specified, **allow rules always take precedence** over deny rules. This means if an IP address is in both lists, it will be allowed.
192+
When both allow and deny rules are specified, **allow rules always take precedence** over deny rules. This means if an IP address is in both lists, it will be allowed.
189193

190194
<CodeGroup>
191195
```js JavaScript & TypeScript
192-
import { Sandbox, ALL_TRAFFIC } from 'e2b'
196+
import { Sandbox } from 'e2b'
193197

194-
// Even though ALL_TRAFFIC is denied, 1.1.1.1 and 8.8.8.8 are explicitly allowed
198+
// Even though all traffic is denied, 1.1.1.1 and 8.8.8.8 are explicitly allowed
195199
const sandbox = await Sandbox.create({
196200
network: {
197-
denyOut: [ALL_TRAFFIC],
201+
denyOut: ({ allTraffic }) => [allTraffic],
198202
allowOut: ['1.1.1.1', '8.8.8.8']
199203
}
200204
})
201205
```
202206
```python Python
203-
from e2b import Sandbox, ALL_TRAFFIC
207+
from e2b import Sandbox
204208

205-
# Even though ALL_TRAFFIC is denied, 1.1.1.1 and 8.8.8.8 are explicitly allowed
209+
# Even though all traffic is denied, 1.1.1.1 and 8.8.8.8 are explicitly allowed
206210
sandbox = Sandbox.create(
207211
network={
208-
"deny_out": [ALL_TRAFFIC],
212+
"deny_out": lambda ctx: [ctx.all_traffic],
209213
"allow_out": ["1.1.1.1", "8.8.8.8"]
210214
}
211215
)
212216
```
213217
</CodeGroup>
214218

215-
### ALL_TRAFFIC helper
216-
217-
The `ALL_TRAFFIC` constant represents the CIDR range `0.0.0.0/0`, which matches all IP addresses. Use it to easily deny or allow all network traffic:
218-
219-
<CodeGroup>
220-
```js JavaScript & TypeScript
221-
import { Sandbox, ALL_TRAFFIC } from 'e2b'
222-
223-
// Deny all outbound traffic
224-
const sandbox = await Sandbox.create({
225-
network: {
226-
denyOut: [ALL_TRAFFIC]
227-
}
228-
})
229-
```
230-
```python Python
231-
from e2b import Sandbox, ALL_TRAFFIC
232-
233-
# Deny all outbound traffic
234-
sandbox = Sandbox.create(
235-
network={
236-
"deny_out": [ALL_TRAFFIC]
237-
}
238-
)
239-
```
240-
</CodeGroup>
241-
242219
### Per-host request transforms
243220

244221
<Note>
245222
Per-host request transforms are currently in private beta.
246223
If you'd like access, please reach out to us at [support@e2b.dev](mailto:support@e2b.dev).
247224
</Note>
248225

249-
You can register per-host rules under `network.rules` to apply transforms (for example, inject HTTP headers) on outbound requests matching a host. Rules are keyed by host and registering one does **not** grant egress on its own — the host must still be referenced via `allowOut`.
226+
You can register per-host rules under `network.rules` to apply transforms (for example, inject HTTP headers) on outbound requests matching a host. Rules are keyed by host and registering one does **not** grant egress on its own — the host must still be referenced via `allowOut` / `allow_out`.
250227

251228
The `transform.headers` object is sent on the wire as-is and injected by the egress proxy on matching HTTP/HTTPS requests.
252229

@@ -258,6 +235,9 @@ await Sandbox.create({
258235
network: {
259236
// Only allow egress to hosts that have rules registered.
260237
allowOut: ({ rules }) => [...rules.keys()],
238+
// Deny all other traffic
239+
denyOut: ({ allTraffic }) => [allTraffic],
240+
// Register per-host rules
261241
rules: {
262242
'api.example.com': [
263243
{
@@ -275,7 +255,11 @@ from e2b import Sandbox
275255

276256
sandbox = Sandbox.create(
277257
network={
258+
# Only allow egress to hosts that have rules registered.
278259
"allow_out": lambda ctx: list(ctx.rules.keys()),
260+
# Deny all other traffic
261+
"deny_out": lambda ctx: [ctx.all_traffic],
262+
# Register per-host rules
279263
"rules": {
280264
"api.example.com": [
281265
{
@@ -302,48 +286,13 @@ await Sandbox.create({
302286
})
303287
```
304288

305-
### Selector callbacks for `allowOut` and `denyOut`
306-
307-
`allowOut` and `denyOut` accept either a static list (as shown above) or a **selector callback** that receives a context object — `{ allTraffic, rules }` in JavaScript and `ctx.all_traffic` / `ctx.rules` in Python. This lets you derive policies from the registered rule hosts without duplicating them, and provides a typed alternative to importing `ALL_TRAFFIC`.
308-
309-
- `allTraffic` (JS) / `ctx.all_traffic` (Python) is the literal `'0.0.0.0/0'`.
310-
- `rules` is a `Map` (Python `Mapping`) view of `network.rules`.
311-
312-
<CodeGroup>
313-
```js JavaScript & TypeScript
314-
import { Sandbox } from 'e2b'
315-
316-
// Block all egress except an explicit allowlist
317-
await Sandbox.create({
318-
network: {
319-
denyOut: ({ allTraffic }) => [allTraffic], // allTraffic === '0.0.0.0/0'
320-
allowOut: ['1.1.1.1', '8.8.8.0/24'],
321-
},
322-
})
323-
```
324-
```python Python
325-
from e2b import Sandbox
326-
327-
Sandbox.create(
328-
network={
329-
"deny_out": lambda ctx: [ctx.all_traffic],
330-
"allow_out": ["1.1.1.1", "8.8.8.0/24"],
331-
},
332-
)
333-
```
334-
</CodeGroup>
335-
336-
<Note>
337-
The selector form (`({ allTraffic }) => [allTraffic]` / `lambda ctx: [ctx.all_traffic]`) is the recommended way to express "everything". The `ALL_TRAFFIC` constant is still exported for backward compatibility.
338-
</Note>
339-
340289
### Updating network settings on a running sandbox
341290

342291
You can update the network configuration of an already running sandbox using `updateNetwork` (JavaScript) or `update_network` (Python). This replaces the current egress rules with the provided configuration without restarting the sandbox.
343292

344293
<CodeGroup>
345294
```js JavaScript & TypeScript
346-
import { Sandbox, ALL_TRAFFIC } from 'e2b'
295+
import { Sandbox } from 'e2b'
347296

348297
const sandbox = await Sandbox.create()
349298

@@ -354,15 +303,15 @@ await sandbox.updateNetwork({
354303

355304
// Replace with an allow-list only
356305
await sandbox.updateNetwork({
357-
denyOut: [ALL_TRAFFIC],
306+
denyOut: ({ allTraffic }) => [allTraffic],
358307
allowOut: ['api.example.com'],
359308
})
360309

361310
// Toggle internet access without recreating the sandbox
362311
await sandbox.updateNetwork({ allowInternetAccess: false })
363312
```
364313
```python Python
365-
from e2b import Sandbox, ALL_TRAFFIC
314+
from e2b import Sandbox
366315

367316
sandbox = Sandbox.create()
368317

@@ -371,7 +320,7 @@ sandbox.update_network({"deny_out": ["8.8.8.8"]})
371320

372321
# Replace with an allow-list only
373322
sandbox.update_network({
374-
"deny_out": [ALL_TRAFFIC],
323+
"deny_out": lambda ctx: [ctx.all_traffic],
375324
"allow_out": ["api.example.com"],
376325
})
377326

@@ -381,10 +330,10 @@ sandbox.update_network({"allow_internet_access": False})
381330
</CodeGroup>
382331

383332
<Note>
384-
`updateNetwork` / `update_network` **replaces** the current egress configuration — it does not merge with the existing rules. Calling it with an empty object (`updateNetwork({})` / `update_network({})`) clears all `allowOut` / `denyOut` / per-host rules set at create time.
333+
`updateNetwork` / `update_network` **replaces** the current egress configuration — it does not merge with the existing rules. Calling it with an empty object (`updateNetwork({})` / `update_network({})`) clears all allow and deny rules set at create time.
385334
</Note>
386335

387-
The create-only options `allowPublicTraffic` and `maskRequestHost` cannot be changed after the sandbox is created.
336+
Create-only options such as `allowPublicTraffic` / `allow_public_traffic`, `maskRequestHost` / `mask_request_host` and network rules in `network.rules` cannot be changed after the sandbox is created.
388337

389338
## Sandbox public URL
390339
Every sandbox has a public URL that can be used to access running services inside the sandbox.
@@ -425,7 +374,7 @@ The first leftmost part of the host is the port number we passed to the method.
425374

426375
## Restricting public access to sandbox URLs
427376

428-
By default, sandbox URLs are publicly accessible. You can restrict access to require authentication using the `allowPublicTraffic` option:
377+
By default, sandbox URLs are publicly accessible. You can restrict access to require authentication using the `allowPublicTraffic` / `allow_public_traffic` option:
429378

430379
<CodeGroup>
431380
```js JavaScript & TypeScript
@@ -491,7 +440,7 @@ print(response2.status_code) # 200
491440
```
492441
</CodeGroup>
493442

494-
When `allowPublicTraffic` is set to `false`, all requests to the sandbox's public URLs must include the `e2b-traffic-access-token` header with the value from `sandbox.trafficAccessToken`.
443+
When `allowPublicTraffic` / `allow_public_traffic` is set to a falsy value, all requests to the sandbox's public URLs must include the `e2b-traffic-access-token` header with the value from `sandbox.trafficAccessToken` / `sandbox.traffic_access_token`.
495444

496445
## Connecting to a server running inside the sandbox
497446
You can start a server inside the sandbox and connect to it using the approach above.
@@ -587,7 +536,7 @@ Response from server inside sandbox: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0
587536

588537
## Masking request host headers
589538

590-
You can customize the `Host` header that gets sent to services running inside the sandbox using the `maskRequestHost` option. This is useful when your application expects a specific host format.
539+
You can customize the `Host` header that gets sent to services running inside the sandbox using the `maskRequestHost` / `mask_request_host` option. This is useful when your application expects a specific host format.
591540

592541
<CodeGroup>
593542
```js JavaScript & TypeScript

0 commit comments

Comments
 (0)