Secure, Fast, and Extensible Sandbox runtime for AI agents.
fix(sdk-csharp): honor an explicit API key header regardless of casing
xxwtiancaia9437b7b715d
diff --git a/docs/sdks/csharp.md b/docs/sdks/csharp.mdindex 0ef882aa1..8a1c80dc7 100644--- a/docs/sdks/csharp.md+++ b/docs/sdks/csharp.md@@ -487,6 +487,8 @@ await sandbox.PatchMetadataAsync(new Dictionary<string, string?>The `ConnectionConfig` class manages API server connection settings.+An explicit `OPEN-SANDBOX-API-KEY` entry in `Headers` takes precedence over `ApiKey` and its environment fallback, regardless of header-name casing. A present-but-empty entry is preserved rather than replaced with the configured key. This also applies to SDK telemetry requests.+::: info Transfer timeouts`RequestTimeoutSeconds` bounds the wait for response headers, not the whole transfer. The deadline starts with the request (so the connect phase is bounded too), is cleared while the request body is written, and is re-armed once the body of the current hop has been sent: a healthy upload or download that keeps making progress is never cut off by it. A download that gets no response headers, or an upload that gets none within the timeout after its body has been sent, fails with a `TaskCanceledException` carrying a `TimeoutException` instead of hanging (the Go SDK's `ResponseHeaderTimeout` contract); a caller cancellation still surfaces as an `OperationCanceledException` with the caller's token. All non-transfer calls keep the whole-call request timeout.diff --git a/sdks/sandbox/csharp/src/OpenSandbox/Config/ConnectionConfig.cs b/sdks/sandbox/csharp/src/OpenSandbox/Config/ConnectionConfig.csindex f4794d606..55a60353e 100644--- a/sdks/sandbox/csharp/src/OpenSandbox/Config/ConnectionConfig.cs+++ b/sdks/sandbox/csharp/src/OpenSandbox/Config/ConnectionConfig.cs@@ -272,8 +272,11 @@ public sealed class ConnectionConfigvar headers = new Dictionary<string, string>(options.Headers ?? new Dictionary<string, string>());- // Add API key header if not already present- if (!string.IsNullOrEmpty(ApiKey) && !headers.ContainsKey(Constants.ApiKeyHeader))+ // Add API key header if not already present. HTTP header names are+ // case-insensitive, so any casing of the header set by the caller+ // suppresses the default; an explicit empty value is preserved.+ if (!string.IsNullOrEmpty(ApiKey) &&+ !headers.Keys.Any(k => string.Equals(k, Constants.ApiKeyHeader, StringComparison.OrdinalIgnoreCase))){headers[Constants.ApiKeyHeader] = ApiKey;}diff --git a/sdks/sandbox/csharp/src/OpenSandbox/Internal/LifecycleMetricsReporter.cs b/sdks/sandbox/csharp/src/OpenSandbox/Internal/LifecycleMetricsReporter.csindex 2855b70e0..446d9a128 100644--- a/sdks/sandbox/csharp/src/OpenSandbox/Internal/LifecycleMetricsReporter.cs+++ b/sdks/sandbox/csharp/src/OpenSandbox/Internal/LifecycleMetricsReporter.cs@@ -111,9 +111,9 @@ internal static class LifecycleMetricsReporter}request.Headers.TryAddWithoutValidation(header.Key, header.Value);}- if (!string.IsNullOrEmpty(connectionConfig.ApiKey))+ if (!string.IsNullOrEmpty(connectionConfig.ApiKey) &&+ !request.Headers.Contains(Constants.ApiKeyHeader)){- request.Headers.Remove(Constants.ApiKeyHeader);request.Headers.TryAddWithoutValidation(Constants.ApiKeyHeader, connectionConfig.ApiKey);}diff --git a/sdks/sandbox/csharp/tests/OpenSandbox.Tests/ConnectionConfigTests.cs b/sdks/sandbox/csharp/tests/OpenSandbox.Tests/ConnectionConfigTests.csindex 812a2442b..d4f60cb70 100644--- a/sdks/sandbox/csharp/tests/OpenSandbox.Tests/ConnectionConfigTests.cs+++ b/sdks/sandbox/csharp/tests/OpenSandbox.Tests/ConnectionConfigTests.cs@@ -209,4 +209,73 @@ public class ConnectionConfigTestsclient.Should().NotBeNull();client.Timeout.Should().Be(Timeout.InfiniteTimeSpan);}++ [Fact]+ public void Constructor_WithExplicitApiKeyHeaderInAnyCase_ShouldSuppressDefaultKey()+ {+ // Arrange+ var options = new ConnectionConfigOptions+ {+ ApiKey = "configured-key",+ Headers = new Dictionary<string, string>+ {+ ["open-sandbox-api-key"] = "explicit-key"+ }+ };++ // Act+ var config = new ConnectionConfig(options);++ // Assert+ var apiKeyEntries = config.Headers+ .Where(h => string.Equals(h.Key, Constants.ApiKeyHeader, StringComparison.OrdinalIgnoreCase))+ .ToList();+ apiKeyEntries.Should().HaveCount(1, "an explicit API key header suppresses the default regardless of casing");+ apiKeyEntries[0].Should().Be(new KeyValuePair<string, string>("open-sandbox-api-key", "explicit-key"));+ }++ [Fact]+ public void Constructor_WithEmptyExplicitApiKeyHeader_ShouldPreserveIt()+ {+ // Arrange+ var options = new ConnectionConfigOptions+ {+ ApiKey = "configured-key",+ Headers = new Dictionary<string, string>+ {+ ["Open-Sandbox-Api-Key"] = ""+ }+ };++ // Act+ var config = new ConnectionConfig(options);++ // Assert+ var apiKeyEntries = config.Headers+ .Where(h => string.Equals(h.Key, Constants.ApiKeyHeader, StringComparison.OrdinalIgnoreCase))+ .ToList();+ apiKeyEntries.Should().HaveCount(1);+ apiKeyEntries[0].Value.Should().BeEmpty("a present-but-empty explicit header is not replaced by the default");+ }++ [Fact]+ public void Constructor_WithCanonicalExplicitApiKeyHeader_ShouldKeepExplicitValue()+ {+ // Arrange+ var options = new ConnectionConfigOptions+ {+ ApiKey = "configured-key",+ Headers = new Dictionary<string, string>+ {+ [Constants.ApiKeyHeader] = "explicit-key"+ }+ };++ // Act+ var config = new ConnectionConfig(options);++ // Assert+ config.Headers.Should().HaveCount(1);+ config.Headers[Constants.ApiKeyHeader].Should().Be("explicit-key");+ }}diff --git a/sdks/sandbox/csharp/tests/OpenSandbox.Tests/LifecycleMetricsReporterTests.cs b/sdks/sandbox/csharp/tests/OpenSandbox.Tests/LifecycleMetricsReporterTests.csindex 0d61949e3..2c8bc2301 100644--- a/sdks/sandbox/csharp/tests/OpenSandbox.Tests/LifecycleMetricsReporterTests.cs+++ b/sdks/sandbox/csharp/tests/OpenSandbox.Tests/LifecycleMetricsReporterTests.cs@@ -135,6 +135,64 @@ public class LifecycleMetricsReporterTestsreceived.Headers[Constants.ApiKeyHeader].Should().Be("test-key");}+ [Fact]+ public async Task ReportSandboxCreate_PreservesExplicitApiKeyHeaderOverConfiguredKey()+ {+ using var server = new StubHttpServer();+ var config = new ConnectionConfig(new ConnectionConfigOptions+ {+ Domain = $"127.0.0.1:{server.Port}",+ Protocol = ConnectionProtocol.Http,+ RequestTimeoutSeconds = 5,+ ApiKey = "configured-key",+ Headers = new Dictionary<string, string>+ {+ [Constants.ApiKeyHeader] = "explicit-key"+ }+ });++ LifecycleMetricsReporter.ReportSandboxCreate(+ config,+ sandboxId: "sbx",+ image: "python:3.12",+ createDurationMs: 42,+ success: true);++ var received = await server.WaitForRequestAsync(TimeSpan.FromSeconds(3));+ received.Should().NotBeNull();+ received!.Headers[Constants.ApiKeyHeader].Should().Be("explicit-key",+ "an explicit API key header takes precedence over the configured ApiKey on metrics requests");+ }++ [Fact]+ public async Task ReportSandboxCreate_DoesNotDuplicateLowercaseApiKeyHeader()+ {+ using var server = new StubHttpServer();+ var config = new ConnectionConfig(new ConnectionConfigOptions+ {+ Domain = $"127.0.0.1:{server.Port}",+ Protocol = ConnectionProtocol.Http,+ RequestTimeoutSeconds = 5,+ ApiKey = "configured-key",+ Headers = new Dictionary<string, string>+ {+ ["open-sandbox-api-key"] = "explicit-key"+ }+ });++ LifecycleMetricsReporter.ReportSandboxCreate(+ config,+ sandboxId: "sbx",+ image: "python:3.12",+ createDurationMs: 42,+ success: true);++ var received = await server.WaitForRequestAsync(TimeSpan.FromSeconds(3));+ received.Should().NotBeNull();+ received!.Headers[Constants.ApiKeyHeader].Should().Be("explicit-key",+ "the caller's header is sent once, not duplicated with the configured key");+ }+/// <summary>/// Tiny single-shot HTTP stub that accepts one request and captures the request line, headers, and body./// Only intended for these fire-and-forget metric tests.