Skip to content

Commit 2577c8d

Browse files
docs: per-host network rules and selector callbacks (#227)
* docs: document per-host network rules and selector callbacks * fixes
1 parent bcd712b commit 2577c8d

1 file changed

Lines changed: 93 additions & 0 deletions

File tree

docs/sandbox/internet-access.mdx

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,99 @@ sandbox = Sandbox.create(
229229
```
230230
</CodeGroup>
231231

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+
await Sandbox.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": lambda ctx: 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`:
279+
280+
```js JavaScript & TypeScript
281+
const rules = new Map([
282+
['api.example.com', [{ transform: { headers: { 'X-Trace': 'on' } } }]],
283+
])
284+
285+
await Sandbox.create({
286+
network: { allowOut: ({ rules }) => [...rules.keys()], rules },
287+
})
288+
```
289+
290+
### 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`.
296+
297+
<CodeGroup>
298+
```js JavaScript & TypeScript
299+
import { Sandbox } from 'e2b'
300+
301+
// Block all egress except an explicit allowlist
302+
await Sandbox.create({
303+
network: {
304+
denyOut: ({ allTraffic }) => [allTraffic], // allTraffic === '0.0.0.0/0'
305+
allowOut: ['1.1.1.1', '8.8.8.0/24'],
306+
},
307+
})
308+
```
309+
```python Python
310+
from e2b import Sandbox
311+
312+
Sandbox.create(
313+
network={
314+
"deny_out": lambda ctx: [ctx.all_traffic],
315+
"allow_out": ["1.1.1.1", "8.8.8.0/24"],
316+
},
317+
)
318+
```
319+
</CodeGroup>
320+
321+
<Note>
322+
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+
232325
### Updating network settings on a running sandbox
233326

234327
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

Comments
 (0)