Secure, Fast, and Extensible Sandbox runtime for AI agents.
Merge remote-tracking branch 'origin/main' into fix/js-sdk-redirect-credential-leak
LiuZhangHaoa084e40153d8
diff --cc docs/sdks/javascript.mdindex e73d868cf,5cf656e54..e85b04229--- a/docs/sdks/javascript.md+++ b/docs/sdks/javascript.md@@@ -516,35 -516,12 +516,41 @@@ const config2 = new ConnectionConfig(});```+#### Redirects and protected headers++The SDK strips OpenSandbox credentials before following a cross-origin redirect.+Same-origin redirects preserve request headers, while cross-origin redirects remove+every header whose name starts with `OPEN-SANDBOX-` or `OPENSANDBOX-`+(case-insensitive), such as `OPEN-SANDBOX-API-KEY` and `OPENSANDBOX-EGRESS-AUTH`. In+addition, the standard credential headers `Authorization`, `Cookie`, and+`Proxy-Authorization` are removed, matching native `fetch`. An origin is the+combination of scheme, host, and port, so changing any of those values is+cross-origin. Other custom headers are not stripped automatically. Requests that do+not carry any protected header keep the runtime's default redirect behavior.++When a guarded request is redirected, the request timeout applies as a single budget+for the whole chain rather than per hop: the deadline starts with the first dispatch,+and the caller's abort signal (plus any fetch options set on the `Request`) is carried+across every hop.++File uploads never follow redirects, because a streamed or in-memory multipart body+cannot always be replayed. A redirect response from an upload is surfaced as a+`SandboxApiException` with the original status code on Node.js; in browsers the+opaque redirect reports status `0`.++::: warning Browser limitation+Browsers do not expose the target of a `redirect: "manual"` response, so the SDK+cannot inspect it before following. For a request that carries protected headers, the+SDK fails closed (throws a `SandboxApiException`) instead of risking a cross-origin+credential replay. Node.js is unaffected.+:::++ The JavaScript Code Interpreter SDK inherits the sandbox's connection+ configuration. With `useServerProxy: true`, context requests and streamed code+ execution include the configured API key in `OPEN-SANDBOX-API-KEY` for server+ authentication. In direct mode, the SDK does not attach this key to code+ execution requests.+### 2. Sandbox Creation Configuration`Sandbox.create()` allows configuring the sandbox environment.diff --cc sdks/code-interpreter/javascript/src/factory/defaultAdapterFactory.tsindex bac0485cb,64f82f102..b81bfa3ef--- a/sdks/code-interpreter/javascript/src/factory/defaultAdapterFactory.ts+++ b/sdks/code-interpreter/javascript/src/factory/defaultAdapterFactory.ts@@@ -23,14 -22,14 +26,18 @@@ importexport class DefaultAdapterFactory implements AdapterFactory {createCodes(opts: CreateCodesStackOptions): Codes {- const headers: Record<string, string> = {- ...(opts.sandbox.connectionConfig.headers ?? {}),- ...(opts.endpointHeaders ?? {}),- };+ // The tenant API key only travels with server-proxied requests; execd on the+ // sandbox endpoint performs no authentication.+ const headers = createDataPlaneHeaders(+ opts.sandbox.connectionConfig.headers ?? {},+ opts.endpointHeaders,+ opts.sandbox.connectionConfig.useServerProxy,+ opts.sandbox.connectionConfig.apiKey,+ );+ // Strip protected OpenSandbox headers before following a cross-origin+ // redirect, matching the sandbox SDK (issue #2170).+ const execdFetch = createRedirectSafeExecdFetch(opts.sandbox, opts.execdBaseUrl);+ const execdSseFetch = createRedirectSafeExecdSseFetch(opts.sandbox, opts.execdBaseUrl);const client = createExecdClient({baseUrl: opts.execdBaseUrl,headers,diff --cc sdks/code-interpreter/javascript/tests/redirect-headers.test.mjsindex e88edc167,000000000..8afaac356mode 100644,000000..100644--- a/sdks/code-interpreter/javascript/tests/redirect-headers.test.mjs+++ b/sdks/code-interpreter/javascript/tests/redirect-headers.test.mjs@@@ -1,155 -1,0 +1,160 @@@+// Copyright 2026 The OpenSandbox Authors+//+// Licensed under the Apache License, Version 2.0 (the "License");+// you may not use this file except in compliance with the License.+// You may obtain a copy of the License at+//+// http://www.apache.org/licenses/LICENSE-2.0+//+// Unless required by applicable law or agreed to in writing, software+// distributed under the License is distributed on an "AS IS" BASIS,+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+// See the License for the specific language governing permissions and+// limitations under the License.++import assert from "node:assert/strict";+import test from "node:test";++import { DefaultAdapterFactory } from "../dist/index.js";++test("code-interpreter requests strip protected headers on cross-origin redirects", async () => {+ const calls = [];+ const fetchImpl = async (input, init = {}) => {+ const request = input instanceof Request ? input : new Request(input, init);+ calls.push({+ url: request.url,+ headers: Object.fromEntries(request.headers.entries()),+ });++ if (new URL(request.url).origin === "http://sandbox.internal:3456") {+ return new Response(null, {+ status: 302,+ headers: { location: "http://other.example/final" },+ });+ }+ return new Response(JSON.stringify({ id: "ctx-1", language: "python" }), {+ status: 200,+ headers: { "content-type": "application/json" },+ });+ };++ const sandbox = {+ connectionConfig: {+ headers: { "x-global": "global" },++ // Server-proxied mode is the deployment where the tenant API key rides++ // along; direct execd requests never carry it.++ useServerProxy: true,+ fetch: fetchImpl,+ sseFetch: fetchImpl,+ },+ };++ const codes = new DefaultAdapterFactory().createCodes({+ sandbox,+ execdBaseUrl: "http://sandbox.internal:3456",+ endpointHeaders: { "OPEN-SANDBOX-API-KEY": "tenant-secret" },+ });++ const context = await codes.createContext("python");+ assert.equal(context.id, "ctx-1");++ assert.equal(calls.length, 2);+ // The configured endpoint still receives the protected credential...+ assert.equal(calls[0].headers["open-sandbox-api-key"], "tenant-secret");+ // ...while the cross-origin redirect target does not.+ assert.equal(calls[1].url, "http://other.example/final");+ assert.equal(calls[1].headers["open-sandbox-api-key"], undefined);+});++test("streaming run() strips protected headers on cross-origin redirects", async () => {+ const calls = [];+ const fetchImpl = async (input, init = {}) => {+ const request = input instanceof Request ? input : new Request(input, init);+ calls.push({+ url: request.url,+ headers: Object.fromEntries(request.headers.entries()),+ });++ if (new URL(request.url).origin === "http://sandbox.internal:3456") {+ return new Response(null, {+ status: 302,+ headers: { location: "http://other.example/stream" },+ });+ }+ return new Response(+ [+ JSON.stringify({ type: "stdout", text: "hi", timestamp: 1 }),+ JSON.stringify({ type: "execution_complete", execution_time: 1, timestamp: 2 }),+ ].join("\n"),+ { status: 200, headers: { "content-type": "text/event-stream" } },+ );+ };++ const sandbox = {+ connectionConfig: {+ headers: {},++ useServerProxy: true,+ fetch: fetchImpl,+ sseFetch: fetchImpl,+ },+ };++ const codes = new DefaultAdapterFactory().createCodes({+ sandbox,+ execdBaseUrl: "http://sandbox.internal:3456",+ endpointHeaders: { "OPEN-SANDBOX-API-KEY": "tenant-secret" },+ });++ const execution = await codes.run("print('hi')");+ assert.equal(execution.logs.stdout[0]?.text, "hi");++ assert.equal(calls.length, 2);+ assert.equal(calls[0].headers["open-sandbox-api-key"], "tenant-secret");+ assert.equal(calls[1].url, "http://other.example/stream");+ assert.equal(calls[1].headers["open-sandbox-api-key"], undefined);+});++test("same-origin redirects preserve the credential", async () => {+ const calls = [];+ const fetchImpl = async (input, init = {}) => {+ const request = input instanceof Request ? input : new Request(input, init);+ calls.push({+ url: request.url,+ headers: Object.fromEntries(request.headers.entries()),+ });++ if (calls.length === 1) {+ return new Response(null, {+ status: 302,+ headers: { location: "http://sandbox.internal:3456/code/context2" },+ });+ }+ return new Response(JSON.stringify({ id: "ctx-1", language: "python" }), {+ status: 200,+ headers: { "content-type": "application/json" },+ });+ };++ const sandbox = {+ connectionConfig: {+ headers: {},++ useServerProxy: true,+ fetch: fetchImpl,+ sseFetch: fetchImpl,+ },+ };++ const codes = new DefaultAdapterFactory().createCodes({+ sandbox,+ execdBaseUrl: "http://sandbox.internal:3456",+ endpointHeaders: { "OPEN-SANDBOX-API-KEY": "tenant-secret" },+ });++ const context = await codes.createContext("python");+ assert.equal(context.id, "ctx-1");++ assert.equal(calls.length, 2);+ assert.equal(calls[1].url, "http://sandbox.internal:3456/code/context2");+ // Same-origin hop keeps the credential.+ assert.equal(calls[1].headers["open-sandbox-api-key"], "tenant-secret");+});