Secure, Fast, and Extensible Sandbox runtime for AI agents.
fix(sdk-kotlin): keep an explicit API key header ahead of the configured key
xxwtiancaibedbaabbd495
diff --git a/docs/sdks/kotlin.md b/docs/sdks/kotlin.mdindex 001cbb1a4..d418c7e8c 100644--- a/docs/sdks/kotlin.md+++ b/docs/sdks/kotlin.md@@ -474,6 +474,8 @@ sandbox.patchMetadata(patch);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. The header is attached only when a key is configured. This also applies to SDK telemetry requests.+::: info Transfer timeouts`requestTimeout` bounds each transport phase - connecting, waiting for response headers, and waiting for more of a response body - but it never caps a healthy file upload or download that keeps making progress. A transfer that stalls for longer than the timeout, or an upload that gets no response headers within it after the body has been sent, fails instead of hanging (the Go SDK's `ResponseHeaderTimeout` contract). An explicit `RetryPolicy.overallDeadline` still caps the whole transfer.:::diff --git a/sdks/sandbox/kotlin/sandbox/src/main/kotlin/com/alibaba/opensandbox/sandbox/HttpClientProvider.kt b/sdks/sandbox/kotlin/sandbox/src/main/kotlin/com/alibaba/opensandbox/sandbox/HttpClientProvider.ktindex b2a644f69..87ecda933 100644--- a/sdks/sandbox/kotlin/sandbox/src/main/kotlin/com/alibaba/opensandbox/sandbox/HttpClientProvider.kt+++ b/sdks/sandbox/kotlin/sandbox/src/main/kotlin/com/alibaba/opensandbox/sandbox/HttpClientProvider.kt@@ -224,8 +224,14 @@ class HttpClientProvider(private class AuthenticationInterceptor(private val apiKey: String) : Interceptor {override fun intercept(chain: Interceptor.Chain): Response {+ val request = chain.request()+ // An explicit header from ConnectionConfig.headers (matched+ // case-insensitively by OkHttp) wins over the configured API key.+ if (apiKey.isBlank() || request.header("OPEN-SANDBOX-API-KEY") != null) {+ return chain.proceed(request)+ }return chain.proceed(- chain.request().newBuilder()+ request.newBuilder().header("OPEN-SANDBOX-API-KEY", apiKey).build(),)diff --git a/sdks/sandbox/kotlin/sandbox/src/test/kotlin/com/alibaba/opensandbox/sandbox/ApiKeyHeaderTest.kt b/sdks/sandbox/kotlin/sandbox/src/test/kotlin/com/alibaba/opensandbox/sandbox/ApiKeyHeaderTest.ktnew file mode 100644index 000000000..409fcfa2a--- /dev/null+++ b/sdks/sandbox/kotlin/sandbox/src/test/kotlin/com/alibaba/opensandbox/sandbox/ApiKeyHeaderTest.kt@@ -0,0 +1,107 @@+/*+ * 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.+ */++package com.alibaba.opensandbox.sandbox++import com.alibaba.opensandbox.sandbox.config.ConnectionConfig+import okhttp3.Request+import okhttp3.mockwebserver.MockResponse+import okhttp3.mockwebserver.MockWebServer+import org.junit.jupiter.api.AfterEach+import org.junit.jupiter.api.Assertions.assertEquals+import org.junit.jupiter.api.Assertions.assertNull+import org.junit.jupiter.api.BeforeEach+import org.junit.jupiter.api.Test++class ApiKeyHeaderTest {+ private lateinit var server: MockWebServer++ @BeforeEach+ fun setUp() {+ server = MockWebServer()+ server.start()+ }++ @AfterEach+ fun tearDown() {+ server.shutdown()+ }++ private fun getAuthenticated(config: ConnectionConfig) {+ HttpClientProvider(config).use { provider ->+ val request = Request.Builder().url(server.url("/")).build()+ provider.authenticatedClient.newCall(request).execute().use { it.body?.close() }+ }+ }++ @Test+ fun `authenticated request carries the configured api key`() {+ server.enqueue(MockResponse().setResponseCode(200))++ getAuthenticated(+ ConnectionConfig.builder()+ .apiKey("configured-key")+ .build(),+ )++ val recorded = server.takeRequest()+ assertEquals(listOf("configured-key"), recorded.headers.values("OPEN-SANDBOX-API-KEY"))+ }++ @Test+ fun `user-provided api key header is not overwritten`() {+ server.enqueue(MockResponse().setResponseCode(200))++ getAuthenticated(+ ConnectionConfig.builder()+ .apiKey("configured-key")+ .addHeader("OPEN-SANDBOX-API-KEY", "explicit-key")+ .build(),+ )++ val recorded = server.takeRequest()+ assertEquals(listOf("explicit-key"), recorded.headers.values("OPEN-SANDBOX-API-KEY"))+ }++ @Test+ fun `user-provided api key header in lowercase is not overwritten`() {+ server.enqueue(MockResponse().setResponseCode(200))++ getAuthenticated(+ ConnectionConfig.builder()+ .apiKey("configured-key")+ .addHeader("open-sandbox-api-key", "explicit-key")+ .build(),+ )++ val recorded = server.takeRequest()+ assertEquals(listOf("explicit-key"), recorded.headers.values("OPEN-SANDBOX-API-KEY"))+ }++ @Test+ fun `api key header is omitted when no key is configured`() {+ server.enqueue(MockResponse().setResponseCode(200))++ getAuthenticated(+ ConnectionConfig.builder()+ .headers(mapOf("X-Custom" to "value"))+ .build(),+ )++ val recorded = server.takeRequest()+ assertNull(recorded.getHeader("OPEN-SANDBOX-API-KEY"))+ }+}