Skip to content

armgit / E2B

Public Imported from GitHub
0

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

fix(sdk): guard egressProxy shape in JS and drop null proxy credentials (#1757)

devin-ai-integration[bot]b8029973aa7d
diff --git a/.changeset/egress-proxy-guard-and-null-credentials.md b/.changeset/egress-proxy-guard-and-null-credentials.md
new file mode 100644
index 000000000..80f714f93
--- /dev/null
+++ b/.changeset/egress-proxy-guard-and-null-credentials.md
@@ -0,0 +1,41 @@
+---
+'@e2b/python-sdk': patch
+'e2b': patch
+---
+
+Fix two `network.egressProxy` / `network["egress_proxy"]` cases an untyped caller reaches.
+
+The JS SDK had no shape guard: `buildEgressProxyBody` rebuilds the body from the known fields, so an `address` that was missing or not a string vanished and the caller got an API error about a config they never wrote (`{"egressProxy":{}}`). It now raises `InvalidArgumentError` naming the option, the way the Python SDK already did:
+
+```ts
+// InvalidArgumentError: network egressProxy must be an object with a string
+// 'address' (e.g. 'proxy.example.com:1080').
+await Sandbox.create({
+ network: { egressProxy: 'proxy.example.com:1080' as never },
+})
+```
+
+A `null` / `None` username or password is now treated as absent instead of being serialized as a JSON null the API rejects — reading a credential out of an unset environment variable is how a caller lands there, and it means the proxy takes no credentials:
+
+```ts
+await Sandbox.create({
+ network: {
+ egressProxy: {
+ address: 'proxy.example.com:1080',
+ // Unset in the environment; the proxy takes no credentials.
+ username: process.env.PROXY_USER,
+ },
+ },
+})
+```
+
+```python
+Sandbox.create(
+ network={
+ "egress_proxy": {
+ "address": "proxy.example.com:1080",
+ "username": os.environ.get("PROXY_USER"),
+ },
+ },
+)
+```
diff --git a/packages/js-sdk/CHANGELOG.md b/packages/js-sdk/CHANGELOG.md
index c0002e840..4fa1dbad2 100644
--- a/packages/js-sdk/CHANGELOG.md
+++ b/packages/js-sdk/CHANGELOG.md
@@ -267,7 +267,7 @@
```python
info = sandbox.get_info()
- print(info.network["egress_proxy"])
+ print((info.network or {}).get("egress_proxy"))
# {'address': 'proxy.example.com:1080', 'username': 'proxy-user'}
```
diff --git a/packages/js-sdk/src/sandbox/sandboxApi.ts b/packages/js-sdk/src/sandbox/sandboxApi.ts
index 3af6b1bac..1fbc7a9fc 100644
--- a/packages/js-sdk/src/sandbox/sandboxApi.ts
+++ b/packages/js-sdk/src/sandbox/sandboxApi.ts
@@ -1021,20 +1021,29 @@ function resolveRulesForBody(
/**
* Rebuild the proxy config from the known fields so stray properties on the
* caller's object never reach the wire and a later mutation of it cannot alter
- * the in-flight request. Validation is the server's — it is the only side that
- * can tell whether the address resolves, and to where.
+ * the in-flight request. Address reachability is the server's — it is the only
+ * side that can tell whether the address resolves, and to where. The required
+ * `address` is checked here so an untyped caller that omits it is told which
+ * option is wrong instead of getting an API error about a body it never wrote.
*/
function buildEgressProxyBody(
egressProxy: SandboxEgressProxyOpts
): components['schemas']['SandboxEgressProxyConfig'] {
+ // Re-check at runtime for callers that bypass the type; Python's
+ // `_build_egress_proxy` checks the same way.
+ if (!isPlainObject(egressProxy) || typeof egressProxy.address !== 'string') {
+ throw new InvalidArgumentError(
+ `network egressProxy must be an object with a string 'address' (e.g. 'proxy.example.com:1080').`
+ )
+ }
+
+ // `!= null` also skips a `null` credential, which is what reading one out of
+ // an unset environment variable yields and what "this proxy takes no
+ // credentials" means; the API only accepts a string.
return {
address: egressProxy.address,
- ...(egressProxy.username !== undefined
- ? { username: egressProxy.username }
- : {}),
- ...(egressProxy.password !== undefined
- ? { password: egressProxy.password }
- : {}),
+ ...(egressProxy.username != null ? { username: egressProxy.username } : {}),
+ ...(egressProxy.password != null ? { password: egressProxy.password } : {}),
}
}
diff --git a/packages/js-sdk/tests/sandbox/egressProxy.test.ts b/packages/js-sdk/tests/sandbox/egressProxy.test.ts
index b861fd2df..a807ed34f 100644
--- a/packages/js-sdk/tests/sandbox/egressProxy.test.ts
+++ b/packages/js-sdk/tests/sandbox/egressProxy.test.ts
@@ -129,6 +129,45 @@ test('Sandbox.create strips unknown egress proxy properties', async () => {
})
})
+test.for([
+ ['an empty object', {}],
+ ['a non-string address', { address: 1080 }],
+ ['a bare address string', 'proxy.example.com:1080'],
+])(
+ 'Sandbox.create rejects %s as the egress proxy',
+ async ([, egressProxy]: [string, unknown]) => {
+ // Callers that bypass the type used to have the address silently dropped
+ // from the rebuilt body and got an API error about a config they never
+ // wrote; Python raises here too.
+ await expect(
+ Sandbox.create('base', {
+ apiKey: TEST_API_KEY,
+ network: { egressProxy: egressProxy as never },
+ })
+ ).rejects.toThrow(/egressProxy/)
+ }
+)
+
+test('Sandbox.create omits null credentials', async () => {
+ // Reading a credential out of an unset environment variable is how a caller
+ // lands here, and it means the proxy takes no credentials — the API only
+ // accepts a string.
+ await Sandbox.create('base', {
+ apiKey: TEST_API_KEY,
+ network: {
+ egressProxy: {
+ address: 'proxy.example.com:1080',
+ username: null,
+ password: null,
+ } as never,
+ },
+ })
+
+ expect(lastCreateBody?.network.egressProxy).toEqual({
+ address: 'proxy.example.com:1080',
+ })
+})
+
test('a later mutation of the caller object does not reach the wire', async () => {
const egressProxy = { address: 'proxy.example.com:1080', username: 'before' }
diff --git a/packages/python-sdk/CHANGELOG.md b/packages/python-sdk/CHANGELOG.md
index a6d58abca..a49513fc1 100644
--- a/packages/python-sdk/CHANGELOG.md
+++ b/packages/python-sdk/CHANGELOG.md
@@ -258,7 +258,7 @@
```python
info = sandbox.get_info()
- print(info.network["egress_proxy"])
+ print((info.network or {}).get("egress_proxy"))
# {'address': 'proxy.example.com:1080', 'username': 'proxy-user'}
```
diff --git a/packages/python-sdk/e2b/sandbox/sandbox_api.py b/packages/python-sdk/e2b/sandbox/sandbox_api.py
index d53c84915..14a2c1f2e 100644
--- a/packages/python-sdk/e2b/sandbox/sandbox_api.py
+++ b/packages/python-sdk/e2b/sandbox/sandbox_api.py
@@ -670,10 +670,13 @@ def _build_egress_proxy(
"(e.g. 'proxy.example.com:1080')."
)
+ # `is not None` also skips a ``None`` credential, which is what reading one
+ # out of an unset environment variable yields and what "this proxy takes no
+ # credentials" means; the API only accepts a string.
body = ClientSandboxEgressProxyConfig(address=egress_proxy["address"])
- if "username" in egress_proxy:
+ if egress_proxy.get("username") is not None:
body.username = egress_proxy["username"]
- if "password" in egress_proxy:
+ if egress_proxy.get("password") is not None:
body.password = egress_proxy["password"]
return body
diff --git a/packages/python-sdk/tests/shared/sandbox/test_egress_proxy.py b/packages/python-sdk/tests/shared/sandbox/test_egress_proxy.py
index e097ee171..75f3f1bc3 100644
--- a/packages/python-sdk/tests/shared/sandbox/test_egress_proxy.py
+++ b/packages/python-sdk/tests/shared/sandbox/test_egress_proxy.py
@@ -85,6 +85,26 @@ def test_create_rejects_a_malformed_egress_proxy(egress_proxy):
build_network_config(cast(Any, {"egress_proxy": egress_proxy}))
+def test_create_omits_none_credentials():
+ # Reading a credential out of an unset environment variable is how a caller
+ # lands here, and it means the proxy takes no credentials — the API only
+ # accepts a string.
+ network = cast(
+ Any,
+ {
+ "egress_proxy": {
+ "address": "proxy.example.com:1080",
+ "username": None,
+ "password": None,
+ },
+ },
+ )
+
+ body = build_network_config(network)
+ assert body is not None
+ assert body["egress_proxy"].to_dict() == {"address": "proxy.example.com:1080"}
+
+
def test_create_strips_unknown_egress_proxy_keys():
# An untyped caller can copy an extra key out of a config file; the API
# rejects unknown properties.