Skip to content

Commit e4f5274

Browse files
Add network configuration documentation for PR #1016 (#60)
Co-authored-by: mintlify[bot] <109931778+mintlify[bot]@users.noreply.github.com> Co-authored-by: Jakub Dobry <[email protected]>
1 parent 43c8949 commit e4f5274

File tree

1 file changed

+217
-4
lines changed

1 file changed

+217
-4
lines changed

docs/sandbox/internet-access.mdx

Lines changed: 217 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,113 @@ isolated_sandbox = Sandbox.create(allow_internet_access=False)
3131

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

34+
<Note>
35+
Setting `allowInternetAccess` to `false` is equivalent to setting `network.denyOut` to `['0.0.0.0/0']` (denying all traffic).
36+
</Note>
37+
38+
## Fine-grained network control
39+
40+
For more granular control over network access, you can use the `network` configuration option to specify allow and deny lists for outbound traffic.
41+
42+
### Allow and deny lists
43+
44+
You can specify IP addresses or CIDR blocks that the sandbox can or cannot connect to:
45+
46+
<CodeGroup>
47+
```js JavaScript & TypeScript
48+
import { Sandbox, ALL_TRAFFIC } from '@e2b/code-interpreter'
49+
50+
// Deny all traffic except specific IPs
51+
const sandbox = await Sandbox.create({
52+
network: {
53+
denyOut: [ALL_TRAFFIC],
54+
allowOut: ['1.1.1.1', '8.8.8.0/24']
55+
}
56+
})
57+
58+
// Deny specific IPs only
59+
const restrictedSandbox = await Sandbox.create({
60+
network: {
61+
denyOut: ['8.8.8.8']
62+
}
63+
})
64+
```
65+
```python Python
66+
from e2b_code_interpreter import Sandbox, ALL_TRAFFIC
67+
68+
# Deny all traffic except specific IPs
69+
sandbox = Sandbox.create(
70+
network={
71+
"deny_out": [ALL_TRAFFIC],
72+
"allow_out": ["1.1.1.1", "8.8.8.0/24"]
73+
}
74+
)
75+
76+
# Deny specific IPs only
77+
restricted_sandbox = Sandbox.create(
78+
network={
79+
"deny_out": ["8.8.8.8"]
80+
}
81+
)
82+
```
83+
</CodeGroup>
84+
85+
### Priority rules
86+
87+
When both `allowOut` and `denyOut` are specified, **allow rules always take precedence** over deny rules. This means if an IP address is in both lists, it will be allowed.
88+
89+
<CodeGroup>
90+
```js JavaScript & TypeScript
91+
import { Sandbox, ALL_TRAFFIC } from '@e2b/code-interpreter'
92+
93+
// Even though ALL_TRAFFIC is denied, 1.1.1.1 and 8.8.8.8 are explicitly allowed
94+
const sandbox = await Sandbox.create({
95+
network: {
96+
denyOut: [ALL_TRAFFIC],
97+
allowOut: ['1.1.1.1', '8.8.8.8']
98+
}
99+
})
100+
```
101+
```python Python
102+
from e2b_code_interpreter import Sandbox, ALL_TRAFFIC
103+
104+
# Even though ALL_TRAFFIC is denied, 1.1.1.1 and 8.8.8.8 are explicitly allowed
105+
sandbox = Sandbox.create(
106+
network={
107+
"deny_out": [ALL_TRAFFIC],
108+
"allow_out": ["1.1.1.1", "8.8.8.8"]
109+
}
110+
)
111+
```
112+
</CodeGroup>
113+
114+
### ALL_TRAFFIC helper
115+
116+
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:
117+
118+
<CodeGroup>
119+
```js JavaScript & TypeScript
120+
import { Sandbox, ALL_TRAFFIC } from '@e2b/code-interpreter'
121+
122+
// Deny all outbound traffic
123+
const sandbox = await Sandbox.create({
124+
network: {
125+
denyOut: [ALL_TRAFFIC]
126+
}
127+
})
128+
```
129+
```python Python
130+
from e2b_code_interpreter import Sandbox, ALL_TRAFFIC
131+
132+
# Deny all outbound traffic
133+
sandbox = Sandbox.create(
134+
network={
135+
"deny_out": [ALL_TRAFFIC]
136+
}
137+
)
138+
```
139+
</CodeGroup>
140+
34141
## Sandbox public URL
35142
Every sandbox has a public URL that can be used to access running services inside the sandbox.
36143

@@ -68,6 +175,76 @@ https://3000-i62mff4ahtrdfdkyn2esc.e2b.app
68175

69176
The first leftmost part of the host is the port number we passed to the method.
70177

178+
## Restricting public access to sandbox URLs
179+
180+
By default, sandbox URLs are publicly accessible. You can restrict access to require authentication using the `allowPublicTraffic` option:
181+
182+
<CodeGroup>
183+
```js JavaScript & TypeScript
184+
import { Sandbox } from '@e2b/code-interpreter'
185+
186+
// Create sandbox with restricted public access
187+
const sandbox = await Sandbox.create({
188+
network: {
189+
allowPublicTraffic: false
190+
}
191+
})
192+
193+
// The sandbox has a traffic access token
194+
console.log(sandbox.trafficAccessToken)
195+
196+
// Start a server inside the sandbox
197+
await sandbox.commands.run('python -m http.server 8080', { background: true })
198+
199+
const host = sandbox.getHost(8080)
200+
const url = `https://${host}`
201+
202+
// Request without token will fail with 403
203+
const response1 = await fetch(url)
204+
console.log(response1.status) // 403
205+
206+
// Request with token will succeed
207+
const response2 = await fetch(url, {
208+
headers: {
209+
'e2b-traffic-access-token': sandbox.trafficAccessToken
210+
}
211+
})
212+
console.log(response2.status) // 200
213+
```
214+
```python Python
215+
import requests
216+
from e2b_code_interpreter import Sandbox
217+
218+
# Create sandbox with restricted public access
219+
sandbox = Sandbox.create(
220+
network={
221+
"allow_public_traffic": False
222+
}
223+
)
224+
225+
# The sandbox has a traffic access token
226+
print(sandbox.traffic_access_token)
227+
228+
# Start a server inside the sandbox
229+
sandbox.commands.run("python -m http.server 8080", background=True)
230+
231+
host = sandbox.get_host(8080)
232+
url = f"https://{host}"
233+
234+
# Request without token will fail with 403
235+
response1 = requests.get(url)
236+
print(response1.status_code) # 403
237+
238+
# Request with token will succeed
239+
response2 = requests.get(url, headers={
240+
'e2b-traffic-access-token': sandbox.traffic_access_token
241+
})
242+
print(response2.status_code) # 200
243+
```
244+
</CodeGroup>
245+
246+
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`.
247+
71248
## Connecting to a server running inside the sandbox
72249
You can start a server inside the sandbox and connect to it using the approach above.
73250

@@ -94,6 +271,7 @@ console.log('Response from server inside sandbox:', data);
94271
await process.kill()
95272
```
96273
```python Python
274+
import requests
97275
from e2b_code_interpreter import Sandbox
98276

99277
sandbox = Sandbox.create()
@@ -105,16 +283,15 @@ url = f"https://{host}"
105283
print('Server started at:', url)
106284

107285
# Fetch data from the server inside the sandbox.
108-
response = sandbox.commands.run(f"curl {url}")
109-
data = response.stdout
110-
print("Response from server inside sandbox:", data)
286+
response = requests.get(url)
287+
data = response.text
288+
print('Response from server inside sandbox:', data)
111289

112290
# Kill the server process inside the sandbox.
113291
process.kill()
114292
```
115293
</CodeGroup>
116294

117-
118295
This output will look like this:
119296
<CodeGroup>
120297
```bash JavaScript & TypeScript
@@ -158,3 +335,39 @@ Response from server inside sandbox: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0
158335
</html>
159336
```
160337
</CodeGroup>
338+
339+
340+
## Masking request host headers
341+
342+
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.
343+
344+
<CodeGroup>
345+
```js JavaScript & TypeScript
346+
import { Sandbox } from '@e2b/code-interpreter'
347+
348+
// Create sandbox with custom host masking
349+
const sandbox = await Sandbox.create({
350+
network: {
351+
maskRequestHost: 'localhost:${PORT}'
352+
}
353+
})
354+
355+
// The ${PORT} variable will be replaced with the actual port number
356+
// Requests to the sandbox will have Host header set to for example: localhost:8080
357+
```
358+
```python Python
359+
from e2b_code_interpreter import Sandbox
360+
361+
# Create sandbox with custom host masking
362+
sandbox = Sandbox.create(
363+
network={
364+
"mask_request_host": "localhost:${PORT}"
365+
}
366+
)
367+
368+
# The ${PORT} variable will be replaced with the actual port number
369+
# Requests to the sandbox will have Host header set to for example: localhost:8080
370+
```
371+
</CodeGroup>
372+
373+
The `${PORT}` variable in the mask will be automatically replaced with the actual port number of the requested service.

0 commit comments

Comments
 (0)