Skip to content

armgit / E2B

Public Imported from GitHub
0

Open-source, secure environment with real-world tools for enterprise-grade agents.

Keep integration attribution on connection config (#1459)

Matt Brockmanf160f08c7bc0
diff --git a/.changeset/private-integration-attribution.md b/.changeset/private-integration-attribution.md
new file mode 100644
index 000000000..7972a4b41
--- /dev/null
+++ b/.changeset/private-integration-attribution.md
@@ -0,0 +1,6 @@
+---
+"e2b": patch
+"@e2b/python-sdk": patch
+---
+
+Keep integration user-agent attribution on `ConnectionConfig` instead of exposing it through sandbox, template, volume, or per-call API option types.
diff --git a/packages/js-sdk/src/connectionConfig.ts b/packages/js-sdk/src/connectionConfig.ts
index a1f578d9a..63adf1bf5 100644
--- a/packages/js-sdk/src/connectionConfig.ts
+++ b/packages/js-sdk/src/connectionConfig.ts
@@ -93,13 +93,6 @@ export interface ConnectionOpts {
*/
apiHeaders?: Record<string, string>
- /**
- * Integration wrapping the E2B SDK, appended to the `User-Agent`.
- *
- * @example 'e2b-code-interpreter/0.1.0'
- */
- integration?: string
-
/**
* An optional `AbortSignal` that can be used to cancel the in-flight request.
* When the signal is aborted, the underlying `fetch` is aborted and the
@@ -108,6 +101,18 @@ export interface ConnectionOpts {
signal?: AbortSignal
}
+/**
+ * Options accepted by `ConnectionConfig`.
+ */
+export interface ConnectionConfigOpts extends ConnectionOpts {
+ /**
+ * Integration wrapping the E2B SDK, appended to the `User-Agent`.
+ *
+ * @example 'e2b-code-interpreter/0.1.0'
+ */
+ integration?: string
+}
+
/**
* Build an `AbortSignal` that combines an optional request-timeout signal
* (via `AbortSignal.timeout`) with an optional user-provided signal.
@@ -350,7 +355,7 @@ export class ConnectionConfig {
readonly proxy?: string
- constructor(opts?: ConnectionOpts) {
+ constructor(opts?: ConnectionConfigOpts) {
this.apiKey = opts?.apiKey || ConnectionConfig.apiKey
this.validateApiKey =
opts?.validateApiKey ?? ConnectionConfig.validateApiKey
diff --git a/packages/js-sdk/src/index.ts b/packages/js-sdk/src/index.ts
index 237b90650..e20436ed2 100644
--- a/packages/js-sdk/src/index.ts
+++ b/packages/js-sdk/src/index.ts
@@ -2,7 +2,11 @@ export { ApiClient } from './api'
export type { components, paths } from './api'
export { ConnectionConfig } from './connectionConfig'
-export type { ConnectionOpts, Username } from './connectionConfig'
+export type {
+ ConnectionConfigOpts,
+ ConnectionOpts,
+ Username,
+} from './connectionConfig'
export {
AuthenticationError,
FileNotFoundError,
diff --git a/packages/js-sdk/tests/connectionConfig.test.ts b/packages/js-sdk/tests/connectionConfig.test.ts
index c799572f2..97fa044a7 100644
--- a/packages/js-sdk/tests/connectionConfig.test.ts
+++ b/packages/js-sdk/tests/connectionConfig.test.ts
@@ -183,7 +183,7 @@ test('debug defaults to E2B_DEBUG env var', () => {
assert.equal(config.debug, true)
})
-test('integration options are appended to the user agent', () => {
+test('integration option is appended to the user agent', () => {
const config = new ConnectionConfig({
integration: 'testing/version',
})
diff --git a/packages/js-sdk/tests/sandbox/rpcHeaders.test.ts b/packages/js-sdk/tests/sandbox/rpcHeaders.test.ts
index 1c4f3ee5c..7c6251e70 100644
--- a/packages/js-sdk/tests/sandbox/rpcHeaders.test.ts
+++ b/packages/js-sdk/tests/sandbox/rpcHeaders.test.ts
@@ -18,13 +18,14 @@ vi.mock('../../src/envd/http2', () => ({
}))
test('does not pass custom connection headers to envd RPC requests', async () => {
- const { Sandbox } = await import('../../src')
+ const { ConnectionConfig, Sandbox } = await import('../../src')
+ const config = new ConnectionConfig({ integration: 'testing/version' })
const sandbox = new Sandbox({
+ ...config,
sandboxId: 'sbx-test',
sandboxDomain: 'sandbox.e2b.dev',
envdVersion: '0.2.4',
envdAccessToken: 'tok',
- integration: 'testing/version',
headers: {
Authorization: 'Bearer user-token',
'X-Custom': 'secret',
diff --git a/packages/python-sdk/e2b/connection_config.py b/packages/python-sdk/e2b/connection_config.py
index b9bef7102..050a4ca4e 100644
--- a/packages/python-sdk/e2b/connection_config.py
+++ b/packages/python-sdk/e2b/connection_config.py
@@ -31,9 +31,6 @@ class ApiParams(TypedDict, total=False):
api_headers: Optional[Dict[str, str]]
"""Additional headers to send with E2B API requests."""
- integration: Optional[str]
- """Integration wrapping the E2B SDK, appended to the User-Agent."""
-
api_key: Optional[str]
"""E2B API Key to use for authentication, defaults to `E2B_API_KEY` environment variable."""
@@ -148,9 +145,10 @@ class ConnectionConfig:
self.access_token = access_token or ConnectionConfig._access_token()
self.integration = integration
self.headers = {**(headers or {}), **(api_headers or {})}
- self.headers["User-Agent"] = self._build_user_agent(
- self.integration,
- )
+ if self.integration is not None or "User-Agent" not in self.headers:
+ self.headers["User-Agent"] = self._build_user_agent(
+ self.integration,
+ )
self.__extra_sandbox_headers = extra_sandbox_headers or {}
self.proxy = proxy
@@ -240,7 +238,6 @@ class ConnectionConfig:
"""
headers = opts.get("headers")
api_headers = opts.get("api_headers")
- integration = opts.get("integration", self.integration)
request_timeout = opts.get("request_timeout")
api_key = opts.get("api_key")
validate_api_key = opts.get("validate_api_key")
@@ -255,9 +252,9 @@ class ConnectionConfig:
req_headers.update(headers)
if api_headers is not None:
req_headers.update(api_headers)
- if integration is not None:
+ if self.integration is not None:
req_headers["User-Agent"] = self._build_user_agent(
- integration,
+ self.integration,
)
# `logger` is a construction-time option rather than a per-request
@@ -279,7 +276,6 @@ class ConnectionConfig:
debug=debug if debug is not None else self.debug,
request_timeout=self.get_request_timeout(request_timeout),
headers=req_headers,
- integration=integration,
proxy=proxy if proxy is not None else self.proxy,
sandbox_url=(
sandbox_url
diff --git a/packages/python-sdk/tests/async/sandbox_async/test_config_propagation.py b/packages/python-sdk/tests/async/sandbox_async/test_config_propagation.py
index 2a51e7184..15740cd48 100644
--- a/packages/python-sdk/tests/async/sandbox_async/test_config_propagation.py
+++ b/packages/python-sdk/tests/async/sandbox_async/test_config_propagation.py
@@ -103,12 +103,15 @@ async def test_connect_sets_stable_host_routing_headers(monkeypatch, test_api_ke
)
monkeypatch.setattr(sandbox_async_main.SandboxApi, "_cls_connect", mock_connect)
- sandbox = await AsyncSandbox.connect(
- "sbx-test",
+ config = ConnectionConfig(
api_key=test_api_key,
headers=BASE_HEADERS,
integration="testing/version",
)
+ sandbox = await AsyncSandbox.connect(
+ "sbx-test",
+ **config.get_api_params(),
+ )
assert sandbox.envd_api_url == "https://sandbox.e2b.app"
assert "X-Test" not in sandbox.connection_config.sandbox_headers
diff --git a/packages/python-sdk/tests/sync/sandbox_sync/test_config_propagation.py b/packages/python-sdk/tests/sync/sandbox_sync/test_config_propagation.py
index fef4e3000..05034f05e 100644
--- a/packages/python-sdk/tests/sync/sandbox_sync/test_config_propagation.py
+++ b/packages/python-sdk/tests/sync/sandbox_sync/test_config_propagation.py
@@ -90,12 +90,15 @@ def test_connect_sets_stable_host_routing_headers(monkeypatch, test_api_key):
)
monkeypatch.setattr(sandbox_sync_main.SandboxApi, "_cls_connect", mock_connect)
- sandbox = Sandbox.connect(
- "sbx-test",
+ config = ConnectionConfig(
api_key=test_api_key,
headers=BASE_HEADERS,
integration="testing/version",
)
+ sandbox = Sandbox.connect(
+ "sbx-test",
+ **config.get_api_params(),
+ )
assert sandbox.envd_api_url == "https://sandbox.e2b.app"
assert "X-Test" not in sandbox.connection_config.sandbox_headers
diff --git a/packages/python-sdk/tests/test_connection_config.py b/packages/python-sdk/tests/test_connection_config.py
index 099459d19..1e4e7cc63 100644
--- a/packages/python-sdk/tests/test_connection_config.py
+++ b/packages/python-sdk/tests/test_connection_config.py
@@ -131,7 +131,7 @@ def test_debug_defaults_to_env_var(monkeypatch):
assert config.debug is True
-def test_integration_options_are_appended_to_user_agent():
+def test_integration_option_is_appended_to_user_agent():
config = ConnectionConfig(integration="testing/version")
assert config.headers["User-Agent"].startswith("e2b-python-sdk/")
@@ -143,6 +143,9 @@ def test_integration_option_survives_api_param_rebuilds():
rebuilt_config = ConnectionConfig(**config.get_api_params())
assert rebuilt_config.headers["User-Agent"].endswith(" testing/version")
+ assert rebuilt_config.get_api_params(api_headers={"X-Test": "1"})["headers"][
+ "User-Agent"
+ ].endswith(" testing/version")
def test_request_timeout_zero_means_no_timeout():