Skip to content

armgit / E2B

Public Imported from GitHub
0

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

Add sandbox metadata docs and update env var tests (#804)

0div9b4861d2eade
diff --git a/apps/web/src/app/(docs)/docs/sandbox/environment-variables/page.mdx b/apps/web/src/app/(docs)/docs/sandbox/environment-variables/page.mdx
index c6c4add31..da8dc640c 100644
--- a/apps/web/src/app/(docs)/docs/sandbox/environment-variables/page.mdx
+++ b/apps/web/src/app/(docs)/docs/sandbox/environment-variables/page.mdx
@@ -4,20 +4,32 @@ This page covers how to set and use environment variables in a sandbox, and defa
## Default environment variables
### Knowing if you are inside a sandbox
-Sometimes it's useful to know if the code is running inside a sandbox. Upon creating a sandbox, an environment variable `E2B_SANDBOX` is automatically set to `true`.
+Sometimes it's useful to know if the code is running inside a sandbox. Upon creating a sandbox, useful sandbox metadata is set as environment variables for commands:
+- `E2B_SANDBOX` is set to `true` for processes to know if they are inside our VM.
+- `E2B_SANDBOX_ID` to know the ID of the sandbox.
+- `E2B_TEAM_ID` to know the team ID that created the sandbox.
+- `E2B_TEMPLATE_ID` to know what template was used for the current sandbox.
+
You can try it out by running the following code in the sandbox:
<CodeGroup>
```js
const sandbox = await Sandbox.create()
-const result = await sandbox.commands.run('echo $E2B_SANDBOX')
+const result = await sandbox.commands.run('echo $E2B_SANDBOX_ID')
```
```python
sandbox = Sandbox()
-result = sandbox.commands.run("echo $E2B_SANDBOX")
+result = sandbox.commands.run("echo $E2B_SANDBOX_ID")
```
</CodeGroup>
+<Note>
+These default environment variables are only accessible via the SDK, when using the CLI you can find them in the form of dot files in the `/run/e2b/` dir:
+```sh
+user@e2b:~$ ls -a /run/e2b/
+.E2B_SANDBOX .E2B_SANDBOX_ID .E2B_TEAM_ID .E2B_TEMPLATE_ID
+```
+</Note>
---
@@ -52,10 +64,11 @@ sandbox = Sandbox(
</CodeGroup>
### 2. Setting environment variables when running code
-You can set environment variables for specific code execution call in the sandbox.
+You can set environment variables for specific code execution call in the sandbox. This is recommended for passing secrets.
<Note>
-If you had a global environment variable with the same name, it will be overridden.
+- These environment variables are securely scoped to the command and will not be global or accessible inside the sandbox or for subsequent commands.
+- If you had a global environment variable with the same name, it will be overridden only for the command.
</Note>
<CodeGroup>
@@ -79,10 +92,11 @@ result = sandbox.run_code(
</CodeGroup>
### 3. Setting environment variables when running commands
-You can set environment variables for specific command execution in the sandbox.
+You can set environment variables for specific command execution in the sandbox. This is recommended for passing secrets.
<Note>
-If you had a global environment variable with the same name, it will be overridden.
+- These environment variables are securely scoped to the command and will not be global or accessible inside the sandbox or for subsequent commands.
+- If you had a global environment variable with the same name, it will be overridden only for the command.
</Note>
<CodeGroup>
diff --git a/packages/js-sdk/tests/integration/randomness.test.ts b/packages/js-sdk/tests/integration/randomness.test.ts
new file mode 100644
index 000000000..a982eb31f
--- /dev/null
+++ b/packages/js-sdk/tests/integration/randomness.test.ts
@@ -0,0 +1,53 @@
+import { test, assert } from 'vitest'
+
+import Sandbox from '../../src/index.js'
+import { isIntegrationTest } from '../setup.js'
+
+const integrationTestTemplate = 'en716jw99aj63v1k8ugh'
+
+test.skipIf(!isIntegrationTest)(
+ 'test python random number generation in same sandbox',
+ async () => {
+ const sbx = await Sandbox.create(integrationTestTemplate, {
+ timeoutMs: 120,
+ })
+ console.log('sandboxId', sbx.sandboxId)
+ const cmd1 = await sbx.commands.run(
+ 'python -c "import numpy as np; print([np.random.normal(),np.random.normal(),np.random.normal()])"'
+ )
+ console.log('cmd1 stdout', cmd1.stdout)
+
+ const cmd2 = await sbx.commands.run(
+ 'python -c "import numpy as np; print([np.random.normal(),np.random.normal(),np.random.normal()])"'
+ )
+ console.log('cmd2 stdout', cmd2.stdout)
+
+ assert.notEqual(cmd1.stdout, cmd2.stdout)
+ }
+)
+
+test.skipIf(!isIntegrationTest)(
+ 'test python random number generation with same template',
+ async () => {
+ const sbx = await Sandbox.create(integrationTestTemplate, {
+ timeoutMs: 120,
+ })
+ console.log('sandboxId 1', sbx.sandboxId)
+ const cmd1 = await sbx.commands.run(
+ 'python -c "import numpy as np; print([np.random.normal(),np.random.normal(),np.random.normal()])"'
+ )
+ console.log('cmd1 stdout', cmd1.stdout)
+ await sbx.kill()
+
+ const sbx2 = await Sandbox.create(integrationTestTemplate, {
+ timeoutMs: 120,
+ })
+ console.log('sandboxId 2', sbx2.sandboxId)
+ const cmd2 = await sbx2.commands.run(
+ 'python -c "import numpy as np; print([np.random.normal(),np.random.normal(),np.random.normal()])"'
+ )
+ console.log('cmd2 stdout', cmd2.stdout)
+
+ assert.notEqual(cmd1.stdout, cmd2.stdout)
+ }
+)
diff --git a/packages/js-sdk/tests/sandbox/commands/envVars.test.ts b/packages/js-sdk/tests/sandbox/commands/envVars.test.ts
index 87cd1864f..d1965df09 100644
--- a/packages/js-sdk/tests/sandbox/commands/envVars.test.ts
+++ b/packages/js-sdk/tests/sandbox/commands/envVars.test.ts
@@ -1,24 +1,36 @@
-import { assert, test } from 'vitest'
+import { assert } from 'vitest'
import { sandboxTest, isDebug, template } from '../../setup.js'
import { Sandbox } from '../../../src'
-sandboxTest.skipIf(isDebug)('env vars', async ({ sandbox }) => {
- const cmd = await sandbox.commands.run('echo $FOO', { envs: { FOO: 'bar' } })
+sandboxTest.skipIf(isDebug)('sandbox global env vars', async () => {
+ const sandbox = await Sandbox.create(template, { envs: { FOO: 'bar' } })
+ const cmd = await sandbox.commands.run('echo $FOO')
assert.equal(cmd.exitCode, 0)
assert.equal(cmd.stdout.trim(), 'bar')
})
-test.skipIf(isDebug)('env vars on sandbox', async () => {
- const sandbox = await Sandbox.create(template, { envs: { FOO: 'bar' } })
+sandboxTest('bash command scoped env vars', async ({ sandbox }) => {
+ const cmd = await sandbox.commands.run('echo $FOO', {
+ envs: { FOO: 'bar' },
+ })
- try {
- const cmd = await sandbox.commands.run('echo "$FOO"')
+ assert.equal(cmd.exitCode, 0)
+ assert.equal(cmd.stdout.trim(), 'bar')
- assert.equal(cmd.exitCode, 0)
- assert.equal(cmd.stdout.trim(), 'bar')
- } finally {
- await sandbox.kill()
- }
+ // test that it is secure and not accessible to subsequent commands
+ const cmd2 = await sandbox.commands.run('sudo echo "$FOO"')
+ assert.equal(cmd2.exitCode, 0)
+ assert.equal(cmd2.stdout.trim(), '')
+})
+
+sandboxTest('python command scoped env vars', async ({ sandbox }) => {
+ const cmd = await sandbox.commands.run(
+ 'python3 -c "import os; print(os.environ[\'FOO\'])"',
+ { envs: { FOO: 'bar' } }
+ )
+
+ assert.equal(cmd.exitCode, 0)
+ assert.equal(cmd.stdout.trim(), 'bar')
})
diff --git a/packages/python-sdk/tests/async/sandbox_async/commands/test_env_vars.py b/packages/python-sdk/tests/async/sandbox_async/commands/test_env_vars.py
index 0e06ccc22..20b5a6f3d 100644
--- a/packages/python-sdk/tests/async/sandbox_async/commands/test_env_vars.py
+++ b/packages/python-sdk/tests/async/sandbox_async/commands/test_env_vars.py
@@ -16,3 +16,22 @@ async def test_sandbox_envs(template):
assert cmd.stdout.strip() == "bar"
finally:
await sbx.kill()
+
+
+async def test_bash_command_scoped_env_vars(async_sandbox: AsyncSandbox):
+ cmd = await async_sandbox.commands.run("echo $FOO", envs={"FOO": "bar"})
+ assert cmd.exit_code == 0
+ assert cmd.stdout.strip() == "bar"
+
+ # test that it is secure and not accessible to subsequent commands
+ cmd2 = await async_sandbox.commands.run('sudo echo "$FOO"')
+ assert cmd2.exit_code == 0
+ assert cmd2.stdout.strip() == ""
+
+
+async def test_python_command_scoped_env_vars(async_sandbox: AsyncSandbox):
+ cmd = await async_sandbox.commands.run(
+ "python3 -c \"import os; print(os.environ['FOO'])\"", envs={"FOO": "bar"}
+ )
+ assert cmd.exit_code == 0
+ assert cmd.stdout.strip() == "bar"
diff --git a/packages/python-sdk/tests/sync/sandbox_sync/commands/test_env_vars.py b/packages/python-sdk/tests/sync/sandbox_sync/commands/test_env_vars.py
index 572ca3f5f..897e4a7c3 100644
--- a/packages/python-sdk/tests/sync/sandbox_sync/commands/test_env_vars.py
+++ b/packages/python-sdk/tests/sync/sandbox_sync/commands/test_env_vars.py
@@ -16,3 +16,22 @@ def test_sandbox_envs(template):
assert cmd.stdout.strip() == "bar"
finally:
sandbox.kill()
+
+
+def test_bash_command_scoped_env_vars(sandbox: Sandbox):
+ cmd = sandbox.commands.run("echo $FOO", envs={"FOO": "bar"})
+ assert cmd.exit_code == 0
+ assert cmd.stdout.strip() == "bar"
+
+ # test that it is secure and not accessible to subsequent commands
+ cmd2 = sandbox.commands.run('sudo echo "$FOO"')
+ assert cmd2.exit_code == 0
+ assert cmd2.stdout.strip() == ""
+
+
+def test_python_command_scoped_env_vars(sandbox: Sandbox):
+ cmd = sandbox.commands.run(
+ "python3 -c \"import os; print(os.environ['FOO'])\"", envs={"FOO": "bar"}
+ )
+ assert cmd.exit_code == 0
+ assert cmd.stdout.strip() == "bar"