@cloudflare/sandbox 0.0.0-ee8c772 → 0.0.0-eec5bb6

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 (42) hide show
  1. package/CHANGELOG.md +199 -0
  2. package/Dockerfile +96 -10
  3. package/README.md +806 -23
  4. package/container_src/bun.lock +76 -0
  5. package/container_src/circuit-breaker.ts +121 -0
  6. package/container_src/control-process.ts +784 -0
  7. package/container_src/handler/exec.ts +185 -0
  8. package/container_src/handler/file.ts +406 -0
  9. package/container_src/handler/git.ts +130 -0
  10. package/container_src/handler/ports.ts +314 -0
  11. package/container_src/handler/process.ts +568 -0
  12. package/container_src/handler/session.ts +92 -0
  13. package/container_src/index.ts +432 -2740
  14. package/container_src/interpreter-service.ts +276 -0
  15. package/container_src/isolation.ts +1038 -0
  16. package/container_src/mime-processor.ts +255 -0
  17. package/container_src/package.json +9 -0
  18. package/container_src/runtime/executors/javascript/node_executor.ts +123 -0
  19. package/container_src/runtime/executors/python/ipython_executor.py +338 -0
  20. package/container_src/runtime/executors/typescript/ts_executor.ts +138 -0
  21. package/container_src/runtime/process-pool.ts +464 -0
  22. package/container_src/shell-escape.ts +42 -0
  23. package/container_src/startup.sh +11 -0
  24. package/container_src/types.ts +131 -0
  25. package/package.json +6 -8
  26. package/src/client.ts +442 -1362
  27. package/src/errors.ts +219 -0
  28. package/src/index.ts +72 -126
  29. package/src/interpreter-client.ts +352 -0
  30. package/src/interpreter-types.ts +390 -0
  31. package/src/interpreter.ts +150 -0
  32. package/src/request-handler.ts +144 -0
  33. package/src/sandbox.ts +747 -0
  34. package/src/security.ts +113 -0
  35. package/src/sse-parser.ts +147 -0
  36. package/src/types.ts +502 -0
  37. package/tsconfig.json +1 -1
  38. package/tests/client.example.ts +0 -308
  39. package/tests/connection-test.ts +0 -81
  40. package/tests/simple-test.ts +0 -81
  41. package/tests/test1.ts +0 -281
  42. package/tests/test2.ts +0 -929
@@ -0,0 +1,390 @@
1
+ // Context Management
2
+ export interface CreateContextOptions {
3
+ /**
4
+ * Programming language for the context
5
+ * @default 'python'
6
+ */
7
+ language?: "python" | "javascript" | "typescript";
8
+
9
+ /**
10
+ * Working directory for the context
11
+ * @default '/workspace'
12
+ */
13
+ cwd?: string;
14
+
15
+ /**
16
+ * Environment variables for the context
17
+ */
18
+ envVars?: Record<string, string>;
19
+
20
+ /**
21
+ * Request timeout in milliseconds
22
+ * @default 30000
23
+ */
24
+ timeout?: number;
25
+ }
26
+
27
+ export interface CodeContext {
28
+ /**
29
+ * Unique identifier for the context
30
+ */
31
+ readonly id: string;
32
+
33
+ /**
34
+ * Programming language of the context
35
+ */
36
+ readonly language: string;
37
+
38
+ /**
39
+ * Current working directory
40
+ */
41
+ readonly cwd: string;
42
+
43
+ /**
44
+ * When the context was created
45
+ */
46
+ readonly createdAt: Date;
47
+
48
+ /**
49
+ * When the context was last used
50
+ */
51
+ readonly lastUsed: Date;
52
+ }
53
+
54
+ // Execution Options
55
+ export interface RunCodeOptions {
56
+ /**
57
+ * Context to run the code in. If not provided, uses default context for the language
58
+ */
59
+ context?: CodeContext;
60
+
61
+ /**
62
+ * Language to use if context is not provided
63
+ * @default 'python'
64
+ */
65
+ language?: "python" | "javascript" | "typescript";
66
+
67
+ /**
68
+ * Environment variables for this execution
69
+ */
70
+ envVars?: Record<string, string>;
71
+
72
+ /**
73
+ * Execution timeout in milliseconds
74
+ * @default 60000
75
+ */
76
+ timeout?: number;
77
+
78
+ /**
79
+ * AbortSignal for cancelling execution
80
+ */
81
+ signal?: AbortSignal;
82
+
83
+ /**
84
+ * Callback for stdout output
85
+ */
86
+ onStdout?: (output: OutputMessage) => void | Promise<void>;
87
+
88
+ /**
89
+ * Callback for stderr output
90
+ */
91
+ onStderr?: (output: OutputMessage) => void | Promise<void>;
92
+
93
+ /**
94
+ * Callback for execution results (charts, tables, etc)
95
+ */
96
+ onResult?: (result: Result) => void | Promise<void>;
97
+
98
+ /**
99
+ * Callback for execution errors
100
+ */
101
+ onError?: (error: ExecutionError) => void | Promise<void>;
102
+ }
103
+
104
+ // Output Messages
105
+ export interface OutputMessage {
106
+ /**
107
+ * The output text
108
+ */
109
+ text: string;
110
+
111
+ /**
112
+ * Timestamp of the output
113
+ */
114
+ timestamp: number;
115
+ }
116
+
117
+ // Execution Results
118
+ export interface Result {
119
+ /**
120
+ * Plain text representation
121
+ */
122
+ text?: string;
123
+
124
+ /**
125
+ * HTML representation (tables, formatted output)
126
+ */
127
+ html?: string;
128
+
129
+ /**
130
+ * PNG image data (base64 encoded)
131
+ */
132
+ png?: string;
133
+
134
+ /**
135
+ * JPEG image data (base64 encoded)
136
+ */
137
+ jpeg?: string;
138
+
139
+ /**
140
+ * SVG image data
141
+ */
142
+ svg?: string;
143
+
144
+ /**
145
+ * LaTeX representation
146
+ */
147
+ latex?: string;
148
+
149
+ /**
150
+ * Markdown representation
151
+ */
152
+ markdown?: string;
153
+
154
+ /**
155
+ * JavaScript code to execute
156
+ */
157
+ javascript?: string;
158
+
159
+ /**
160
+ * JSON data
161
+ */
162
+ json?: any;
163
+
164
+ /**
165
+ * Chart data if the result is a visualization
166
+ */
167
+ chart?: ChartData;
168
+
169
+ /**
170
+ * Raw data object
171
+ */
172
+ data?: any;
173
+
174
+ /**
175
+ * Available output formats
176
+ */
177
+ formats(): string[];
178
+ }
179
+
180
+ // Chart Data
181
+ export interface ChartData {
182
+ /**
183
+ * Type of chart
184
+ */
185
+ type:
186
+ | "line"
187
+ | "bar"
188
+ | "scatter"
189
+ | "pie"
190
+ | "histogram"
191
+ | "heatmap"
192
+ | "unknown";
193
+
194
+ /**
195
+ * Chart title
196
+ */
197
+ title?: string;
198
+
199
+ /**
200
+ * Chart data (format depends on library)
201
+ */
202
+ data: any;
203
+
204
+ /**
205
+ * Chart layout/configuration
206
+ */
207
+ layout?: any;
208
+
209
+ /**
210
+ * Additional configuration
211
+ */
212
+ config?: any;
213
+
214
+ /**
215
+ * Library that generated the chart
216
+ */
217
+ library?: "matplotlib" | "plotly" | "altair" | "seaborn" | "unknown";
218
+
219
+ /**
220
+ * Base64 encoded image if available
221
+ */
222
+ image?: string;
223
+ }
224
+
225
+ // Execution Error
226
+ export interface ExecutionError {
227
+ /**
228
+ * Error name/type (e.g., 'NameError', 'SyntaxError')
229
+ */
230
+ name: string;
231
+
232
+ /**
233
+ * Error message
234
+ */
235
+ value: string;
236
+
237
+ /**
238
+ * Stack trace
239
+ */
240
+ traceback: string[];
241
+
242
+ /**
243
+ * Line number where error occurred
244
+ */
245
+ lineNumber?: number;
246
+ }
247
+
248
+ // Serializable execution result
249
+ export interface ExecutionResult {
250
+ code: string;
251
+ logs: {
252
+ stdout: string[];
253
+ stderr: string[];
254
+ };
255
+ error?: ExecutionError;
256
+ executionCount?: number;
257
+ results: Array<{
258
+ text?: string;
259
+ html?: string;
260
+ png?: string;
261
+ jpeg?: string;
262
+ svg?: string;
263
+ latex?: string;
264
+ markdown?: string;
265
+ javascript?: string;
266
+ json?: any;
267
+ chart?: ChartData;
268
+ data?: any;
269
+ }>;
270
+ }
271
+
272
+ // Execution Result Container
273
+ export class Execution {
274
+ /**
275
+ * All results from the execution
276
+ */
277
+ public results: Result[] = [];
278
+
279
+ /**
280
+ * Accumulated stdout and stderr
281
+ */
282
+ public logs = {
283
+ stdout: [] as string[],
284
+ stderr: [] as string[],
285
+ };
286
+
287
+ /**
288
+ * Execution error if any
289
+ */
290
+ public error?: ExecutionError;
291
+
292
+ /**
293
+ * Execution count (for interpreter)
294
+ */
295
+ public executionCount?: number;
296
+
297
+ constructor(
298
+ public readonly code: string,
299
+ public readonly context: CodeContext
300
+ ) {}
301
+
302
+ /**
303
+ * Convert to a plain object for serialization
304
+ */
305
+ toJSON(): ExecutionResult {
306
+ return {
307
+ code: this.code,
308
+ logs: this.logs,
309
+ error: this.error,
310
+ executionCount: this.executionCount,
311
+ results: this.results.map((result) => ({
312
+ text: result.text,
313
+ html: result.html,
314
+ png: result.png,
315
+ jpeg: result.jpeg,
316
+ svg: result.svg,
317
+ latex: result.latex,
318
+ markdown: result.markdown,
319
+ javascript: result.javascript,
320
+ json: result.json,
321
+ chart: result.chart,
322
+ data: result.data,
323
+ })),
324
+ };
325
+ }
326
+ }
327
+
328
+ // Implementation of Result
329
+ export class ResultImpl implements Result {
330
+ constructor(private raw: any) {}
331
+
332
+ get text(): string | undefined {
333
+ return this.raw.text || this.raw.data?.["text/plain"];
334
+ }
335
+
336
+ get html(): string | undefined {
337
+ return this.raw.html || this.raw.data?.["text/html"];
338
+ }
339
+
340
+ get png(): string | undefined {
341
+ return this.raw.png || this.raw.data?.["image/png"];
342
+ }
343
+
344
+ get jpeg(): string | undefined {
345
+ return this.raw.jpeg || this.raw.data?.["image/jpeg"];
346
+ }
347
+
348
+ get svg(): string | undefined {
349
+ return this.raw.svg || this.raw.data?.["image/svg+xml"];
350
+ }
351
+
352
+ get latex(): string | undefined {
353
+ return this.raw.latex || this.raw.data?.["text/latex"];
354
+ }
355
+
356
+ get markdown(): string | undefined {
357
+ return this.raw.markdown || this.raw.data?.["text/markdown"];
358
+ }
359
+
360
+ get javascript(): string | undefined {
361
+ return this.raw.javascript || this.raw.data?.["application/javascript"];
362
+ }
363
+
364
+ get json(): any {
365
+ return this.raw.json || this.raw.data?.["application/json"];
366
+ }
367
+
368
+ get chart(): ChartData | undefined {
369
+ return this.raw.chart;
370
+ }
371
+
372
+ get data(): any {
373
+ return this.raw.data;
374
+ }
375
+
376
+ formats(): string[] {
377
+ const formats: string[] = [];
378
+ if (this.text) formats.push("text");
379
+ if (this.html) formats.push("html");
380
+ if (this.png) formats.push("png");
381
+ if (this.jpeg) formats.push("jpeg");
382
+ if (this.svg) formats.push("svg");
383
+ if (this.latex) formats.push("latex");
384
+ if (this.markdown) formats.push("markdown");
385
+ if (this.javascript) formats.push("javascript");
386
+ if (this.json) formats.push("json");
387
+ if (this.chart) formats.push("chart");
388
+ return formats;
389
+ }
390
+ }
@@ -0,0 +1,150 @@
1
+ import type { InterpreterClient } from "./interpreter-client.js";
2
+ import {
3
+ type CodeContext,
4
+ type CreateContextOptions,
5
+ Execution,
6
+ ResultImpl,
7
+ type RunCodeOptions,
8
+ } from "./interpreter-types.js";
9
+ import type { Sandbox } from "./sandbox.js";
10
+
11
+ export class CodeInterpreter {
12
+ private interpreterClient: InterpreterClient;
13
+ private contexts = new Map<string, CodeContext>();
14
+
15
+ constructor(sandbox: Sandbox) {
16
+ this.interpreterClient = sandbox.client as InterpreterClient;
17
+ }
18
+
19
+ /**
20
+ * Create a new code execution context
21
+ */
22
+ async createCodeContext(
23
+ options: CreateContextOptions = {}
24
+ ): Promise<CodeContext> {
25
+ const context = await this.interpreterClient.createCodeContext(options);
26
+ this.contexts.set(context.id, context);
27
+ return context;
28
+ }
29
+
30
+ /**
31
+ * Run code with optional context
32
+ */
33
+ async runCode(
34
+ code: string,
35
+ options: RunCodeOptions = {}
36
+ ): Promise<Execution> {
37
+ // Get or create context
38
+ let context = options.context;
39
+ if (!context) {
40
+ // Try to find or create a default context for the language
41
+ const language = options.language || "python";
42
+ context = await this.getOrCreateDefaultContext(language);
43
+ }
44
+
45
+ // Create execution object to collect results
46
+ const execution = new Execution(code, context);
47
+
48
+ // Stream execution
49
+ await this.interpreterClient.runCodeStream(context.id, code, options.language, {
50
+ onStdout: (output) => {
51
+ execution.logs.stdout.push(output.text);
52
+ if (options.onStdout) return options.onStdout(output);
53
+ },
54
+ onStderr: (output) => {
55
+ execution.logs.stderr.push(output.text);
56
+ if (options.onStderr) return options.onStderr(output);
57
+ },
58
+ onResult: async (result) => {
59
+ execution.results.push(new ResultImpl(result) as any);
60
+ if (options.onResult) return options.onResult(result);
61
+ },
62
+ onError: (error) => {
63
+ execution.error = error;
64
+ if (options.onError) return options.onError(error);
65
+ },
66
+ });
67
+
68
+ return execution;
69
+ }
70
+
71
+ /**
72
+ * Run code and return a streaming response
73
+ */
74
+ async runCodeStream(
75
+ code: string,
76
+ options: RunCodeOptions = {}
77
+ ): Promise<ReadableStream> {
78
+ // Get or create context
79
+ let context = options.context;
80
+ if (!context) {
81
+ const language = options.language || "python";
82
+ context = await this.getOrCreateDefaultContext(language);
83
+ }
84
+
85
+ // Create streaming response
86
+ const response = await this.interpreterClient.doFetch("/api/execute/code", {
87
+ method: "POST",
88
+ headers: {
89
+ "Content-Type": "application/json",
90
+ Accept: "text/event-stream",
91
+ },
92
+ body: JSON.stringify({
93
+ context_id: context.id,
94
+ code,
95
+ language: options.language,
96
+ }),
97
+ });
98
+
99
+ if (!response.ok) {
100
+ const errorData = (await response
101
+ .json()
102
+ .catch(() => ({ error: "Unknown error" }))) as { error?: string };
103
+ throw new Error(
104
+ errorData.error || `Failed to execute code: ${response.status}`
105
+ );
106
+ }
107
+
108
+ if (!response.body) {
109
+ throw new Error("No response body for streaming execution");
110
+ }
111
+
112
+ return response.body;
113
+ }
114
+
115
+ /**
116
+ * List all code contexts
117
+ */
118
+ async listCodeContexts(): Promise<CodeContext[]> {
119
+ const contexts = await this.interpreterClient.listCodeContexts();
120
+
121
+ // Update local cache
122
+ for (const context of contexts) {
123
+ this.contexts.set(context.id, context);
124
+ }
125
+
126
+ return contexts;
127
+ }
128
+
129
+ /**
130
+ * Delete a code context
131
+ */
132
+ async deleteCodeContext(contextId: string): Promise<void> {
133
+ await this.interpreterClient.deleteCodeContext(contextId);
134
+ this.contexts.delete(contextId);
135
+ }
136
+
137
+ private async getOrCreateDefaultContext(
138
+ language: "python" | "javascript" | "typescript"
139
+ ): Promise<CodeContext> {
140
+ // Check if we have a cached context for this language
141
+ for (const context of this.contexts.values()) {
142
+ if (context.language === language) {
143
+ return context;
144
+ }
145
+ }
146
+
147
+ // Create new default context
148
+ return this.createCodeContext({ language });
149
+ }
150
+ }
@@ -0,0 +1,144 @@
1
+ import { getSandbox, type Sandbox } from "./sandbox";
2
+ import {
3
+ logSecurityEvent,
4
+ sanitizeSandboxId,
5
+ validatePort
6
+ } from "./security";
7
+
8
+ export interface SandboxEnv {
9
+ Sandbox: DurableObjectNamespace<Sandbox>;
10
+ }
11
+
12
+ export interface RouteInfo {
13
+ port: number;
14
+ sandboxId: string;
15
+ path: string;
16
+ }
17
+
18
+ export async function proxyToSandbox<E extends SandboxEnv>(
19
+ request: Request,
20
+ env: E
21
+ ): Promise<Response | null> {
22
+ try {
23
+ const url = new URL(request.url);
24
+ const routeInfo = extractSandboxRoute(url);
25
+
26
+ if (!routeInfo) {
27
+ return null; // Not a request to an exposed container port
28
+ }
29
+
30
+ const { sandboxId, port, path } = routeInfo;
31
+ const sandbox = getSandbox(env.Sandbox, sandboxId);
32
+
33
+ // Build proxy request with proper headers
34
+ let proxyUrl: string;
35
+
36
+ // Route based on the target port
37
+ if (port !== 3000) {
38
+ // Route directly to user's service on the specified port
39
+ proxyUrl = `http://localhost:${port}${path}${url.search}`;
40
+ } else {
41
+ // Port 3000 is our control plane - route normally
42
+ proxyUrl = `http://localhost:3000${path}${url.search}`;
43
+ }
44
+
45
+ const proxyRequest = new Request(proxyUrl, {
46
+ method: request.method,
47
+ headers: {
48
+ ...Object.fromEntries(request.headers),
49
+ 'X-Original-URL': request.url,
50
+ 'X-Forwarded-Host': url.hostname,
51
+ 'X-Forwarded-Proto': url.protocol.replace(':', ''),
52
+ 'X-Sandbox-Name': sandboxId, // Pass the friendly name
53
+ },
54
+ body: request.body,
55
+ });
56
+
57
+ return sandbox.containerFetch(proxyRequest, port);
58
+ } catch (error) {
59
+ console.error('[Sandbox] Proxy routing error:', error);
60
+ return new Response('Proxy routing error', { status: 500 });
61
+ }
62
+ }
63
+
64
+ function extractSandboxRoute(url: URL): RouteInfo | null {
65
+ // Parse subdomain pattern: port-sandboxId.domain
66
+ const subdomainMatch = url.hostname.match(/^(\d{4,5})-([^.-][^.]*[^.-]|[^.-])\.(.+)$/);
67
+
68
+ if (!subdomainMatch) {
69
+ // Log malformed subdomain attempts
70
+ if (url.hostname.includes('-') && url.hostname.includes('.')) {
71
+ logSecurityEvent('MALFORMED_SUBDOMAIN_ATTEMPT', {
72
+ hostname: url.hostname,
73
+ url: url.toString()
74
+ }, 'medium');
75
+ }
76
+ return null;
77
+ }
78
+
79
+ const portStr = subdomainMatch[1];
80
+ const sandboxId = subdomainMatch[2];
81
+ const domain = subdomainMatch[3];
82
+
83
+ const port = parseInt(portStr, 10);
84
+ if (!validatePort(port)) {
85
+ logSecurityEvent('INVALID_PORT_IN_SUBDOMAIN', {
86
+ port,
87
+ portStr,
88
+ sandboxId,
89
+ hostname: url.hostname,
90
+ url: url.toString()
91
+ }, 'high');
92
+ return null;
93
+ }
94
+
95
+ let sanitizedSandboxId: string;
96
+ try {
97
+ sanitizedSandboxId = sanitizeSandboxId(sandboxId);
98
+ } catch (error) {
99
+ logSecurityEvent('INVALID_SANDBOX_ID_IN_SUBDOMAIN', {
100
+ sandboxId,
101
+ port,
102
+ hostname: url.hostname,
103
+ url: url.toString(),
104
+ error: error instanceof Error ? error.message : 'Unknown error'
105
+ }, 'high');
106
+ return null;
107
+ }
108
+
109
+ // DNS subdomain length limit is 63 characters
110
+ if (sandboxId.length > 63) {
111
+ logSecurityEvent('SANDBOX_ID_LENGTH_VIOLATION', {
112
+ sandboxId,
113
+ length: sandboxId.length,
114
+ port,
115
+ hostname: url.hostname
116
+ }, 'medium');
117
+ return null;
118
+ }
119
+
120
+ logSecurityEvent('SANDBOX_ROUTE_EXTRACTED', {
121
+ port,
122
+ sandboxId: sanitizedSandboxId,
123
+ domain,
124
+ path: url.pathname || "/",
125
+ hostname: url.hostname
126
+ }, 'low');
127
+
128
+ return {
129
+ port,
130
+ sandboxId: sanitizedSandboxId,
131
+ path: url.pathname || "/",
132
+ };
133
+ }
134
+
135
+ export function isLocalhostPattern(hostname: string): boolean {
136
+ const hostPart = hostname.split(":")[0];
137
+ return (
138
+ hostPart === "localhost" ||
139
+ hostPart === "127.0.0.1" ||
140
+ hostPart === "::1" ||
141
+ hostPart === "[::1]" ||
142
+ hostPart === "0.0.0.0"
143
+ );
144
+ }