You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/sandbox/internet-access.mdx
+93Lines changed: 93 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -229,6 +229,99 @@ sandbox = Sandbox.create(
229
229
```
230
230
</CodeGroup>
231
231
232
+
### Per-host request transforms
233
+
234
+
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`.
235
+
236
+
The `transform.headers` object is sent on the wire as-is and injected by the egress proxy on matching HTTP/HTTPS requests.
237
+
238
+
<CodeGroup>
239
+
```js JavaScript & TypeScript
240
+
import { Sandbox } from'e2b'
241
+
242
+
awaitSandbox.create({
243
+
network: {
244
+
// Only allow egress to hosts that have rules registered.
245
+
allowOut: ({ rules }) => [...rules.keys()],
246
+
rules: {
247
+
'api.example.com': [
248
+
{
249
+
transform: {
250
+
headers: { 'X-Header':'Content' },
251
+
},
252
+
},
253
+
],
254
+
},
255
+
},
256
+
})
257
+
```
258
+
```python Python
259
+
from e2b import Sandbox
260
+
261
+
sandbox = Sandbox.create(
262
+
network={
263
+
"allow_out": lambdactx: list(ctx.rules.keys()),
264
+
"rules": {
265
+
"api.example.com": [
266
+
{
267
+
"transform": {
268
+
"headers": {"X-Header": "Content"},
269
+
},
270
+
},
271
+
],
272
+
},
273
+
},
274
+
)
275
+
```
276
+
</CodeGroup>
277
+
278
+
In JavaScript, `network.rules` accepts either a plain object or a `Map`:
### Selector callbacks for `allowOut` and `denyOut`
291
+
292
+
`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`.
293
+
294
+
-`allTraffic` (JS) / `ctx.all_traffic` (Python) is the literal `'0.0.0.0/0'`.
295
+
-`rules` is a `Map` (Python `Mapping`) view of `network.rules`.
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.
323
+
</Note>
324
+
232
325
### Updating network settings on a running sandbox
233
326
234
327
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.
0 commit comments