@cloudflare/sandbox 0.0.8 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (56) hide show
  1. package/CHANGELOG.md +16 -0
  2. package/Dockerfile +73 -9
  3. package/container_src/handler/exec.ts +337 -0
  4. package/container_src/handler/file.ts +844 -0
  5. package/container_src/handler/git.ts +182 -0
  6. package/container_src/handler/ports.ts +314 -0
  7. package/container_src/handler/process.ts +640 -0
  8. package/container_src/index.ts +102 -2647
  9. package/container_src/types.ts +103 -0
  10. package/dist/chunk-6THNBO4S.js +46 -0
  11. package/dist/chunk-6THNBO4S.js.map +1 -0
  12. package/dist/chunk-6UAWTJ5S.js +85 -0
  13. package/dist/chunk-6UAWTJ5S.js.map +1 -0
  14. package/dist/chunk-G4XT4SP7.js +638 -0
  15. package/dist/chunk-G4XT4SP7.js.map +1 -0
  16. package/dist/chunk-ISFOIYQC.js +585 -0
  17. package/dist/chunk-ISFOIYQC.js.map +1 -0
  18. package/dist/chunk-NNGBXDMY.js +89 -0
  19. package/dist/chunk-NNGBXDMY.js.map +1 -0
  20. package/dist/client-Da-mLX4p.d.ts +210 -0
  21. package/dist/client.d.ts +2 -1
  22. package/dist/client.js +3 -37
  23. package/dist/index.d.ts +5 -200
  24. package/dist/index.js +17 -106
  25. package/dist/index.js.map +1 -1
  26. package/dist/request-handler.d.ts +16 -0
  27. package/dist/request-handler.js +12 -0
  28. package/dist/request-handler.js.map +1 -0
  29. package/dist/sandbox.d.ts +3 -0
  30. package/dist/sandbox.js +12 -0
  31. package/dist/sandbox.js.map +1 -0
  32. package/dist/security.d.ts +30 -0
  33. package/dist/security.js +13 -0
  34. package/dist/security.js.map +1 -0
  35. package/dist/sse-parser.d.ts +28 -0
  36. package/dist/sse-parser.js +11 -0
  37. package/dist/sse-parser.js.map +1 -0
  38. package/dist/types.d.ts +284 -0
  39. package/dist/types.js +19 -0
  40. package/dist/types.js.map +1 -0
  41. package/package.json +2 -7
  42. package/src/client.ts +320 -1242
  43. package/src/index.ts +20 -136
  44. package/src/request-handler.ts +144 -0
  45. package/src/sandbox.ts +645 -0
  46. package/src/security.ts +113 -0
  47. package/src/sse-parser.ts +147 -0
  48. package/src/types.ts +386 -0
  49. package/README.md +0 -65
  50. package/dist/chunk-7WZJ3TRE.js +0 -1364
  51. package/dist/chunk-7WZJ3TRE.js.map +0 -1
  52. package/tests/client.example.ts +0 -308
  53. package/tests/connection-test.ts +0 -81
  54. package/tests/simple-test.ts +0 -81
  55. package/tests/test1.ts +0 -281
  56. package/tests/test2.ts +0 -929
package/src/index.ts CHANGED
@@ -1,136 +1,20 @@
1
- import { Container, getContainer } from "@cloudflare/containers";
2
- import { HttpClient } from "./client";
3
-
4
- export function getSandbox(ns: DurableObjectNamespace<Sandbox>, id: string) {
5
- return getContainer(ns, id);
6
- }
7
-
8
- export class Sandbox<Env = unknown> extends Container<Env> {
9
- defaultPort = 3000; // The default port for the container to listen on
10
- sleepAfter = "3m"; // Sleep the sandbox if no requests are made in this timeframe
11
- client: HttpClient;
12
-
13
- constructor(ctx: DurableObjectState, env: Env) {
14
- super(ctx, env);
15
- this.client = new HttpClient({
16
- onCommandComplete: (success, exitCode, stdout, stderr, command, args) => {
17
- console.log(
18
- `[Container] Command completed: ${command}, Success: ${success}, Exit code: ${exitCode}`
19
- );
20
- },
21
- onCommandStart: (command, args) => {
22
- console.log(
23
- `[Container] Command started: ${command} ${args.join(" ")}`
24
- );
25
- },
26
- onError: (error, command, args) => {
27
- console.error(`[Container] Command error: ${error}`);
28
- },
29
- onOutput: (stream, data, command) => {
30
- console.log(`[Container] [${stream}] ${data}`);
31
- },
32
- port: this.defaultPort,
33
- stub: this,
34
- });
35
- }
36
-
37
- envVars = {
38
- MESSAGE: "I was passed in via the Sandbox class!",
39
- };
40
-
41
- override onStart() {
42
- console.log("Sandbox successfully started");
43
- }
44
-
45
- override onStop() {
46
- console.log("Sandbox successfully shut down");
47
- if (this.client) {
48
- this.client.clearSession();
49
- }
50
- }
51
-
52
- override onError(error: unknown) {
53
- console.log("Sandbox error:", error);
54
- }
55
-
56
- async exec(command: string, args: string[], options?: { stream?: boolean }) {
57
- if (options?.stream) {
58
- return this.client.executeStream(command, args);
59
- }
60
- return this.client.execute(command, args);
61
- }
62
-
63
- async gitCheckout(
64
- repoUrl: string,
65
- options: { branch?: string; targetDir?: string; stream?: boolean }
66
- ) {
67
- if (options?.stream) {
68
- return this.client.gitCheckoutStream(
69
- repoUrl,
70
- options.branch,
71
- options.targetDir
72
- );
73
- }
74
- return this.client.gitCheckout(repoUrl, options.branch, options.targetDir);
75
- }
76
-
77
- async mkdir(
78
- path: string,
79
- options: { recursive?: boolean; stream?: boolean } = {}
80
- ) {
81
- if (options?.stream) {
82
- return this.client.mkdirStream(path, options.recursive);
83
- }
84
- return this.client.mkdir(path, options.recursive);
85
- }
86
-
87
- async writeFile(
88
- path: string,
89
- content: string,
90
- options: { encoding?: string; stream?: boolean } = {}
91
- ) {
92
- if (options?.stream) {
93
- return this.client.writeFileStream(path, content, options.encoding);
94
- }
95
- return this.client.writeFile(path, content, options.encoding);
96
- }
97
-
98
- async deleteFile(path: string, options: { stream?: boolean } = {}) {
99
- if (options?.stream) {
100
- return this.client.deleteFileStream(path);
101
- }
102
- return this.client.deleteFile(path);
103
- }
104
-
105
- async renameFile(
106
- oldPath: string,
107
- newPath: string,
108
- options: { stream?: boolean } = {}
109
- ) {
110
- if (options?.stream) {
111
- return this.client.renameFileStream(oldPath, newPath);
112
- }
113
- return this.client.renameFile(oldPath, newPath);
114
- }
115
-
116
- async moveFile(
117
- sourcePath: string,
118
- destinationPath: string,
119
- options: { stream?: boolean } = {}
120
- ) {
121
- if (options?.stream) {
122
- return this.client.moveFileStream(sourcePath, destinationPath);
123
- }
124
- return this.client.moveFile(sourcePath, destinationPath);
125
- }
126
-
127
- async readFile(
128
- path: string,
129
- options: { encoding?: string; stream?: boolean } = {}
130
- ) {
131
- if (options?.stream) {
132
- return this.client.readFileStream(path, options.encoding);
133
- }
134
- return this.client.readFile(path, options.encoding);
135
- }
136
- }
1
+ // Export types from client
2
+ export type {
3
+ DeleteFileResponse, ExecuteResponse,
4
+ GitCheckoutResponse,
5
+ MkdirResponse, MoveFileResponse,
6
+ ReadFileResponse, RenameFileResponse, WriteFileResponse
7
+ } from "./client";
8
+
9
+ // Re-export request handler utilities
10
+ export {
11
+ proxyToSandbox, type RouteInfo, type SandboxEnv
12
+ } from './request-handler';
13
+
14
+ export { getSandbox, Sandbox } from "./sandbox";
15
+
16
+ // Export SSE parser for converting ReadableStream to AsyncIterable
17
+ export { asyncIterableToSSEStream, parseSSEStream, responseToAsyncIterable } from "./sse-parser";
18
+
19
+ // Export event types for streaming
20
+ export type { ExecEvent, LogEvent } from "./types";
@@ -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
+ }