OpenAICloudflare Developer PlatformApr 13, 2026, 12:00 AM

Containers, Agents - Secure credential injection and dynamic egress policies for Sandboxes

A condensed section focused on the key takeaways first.

Original Post

Quick Digest

Summary

A condensed section focused on the key takeaways first.

openaienmodel: gpt-5-mini-2025-08-07

Containers, Agents - Secure credential injection and dynamic egress policies for Sandboxes

Key Points

  • Zero-trust credential injection without exposing secrets
  • Per-instance ephemeral TLS interception with sandbox-trusted CA
  • Runtime-changeable egress policies via outbound handlers

Summary

Cloudflare added zero-trust outbound controls for Sandboxes and Containers: outbound Workers can inject credentials, perform TLS interception with per-instance ephemeral CAs, enforce allow/deny host lists, and apply dynamic per-instance egress policies. Secrets and ephemeral private keys never enter the sandbox; outbound handlers run outside the container runtime and attach auth transparently. Handlers can be changed at runtime without restarting the sandbox.

Key Points

  • Credential injection: outbound handlers run in the Workers runtime and can attach secrets to requests without exposing tokens to sandboxed workloads. Use ctx.containerId to fetch per-instance credentials.
  • TLS interception: each sandbox instance gets a unique ephemeral CA (trusted inside the sandbox); the private key never leaves the runtime sidecar, enabling transparent HTTPS proxying.
  • Host filtering: use allowedHosts (deny-by-default allowlist) and deniedHosts; both support glob patterns for matching.
  • Dynamic handlers: define named handlers in outboundHandlers and apply or change them at runtime with setOutboundHandler() or setOutboundByHost() to modify egress without restarting the sandbox.
  • Handler params: outbound handlers accept params so you can customize behavior per instance without creating new handler functions.
  • Upgrade: install @cloudflare/containers@0.3.0 or @cloudflare/sandbox@0.8.9 to enable these features.

Practical Notes for Engineers

  • Store and rotate secrets in the Worker environment; rotated secrets are picked up immediately by outbound handlers.
  • Use ctx.containerId to map instance-specific keys (e.g., per-repo or per-user credentials) and avoid embedding secrets in sandboxed code.
  • Apply a permissive handler for setup (e.g., allowHosts) and switch to a restrictive handler (e.g., noHttp) after initialization using await sandbox.setOutboundHandler().

Links

  • See docs: Sandbox outbound traffic and Container outbound traffic for full examples and API details.

Full Translation

Translations

A translation section that keeps the flow of the original article.

openaijamodel: gpt-5-mini-2025-08-07

Containers、Agents - サンドボックス向けの安全な資格情報注入と動的な送信ポリシー

サンドボックス向けの安全な資格情報注入と動的な送信(egress)ポリシー

Outbound Workers for Sandboxes and Containers は、ゼロトラストの資格情報注入、TLS インターセプション、許可/拒否リスト、およびインスタンスごとの動的な送信ポリシーをサポートするようになりました。これらの機能により、プラットフォームはユーザー生成コードやコーディングエージェントなどの信頼されていないワークロードに秘密情報を渡すことなく、サンドボックスから外部へ出ていく通信を完全に制御できます。

資格情報注入 (Credential injection)

Outbound ハンドラは Workers ランタイムで動作し、サンドボックスの外側にあるため、サンドボックスが直接見ることのない秘密情報を保持できます。サンドボックス内のワークロードは平文のリクエストを行い、リクエストが上流に転送される前に資格情報が透過的に付与されます。例えば、サンドボックス内でエージェントを動かし、そのエージェントが Github に対して行うリクエストを認証付きにすることができますが、エージェントが資格情報にアクセスすることは決してできません。

TypeScript
export class MySandbox extends Sandbox {}
MySandbox.outboundByHost = {
  "github.com": (request: Request, env: Env, ctx: OutboundHandlerContext) => {
    const requestWithAuth = new Request(request);
    requestWithAuth.headers.set("x-auth-token", env.SECRET);
    return fetch(requestWithAuth);
  },
};

コードの説明:

  • トークン(資格情報)はサンドボックス内に渡されません。
  • Worker 環境でシークレットをローテーションすれば、すべてのリクエストは即座に新しい資格情報を参照します。

インスタンスごとにユニークな資格情報を注入するには、ctx.containerId を使うと簡単です。

TypeScript
MySandbox.outboundByHost = {
  "my-internal-vcs.dev": async (request: Request, env: Env, ctx: OutboundHandlerContext) => {
    const authKey = await env.KEYS.get(ctx.containerId);
    const requestWithAuth = new Request(request);
    requestWithAuth.headers.set("x-auth-token", authKey);
    return fetch(requestWithAuth);
  },
};

TLS インターセプション (TLS interception)

Outbound Workers は HTTPS トラフィックをインターセプトできるようになりました。各サンドボックスインスタンスごとに一意のエフェメラル(短期)証明書局(CA)と秘密鍵が作成されます。CA はサンドボックス内に配置され、デフォルトで信頼されます。エフェメラルな秘密鍵はコンテナランタイムのサイドカープロセスから決して外に出ず、インスタンス間で共有されることはありません。

TLS インターセプションが有効な場合、Outbound Workers は HTTP と HTTPS の両方のトラフィックに対して透過的プロキシとして機能できます。

許可/拒否ホスト(Allow and deny hosts)

allowedHostsdeniedHosts を使ってアウトバウンドトラフィックを簡単にフィルタリングできます。allowedHosts が設定されると、デフォルトで拒否する allowlist(deny-by-default の allowlist)になります。両方のプロパティは glob パターンをサポートします。

TypeScript
export class MySandbox extends Sandbox {
  allowedHosts = [
    "github.com",
    "npmjs.org",
  ];
}

動的なアウトバウンドハンドラ(Dynamic outbound handlers)

名前付きのアウトバウンドハンドラを定義しておき、setOutboundHandler()setOutboundByHost() を使ってランタイムで適用・削除できます。これにより、サンドボックスを再起動することなく実行中のサンドボックスの送信ポリシーを変更できます。

TypeScript
export class MySandbox extends Sandbox {}
MySandbox.outboundHandlers = {
  allowHosts: async (req: Request, env: Env, ctx: OutboundHandlerContext) => {
    const url = new URL(req.url);
    if (ctx.params.allowedHostnames.includes(url.hostname)) {
      return fetch(req);
    }
    return new Response(null, { status: 403 });
  },
  noHttp: async () => {
    return new Response(null, { status: 403 });
  },
};

コードの説明:

  • ハンドラは名前付きで定義され、パラメータを受け取れるため、インスタンスごとに挙動をカスタマイズできます(ハンドラ関数を個別に定義する必要はありません)。

ワーカー側からプログラム的にハンドラを適用する例:

TypeScript
const sandbox = getSandbox(env.Sandbox, userId);
// セットアップ時にネットワークを開放
await sandbox.setOutboundHandler("allowHosts", {
  allowedHostnames: ["github.com", "npmjs.org"],
});
await sandbox.exec("npm install");
// セットアップ後にロックダウン
await sandbox.setOutboundHandler("noHttp");
  • ハンドラは params を受け取れるため、インスタンスごとの挙動を単一のハンドラで制御できます。

はじめに (Get started)

これらの機能を利用するには、@cloudflare/containers@0.3.0 または @cloudflare/sandbox@0.8.9 にアップグレードしてください。詳細は「Sandbox outbound traffic」と「Container outbound traffic」を参照してください。