@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
@@ -1,162 +1,164 @@
1
+ import type { FileChunk, FileMetadata, FileStreamEvent } from '@repo/shared';
2
+
1
3
  /**
2
- * File streaming utilities for reading binary and text files
3
- * Provides simple AsyncIterable API over SSE stream with automatic base64 decoding
4
+ * Parse SSE (Server-Sent Events) lines from a stream
4
5
  */
6
+ async function* parseSSE(stream: ReadableStream<Uint8Array>): AsyncGenerator<FileStreamEvent> {
7
+ const reader = stream.getReader();
8
+ const decoder = new TextDecoder();
9
+ let buffer = '';
10
+
11
+ try {
12
+ while (true) {
13
+ const { done, value } = await reader.read();
14
+
15
+ if (done) {
16
+ break;
17
+ }
18
+
19
+ buffer += decoder.decode(value, { stream: true });
20
+ const lines = buffer.split('\n');
5
21
 
6
- import { parseSSEStream } from './sse-parser';
7
- import type { FileChunk, FileMetadata, FileStreamEvent } from './types';
22
+ // Keep the last incomplete line in the buffer
23
+ buffer = lines.pop() || '';
24
+
25
+ for (const line of lines) {
26
+ if (line.startsWith('data: ')) {
27
+ const data = line.slice(6); // Remove 'data: ' prefix
28
+ try {
29
+ const event = JSON.parse(data) as FileStreamEvent;
30
+ yield event;
31
+ } catch (error) {
32
+ console.error('Failed to parse SSE event:', error);
33
+ }
34
+ }
35
+ }
36
+ }
37
+ } finally {
38
+ reader.releaseLock();
39
+ }
40
+ }
8
41
 
9
42
  /**
10
- * Convert ReadableStream of SSE file events to AsyncIterable of file chunks
11
- * Automatically decodes base64 for binary files and provides metadata
43
+ * Stream a file from the sandbox with automatic base64 decoding for binary files
12
44
  *
13
- * @param stream - The SSE ReadableStream from readFileStream()
14
- * @param signal - Optional AbortSignal for cancellation
15
- * @returns AsyncIterable that yields file chunks (string for text, Uint8Array for binary)
45
+ * @param stream - The ReadableStream from readFileStream()
46
+ * @returns AsyncGenerator that yields FileChunk (string for text, Uint8Array for binary)
16
47
  *
17
48
  * @example
18
- * ```typescript
49
+ * ```ts
19
50
  * const stream = await sandbox.readFileStream('/path/to/file.png');
20
- *
21
51
  * for await (const chunk of streamFile(stream)) {
22
52
  * if (chunk instanceof Uint8Array) {
23
- * // Binary chunk - already decoded from base64
24
- * console.log('Binary chunk:', chunk.byteLength, 'bytes');
53
+ * // Binary chunk
54
+ * console.log('Binary chunk:', chunk.length, 'bytes');
25
55
  * } else {
26
56
  * // Text chunk
27
57
  * console.log('Text chunk:', chunk);
28
58
  * }
29
59
  * }
30
- *
31
- * // Access metadata
32
- * const iter = streamFile(stream);
33
- * for await (const chunk of iter) {
34
- * console.log('MIME type:', iter.metadata?.mimeType);
35
- * // process chunk...
36
- * }
37
60
  * ```
38
61
  */
39
- export async function* streamFile(
40
- stream: ReadableStream<Uint8Array>,
41
- signal?: AbortSignal
42
- ): AsyncGenerator<FileChunk, void, undefined> {
43
- let metadata: FileMetadata | undefined;
62
+ export async function* streamFile(stream: ReadableStream<Uint8Array>): AsyncGenerator<FileChunk, FileMetadata> {
63
+ let metadata: FileMetadata | null = null;
44
64
 
45
- try {
46
- for await (const event of parseSSEStream<FileStreamEvent>(stream, signal)) {
47
- switch (event.type) {
48
- case 'metadata':
49
- // Store metadata for access via iterator
50
- metadata = {
51
- mimeType: event.mimeType,
52
- size: event.size,
53
- isBinary: event.isBinary,
54
- encoding: event.encoding,
55
- };
56
- // Store on generator function for external access
57
- (streamFile as any).metadata = metadata;
58
- break;
59
-
60
- case 'chunk':
61
- // Auto-decode base64 for binary files
62
- if (metadata?.isBinary && metadata?.encoding === 'base64') {
63
- // Decode base64 to Uint8Array
64
- const binaryString = atob(event.data);
65
- const bytes = new Uint8Array(binaryString.length);
66
- for (let i = 0; i < binaryString.length; i++) {
67
- bytes[i] = binaryString.charCodeAt(i);
68
- }
69
- yield bytes;
70
- } else {
71
- // Text file - yield as-is
72
- yield event.data;
65
+ for await (const event of parseSSE(stream)) {
66
+ switch (event.type) {
67
+ case 'metadata':
68
+ metadata = {
69
+ mimeType: event.mimeType,
70
+ size: event.size,
71
+ isBinary: event.isBinary,
72
+ encoding: event.encoding,
73
+ };
74
+ break;
75
+
76
+ case 'chunk':
77
+ if (!metadata) {
78
+ throw new Error('Received chunk before metadata');
79
+ }
80
+
81
+ if (metadata.isBinary && metadata.encoding === 'base64') {
82
+ // Decode base64 to Uint8Array for binary files
83
+ const binaryString = atob(event.data);
84
+ const bytes = new Uint8Array(binaryString.length);
85
+ for (let i = 0; i < binaryString.length; i++) {
86
+ bytes[i] = binaryString.charCodeAt(i);
73
87
  }
74
- break;
88
+ yield bytes;
89
+ } else {
90
+ // Text files - yield as-is
91
+ yield event.data;
92
+ }
93
+ break;
75
94
 
76
- case 'complete':
77
- // Stream completed successfully
78
- console.log(`[streamFile] File streaming complete: ${event.bytesRead} bytes read`);
79
- return;
95
+ case 'complete':
96
+ if (!metadata) {
97
+ throw new Error('Stream completed without metadata');
98
+ }
99
+ return metadata;
80
100
 
81
- case 'error':
82
- // Stream error
83
- throw new Error(`File streaming error: ${event.error}`);
84
- }
101
+ case 'error':
102
+ throw new Error(`File streaming error: ${event.error}`);
85
103
  }
86
- } catch (error) {
87
- console.error('[streamFile] Error streaming file:', error);
88
- throw error;
89
104
  }
105
+
106
+ throw new Error('Stream ended unexpectedly');
90
107
  }
91
108
 
92
109
  /**
93
- * Helper to collect entire file from stream into memory
94
- * Useful for smaller files where you want the complete content at once
110
+ * Collect an entire file into memory from a stream
95
111
  *
96
- * @param stream - The SSE ReadableStream from readFileStream()
97
- * @param signal - Optional AbortSignal for cancellation
98
- * @returns Object with content (string or Uint8Array) and metadata
112
+ * @param stream - The ReadableStream from readFileStream()
113
+ * @returns Object containing the file content and metadata
99
114
  *
100
115
  * @example
101
- * ```typescript
102
- * const stream = await sandbox.readFileStream('/path/to/image.png');
116
+ * ```ts
117
+ * const stream = await sandbox.readFileStream('/path/to/file.txt');
103
118
  * const { content, metadata } = await collectFile(stream);
104
- *
105
- * if (content instanceof Uint8Array) {
106
- * console.log('Binary file:', metadata.mimeType, content.byteLength, 'bytes');
107
- * } else {
108
- * console.log('Text file:', metadata.mimeType, content.length, 'chars');
109
- * }
119
+ * console.log('Content:', content);
120
+ * console.log('MIME type:', metadata.mimeType);
110
121
  * ```
111
122
  */
112
- export async function collectFile(
113
- stream: ReadableStream<Uint8Array>,
114
- signal?: AbortSignal
115
- ): Promise<{ content: string | Uint8Array; metadata: FileMetadata }> {
116
- let metadata: FileMetadata | undefined;
117
- const chunks: FileChunk[] = [];
118
-
119
- for await (const chunk of streamFile(stream, signal)) {
120
- chunks.push(chunk);
121
- // Capture metadata from first iteration
122
- if (!metadata && (streamFile as any).metadata) {
123
- metadata = (streamFile as any).metadata;
124
- }
123
+ export async function collectFile(stream: ReadableStream<Uint8Array>): Promise<{
124
+ content: string | Uint8Array;
125
+ metadata: FileMetadata;
126
+ }> {
127
+ const chunks: Array<string | Uint8Array> = [];
128
+
129
+ // Iterate through the generator and get the return value (metadata)
130
+ const generator = streamFile(stream);
131
+ let result = await generator.next();
132
+
133
+ while (!result.done) {
134
+ chunks.push(result.value);
135
+ result = await generator.next();
125
136
  }
126
137
 
138
+ const metadata = result.value;
139
+
127
140
  if (!metadata) {
128
- throw new Error('No metadata received from file stream');
141
+ throw new Error('Failed to get file metadata');
129
142
  }
130
143
 
131
144
  // Combine chunks based on type
132
- if (chunks.length === 0) {
133
- // Empty file
134
- return {
135
- content: metadata.isBinary ? new Uint8Array(0) : '',
136
- metadata,
137
- };
138
- }
139
-
140
- // Check if binary or text based on first chunk
141
- if (chunks[0] instanceof Uint8Array) {
142
- // Binary file - concatenate Uint8Arrays
143
- const totalLength = chunks.reduce((sum, chunk) => {
144
- return sum + (chunk as Uint8Array).byteLength;
145
- }, 0);
146
-
147
- const result = new Uint8Array(totalLength);
145
+ if (metadata.isBinary) {
146
+ // Binary file - combine Uint8Arrays
147
+ const totalLength = chunks.reduce((sum, chunk) =>
148
+ sum + (chunk instanceof Uint8Array ? chunk.length : 0), 0
149
+ );
150
+ const combined = new Uint8Array(totalLength);
148
151
  let offset = 0;
149
152
  for (const chunk of chunks) {
150
- result.set(chunk as Uint8Array, offset);
151
- offset += (chunk as Uint8Array).byteLength;
153
+ if (chunk instanceof Uint8Array) {
154
+ combined.set(chunk, offset);
155
+ offset += chunk.length;
156
+ }
152
157
  }
153
-
154
- return { content: result, metadata };
158
+ return { content: combined, metadata };
155
159
  } else {
156
- // Text file - concatenate strings
157
- return {
158
- content: chunks.join(''),
159
- metadata,
160
- };
160
+ // Text file - combine strings
161
+ const combined = chunks.filter(c => typeof c === 'string').join('');
162
+ return { content: combined, metadata };
161
163
  }
162
164
  }
package/src/index.ts CHANGED
@@ -1,81 +1,93 @@
1
- // biome-ignore-start assist/source/organizeImports: Need separate exports for deprecation warnings to work properly
2
- /**
3
- * @deprecated Use `InterpreterNotReadyError` instead. Will be removed in a future version.
4
- */
5
- export { InterpreterNotReadyError as JupyterNotReadyError } from "./errors";
1
+ // Export the main Sandbox class and utilities
6
2
 
7
- /**
8
- * @deprecated Use `isInterpreterNotReadyError` instead. Will be removed in a future version.
9
- */
10
- export { isInterpreterNotReadyError as isJupyterNotReadyError } from "./errors";
11
- // biome-ignore-end assist/source/organizeImports: Need separate exports for deprecation warnings to work properly
12
3
 
13
- // Export API response types
4
+ // Export the new client architecture
14
5
  export {
15
- CodeExecutionError,
16
- ContainerNotReadyError,
17
- ContextNotFoundError,
18
- InterpreterNotReadyError,
19
- isInterpreterNotReadyError,
20
- isRetryableError,
21
- isSandboxError,
22
- parseErrorResponse,
23
- SandboxError,
24
- type SandboxErrorResponse,
25
- SandboxNetworkError,
26
- ServiceUnavailableError,
27
- } from "./errors";
28
-
29
- // Export code interpreter types
30
- export type {
31
- ChartData,
32
- CodeContext,
33
- CreateContextOptions,
34
- Execution,
35
- ExecutionError,
36
- OutputMessage,
37
- Result,
38
- RunCodeOptions,
39
- } from "./interpreter-types";
40
- // Export the implementations
41
- export { ResultImpl } from "./interpreter-types";
42
- // Re-export request handler utilities
43
- export {
44
- proxyToSandbox,
45
- type RouteInfo,
46
- type SandboxEnv,
47
- } from "./request-handler";
6
+ CommandClient,
7
+ FileClient,
8
+ GitClient,
9
+ PortClient,
10
+ ProcessClient,
11
+ SandboxClient,
12
+ UtilityClient
13
+ } from "./clients";
48
14
  export { getSandbox, Sandbox } from "./sandbox";
49
- // Export SSE parser for converting ReadableStream to AsyncIterable
50
- export {
51
- asyncIterableToSSEStream,
52
- parseSSEStream,
53
- responseToAsyncIterable,
54
- } from "./sse-parser";
55
- // Export file streaming utilities
56
- export { streamFile, collectFile } from "./file-stream";
15
+
16
+ // Legacy types are now imported from the new client architecture
17
+
18
+ // Export core SDK types for consumers
57
19
  export type {
58
- DeleteFileResponse,
20
+ BaseExecOptions,
59
21
  ExecEvent,
60
22
  ExecOptions,
61
- ExecResult,
62
- ExecuteResponse,
63
- ExecutionSession,
64
- FileChunk,
65
- FileMetadata,
66
- FileStream,
67
- FileStreamEvent,
68
- GitCheckoutResponse,
23
+ ExecResult,FileChunk, FileMetadata, FileStreamEvent,
69
24
  ISandbox,
70
- ListFilesResponse,
71
25
  LogEvent,
72
- MkdirResponse,
73
- MoveFileResponse,
74
26
  Process,
75
27
  ProcessOptions,
76
28
  ProcessStatus,
77
- ReadFileResponse,
78
- RenameFileResponse,
79
- StreamOptions,
80
- WriteFileResponse,
81
- } from "./types";
29
+ StreamOptions
30
+ } from "@repo/shared";
31
+ export * from '@repo/shared';
32
+ // Export type guards for runtime validation
33
+ export {
34
+ isExecResult,
35
+ isProcess,
36
+ isProcessStatus
37
+ } from "@repo/shared";
38
+ // Export all client types from new architecture
39
+ export type {
40
+ BaseApiResponse,
41
+ CommandsResponse,
42
+ ContainerStub,
43
+ ErrorResponse,
44
+
45
+ // Command client types
46
+ ExecuteRequest,
47
+ ExecuteResponse as CommandExecuteResponse,
48
+
49
+ // Port client types
50
+ ExposePortRequest,
51
+ FileOperationRequest,
52
+
53
+ // Git client types
54
+ GitCheckoutRequest,
55
+ GitCheckoutResult,
56
+ // Base client types
57
+ HttpClientOptions as SandboxClientOptions,
58
+
59
+ // File client types
60
+ MkdirRequest,
61
+
62
+ // Utility client types
63
+ PingResponse,
64
+ PortCloseResult,
65
+ PortExposeResult,
66
+ PortListResult,
67
+ ProcessCleanupResult,
68
+ ProcessInfoResult,
69
+ ProcessKillResult,
70
+ ProcessListResult,
71
+ ProcessLogsResult,
72
+ ProcessStartResult,
73
+ ReadFileRequest,
74
+ RequestConfig,
75
+ ResponseHandler,
76
+ SessionRequest,
77
+
78
+ // Process client types
79
+ StartProcessRequest,
80
+ UnexposePortRequest,
81
+ WriteFileRequest
82
+ } from "./clients";
83
+ export type { ExecutionCallbacks, InterpreterClient } from './clients/interpreter-client.js';
84
+ // Export file streaming utilities for binary file support
85
+ export { collectFile, streamFile } from './file-stream';
86
+ // Export interpreter functionality
87
+ export { CodeInterpreter } from './interpreter.js';
88
+ // Re-export request handler utilities
89
+ export {
90
+ proxyToSandbox, type RouteInfo, type SandboxEnv
91
+ } from './request-handler';
92
+ // Export SSE parser for converting ReadableStream to AsyncIterable
93
+ export { asyncIterableToSSEStream, parseSSEStream, responseToAsyncIterable } from "./sse-parser";
@@ -1,19 +1,24 @@
1
- import type { InterpreterClient } from "./interpreter-client.js";
2
1
  import {
3
2
  type CodeContext,
4
3
  type CreateContextOptions,
5
4
  Execution,
5
+ type ExecutionError,
6
+ type OutputMessage,
7
+ type Result,
6
8
  ResultImpl,
7
9
  type RunCodeOptions,
8
- } from "./interpreter-types.js";
10
+ } from "@repo/shared";
11
+ import type { InterpreterClient } from "./clients/interpreter-client.js";
9
12
  import type { Sandbox } from "./sandbox.js";
13
+ import { validateLanguage } from "./security.js";
10
14
 
11
15
  export class CodeInterpreter {
12
16
  private interpreterClient: InterpreterClient;
13
17
  private contexts = new Map<string, CodeContext>();
14
18
 
15
19
  constructor(sandbox: Sandbox) {
16
- this.interpreterClient = sandbox.client as InterpreterClient;
20
+ // In init-testing architecture, client is a SandboxClient with an interpreter property
21
+ this.interpreterClient = (sandbox.client as any).interpreter as InterpreterClient;
17
22
  }
18
23
 
19
24
  /**
@@ -22,6 +27,9 @@ export class CodeInterpreter {
22
27
  async createCodeContext(
23
28
  options: CreateContextOptions = {}
24
29
  ): Promise<CodeContext> {
30
+ // Validate language before sending to container
31
+ validateLanguage(options.language);
32
+
25
33
  const context = await this.interpreterClient.createCodeContext(options);
26
34
  this.contexts.set(context.id, context);
27
35
  return context;
@@ -47,19 +55,19 @@ export class CodeInterpreter {
47
55
 
48
56
  // Stream execution
49
57
  await this.interpreterClient.runCodeStream(context.id, code, options.language, {
50
- onStdout: (output) => {
58
+ onStdout: (output: OutputMessage) => {
51
59
  execution.logs.stdout.push(output.text);
52
60
  if (options.onStdout) return options.onStdout(output);
53
61
  },
54
- onStderr: (output) => {
62
+ onStderr: (output: OutputMessage) => {
55
63
  execution.logs.stderr.push(output.text);
56
64
  if (options.onStderr) return options.onStderr(output);
57
65
  },
58
- onResult: async (result) => {
66
+ onResult: async (result: Result) => {
59
67
  execution.results.push(new ResultImpl(result) as any);
60
68
  if (options.onResult) return options.onResult(result);
61
69
  },
62
- onError: (error) => {
70
+ onError: (error: ExecutionError) => {
63
71
  execution.error = error;
64
72
  if (options.onError) return options.onError(error);
65
73
  },
@@ -83,7 +91,8 @@ export class CodeInterpreter {
83
91
  }
84
92
 
85
93
  // Create streaming response
86
- const response = await this.interpreterClient.doFetch("/api/execute/code", {
94
+ // Note: doFetch is protected but we need direct access for raw stream response
95
+ const response = await (this.interpreterClient as any).doFetch("/api/execute/code", {
87
96
  method: "POST",
88
97
  headers: {
89
98
  "Content-Type": "application/json",
@@ -13,6 +13,7 @@ export interface RouteInfo {
13
13
  port: number;
14
14
  sandboxId: string;
15
15
  path: string;
16
+ token: string;
16
17
  }
17
18
 
18
19
  export async function proxyToSandbox<E extends SandboxEnv>(
@@ -27,9 +28,40 @@ export async function proxyToSandbox<E extends SandboxEnv>(
27
28
  return null; // Not a request to an exposed container port
28
29
  }
29
30
 
30
- const { sandboxId, port, path } = routeInfo;
31
+ const { sandboxId, port, path, token } = routeInfo;
31
32
  const sandbox = getSandbox(env.Sandbox, sandboxId);
32
33
 
34
+ // Critical security check: Validate token (mandatory for all user ports)
35
+ // Skip check for control plane port 3000
36
+ if (port !== 3000) {
37
+ // Validate the token matches the port
38
+ const isValidToken = await sandbox.validatePortToken(port, token);
39
+ if (!isValidToken) {
40
+ logSecurityEvent('INVALID_TOKEN_ACCESS_BLOCKED', {
41
+ port,
42
+ sandboxId,
43
+ path,
44
+ hostname: url.hostname,
45
+ url: request.url,
46
+ method: request.method,
47
+ userAgent: request.headers.get('User-Agent') || 'unknown'
48
+ }, 'high');
49
+
50
+ return new Response(
51
+ JSON.stringify({
52
+ error: `Access denied: Invalid token or port not exposed`,
53
+ code: 'INVALID_TOKEN'
54
+ }),
55
+ {
56
+ status: 404,
57
+ headers: {
58
+ 'Content-Type': 'application/json'
59
+ }
60
+ }
61
+ );
62
+ }
63
+ }
64
+
33
65
  // Build proxy request with proper headers
34
66
  let proxyUrl: string;
35
67
 
@@ -52,6 +84,8 @@ export async function proxyToSandbox<E extends SandboxEnv>(
52
84
  'X-Sandbox-Name': sandboxId, // Pass the friendly name
53
85
  },
54
86
  body: request.body,
87
+ // @ts-expect-error - duplex required for body streaming in modern runtimes
88
+ duplex: 'half',
55
89
  });
56
90
 
57
91
  return sandbox.containerFetch(proxyRequest, port);
@@ -62,8 +96,8 @@ export async function proxyToSandbox<E extends SandboxEnv>(
62
96
  }
63
97
 
64
98
  function extractSandboxRoute(url: URL): RouteInfo | null {
65
- // Parse subdomain pattern: port-sandboxId.domain
66
- const subdomainMatch = url.hostname.match(/^(\d{4,5})-([^.-][^.]*[^.-]|[^.-])\.(.+)$/);
99
+ // Parse subdomain pattern: port-sandboxId-token.domain (tokens mandatory)
100
+ const subdomainMatch = url.hostname.match(/^(\d{4,5})-([^.-][^.]*[^.-]|[^.-])-([a-zA-Z0-9_-]{12,20})\.(.+)$/);
67
101
 
68
102
  if (!subdomainMatch) {
69
103
  // Log malformed subdomain attempts
@@ -78,7 +112,8 @@ function extractSandboxRoute(url: URL): RouteInfo | null {
78
112
 
79
113
  const portStr = subdomainMatch[1];
80
114
  const sandboxId = subdomainMatch[2];
81
- const domain = subdomainMatch[3];
115
+ const token = subdomainMatch[3]; // Mandatory token
116
+ const domain = subdomainMatch[4];
82
117
 
83
118
  const port = parseInt(portStr, 10);
84
119
  if (!validatePort(port)) {
@@ -122,23 +157,42 @@ function extractSandboxRoute(url: URL): RouteInfo | null {
122
157
  sandboxId: sanitizedSandboxId,
123
158
  domain,
124
159
  path: url.pathname || "/",
125
- hostname: url.hostname
160
+ hostname: url.hostname,
161
+ hasToken: !!token
126
162
  }, 'low');
127
163
 
128
164
  return {
129
165
  port,
130
166
  sandboxId: sanitizedSandboxId,
131
167
  path: url.pathname || "/",
168
+ token,
132
169
  };
133
170
  }
134
171
 
135
172
  export function isLocalhostPattern(hostname: string): boolean {
173
+ // Handle IPv6 addresses in brackets (with or without port)
174
+ if (hostname.startsWith('[')) {
175
+ if (hostname.includes(']:')) {
176
+ // [::1]:port format
177
+ const ipv6Part = hostname.substring(0, hostname.indexOf(']:') + 1);
178
+ return ipv6Part === '[::1]';
179
+ } else {
180
+ // [::1] format without port
181
+ return hostname === '[::1]';
182
+ }
183
+ }
184
+
185
+ // Handle bare IPv6 without brackets
186
+ if (hostname === '::1') {
187
+ return true;
188
+ }
189
+
190
+ // For IPv4 and regular hostnames, split on colon to remove port
136
191
  const hostPart = hostname.split(":")[0];
192
+
137
193
  return (
138
194
  hostPart === "localhost" ||
139
195
  hostPart === "127.0.0.1" ||
140
- hostPart === "::1" ||
141
- hostPart === "[::1]" ||
142
196
  hostPart === "0.0.0.0"
143
197
  );
144
198
  }