@@ -642,6 +642,8 @@ def _connection_config(
642642 kwargs ["request_timeout" ] = timedelta (seconds = request_timeout_s )
643643 if self ._connection .use_server_proxy :
644644 kwargs ["use_server_proxy" ] = True
645+ if self ._connection .api_key is not None :
646+ kwargs ["headers" ] = {"OPEN-SANDBOX-API-KEY" : self ._connection .api_key }
645647 if self ._connection .keepalive_expiry_s is not None or self ._connection .disable_connection_pooling :
646648 kwargs ["transport" ] = self ._get_transport ()
647649 config = ConnectionConfig (** kwargs )
@@ -1114,12 +1116,12 @@ async def endpoint(self, handle: SandboxHandle, port: int) -> SandboxEndpoint:
11141116 # defaults match the connection that produced this endpoint.
11151117 scheme = connection .get_base_url ().split ("://" , 1 )[0 ]
11161118 endpoint_url = f"{ scheme } ://{ endpoint_url .lstrip ('/' )} "
1117- headers = dict (getattr (resolved , "headers" , None ) or {})
1118- api_key = connection . headers .get ( "OPEN-SANDBOX-API-KEY" )
1119- if connection . use_server_proxy and api_key :
1120- # Direct endpoints terminate at untrusted sandbox code; only proxy
1121- # endpoints may receive server credentials .
1122- headers .setdefault ("OPEN-SANDBOX-API-KEY" , api_key )
1119+ headers = dict (getattr (connection , "headers" , None ) or {})
1120+ headers .update ( getattr ( resolved , "headers" , None ) or {} )
1121+ if not getattr ( connection , " use_server_proxy" , self . _connection . use_server_proxy ) :
1122+ # Direct endpoints terminate at untrusted sandbox code and must
1123+ # never receive the management credential .
1124+ headers .pop ("OPEN-SANDBOX-API-KEY" , None )
11231125 return SandboxEndpoint (endpoint = endpoint_url , headers = headers )
11241126
11251127 def _command_retry_count (self ) -> int :
0 commit comments