Secure, Fast, and Extensible Sandbox runtime for AI agents.
fix(sdk-go): keep tenant key out of direct-mode config headers
JasonBuildAI6f7dea8e080f
diff --git a/docs/sdks/go.md b/docs/sdks/go.mdindex c5bc01faa..248f3fa0a 100644--- a/docs/sdks/go.md+++ b/docs/sdks/go.md@@ -71,6 +71,8 @@ func main() {`ConnectSandbox` and `ResumeSandbox` check health only when you pass the optional `ReadyOptions` argument (an empty `ReadyOptions{}` uses the default check). Omitting it resolves the endpoint without a health check. Pause is asynchronous: wait until sandbox info reports `Paused` before resuming.+`ConnectionConfig.APIKey` authenticates lifecycle requests. Data-plane requests carry `OPEN-SANDBOX-API-KEY` only when `UseServerProxy` is true; in direct mode the SDK removes that header from `ConnectionConfig.Headers` before sending to execd or egress because those endpoints run inside the sandbox. Use the endpoint headers returned by `GetEndpoint` for direct data-plane authentication.+The following snippets go inside a function returning `error`, with the live `sandbox`, `config`, and `ctx` above. Add standard-library imports used by each snippet. Creation examples are alternatives; terminate sandboxes with `Kill` when done. `Close` currently does not terminate the sandbox.### Run a command with streaming outputdiff --git a/sdks/sandbox/go/config.go b/sdks/sandbox/go/config.goindex 93a5b1693..68f253259 100644--- a/sdks/sandbox/go/config.go+++ b/sdks/sandbox/go/config.go@@ -47,7 +47,9 @@ type ConnectionConfig struct {// context to limit those.RequestTimeout time.Duration- // Headers are custom HTTP headers added to all requests.+ // Headers are custom HTTP headers added to lifecycle and data-plane+ // requests. In direct mode, OPEN-SANDBOX-API-KEY is omitted from+ // execd/egress requests because those endpoints run inside the sandbox.Headers map[string]string// HTTPClient is an optional custom HTTP client. If nil, a default is created.@@ -166,6 +168,14 @@ func (c *ConnectionConfig) GetRequestTimeout() time.Duration {// clientOpts builds the common Option slice from config fields.func (c *ConnectionConfig) clientOpts(includeAuthHeader bool) []Option {+ return c.clientOptsWithHeaders(includeAuthHeader, c.Headers)+}++// clientOptsWithHeaders builds the common Option slice with an explicit header+// map. Lifecycle clients pass ConnectionConfig.Headers unchanged; data-plane+// clients pass the result of dataPlaneHeaders so the tenant credential policy+// is applied consistently.+func (c *ConnectionConfig) clientOptsWithHeaders(includeAuthHeader bool, headers map[string]string) []Option {var opts []Optionif includeAuthHeader && c.AuthHeader != "" {opts = append(opts, WithAuthHeader(c.AuthHeader))@@ -180,8 +190,8 @@ func (c *ConnectionConfig) clientOpts(includeAuthHeader bool) []Option {if t := c.GetRequestTimeout(); t > 0 {opts = append(opts, WithTimeout(t))}- if len(c.Headers) > 0 {- opts = append(opts, WithHeaders(c.Headers))+ if len(headers) > 0 {+ opts = append(opts, WithHeaders(headers))}if c.Retry != nil {opts = append(opts, WithRetry(*c.Retry))@@ -189,6 +199,27 @@ func (c *ConnectionConfig) clientOpts(includeAuthHeader bool) []Option {return opts}+// dataPlaneHeaders merges connection and endpoint headers for execd/egress.+// Direct-mode requests terminate inside the sandbox, so the tenant API key must+// never be forwarded there, even when a caller supplied it via Headers.+func (c *ConnectionConfig) dataPlaneHeaders(endpointHeaders map[string]string) map[string]string {+ if len(c.Headers) == 0 && len(endpointHeaders) == 0 {+ return nil+ }++ headers := make(map[string]string, len(c.Headers)+len(endpointHeaders))+ for key, value := range c.Headers {+ headers[key] = value+ }+ for key, value := range endpointHeaders {+ headers[key] = value+ }+ if !c.UseServerProxy {+ deleteHeaderFold(headers, serverProxyAuthHeader)+ }+ return headers+}+// lifecycleClient creates a LifecycleClient from this config.// Appends the API version prefix (/v1) to the base URL, as required by// NewLifecycleClient and the OpenSandbox lifecycle API spec.@@ -204,10 +235,7 @@ func (c *ConnectionConfig) lifecycleClient() *LifecycleClient {// returned by the lifecycle GetEndpoint call (auth tokens, routing hints,// sticky-session keys, etc.) are forwarded as-is on every subsequent request.func (c *ConnectionConfig) execdClient(endpointURL string, endpointHeaders map[string]string) *ExecdClient {- opts := c.clientOpts(true)- if len(endpointHeaders) > 0 {- opts = append(opts, WithHeaders(endpointHeaders))- }+ opts := c.clientOptsWithHeaders(true, c.dataPlaneHeaders(endpointHeaders))return NewExecdClient(endpointURL, "", opts...)}@@ -215,9 +243,6 @@ func (c *ConnectionConfig) execdClient(endpointURL string, endpointHeaders map[s// returned by the lifecycle GetEndpoint call are forwarded as-is on every// subsequent request.func (c *ConnectionConfig) egressClient(endpointURL string, endpointHeaders map[string]string) *EgressClient {- opts := c.clientOpts(false)- if len(endpointHeaders) > 0 {- opts = append(opts, WithHeaders(endpointHeaders))- }+ opts := c.clientOptsWithHeaders(false, c.dataPlaneHeaders(endpointHeaders))return NewEgressClient(endpointURL, "", opts...)}diff --git a/sdks/sandbox/go/http_redirect_test.go b/sdks/sandbox/go/http_redirect_test.goindex 111d2cf11..6139a49c3 100644--- a/sdks/sandbox/go/http_redirect_test.go+++ b/sdks/sandbox/go/http_redirect_test.go@@ -218,9 +218,9 @@ func TestRedirectRequestPaths(t *testing.T) {case "execd":require.NoError(t, NewExecdClient(source.URL, "execd-token", opts...).Ping(ctx))case "proxy-execd":- // ConnectionConfig applies the lifecycle auth override even to- // execd, whose endpoint headers still use its intrinsic token name.- cfg := ConnectionConfig{AuthHeader: "X-API-Key", Headers: map[string]string{+ // Proxy-mode data-plane requests retain the tenant key while+ // ConnectionConfig applies the lifecycle auth override to execd.+ cfg := ConnectionConfig{UseServerProxy: true, AuthHeader: "X-API-Key", Headers: map[string]string{"X-API-Key": "tenant", "X-Custom": "keep",}}client := cfg.execdClient(source.URL, map[string]string{diff --git a/sdks/sandbox/go/sandbox.go b/sdks/sandbox/go/sandbox.goindex 0e02a5b1b..832a1a98c 100644--- a/sdks/sandbox/go/sandbox.go+++ b/sdks/sandbox/go/sandbox.go@@ -40,6 +40,15 @@ func hasHeaderFold(headers map[string]string, name string) bool {return false}+// deleteHeaderFold removes every casing of name from headers.+func deleteHeaderFold(headers map[string]string, name string) {+ for key := range headers {+ if strings.EqualFold(key, name) {+ delete(headers, key)+ }+ }+}+// SandboxCreateOptions configures sandbox creation.type SandboxCreateOptions struct {// Image is the container image URI (required).diff --git a/sdks/sandbox/go/sandbox_proxy_auth_test.go b/sdks/sandbox/go/sandbox_proxy_auth_test.goindex e59c6948e..10d96fc9c 100644--- a/sdks/sandbox/go/sandbox_proxy_auth_test.go+++ b/sdks/sandbox/go/sandbox_proxy_auth_test.go@@ -158,6 +158,84 @@ func TestResolveExecdDirectModeCarriesNoTenantKey(t *testing.T) {require.Equal(t, "", headers.Get("X-EXECD-ACCESS-TOKEN"))}+// TestResolveExecdDirectModeStripsConfigProxyKey covers a caller that puts the+// tenant key in ConnectionConfig.Headers. That map is applied to every client,+// but direct data-plane requests terminate inside the untrusted sandbox.+func TestResolveExecdDirectModeStripsConfigProxyKey(t *testing.T) {+ var directHeaders capturedHeaders+ direct := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ directHeaders.record(r)+ w.WriteHeader(http.StatusOK)+ }))+ defer direct.Close()++ sb := serverProxyFixture(t, direct.URL, nil, false)+ sb.config.Headers = map[string]string{+ "Open-Sandbox-Api-Key": "tenant-key-1",+ "X-Custom": "keep",+ }++ require.NoErrorf(t, sb.resolveExecd(context.Background()), "resolveExecd")+ require.NoErrorf(t, sb.execd.Ping(context.Background()), "Ping")++ headers := directHeaders.first(t)+ require.Equal(t, "", headers.Get("OPEN-SANDBOX-API-KEY"),+ "direct-mode requests must not receive the tenant key from ConnectionConfig.Headers")+ require.Equal(t, "keep", headers.Get("X-Custom"))+}++// TestResolveExecdDirectModeStripsEndpointProxyKey applies the same policy to+// endpoint metadata while preserving the endpoint's actual execd credential.+func TestResolveExecdDirectModeStripsEndpointProxyKey(t *testing.T) {+ var directHeaders capturedHeaders+ direct := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ directHeaders.record(r)+ w.WriteHeader(http.StatusOK)+ }))+ defer direct.Close()++ sb := serverProxyFixture(t, direct.URL, map[string]string{+ "Open-Sandbox-Api-Key": "endpoint-key",+ "X-EXECD-ACCESS-TOKEN": "endpoint-token",+ "X-Custom": "keep",+ }, false)++ require.NoErrorf(t, sb.resolveExecd(context.Background()), "resolveExecd")+ require.NoErrorf(t, sb.execd.Ping(context.Background()), "Ping")++ headers := directHeaders.first(t)+ require.Equal(t, "", headers.Get("OPEN-SANDBOX-API-KEY"),+ "direct-mode requests must not receive the tenant key from endpoint headers")+ require.Equal(t, "endpoint-token", headers.Get("X-EXECD-ACCESS-TOKEN"))+ require.Equal(t, "keep", headers.Get("X-Custom"))+}++// TestResolveEgressDirectModeStripsConfigProxyKey is the egress twin of the+// config-header regression above.+func TestResolveEgressDirectModeStripsConfigProxyKey(t *testing.T) {+ var directHeaders capturedHeaders+ direct := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ directHeaders.record(r)+ jsonResponse(w, http.StatusOK, PolicyStatusResponse{Status: "ok"})+ }))+ defer direct.Close()++ sb := serverProxyFixture(t, direct.URL, nil, false)+ sb.config.Headers = map[string]string{+ "open-sandbox-api-key": "tenant-key-1",+ "X-Custom": "keep",+ }++ require.NoErrorf(t, sb.resolveEgress(context.Background()), "resolveEgress")+ _, err := sb.egress.GetPolicy(context.Background())+ require.NoErrorf(t, err, "GetPolicy")++ headers := directHeaders.first(t)+ require.Equal(t, "", headers.Get("OPEN-SANDBOX-API-KEY"),+ "direct-mode requests must not receive the tenant key from ConnectionConfig.Headers")+ require.Equal(t, "keep", headers.Get("X-Custom"))+}+// TestResolveEgressProxyModeAuthenticatesWithServerHeader covers the egress// sidecar path, which had the same wrong-header fallback as execd.func TestResolveEgressProxyModeAuthenticatesWithServerHeader(t *testing.T) {