Outbound Handler Egress Control: Credentials, Allowlists, and HTTPS Interception
How Cloudflare Containers, Sandboxes, and dynamic Workers restrict outbound HTTP, inject secrets safely, and intercept HTTPS without exposing tokens.
This page summarises Cloudflare documentation on controlling what untrusted code in a Container, Sandbox, or dynamic Worker can reach on the network. The pattern is the same everywhere: keep secrets and policy in your Worker, run user or agent code in an isolated environment, and route fetch() (and related traffic) through outbound handlers or egress gateways you trust.
Why outbound handlers exist: credentials the sandbox must not see
Section titled “Why outbound handlers exist: credentials the sandbox must not see”Any process inside a sandbox can read values you pass in—environment variables, command arguments, files, and URLs such as a Git clone URL stored in .git/config. Cloudflare’s Sandbox security docs illustrate the risk: code from a test dependency can read a token from the repository config and exfiltrate it wherever the sandbox can connect.
Any process in a sandbox can read a token that the sandbox holds. Keep the token in your Worker instead, and give the sandbox only the access it needs.
— Cloudflare, Credentials and network access
Outbound handlers run in the Workers runtime, outside the container or sandbox. The isolated workload sends a plain HTTP or HTTPS request; your handler decides whether to allow it, modifies it (for example by adding Authorization), and forwards it. The workload never receives the secret.
Because outbound handlers run in the Workers runtime, outside the sandbox, they can hold secrets the sandbox never sees. A sandboxed workload can make a plain request, and credentials are transparently attached before a request is forwarded upstream.
— Cloudflare, Handle outbound traffic
The same idea applies to dynamic Workers loaded through a loader: set globalOutbound on the code object so every fetch() and connect() from untrusted code passes through a WorkerEntrypoint gateway—or set globalOutbound to null to block the network entirely. See Egress control and API reference for globalOutbound.
enableInternet: default deny versus open egress
Section titled “enableInternet: default deny versus open egress”For Containers and Sandboxes, enableInternet = false blocks public Internet access by default. Only destinations you allow through allowedHosts, deniedHosts, or outbound handlers can leave the instance. Only ports 80 and 443 are available for that controlled HTTP(S) path; other ports behave according to your settings (with enableInternet: false, non-HTTP traffic is denied).
Use
enableInternet = falseto block public internet access by default… WhenenableInternetis false, only traffic thatallowedHostsor an outbound handler allows can leave the container.— Cloudflare, Handle outbound traffic (Containers)
enableInternet takes effect when the container or sandbox starts. Handler and allowlist changes can apply to a running instance without restart (see runtime setters below). An already-running instance keeps the Internet mode it started with.
allowedHosts and deniedHosts: host allowlists and blocklists
Section titled “allowedHosts and deniedHosts: host allowlists and blocklists”You can declare host filters on the Container or Sandbox class. When allowedHosts is set, policy becomes deny-by-default: only listed hosts (or globs with *) continue toward handlers or the public Internet. deniedHosts is checked first and blocks matching destinations immediately.
| Mechanism | Effect when set |
|---|---|
deniedHosts |
Matching host or IP denied before handlers |
allowedHosts |
Deny-by-default; only matches proceed |
enableInternet: false |
No public egress except what you explicitly allow |
enableInternet: true |
Unmatched traffic may egress if not denied |
Both list types support simple glob patterns where * matches any sequence of characters.
For Containers with interceptHttps = true, deniedHosts can apply to HTTPS on port 443 as well as HTTP on port 80. With enableInternet: true, traffic on other ports can bypass deniedHosts; to block a destination on every port, use enableInternet: false and allow only what you need. Details are in Handle outbound traffic (Containers).
Export ContainerProxy from your Worker entrypoint so outbound interception is wired correctly—the Sandbox SDK outbound guide repeats the same requirement for sandboxes.
outbound and outboundByHost: programmable egress proxies
Section titled “outbound and outboundByHost: programmable egress proxies”Outbound handlers are programmable egress proxies colocated with the container. They see all Workers bindings in env, so one hostname can mean “call KV” instead of “call the public Internet.”
outbound: catch-all for HTTP on port 80 and (when HTTPS interception is enabled) HTTPS on port 443.outboundByHost: per-hostname (or IP) handlers; these take precedence over the catch-all.
Use
outboundto intercept HTTP requests on port 80 and HTTPS requests on port 443… UseoutboundByHostto map specific domain names or IP addresses to proxy functions.— Cloudflare, Handle outbound traffic (Containers)
From inside the container, a call like http://my.kv/some-key can resolve to a handler that runs env.KV.get—no SDK inside the sandbox. See Connect to Workers and Bindings (Containers) and the Sandbox bindings guide.
Evaluation order (summary from the same outbound-traffic pages):
deniedHostsallowedHosts(if set)- Instance rules from
setOutboundByHost() - Class-level
outboundByHost - Instance
setOutboundHandler()override - Class-level
outbound - Direct Internet egress only if
allowedHostsorenableInternetpermits it
Restricting a coding agent to AI Gateway and GitHub
Section titled “Restricting a coding agent to AI Gateway and GitHub”The Build a coding agent runner guide implements an Outbound WorkerEntrypoint that allows only HTTPS to gateway.ai.cloudflare.com (under your account path) and github.com. Everything else returns 403. The handler strips a placeholder x-api-key from the agent and sets cf-aig-authorization with the real gateway token so Anthropic never receives the wrong credential.
Call an authenticated API without giving the sandbox the token
Section titled “Call an authenticated API without giving the sandbox the token”Call an authenticated API from a sandbox shows a GitHubGateway entrypoint that allows only GET https://api.github.com/user with an empty query string; any other request gets Forbidden. The Durable Object calls interceptOutboundHttps("api.github.com", …) and starts the container with enableInternet: false. Container code runs a normal fetch("https://api.github.com/user"); the gateway adds Authorization: Bearer from env.GITHUB_TOKEN.
Credential injection: patterns and per-instance keys
Section titled “Credential injection: patterns and per-instance keys”Class-level outboundByHost handlers attach static secrets from env. For per-sandbox credentials, use ctx.containerId in the handler to look up a key (for example from KV) before forwarding—described in the credential injection changelog and Container outbound traffic.
No token is ever passed into the sandbox. You can rotate secrets in the Worker environment and every request will pick them up immediately.
— Cloudflare, Secure credential injection and dynamic egress policies for Sandboxes
Dynamic Workers use the same injection model through globalOutbound and ctx.props on a gateway entrypoint; see Egress control — Inject credentials.
Workers for Platforms can attach a platform-wide Outbound Worker to intercept customer Workers’ fetch() calls (logging, allowlists, platform JWTs). Configuration is in Outbound Workers (Workers for Platforms). Note: when an Outbound Worker is enabled, customer Workers cannot use connect() for raw TCP—all outbound communication must go through the Outbound Worker’s fetch.
HTTPS interception: ephemeral CA and trusting /etc/cloudflare/certs/cloudflare-containers-ca.crt
Section titled “HTTPS interception: ephemeral CA and trusting /etc/cloudflare/certs/cloudflare-containers-ca.crt”By default, Container HTTPS is not intercepted until you set interceptHttps = true and define outbound or outboundByHost. Then the platform creates an ephemeral CA at /etc/cloudflare/certs/cloudflare-containers-ca.crt per instance.
Outbound Workers now intercept HTTPS traffic. A unique ephemeral certificate authority (CA) and private key are created for each sandbox instance. The CA is placed into the sandbox and trusted by default. The ephemeral private key never leaves the container runtime sidecar process and is never shared across instances.
— Cloudflare, Changelog — credential injection and TLS
For Containers, you may need to copy the CA into the distro trust store in your entrypoint before starting the app (Trust the CA certificate). Runtimes that use their own bundle (Node.js, curl, Git) should set variables such as NODE_EXTRA_CA_CERTS, CURL_CA_BUNDLE, and GIT_SSL_CAINFO—the coding-agent and authenticated-API examples pass these into exec() / startup.
The Sandbox 1.0 migration guide Move outbound rules documents a TRUST shell step that waits for the CA file, merges it into the system bundle, and fails startup if trust cannot be established—important because each container gets a new certificate and snapshots must not accumulate old CAs.
interceptOutboundHttp, interceptOutboundHttps, and limits
Section titled “interceptOutboundHttp, interceptOutboundHttps, and limits”Lower-level control lives on ctx.container in the Durable Object: register a WorkerEntrypoint Fetcher per hostname, glob, IP, port, or CIDR. See Durable Object Container API.
interceptOutboundHttp: HTTP on port 80 for matching targets.interceptOutboundHttps: HTTPS (default port 443; optional port in the target string).interceptAllOutboundHttp: all HTTP on port 80; does not cover HTTPS—also registerinterceptOutboundHttps("*")if you need all HTTPS.
With enableInternet: false, DNS for intercepted hostnames returns a placeholder address that routes to your intercept; other lookups time out. Register hostname-specific intercepts before interceptAllOutboundHttp() so they still receive traffic.
Intercept budget: a container shares 128 intercept entries across these APIs. A hostname (or glob) uses two entries (IPv4 and IPv6); IP/CIDR uses one. Exceeding the limit throws You can't configure more than 128 egress interceptors. You cannot remove an intercept while the container runs; for changing hostname sets, use interceptAllOutboundHttp() or * and decide policy inside one entrypoint.
Sandbox SDK 1.0 replaces static outbound / outboundByHost class fields with a single WorkerEntrypoint registered via interceptOutboundHttp / interceptOutboundHttps, as described in Move outbound rules and the API map.
Dynamic egress at runtime: setOutboundHandler and setOutboundByHost
Section titled “Dynamic egress at runtime: setOutboundHandler and setOutboundByHost”Named handlers in outboundHandlers can be attached or removed on a live sandbox without restart—for example allow GitHub and npm during npm install, then lock down with a noHttp handler.
Define named outbound handlers then apply or remove them at runtime using
setOutboundHandler()orsetOutboundByHost(). This lets you change egress policy for a running sandbox without restarting it.— Cloudflare, Changelog — dynamic outbound handlers
Containers support the same pattern via setOutboundByHost(), setAllowedHosts(), allowHost(), and related APIs in Handle outbound traffic (Containers). Re-registering intercepts or entrypoints updates policy on open connections without dropping them (Durable Object Container API).
globalOutbound for dynamic Workers: block, redirect, or inherit
Section titled “globalOutbound for dynamic Workers: block, redirect, or inherit”From the dynamic Workers API reference:
- Unspecified: inherit parent network access (usually full Internet).
null:fetch()andconnect()throw; fully isolated.- Service binding /
ctx.exportsentrypoint: every outbound call goes through your gateway (restrict hosts, inject credentials, log).
Using ctx.exports lets you pass per-sandbox props (for example tenant id) into the gateway when the dynamic Worker is created.
Common mistakes
Section titled “Common mistakes”- Putting API tokens in clone URLs, env vars, or files inside the sandbox—any code there can read and exfiltrate them (Sandbox security).
- Leaving
enableInternet: truewhile relying on an outbound handler—traffic not covered by an intercept still egresses directly (Sandbox security — Every opening is also a way out). - Forgetting
interceptHttps(Containers) or HTTPS intercept registration (Sandbox 1.0)—allowlists and handlers may apply only to HTTP while HTTPS bypasses policy (Sandbox SDK outbound guide). - Registering hostname intercepts after
interceptAllOutboundHttp()—the catch-all consumes those requests (Durable Object Container API; DirectoryBackupintercept()must run before a catch-all or backups fail withBACKUP_TRANSFER). - Leaving agent placeholder keys on requests to AI Gateway—the gateway may forward them upstream and break authentication (Build a coding agent runner).
- Using HTTPS to a bare IP with
enableInternet: falseand a*HTTPS intercept—such requests fail; use a hostname (Move outbound rules — What changes for requests).
How to check this on your site
Section titled “How to check this on your site”-
Deploy the Worker that defines your outbound entrypoint or sandbox class, following Call an authenticated API from a sandbox (
npx wrangler deploy,yarn wrangler deploy, orpnpm wrangler deploy). -
Hit a test route that exercises sandbox egress—for example
curl https://<YOUR_WORKER>.<YOUR_SUBDOMAIN>.workers.dev/usernameand confirm the JSON response matches an allowed upstream call. -
From inside the sandbox (or via your agent), request a host you blocked and confirm you receive the documented denial (for example
403,Origin is disallowedwith status 520 after a 1.0 migration, oris not reachable from this sandboxfrom the coding-agentOutboundexample). -
Request an allowed host over HTTPS (for example
git ls-remote https://github.com/octocat/Hello-World HEADas in Move outbound rules — Check the rules) without disabling certificate verification—this confirms the intercept CA is trusted. -
You can check automatically in Answerwell (our tool): https://answerwell.org/workspace
Report an error or ask a question
Section titled “Report an error or ask a question”If something on this page is wrong or unclear, email yimasynthetic@gmail.com with the page URL and the exact statement you are asking about so it can be corrected.