@cloudflare/sandbox 0.0.0-46eb4e6 → 0.0.0-485cf61

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 (95) hide show
  1. package/CHANGELOG.md +0 -6
  2. package/Dockerfile +82 -18
  3. package/README.md +89 -824
  4. package/dist/chunk-3NEP4CNV.js +99 -0
  5. package/dist/chunk-3NEP4CNV.js.map +1 -0
  6. package/dist/chunk-6IYG2RIN.js +117 -0
  7. package/dist/chunk-6IYG2RIN.js.map +1 -0
  8. package/dist/chunk-HB44YO2A.js +2331 -0
  9. package/dist/chunk-HB44YO2A.js.map +1 -0
  10. package/dist/chunk-KPVMMMIP.js +105 -0
  11. package/dist/chunk-KPVMMMIP.js.map +1 -0
  12. package/dist/chunk-NNGBXDMY.js +89 -0
  13. package/dist/chunk-NNGBXDMY.js.map +1 -0
  14. package/dist/file-stream.d.ts +43 -0
  15. package/dist/file-stream.js +9 -0
  16. package/dist/file-stream.js.map +1 -0
  17. package/dist/index.d.ts +9 -0
  18. package/dist/index.js +55 -0
  19. package/dist/index.js.map +1 -0
  20. package/dist/interpreter.d.ts +33 -0
  21. package/dist/interpreter.js +8 -0
  22. package/dist/interpreter.js.map +1 -0
  23. package/dist/request-handler.d.ts +18 -0
  24. package/dist/request-handler.js +12 -0
  25. package/dist/request-handler.js.map +1 -0
  26. package/dist/sandbox-CtlKjZwf.d.ts +583 -0
  27. package/dist/sandbox.d.ts +4 -0
  28. package/dist/sandbox.js +12 -0
  29. package/dist/sandbox.js.map +1 -0
  30. package/dist/security.d.ts +35 -0
  31. package/dist/security.js +15 -0
  32. package/dist/security.js.map +1 -0
  33. package/dist/sse-parser.d.ts +28 -0
  34. package/dist/sse-parser.js +11 -0
  35. package/dist/sse-parser.js.map +1 -0
  36. package/package.json +11 -5
  37. package/src/clients/base-client.ts +297 -0
  38. package/src/clients/command-client.ts +118 -0
  39. package/src/clients/file-client.ts +272 -0
  40. package/src/clients/git-client.ts +95 -0
  41. package/src/clients/index.ts +63 -0
  42. package/src/{interpreter-client.ts → clients/interpreter-client.ts} +151 -171
  43. package/src/clients/port-client.ts +108 -0
  44. package/src/clients/process-client.ts +180 -0
  45. package/src/clients/sandbox-client.ts +41 -0
  46. package/src/clients/types.ts +81 -0
  47. package/src/clients/utility-client.ts +97 -0
  48. package/src/errors/adapter.ts +180 -0
  49. package/src/errors/classes.ts +469 -0
  50. package/src/errors/index.ts +105 -0
  51. package/src/file-stream.ts +119 -117
  52. package/src/index.ts +81 -69
  53. package/src/interpreter.ts +17 -8
  54. package/src/request-handler.ts +61 -7
  55. package/src/sandbox.ts +698 -495
  56. package/src/security.ts +20 -0
  57. package/startup.sh +7 -0
  58. package/tests/base-client.test.ts +328 -0
  59. package/tests/command-client.test.ts +407 -0
  60. package/tests/file-client.test.ts +643 -0
  61. package/tests/file-stream.test.ts +306 -0
  62. package/tests/git-client.test.ts +328 -0
  63. package/tests/port-client.test.ts +301 -0
  64. package/tests/process-client.test.ts +658 -0
  65. package/tests/sandbox.test.ts +465 -0
  66. package/tests/sse-parser.test.ts +291 -0
  67. package/tests/utility-client.test.ts +266 -0
  68. package/tests/wrangler.jsonc +35 -0
  69. package/tsconfig.json +9 -1
  70. package/vitest.config.ts +31 -0
  71. package/container_src/bun.lock +0 -76
  72. package/container_src/circuit-breaker.ts +0 -121
  73. package/container_src/control-process.ts +0 -784
  74. package/container_src/handler/exec.ts +0 -185
  75. package/container_src/handler/file.ts +0 -457
  76. package/container_src/handler/git.ts +0 -130
  77. package/container_src/handler/ports.ts +0 -314
  78. package/container_src/handler/process.ts +0 -568
  79. package/container_src/handler/session.ts +0 -92
  80. package/container_src/index.ts +0 -600
  81. package/container_src/interpreter-service.ts +0 -276
  82. package/container_src/isolation.ts +0 -1213
  83. package/container_src/mime-processor.ts +0 -255
  84. package/container_src/package.json +0 -18
  85. package/container_src/runtime/executors/javascript/node_executor.ts +0 -123
  86. package/container_src/runtime/executors/python/ipython_executor.py +0 -338
  87. package/container_src/runtime/executors/typescript/ts_executor.ts +0 -138
  88. package/container_src/runtime/process-pool.ts +0 -464
  89. package/container_src/shell-escape.ts +0 -42
  90. package/container_src/startup.sh +0 -11
  91. package/container_src/types.ts +0 -131
  92. package/src/client.ts +0 -1048
  93. package/src/errors.ts +0 -219
  94. package/src/interpreter-types.ts +0 -390
  95. package/src/types.ts +0 -571
package/src/security.ts CHANGED
@@ -83,6 +83,26 @@ export function sanitizeSandboxId(id: string): string {
83
83
  }
84
84
 
85
85
 
86
+ /**
87
+ * Validates language for code interpreter
88
+ * Only allows supported languages
89
+ */
90
+ export function validateLanguage(language: string | undefined): void {
91
+ if (!language) {
92
+ return; // undefined is valid, will default to python
93
+ }
94
+
95
+ const supportedLanguages = ['python', 'python3', 'javascript', 'js', 'node', 'typescript', 'ts'];
96
+ const normalized = language.toLowerCase();
97
+
98
+ if (!supportedLanguages.includes(normalized)) {
99
+ throw new SecurityError(
100
+ `Unsupported language '${language}'. Supported languages: python, javascript, typescript`,
101
+ 'INVALID_LANGUAGE'
102
+ );
103
+ }
104
+ }
105
+
86
106
  /**
87
107
  * Logs security events for monitoring
88
108
  */
package/startup.sh ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+
3
+ echo "[Startup] Starting container server..."
4
+ echo "[Startup] Environment configured:"
5
+ echo " - Working directory: $(pwd)"
6
+ echo "[Startup] Starting Bun server..."
7
+ exec bun dist/index.js
@@ -0,0 +1,328 @@
1
+ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
2
+ import type { BaseApiResponse, HttpClientOptions } from '../src/clients';
3
+ import { BaseHttpClient } from '../src/clients/base-client';
4
+ import type { ErrorResponse } from '../src/errors';
5
+ import {
6
+ CommandError,
7
+ FileNotFoundError,
8
+ FileSystemError,
9
+ PermissionDeniedError,
10
+ SandboxError
11
+ } from '../src/errors';
12
+
13
+ interface TestDataResponse extends BaseApiResponse {
14
+ data: string;
15
+ }
16
+
17
+ interface TestResourceResponse extends BaseApiResponse {
18
+ id: string;
19
+ }
20
+
21
+ interface TestSourceResponse extends BaseApiResponse {
22
+ source: string;
23
+ }
24
+
25
+ interface TestStatusResponse extends BaseApiResponse {
26
+ status: string;
27
+ }
28
+
29
+ class TestHttpClient extends BaseHttpClient {
30
+ constructor(options: HttpClientOptions = {}) {
31
+ super({
32
+ baseUrl: 'http://test.com',
33
+ port: 3000,
34
+ ...options,
35
+ });
36
+ }
37
+
38
+ public async testRequest<T = BaseApiResponse>(endpoint: string, data?: Record<string, unknown>): Promise<T> {
39
+ if (data) {
40
+ return this.post<T>(endpoint, data);
41
+ }
42
+ return this.get<T>(endpoint);
43
+ }
44
+
45
+ public async testStreamRequest(endpoint: string): Promise<ReadableStream> {
46
+ const response = await this.doFetch(endpoint);
47
+ return this.handleStreamResponse(response);
48
+ }
49
+
50
+ public async testErrorHandling(errorResponse: ErrorResponse) {
51
+ const response = new Response(
52
+ JSON.stringify(errorResponse),
53
+ { status: errorResponse.httpStatus || 400 }
54
+ );
55
+ return this.handleErrorResponse(response);
56
+ }
57
+ }
58
+
59
+ describe('BaseHttpClient', () => {
60
+ let client: TestHttpClient;
61
+ let mockFetch: ReturnType<typeof vi.fn>;
62
+ let onError: ReturnType<typeof vi.fn>;
63
+
64
+ beforeEach(() => {
65
+ vi.clearAllMocks();
66
+
67
+ mockFetch = vi.fn();
68
+ global.fetch = mockFetch as unknown as typeof fetch;
69
+ onError = vi.fn();
70
+
71
+ client = new TestHttpClient({
72
+ baseUrl: 'http://test.com',
73
+ port: 3000,
74
+ onError,
75
+ });
76
+ });
77
+
78
+ afterEach(() => {
79
+ vi.restoreAllMocks();
80
+ });
81
+
82
+ describe('core request functionality', () => {
83
+ it('should handle successful API requests', async () => {
84
+ mockFetch.mockResolvedValue(new Response(
85
+ JSON.stringify({ success: true, data: 'operation completed' }),
86
+ { status: 200 }
87
+ ));
88
+
89
+ const result = await client.testRequest<TestDataResponse>('/api/test');
90
+
91
+ expect(result.success).toBe(true);
92
+ expect(result.data).toBe('operation completed');
93
+ });
94
+
95
+ it('should handle POST requests with data', async () => {
96
+ const requestData = { action: 'create', name: 'test-resource' };
97
+ mockFetch.mockResolvedValue(new Response(
98
+ JSON.stringify({ success: true, id: 'resource-123' }),
99
+ { status: 201 }
100
+ ));
101
+
102
+ const result = await client.testRequest<TestResourceResponse>('/api/create', requestData);
103
+
104
+ expect(result.success).toBe(true);
105
+ expect(result.id).toBe('resource-123');
106
+
107
+ const [url, options] = mockFetch.mock.calls[0];
108
+ expect(url).toBe('http://test.com/api/create');
109
+ expect(options.method).toBe('POST');
110
+ expect(options.headers['Content-Type']).toBe('application/json');
111
+ expect(JSON.parse(options.body)).toEqual(requestData);
112
+ });
113
+ });
114
+
115
+ describe('error handling and mapping', () => {
116
+ it('should map container errors to client errors', async () => {
117
+ const errorMappingTests = [
118
+ {
119
+ containerError: {
120
+ code: 'FILE_NOT_FOUND',
121
+ message: 'File not found: /test.txt',
122
+ context: { path: '/test.txt' },
123
+ httpStatus: 404,
124
+ timestamp: new Date().toISOString()
125
+ },
126
+ expectedError: FileNotFoundError,
127
+ },
128
+ {
129
+ containerError: {
130
+ code: 'PERMISSION_DENIED',
131
+ message: 'Permission denied',
132
+ context: { path: '/secure.txt' },
133
+ httpStatus: 403,
134
+ timestamp: new Date().toISOString()
135
+ },
136
+ expectedError: PermissionDeniedError,
137
+ },
138
+ {
139
+ containerError: {
140
+ code: 'COMMAND_EXECUTION_ERROR',
141
+ message: 'Command failed: badcmd',
142
+ context: { command: 'badcmd' },
143
+ httpStatus: 400,
144
+ timestamp: new Date().toISOString()
145
+ },
146
+ expectedError: CommandError,
147
+ },
148
+ {
149
+ containerError: {
150
+ code: 'FILESYSTEM_ERROR',
151
+ message: 'Filesystem error',
152
+ context: { path: '/test' },
153
+ httpStatus: 500,
154
+ timestamp: new Date().toISOString()
155
+ },
156
+ expectedError: FileSystemError,
157
+ },
158
+ {
159
+ containerError: {
160
+ code: 'UNKNOWN_ERROR',
161
+ message: 'Unknown error',
162
+ context: {},
163
+ httpStatus: 500,
164
+ timestamp: new Date().toISOString()
165
+ },
166
+ expectedError: SandboxError,
167
+ }
168
+ ];
169
+
170
+ for (const test of errorMappingTests) {
171
+ await expect(client.testErrorHandling(test.containerError as ErrorResponse))
172
+ .rejects.toThrow(test.expectedError);
173
+
174
+ expect(onError).toHaveBeenCalledWith(test.containerError.message, undefined);
175
+ }
176
+ });
177
+
178
+ it('should handle malformed error responses', async () => {
179
+ mockFetch.mockResolvedValue(new Response(
180
+ 'invalid json {',
181
+ { status: 500 }
182
+ ));
183
+
184
+ await expect(client.testRequest('/api/test'))
185
+ .rejects.toThrow(SandboxError);
186
+ });
187
+
188
+ it('should handle network failures', async () => {
189
+ mockFetch.mockRejectedValue(new Error('Network connection timeout'));
190
+
191
+ await expect(client.testRequest('/api/test'))
192
+ .rejects.toThrow('Network connection timeout');
193
+ });
194
+
195
+ it('should handle server unavailable scenarios', async () => {
196
+ mockFetch.mockResolvedValue(new Response(
197
+ 'Service Unavailable',
198
+ { status: 503 }
199
+ ));
200
+
201
+ await expect(client.testRequest('/api/test'))
202
+ .rejects.toThrow(SandboxError);
203
+
204
+ expect(onError).toHaveBeenCalledWith('HTTP error! status: 503', undefined);
205
+ });
206
+ });
207
+
208
+
209
+ describe('streaming functionality', () => {
210
+ it('should handle streaming responses', async () => {
211
+ const streamData = 'data: {"type":"output","content":"stream data"}\n\n';
212
+ const mockStream = new ReadableStream({
213
+ start(controller) {
214
+ controller.enqueue(new TextEncoder().encode(streamData));
215
+ controller.close();
216
+ }
217
+ });
218
+
219
+ mockFetch.mockResolvedValue(new Response(mockStream, {
220
+ status: 200,
221
+ headers: { 'Content-Type': 'text/event-stream' }
222
+ }));
223
+
224
+ const stream = await client.testStreamRequest('/api/stream');
225
+
226
+ expect(stream).toBeInstanceOf(ReadableStream);
227
+
228
+ const reader = stream.getReader();
229
+ const { done, value } = await reader.read();
230
+ const content = new TextDecoder().decode(value);
231
+
232
+ expect(done).toBe(false);
233
+ expect(content).toContain('stream data');
234
+
235
+ reader.releaseLock();
236
+ });
237
+
238
+ it('should handle streaming errors', async () => {
239
+ mockFetch.mockResolvedValue(new Response(
240
+ JSON.stringify({ error: 'Stream initialization failed', code: 'STREAM_ERROR' }),
241
+ { status: 400 }
242
+ ));
243
+
244
+ await expect(client.testStreamRequest('/api/bad-stream'))
245
+ .rejects.toThrow(SandboxError);
246
+ });
247
+
248
+ it('should handle missing stream body', async () => {
249
+ mockFetch.mockResolvedValue(new Response(null, {
250
+ status: 200,
251
+ headers: { 'Content-Type': 'text/event-stream' }
252
+ }));
253
+
254
+ await expect(client.testStreamRequest('/api/empty-stream'))
255
+ .rejects.toThrow('No response body for streaming');
256
+ });
257
+ });
258
+
259
+ describe('stub integration', () => {
260
+ it('should use stub when provided instead of fetch', async () => {
261
+ const stubFetch = vi.fn().mockResolvedValue(new Response(
262
+ JSON.stringify({ success: true, source: 'stub' }),
263
+ { status: 200 }
264
+ ));
265
+
266
+ const stub = { containerFetch: stubFetch };
267
+ const stubClient = new TestHttpClient({
268
+ baseUrl: 'http://test.com',
269
+ port: 3000,
270
+ stub,
271
+ });
272
+
273
+ const result = await stubClient.testRequest<TestSourceResponse>('/api/stub-test');
274
+
275
+ expect(result.success).toBe(true);
276
+ expect(result.source).toBe('stub');
277
+ expect(stubFetch).toHaveBeenCalledWith(
278
+ 'http://localhost:3000/api/stub-test',
279
+ { method: 'GET' },
280
+ 3000
281
+ );
282
+ expect(mockFetch).not.toHaveBeenCalled();
283
+ });
284
+
285
+ it('should handle stub errors', async () => {
286
+ const stubFetch = vi.fn().mockRejectedValue(new Error('Stub connection failed'));
287
+ const stub = { containerFetch: stubFetch };
288
+ const stubClient = new TestHttpClient({
289
+ baseUrl: 'http://test.com',
290
+ port: 3000,
291
+ stub,
292
+ });
293
+
294
+ await expect(stubClient.testRequest('/api/stub-error'))
295
+ .rejects.toThrow('Stub connection failed');
296
+ });
297
+ });
298
+
299
+ describe('edge cases and resilience', () => {
300
+ it('should handle responses with unusual status codes', async () => {
301
+ const unusualStatusTests = [
302
+ { status: 201, shouldSucceed: true },
303
+ { status: 202, shouldSucceed: true },
304
+ { status: 409, shouldSucceed: false },
305
+ { status: 422, shouldSucceed: false },
306
+ { status: 429, shouldSucceed: false },
307
+ ];
308
+
309
+ for (const test of unusualStatusTests) {
310
+ mockFetch.mockResolvedValueOnce(new Response(
311
+ test.shouldSucceed
312
+ ? JSON.stringify({ success: true, status: test.status })
313
+ : JSON.stringify({ error: `Status ${test.status}` }),
314
+ { status: test.status }
315
+ ));
316
+
317
+ if (test.shouldSucceed) {
318
+ const result = await client.testRequest<TestStatusResponse>('/api/unusual-status');
319
+ expect(result.success).toBe(true);
320
+ expect(result.status).toBe(test.status);
321
+ } else {
322
+ await expect(client.testRequest('/api/unusual-status'))
323
+ .rejects.toThrow();
324
+ }
325
+ }
326
+ });
327
+ });
328
+ });