@cloudflare/sandbox 0.0.0-d81d2a5 → 0.0.0-d8514d1
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.
- package/CHANGELOG.md +234 -0
- package/Dockerfile +172 -84
- package/README.md +92 -707
- package/dist/index.d.ts +1953 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +3280 -0
- package/dist/index.js.map +1 -0
- package/package.json +16 -8
- package/src/clients/base-client.ts +295 -0
- package/src/clients/command-client.ts +115 -0
- package/src/clients/file-client.ts +300 -0
- package/src/clients/git-client.ts +98 -0
- package/src/clients/index.ts +64 -0
- package/src/clients/interpreter-client.ts +333 -0
- package/src/clients/port-client.ts +105 -0
- package/src/clients/process-client.ts +180 -0
- package/src/clients/sandbox-client.ts +39 -0
- package/src/clients/types.ts +88 -0
- package/src/clients/utility-client.ts +156 -0
- package/src/errors/adapter.ts +238 -0
- package/src/errors/classes.ts +594 -0
- package/src/errors/index.ts +109 -0
- package/src/file-stream.ts +169 -0
- package/src/index.ts +98 -23
- package/src/interpreter.ts +62 -44
- package/src/request-handler.ts +94 -55
- package/src/sandbox.ts +887 -397
- package/src/security.ts +34 -28
- package/src/sse-parser.ts +8 -11
- package/src/version.ts +6 -0
- package/startup.sh +3 -0
- package/tests/base-client.test.ts +364 -0
- package/tests/command-client.test.ts +444 -0
- package/tests/file-client.test.ts +831 -0
- package/tests/file-stream.test.ts +310 -0
- package/tests/get-sandbox.test.ts +149 -0
- package/tests/git-client.test.ts +487 -0
- package/tests/port-client.test.ts +293 -0
- package/tests/process-client.test.ts +683 -0
- package/tests/request-handler.test.ts +292 -0
- package/tests/sandbox.test.ts +739 -0
- package/tests/sse-parser.test.ts +291 -0
- package/tests/utility-client.test.ts +339 -0
- package/tests/version.test.ts +16 -0
- package/tests/wrangler.jsonc +35 -0
- package/tsconfig.json +9 -1
- package/tsdown.config.ts +12 -0
- package/vitest.config.ts +31 -0
- package/container_src/bun.lock +0 -122
- package/container_src/handler/exec.ts +0 -340
- package/container_src/handler/file.ts +0 -844
- package/container_src/handler/git.ts +0 -182
- package/container_src/handler/ports.ts +0 -314
- package/container_src/handler/process.ts +0 -640
- package/container_src/index.ts +0 -531
- package/container_src/jupyter-server.ts +0 -336
- package/container_src/mime-processor.ts +0 -255
- package/container_src/package.json +0 -18
- package/container_src/startup.sh +0 -52
- package/container_src/types.ts +0 -108
- package/src/client.ts +0 -1021
- package/src/interpreter-types.ts +0 -383
- package/src/jupyter-client.ts +0 -266
- package/src/types.ts +0 -401
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
import type { FileChunk, FileMetadata, FileStreamEvent } from '@repo/shared';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Parse SSE (Server-Sent Events) lines from a stream
|
|
5
|
+
*/
|
|
6
|
+
async function* parseSSE(
|
|
7
|
+
stream: ReadableStream<Uint8Array>
|
|
8
|
+
): AsyncGenerator<FileStreamEvent> {
|
|
9
|
+
const reader = stream.getReader();
|
|
10
|
+
const decoder = new TextDecoder();
|
|
11
|
+
let buffer = '';
|
|
12
|
+
|
|
13
|
+
try {
|
|
14
|
+
while (true) {
|
|
15
|
+
const { done, value } = await reader.read();
|
|
16
|
+
|
|
17
|
+
if (done) {
|
|
18
|
+
break;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
buffer += decoder.decode(value, { stream: true });
|
|
22
|
+
const lines = buffer.split('\n');
|
|
23
|
+
|
|
24
|
+
// Keep the last incomplete line in the buffer
|
|
25
|
+
buffer = lines.pop() || '';
|
|
26
|
+
|
|
27
|
+
for (const line of lines) {
|
|
28
|
+
if (line.startsWith('data: ')) {
|
|
29
|
+
const data = line.slice(6); // Remove 'data: ' prefix
|
|
30
|
+
try {
|
|
31
|
+
const event = JSON.parse(data) as FileStreamEvent;
|
|
32
|
+
yield event;
|
|
33
|
+
} catch {
|
|
34
|
+
// Skip invalid JSON events and continue processing
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
} finally {
|
|
40
|
+
reader.releaseLock();
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Stream a file from the sandbox with automatic base64 decoding for binary files
|
|
46
|
+
*
|
|
47
|
+
* @param stream - The ReadableStream from readFileStream()
|
|
48
|
+
* @returns AsyncGenerator that yields FileChunk (string for text, Uint8Array for binary)
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* ```ts
|
|
52
|
+
* const stream = await sandbox.readFileStream('/path/to/file.png');
|
|
53
|
+
* for await (const chunk of streamFile(stream)) {
|
|
54
|
+
* if (chunk instanceof Uint8Array) {
|
|
55
|
+
* // Binary chunk
|
|
56
|
+
* console.log('Binary chunk:', chunk.length, 'bytes');
|
|
57
|
+
* } else {
|
|
58
|
+
* // Text chunk
|
|
59
|
+
* console.log('Text chunk:', chunk);
|
|
60
|
+
* }
|
|
61
|
+
* }
|
|
62
|
+
* ```
|
|
63
|
+
*/
|
|
64
|
+
export async function* streamFile(
|
|
65
|
+
stream: ReadableStream<Uint8Array>
|
|
66
|
+
): AsyncGenerator<FileChunk, FileMetadata> {
|
|
67
|
+
let metadata: FileMetadata | null = null;
|
|
68
|
+
|
|
69
|
+
for await (const event of parseSSE(stream)) {
|
|
70
|
+
switch (event.type) {
|
|
71
|
+
case 'metadata':
|
|
72
|
+
metadata = {
|
|
73
|
+
mimeType: event.mimeType,
|
|
74
|
+
size: event.size,
|
|
75
|
+
isBinary: event.isBinary,
|
|
76
|
+
encoding: event.encoding
|
|
77
|
+
};
|
|
78
|
+
break;
|
|
79
|
+
|
|
80
|
+
case 'chunk':
|
|
81
|
+
if (!metadata) {
|
|
82
|
+
throw new Error('Received chunk before metadata');
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
if (metadata.isBinary && metadata.encoding === 'base64') {
|
|
86
|
+
// Decode base64 to Uint8Array for binary files
|
|
87
|
+
const binaryString = atob(event.data);
|
|
88
|
+
const bytes = new Uint8Array(binaryString.length);
|
|
89
|
+
for (let i = 0; i < binaryString.length; i++) {
|
|
90
|
+
bytes[i] = binaryString.charCodeAt(i);
|
|
91
|
+
}
|
|
92
|
+
yield bytes;
|
|
93
|
+
} else {
|
|
94
|
+
// Text files - yield as-is
|
|
95
|
+
yield event.data;
|
|
96
|
+
}
|
|
97
|
+
break;
|
|
98
|
+
|
|
99
|
+
case 'complete':
|
|
100
|
+
if (!metadata) {
|
|
101
|
+
throw new Error('Stream completed without metadata');
|
|
102
|
+
}
|
|
103
|
+
return metadata;
|
|
104
|
+
|
|
105
|
+
case 'error':
|
|
106
|
+
throw new Error(`File streaming error: ${event.error}`);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
throw new Error('Stream ended unexpectedly');
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Collect an entire file into memory from a stream
|
|
115
|
+
*
|
|
116
|
+
* @param stream - The ReadableStream from readFileStream()
|
|
117
|
+
* @returns Object containing the file content and metadata
|
|
118
|
+
*
|
|
119
|
+
* @example
|
|
120
|
+
* ```ts
|
|
121
|
+
* const stream = await sandbox.readFileStream('/path/to/file.txt');
|
|
122
|
+
* const { content, metadata } = await collectFile(stream);
|
|
123
|
+
* console.log('Content:', content);
|
|
124
|
+
* console.log('MIME type:', metadata.mimeType);
|
|
125
|
+
* ```
|
|
126
|
+
*/
|
|
127
|
+
export async function collectFile(stream: ReadableStream<Uint8Array>): Promise<{
|
|
128
|
+
content: string | Uint8Array;
|
|
129
|
+
metadata: FileMetadata;
|
|
130
|
+
}> {
|
|
131
|
+
const chunks: Array<string | Uint8Array> = [];
|
|
132
|
+
|
|
133
|
+
// Iterate through the generator and get the return value (metadata)
|
|
134
|
+
const generator = streamFile(stream);
|
|
135
|
+
let result = await generator.next();
|
|
136
|
+
|
|
137
|
+
while (!result.done) {
|
|
138
|
+
chunks.push(result.value);
|
|
139
|
+
result = await generator.next();
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
const metadata = result.value;
|
|
143
|
+
|
|
144
|
+
if (!metadata) {
|
|
145
|
+
throw new Error('Failed to get file metadata');
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
// Combine chunks based on type
|
|
149
|
+
if (metadata.isBinary) {
|
|
150
|
+
// Binary file - combine Uint8Arrays
|
|
151
|
+
const totalLength = chunks.reduce(
|
|
152
|
+
(sum, chunk) => sum + (chunk instanceof Uint8Array ? chunk.length : 0),
|
|
153
|
+
0
|
|
154
|
+
);
|
|
155
|
+
const combined = new Uint8Array(totalLength);
|
|
156
|
+
let offset = 0;
|
|
157
|
+
for (const chunk of chunks) {
|
|
158
|
+
if (chunk instanceof Uint8Array) {
|
|
159
|
+
combined.set(chunk, offset);
|
|
160
|
+
offset += chunk.length;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
return { content: combined, metadata };
|
|
164
|
+
} else {
|
|
165
|
+
// Text file - combine strings
|
|
166
|
+
const combined = chunks.filter((c) => typeof c === 'string').join('');
|
|
167
|
+
return { content: combined, metadata };
|
|
168
|
+
}
|
|
169
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,29 +1,104 @@
|
|
|
1
|
-
// Export
|
|
1
|
+
// Export the main Sandbox class and utilities
|
|
2
|
+
|
|
3
|
+
// Export the new client architecture
|
|
4
|
+
export {
|
|
5
|
+
CommandClient,
|
|
6
|
+
FileClient,
|
|
7
|
+
GitClient,
|
|
8
|
+
PortClient,
|
|
9
|
+
ProcessClient,
|
|
10
|
+
SandboxClient,
|
|
11
|
+
UtilityClient
|
|
12
|
+
} from './clients';
|
|
13
|
+
export { getSandbox, Sandbox } from './sandbox';
|
|
14
|
+
|
|
15
|
+
// Legacy types are now imported from the new client architecture
|
|
16
|
+
|
|
17
|
+
// Export core SDK types for consumers
|
|
18
|
+
export type {
|
|
19
|
+
BaseExecOptions,
|
|
20
|
+
ExecEvent,
|
|
21
|
+
ExecOptions,
|
|
22
|
+
ExecResult,
|
|
23
|
+
FileChunk,
|
|
24
|
+
FileMetadata,
|
|
25
|
+
FileStreamEvent,
|
|
26
|
+
ISandbox,
|
|
27
|
+
LogEvent,
|
|
28
|
+
Process,
|
|
29
|
+
ProcessOptions,
|
|
30
|
+
ProcessStatus,
|
|
31
|
+
StreamOptions
|
|
32
|
+
} from '@repo/shared';
|
|
33
|
+
export * from '@repo/shared';
|
|
34
|
+
// Export type guards for runtime validation
|
|
35
|
+
export { isExecResult, isProcess, isProcessStatus } from '@repo/shared';
|
|
36
|
+
// Export all client types from new architecture
|
|
2
37
|
export type {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
38
|
+
BaseApiResponse,
|
|
39
|
+
CommandsResponse,
|
|
40
|
+
ContainerStub,
|
|
41
|
+
|
|
42
|
+
// Utility client types
|
|
43
|
+
CreateSessionRequest,
|
|
44
|
+
CreateSessionResponse,
|
|
45
|
+
DeleteSessionRequest,
|
|
46
|
+
DeleteSessionResponse,
|
|
47
|
+
ErrorResponse,
|
|
48
|
+
|
|
49
|
+
// Command client types
|
|
50
|
+
ExecuteRequest,
|
|
51
|
+
ExecuteResponse as CommandExecuteResponse,
|
|
52
|
+
|
|
53
|
+
// Port client types
|
|
54
|
+
ExposePortRequest,
|
|
55
|
+
FileOperationRequest,
|
|
56
|
+
|
|
57
|
+
// Git client types
|
|
58
|
+
GitCheckoutRequest,
|
|
59
|
+
GitCheckoutResult,
|
|
60
|
+
// Base client types
|
|
61
|
+
HttpClientOptions as SandboxClientOptions,
|
|
62
|
+
|
|
63
|
+
// File client types
|
|
64
|
+
MkdirRequest,
|
|
65
|
+
PingResponse,
|
|
66
|
+
PortCloseResult,
|
|
67
|
+
PortExposeResult,
|
|
68
|
+
PortListResult,
|
|
69
|
+
ProcessCleanupResult,
|
|
70
|
+
ProcessInfoResult,
|
|
71
|
+
ProcessKillResult,
|
|
72
|
+
ProcessListResult,
|
|
73
|
+
ProcessLogsResult,
|
|
74
|
+
ProcessStartResult,
|
|
75
|
+
ReadFileRequest,
|
|
76
|
+
RequestConfig,
|
|
77
|
+
ResponseHandler,
|
|
78
|
+
SessionRequest,
|
|
79
|
+
|
|
80
|
+
// Process client types
|
|
81
|
+
StartProcessRequest,
|
|
82
|
+
UnexposePortRequest,
|
|
83
|
+
WriteFileRequest
|
|
84
|
+
} from './clients';
|
|
9
85
|
export type {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
RunCodeOptions
|
|
18
|
-
} from "./interpreter-types";
|
|
19
|
-
// Export the implementations
|
|
20
|
-
export { ResultImpl } from "./interpreter-types";
|
|
86
|
+
ExecutionCallbacks,
|
|
87
|
+
InterpreterClient
|
|
88
|
+
} from './clients/interpreter-client.js';
|
|
89
|
+
// Export file streaming utilities for binary file support
|
|
90
|
+
export { collectFile, streamFile } from './file-stream';
|
|
91
|
+
// Export interpreter functionality
|
|
92
|
+
export { CodeInterpreter } from './interpreter.js';
|
|
21
93
|
// Re-export request handler utilities
|
|
22
94
|
export {
|
|
23
|
-
proxyToSandbox,
|
|
95
|
+
proxyToSandbox,
|
|
96
|
+
type RouteInfo,
|
|
97
|
+
type SandboxEnv
|
|
24
98
|
} from './request-handler';
|
|
25
|
-
export { getSandbox, Sandbox } from "./sandbox";
|
|
26
99
|
// Export SSE parser for converting ReadableStream to AsyncIterable
|
|
27
|
-
export {
|
|
28
|
-
|
|
29
|
-
|
|
100
|
+
export {
|
|
101
|
+
asyncIterableToSSEStream,
|
|
102
|
+
parseSSEStream,
|
|
103
|
+
responseToAsyncIterable
|
|
104
|
+
} from './sse-parser';
|
package/src/interpreter.ts
CHANGED
|
@@ -2,18 +2,24 @@ import {
|
|
|
2
2
|
type CodeContext,
|
|
3
3
|
type CreateContextOptions,
|
|
4
4
|
Execution,
|
|
5
|
+
type ExecutionError,
|
|
6
|
+
type OutputMessage,
|
|
7
|
+
type Result,
|
|
5
8
|
ResultImpl,
|
|
6
|
-
type RunCodeOptions
|
|
7
|
-
} from
|
|
8
|
-
import type {
|
|
9
|
-
import type { Sandbox } from
|
|
9
|
+
type RunCodeOptions
|
|
10
|
+
} from '@repo/shared';
|
|
11
|
+
import type { InterpreterClient } from './clients/interpreter-client.js';
|
|
12
|
+
import type { Sandbox } from './sandbox.js';
|
|
13
|
+
import { validateLanguage } from './security.js';
|
|
10
14
|
|
|
11
15
|
export class CodeInterpreter {
|
|
12
|
-
private
|
|
16
|
+
private interpreterClient: InterpreterClient;
|
|
13
17
|
private contexts = new Map<string, CodeContext>();
|
|
14
18
|
|
|
15
19
|
constructor(sandbox: Sandbox) {
|
|
16
|
-
|
|
20
|
+
// In init-testing architecture, client is a SandboxClient with an interpreter property
|
|
21
|
+
this.interpreterClient = (sandbox.client as any)
|
|
22
|
+
.interpreter as InterpreterClient;
|
|
17
23
|
}
|
|
18
24
|
|
|
19
25
|
/**
|
|
@@ -22,7 +28,10 @@ export class CodeInterpreter {
|
|
|
22
28
|
async createCodeContext(
|
|
23
29
|
options: CreateContextOptions = {}
|
|
24
30
|
): Promise<CodeContext> {
|
|
25
|
-
|
|
31
|
+
// Validate language before sending to container
|
|
32
|
+
validateLanguage(options.language);
|
|
33
|
+
|
|
34
|
+
const context = await this.interpreterClient.createCodeContext(options);
|
|
26
35
|
this.contexts.set(context.id, context);
|
|
27
36
|
return context;
|
|
28
37
|
}
|
|
@@ -38,7 +47,7 @@ export class CodeInterpreter {
|
|
|
38
47
|
let context = options.context;
|
|
39
48
|
if (!context) {
|
|
40
49
|
// Try to find or create a default context for the language
|
|
41
|
-
const language = options.language ||
|
|
50
|
+
const language = options.language || 'python';
|
|
42
51
|
context = await this.getOrCreateDefaultContext(language);
|
|
43
52
|
}
|
|
44
53
|
|
|
@@ -46,24 +55,29 @@ export class CodeInterpreter {
|
|
|
46
55
|
const execution = new Execution(code, context);
|
|
47
56
|
|
|
48
57
|
// Stream execution
|
|
49
|
-
await this.
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
58
|
+
await this.interpreterClient.runCodeStream(
|
|
59
|
+
context.id,
|
|
60
|
+
code,
|
|
61
|
+
options.language,
|
|
62
|
+
{
|
|
63
|
+
onStdout: (output: OutputMessage) => {
|
|
64
|
+
execution.logs.stdout.push(output.text);
|
|
65
|
+
if (options.onStdout) return options.onStdout(output);
|
|
66
|
+
},
|
|
67
|
+
onStderr: (output: OutputMessage) => {
|
|
68
|
+
execution.logs.stderr.push(output.text);
|
|
69
|
+
if (options.onStderr) return options.onStderr(output);
|
|
70
|
+
},
|
|
71
|
+
onResult: async (result: Result) => {
|
|
72
|
+
execution.results.push(new ResultImpl(result) as any);
|
|
73
|
+
if (options.onResult) return options.onResult(result);
|
|
74
|
+
},
|
|
75
|
+
onError: (error: ExecutionError) => {
|
|
76
|
+
execution.error = error;
|
|
77
|
+
if (options.onError) return options.onError(error);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
);
|
|
67
81
|
|
|
68
82
|
return execution;
|
|
69
83
|
}
|
|
@@ -78,35 +92,39 @@ export class CodeInterpreter {
|
|
|
78
92
|
// Get or create context
|
|
79
93
|
let context = options.context;
|
|
80
94
|
if (!context) {
|
|
81
|
-
const language = options.language ||
|
|
95
|
+
const language = options.language || 'python';
|
|
82
96
|
context = await this.getOrCreateDefaultContext(language);
|
|
83
97
|
}
|
|
84
98
|
|
|
85
99
|
// Create streaming response
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
100
|
+
// Note: doFetch is protected but we need direct access for raw stream response
|
|
101
|
+
const response = await (this.interpreterClient as any).doFetch(
|
|
102
|
+
'/api/execute/code',
|
|
103
|
+
{
|
|
104
|
+
method: 'POST',
|
|
105
|
+
headers: {
|
|
106
|
+
'Content-Type': 'application/json',
|
|
107
|
+
Accept: 'text/event-stream'
|
|
108
|
+
},
|
|
109
|
+
body: JSON.stringify({
|
|
110
|
+
context_id: context.id,
|
|
111
|
+
code,
|
|
112
|
+
language: options.language
|
|
113
|
+
})
|
|
114
|
+
}
|
|
115
|
+
);
|
|
98
116
|
|
|
99
117
|
if (!response.ok) {
|
|
100
118
|
const errorData = (await response
|
|
101
119
|
.json()
|
|
102
|
-
.catch(() => ({ error:
|
|
120
|
+
.catch(() => ({ error: 'Unknown error' }))) as { error?: string };
|
|
103
121
|
throw new Error(
|
|
104
122
|
errorData.error || `Failed to execute code: ${response.status}`
|
|
105
123
|
);
|
|
106
124
|
}
|
|
107
125
|
|
|
108
126
|
if (!response.body) {
|
|
109
|
-
throw new Error(
|
|
127
|
+
throw new Error('No response body for streaming execution');
|
|
110
128
|
}
|
|
111
129
|
|
|
112
130
|
return response.body;
|
|
@@ -116,7 +134,7 @@ export class CodeInterpreter {
|
|
|
116
134
|
* List all code contexts
|
|
117
135
|
*/
|
|
118
136
|
async listCodeContexts(): Promise<CodeContext[]> {
|
|
119
|
-
const contexts = await this.
|
|
137
|
+
const contexts = await this.interpreterClient.listCodeContexts();
|
|
120
138
|
|
|
121
139
|
// Update local cache
|
|
122
140
|
for (const context of contexts) {
|
|
@@ -130,12 +148,12 @@ export class CodeInterpreter {
|
|
|
130
148
|
* Delete a code context
|
|
131
149
|
*/
|
|
132
150
|
async deleteCodeContext(contextId: string): Promise<void> {
|
|
133
|
-
await this.
|
|
151
|
+
await this.interpreterClient.deleteCodeContext(contextId);
|
|
134
152
|
this.contexts.delete(contextId);
|
|
135
153
|
}
|
|
136
154
|
|
|
137
155
|
private async getOrCreateDefaultContext(
|
|
138
|
-
language:
|
|
156
|
+
language: 'python' | 'javascript' | 'typescript'
|
|
139
157
|
): Promise<CodeContext> {
|
|
140
158
|
// Check if we have a cached context for this language
|
|
141
159
|
for (const context of this.contexts.values()) {
|
package/src/request-handler.ts
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
validatePort
|
|
6
|
-
} from "./security";
|
|
1
|
+
import { switchPort } from '@cloudflare/containers';
|
|
2
|
+
import { createLogger, type LogContext, TraceContext } from '@repo/shared';
|
|
3
|
+
import { getSandbox, type Sandbox } from './sandbox';
|
|
4
|
+
import { sanitizeSandboxId, validatePort } from './security';
|
|
7
5
|
|
|
8
6
|
export interface SandboxEnv {
|
|
9
7
|
Sandbox: DurableObjectNamespace<Sandbox>;
|
|
@@ -13,12 +11,22 @@ export interface RouteInfo {
|
|
|
13
11
|
port: number;
|
|
14
12
|
sandboxId: string;
|
|
15
13
|
path: string;
|
|
14
|
+
token: string;
|
|
16
15
|
}
|
|
17
16
|
|
|
18
17
|
export async function proxyToSandbox<E extends SandboxEnv>(
|
|
19
18
|
request: Request,
|
|
20
19
|
env: E
|
|
21
20
|
): Promise<Response | null> {
|
|
21
|
+
// Create logger context for this request
|
|
22
|
+
const traceId =
|
|
23
|
+
TraceContext.fromHeaders(request.headers) || TraceContext.generate();
|
|
24
|
+
const logger = createLogger({
|
|
25
|
+
component: 'sandbox-do',
|
|
26
|
+
traceId,
|
|
27
|
+
operation: 'proxy'
|
|
28
|
+
});
|
|
29
|
+
|
|
22
30
|
try {
|
|
23
31
|
const url = new URL(request.url);
|
|
24
32
|
const routeInfo = extractSandboxRoute(url);
|
|
@@ -27,9 +35,48 @@ export async function proxyToSandbox<E extends SandboxEnv>(
|
|
|
27
35
|
return null; // Not a request to an exposed container port
|
|
28
36
|
}
|
|
29
37
|
|
|
30
|
-
const { sandboxId, port, path } = routeInfo;
|
|
38
|
+
const { sandboxId, port, path, token } = routeInfo;
|
|
31
39
|
const sandbox = getSandbox(env.Sandbox, sandboxId);
|
|
32
40
|
|
|
41
|
+
// Critical security check: Validate token (mandatory for all user ports)
|
|
42
|
+
// Skip check for control plane port 3000
|
|
43
|
+
if (port !== 3000) {
|
|
44
|
+
// Validate the token matches the port
|
|
45
|
+
const isValidToken = await sandbox.validatePortToken(port, token);
|
|
46
|
+
if (!isValidToken) {
|
|
47
|
+
logger.warn('Invalid token access blocked', {
|
|
48
|
+
port,
|
|
49
|
+
sandboxId,
|
|
50
|
+
path,
|
|
51
|
+
hostname: url.hostname,
|
|
52
|
+
url: request.url,
|
|
53
|
+
method: request.method,
|
|
54
|
+
userAgent: request.headers.get('User-Agent') || 'unknown'
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
return new Response(
|
|
58
|
+
JSON.stringify({
|
|
59
|
+
error: `Access denied: Invalid token or port not exposed`,
|
|
60
|
+
code: 'INVALID_TOKEN'
|
|
61
|
+
}),
|
|
62
|
+
{
|
|
63
|
+
status: 404,
|
|
64
|
+
headers: {
|
|
65
|
+
'Content-Type': 'application/json'
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// Detect WebSocket upgrade request
|
|
73
|
+
const upgradeHeader = request.headers.get('Upgrade');
|
|
74
|
+
if (upgradeHeader?.toLowerCase() === 'websocket') {
|
|
75
|
+
// WebSocket path: Must use fetch() not containerFetch()
|
|
76
|
+
// This bypasses JSRPC serialization boundary which cannot handle WebSocket upgrades
|
|
77
|
+
return await sandbox.fetch(switchPort(request, port));
|
|
78
|
+
}
|
|
79
|
+
|
|
33
80
|
// Build proxy request with proper headers
|
|
34
81
|
let proxyUrl: string;
|
|
35
82
|
|
|
@@ -49,46 +96,41 @@ export async function proxyToSandbox<E extends SandboxEnv>(
|
|
|
49
96
|
'X-Original-URL': request.url,
|
|
50
97
|
'X-Forwarded-Host': url.hostname,
|
|
51
98
|
'X-Forwarded-Proto': url.protocol.replace(':', ''),
|
|
52
|
-
'X-Sandbox-Name': sandboxId
|
|
99
|
+
'X-Sandbox-Name': sandboxId // Pass the friendly name
|
|
53
100
|
},
|
|
54
101
|
body: request.body,
|
|
102
|
+
// @ts-expect-error - duplex required for body streaming in modern runtimes
|
|
103
|
+
duplex: 'half'
|
|
55
104
|
});
|
|
56
105
|
|
|
57
|
-
return sandbox.containerFetch(proxyRequest, port);
|
|
106
|
+
return await sandbox.containerFetch(proxyRequest, port);
|
|
58
107
|
} catch (error) {
|
|
59
|
-
|
|
108
|
+
logger.error(
|
|
109
|
+
'Proxy routing error',
|
|
110
|
+
error instanceof Error ? error : new Error(String(error))
|
|
111
|
+
);
|
|
60
112
|
return new Response('Proxy routing error', { status: 500 });
|
|
61
113
|
}
|
|
62
114
|
}
|
|
63
115
|
|
|
64
116
|
function extractSandboxRoute(url: URL): RouteInfo | null {
|
|
65
|
-
// Parse subdomain pattern: port-sandboxId.domain
|
|
66
|
-
|
|
117
|
+
// Parse subdomain pattern: port-sandboxId-token.domain (tokens mandatory)
|
|
118
|
+
// Token is always exactly 16 chars (generated by generatePortToken)
|
|
119
|
+
const subdomainMatch = url.hostname.match(
|
|
120
|
+
/^(\d{4,5})-([^.-][^.]*?[^.-]|[^.-])-([a-z0-9_-]{16})\.(.+)$/
|
|
121
|
+
);
|
|
67
122
|
|
|
68
123
|
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
124
|
return null;
|
|
77
125
|
}
|
|
78
126
|
|
|
79
127
|
const portStr = subdomainMatch[1];
|
|
80
128
|
const sandboxId = subdomainMatch[2];
|
|
81
|
-
const
|
|
129
|
+
const token = subdomainMatch[3]; // Mandatory token
|
|
130
|
+
const domain = subdomainMatch[4];
|
|
82
131
|
|
|
83
132
|
const port = parseInt(portStr, 10);
|
|
84
133
|
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
134
|
return null;
|
|
93
135
|
}
|
|
94
136
|
|
|
@@ -96,49 +138,46 @@ function extractSandboxRoute(url: URL): RouteInfo | null {
|
|
|
96
138
|
try {
|
|
97
139
|
sanitizedSandboxId = sanitizeSandboxId(sandboxId);
|
|
98
140
|
} 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
141
|
return null;
|
|
107
142
|
}
|
|
108
143
|
|
|
109
144
|
// DNS subdomain length limit is 63 characters
|
|
110
145
|
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
146
|
return null;
|
|
118
147
|
}
|
|
119
148
|
|
|
120
|
-
logSecurityEvent('SANDBOX_ROUTE_EXTRACTED', {
|
|
121
|
-
port,
|
|
122
|
-
sandboxId: sanitizedSandboxId,
|
|
123
|
-
domain,
|
|
124
|
-
path: url.pathname || "/",
|
|
125
|
-
hostname: url.hostname
|
|
126
|
-
}, 'low');
|
|
127
|
-
|
|
128
149
|
return {
|
|
129
150
|
port,
|
|
130
151
|
sandboxId: sanitizedSandboxId,
|
|
131
|
-
path: url.pathname ||
|
|
152
|
+
path: url.pathname || '/',
|
|
153
|
+
token
|
|
132
154
|
};
|
|
133
155
|
}
|
|
134
156
|
|
|
135
157
|
export function isLocalhostPattern(hostname: string): boolean {
|
|
136
|
-
|
|
158
|
+
// Handle IPv6 addresses in brackets (with or without port)
|
|
159
|
+
if (hostname.startsWith('[')) {
|
|
160
|
+
if (hostname.includes(']:')) {
|
|
161
|
+
// [::1]:port format
|
|
162
|
+
const ipv6Part = hostname.substring(0, hostname.indexOf(']:') + 1);
|
|
163
|
+
return ipv6Part === '[::1]';
|
|
164
|
+
} else {
|
|
165
|
+
// [::1] format without port
|
|
166
|
+
return hostname === '[::1]';
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
// Handle bare IPv6 without brackets
|
|
171
|
+
if (hostname === '::1') {
|
|
172
|
+
return true;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
// For IPv4 and regular hostnames, split on colon to remove port
|
|
176
|
+
const hostPart = hostname.split(':')[0];
|
|
177
|
+
|
|
137
178
|
return (
|
|
138
|
-
hostPart ===
|
|
139
|
-
hostPart ===
|
|
140
|
-
hostPart ===
|
|
141
|
-
hostPart === "[::1]" ||
|
|
142
|
-
hostPart === "0.0.0.0"
|
|
179
|
+
hostPart === 'localhost' ||
|
|
180
|
+
hostPart === '127.0.0.1' ||
|
|
181
|
+
hostPart === '0.0.0.0'
|
|
143
182
|
);
|
|
144
183
|
}
|