Skip to content

Network

Every sandbox has an outbound policy. Archil enforces it outside the microVM, so code inside can’t change it, and it can add headers to requests as they leave, so a key the sandbox uses never sits inside it.

The policy is enforced outside the microVM

Section titled “The policy is enforced outside the microVM”
GET /usersandboxegress: denyeverything elseagentyour codeapi.github.comallowed · + Authorizationexample.comnot listedGET /usersandboxegress: denyeverything elseagentyour codeapi.github.comallowed · + Authorizationexample.comnot listed
With default: deny, a sandbox reaches only the hosts it allows. The policy adds the Authorization header to requests for api.github.com, so the token never enters the sandbox.
src/agent.ts
export const MySandbox = Archil.Sandbox("MySandbox", {
network: {
egress: { default: "deny", allow: ["registry.npmjs.org", "*.github.com"] },
},
});

Deny entries win over allow entries. A wildcard matches subdomains, not the apex domain.

An allow entry can set headers on matching HTTPS requests. The sandbox never sees the value:

src/agent.ts
export const MySandbox = Archil.Sandbox("MySandbox", {
network: {
egress: {
default: "deny",
allow: [
{ target: "api.github.com", headers: { Authorization: Config.Redacted("GITHUB_AUTH") } },
],
},
},
});

Archil has no secret store, so the value lives in the sandbox’s policy.

An allow entry can also forward matching requests to a URL of yours. Archil decrypts the request, applies the entry’s headers, and sends it to forwardUrl with the original path and query. It names the original destination and the calling sandbox in archil-forwarded-host, archil-forwarded-scheme, archil-forwarded-port, archil-forwarded-path and archil-sandbox-id headers.

forwarded · + tokencallssandboxegress: denyagentyour codeMyGatewayWorker · + x-api-keyapi.example.comreceives the keyforwarded · + tokencallssandboxegress: denyagentyour codeMyGatewayWorker · + x-api-keyapi.example.comreceives the key
The agent calls api.example.com as usual. Archil forwards the request to the gateway and adds the token that proves it came from this policy. The gateway adds the API key and calls the real host. Neither secret enters the sandbox.

Forward the API’s requests to the gateway

Section titled “Forward the API’s requests to the gateway”
alchemy.run.ts
const gateway = yield* MyGateway;
const sandbox = yield* Archil.Sandbox("MySandbox", {
network: {
egress: {
default: "deny",
allow: [
{
target: "api.example.com",
headers: { "X-Gateway-Token": Config.Redacted("GATEWAY_TOKEN") },
forwardUrl: gateway.url.as<string>(),
},
],
},
},
});

Requests for api.example.com now go to the gateway. Archil adds the gateway token on the way, so the gateway can tell they came through this policy.

src/Gateway.ts
export default class MyGateway extends Cloudflare.Worker<MyGateway>()(
"MyGateway",
{
main: import.meta.url,
env: {
GATEWAY_TOKEN: Config.Redacted("GATEWAY_TOKEN"),
API_KEY: Config.Redacted("API_KEY"),
},
},
Effect.succeed({ fetch: forward }),
) {}

A Cloudflare Worker makes a small gateway. It holds the token it expects and the API key it adds. forward is the handler the next steps build.

src/Gateway.ts
const request = yield* HttpServerRequest;
const env = yield* Cloudflare.Workers.WorkerEnvironment;
if (request.headers["x-gateway-token"] !== env.GATEWAY_TOKEN) {
return HttpServerResponse.text("forbidden", { status: 403 });
}

The gateway’s URL is public, so anyone can call it. Only Archil adds the token.

src/Gateway.ts
const host = request.headers["archil-forwarded-host"];
if (host === undefined || !["api.example.com"].includes(host)) {
return HttpServerResponse.text("host not allowed", { status: 403 });
}

Without the list, the gateway would add your key to requests for any host.

src/Gateway.ts
const { pathname, search } = new URL(request.originalUrl);
const client = yield* HttpClient.HttpClient;
const upstream = yield* client.execute(
HttpClientRequest.get(`https://${host}${pathname}${search}`).pipe(
HttpClientRequest.setHeader("x-api-key", env.API_KEY),
),
);
return HttpServerResponse.text(yield* upstream.text, { status: upstream.status });

Archil appends the original path and query to forwardUrl, so the gateway rebuilds the URL from them. For other methods, bodies and headers, copy them from request, leaving out the archil- headers and the token.

The gateway holds the API key, so rotating it changes only the Worker. archil-sandbox-id tells it which sandbox called, which it can use to log, rate-limit or pick a key per sandbox. forwardUrl must be a public HTTPS URL on a domain name, so a workers.dev URL or a custom domain works, and an IP address doesn’t.

yield* sandbox.setEgress({ default: "deny", allow: ["api.openai.com"] });

The new policy applies at once. On a declared Archil.Sandbox, changing network updates it in place.

On Archil’s Free plan, sandboxes reach only a managed allowlist of package registries, Git hosts and model APIs, and adding headers needs the Developer plan or above. A sandbox created on a paid plan with no policy reaches anything.