Skip to content

armgit / E2B

Public Imported from GitHub
0

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

fix(js-sdk): validate sandbox create options before requiring an API key (#1853)

devin-ai-integration[bot]90e3fc564c60
diff --git a/.changeset/lifecycle-validate-before-auth.md b/.changeset/lifecycle-validate-before-auth.md
new file mode 100644
index 000000000..3c5f50fcb
--- /dev/null
+++ b/.changeset/lifecycle-validate-before-auth.md
@@ -0,0 +1,5 @@
+---
+'e2b': patch
+---
+
+Reject invalid `Sandbox.create` lifecycle options and `Sandbox.connect` `onResume` values before requiring an API key, matching the Python SDK.
diff --git a/packages/code-interpreter-python/uv.lock b/packages/code-interpreter-python/uv.lock
index f3d94c3fb..8ebd0716c 100644
--- a/packages/code-interpreter-python/uv.lock
+++ b/packages/code-interpreter-python/uv.lock
@@ -168,7 +168,7 @@ resolution-markers = [
"python_full_version < '3.11'",
]
dependencies = [
- { name = "numpy", version = "2.2.6", source = { registry = "https://pypi.org/simple" }, marker = "python_full_version < '3.11'" },
+ { name = "numpy", version = "2.2.6", source = { registry = "https://pypi.org/simple" } },
]
sdist = { url = "https://files.pythonhosted.org/packages/66/54/eb9bfc647b19f2009dd5c7f5ec51c4e6ca831725f1aea7a993034f483147/contourpy-1.3.2.tar.gz", hash = "sha256:b6945942715a034c671b7fc54f9588126b0b8bf23db2696e3ca8328f3ff0ab54", size = 13466130, upload-time = "2025-04-15T17:47:53.79Z" }
wheels = [
@@ -240,7 +240,7 @@ resolution-markers = [
"python_full_version == '3.11.*'",
]
dependencies = [
- { name = "numpy", version = "2.4.6", source = { registry = "https://pypi.org/simple" }, marker = "python_full_version == '3.11.*'" },
+ { name = "numpy", version = "2.4.6", source = { registry = "https://pypi.org/simple" }, marker = "python_full_version < '3.12'" },
{ name = "numpy", version = "2.5.2", source = { registry = "https://pypi.org/simple" }, marker = "python_full_version >= '3.12'" },
]
sdist = { url = "https://files.pythonhosted.org/packages/58/01/1253e6698a07380cd31a736d248a3f2a50a7c88779a1813da27503cadc2a/contourpy-1.3.3.tar.gz", hash = "sha256:083e12155b210502d0bca491432bb04d56dc3432f95a979b429f2848c3dbe880", size = 13466174, upload-time = "2025-07-26T12:03:12.549Z" }
@@ -498,7 +498,7 @@ name = "exceptiongroup"
version = "1.3.1"
source = { registry = "https://pypi.org/simple" }
dependencies = [
- { name = "typing-extensions", marker = "python_full_version < '3.11'" },
+ { name = "typing-extensions" },
]
sdist = { url = "https://files.pythonhosted.org/packages/50/79/66800aadf48771f6b62f7eb014e352e5d06856655206165d775e675a02c9/exceptiongroup-1.3.1.tar.gz", hash = "sha256:8b412432c6055b0b7d14c310000ae93352ed6754f70fa8f7c34141f91c4e3219", size = 30371, upload-time = "2025-11-21T23:01:54.787Z" }
wheels = [
@@ -929,15 +929,15 @@ resolution-markers = [
"python_full_version < '3.11'",
]
dependencies = [
- { name = "contourpy", version = "1.3.2", source = { registry = "https://pypi.org/simple" }, marker = "python_full_version < '3.11'" },
- { name = "cycler", marker = "python_full_version < '3.11'" },
- { name = "fonttools", marker = "python_full_version < '3.11'" },
- { name = "kiwisolver", marker = "python_full_version < '3.11'" },
- { name = "numpy", version = "2.2.6", source = { registry = "https://pypi.org/simple" }, marker = "python_full_version < '3.11'" },
- { name = "packaging", marker = "python_full_version < '3.11'" },
- { name = "pillow", marker = "python_full_version < '3.11'" },
- { name = "pyparsing", marker = "python_full_version < '3.11'" },
- { name = "python-dateutil", marker = "python_full_version < '3.11'" },
+ { name = "contourpy", version = "1.3.2", source = { registry = "https://pypi.org/simple" } },
+ { name = "cycler" },
+ { name = "fonttools" },
+ { name = "kiwisolver" },
+ { name = "numpy", version = "2.2.6", source = { registry = "https://pypi.org/simple" } },
+ { name = "packaging" },
+ { name = "pillow" },
+ { name = "pyparsing" },
+ { name = "python-dateutil" },
]
sdist = { url = "https://files.pythonhosted.org/packages/63/1b/4be5be87d43d327a0cf4de1a56e86f7f84c89312452406cf122efe2839e6/matplotlib-3.10.9.tar.gz", hash = "sha256:fd66508e8c6877d98e586654b608a0456db8d7e8a546eb1e2600efd957302358", size = 34811233, upload-time = "2026-04-24T00:14:13.539Z" }
wheels = [
@@ -1007,16 +1007,16 @@ resolution-markers = [
"python_full_version == '3.11.*'",
]
dependencies = [
- { name = "contourpy", version = "1.3.3", source = { registry = "https://pypi.org/simple" }, marker = "python_full_version >= '3.11'" },
- { name = "cycler", marker = "python_full_version >= '3.11'" },
- { name = "fonttools", marker = "python_full_version >= '3.11'" },
- { name = "kiwisolver", marker = "python_full_version >= '3.11'" },
- { name = "numpy", version = "2.4.6", source = { registry = "https://pypi.org/simple" }, marker = "python_full_version == '3.11.*'" },
+ { name = "contourpy", version = "1.3.3", source = { registry = "https://pypi.org/simple" } },
+ { name = "cycler" },
+ { name = "fonttools" },
+ { name = "kiwisolver" },
+ { name = "numpy", version = "2.4.6", source = { registry = "https://pypi.org/simple" }, marker = "python_full_version < '3.12'" },
{ name = "numpy", version = "2.5.2", source = { registry = "https://pypi.org/simple" }, marker = "python_full_version >= '3.12'" },
- { name = "packaging", marker = "python_full_version >= '3.11'" },
- { name = "pillow", marker = "python_full_version >= '3.11'" },
- { name = "pyparsing", marker = "python_full_version >= '3.11'" },
- { name = "python-dateutil", marker = "python_full_version >= '3.11'" },
+ { name = "packaging" },
+ { name = "pillow" },
+ { name = "pyparsing" },
+ { name = "python-dateutil" },
]
sdist = { url = "https://files.pythonhosted.org/packages/49/64/f9a391af28f518b11ad45a8a712353c94a0aefce09d3703200e5c54b610a/matplotlib-3.11.1.tar.gz", hash = "sha256:69647db5746941c793d6e445a4cd349323ffb87d9cc958c2ad84a659b4832d30", size = 32612045, upload-time = "2026-07-18T03:39:46.63Z" }
wheels = [
diff --git a/packages/js-sdk/src/sandbox/sandboxApi.ts b/packages/js-sdk/src/sandbox/sandboxApi.ts
index 2dd9c392d..0b6f215a5 100644
--- a/packages/js-sdk/src/sandbox/sandboxApi.ts
+++ b/packages/js-sdk/src/sandbox/sandboxApi.ts
@@ -1655,9 +1655,6 @@ export class SandboxApi extends ClientFactory {
timeoutMs: number,
opts?: SandboxOpts
) {
- const apiOpts = this.resolveOpts(opts)
- const config = new ConnectionConfig(apiOpts)
- const client = new ApiClient(config)
// onTimeout accepts a bare action (`'pause'` / `'kill'`) or the object form
// `{ action, keepMemory }`. The discriminated union type forbids `keepMemory`
// on `action: 'kill'`; re-check at runtime for untyped callers.
@@ -1740,6 +1737,9 @@ export class SandboxApi extends ClientFactory {
)
}
+ const apiOpts = this.resolveOpts(opts)
+ const config = new ConnectionConfig(apiOpts)
+ const client = new ApiClient(config)
const res = await client.api.POST('/sandboxes', {
body,
signal: config.getSignal(apiOpts?.requestTimeoutMs, apiOpts?.signal),
@@ -1842,9 +1842,6 @@ export class SandboxApi extends ClientFactory {
const apiOpts = this.resolveOpts(opts)
const timeoutMs = apiOpts?.timeoutMs ?? DEFAULT_SANDBOX_TIMEOUT_MS
- const config = new ConnectionConfig(apiOpts)
- const client = new ApiClient(config)
-
// A nullish value is not a choice of restore, matching every other nullish
// option. Any other value outside the union never reaches the API — it is
// resolved here into the boolean memory field — so it cannot be rejected
@@ -1857,6 +1854,9 @@ export class SandboxApi extends ClientFactory {
)
}
+ const config = new ConnectionConfig(apiOpts)
+ const client = new ApiClient(config)
+
const res = await client.api.POST('/sandboxes/{sandboxID}/connect', {
params: {
path: {
diff --git a/packages/js-sdk/tests/sandbox/lifecycleRequest.test.ts b/packages/js-sdk/tests/sandbox/lifecycleRequest.test.ts
index 12fe5d5f0..c85dbed0d 100644
--- a/packages/js-sdk/tests/sandbox/lifecycleRequest.test.ts
+++ b/packages/js-sdk/tests/sandbox/lifecycleRequest.test.ts
@@ -127,6 +127,39 @@ test('Sandbox.create rejects autoResume without a timeout action', async () => {
expect(lastCreateBody).toBeUndefined()
})
+async function expectInvalidLifecycleWithoutApiKey(
+ lifecycle: NonNullable<Parameters<typeof Sandbox.create>[1]>['lifecycle']
+) {
+ const previous = process.env.E2B_API_KEY
+ delete process.env.E2B_API_KEY
+ try {
+ await expect(Sandbox.create('base', { lifecycle })).rejects.toThrowError(
+ InvalidArgumentError
+ )
+ } finally {
+ if (previous === undefined) {
+ delete process.env.E2B_API_KEY
+ } else {
+ process.env.E2B_API_KEY = previous
+ }
+ }
+ expect(lastCreateBody).toBeUndefined()
+}
+
+test('filesystem-only auto-pause with auto-resume is InvalidArgumentError without an API key', async () => {
+ await expectInvalidLifecycleWithoutApiKey({
+ onTimeout: { action: 'pause', keepMemory: false },
+ autoResume: true,
+ })
+})
+
+test('keepMemory on kill is InvalidArgumentError without an API key', async () => {
+ await expectInvalidLifecycleWithoutApiKey({
+ // @ts-expect-error keepMemory is not allowed with action: 'kill'
+ onTimeout: { action: 'kill', keepMemory: false },
+ })
+})
+
test('an explicit autoResume: false is sent', async () => {
await Sandbox.create('base', {
apiKey: TEST_API_KEY,
diff --git a/packages/js-sdk/tests/sandbox/onResumeRequest.test.ts b/packages/js-sdk/tests/sandbox/onResumeRequest.test.ts
index 4f3edcd10..a1e82a2b8 100644
--- a/packages/js-sdk/tests/sandbox/onResumeRequest.test.ts
+++ b/packages/js-sdk/tests/sandbox/onResumeRequest.test.ts
@@ -104,3 +104,23 @@ test.for(unrecognized)(
expect(lastConnectBody).toBeUndefined()
}
)
+
+test('an unrecognized onResume is InvalidArgumentError without an API key', async () => {
+ const previous = process.env.E2B_API_KEY
+ delete process.env.E2B_API_KEY
+ try {
+ await expect(
+ Sandbox.connect('test-sandbox-id', {
+ // @ts-expect-error deliberately outside the union
+ onResume: 'Reboot',
+ })
+ ).rejects.toThrowError(InvalidArgumentError)
+ } finally {
+ if (previous === undefined) {
+ delete process.env.E2B_API_KEY
+ } else {
+ process.env.E2B_API_KEY = previous
+ }
+ }
+ expect(lastConnectBody).toBeUndefined()
+})