@cloudflare/sandbox 0.0.0-dcf36ef → 0.0.0-e489cbb

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 (96) hide show
  1. package/CHANGELOG.md +8 -10
  2. package/Dockerfile +82 -18
  3. package/README.md +89 -824
  4. package/dist/chunk-53JFOF7F.js +2352 -0
  5. package/dist/chunk-53JFOF7F.js.map +1 -0
  6. package/dist/chunk-BFVUNTP4.js +104 -0
  7. package/dist/chunk-BFVUNTP4.js.map +1 -0
  8. package/dist/chunk-EKSWCBCA.js +86 -0
  9. package/dist/chunk-EKSWCBCA.js.map +1 -0
  10. package/dist/chunk-JXZMAU2C.js +559 -0
  11. package/dist/chunk-JXZMAU2C.js.map +1 -0
  12. package/dist/chunk-Z532A7QC.js +78 -0
  13. package/dist/chunk-Z532A7QC.js.map +1 -0
  14. package/dist/file-stream.d.ts +43 -0
  15. package/dist/file-stream.js +9 -0
  16. package/dist/file-stream.js.map +1 -0
  17. package/dist/index.d.ts +9 -0
  18. package/dist/index.js +66 -0
  19. package/dist/index.js.map +1 -0
  20. package/dist/interpreter.d.ts +33 -0
  21. package/dist/interpreter.js +8 -0
  22. package/dist/interpreter.js.map +1 -0
  23. package/dist/request-handler.d.ts +18 -0
  24. package/dist/request-handler.js +12 -0
  25. package/dist/request-handler.js.map +1 -0
  26. package/dist/sandbox-D9K2ypln.d.ts +583 -0
  27. package/dist/sandbox.d.ts +4 -0
  28. package/dist/sandbox.js +12 -0
  29. package/dist/sandbox.js.map +1 -0
  30. package/dist/security.d.ts +31 -0
  31. package/dist/security.js +13 -0
  32. package/dist/security.js.map +1 -0
  33. package/dist/sse-parser.d.ts +28 -0
  34. package/dist/sse-parser.js +11 -0
  35. package/dist/sse-parser.js.map +1 -0
  36. package/package.json +12 -4
  37. package/src/clients/base-client.ts +280 -0
  38. package/src/clients/command-client.ts +115 -0
  39. package/src/clients/file-client.ts +269 -0
  40. package/src/clients/git-client.ts +92 -0
  41. package/src/clients/index.ts +63 -0
  42. package/src/{interpreter-client.ts → clients/interpreter-client.ts} +148 -171
  43. package/src/clients/port-client.ts +105 -0
  44. package/src/clients/process-client.ts +177 -0
  45. package/src/clients/sandbox-client.ts +41 -0
  46. package/src/clients/types.ts +84 -0
  47. package/src/clients/utility-client.ts +94 -0
  48. package/src/errors/adapter.ts +180 -0
  49. package/src/errors/classes.ts +469 -0
  50. package/src/errors/index.ts +105 -0
  51. package/src/file-stream.ts +119 -117
  52. package/src/index.ts +81 -69
  53. package/src/interpreter.ts +17 -8
  54. package/src/request-handler.ts +69 -43
  55. package/src/sandbox.ts +694 -533
  56. package/src/security.ts +14 -23
  57. package/src/sse-parser.ts +4 -8
  58. package/startup.sh +3 -0
  59. package/tests/base-client.test.ts +328 -0
  60. package/tests/command-client.test.ts +407 -0
  61. package/tests/file-client.test.ts +643 -0
  62. package/tests/file-stream.test.ts +306 -0
  63. package/tests/git-client.test.ts +328 -0
  64. package/tests/port-client.test.ts +301 -0
  65. package/tests/process-client.test.ts +658 -0
  66. package/tests/sandbox.test.ts +465 -0
  67. package/tests/sse-parser.test.ts +290 -0
  68. package/tests/utility-client.test.ts +266 -0
  69. package/tests/wrangler.jsonc +35 -0
  70. package/tsconfig.json +9 -1
  71. package/vitest.config.ts +31 -0
  72. package/container_src/bun.lock +0 -76
  73. package/container_src/circuit-breaker.ts +0 -121
  74. package/container_src/control-process.ts +0 -784
  75. package/container_src/handler/exec.ts +0 -185
  76. package/container_src/handler/file.ts +0 -457
  77. package/container_src/handler/git.ts +0 -130
  78. package/container_src/handler/ports.ts +0 -314
  79. package/container_src/handler/process.ts +0 -568
  80. package/container_src/handler/session.ts +0 -92
  81. package/container_src/index.ts +0 -601
  82. package/container_src/interpreter-service.ts +0 -276
  83. package/container_src/isolation.ts +0 -1213
  84. package/container_src/mime-processor.ts +0 -255
  85. package/container_src/package.json +0 -18
  86. package/container_src/runtime/executors/javascript/node_executor.ts +0 -123
  87. package/container_src/runtime/executors/python/ipython_executor.py +0 -338
  88. package/container_src/runtime/executors/typescript/ts_executor.ts +0 -138
  89. package/container_src/runtime/process-pool.ts +0 -464
  90. package/container_src/shell-escape.ts +0 -42
  91. package/container_src/startup.sh +0 -11
  92. package/container_src/types.ts +0 -131
  93. package/src/client.ts +0 -1048
  94. package/src/errors.ts +0 -219
  95. package/src/interpreter-types.ts +0 -390
  96. package/src/types.ts +0 -571
@@ -1,6 +1,6 @@
1
+ import { createLogger, type LogContext, TraceContext } from "@repo/shared";
1
2
  import { getSandbox, type Sandbox } from "./sandbox";
2
3
  import {
3
- logSecurityEvent,
4
4
  sanitizeSandboxId,
5
5
  validatePort
6
6
  } from "./security";
@@ -13,12 +13,21 @@ export interface RouteInfo {
13
13
  port: number;
14
14
  sandboxId: string;
15
15
  path: string;
16
+ token: string;
16
17
  }
17
18
 
18
19
  export async function proxyToSandbox<E extends SandboxEnv>(
19
20
  request: Request,
20
21
  env: E
21
22
  ): Promise<Response | null> {
23
+ // Create logger context for this request
24
+ const traceId = TraceContext.fromHeaders(request.headers) || TraceContext.generate();
25
+ const logger = createLogger({
26
+ component: 'sandbox-do',
27
+ traceId,
28
+ operation: 'proxy'
29
+ });
30
+
22
31
  try {
23
32
  const url = new URL(request.url);
24
33
  const routeInfo = extractSandboxRoute(url);
@@ -27,9 +36,40 @@ export async function proxyToSandbox<E extends SandboxEnv>(
27
36
  return null; // Not a request to an exposed container port
28
37
  }
29
38
 
30
- const { sandboxId, port, path } = routeInfo;
39
+ const { sandboxId, port, path, token } = routeInfo;
31
40
  const sandbox = getSandbox(env.Sandbox, sandboxId);
32
41
 
42
+ // Critical security check: Validate token (mandatory for all user ports)
43
+ // Skip check for control plane port 3000
44
+ if (port !== 3000) {
45
+ // Validate the token matches the port
46
+ const isValidToken = await sandbox.validatePortToken(port, token);
47
+ if (!isValidToken) {
48
+ logger.warn('Invalid token access blocked', {
49
+ port,
50
+ sandboxId,
51
+ path,
52
+ hostname: url.hostname,
53
+ url: request.url,
54
+ method: request.method,
55
+ userAgent: request.headers.get('User-Agent') || 'unknown'
56
+ });
57
+
58
+ return new Response(
59
+ JSON.stringify({
60
+ error: `Access denied: Invalid token or port not exposed`,
61
+ code: 'INVALID_TOKEN'
62
+ }),
63
+ {
64
+ status: 404,
65
+ headers: {
66
+ 'Content-Type': 'application/json'
67
+ }
68
+ }
69
+ );
70
+ }
71
+ }
72
+
33
73
  // Build proxy request with proper headers
34
74
  let proxyUrl: string;
35
75
 
@@ -52,43 +92,32 @@ export async function proxyToSandbox<E extends SandboxEnv>(
52
92
  'X-Sandbox-Name': sandboxId, // Pass the friendly name
53
93
  },
54
94
  body: request.body,
95
+ // @ts-expect-error - duplex required for body streaming in modern runtimes
96
+ duplex: 'half',
55
97
  });
56
98
 
57
99
  return sandbox.containerFetch(proxyRequest, port);
58
100
  } catch (error) {
59
- console.error('[Sandbox] Proxy routing error:', error);
101
+ logger.error('Proxy routing error', error instanceof Error ? error : new Error(String(error)));
60
102
  return new Response('Proxy routing error', { status: 500 });
61
103
  }
62
104
  }
63
105
 
64
106
  function extractSandboxRoute(url: URL): RouteInfo | null {
65
- // Parse subdomain pattern: port-sandboxId.domain
66
- const subdomainMatch = url.hostname.match(/^(\d{4,5})-([^.-][^.]*[^.-]|[^.-])\.(.+)$/);
107
+ // Parse subdomain pattern: port-sandboxId-token.domain (tokens mandatory)
108
+ const subdomainMatch = url.hostname.match(/^(\d{4,5})-([^.-][^.]*[^.-]|[^.-])-([a-zA-Z0-9_-]{12,20})\.(.+)$/);
67
109
 
68
110
  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
111
  return null;
77
112
  }
78
113
 
79
114
  const portStr = subdomainMatch[1];
80
115
  const sandboxId = subdomainMatch[2];
81
- const domain = subdomainMatch[3];
116
+ const token = subdomainMatch[3]; // Mandatory token
117
+ const domain = subdomainMatch[4];
82
118
 
83
119
  const port = parseInt(portStr, 10);
84
120
  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
121
  return null;
93
122
  }
94
123
 
@@ -96,49 +125,46 @@ function extractSandboxRoute(url: URL): RouteInfo | null {
96
125
  try {
97
126
  sanitizedSandboxId = sanitizeSandboxId(sandboxId);
98
127
  } 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
128
  return null;
107
129
  }
108
130
 
109
131
  // DNS subdomain length limit is 63 characters
110
132
  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
133
  return null;
118
134
  }
119
135
 
120
- logSecurityEvent('SANDBOX_ROUTE_EXTRACTED', {
121
- port,
122
- sandboxId: sanitizedSandboxId,
123
- domain,
124
- path: url.pathname || "/",
125
- hostname: url.hostname
126
- }, 'low');
127
-
128
136
  return {
129
137
  port,
130
138
  sandboxId: sanitizedSandboxId,
131
139
  path: url.pathname || "/",
140
+ token,
132
141
  };
133
142
  }
134
143
 
135
144
  export function isLocalhostPattern(hostname: string): boolean {
145
+ // Handle IPv6 addresses in brackets (with or without port)
146
+ if (hostname.startsWith('[')) {
147
+ if (hostname.includes(']:')) {
148
+ // [::1]:port format
149
+ const ipv6Part = hostname.substring(0, hostname.indexOf(']:') + 1);
150
+ return ipv6Part === '[::1]';
151
+ } else {
152
+ // [::1] format without port
153
+ return hostname === '[::1]';
154
+ }
155
+ }
156
+
157
+ // Handle bare IPv6 without brackets
158
+ if (hostname === '::1') {
159
+ return true;
160
+ }
161
+
162
+ // For IPv4 and regular hostnames, split on colon to remove port
136
163
  const hostPart = hostname.split(":")[0];
164
+
137
165
  return (
138
166
  hostPart === "localhost" ||
139
167
  hostPart === "127.0.0.1" ||
140
- hostPart === "::1" ||
141
- hostPart === "[::1]" ||
142
168
  hostPart === "0.0.0.0"
143
169
  );
144
170
  }