Skip to content

Secure, Fast, and Extensible Sandbox runtime for AI agents.

fix(sdk): preserve an explicitly configured data-plane API key

JasonBuildAI66d7f4079045
diff --git a/sdks/code-interpreter/csharp/tests/OpenSandbox.CodeInterpreter.Tests/DataPlaneAuthTests.cs b/sdks/code-interpreter/csharp/tests/OpenSandbox.CodeInterpreter.Tests/DataPlaneAuthTests.cs
index 80963d2e6..72d028e2c 100644
--- a/sdks/code-interpreter/csharp/tests/OpenSandbox.CodeInterpreter.Tests/DataPlaneAuthTests.cs
+++ b/sdks/code-interpreter/csharp/tests/OpenSandbox.CodeInterpreter.Tests/DataPlaneAuthTests.cs
@@ -53,12 +53,23 @@ public class DataPlaneAuthTests
Assert.Equal("endpoint-secret", options.ExecdHeaders[ApiKeyHeader]);
}
+ [Fact]
+ public async Task CreateAsync_ServerProxyMode_KeepsExplicitConnectionApiKey()
+ {
+ var options = await CaptureCodesOptionsAsync(
+ useServerProxy: true,
+ connectionHeaders: new Dictionary<string, string> { [ApiKeyHeader] = "explicit-secret" });
+
+ Assert.Equal("explicit-secret", options.ExecdHeaders[ApiKeyHeader]);
+ }
+
private static async Task<CreateCodesStackOptions> CaptureCodesOptionsAsync(
bool useServerProxy,
- IReadOnlyDictionary<string, string>? endpointHeaders = null)
+ IReadOnlyDictionary<string, string>? endpointHeaders = null,
+ IReadOnlyDictionary<string, string>? connectionHeaders = null)
{
var captured = new CapturingAdapterFactory();
- var sandbox = await CreateSandboxAsync(useServerProxy, endpointHeaders);
+ var sandbox = await CreateSandboxAsync(useServerProxy, endpointHeaders, connectionHeaders);
await CodeInterpreter.CreateAsync(sandbox, new CodeInterpreterCreateOptions
{
AdapterFactory = captured,
@@ -72,7 +83,8 @@ public class DataPlaneAuthTests
private static async Task<Sandbox> CreateSandboxAsync(
bool useServerProxy,
- IReadOnlyDictionary<string, string>? endpointHeaders)
+ IReadOnlyDictionary<string, string>? endpointHeaders,
+ IReadOnlyDictionary<string, string>? connectionHeaders)
{
var sandboxesMock = new Mock<ISandboxes>();
sandboxesMock
@@ -120,7 +132,10 @@ public class DataPlaneAuthTests
{
Domain = "localhost:8080",
ApiKey = "tenant-secret",
- UseServerProxy = useServerProxy
+ UseServerProxy = useServerProxy,
+ Headers = connectionHeaders is null
+ ? null
+ : new Dictionary<string, string>(connectionHeaders)
}),
AdapterFactory = adapterFactoryMock.Object,
SkipHealthCheck = true
diff --git a/sdks/code-interpreter/javascript/tests/data-plane-auth.test.mjs b/sdks/code-interpreter/javascript/tests/data-plane-auth.test.mjs
index 12ee69579..0b549cc8f 100644
--- a/sdks/code-interpreter/javascript/tests/data-plane-auth.test.mjs
+++ b/sdks/code-interpreter/javascript/tests/data-plane-auth.test.mjs
@@ -42,7 +42,7 @@ function createConfig(useServerProxy, requests, includeExplicitApiKey = false) {
const headers = { "x-custom-header": "custom-value" };
if (includeExplicitApiKey) {
- headers[API_KEY_HEADER] = "explicit-secret";
+ headers["OPEN-SANDBOX-API-KEY"] = "explicit-secret";
}
const config = new ConnectionConfig({
@@ -56,12 +56,16 @@ function createConfig(useServerProxy, requests, includeExplicitApiKey = false) {
return config;
}
-async function sendCodeRequests(useServerProxy, useEndpointApiKey = false) {
+async function sendCodeRequests(
+ useServerProxy,
+ useEndpointApiKey = false,
+ useExplicitConnectionApiKey = false,
+) {
const requests = [];
const connectionConfig = createConfig(
useServerProxy,
requests,
- !useServerProxy,
+ !useServerProxy || useExplicitConnectionApiKey,
);
const factory = new DefaultAdapterFactory();
const codes = factory.createCodes({
@@ -106,3 +110,12 @@ test("server-proxied code requests preserve endpoint-specific API keys", async (
assert.equal(request.headers.get(API_KEY_HEADER), "endpoint-secret");
}
});
+
+test("server-proxied code requests keep an explicitly configured API key", async () => {
+ const requests = await sendCodeRequests(true, false, true);
+
+ assert.equal(requests.length, 2);
+ for (const request of requests) {
+ assert.equal(request.headers.get(API_KEY_HEADER), "explicit-secret");
+ }
+});
diff --git a/sdks/sandbox/javascript/src/factory/defaultAdapterFactory.ts b/sdks/sandbox/javascript/src/factory/defaultAdapterFactory.ts
index 988096fe7..3890c3ef9 100644
--- a/sdks/sandbox/javascript/src/factory/defaultAdapterFactory.ts
+++ b/sdks/sandbox/javascript/src/factory/defaultAdapterFactory.ts
@@ -46,7 +46,8 @@ const API_KEY_HEADER = "OPEN-SANDBOX-API-KEY";
* The tenant API key is attached only in server-proxy mode: direct requests go
* straight to the sandbox, which performs no authentication, so the key must
* never travel into it. A credential minted for a specific endpoint wins over
- * the connection-level key.
+ * the connection-level key, and a credential explicitly configured in the
+ * connection headers wins over `apiKey`.
*
* Exported through the internal entrypoint so companion SDKs that build their
* own data-plane clients (for example the code interpreter) share this policy
@@ -65,8 +66,11 @@ export function createDataPlaneHeaders(
const endpointApiKey = Object.entries(endpointHeaders ?? {}).find(
([key]) => key.toLowerCase() === API_KEY_HEADER.toLowerCase(),
);
+ const connectionApiKey = Object.entries(connectionHeaders).find(
+ ([key]) => key.toLowerCase() === API_KEY_HEADER.toLowerCase(),
+ );
- if (!useServerProxy || endpointApiKey || apiKey) {
+ if (!useServerProxy || endpointApiKey || (!connectionApiKey && apiKey)) {
for (const key of Object.keys(headers)) {
if (key.toLowerCase() === API_KEY_HEADER.toLowerCase()) {
delete headers[key];
@@ -77,7 +81,7 @@ export function createDataPlaneHeaders(
if (useServerProxy) {
if (endpointApiKey) {
headers[endpointApiKey[0]] = endpointApiKey[1];
- } else if (apiKey) {
+ } else if (!connectionApiKey && apiKey) {
headers[API_KEY_HEADER] = apiKey;
}
}
diff --git a/sdks/sandbox/javascript/tests/data-plane-auth.test.mjs b/sdks/sandbox/javascript/tests/data-plane-auth.test.mjs
index 32d6473f6..33a0fe2e9 100644
--- a/sdks/sandbox/javascript/tests/data-plane-auth.test.mjs
+++ b/sdks/sandbox/javascript/tests/data-plane-auth.test.mjs
@@ -41,7 +41,7 @@ function createConfig(useServerProxy, requests, includeExplicitApiKey = false) {
const headers = { "X-Custom-Header": "custom-value" };
if (includeExplicitApiKey) {
- headers["open-sandbox-api-key"] = "explicit-secret";
+ headers["OPEN-SANDBOX-API-KEY"] = "explicit-secret";
}
const config = new ConnectionConfig({
@@ -55,12 +55,16 @@ function createConfig(useServerProxy, requests, includeExplicitApiKey = false) {
return config;
}
-async function sendDataPlaneRequests(useServerProxy, useEndpointApiKeys = false) {
+async function sendDataPlaneRequests(
+ useServerProxy,
+ useEndpointApiKeys = false,
+ useExplicitConnectionApiKey = false,
+) {
const requests = [];
const connectionConfig = createConfig(
useServerProxy,
requests,
- !useServerProxy,
+ !useServerProxy || useExplicitConnectionApiKey,
);
const factory = new DefaultAdapterFactory();
const execd = factory.createExecdStack({
@@ -117,6 +121,15 @@ test("server-proxied requests preserve endpoint-specific API keys", async () =>
assert.equal(requests[1].headers.get(API_KEY_HEADER), "egress-secret");
});
+test("server-proxied requests keep an explicitly configured API key", async () => {
+ const requests = await sendDataPlaneRequests(true, false, true);
+
+ assert.equal(requests.length, 2);
+ for (const request of requests) {
+ assert.equal(request.headers.get(API_KEY_HEADER), "explicit-secret");
+ }
+});
+
test("lifecycle requests retain the tenant API key in direct mode", async () => {
const requests = [];
const connectionConfig = createConfig(false, requests);