@cloudflare/sandbox 0.4.18 → 0.5.1
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/.turbo/turbo-build.log +8 -8
- package/CHANGELOG.md +52 -0
- package/Dockerfile +5 -1
- package/LICENSE +176 -0
- package/README.md +1 -1
- package/dist/index.d.ts +296 -312
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +525 -55
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
- package/src/clients/base-client.ts +107 -46
- package/src/index.ts +19 -2
- package/src/request-handler.ts +2 -1
- package/src/sandbox.ts +637 -24
- package/src/storage-mount/credential-detection.ts +41 -0
- package/src/storage-mount/errors.ts +51 -0
- package/src/storage-mount/index.ts +17 -0
- package/src/storage-mount/provider-detection.ts +93 -0
- package/src/storage-mount/types.ts +17 -0
- package/src/version.ts +1 -1
- package/tests/base-client.test.ts +218 -0
- package/tests/get-sandbox.test.ts +24 -1
- package/tests/sandbox.test.ts +121 -0
- package/tests/storage-mount/credential-detection.test.ts +119 -0
- package/tests/storage-mount/provider-detection.test.ts +77 -0
- package/tsdown.config.ts +2 -1
package/src/sandbox.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import type { DurableObject } from 'cloudflare:workers';
|
|
2
1
|
import { Container, getContainer, switchPort } from '@cloudflare/containers';
|
|
3
2
|
import type {
|
|
3
|
+
BucketCredentials,
|
|
4
|
+
BucketProvider,
|
|
4
5
|
CodeContext,
|
|
5
6
|
CreateContextOptions,
|
|
6
7
|
ExecEvent,
|
|
@@ -9,6 +10,7 @@ import type {
|
|
|
9
10
|
ExecutionResult,
|
|
10
11
|
ExecutionSession,
|
|
11
12
|
ISandbox,
|
|
13
|
+
MountBucketOptions,
|
|
12
14
|
Process,
|
|
13
15
|
ProcessOptions,
|
|
14
16
|
ProcessStatus,
|
|
@@ -19,8 +21,10 @@ import type {
|
|
|
19
21
|
} from '@repo/shared';
|
|
20
22
|
import {
|
|
21
23
|
createLogger,
|
|
24
|
+
getEnvString,
|
|
22
25
|
runWithLogger,
|
|
23
26
|
type SessionDeleteResult,
|
|
27
|
+
shellEscape,
|
|
24
28
|
TraceContext
|
|
25
29
|
} from '@repo/shared';
|
|
26
30
|
import { type ExecuteResponse, SandboxClient } from './clients';
|
|
@@ -30,6 +34,16 @@ import { CodeInterpreter } from './interpreter';
|
|
|
30
34
|
import { isLocalhostPattern } from './request-handler';
|
|
31
35
|
import { SecurityError, sanitizeSandboxId, validatePort } from './security';
|
|
32
36
|
import { parseSSEStream } from './sse-parser';
|
|
37
|
+
import {
|
|
38
|
+
detectCredentials,
|
|
39
|
+
detectProviderFromUrl,
|
|
40
|
+
resolveS3fsOptions
|
|
41
|
+
} from './storage-mount';
|
|
42
|
+
import {
|
|
43
|
+
InvalidMountConfigError,
|
|
44
|
+
S3FSMountError
|
|
45
|
+
} from './storage-mount/errors';
|
|
46
|
+
import type { MountInfo } from './storage-mount/types';
|
|
33
47
|
import { SDK_VERSION } from './version';
|
|
34
48
|
|
|
35
49
|
export function getSandbox(
|
|
@@ -37,10 +51,24 @@ export function getSandbox(
|
|
|
37
51
|
id: string,
|
|
38
52
|
options?: SandboxOptions
|
|
39
53
|
): Sandbox {
|
|
40
|
-
const
|
|
54
|
+
const sanitizedId = sanitizeSandboxId(id);
|
|
55
|
+
const effectiveId = options?.normalizeId
|
|
56
|
+
? sanitizedId.toLowerCase()
|
|
57
|
+
: sanitizedId;
|
|
58
|
+
|
|
59
|
+
const hasUppercase = /[A-Z]/.test(sanitizedId);
|
|
60
|
+
if (!options?.normalizeId && hasUppercase) {
|
|
61
|
+
const logger = createLogger({ component: 'sandbox-do' });
|
|
62
|
+
logger.warn(
|
|
63
|
+
`Sandbox ID "${sanitizedId}" contains uppercase letters, which causes issues with preview URLs (hostnames are case-insensitive). ` +
|
|
64
|
+
`normalizeId will default to true in a future version to prevent this. ` +
|
|
65
|
+
`Use lowercase IDs or pass { normalizeId: true } to prepare.`
|
|
66
|
+
);
|
|
67
|
+
}
|
|
41
68
|
|
|
42
|
-
|
|
43
|
-
|
|
69
|
+
const stub = getContainer(ns, effectiveId) as unknown as Sandbox;
|
|
70
|
+
|
|
71
|
+
stub.setSandboxName?.(effectiveId, options?.normalizeId);
|
|
44
72
|
|
|
45
73
|
if (options?.baseUrl) {
|
|
46
74
|
stub.setBaseUrl(options.baseUrl);
|
|
@@ -54,6 +82,10 @@ export function getSandbox(
|
|
|
54
82
|
stub.setKeepAlive(options.keepAlive);
|
|
55
83
|
}
|
|
56
84
|
|
|
85
|
+
if (options?.containerTimeouts) {
|
|
86
|
+
stub.setContainerTimeouts(options.containerTimeouts);
|
|
87
|
+
}
|
|
88
|
+
|
|
57
89
|
return Object.assign(stub, {
|
|
58
90
|
wsConnect: connect(stub)
|
|
59
91
|
});
|
|
@@ -63,7 +95,6 @@ export function connect(stub: {
|
|
|
63
95
|
fetch: (request: Request) => Promise<Response>;
|
|
64
96
|
}) {
|
|
65
97
|
return async (request: Request, port: number) => {
|
|
66
|
-
// Validate port before routing
|
|
67
98
|
if (!validatePort(port)) {
|
|
68
99
|
throw new SecurityError(
|
|
69
100
|
`Invalid or restricted port: ${port}. Ports must be in range 1024-65535 and not reserved.`
|
|
@@ -81,25 +112,54 @@ export class Sandbox<Env = unknown> extends Container<Env> implements ISandbox {
|
|
|
81
112
|
client: SandboxClient;
|
|
82
113
|
private codeInterpreter: CodeInterpreter;
|
|
83
114
|
private sandboxName: string | null = null;
|
|
115
|
+
private normalizeId: boolean = false;
|
|
84
116
|
private baseUrl: string | null = null;
|
|
85
117
|
private portTokens: Map<number, string> = new Map();
|
|
86
118
|
private defaultSession: string | null = null;
|
|
87
119
|
envVars: Record<string, string> = {};
|
|
88
120
|
private logger: ReturnType<typeof createLogger>;
|
|
89
121
|
private keepAliveEnabled: boolean = false;
|
|
122
|
+
private activeMounts: Map<string, MountInfo> = new Map();
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Default container startup timeouts (conservative for production)
|
|
126
|
+
* Based on Cloudflare docs: "Containers take several minutes to provision"
|
|
127
|
+
*/
|
|
128
|
+
private readonly DEFAULT_CONTAINER_TIMEOUTS = {
|
|
129
|
+
// Time to get container instance and launch VM
|
|
130
|
+
// @cloudflare/containers default: 8s (too short for cold starts)
|
|
131
|
+
instanceGetTimeoutMS: 30_000, // 30 seconds
|
|
132
|
+
|
|
133
|
+
// Time for application to start and ports to be ready
|
|
134
|
+
// @cloudflare/containers default: 20s
|
|
135
|
+
portReadyTimeoutMS: 90_000, // 90 seconds (allows for heavy containers)
|
|
136
|
+
|
|
137
|
+
// Polling interval for checking container readiness
|
|
138
|
+
// @cloudflare/containers default: 300ms (too aggressive)
|
|
139
|
+
waitIntervalMS: 1000 // 1 second (reduces load)
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Active container timeout configuration
|
|
144
|
+
* Can be set via options, env vars, or defaults
|
|
145
|
+
*/
|
|
146
|
+
private containerTimeouts = { ...this.DEFAULT_CONTAINER_TIMEOUTS };
|
|
90
147
|
|
|
91
148
|
constructor(ctx: DurableObjectState<{}>, env: Env) {
|
|
92
149
|
super(ctx, env);
|
|
93
150
|
|
|
94
|
-
const envObj = env as
|
|
151
|
+
const envObj = env as Record<string, unknown>;
|
|
95
152
|
// Set sandbox environment variables from env object
|
|
96
153
|
const sandboxEnvKeys = ['SANDBOX_LOG_LEVEL', 'SANDBOX_LOG_FORMAT'] as const;
|
|
97
154
|
sandboxEnvKeys.forEach((key) => {
|
|
98
155
|
if (envObj?.[key]) {
|
|
99
|
-
this.envVars[key] = envObj[key];
|
|
156
|
+
this.envVars[key] = String(envObj[key]);
|
|
100
157
|
}
|
|
101
158
|
});
|
|
102
159
|
|
|
160
|
+
// Initialize timeouts with env var fallbacks
|
|
161
|
+
this.containerTimeouts = this.getDefaultTimeouts(envObj);
|
|
162
|
+
|
|
103
163
|
this.logger = createLogger({
|
|
104
164
|
component: 'sandbox-do',
|
|
105
165
|
sandboxId: this.ctx.id.toString()
|
|
@@ -115,10 +175,11 @@ export class Sandbox<Env = unknown> extends Container<Env> implements ISandbox {
|
|
|
115
175
|
// The CodeInterpreter extracts client.interpreter from the sandbox
|
|
116
176
|
this.codeInterpreter = new CodeInterpreter(this);
|
|
117
177
|
|
|
118
|
-
// Load the sandbox name, port tokens, and default session from storage on initialization
|
|
119
178
|
this.ctx.blockConcurrencyWhile(async () => {
|
|
120
179
|
this.sandboxName =
|
|
121
180
|
(await this.ctx.storage.get<string>('sandboxName')) || null;
|
|
181
|
+
this.normalizeId =
|
|
182
|
+
(await this.ctx.storage.get<boolean>('normalizeId')) || false;
|
|
122
183
|
this.defaultSession =
|
|
123
184
|
(await this.ctx.storage.get<string>('defaultSession')) || null;
|
|
124
185
|
const storedTokens =
|
|
@@ -130,14 +191,27 @@ export class Sandbox<Env = unknown> extends Container<Env> implements ISandbox {
|
|
|
130
191
|
for (const [portStr, token] of Object.entries(storedTokens)) {
|
|
131
192
|
this.portTokens.set(parseInt(portStr, 10), token);
|
|
132
193
|
}
|
|
194
|
+
|
|
195
|
+
// Load saved timeout configuration (highest priority)
|
|
196
|
+
const storedTimeouts =
|
|
197
|
+
await this.ctx.storage.get<
|
|
198
|
+
NonNullable<SandboxOptions['containerTimeouts']>
|
|
199
|
+
>('containerTimeouts');
|
|
200
|
+
if (storedTimeouts) {
|
|
201
|
+
this.containerTimeouts = {
|
|
202
|
+
...this.containerTimeouts,
|
|
203
|
+
...storedTimeouts
|
|
204
|
+
};
|
|
205
|
+
}
|
|
133
206
|
});
|
|
134
207
|
}
|
|
135
208
|
|
|
136
|
-
|
|
137
|
-
async setSandboxName(name: string): Promise<void> {
|
|
209
|
+
async setSandboxName(name: string, normalizeId?: boolean): Promise<void> {
|
|
138
210
|
if (!this.sandboxName) {
|
|
139
211
|
this.sandboxName = name;
|
|
212
|
+
this.normalizeId = normalizeId || false;
|
|
140
213
|
await this.ctx.storage.put('sandboxName', name);
|
|
214
|
+
await this.ctx.storage.put('normalizeId', this.normalizeId);
|
|
141
215
|
}
|
|
142
216
|
}
|
|
143
217
|
|
|
@@ -200,11 +274,427 @@ export class Sandbox<Env = unknown> extends Container<Env> implements ISandbox {
|
|
|
200
274
|
}
|
|
201
275
|
}
|
|
202
276
|
|
|
277
|
+
/**
|
|
278
|
+
* RPC method to configure container startup timeouts
|
|
279
|
+
*/
|
|
280
|
+
async setContainerTimeouts(
|
|
281
|
+
timeouts: NonNullable<SandboxOptions['containerTimeouts']>
|
|
282
|
+
): Promise<void> {
|
|
283
|
+
const validated = { ...this.containerTimeouts };
|
|
284
|
+
|
|
285
|
+
// Validate each timeout if provided
|
|
286
|
+
if (timeouts.instanceGetTimeoutMS !== undefined) {
|
|
287
|
+
validated.instanceGetTimeoutMS = this.validateTimeout(
|
|
288
|
+
timeouts.instanceGetTimeoutMS,
|
|
289
|
+
'instanceGetTimeoutMS',
|
|
290
|
+
5_000,
|
|
291
|
+
300_000
|
|
292
|
+
);
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
if (timeouts.portReadyTimeoutMS !== undefined) {
|
|
296
|
+
validated.portReadyTimeoutMS = this.validateTimeout(
|
|
297
|
+
timeouts.portReadyTimeoutMS,
|
|
298
|
+
'portReadyTimeoutMS',
|
|
299
|
+
10_000,
|
|
300
|
+
600_000
|
|
301
|
+
);
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
if (timeouts.waitIntervalMS !== undefined) {
|
|
305
|
+
validated.waitIntervalMS = this.validateTimeout(
|
|
306
|
+
timeouts.waitIntervalMS,
|
|
307
|
+
'waitIntervalMS',
|
|
308
|
+
100,
|
|
309
|
+
5_000
|
|
310
|
+
);
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
this.containerTimeouts = validated;
|
|
314
|
+
|
|
315
|
+
// Persist to storage
|
|
316
|
+
await this.ctx.storage.put('containerTimeouts', this.containerTimeouts);
|
|
317
|
+
|
|
318
|
+
this.logger.debug('Container timeouts updated', this.containerTimeouts);
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
/**
|
|
322
|
+
* Validate a timeout value is within acceptable range
|
|
323
|
+
* Throws error if invalid - used for user-provided values
|
|
324
|
+
*/
|
|
325
|
+
private validateTimeout(
|
|
326
|
+
value: number,
|
|
327
|
+
name: string,
|
|
328
|
+
min: number,
|
|
329
|
+
max: number
|
|
330
|
+
): number {
|
|
331
|
+
if (
|
|
332
|
+
typeof value !== 'number' ||
|
|
333
|
+
Number.isNaN(value) ||
|
|
334
|
+
!Number.isFinite(value)
|
|
335
|
+
) {
|
|
336
|
+
throw new Error(`${name} must be a valid finite number, got ${value}`);
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
if (value < min || value > max) {
|
|
340
|
+
throw new Error(
|
|
341
|
+
`${name} must be between ${min}-${max}ms, got ${value}ms`
|
|
342
|
+
);
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
return value;
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
/**
|
|
349
|
+
* Get default timeouts with env var fallbacks and validation
|
|
350
|
+
* Precedence: SDK defaults < Env vars < User config
|
|
351
|
+
*/
|
|
352
|
+
private getDefaultTimeouts(
|
|
353
|
+
env: Record<string, unknown>
|
|
354
|
+
): typeof this.DEFAULT_CONTAINER_TIMEOUTS {
|
|
355
|
+
const parseAndValidate = (
|
|
356
|
+
envVar: string | undefined,
|
|
357
|
+
name: keyof typeof this.DEFAULT_CONTAINER_TIMEOUTS,
|
|
358
|
+
min: number,
|
|
359
|
+
max: number
|
|
360
|
+
): number => {
|
|
361
|
+
const defaultValue = this.DEFAULT_CONTAINER_TIMEOUTS[name];
|
|
362
|
+
|
|
363
|
+
if (envVar === undefined) {
|
|
364
|
+
return defaultValue;
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
const parsed = parseInt(envVar, 10);
|
|
368
|
+
|
|
369
|
+
if (Number.isNaN(parsed)) {
|
|
370
|
+
this.logger.warn(
|
|
371
|
+
`Invalid ${name}: "${envVar}" is not a number. Using default: ${defaultValue}ms`
|
|
372
|
+
);
|
|
373
|
+
return defaultValue;
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
if (parsed < min || parsed > max) {
|
|
377
|
+
this.logger.warn(
|
|
378
|
+
`Invalid ${name}: ${parsed}ms. Must be ${min}-${max}ms. Using default: ${defaultValue}ms`
|
|
379
|
+
);
|
|
380
|
+
return defaultValue;
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
return parsed;
|
|
384
|
+
};
|
|
385
|
+
|
|
386
|
+
return {
|
|
387
|
+
instanceGetTimeoutMS: parseAndValidate(
|
|
388
|
+
getEnvString(env, 'SANDBOX_INSTANCE_TIMEOUT_MS'),
|
|
389
|
+
'instanceGetTimeoutMS',
|
|
390
|
+
5_000, // Min 5s
|
|
391
|
+
300_000 // Max 5min
|
|
392
|
+
),
|
|
393
|
+
portReadyTimeoutMS: parseAndValidate(
|
|
394
|
+
getEnvString(env, 'SANDBOX_PORT_TIMEOUT_MS'),
|
|
395
|
+
'portReadyTimeoutMS',
|
|
396
|
+
10_000, // Min 10s
|
|
397
|
+
600_000 // Max 10min
|
|
398
|
+
),
|
|
399
|
+
waitIntervalMS: parseAndValidate(
|
|
400
|
+
getEnvString(env, 'SANDBOX_POLL_INTERVAL_MS'),
|
|
401
|
+
'waitIntervalMS',
|
|
402
|
+
100, // Min 100ms
|
|
403
|
+
5_000 // Max 5s
|
|
404
|
+
)
|
|
405
|
+
};
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
/*
|
|
409
|
+
* Mount an S3-compatible bucket as a local directory using S3FS-FUSE
|
|
410
|
+
*
|
|
411
|
+
* Requires explicit endpoint URL. Credentials are auto-detected from environment
|
|
412
|
+
* variables or can be provided explicitly.
|
|
413
|
+
*
|
|
414
|
+
* @param bucket - Bucket name
|
|
415
|
+
* @param mountPath - Absolute path in container to mount at
|
|
416
|
+
* @param options - Configuration options with required endpoint
|
|
417
|
+
* @throws MissingCredentialsError if no credentials found in environment
|
|
418
|
+
* @throws S3FSMountError if S3FS mount command fails
|
|
419
|
+
* @throws InvalidMountConfigError if bucket name, mount path, or endpoint is invalid
|
|
420
|
+
*/
|
|
421
|
+
async mountBucket(
|
|
422
|
+
bucket: string,
|
|
423
|
+
mountPath: string,
|
|
424
|
+
options: MountBucketOptions
|
|
425
|
+
): Promise<void> {
|
|
426
|
+
this.logger.info(`Mounting bucket ${bucket} to ${mountPath}`);
|
|
427
|
+
|
|
428
|
+
// Validate options
|
|
429
|
+
this.validateMountOptions(bucket, mountPath, options);
|
|
430
|
+
|
|
431
|
+
// Detect provider from explicit option or URL pattern
|
|
432
|
+
const provider: BucketProvider | null =
|
|
433
|
+
options.provider || detectProviderFromUrl(options.endpoint);
|
|
434
|
+
|
|
435
|
+
this.logger.debug(`Detected provider: ${provider || 'unknown'}`, {
|
|
436
|
+
explicitProvider: options.provider
|
|
437
|
+
});
|
|
438
|
+
|
|
439
|
+
// Detect credentials
|
|
440
|
+
const credentials = detectCredentials(options, this.envVars);
|
|
441
|
+
|
|
442
|
+
// Generate unique password file path
|
|
443
|
+
const passwordFilePath = this.generatePasswordFilePath();
|
|
444
|
+
|
|
445
|
+
// Reserve mount path immediately to prevent race conditions
|
|
446
|
+
// (two concurrent mount calls would both pass validation otherwise)
|
|
447
|
+
this.activeMounts.set(mountPath, {
|
|
448
|
+
bucket,
|
|
449
|
+
mountPath,
|
|
450
|
+
endpoint: options.endpoint,
|
|
451
|
+
provider,
|
|
452
|
+
passwordFilePath,
|
|
453
|
+
mounted: false
|
|
454
|
+
});
|
|
455
|
+
|
|
456
|
+
try {
|
|
457
|
+
// Create password file with credentials
|
|
458
|
+
await this.createPasswordFile(passwordFilePath, bucket, credentials);
|
|
459
|
+
|
|
460
|
+
// Create mount directory
|
|
461
|
+
await this.exec(`mkdir -p ${shellEscape(mountPath)}`);
|
|
462
|
+
|
|
463
|
+
// Execute S3FS mount with password file
|
|
464
|
+
await this.executeS3FSMount(
|
|
465
|
+
bucket,
|
|
466
|
+
mountPath,
|
|
467
|
+
options,
|
|
468
|
+
provider,
|
|
469
|
+
passwordFilePath
|
|
470
|
+
);
|
|
471
|
+
|
|
472
|
+
// Mark as successfully mounted
|
|
473
|
+
this.activeMounts.set(mountPath, {
|
|
474
|
+
bucket,
|
|
475
|
+
mountPath,
|
|
476
|
+
endpoint: options.endpoint,
|
|
477
|
+
provider,
|
|
478
|
+
passwordFilePath,
|
|
479
|
+
mounted: true
|
|
480
|
+
});
|
|
481
|
+
|
|
482
|
+
this.logger.info(`Successfully mounted bucket ${bucket} to ${mountPath}`);
|
|
483
|
+
} catch (error) {
|
|
484
|
+
// Clean up password file on failure
|
|
485
|
+
await this.deletePasswordFile(passwordFilePath);
|
|
486
|
+
|
|
487
|
+
// Clean up reservation on failure
|
|
488
|
+
this.activeMounts.delete(mountPath);
|
|
489
|
+
throw error;
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
/**
|
|
494
|
+
* Manually unmount a bucket filesystem
|
|
495
|
+
*
|
|
496
|
+
* @param mountPath - Absolute path where the bucket is mounted
|
|
497
|
+
* @throws InvalidMountConfigError if mount path doesn't exist or isn't mounted
|
|
498
|
+
*/
|
|
499
|
+
async unmountBucket(mountPath: string): Promise<void> {
|
|
500
|
+
this.logger.info(`Unmounting bucket from ${mountPath}`);
|
|
501
|
+
|
|
502
|
+
// Look up mount by path
|
|
503
|
+
const mountInfo = this.activeMounts.get(mountPath);
|
|
504
|
+
|
|
505
|
+
// Throw error if mount doesn't exist
|
|
506
|
+
if (!mountInfo) {
|
|
507
|
+
throw new InvalidMountConfigError(
|
|
508
|
+
`No active mount found at path: ${mountPath}`
|
|
509
|
+
);
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
// Unmount the filesystem
|
|
513
|
+
try {
|
|
514
|
+
await this.exec(`fusermount -u ${shellEscape(mountPath)}`);
|
|
515
|
+
mountInfo.mounted = false;
|
|
516
|
+
|
|
517
|
+
// Only remove from tracking if unmount succeeded
|
|
518
|
+
this.activeMounts.delete(mountPath);
|
|
519
|
+
} finally {
|
|
520
|
+
// Always cleanup password file, even if unmount fails
|
|
521
|
+
await this.deletePasswordFile(mountInfo.passwordFilePath);
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
this.logger.info(`Successfully unmounted bucket from ${mountPath}`);
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
/**
|
|
528
|
+
* Validate mount options
|
|
529
|
+
*/
|
|
530
|
+
private validateMountOptions(
|
|
531
|
+
bucket: string,
|
|
532
|
+
mountPath: string,
|
|
533
|
+
options: MountBucketOptions
|
|
534
|
+
): void {
|
|
535
|
+
// Require endpoint field
|
|
536
|
+
if (!options.endpoint) {
|
|
537
|
+
throw new InvalidMountConfigError(
|
|
538
|
+
'Endpoint is required. Provide the full S3-compatible endpoint URL.'
|
|
539
|
+
);
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
// Basic URL validation
|
|
543
|
+
try {
|
|
544
|
+
new URL(options.endpoint);
|
|
545
|
+
} catch (error) {
|
|
546
|
+
throw new InvalidMountConfigError(
|
|
547
|
+
`Invalid endpoint URL: "${options.endpoint}". Must be a valid HTTP(S) URL.`
|
|
548
|
+
);
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
// Validate bucket name (S3-compatible naming rules)
|
|
552
|
+
const bucketNameRegex = /^[a-z0-9]([a-z0-9.-]{0,61}[a-z0-9])?$/;
|
|
553
|
+
if (!bucketNameRegex.test(bucket)) {
|
|
554
|
+
throw new InvalidMountConfigError(
|
|
555
|
+
`Invalid bucket name: "${bucket}". Bucket names must be 3-63 characters, ` +
|
|
556
|
+
`lowercase alphanumeric, dots, or hyphens, and cannot start/end with dots or hyphens.`
|
|
557
|
+
);
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
// Validate mount path is absolute
|
|
561
|
+
if (!mountPath.startsWith('/')) {
|
|
562
|
+
throw new InvalidMountConfigError(
|
|
563
|
+
`Mount path must be absolute (start with /): "${mountPath}"`
|
|
564
|
+
);
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
// Check for duplicate mount path
|
|
568
|
+
if (this.activeMounts.has(mountPath)) {
|
|
569
|
+
const existingMount = this.activeMounts.get(mountPath);
|
|
570
|
+
throw new InvalidMountConfigError(
|
|
571
|
+
`Mount path "${mountPath}" is already in use by bucket "${existingMount?.bucket}". ` +
|
|
572
|
+
`Unmount the existing bucket first or use a different mount path.`
|
|
573
|
+
);
|
|
574
|
+
}
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
/**
|
|
578
|
+
* Generate unique password file path for s3fs credentials
|
|
579
|
+
*/
|
|
580
|
+
private generatePasswordFilePath(): string {
|
|
581
|
+
const uuid = crypto.randomUUID();
|
|
582
|
+
return `/tmp/.passwd-s3fs-${uuid}`;
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
/**
|
|
586
|
+
* Create password file with s3fs credentials
|
|
587
|
+
* Format: bucket:accessKeyId:secretAccessKey
|
|
588
|
+
*/
|
|
589
|
+
private async createPasswordFile(
|
|
590
|
+
passwordFilePath: string,
|
|
591
|
+
bucket: string,
|
|
592
|
+
credentials: BucketCredentials
|
|
593
|
+
): Promise<void> {
|
|
594
|
+
const content = `${bucket}:${credentials.accessKeyId}:${credentials.secretAccessKey}`;
|
|
595
|
+
|
|
596
|
+
await this.writeFile(passwordFilePath, content);
|
|
597
|
+
|
|
598
|
+
await this.exec(`chmod 0600 ${shellEscape(passwordFilePath)}`);
|
|
599
|
+
|
|
600
|
+
this.logger.debug(`Created password file: ${passwordFilePath}`);
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
/**
|
|
604
|
+
* Delete password file
|
|
605
|
+
*/
|
|
606
|
+
private async deletePasswordFile(passwordFilePath: string): Promise<void> {
|
|
607
|
+
try {
|
|
608
|
+
await this.exec(`rm -f ${shellEscape(passwordFilePath)}`);
|
|
609
|
+
this.logger.debug(`Deleted password file: ${passwordFilePath}`);
|
|
610
|
+
} catch (error) {
|
|
611
|
+
this.logger.warn(`Failed to delete password file ${passwordFilePath}`, {
|
|
612
|
+
error: error instanceof Error ? error.message : String(error)
|
|
613
|
+
});
|
|
614
|
+
}
|
|
615
|
+
}
|
|
616
|
+
|
|
617
|
+
/**
|
|
618
|
+
* Execute S3FS mount command
|
|
619
|
+
*/
|
|
620
|
+
private async executeS3FSMount(
|
|
621
|
+
bucket: string,
|
|
622
|
+
mountPath: string,
|
|
623
|
+
options: MountBucketOptions,
|
|
624
|
+
provider: BucketProvider | null,
|
|
625
|
+
passwordFilePath: string
|
|
626
|
+
): Promise<void> {
|
|
627
|
+
// Resolve s3fs options (provider defaults + user overrides)
|
|
628
|
+
const resolvedOptions = resolveS3fsOptions(provider, options.s3fsOptions);
|
|
629
|
+
|
|
630
|
+
// Build s3fs mount command
|
|
631
|
+
const s3fsArgs: string[] = [];
|
|
632
|
+
|
|
633
|
+
// Add password file option FIRST
|
|
634
|
+
s3fsArgs.push(`passwd_file=${passwordFilePath}`);
|
|
635
|
+
|
|
636
|
+
// Add resolved provider-specific and user options
|
|
637
|
+
s3fsArgs.push(...resolvedOptions);
|
|
638
|
+
|
|
639
|
+
// Add read-only flag if requested
|
|
640
|
+
if (options.readOnly) {
|
|
641
|
+
s3fsArgs.push('ro');
|
|
642
|
+
}
|
|
643
|
+
|
|
644
|
+
// Add endpoint URL
|
|
645
|
+
s3fsArgs.push(`url=${options.endpoint}`);
|
|
646
|
+
|
|
647
|
+
// Build final command with escaped options
|
|
648
|
+
const optionsStr = shellEscape(s3fsArgs.join(','));
|
|
649
|
+
const mountCmd = `s3fs ${shellEscape(bucket)} ${shellEscape(mountPath)} -o ${optionsStr}`;
|
|
650
|
+
|
|
651
|
+
this.logger.debug('Executing s3fs mount', {
|
|
652
|
+
bucket,
|
|
653
|
+
mountPath,
|
|
654
|
+
provider,
|
|
655
|
+
resolvedOptions
|
|
656
|
+
});
|
|
657
|
+
|
|
658
|
+
// Execute mount command
|
|
659
|
+
const result = await this.exec(mountCmd);
|
|
660
|
+
|
|
661
|
+
if (result.exitCode !== 0) {
|
|
662
|
+
throw new S3FSMountError(
|
|
663
|
+
`S3FS mount failed: ${result.stderr || result.stdout || 'Unknown error'}`
|
|
664
|
+
);
|
|
665
|
+
}
|
|
666
|
+
|
|
667
|
+
this.logger.debug('Mount command executed successfully');
|
|
668
|
+
}
|
|
669
|
+
|
|
203
670
|
/**
|
|
204
671
|
* Cleanup and destroy the sandbox container
|
|
205
672
|
*/
|
|
206
673
|
override async destroy(): Promise<void> {
|
|
207
674
|
this.logger.info('Destroying sandbox container');
|
|
675
|
+
|
|
676
|
+
// Unmount all mounted buckets and cleanup password files
|
|
677
|
+
for (const [mountPath, mountInfo] of this.activeMounts.entries()) {
|
|
678
|
+
if (mountInfo.mounted) {
|
|
679
|
+
try {
|
|
680
|
+
this.logger.info(
|
|
681
|
+
`Unmounting bucket ${mountInfo.bucket} from ${mountPath}`
|
|
682
|
+
);
|
|
683
|
+
await this.exec(`fusermount -u ${shellEscape(mountPath)}`);
|
|
684
|
+
mountInfo.mounted = false;
|
|
685
|
+
} catch (error) {
|
|
686
|
+
const errorMsg =
|
|
687
|
+
error instanceof Error ? error.message : String(error);
|
|
688
|
+
this.logger.warn(
|
|
689
|
+
`Failed to unmount bucket ${mountInfo.bucket} from ${mountPath}: ${errorMsg}`
|
|
690
|
+
);
|
|
691
|
+
}
|
|
692
|
+
}
|
|
693
|
+
|
|
694
|
+
// Always cleanup password file
|
|
695
|
+
await this.deletePasswordFile(mountInfo.passwordFilePath);
|
|
696
|
+
}
|
|
697
|
+
|
|
208
698
|
await super.destroy();
|
|
209
699
|
}
|
|
210
700
|
|
|
@@ -278,6 +768,117 @@ export class Sandbox<Env = unknown> extends Container<Env> implements ISandbox {
|
|
|
278
768
|
);
|
|
279
769
|
}
|
|
280
770
|
|
|
771
|
+
/**
|
|
772
|
+
* Override Container.containerFetch to use production-friendly timeouts
|
|
773
|
+
* Automatically starts container with longer timeouts if not running
|
|
774
|
+
*/
|
|
775
|
+
override async containerFetch(
|
|
776
|
+
requestOrUrl: Request | string | URL,
|
|
777
|
+
portOrInit?: number | RequestInit,
|
|
778
|
+
portParam?: number
|
|
779
|
+
): Promise<Response> {
|
|
780
|
+
// Parse arguments to extract request and port
|
|
781
|
+
const { request, port } = this.parseContainerFetchArgs(
|
|
782
|
+
requestOrUrl,
|
|
783
|
+
portOrInit,
|
|
784
|
+
portParam
|
|
785
|
+
);
|
|
786
|
+
|
|
787
|
+
const state = await this.getState();
|
|
788
|
+
|
|
789
|
+
// If container not healthy, start it with production timeouts
|
|
790
|
+
if (state.status !== 'healthy') {
|
|
791
|
+
try {
|
|
792
|
+
this.logger.debug('Starting container with configured timeouts', {
|
|
793
|
+
instanceTimeout: this.containerTimeouts.instanceGetTimeoutMS,
|
|
794
|
+
portTimeout: this.containerTimeouts.portReadyTimeoutMS
|
|
795
|
+
});
|
|
796
|
+
|
|
797
|
+
await this.startAndWaitForPorts({
|
|
798
|
+
ports: port,
|
|
799
|
+
cancellationOptions: {
|
|
800
|
+
instanceGetTimeoutMS: this.containerTimeouts.instanceGetTimeoutMS,
|
|
801
|
+
portReadyTimeoutMS: this.containerTimeouts.portReadyTimeoutMS,
|
|
802
|
+
waitInterval: this.containerTimeouts.waitIntervalMS,
|
|
803
|
+
abort: request.signal
|
|
804
|
+
}
|
|
805
|
+
});
|
|
806
|
+
} catch (e) {
|
|
807
|
+
// Handle provisioning vs startup errors
|
|
808
|
+
if (this.isNoInstanceError(e)) {
|
|
809
|
+
return new Response(
|
|
810
|
+
'Container is currently provisioning. This can take several minutes on first deployment. Please retry in a moment.',
|
|
811
|
+
{
|
|
812
|
+
status: 503,
|
|
813
|
+
headers: { 'Retry-After': '10' } // Suggest 10s retry
|
|
814
|
+
}
|
|
815
|
+
);
|
|
816
|
+
}
|
|
817
|
+
|
|
818
|
+
// Other startup errors
|
|
819
|
+
this.logger.error(
|
|
820
|
+
'Container startup failed',
|
|
821
|
+
e instanceof Error ? e : new Error(String(e))
|
|
822
|
+
);
|
|
823
|
+
return new Response(
|
|
824
|
+
`Failed to start container: ${e instanceof Error ? e.message : String(e)}`,
|
|
825
|
+
{ status: 500 }
|
|
826
|
+
);
|
|
827
|
+
}
|
|
828
|
+
}
|
|
829
|
+
|
|
830
|
+
// Delegate to parent for the actual fetch (handles TCP port access internally)
|
|
831
|
+
return await super.containerFetch(requestOrUrl, portOrInit, portParam);
|
|
832
|
+
}
|
|
833
|
+
|
|
834
|
+
/**
|
|
835
|
+
* Helper: Check if error is "no container instance available"
|
|
836
|
+
*/
|
|
837
|
+
private isNoInstanceError(error: unknown): boolean {
|
|
838
|
+
return (
|
|
839
|
+
error instanceof Error &&
|
|
840
|
+
error.message.toLowerCase().includes('no container instance')
|
|
841
|
+
);
|
|
842
|
+
}
|
|
843
|
+
|
|
844
|
+
/**
|
|
845
|
+
* Helper: Parse containerFetch arguments (supports multiple signatures)
|
|
846
|
+
*/
|
|
847
|
+
private parseContainerFetchArgs(
|
|
848
|
+
requestOrUrl: Request | string | URL,
|
|
849
|
+
portOrInit?: number | RequestInit,
|
|
850
|
+
portParam?: number
|
|
851
|
+
): { request: Request; port: number } {
|
|
852
|
+
let request: Request;
|
|
853
|
+
let port: number | undefined;
|
|
854
|
+
|
|
855
|
+
if (requestOrUrl instanceof Request) {
|
|
856
|
+
request = requestOrUrl;
|
|
857
|
+
port = typeof portOrInit === 'number' ? portOrInit : undefined;
|
|
858
|
+
} else {
|
|
859
|
+
const url =
|
|
860
|
+
typeof requestOrUrl === 'string'
|
|
861
|
+
? requestOrUrl
|
|
862
|
+
: requestOrUrl.toString();
|
|
863
|
+
const init = typeof portOrInit === 'number' ? {} : portOrInit || {};
|
|
864
|
+
port =
|
|
865
|
+
typeof portOrInit === 'number'
|
|
866
|
+
? portOrInit
|
|
867
|
+
: typeof portParam === 'number'
|
|
868
|
+
? portParam
|
|
869
|
+
: undefined;
|
|
870
|
+
request = new Request(url, init);
|
|
871
|
+
}
|
|
872
|
+
|
|
873
|
+
port ??= this.defaultPort;
|
|
874
|
+
|
|
875
|
+
if (port === undefined) {
|
|
876
|
+
throw new Error('No port specified for container fetch');
|
|
877
|
+
}
|
|
878
|
+
|
|
879
|
+
return { request, port };
|
|
880
|
+
}
|
|
881
|
+
|
|
281
882
|
/**
|
|
282
883
|
* Override onActivityExpired to prevent automatic shutdown when keepAlive is enabled
|
|
283
884
|
* When keepAlive is disabled, calls parent implementation which stops the container
|
|
@@ -389,9 +990,12 @@ export class Sandbox<Env = unknown> extends Container<Env> implements ISandbox {
|
|
|
389
990
|
// Persist to storage so it survives hot reloads
|
|
390
991
|
await this.ctx.storage.put('defaultSession', sessionId);
|
|
391
992
|
this.logger.debug('Default session initialized', { sessionId });
|
|
392
|
-
} catch (error:
|
|
993
|
+
} catch (error: unknown) {
|
|
393
994
|
// If session already exists (e.g., after hot reload), reuse it
|
|
394
|
-
if (
|
|
995
|
+
if (
|
|
996
|
+
error instanceof Error &&
|
|
997
|
+
error.message.includes('already exists')
|
|
998
|
+
) {
|
|
395
999
|
this.logger.debug('Reusing existing session after reload', {
|
|
396
1000
|
sessionId
|
|
397
1001
|
});
|
|
@@ -1031,20 +1635,29 @@ export class Sandbox<Env = unknown> extends Container<Env> implements ISandbox {
|
|
|
1031
1635
|
);
|
|
1032
1636
|
}
|
|
1033
1637
|
|
|
1034
|
-
//
|
|
1035
|
-
const
|
|
1638
|
+
// Hostnames are case-insensitive, routing requests to wrong DO instance when keys contain uppercase letters
|
|
1639
|
+
const effectiveId = this.sandboxName || sandboxId;
|
|
1640
|
+
const hasUppercase = /[A-Z]/.test(effectiveId);
|
|
1641
|
+
if (!this.normalizeId && hasUppercase) {
|
|
1642
|
+
throw new SecurityError(
|
|
1643
|
+
`Preview URLs require lowercase sandbox IDs. Your ID "${effectiveId}" contains uppercase letters.\n\n` +
|
|
1644
|
+
`To fix this:\n` +
|
|
1645
|
+
`1. Create a new sandbox with: getSandbox(ns, "${effectiveId}", { normalizeId: true })\n` +
|
|
1646
|
+
`2. This will create a sandbox with ID: "${effectiveId.toLowerCase()}"\n\n` +
|
|
1647
|
+
`Note: Due to DNS case-insensitivity, IDs with uppercase letters cannot be used with preview URLs.`
|
|
1648
|
+
);
|
|
1649
|
+
}
|
|
1650
|
+
|
|
1651
|
+
const sanitizedSandboxId = sanitizeSandboxId(sandboxId).toLowerCase();
|
|
1036
1652
|
|
|
1037
1653
|
const isLocalhost = isLocalhostPattern(hostname);
|
|
1038
1654
|
|
|
1039
1655
|
if (isLocalhost) {
|
|
1040
|
-
// Unified subdomain approach for localhost (RFC 6761)
|
|
1041
1656
|
const [host, portStr] = hostname.split(':');
|
|
1042
1657
|
const mainPort = portStr || '80';
|
|
1043
1658
|
|
|
1044
|
-
// Use URL constructor for safe URL building
|
|
1045
1659
|
try {
|
|
1046
1660
|
const baseUrl = new URL(`http://${host}:${mainPort}`);
|
|
1047
|
-
// Construct subdomain safely with mandatory token
|
|
1048
1661
|
const subdomainHost = `${port}-${sanitizedSandboxId}-${token}.${host}`;
|
|
1049
1662
|
baseUrl.hostname = subdomainHost;
|
|
1050
1663
|
|
|
@@ -1058,13 +1671,8 @@ export class Sandbox<Env = unknown> extends Container<Env> implements ISandbox {
|
|
|
1058
1671
|
}
|
|
1059
1672
|
}
|
|
1060
1673
|
|
|
1061
|
-
// Production subdomain logic - enforce HTTPS
|
|
1062
1674
|
try {
|
|
1063
|
-
|
|
1064
|
-
const protocol = 'https';
|
|
1065
|
-
const baseUrl = new URL(`${protocol}://${hostname}`);
|
|
1066
|
-
|
|
1067
|
-
// Construct subdomain safely with mandatory token
|
|
1675
|
+
const baseUrl = new URL(`https://${hostname}`);
|
|
1068
1676
|
const subdomainHost = `${port}-${sanitizedSandboxId}-${token}.${hostname}`;
|
|
1069
1677
|
baseUrl.hostname = subdomainHost;
|
|
1070
1678
|
|
|
@@ -1229,7 +1837,12 @@ export class Sandbox<Env = unknown> extends Container<Env> implements ISandbox {
|
|
|
1229
1837
|
this.codeInterpreter.runCodeStream(code, options),
|
|
1230
1838
|
listCodeContexts: () => this.codeInterpreter.listCodeContexts(),
|
|
1231
1839
|
deleteCodeContext: (contextId) =>
|
|
1232
|
-
this.codeInterpreter.deleteCodeContext(contextId)
|
|
1840
|
+
this.codeInterpreter.deleteCodeContext(contextId),
|
|
1841
|
+
|
|
1842
|
+
// Bucket mounting - sandbox-level operations
|
|
1843
|
+
mountBucket: (bucket, mountPath, options) =>
|
|
1844
|
+
this.mountBucket(bucket, mountPath, options),
|
|
1845
|
+
unmountBucket: (mountPath) => this.unmountBucket(mountPath)
|
|
1233
1846
|
};
|
|
1234
1847
|
}
|
|
1235
1848
|
|