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
description: "Sandbox with automatic HTTP header injection on outbound requests"
4
4
---
5
5
6
-
This template runs a transparent [mitmproxy](https://mitmproxy.org/) inside the sandbox that automatically injects a custom `X-Sandbox-ID`header into every outbound HTTP and HTTPS request. This is useful for tracking, auditing, or routing requests that originate from specific sandboxes.
6
+
This template automatically tags every outbound HTTP and HTTPS request from the sandbox with a custom header containing the sandbox ID. No code changes needed — any request your agent makes to external APIs will carry the `X-Sandbox-ID` header transparently.
7
7
8
-
The template includes:
9
-
-**[mitmproxy](https://mitmproxy.org/)** running in transparent mode to intercept outbound traffic
10
-
-**iptables** rules that redirect HTTP/HTTPS traffic through the proxy
11
-
-**Automatic CA certificate trust** so HTTPS requests work without certificate errors in Python, Node.js, and OpenSSL
12
-
-**Socket marking** via an `LD_PRELOAD` library to prevent proxy redirect loops
8
+
This enables you to:
9
+
-**Track API usage per sandbox** — correlate external API calls back to the sandbox that made them
10
+
-**Route traffic** — use the header in your API gateway or proxy to apply per-sandbox rate limits, logging, or access policies
11
+
-**Audit agent behavior** — see exactly which external services each sandbox is calling
13
12
14
-
## Template definition
13
+
## How it works
15
14
16
-
The template installs mitmproxy with its dependencies, copies the proxy addon and startup scripts, and compiles a small C library used to prevent redirect loops. The start command launches the proxy and waits for it to signal readiness.
15
+
A transparent proxy runs inside the sandbox and intercepts all outbound HTTP/HTTPS traffic. It injects an `X-Sandbox-ID` header into every request before forwarding it to the destination. The sandbox's CA certificate is automatically trusted so HTTPS works without any configuration in Python, Node.js, or any other runtime.
The mitmproxy addon reads the sandbox ID from `/run/e2b/.E2B_SANDBOX_ID` (written by E2B at sandbox boot) and injects it as a header into every outbound request. The header name defaults to `X-Sandbox-ID` but can be configured via the `HEADER_NAME` environment variable.
# Cache only once we have a real value; otherwise retry next request.
107
-
if sid and sid !="unknown":
108
-
_sandbox_id = sid
109
-
else:
110
-
sid ="unknown"
111
-
112
-
flow.request.headers[HEADER_NAME] = sid
113
-
```
114
-
115
-
## Socket marking library
116
-
117
-
To prevent redirect loops, mitmproxy's own outbound connections need to bypass the iptables redirect rules. This small C library is loaded via `LD_PRELOAD` and marks all sockets mitmproxy creates with `SO_MARK=1`. The iptables rules then skip any packets with that mark.
118
-
119
-
```c sockmark.c
120
-
#define_GNU_SOURCE
121
-
#include<sys/socket.h>
122
-
#include<dlfcn.h>
123
-
124
-
intsocket(int domain, int type, int protocol) {
125
-
int (*real_socket)(int, int, int) =dlsym(RTLD_NEXT, "socket");
126
-
intfd=real_socket(domain, type, protocol);
127
-
if (fd>=0&& (domain==AF_INET||domain==AF_INET6)) {
The startup script runs when the sandbox starts. It fetches the sandbox ID from the instance metadata service, generates and installs the mitmproxy CA certificate, configures iptables to redirect HTTP (port 80) and HTTPS (port 443) traffic through the proxy, and launches mitmproxy in transparent mode.
138
-
139
-
```bash start-proxy.sh expandable
140
-
#!/usr/bin/env bash
141
-
set -euo pipefail
142
-
143
-
PROXY_PORT="${PROXY_PORT:-8080}"
144
-
ADDON_PATH="/opt/mitmproxy/add_header.py"
145
-
SOCKMARK_LIB="/opt/mitmproxy/sockmark.so"
146
-
CONFDIR="/root/.mitmproxy"
147
-
148
-
echo "=== Egress Header Proxy Setup ==="
149
-
echo "Proxy port: ${PROXY_PORT}"
150
-
151
-
# --- 0. Resolve sandbox ID from MMDS (Firecracker metadata service) ---
152
-
MMDS_TOKEN=$(curl -sf -X PUT "http://169.254.169.254/latest/api/token" \
Create a sandbox from the template and make a request to [httpbin.org](https://httpbin.org) to confirm the `X-Sandbox-ID` header is being injected into outbound requests.
The full template source including the proxy addon, startup script, and build configuration is available on [GitHub](https://github.com/beran-t/customer-starter-templates/tree/main/templates/sandbox-egress-header).
0 commit comments