@cloudflare/sandbox 0.0.0-9fa3058 → 0.0.0-a55a10c
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 +157 -0
- package/Dockerfile +112 -54
- package/README.md +90 -490
- package/dist/chunk-2P3MDMNJ.js +2367 -0
- package/dist/chunk-2P3MDMNJ.js.map +1 -0
- package/dist/chunk-BFVUNTP4.js +104 -0
- package/dist/chunk-BFVUNTP4.js.map +1 -0
- package/dist/chunk-EKSWCBCA.js +86 -0
- package/dist/chunk-EKSWCBCA.js.map +1 -0
- package/dist/chunk-JXZMAU2C.js +559 -0
- package/dist/chunk-JXZMAU2C.js.map +1 -0
- package/dist/chunk-Z532A7QC.js +78 -0
- package/dist/chunk-Z532A7QC.js.map +1 -0
- package/dist/file-stream.d.ts +43 -0
- package/dist/file-stream.js +9 -0
- package/dist/file-stream.js.map +1 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +66 -0
- package/dist/index.js.map +1 -0
- package/dist/interpreter.d.ts +33 -0
- package/dist/interpreter.js +8 -0
- package/dist/interpreter.js.map +1 -0
- package/dist/request-handler.d.ts +18 -0
- package/dist/request-handler.js +12 -0
- package/dist/request-handler.js.map +1 -0
- package/dist/sandbox-CZTMzV2R.d.ts +587 -0
- package/dist/sandbox.d.ts +4 -0
- package/dist/sandbox.js +12 -0
- package/dist/sandbox.js.map +1 -0
- package/dist/security.d.ts +31 -0
- package/dist/security.js +13 -0
- package/dist/security.js.map +1 -0
- package/dist/sse-parser.d.ts +28 -0
- package/dist/sse-parser.js +11 -0
- package/dist/sse-parser.js.map +1 -0
- package/package.json +13 -5
- package/src/clients/base-client.ts +280 -0
- package/src/clients/command-client.ts +115 -0
- package/src/clients/file-client.ts +269 -0
- package/src/clients/git-client.ts +92 -0
- package/src/clients/index.ts +63 -0
- package/src/clients/interpreter-client.ts +329 -0
- package/src/clients/port-client.ts +105 -0
- package/src/clients/process-client.ts +177 -0
- package/src/clients/sandbox-client.ts +41 -0
- package/src/clients/types.ts +84 -0
- package/src/clients/utility-client.ts +94 -0
- package/src/errors/adapter.ts +180 -0
- package/src/errors/classes.ts +469 -0
- package/src/errors/index.ts +105 -0
- package/src/file-stream.ts +164 -0
- package/src/index.ts +85 -12
- package/src/interpreter.ts +159 -0
- package/src/request-handler.ts +69 -43
- package/src/sandbox.ts +578 -288
- package/src/security.ts +14 -23
- package/src/sse-parser.ts +4 -8
- package/startup.sh +3 -0
- package/tests/base-client.test.ts +328 -0
- package/tests/command-client.test.ts +407 -0
- package/tests/file-client.test.ts +643 -0
- package/tests/file-stream.test.ts +306 -0
- package/tests/git-client.test.ts +328 -0
- package/tests/port-client.test.ts +301 -0
- package/tests/process-client.test.ts +658 -0
- package/tests/sandbox.test.ts +465 -0
- package/tests/sse-parser.test.ts +290 -0
- package/tests/utility-client.test.ts +266 -0
- package/tests/wrangler.jsonc +35 -0
- package/tsconfig.json +9 -1
- package/vitest.config.ts +31 -0
- package/container_src/handler/exec.ts +0 -337
- 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 -361
- package/container_src/package.json +0 -9
- package/container_src/types.ts +0 -103
- package/src/client.ts +0 -1038
- package/src/types.ts +0 -386
package/src/sandbox.ts
CHANGED
|
@@ -1,67 +1,100 @@
|
|
|
1
|
+
import type { DurableObject } from 'cloudflare:workers';
|
|
1
2
|
import { Container, getContainer } from "@cloudflare/containers";
|
|
2
|
-
import { HttpClient } from "./client";
|
|
3
|
-
import { isLocalhostPattern } from "./request-handler";
|
|
4
|
-
import {
|
|
5
|
-
logSecurityEvent,
|
|
6
|
-
SecurityError,
|
|
7
|
-
sanitizeSandboxId,
|
|
8
|
-
validatePort
|
|
9
|
-
} from "./security";
|
|
10
3
|
import type {
|
|
4
|
+
CodeContext,
|
|
5
|
+
CreateContextOptions,
|
|
6
|
+
ExecEvent,
|
|
11
7
|
ExecOptions,
|
|
12
8
|
ExecResult,
|
|
9
|
+
ExecutionResult,
|
|
10
|
+
ExecutionSession,
|
|
13
11
|
ISandbox,
|
|
14
12
|
Process,
|
|
15
13
|
ProcessOptions,
|
|
16
14
|
ProcessStatus,
|
|
15
|
+
RunCodeOptions,
|
|
16
|
+
SessionOptions,
|
|
17
17
|
StreamOptions
|
|
18
|
-
} from "
|
|
18
|
+
} from "@repo/shared";
|
|
19
|
+
import { createLogger, runWithLogger, TraceContext } from "@repo/shared";
|
|
20
|
+
import { type ExecuteResponse, SandboxClient } from "./clients";
|
|
21
|
+
import type { ErrorResponse } from './errors';
|
|
22
|
+
import { CustomDomainRequiredError, ErrorCode } from './errors';
|
|
23
|
+
import { CodeInterpreter } from "./interpreter";
|
|
24
|
+
import { isLocalhostPattern } from "./request-handler";
|
|
19
25
|
import {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
26
|
+
SecurityError,
|
|
27
|
+
sanitizeSandboxId,
|
|
28
|
+
validatePort
|
|
29
|
+
} from "./security";
|
|
30
|
+
import { parseSSEStream } from "./sse-parser";
|
|
23
31
|
|
|
24
|
-
export function getSandbox(ns: DurableObjectNamespace<Sandbox>, id: string
|
|
32
|
+
export function getSandbox(ns: DurableObjectNamespace<Sandbox>, id: string, options?: {
|
|
33
|
+
baseUrl: string
|
|
34
|
+
}) {
|
|
25
35
|
const stub = getContainer(ns, id);
|
|
26
36
|
|
|
27
37
|
// Store the name on first access
|
|
28
38
|
stub.setSandboxName?.(id);
|
|
29
39
|
|
|
40
|
+
if(options?.baseUrl) {
|
|
41
|
+
stub.setBaseUrl(options.baseUrl);
|
|
42
|
+
}
|
|
43
|
+
|
|
30
44
|
return stub;
|
|
31
45
|
}
|
|
32
46
|
|
|
33
47
|
export class Sandbox<Env = unknown> extends Container<Env> implements ISandbox {
|
|
34
48
|
defaultPort = 3000; // Default port for the container's Bun server
|
|
35
49
|
sleepAfter = "3m"; // Sleep the sandbox if no requests are made in this timeframe
|
|
36
|
-
|
|
50
|
+
|
|
51
|
+
client: SandboxClient;
|
|
52
|
+
private codeInterpreter: CodeInterpreter;
|
|
37
53
|
private sandboxName: string | null = null;
|
|
54
|
+
private baseUrl: string | null = null;
|
|
55
|
+
private portTokens: Map<number, string> = new Map();
|
|
56
|
+
private defaultSession: string | null = null;
|
|
57
|
+
envVars: Record<string, string> = {};
|
|
58
|
+
private logger: ReturnType<typeof createLogger>;
|
|
38
59
|
|
|
39
|
-
constructor(ctx:
|
|
60
|
+
constructor(ctx: DurableObject['ctx'], env: Env) {
|
|
40
61
|
super(ctx, env);
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
62
|
+
|
|
63
|
+
const envObj = env as any;
|
|
64
|
+
// Set sandbox environment variables from env object
|
|
65
|
+
const sandboxEnvKeys = ['SANDBOX_LOG_LEVEL', 'SANDBOX_LOG_FORMAT'] as const;
|
|
66
|
+
sandboxEnvKeys.forEach(key => {
|
|
67
|
+
if (envObj?.[key]) {
|
|
68
|
+
this.envVars[key] = envObj[key];
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
this.logger = createLogger({
|
|
73
|
+
component: 'sandbox-do',
|
|
74
|
+
sandboxId: this.ctx.id.toString()
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
this.client = new SandboxClient({
|
|
78
|
+
logger: this.logger,
|
|
58
79
|
port: 3000, // Control plane port
|
|
59
80
|
stub: this,
|
|
60
81
|
});
|
|
61
82
|
|
|
62
|
-
//
|
|
83
|
+
// Initialize code interpreter - pass 'this' after client is ready
|
|
84
|
+
// The CodeInterpreter extracts client.interpreter from the sandbox
|
|
85
|
+
this.codeInterpreter = new CodeInterpreter(this);
|
|
86
|
+
|
|
87
|
+
// Load the sandbox name, port tokens, and default session from storage on initialization
|
|
63
88
|
this.ctx.blockConcurrencyWhile(async () => {
|
|
64
89
|
this.sandboxName = await this.ctx.storage.get<string>('sandboxName') || null;
|
|
90
|
+
this.defaultSession = await this.ctx.storage.get<string>('defaultSession') || null;
|
|
91
|
+
const storedTokens = await this.ctx.storage.get<Record<string, string>>('portTokens') || {};
|
|
92
|
+
|
|
93
|
+
// Convert stored tokens back to Map
|
|
94
|
+
this.portTokens = new Map();
|
|
95
|
+
for (const [portStr, token] of Object.entries(storedTokens)) {
|
|
96
|
+
this.portTokens.set(parseInt(portStr, 10), token);
|
|
97
|
+
}
|
|
65
98
|
});
|
|
66
99
|
}
|
|
67
100
|
|
|
@@ -70,55 +103,93 @@ export class Sandbox<Env = unknown> extends Container<Env> implements ISandbox {
|
|
|
70
103
|
if (!this.sandboxName) {
|
|
71
104
|
this.sandboxName = name;
|
|
72
105
|
await this.ctx.storage.put('sandboxName', name);
|
|
73
|
-
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// RPC method to set the base URL
|
|
110
|
+
async setBaseUrl(baseUrl: string): Promise<void> {
|
|
111
|
+
if (!this.baseUrl) {
|
|
112
|
+
this.baseUrl = baseUrl;
|
|
113
|
+
await this.ctx.storage.put('baseUrl', baseUrl);
|
|
114
|
+
} else {
|
|
115
|
+
if(this.baseUrl !== baseUrl) {
|
|
116
|
+
throw new Error('Base URL already set and different from one previously provided');
|
|
117
|
+
}
|
|
74
118
|
}
|
|
75
119
|
}
|
|
76
120
|
|
|
77
121
|
// RPC method to set environment variables
|
|
78
122
|
async setEnvVars(envVars: Record<string, string>): Promise<void> {
|
|
123
|
+
// Update local state for new sessions
|
|
79
124
|
this.envVars = { ...this.envVars, ...envVars };
|
|
80
|
-
|
|
125
|
+
|
|
126
|
+
// If default session already exists, update it directly
|
|
127
|
+
if (this.defaultSession) {
|
|
128
|
+
// Set environment variables by executing export commands in the existing session
|
|
129
|
+
for (const [key, value] of Object.entries(envVars)) {
|
|
130
|
+
const escapedValue = value.replace(/'/g, "'\\''");
|
|
131
|
+
const exportCommand = `export ${key}='${escapedValue}'`;
|
|
132
|
+
|
|
133
|
+
const result = await this.client.commands.execute(exportCommand, this.defaultSession);
|
|
134
|
+
|
|
135
|
+
if (result.exitCode !== 0) {
|
|
136
|
+
throw new Error(`Failed to set ${key}: ${result.stderr || 'Unknown error'}`);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Cleanup and destroy the sandbox container
|
|
144
|
+
*/
|
|
145
|
+
override async destroy(): Promise<void> {
|
|
146
|
+
this.logger.info('Destroying sandbox container');
|
|
147
|
+
await super.destroy();
|
|
81
148
|
}
|
|
82
149
|
|
|
83
150
|
override onStart() {
|
|
84
|
-
|
|
151
|
+
this.logger.debug('Sandbox started');
|
|
85
152
|
}
|
|
86
153
|
|
|
87
154
|
override onStop() {
|
|
88
|
-
|
|
89
|
-
if (this.client) {
|
|
90
|
-
this.client.clearSession();
|
|
91
|
-
}
|
|
155
|
+
this.logger.debug('Sandbox stopped');
|
|
92
156
|
}
|
|
93
157
|
|
|
94
158
|
override onError(error: unknown) {
|
|
95
|
-
|
|
159
|
+
this.logger.error('Sandbox error', error instanceof Error ? error : new Error(String(error)));
|
|
96
160
|
}
|
|
97
161
|
|
|
98
162
|
// Override fetch to route internal container requests to appropriate ports
|
|
99
163
|
override async fetch(request: Request): Promise<Response> {
|
|
100
|
-
|
|
164
|
+
// Extract or generate trace ID from request
|
|
165
|
+
const traceId = TraceContext.fromHeaders(request.headers) || TraceContext.generate();
|
|
101
166
|
|
|
102
|
-
//
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
167
|
+
// Create request-specific logger with trace ID
|
|
168
|
+
const requestLogger = this.logger.child({ traceId, operation: 'fetch' });
|
|
169
|
+
|
|
170
|
+
return await runWithLogger(requestLogger, async () => {
|
|
171
|
+
const url = new URL(request.url);
|
|
172
|
+
|
|
173
|
+
// Capture and store the sandbox name from the header if present
|
|
174
|
+
if (!this.sandboxName && request.headers.has('X-Sandbox-Name')) {
|
|
175
|
+
const name = request.headers.get('X-Sandbox-Name')!;
|
|
176
|
+
this.sandboxName = name;
|
|
177
|
+
await this.ctx.storage.put('sandboxName', name);
|
|
178
|
+
}
|
|
109
179
|
|
|
110
|
-
|
|
111
|
-
|
|
180
|
+
// Determine which port to route to
|
|
181
|
+
const port = this.determinePort(url);
|
|
112
182
|
|
|
113
|
-
|
|
114
|
-
|
|
183
|
+
// Route to the appropriate port
|
|
184
|
+
return await this.containerFetch(request, port);
|
|
185
|
+
});
|
|
115
186
|
}
|
|
116
187
|
|
|
117
188
|
private determinePort(url: URL): number {
|
|
118
189
|
// Extract port from proxy requests (e.g., /proxy/8080/*)
|
|
119
190
|
const proxyMatch = url.pathname.match(/^\/proxy\/(\d+)/);
|
|
120
191
|
if (proxyMatch) {
|
|
121
|
-
return parseInt(proxyMatch[1]);
|
|
192
|
+
return parseInt(proxyMatch[1], 10);
|
|
122
193
|
}
|
|
123
194
|
|
|
124
195
|
// All other requests go to control plane on port 3000
|
|
@@ -126,9 +197,62 @@ export class Sandbox<Env = unknown> extends Container<Env> implements ISandbox {
|
|
|
126
197
|
return 3000;
|
|
127
198
|
}
|
|
128
199
|
|
|
200
|
+
/**
|
|
201
|
+
* Ensure default session exists - lazy initialization
|
|
202
|
+
* This is called automatically by all public methods that need a session
|
|
203
|
+
*
|
|
204
|
+
* The session is persisted to Durable Object storage to survive hot reloads
|
|
205
|
+
* during development. If a session already exists in the container after reload,
|
|
206
|
+
* we reuse it instead of trying to create a new one.
|
|
207
|
+
*/
|
|
208
|
+
private async ensureDefaultSession(): Promise<string> {
|
|
209
|
+
if (!this.defaultSession) {
|
|
210
|
+
const sessionId = `sandbox-${this.sandboxName || 'default'}`;
|
|
211
|
+
|
|
212
|
+
try {
|
|
213
|
+
// Try to create session in container
|
|
214
|
+
await this.client.utils.createSession({
|
|
215
|
+
id: sessionId,
|
|
216
|
+
env: this.envVars || {},
|
|
217
|
+
cwd: '/workspace',
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
this.defaultSession = sessionId;
|
|
221
|
+
// Persist to storage so it survives hot reloads
|
|
222
|
+
await this.ctx.storage.put('defaultSession', sessionId);
|
|
223
|
+
this.logger.debug('Default session initialized', { sessionId });
|
|
224
|
+
} catch (error: any) {
|
|
225
|
+
// If session already exists (e.g., after hot reload), reuse it
|
|
226
|
+
if (error?.message?.includes('already exists') || error?.message?.includes('Session')) {
|
|
227
|
+
this.logger.debug('Reusing existing session after reload', { sessionId });
|
|
228
|
+
this.defaultSession = sessionId;
|
|
229
|
+
// Persist to storage in case it wasn't saved before
|
|
230
|
+
await this.ctx.storage.put('defaultSession', sessionId);
|
|
231
|
+
} else {
|
|
232
|
+
// Re-throw other errors
|
|
233
|
+
throw error;
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
return this.defaultSession;
|
|
238
|
+
}
|
|
239
|
+
|
|
129
240
|
// Enhanced exec method - always returns ExecResult with optional streaming
|
|
130
241
|
// This replaces the old exec method to match ISandbox interface
|
|
131
242
|
async exec(command: string, options?: ExecOptions): Promise<ExecResult> {
|
|
243
|
+
const session = await this.ensureDefaultSession();
|
|
244
|
+
return this.execWithSession(command, session, options);
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/**
|
|
248
|
+
* Internal session-aware exec implementation
|
|
249
|
+
* Used by both public exec() and session wrappers
|
|
250
|
+
*/
|
|
251
|
+
private async execWithSession(
|
|
252
|
+
command: string,
|
|
253
|
+
sessionId: string,
|
|
254
|
+
options?: ExecOptions
|
|
255
|
+
): Promise<ExecResult> {
|
|
132
256
|
const startTime = Date.now();
|
|
133
257
|
const timestamp = new Date().toISOString();
|
|
134
258
|
|
|
@@ -145,16 +269,13 @@ export class Sandbox<Env = unknown> extends Container<Env> implements ISandbox {
|
|
|
145
269
|
|
|
146
270
|
if (options?.stream && options?.onOutput) {
|
|
147
271
|
// Streaming with callbacks - we need to collect the final result
|
|
148
|
-
result = await this.executeWithStreaming(command, options, startTime, timestamp);
|
|
272
|
+
result = await this.executeWithStreaming(command, sessionId, options, startTime, timestamp);
|
|
149
273
|
} else {
|
|
150
|
-
// Regular execution
|
|
151
|
-
const response = await this.client.execute(
|
|
152
|
-
command,
|
|
153
|
-
options?.sessionId
|
|
154
|
-
);
|
|
274
|
+
// Regular execution with session
|
|
275
|
+
const response = await this.client.commands.execute(command, sessionId);
|
|
155
276
|
|
|
156
277
|
const duration = Date.now() - startTime;
|
|
157
|
-
result = this.mapExecuteResponseToExecResult(response, duration,
|
|
278
|
+
result = this.mapExecuteResponseToExecResult(response, duration, sessionId);
|
|
158
279
|
}
|
|
159
280
|
|
|
160
281
|
// Call completion callback if provided
|
|
@@ -177,6 +298,7 @@ export class Sandbox<Env = unknown> extends Container<Env> implements ISandbox {
|
|
|
177
298
|
|
|
178
299
|
private async executeWithStreaming(
|
|
179
300
|
command: string,
|
|
301
|
+
sessionId: string,
|
|
180
302
|
options: ExecOptions,
|
|
181
303
|
startTime: number,
|
|
182
304
|
timestamp: string
|
|
@@ -185,10 +307,9 @@ export class Sandbox<Env = unknown> extends Container<Env> implements ISandbox {
|
|
|
185
307
|
let stderr = '';
|
|
186
308
|
|
|
187
309
|
try {
|
|
188
|
-
const stream = await this.client.
|
|
189
|
-
const { parseSSEStream } = await import('./sse-parser');
|
|
310
|
+
const stream = await this.client.commands.executeStream(command, sessionId);
|
|
190
311
|
|
|
191
|
-
for await (const event of parseSSEStream<
|
|
312
|
+
for await (const event of parseSSEStream<ExecEvent>(stream)) {
|
|
192
313
|
// Check for cancellation
|
|
193
314
|
if (options.signal?.aborted) {
|
|
194
315
|
throw new Error('Operation was aborted');
|
|
@@ -212,20 +333,20 @@ export class Sandbox<Env = unknown> extends Container<Env> implements ISandbox {
|
|
|
212
333
|
case 'complete': {
|
|
213
334
|
// Use result from complete event if available
|
|
214
335
|
const duration = Date.now() - startTime;
|
|
215
|
-
return
|
|
216
|
-
success: event.exitCode === 0,
|
|
217
|
-
exitCode: event.exitCode
|
|
336
|
+
return {
|
|
337
|
+
success: (event.exitCode ?? 0) === 0,
|
|
338
|
+
exitCode: event.exitCode ?? 0,
|
|
218
339
|
stdout,
|
|
219
340
|
stderr,
|
|
220
341
|
command,
|
|
221
342
|
duration,
|
|
222
343
|
timestamp,
|
|
223
|
-
sessionId
|
|
344
|
+
sessionId
|
|
224
345
|
};
|
|
225
346
|
}
|
|
226
347
|
|
|
227
348
|
case 'error':
|
|
228
|
-
throw new Error(event.
|
|
349
|
+
throw new Error(event.data || 'Command execution failed');
|
|
229
350
|
}
|
|
230
351
|
}
|
|
231
352
|
|
|
@@ -241,7 +362,7 @@ export class Sandbox<Env = unknown> extends Container<Env> implements ISandbox {
|
|
|
241
362
|
}
|
|
242
363
|
|
|
243
364
|
private mapExecuteResponseToExecResult(
|
|
244
|
-
response:
|
|
365
|
+
response: ExecuteResponse,
|
|
245
366
|
duration: number,
|
|
246
367
|
sessionId?: string
|
|
247
368
|
): ExecResult {
|
|
@@ -257,57 +378,68 @@ export class Sandbox<Env = unknown> extends Container<Env> implements ISandbox {
|
|
|
257
378
|
};
|
|
258
379
|
}
|
|
259
380
|
|
|
381
|
+
/**
|
|
382
|
+
* Create a Process domain object from HTTP client DTO
|
|
383
|
+
* Centralizes process object creation with bound methods
|
|
384
|
+
* This eliminates duplication across startProcess, listProcesses, getProcess, and session wrappers
|
|
385
|
+
*/
|
|
386
|
+
private createProcessFromDTO(
|
|
387
|
+
data: {
|
|
388
|
+
id: string;
|
|
389
|
+
pid?: number;
|
|
390
|
+
command: string;
|
|
391
|
+
status: ProcessStatus;
|
|
392
|
+
startTime: string | Date;
|
|
393
|
+
endTime?: string | Date;
|
|
394
|
+
exitCode?: number;
|
|
395
|
+
},
|
|
396
|
+
sessionId: string
|
|
397
|
+
): Process {
|
|
398
|
+
return {
|
|
399
|
+
id: data.id,
|
|
400
|
+
pid: data.pid,
|
|
401
|
+
command: data.command,
|
|
402
|
+
status: data.status,
|
|
403
|
+
startTime: typeof data.startTime === 'string' ? new Date(data.startTime) : data.startTime,
|
|
404
|
+
endTime: data.endTime ? (typeof data.endTime === 'string' ? new Date(data.endTime) : data.endTime) : undefined,
|
|
405
|
+
exitCode: data.exitCode,
|
|
406
|
+
sessionId,
|
|
407
|
+
|
|
408
|
+
kill: async (signal?: string) => {
|
|
409
|
+
await this.killProcess(data.id, signal);
|
|
410
|
+
},
|
|
411
|
+
|
|
412
|
+
getStatus: async () => {
|
|
413
|
+
const current = await this.getProcess(data.id);
|
|
414
|
+
return current?.status || 'error';
|
|
415
|
+
},
|
|
416
|
+
|
|
417
|
+
getLogs: async () => {
|
|
418
|
+
const logs = await this.getProcessLogs(data.id);
|
|
419
|
+
return { stdout: logs.stdout, stderr: logs.stderr };
|
|
420
|
+
}
|
|
421
|
+
};
|
|
422
|
+
}
|
|
423
|
+
|
|
260
424
|
|
|
261
425
|
// Background process management
|
|
262
|
-
async startProcess(command: string, options?: ProcessOptions): Promise<Process> {
|
|
426
|
+
async startProcess(command: string, options?: ProcessOptions, sessionId?: string): Promise<Process> {
|
|
263
427
|
// Use the new HttpClient method to start the process
|
|
264
428
|
try {
|
|
265
|
-
const
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
timeout: options?.timeout,
|
|
269
|
-
env: options?.env,
|
|
270
|
-
cwd: options?.cwd,
|
|
271
|
-
encoding: options?.encoding,
|
|
272
|
-
autoCleanup: options?.autoCleanup
|
|
429
|
+
const session = sessionId ?? await this.ensureDefaultSession();
|
|
430
|
+
const response = await this.client.processes.startProcess(command, session, {
|
|
431
|
+
processId: options?.processId
|
|
273
432
|
});
|
|
274
433
|
|
|
275
|
-
const
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
startTime: new Date(process.startTime),
|
|
434
|
+
const processObj = this.createProcessFromDTO({
|
|
435
|
+
id: response.processId,
|
|
436
|
+
pid: response.pid,
|
|
437
|
+
command: response.command,
|
|
438
|
+
status: 'running' as ProcessStatus,
|
|
439
|
+
startTime: new Date(),
|
|
282
440
|
endTime: undefined,
|
|
283
|
-
exitCode: undefined
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
async kill(): Promise<void> {
|
|
287
|
-
throw new Error('Method will be replaced');
|
|
288
|
-
},
|
|
289
|
-
async getStatus(): Promise<ProcessStatus> {
|
|
290
|
-
throw new Error('Method will be replaced');
|
|
291
|
-
},
|
|
292
|
-
async getLogs(): Promise<{ stdout: string; stderr: string }> {
|
|
293
|
-
throw new Error('Method will be replaced');
|
|
294
|
-
}
|
|
295
|
-
};
|
|
296
|
-
|
|
297
|
-
// Bind context properly
|
|
298
|
-
processObj.kill = async (signal?: string) => {
|
|
299
|
-
await this.killProcess(process.id, signal);
|
|
300
|
-
};
|
|
301
|
-
|
|
302
|
-
processObj.getStatus = async () => {
|
|
303
|
-
const current = await this.getProcess(process.id);
|
|
304
|
-
return current?.status || 'error';
|
|
305
|
-
};
|
|
306
|
-
|
|
307
|
-
processObj.getLogs = async () => {
|
|
308
|
-
const logs = await this.getProcessLogs(process.id);
|
|
309
|
-
return { stdout: logs.stdout, stderr: logs.stderr };
|
|
310
|
-
};
|
|
441
|
+
exitCode: undefined
|
|
442
|
+
}, session);
|
|
311
443
|
|
|
312
444
|
// Call onStart callback if provided
|
|
313
445
|
if (options?.onStart) {
|
|
@@ -325,108 +457,68 @@ export class Sandbox<Env = unknown> extends Container<Env> implements ISandbox {
|
|
|
325
457
|
}
|
|
326
458
|
}
|
|
327
459
|
|
|
328
|
-
async listProcesses(): Promise<Process[]> {
|
|
329
|
-
const
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
},
|
|
344
|
-
|
|
345
|
-
getStatus: async () => {
|
|
346
|
-
const current = await this.getProcess(processData.id);
|
|
347
|
-
return current?.status || 'error';
|
|
348
|
-
},
|
|
349
|
-
|
|
350
|
-
getLogs: async () => {
|
|
351
|
-
const logs = await this.getProcessLogs(processData.id);
|
|
352
|
-
return { stdout: logs.stdout, stderr: logs.stderr };
|
|
353
|
-
}
|
|
354
|
-
}));
|
|
460
|
+
async listProcesses(sessionId?: string): Promise<Process[]> {
|
|
461
|
+
const session = sessionId ?? await this.ensureDefaultSession();
|
|
462
|
+
const response = await this.client.processes.listProcesses();
|
|
463
|
+
|
|
464
|
+
return response.processes.map(processData =>
|
|
465
|
+
this.createProcessFromDTO({
|
|
466
|
+
id: processData.id,
|
|
467
|
+
pid: processData.pid,
|
|
468
|
+
command: processData.command,
|
|
469
|
+
status: processData.status,
|
|
470
|
+
startTime: processData.startTime,
|
|
471
|
+
endTime: processData.endTime,
|
|
472
|
+
exitCode: processData.exitCode
|
|
473
|
+
}, session)
|
|
474
|
+
);
|
|
355
475
|
}
|
|
356
476
|
|
|
357
|
-
async getProcess(id: string): Promise<Process | null> {
|
|
358
|
-
const
|
|
477
|
+
async getProcess(id: string, sessionId?: string): Promise<Process | null> {
|
|
478
|
+
const session = sessionId ?? await this.ensureDefaultSession();
|
|
479
|
+
const response = await this.client.processes.getProcess(id);
|
|
359
480
|
if (!response.process) {
|
|
360
481
|
return null;
|
|
361
482
|
}
|
|
362
483
|
|
|
363
484
|
const processData = response.process;
|
|
364
|
-
return {
|
|
485
|
+
return this.createProcessFromDTO({
|
|
365
486
|
id: processData.id,
|
|
366
487
|
pid: processData.pid,
|
|
367
488
|
command: processData.command,
|
|
368
489
|
status: processData.status,
|
|
369
|
-
startTime:
|
|
370
|
-
endTime: processData.endTime
|
|
371
|
-
exitCode: processData.exitCode
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
kill: async (signal?: string) => {
|
|
375
|
-
await this.killProcess(processData.id, signal);
|
|
376
|
-
},
|
|
377
|
-
|
|
378
|
-
getStatus: async () => {
|
|
379
|
-
const current = await this.getProcess(processData.id);
|
|
380
|
-
return current?.status || 'error';
|
|
381
|
-
},
|
|
382
|
-
|
|
383
|
-
getLogs: async () => {
|
|
384
|
-
const logs = await this.getProcessLogs(processData.id);
|
|
385
|
-
return { stdout: logs.stdout, stderr: logs.stderr };
|
|
386
|
-
}
|
|
387
|
-
};
|
|
490
|
+
startTime: processData.startTime,
|
|
491
|
+
endTime: processData.endTime,
|
|
492
|
+
exitCode: processData.exitCode
|
|
493
|
+
}, session);
|
|
388
494
|
}
|
|
389
495
|
|
|
390
|
-
async killProcess(id: string,
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
} catch (error) {
|
|
395
|
-
if (error instanceof Error && error.message.includes('Process not found')) {
|
|
396
|
-
throw new ProcessNotFoundError(id);
|
|
397
|
-
}
|
|
398
|
-
throw new SandboxError(
|
|
399
|
-
`Failed to kill process ${id}: ${error instanceof Error ? error.message : 'Unknown error'}`,
|
|
400
|
-
'KILL_PROCESS_FAILED'
|
|
401
|
-
);
|
|
402
|
-
}
|
|
496
|
+
async killProcess(id: string, signal?: string, sessionId?: string): Promise<void> {
|
|
497
|
+
// Note: signal parameter is not currently supported by the HttpClient implementation
|
|
498
|
+
// The HTTP client already throws properly typed errors, so we just let them propagate
|
|
499
|
+
await this.client.processes.killProcess(id);
|
|
403
500
|
}
|
|
404
501
|
|
|
405
|
-
async killAllProcesses(): Promise<number> {
|
|
406
|
-
const response = await this.client.killAllProcesses();
|
|
407
|
-
return response.
|
|
502
|
+
async killAllProcesses(sessionId?: string): Promise<number> {
|
|
503
|
+
const response = await this.client.processes.killAllProcesses();
|
|
504
|
+
return response.cleanedCount;
|
|
408
505
|
}
|
|
409
506
|
|
|
410
|
-
async cleanupCompletedProcesses(): Promise<number> {
|
|
507
|
+
async cleanupCompletedProcesses(sessionId?: string): Promise<number> {
|
|
411
508
|
// For now, this would need to be implemented as a container endpoint
|
|
412
509
|
// as we no longer maintain local process storage
|
|
413
510
|
// We'll return 0 as a placeholder until the container endpoint is added
|
|
414
511
|
return 0;
|
|
415
512
|
}
|
|
416
513
|
|
|
417
|
-
async getProcessLogs(id: string): Promise<{ stdout: string; stderr: string }> {
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
}
|
|
425
|
-
if (error instanceof Error && error.message.includes('Process not found')) {
|
|
426
|
-
throw new ProcessNotFoundError(id);
|
|
427
|
-
}
|
|
428
|
-
throw error;
|
|
429
|
-
}
|
|
514
|
+
async getProcessLogs(id: string, sessionId?: string): Promise<{ stdout: string; stderr: string; processId: string }> {
|
|
515
|
+
// The HTTP client already throws properly typed errors, so we just let them propagate
|
|
516
|
+
const response = await this.client.processes.getProcessLogs(id);
|
|
517
|
+
return {
|
|
518
|
+
stdout: response.stdout,
|
|
519
|
+
stderr: response.stderr,
|
|
520
|
+
processId: response.processId
|
|
521
|
+
};
|
|
430
522
|
}
|
|
431
523
|
|
|
432
524
|
|
|
@@ -437,11 +529,21 @@ export class Sandbox<Env = unknown> extends Container<Env> implements ISandbox {
|
|
|
437
529
|
throw new Error('Operation was aborted');
|
|
438
530
|
}
|
|
439
531
|
|
|
440
|
-
|
|
441
|
-
|
|
532
|
+
const session = await this.ensureDefaultSession();
|
|
533
|
+
// Get the stream from CommandClient
|
|
534
|
+
return this.client.commands.executeStream(command, session);
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
/**
|
|
538
|
+
* Internal session-aware execStream implementation
|
|
539
|
+
*/
|
|
540
|
+
private async execStreamWithSession(command: string, sessionId: string, options?: StreamOptions): Promise<ReadableStream<Uint8Array>> {
|
|
541
|
+
// Check for cancellation
|
|
542
|
+
if (options?.signal?.aborted) {
|
|
543
|
+
throw new Error('Operation was aborted');
|
|
544
|
+
}
|
|
442
545
|
|
|
443
|
-
|
|
444
|
-
return stream;
|
|
546
|
+
return this.client.commands.executeStream(command, sessionId);
|
|
445
547
|
}
|
|
446
548
|
|
|
447
549
|
async streamProcessLogs(processId: string, options?: { signal?: AbortSignal }): Promise<ReadableStream<Uint8Array>> {
|
|
@@ -450,69 +552,117 @@ export class Sandbox<Env = unknown> extends Container<Env> implements ISandbox {
|
|
|
450
552
|
throw new Error('Operation was aborted');
|
|
451
553
|
}
|
|
452
554
|
|
|
453
|
-
|
|
454
|
-
const stream = await this.client.streamProcessLogs(processId);
|
|
455
|
-
|
|
456
|
-
// Return the ReadableStream directly - can be converted to AsyncIterable by consumers
|
|
457
|
-
return stream;
|
|
555
|
+
return this.client.processes.streamProcessLogs(processId);
|
|
458
556
|
}
|
|
459
557
|
|
|
460
558
|
async gitCheckout(
|
|
461
559
|
repoUrl: string,
|
|
462
|
-
options: { branch?: string; targetDir?: string }
|
|
560
|
+
options: { branch?: string; targetDir?: string; sessionId?: string }
|
|
463
561
|
) {
|
|
464
|
-
|
|
562
|
+
const session = options.sessionId ?? await this.ensureDefaultSession();
|
|
563
|
+
return this.client.git.checkout(repoUrl, session, {
|
|
564
|
+
branch: options.branch,
|
|
565
|
+
targetDir: options.targetDir
|
|
566
|
+
});
|
|
465
567
|
}
|
|
466
568
|
|
|
467
569
|
async mkdir(
|
|
468
570
|
path: string,
|
|
469
|
-
options: { recursive?: boolean } = {}
|
|
571
|
+
options: { recursive?: boolean; sessionId?: string } = {}
|
|
470
572
|
) {
|
|
471
|
-
|
|
573
|
+
const session = options.sessionId ?? await this.ensureDefaultSession();
|
|
574
|
+
return this.client.files.mkdir(path, session, { recursive: options.recursive });
|
|
472
575
|
}
|
|
473
576
|
|
|
474
577
|
async writeFile(
|
|
475
578
|
path: string,
|
|
476
579
|
content: string,
|
|
477
|
-
options: { encoding?: string } = {}
|
|
580
|
+
options: { encoding?: string; sessionId?: string } = {}
|
|
478
581
|
) {
|
|
479
|
-
|
|
582
|
+
const session = options.sessionId ?? await this.ensureDefaultSession();
|
|
583
|
+
return this.client.files.writeFile(path, content, session, { encoding: options.encoding });
|
|
480
584
|
}
|
|
481
585
|
|
|
482
|
-
async deleteFile(path: string) {
|
|
483
|
-
|
|
586
|
+
async deleteFile(path: string, sessionId?: string) {
|
|
587
|
+
const session = sessionId ?? await this.ensureDefaultSession();
|
|
588
|
+
return this.client.files.deleteFile(path, session);
|
|
484
589
|
}
|
|
485
590
|
|
|
486
591
|
async renameFile(
|
|
487
592
|
oldPath: string,
|
|
488
|
-
newPath: string
|
|
593
|
+
newPath: string,
|
|
594
|
+
sessionId?: string
|
|
489
595
|
) {
|
|
490
|
-
|
|
596
|
+
const session = sessionId ?? await this.ensureDefaultSession();
|
|
597
|
+
return this.client.files.renameFile(oldPath, newPath, session);
|
|
491
598
|
}
|
|
492
599
|
|
|
493
600
|
async moveFile(
|
|
494
601
|
sourcePath: string,
|
|
495
|
-
destinationPath: string
|
|
602
|
+
destinationPath: string,
|
|
603
|
+
sessionId?: string
|
|
496
604
|
) {
|
|
497
|
-
|
|
605
|
+
const session = sessionId ?? await this.ensureDefaultSession();
|
|
606
|
+
return this.client.files.moveFile(sourcePath, destinationPath, session);
|
|
498
607
|
}
|
|
499
608
|
|
|
500
609
|
async readFile(
|
|
501
610
|
path: string,
|
|
502
|
-
options: { encoding?: string } = {}
|
|
611
|
+
options: { encoding?: string; sessionId?: string } = {}
|
|
612
|
+
) {
|
|
613
|
+
const session = options.sessionId ?? await this.ensureDefaultSession();
|
|
614
|
+
return this.client.files.readFile(path, session, { encoding: options.encoding });
|
|
615
|
+
}
|
|
616
|
+
|
|
617
|
+
/**
|
|
618
|
+
* Stream a file from the sandbox using Server-Sent Events
|
|
619
|
+
* Returns a ReadableStream that can be consumed with streamFile() or collectFile() utilities
|
|
620
|
+
* @param path - Path to the file to stream
|
|
621
|
+
* @param options - Optional session ID
|
|
622
|
+
*/
|
|
623
|
+
async readFileStream(
|
|
624
|
+
path: string,
|
|
625
|
+
options: { sessionId?: string } = {}
|
|
626
|
+
): Promise<ReadableStream<Uint8Array>> {
|
|
627
|
+
const session = options.sessionId ?? await this.ensureDefaultSession();
|
|
628
|
+
return this.client.files.readFileStream(path, session);
|
|
629
|
+
}
|
|
630
|
+
|
|
631
|
+
async listFiles(
|
|
632
|
+
path: string,
|
|
633
|
+
options?: { recursive?: boolean; includeHidden?: boolean }
|
|
503
634
|
) {
|
|
504
|
-
|
|
635
|
+
const session = await this.ensureDefaultSession();
|
|
636
|
+
return this.client.files.listFiles(path, session, options);
|
|
505
637
|
}
|
|
506
638
|
|
|
507
639
|
async exposePort(port: number, options: { name?: string; hostname: string }) {
|
|
508
|
-
|
|
640
|
+
// Check if hostname is workers.dev domain (doesn't support wildcard subdomains)
|
|
641
|
+
if (options.hostname.endsWith('.workers.dev')) {
|
|
642
|
+
const errorResponse: ErrorResponse = {
|
|
643
|
+
code: ErrorCode.CUSTOM_DOMAIN_REQUIRED,
|
|
644
|
+
message: `Port exposure requires a custom domain. .workers.dev domains do not support wildcard subdomains required for port proxying.`,
|
|
645
|
+
context: { originalError: options.hostname },
|
|
646
|
+
httpStatus: 400,
|
|
647
|
+
timestamp: new Date().toISOString()
|
|
648
|
+
};
|
|
649
|
+
throw new CustomDomainRequiredError(errorResponse);
|
|
650
|
+
}
|
|
651
|
+
|
|
652
|
+
const sessionId = await this.ensureDefaultSession();
|
|
653
|
+
await this.client.ports.exposePort(port, sessionId, options?.name);
|
|
509
654
|
|
|
510
655
|
// We need the sandbox name to construct preview URLs
|
|
511
656
|
if (!this.sandboxName) {
|
|
512
657
|
throw new Error('Sandbox name not available. Ensure sandbox is accessed through getSandbox()');
|
|
513
658
|
}
|
|
514
659
|
|
|
515
|
-
|
|
660
|
+
// Generate and store token for this port
|
|
661
|
+
const token = this.generatePortToken();
|
|
662
|
+
this.portTokens.set(port, token);
|
|
663
|
+
await this.persistPortTokens();
|
|
664
|
+
|
|
665
|
+
const url = this.constructPreviewUrl(port, this.sandboxName, options.hostname, token);
|
|
516
666
|
|
|
517
667
|
return {
|
|
518
668
|
url,
|
|
@@ -523,58 +673,101 @@ export class Sandbox<Env = unknown> extends Container<Env> implements ISandbox {
|
|
|
523
673
|
|
|
524
674
|
async unexposePort(port: number) {
|
|
525
675
|
if (!validatePort(port)) {
|
|
526
|
-
logSecurityEvent('INVALID_PORT_UNEXPOSE', {
|
|
527
|
-
port
|
|
528
|
-
}, 'high');
|
|
529
676
|
throw new SecurityError(`Invalid port number: ${port}. Must be between 1024-65535 and not reserved.`);
|
|
530
677
|
}
|
|
531
678
|
|
|
532
|
-
await this.
|
|
679
|
+
const sessionId = await this.ensureDefaultSession();
|
|
680
|
+
await this.client.ports.unexposePort(port, sessionId);
|
|
533
681
|
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
682
|
+
// Clean up token for this port
|
|
683
|
+
if (this.portTokens.has(port)) {
|
|
684
|
+
this.portTokens.delete(port);
|
|
685
|
+
await this.persistPortTokens();
|
|
686
|
+
}
|
|
537
687
|
}
|
|
538
688
|
|
|
539
689
|
async getExposedPorts(hostname: string) {
|
|
540
|
-
const
|
|
690
|
+
const sessionId = await this.ensureDefaultSession();
|
|
691
|
+
const response = await this.client.ports.getExposedPorts(sessionId);
|
|
541
692
|
|
|
542
693
|
// We need the sandbox name to construct preview URLs
|
|
543
694
|
if (!this.sandboxName) {
|
|
544
695
|
throw new Error('Sandbox name not available. Ensure sandbox is accessed through getSandbox()');
|
|
545
696
|
}
|
|
546
697
|
|
|
547
|
-
return response.ports.map(port =>
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
698
|
+
return response.ports.map(port => {
|
|
699
|
+
// Get token for this port - must exist for all exposed ports
|
|
700
|
+
const token = this.portTokens.get(port.port);
|
|
701
|
+
if (!token) {
|
|
702
|
+
throw new Error(`Port ${port.port} is exposed but has no token. This should not happen.`);
|
|
703
|
+
}
|
|
704
|
+
|
|
705
|
+
return {
|
|
706
|
+
url: this.constructPreviewUrl(port.port, this.sandboxName!, hostname, token),
|
|
707
|
+
port: port.port,
|
|
708
|
+
status: port.status,
|
|
709
|
+
};
|
|
710
|
+
});
|
|
553
711
|
}
|
|
554
712
|
|
|
555
713
|
|
|
556
|
-
|
|
714
|
+
async isPortExposed(port: number): Promise<boolean> {
|
|
715
|
+
try {
|
|
716
|
+
const sessionId = await this.ensureDefaultSession();
|
|
717
|
+
const response = await this.client.ports.getExposedPorts(sessionId);
|
|
718
|
+
return response.ports.some(exposedPort => exposedPort.port === port);
|
|
719
|
+
} catch (error) {
|
|
720
|
+
this.logger.error('Error checking if port is exposed', error instanceof Error ? error : new Error(String(error)), { port });
|
|
721
|
+
return false;
|
|
722
|
+
}
|
|
723
|
+
}
|
|
724
|
+
|
|
725
|
+
async validatePortToken(port: number, token: string): Promise<boolean> {
|
|
726
|
+
// First check if port is exposed
|
|
727
|
+
const isExposed = await this.isPortExposed(port);
|
|
728
|
+
if (!isExposed) {
|
|
729
|
+
return false;
|
|
730
|
+
}
|
|
731
|
+
|
|
732
|
+
// Get stored token for this port - must exist for all exposed ports
|
|
733
|
+
const storedToken = this.portTokens.get(port);
|
|
734
|
+
if (!storedToken) {
|
|
735
|
+
// This should not happen - all exposed ports must have tokens
|
|
736
|
+
this.logger.error('Port is exposed but has no token - bug detected', undefined, { port });
|
|
737
|
+
return false;
|
|
738
|
+
}
|
|
739
|
+
|
|
740
|
+
// Constant-time comparison to prevent timing attacks
|
|
741
|
+
return storedToken === token;
|
|
742
|
+
}
|
|
743
|
+
|
|
744
|
+
private generatePortToken(): string {
|
|
745
|
+
// Generate cryptographically secure 16-character token using Web Crypto API
|
|
746
|
+
// Available in Cloudflare Workers runtime
|
|
747
|
+
const array = new Uint8Array(12); // 12 bytes = 16 base64url chars (after padding removal)
|
|
748
|
+
crypto.getRandomValues(array);
|
|
749
|
+
|
|
750
|
+
// Convert to base64url format (URL-safe, no padding, lowercase)
|
|
751
|
+
const base64 = btoa(String.fromCharCode(...array));
|
|
752
|
+
return base64.replace(/\+/g, '-').replace(/\//g, '_').replace(/=/g, '').toLowerCase();
|
|
753
|
+
}
|
|
754
|
+
|
|
755
|
+
private async persistPortTokens(): Promise<void> {
|
|
756
|
+
// Convert Map to plain object for storage
|
|
757
|
+
const tokensObj: Record<string, string> = {};
|
|
758
|
+
for (const [port, token] of this.portTokens.entries()) {
|
|
759
|
+
tokensObj[port.toString()] = token;
|
|
760
|
+
}
|
|
761
|
+
await this.ctx.storage.put('portTokens', tokensObj);
|
|
762
|
+
}
|
|
763
|
+
|
|
764
|
+
private constructPreviewUrl(port: number, sandboxId: string, hostname: string, token: string): string {
|
|
557
765
|
if (!validatePort(port)) {
|
|
558
|
-
logSecurityEvent('INVALID_PORT_REJECTED', {
|
|
559
|
-
port,
|
|
560
|
-
sandboxId,
|
|
561
|
-
hostname
|
|
562
|
-
}, 'high');
|
|
563
766
|
throw new SecurityError(`Invalid port number: ${port}. Must be between 1024-65535 and not reserved.`);
|
|
564
767
|
}
|
|
565
768
|
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
sanitizedSandboxId = sanitizeSandboxId(sandboxId);
|
|
569
|
-
} catch (error) {
|
|
570
|
-
logSecurityEvent('INVALID_SANDBOX_ID_REJECTED', {
|
|
571
|
-
sandboxId,
|
|
572
|
-
port,
|
|
573
|
-
hostname,
|
|
574
|
-
error: error instanceof Error ? error.message : 'Unknown error'
|
|
575
|
-
}, 'high');
|
|
576
|
-
throw error;
|
|
577
|
-
}
|
|
769
|
+
// Validate sandbox ID (will throw SecurityError if invalid)
|
|
770
|
+
const sanitizedSandboxId = sanitizeSandboxId(sandboxId);
|
|
578
771
|
|
|
579
772
|
const isLocalhost = isLocalhostPattern(hostname);
|
|
580
773
|
|
|
@@ -586,28 +779,12 @@ export class Sandbox<Env = unknown> extends Container<Env> implements ISandbox {
|
|
|
586
779
|
// Use URL constructor for safe URL building
|
|
587
780
|
try {
|
|
588
781
|
const baseUrl = new URL(`http://${host}:${mainPort}`);
|
|
589
|
-
// Construct subdomain safely
|
|
590
|
-
const subdomainHost = `${port}-${sanitizedSandboxId}.${host}`;
|
|
782
|
+
// Construct subdomain safely with mandatory token
|
|
783
|
+
const subdomainHost = `${port}-${sanitizedSandboxId}-${token}.${host}`;
|
|
591
784
|
baseUrl.hostname = subdomainHost;
|
|
592
785
|
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
logSecurityEvent('PREVIEW_URL_CONSTRUCTED', {
|
|
596
|
-
port,
|
|
597
|
-
sandboxId: sanitizedSandboxId,
|
|
598
|
-
hostname,
|
|
599
|
-
resultUrl: finalUrl,
|
|
600
|
-
environment: 'localhost'
|
|
601
|
-
}, 'low');
|
|
602
|
-
|
|
603
|
-
return finalUrl;
|
|
786
|
+
return baseUrl.toString();
|
|
604
787
|
} catch (error) {
|
|
605
|
-
logSecurityEvent('URL_CONSTRUCTION_FAILED', {
|
|
606
|
-
port,
|
|
607
|
-
sandboxId: sanitizedSandboxId,
|
|
608
|
-
hostname,
|
|
609
|
-
error: error instanceof Error ? error.message : 'Unknown error'
|
|
610
|
-
}, 'high');
|
|
611
788
|
throw new SecurityError(`Failed to construct preview URL: ${error instanceof Error ? error.message : 'Unknown error'}`);
|
|
612
789
|
}
|
|
613
790
|
}
|
|
@@ -618,29 +795,142 @@ export class Sandbox<Env = unknown> extends Container<Env> implements ISandbox {
|
|
|
618
795
|
const protocol = "https";
|
|
619
796
|
const baseUrl = new URL(`${protocol}://${hostname}`);
|
|
620
797
|
|
|
621
|
-
// Construct subdomain safely
|
|
622
|
-
const subdomainHost = `${port}-${sanitizedSandboxId}.${hostname}`;
|
|
798
|
+
// Construct subdomain safely with mandatory token
|
|
799
|
+
const subdomainHost = `${port}-${sanitizedSandboxId}-${token}.${hostname}`;
|
|
623
800
|
baseUrl.hostname = subdomainHost;
|
|
624
801
|
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
logSecurityEvent('PREVIEW_URL_CONSTRUCTED', {
|
|
628
|
-
port,
|
|
629
|
-
sandboxId: sanitizedSandboxId,
|
|
630
|
-
hostname,
|
|
631
|
-
resultUrl: finalUrl,
|
|
632
|
-
environment: 'production'
|
|
633
|
-
}, 'low');
|
|
634
|
-
|
|
635
|
-
return finalUrl;
|
|
802
|
+
return baseUrl.toString();
|
|
636
803
|
} catch (error) {
|
|
637
|
-
logSecurityEvent('URL_CONSTRUCTION_FAILED', {
|
|
638
|
-
port,
|
|
639
|
-
sandboxId: sanitizedSandboxId,
|
|
640
|
-
hostname,
|
|
641
|
-
error: error instanceof Error ? error.message : 'Unknown error'
|
|
642
|
-
}, 'high');
|
|
643
804
|
throw new SecurityError(`Failed to construct preview URL: ${error instanceof Error ? error.message : 'Unknown error'}`);
|
|
644
805
|
}
|
|
645
806
|
}
|
|
807
|
+
|
|
808
|
+
// ============================================================================
|
|
809
|
+
// Session Management - Advanced Use Cases
|
|
810
|
+
// ============================================================================
|
|
811
|
+
|
|
812
|
+
/**
|
|
813
|
+
* Create isolated execution session for advanced use cases
|
|
814
|
+
* Returns ExecutionSession with full sandbox API bound to specific session
|
|
815
|
+
*/
|
|
816
|
+
async createSession(options?: SessionOptions): Promise<ExecutionSession> {
|
|
817
|
+
const sessionId = options?.id || `session-${Date.now()}`;
|
|
818
|
+
|
|
819
|
+
// Create session in container
|
|
820
|
+
await this.client.utils.createSession({
|
|
821
|
+
id: sessionId,
|
|
822
|
+
env: options?.env,
|
|
823
|
+
cwd: options?.cwd,
|
|
824
|
+
});
|
|
825
|
+
|
|
826
|
+
// Return wrapper that binds sessionId to all operations
|
|
827
|
+
return this.getSessionWrapper(sessionId);
|
|
828
|
+
}
|
|
829
|
+
|
|
830
|
+
/**
|
|
831
|
+
* Get an existing session by ID
|
|
832
|
+
* Returns ExecutionSession wrapper bound to the specified session
|
|
833
|
+
*
|
|
834
|
+
* This is useful for retrieving sessions across different requests/contexts
|
|
835
|
+
* without storing the ExecutionSession object (which has RPC lifecycle limitations)
|
|
836
|
+
*
|
|
837
|
+
* @param sessionId - The ID of an existing session
|
|
838
|
+
* @returns ExecutionSession wrapper bound to the session
|
|
839
|
+
*/
|
|
840
|
+
async getSession(sessionId: string): Promise<ExecutionSession> {
|
|
841
|
+
// No need to verify session exists in container - operations will fail naturally if it doesn't
|
|
842
|
+
return this.getSessionWrapper(sessionId);
|
|
843
|
+
}
|
|
844
|
+
|
|
845
|
+
/**
|
|
846
|
+
* Internal helper to create ExecutionSession wrapper for a given sessionId
|
|
847
|
+
* Used by both createSession and getSession
|
|
848
|
+
*/
|
|
849
|
+
private getSessionWrapper(sessionId: string): ExecutionSession {
|
|
850
|
+
return {
|
|
851
|
+
id: sessionId,
|
|
852
|
+
|
|
853
|
+
// Command execution - delegate to internal session-aware methods
|
|
854
|
+
exec: (command, options) => this.execWithSession(command, sessionId, options),
|
|
855
|
+
execStream: (command, options) => this.execStreamWithSession(command, sessionId, options),
|
|
856
|
+
|
|
857
|
+
// Process management
|
|
858
|
+
startProcess: (command, options) => this.startProcess(command, options, sessionId),
|
|
859
|
+
listProcesses: () => this.listProcesses(sessionId),
|
|
860
|
+
getProcess: (id) => this.getProcess(id, sessionId),
|
|
861
|
+
killProcess: (id, signal) => this.killProcess(id, signal),
|
|
862
|
+
killAllProcesses: () => this.killAllProcesses(),
|
|
863
|
+
cleanupCompletedProcesses: () => this.cleanupCompletedProcesses(),
|
|
864
|
+
getProcessLogs: (id) => this.getProcessLogs(id),
|
|
865
|
+
streamProcessLogs: (processId, options) => this.streamProcessLogs(processId, options),
|
|
866
|
+
|
|
867
|
+
// File operations - pass sessionId via options or parameter
|
|
868
|
+
writeFile: (path, content, options) => this.writeFile(path, content, { ...options, sessionId }),
|
|
869
|
+
readFile: (path, options) => this.readFile(path, { ...options, sessionId }),
|
|
870
|
+
readFileStream: (path) => this.readFileStream(path, { sessionId }),
|
|
871
|
+
mkdir: (path, options) => this.mkdir(path, { ...options, sessionId }),
|
|
872
|
+
deleteFile: (path) => this.deleteFile(path, sessionId),
|
|
873
|
+
renameFile: (oldPath, newPath) => this.renameFile(oldPath, newPath, sessionId),
|
|
874
|
+
moveFile: (sourcePath, destPath) => this.moveFile(sourcePath, destPath, sessionId),
|
|
875
|
+
listFiles: (path, options) => this.client.files.listFiles(path, sessionId, options),
|
|
876
|
+
|
|
877
|
+
// Git operations
|
|
878
|
+
gitCheckout: (repoUrl, options) => this.gitCheckout(repoUrl, { ...options, sessionId }),
|
|
879
|
+
|
|
880
|
+
// Environment management - needs special handling
|
|
881
|
+
setEnvVars: async (envVars: Record<string, string>) => {
|
|
882
|
+
try {
|
|
883
|
+
// Set environment variables by executing export commands
|
|
884
|
+
for (const [key, value] of Object.entries(envVars)) {
|
|
885
|
+
const escapedValue = value.replace(/'/g, "'\\''");
|
|
886
|
+
const exportCommand = `export ${key}='${escapedValue}'`;
|
|
887
|
+
|
|
888
|
+
const result = await this.client.commands.execute(exportCommand, sessionId);
|
|
889
|
+
|
|
890
|
+
if (result.exitCode !== 0) {
|
|
891
|
+
throw new Error(`Failed to set ${key}: ${result.stderr || 'Unknown error'}`);
|
|
892
|
+
}
|
|
893
|
+
}
|
|
894
|
+
} catch (error) {
|
|
895
|
+
this.logger.error('Failed to set environment variables', error instanceof Error ? error : new Error(String(error)), { sessionId });
|
|
896
|
+
throw error;
|
|
897
|
+
}
|
|
898
|
+
},
|
|
899
|
+
|
|
900
|
+
// Code interpreter methods - delegate to sandbox's code interpreter
|
|
901
|
+
createCodeContext: (options) => this.codeInterpreter.createCodeContext(options),
|
|
902
|
+
runCode: async (code, options) => {
|
|
903
|
+
const execution = await this.codeInterpreter.runCode(code, options);
|
|
904
|
+
return execution.toJSON();
|
|
905
|
+
},
|
|
906
|
+
runCodeStream: (code, options) => this.codeInterpreter.runCodeStream(code, options),
|
|
907
|
+
listCodeContexts: () => this.codeInterpreter.listCodeContexts(),
|
|
908
|
+
deleteCodeContext: (contextId) => this.codeInterpreter.deleteCodeContext(contextId),
|
|
909
|
+
};
|
|
910
|
+
}
|
|
911
|
+
|
|
912
|
+
// ============================================================================
|
|
913
|
+
// Code interpreter methods - delegate to CodeInterpreter wrapper
|
|
914
|
+
// ============================================================================
|
|
915
|
+
|
|
916
|
+
async createCodeContext(options?: CreateContextOptions): Promise<CodeContext> {
|
|
917
|
+
return this.codeInterpreter.createCodeContext(options);
|
|
918
|
+
}
|
|
919
|
+
|
|
920
|
+
async runCode(code: string, options?: RunCodeOptions): Promise<ExecutionResult> {
|
|
921
|
+
const execution = await this.codeInterpreter.runCode(code, options);
|
|
922
|
+
return execution.toJSON();
|
|
923
|
+
}
|
|
924
|
+
|
|
925
|
+
async runCodeStream(code: string, options?: RunCodeOptions): Promise<ReadableStream> {
|
|
926
|
+
return this.codeInterpreter.runCodeStream(code, options);
|
|
927
|
+
}
|
|
928
|
+
|
|
929
|
+
async listCodeContexts(): Promise<CodeContext[]> {
|
|
930
|
+
return this.codeInterpreter.listCodeContexts();
|
|
931
|
+
}
|
|
932
|
+
|
|
933
|
+
async deleteCodeContext(contextId: string): Promise<void> {
|
|
934
|
+
return this.codeInterpreter.deleteCodeContext(contextId);
|
|
935
|
+
}
|
|
646
936
|
}
|