@cloudflare/sandbox 0.2.4 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. package/CHANGELOG.md +75 -0
  2. package/Dockerfile +9 -11
  3. package/README.md +69 -7
  4. package/container_src/control-process.ts +784 -0
  5. package/container_src/handler/exec.ts +99 -254
  6. package/container_src/handler/file.ts +179 -837
  7. package/container_src/handler/git.ts +28 -80
  8. package/container_src/handler/process.ts +443 -515
  9. package/container_src/handler/session.ts +92 -0
  10. package/container_src/index.ts +68 -130
  11. package/container_src/isolation.ts +1038 -0
  12. package/container_src/shell-escape.ts +42 -0
  13. package/container_src/types.ts +27 -13
  14. package/dist/{chunk-HHUDRGPY.js → chunk-BEQUGUY4.js} +2 -2
  15. package/dist/{chunk-CKIGERRS.js → chunk-LFLJGISB.js} +240 -264
  16. package/dist/chunk-LFLJGISB.js.map +1 -0
  17. package/dist/{chunk-3CQ6THKA.js → chunk-SMUEY5JR.js} +85 -103
  18. package/dist/chunk-SMUEY5JR.js.map +1 -0
  19. package/dist/{client-Ce40ujDF.d.ts → client-Dny_ro_v.d.ts} +41 -25
  20. package/dist/client.d.ts +1 -1
  21. package/dist/client.js +1 -1
  22. package/dist/index.d.ts +2 -2
  23. package/dist/index.js +8 -9
  24. package/dist/interpreter.d.ts +1 -1
  25. package/dist/jupyter-client.d.ts +1 -1
  26. package/dist/jupyter-client.js +2 -2
  27. package/dist/request-handler.d.ts +1 -1
  28. package/dist/request-handler.js +3 -5
  29. package/dist/sandbox.d.ts +1 -1
  30. package/dist/sandbox.js +3 -5
  31. package/dist/types.d.ts +10 -21
  32. package/dist/types.js +35 -9
  33. package/dist/types.js.map +1 -1
  34. package/package.json +2 -2
  35. package/src/client.ts +120 -135
  36. package/src/index.ts +8 -0
  37. package/src/sandbox.ts +290 -331
  38. package/src/types.ts +15 -24
  39. package/dist/chunk-3CQ6THKA.js.map +0 -1
  40. package/dist/chunk-6EWSYSO7.js +0 -46
  41. package/dist/chunk-6EWSYSO7.js.map +0 -1
  42. package/dist/chunk-CKIGERRS.js.map +0 -1
  43. /package/dist/{chunk-HHUDRGPY.js.map → chunk-BEQUGUY4.js.map} +0 -0
package/CHANGELOG.md CHANGED
@@ -1,5 +1,80 @@
1
1
  # @cloudflare/sandbox
2
2
 
3
+ ## 0.3.1
4
+
5
+ ### Patch Changes
6
+
7
+ - [#71](https://github.com/cloudflare/sandbox-sdk/pull/71) [`fb3c9c2`](https://github.com/cloudflare/sandbox-sdk/commit/fb3c9c22242d9d4f157c26f547f1e697ef7875f9) Thanks [@ghostwriternr](https://github.com/ghostwriternr)! - Bump containers package version
8
+
9
+ - [#70](https://github.com/cloudflare/sandbox-sdk/pull/70) [`e1fa354`](https://github.com/cloudflare/sandbox-sdk/commit/e1fa354ab1bc7b0e89db4901b67028ebf1a93d0a) Thanks [@ghostwriternr](https://github.com/ghostwriternr)! - Fix escaped quotes in file write operations
10
+
11
+ - [#68](https://github.com/cloudflare/sandbox-sdk/pull/68) [`69b91d1`](https://github.com/cloudflare/sandbox-sdk/commit/69b91d1a8f6afb63262cc381ea93e94a033ed5e8) Thanks [@CyrusNuevoDia](https://github.com/CyrusNuevoDia)! - Configurable timeouts via environment variables in isolation.ts
12
+
13
+ - [#66](https://github.com/cloudflare/sandbox-sdk/pull/66) [`eca93b9`](https://github.com/cloudflare/sandbox-sdk/commit/eca93b97e40fa0d3bd9dc27af2cc214ec355b696) Thanks [@peterp](https://github.com/peterp)! - Determine if the port is specified in the URL.
14
+
15
+ ## 0.3.0
16
+
17
+ ### Minor Changes
18
+
19
+ - [#59](https://github.com/cloudflare/sandbox-sdk/pull/59) [`b6757f7`](https://github.com/cloudflare/sandbox-sdk/commit/b6757f730c34381d5a70d513944bbf9840f598ab) Thanks [@ghostwriternr](https://github.com/ghostwriternr)! - Add process isolation for sandbox commands
20
+
21
+ Implements PID namespace isolation to protect control plane processes (Jupyter, Bun) from sandboxed code. Commands executed via `exec()` now run in isolated namespaces that cannot see or interact with system processes.
22
+
23
+ **Key security improvements:**
24
+
25
+ - Control plane processes are hidden from sandboxed commands
26
+ - Platform secrets in `/proc/1/environ` are inaccessible
27
+ - Ports 8888 (Jupyter) and 3000 (Bun) are protected from hijacking
28
+
29
+ **Breaking changes:**
30
+
31
+ 1. **Removed `sessionId` parameter**: The `sessionId` parameter has been removed from all methods (`exec()`, `execStream()`, `startProcess()`, etc.). Each sandbox now maintains its own persistent session automatically.
32
+
33
+ ```javascript
34
+ // Before: manual session management
35
+ await sandbox.exec("cd /app", { sessionId: "my-session" });
36
+
37
+ // After: automatic session per sandbox
38
+ await sandbox.exec("cd /app");
39
+ ```
40
+
41
+ 2. **Commands now maintain state**: Commands within the same sandbox now share state (working directory, environment variables, background processes). Previously each command was stateless.
42
+
43
+ ```javascript
44
+ // Before: each exec was independent
45
+ await sandbox.exec("cd /app");
46
+ await sandbox.exec("pwd"); // Output: /workspace
47
+
48
+ // After: state persists in session
49
+ await sandbox.exec("cd /app");
50
+ await sandbox.exec("pwd"); // Output: /app
51
+ ```
52
+
53
+ **Migration guide:**
54
+
55
+ - Remove `sessionId` from all method calls - each sandbox maintains its own session
56
+ - If you need isolated execution contexts within the same sandbox, use `sandbox.createSession()`:
57
+ ```javascript
58
+ // Create independent sessions with different environments
59
+ const buildSession = await sandbox.createSession({
60
+ name: "build",
61
+ env: { NODE_ENV: "production" },
62
+ cwd: "/build",
63
+ });
64
+ const testSession = await sandbox.createSession({
65
+ name: "test",
66
+ env: { NODE_ENV: "test" },
67
+ cwd: "/test",
68
+ });
69
+ ```
70
+ - Environment variables set in one command persist to the next
71
+ - Background processes remain active until explicitly killed
72
+ - Requires CAP_SYS_ADMIN (available in production, falls back gracefully in dev)
73
+
74
+ ### Patch Changes
75
+
76
+ - [#62](https://github.com/cloudflare/sandbox-sdk/pull/62) [`4bedc3a`](https://github.com/cloudflare/sandbox-sdk/commit/4bedc3aba347f3d4090a6efe2c9778bac00ce74a) Thanks [@ghostwriternr](https://github.com/ghostwriternr)! - Fix broken build due to bun lockfile not being used
77
+
3
78
  ## 0.2.4
4
79
 
5
80
  ### Patch Changes
package/Dockerfile CHANGED
@@ -33,7 +33,6 @@ RUN apt-get update && apt-get install -y \
33
33
  python3-pip \
34
34
  python3.11-venv \
35
35
  # Other useful tools
36
- sudo \
37
36
  ca-certificates \
38
37
  gnupg \
39
38
  lsb-release \
@@ -43,13 +42,8 @@ RUN apt-get update && apt-get install -y \
43
42
  # Set Python 3.11 as default python3
44
43
  RUN update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.11 1
45
44
 
46
- # Install Node.js 20 LTS
47
- # Using the official NodeSource repository setup script
48
- RUN apt-get update && apt-get install -y ca-certificates curl gnupg \
49
- && mkdir -p /etc/apt/keyrings \
50
- && curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg \
51
- && echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_20.x nodistro main" | tee /etc/apt/sources.list.d/nodesource.list \
52
- && apt-get update \
45
+ # Install Node.js 20 LTS using official NodeSource setup script
46
+ RUN curl -fsSL https://deb.nodesource.com/setup_20.x | bash - \
53
47
  && apt-get install -y nodejs \
54
48
  && rm -rf /var/lib/apt/lists/*
55
49
 
@@ -73,7 +67,7 @@ RUN pip3 install --no-cache-dir \
73
67
  seaborn
74
68
 
75
69
  # Install JavaScript kernel (ijavascript) - using E2B's fork
76
- RUN npm install -g --unsafe-perm git+https://github.com/e2b-dev/ijavascript.git \
70
+ RUN npm install -g git+https://github.com/e2b-dev/ijavascript.git \
77
71
  && ijsinstall --install=global
78
72
 
79
73
  # Set up container server directory
@@ -88,11 +82,15 @@ RUN python3 --version && \
88
82
  jupyter kernelspec list
89
83
 
90
84
  # Copy container source files to server directory
91
- COPY container_src/package.json ./
92
- RUN bun install
85
+ COPY container_src/package.json container_src/bun.lock ./
86
+ RUN bun install --frozen-lockfile
93
87
 
94
88
  COPY container_src/ ./
95
89
 
90
+ # Compile TypeScript control process
91
+ # Use npx -p typescript to ensure we get the right tsc command
92
+ RUN npx -p typescript tsc control-process.ts --outDir . --module commonjs --target es2020 --esModuleInterop --skipLibCheck
93
+
96
94
  # Create clean workspace directory for users
97
95
  RUN mkdir -p /workspace
98
96
 
package/README.md CHANGED
@@ -72,7 +72,7 @@ npm install @cloudflare/sandbox
72
72
  1. **Create a Dockerfile** (temporary requirement, will be removed in future releases):
73
73
 
74
74
  ```dockerfile
75
- FROM docker.io/cloudflare/sandbox:0.2.4
75
+ FROM docker.io/cloudflare/sandbox:0.3.1
76
76
 
77
77
  # Expose the ports you want to expose
78
78
  EXPOSE 3000
@@ -254,6 +254,14 @@ console.log(result.stdout); // "production"
254
254
  - `unexposePort(port)` - Remove port exposure
255
255
  - `getExposedPorts()` - List all exposed ports with their URLs
256
256
 
257
+ #### Session Methods
258
+
259
+ - `createSession(options)` - Create an isolated execution session
260
+ - `name`: Session identifier
261
+ - `env`: Environment variables for this session
262
+ - `cwd`: Working directory
263
+ - `isolation`: Enable PID namespace isolation (requires CAP_SYS_ADMIN)
264
+
257
265
  <h2 id="code-interpreter">🧪 Code Interpreter</h2>
258
266
 
259
267
  The Sandbox SDK includes powerful code interpreter capabilities, allowing you to execute Python and JavaScript code with rich outputs including charts, tables, and formatted data.
@@ -703,17 +711,71 @@ for await (const log of parseSSEStream<LogEvent>(logStream)) {
703
711
 
704
712
  ### Session Management
705
713
 
706
- Maintain context across commands:
714
+ The SDK provides two approaches for managing execution context:
715
+
716
+ #### Implicit Sessions (Recommended)
717
+
718
+ Each sandbox maintains its own persistent session automatically:
707
719
 
708
720
  ```typescript
709
- const sessionId = crypto.randomUUID();
721
+ const sandbox = getSandbox(env.Sandbox, "my-app");
710
722
 
711
- // Commands in the same session share working directory
712
- await sandbox.exec("cd /workspace", { sessionId });
713
- await sandbox.exec("npm install", { sessionId });
714
- const app = await sandbox.startProcess("npm start", { sessionId });
723
+ // These commands share state (pwd, env vars, etc.)
724
+ await sandbox.exec("cd /app");
725
+ await sandbox.exec("pwd"); // Output: /app
726
+ await sandbox.exec("export MY_VAR=hello");
727
+ await sandbox.exec("echo $MY_VAR"); // Output: hello
715
728
  ```
716
729
 
730
+ #### Explicit Sessions for Advanced Use Cases
731
+
732
+ Create isolated execution contexts within the same sandbox:
733
+
734
+ ```typescript
735
+ const sandbox = getSandbox(env.Sandbox, "multi-env");
736
+
737
+ // Create independent sessions with different environments
738
+ const buildSession = await sandbox.createSession({
739
+ name: "build",
740
+ env: { NODE_ENV: "production" },
741
+ cwd: "/build"
742
+ });
743
+
744
+ const testSession = await sandbox.createSession({
745
+ name: "test",
746
+ env: { NODE_ENV: "test" },
747
+ cwd: "/test"
748
+ });
749
+
750
+ // Run commands in parallel with different contexts
751
+ await Promise.all([
752
+ buildSession.exec("npm run build"),
753
+ testSession.exec("npm test")
754
+ ]);
755
+ ```
756
+
757
+ #### Security with AI Agents
758
+
759
+ When using AI coding agents, separate development from execution:
760
+
761
+ ```typescript
762
+ // Phase 1: AI agent writes code (with API keys)
763
+ const devSession = await sandbox.createSession({
764
+ name: "ai-development",
765
+ env: { ANTHROPIC_API_KEY: process.env.ANTHROPIC_API_KEY }
766
+ });
767
+ await devSession.exec('opencode "build a web server"');
768
+
769
+ // Phase 2: Run the generated code (without API keys)
770
+ const appSession = await sandbox.createSession({
771
+ name: "app-runtime",
772
+ env: { PORT: "3000" } // Only app-specific vars
773
+ });
774
+ await appSession.exec("node server.js");
775
+ ```
776
+
777
+ > **Best Practice**: Keep AI agent credentials separate from your application runtime to prevent accidental exposure of API keys.
778
+
717
779
  <h2 id="debugging">🔍 Debugging</h2>
718
780
 
719
781
  Enable verbose logging: