@cloudflare/sandbox 0.0.0-d1c7c99 → 0.0.0-d81d2a5

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.
@@ -0,0 +1,266 @@
1
+ import { HttpClient } from './client.js';
2
+ import type {
3
+ CodeContext,
4
+ CreateContextOptions,
5
+ ExecutionError,
6
+ OutputMessage,
7
+ Result
8
+ } from './interpreter-types.js';
9
+
10
+ // API Response types
11
+ interface ContextResponse {
12
+ id: string;
13
+ language: string;
14
+ cwd: string;
15
+ createdAt: string; // ISO date string from JSON
16
+ lastUsed: string; // ISO date string from JSON
17
+ }
18
+
19
+ interface ContextListResponse {
20
+ contexts: ContextResponse[];
21
+ }
22
+
23
+ interface ErrorResponse {
24
+ error: string;
25
+ }
26
+
27
+ // Streaming execution data from the server
28
+ interface StreamingExecutionData {
29
+ type: 'result' | 'stdout' | 'stderr' | 'error' | 'execution_complete';
30
+ text?: string;
31
+ html?: string;
32
+ png?: string; // base64
33
+ jpeg?: string; // base64
34
+ svg?: string;
35
+ latex?: string;
36
+ markdown?: string;
37
+ javascript?: string;
38
+ json?: any;
39
+ chart?: any;
40
+ data?: any;
41
+ metadata?: any;
42
+ execution_count?: number;
43
+ ename?: string;
44
+ evalue?: string;
45
+ traceback?: string[];
46
+ lineNumber?: number;
47
+ timestamp?: number;
48
+ }
49
+
50
+ export interface ExecutionCallbacks {
51
+ onStdout?: (output: OutputMessage) => void | Promise<void>;
52
+ onStderr?: (output: OutputMessage) => void | Promise<void>;
53
+ onResult?: (result: Result) => void | Promise<void>;
54
+ onError?: (error: ExecutionError) => void | Promise<void>;
55
+ }
56
+
57
+ export class JupyterClient extends HttpClient {
58
+ async createCodeContext(options: CreateContextOptions = {}): Promise<CodeContext> {
59
+ const response = await this.doFetch('/api/contexts', {
60
+ method: 'POST',
61
+ headers: { 'Content-Type': 'application/json' },
62
+ body: JSON.stringify({
63
+ language: options.language || 'python',
64
+ cwd: options.cwd || '/workspace',
65
+ env_vars: options.envVars
66
+ }),
67
+ });
68
+
69
+ if (!response.ok) {
70
+ const errorData = await response.json().catch(() => ({ error: 'Unknown error' })) as ErrorResponse;
71
+ throw new Error(errorData.error || `Failed to create context: ${response.status}`);
72
+ }
73
+
74
+ const data = await response.json() as ContextResponse;
75
+ return {
76
+ id: data.id,
77
+ language: data.language,
78
+ cwd: data.cwd,
79
+ createdAt: new Date(data.createdAt),
80
+ lastUsed: new Date(data.lastUsed)
81
+ };
82
+ }
83
+
84
+ async runCodeStream(
85
+ contextId: string | undefined,
86
+ code: string,
87
+ language: string | undefined,
88
+ callbacks: ExecutionCallbacks
89
+ ): Promise<void> {
90
+ const response = await this.doFetch('/api/execute/code', {
91
+ method: 'POST',
92
+ headers: {
93
+ 'Content-Type': 'application/json',
94
+ 'Accept': 'text/event-stream'
95
+ },
96
+ body: JSON.stringify({
97
+ context_id: contextId,
98
+ code,
99
+ language
100
+ }),
101
+ });
102
+
103
+ if (!response.ok) {
104
+ const errorData = await response.json().catch(() => ({ error: 'Unknown error' })) as ErrorResponse;
105
+ throw new Error(errorData.error || `Failed to execute code: ${response.status}`);
106
+ }
107
+
108
+ if (!response.body) {
109
+ throw new Error('No response body for streaming execution');
110
+ }
111
+
112
+ // Process streaming response
113
+ for await (const chunk of this.readLines(response.body)) {
114
+ await this.parseExecutionResult(chunk, callbacks);
115
+ }
116
+ }
117
+
118
+ private async *readLines(stream: ReadableStream<Uint8Array>): AsyncGenerator<string> {
119
+ const reader = stream.getReader();
120
+ let buffer = '';
121
+
122
+ try {
123
+ while (true) {
124
+ const { done, value } = await reader.read();
125
+ if (value) {
126
+ buffer += new TextDecoder().decode(value);
127
+ }
128
+ if (done) break;
129
+
130
+ let newlineIdx = buffer.indexOf('\n');
131
+ while (newlineIdx !== -1) {
132
+ yield buffer.slice(0, newlineIdx);
133
+ buffer = buffer.slice(newlineIdx + 1);
134
+ newlineIdx = buffer.indexOf('\n');
135
+ }
136
+ }
137
+
138
+ // Yield any remaining data
139
+ if (buffer.length > 0) {
140
+ yield buffer;
141
+ }
142
+ } finally {
143
+ reader.releaseLock();
144
+ }
145
+ }
146
+
147
+ private async parseExecutionResult(line: string, callbacks: ExecutionCallbacks) {
148
+ if (!line.trim()) return;
149
+
150
+ try {
151
+ const data = JSON.parse(line) as StreamingExecutionData;
152
+
153
+ switch (data.type) {
154
+ case 'stdout':
155
+ if (callbacks.onStdout && data.text) {
156
+ await callbacks.onStdout({
157
+ text: data.text,
158
+ timestamp: data.timestamp || Date.now()
159
+ });
160
+ }
161
+ break;
162
+
163
+ case 'stderr':
164
+ if (callbacks.onStderr && data.text) {
165
+ await callbacks.onStderr({
166
+ text: data.text,
167
+ timestamp: data.timestamp || Date.now()
168
+ });
169
+ }
170
+ break;
171
+
172
+ case 'result':
173
+ if (callbacks.onResult) {
174
+ // Convert raw result to Result interface
175
+ const result: Result = {
176
+ text: data.text,
177
+ html: data.html,
178
+ png: data.png,
179
+ jpeg: data.jpeg,
180
+ svg: data.svg,
181
+ latex: data.latex,
182
+ markdown: data.markdown,
183
+ javascript: data.javascript,
184
+ json: data.json,
185
+ chart: data.chart,
186
+ data: data.data,
187
+ formats: () => {
188
+ const formats: string[] = [];
189
+ if (data.text) formats.push('text');
190
+ if (data.html) formats.push('html');
191
+ if (data.png) formats.push('png');
192
+ if (data.jpeg) formats.push('jpeg');
193
+ if (data.svg) formats.push('svg');
194
+ if (data.latex) formats.push('latex');
195
+ if (data.markdown) formats.push('markdown');
196
+ if (data.javascript) formats.push('javascript');
197
+ if (data.json) formats.push('json');
198
+ if (data.chart) formats.push('chart');
199
+ return formats;
200
+ }
201
+ };
202
+ await callbacks.onResult(result);
203
+ }
204
+ break;
205
+
206
+ case 'error':
207
+ if (callbacks.onError) {
208
+ await callbacks.onError({
209
+ name: data.ename || 'Error',
210
+ value: data.evalue || data.text || 'Unknown error',
211
+ traceback: data.traceback || [],
212
+ lineNumber: data.lineNumber
213
+ });
214
+ }
215
+ break;
216
+
217
+ case 'execution_complete':
218
+ // Execution completed successfully
219
+ break;
220
+ }
221
+ } catch (error) {
222
+ console.error('[JupyterClient] Error parsing execution result:', error);
223
+ }
224
+ }
225
+
226
+ async listCodeContexts(): Promise<CodeContext[]> {
227
+ const response = await this.doFetch('/api/contexts', {
228
+ method: 'GET',
229
+ headers: { 'Content-Type': 'application/json' }
230
+ });
231
+
232
+ if (!response.ok) {
233
+ const errorData = await response.json().catch(() => ({ error: 'Unknown error' })) as ErrorResponse;
234
+ throw new Error(errorData.error || `Failed to list contexts: ${response.status}`);
235
+ }
236
+
237
+ const data = await response.json() as ContextListResponse;
238
+ return data.contexts.map((ctx) => ({
239
+ id: ctx.id,
240
+ language: ctx.language,
241
+ cwd: ctx.cwd,
242
+ createdAt: new Date(ctx.createdAt),
243
+ lastUsed: new Date(ctx.lastUsed)
244
+ }));
245
+ }
246
+
247
+ async deleteCodeContext(contextId: string): Promise<void> {
248
+ const response = await this.doFetch(`/api/contexts/${contextId}`, {
249
+ method: 'DELETE',
250
+ headers: { 'Content-Type': 'application/json' }
251
+ });
252
+
253
+ if (!response.ok) {
254
+ const errorData = await response.json().catch(() => ({ error: 'Unknown error' })) as ErrorResponse;
255
+ throw new Error(errorData.error || `Failed to delete context: ${response.status}`);
256
+ }
257
+ }
258
+
259
+ // Override parent doFetch to be public for this class
260
+ public async doFetch(
261
+ path: string,
262
+ options?: RequestInit
263
+ ): Promise<Response> {
264
+ return super.doFetch(path, options);
265
+ }
266
+ }
@@ -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
+ }