@cloudflare/sandbox 0.0.0-722aec2 → 0.0.0-75de276
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 +302 -0
- package/Dockerfile +183 -9
- package/README.md +154 -50
- package/dist/index.d.ts +1953 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +3280 -0
- package/dist/index.js.map +1 -0
- package/package.json +16 -12
- package/src/clients/base-client.ts +295 -0
- package/src/clients/command-client.ts +115 -0
- package/src/clients/file-client.ts +300 -0
- package/src/clients/git-client.ts +98 -0
- package/src/clients/index.ts +64 -0
- package/src/clients/interpreter-client.ts +333 -0
- package/src/clients/port-client.ts +105 -0
- package/src/clients/process-client.ts +180 -0
- package/src/clients/sandbox-client.ts +39 -0
- package/src/clients/types.ts +88 -0
- package/src/clients/utility-client.ts +156 -0
- package/src/errors/adapter.ts +238 -0
- package/src/errors/classes.ts +594 -0
- package/src/errors/index.ts +109 -0
- package/src/file-stream.ts +169 -0
- package/src/index.ts +95 -127
- package/src/interpreter.ts +168 -0
- package/src/request-handler.ts +183 -0
- package/src/sandbox.ts +1268 -0
- package/src/security.ts +119 -0
- package/src/sse-parser.ts +144 -0
- package/src/version.ts +6 -0
- package/startup.sh +3 -0
- package/tests/base-client.test.ts +364 -0
- package/tests/command-client.test.ts +444 -0
- package/tests/file-client.test.ts +831 -0
- package/tests/file-stream.test.ts +310 -0
- package/tests/get-sandbox.test.ts +149 -0
- package/tests/git-client.test.ts +487 -0
- package/tests/port-client.test.ts +293 -0
- package/tests/process-client.test.ts +683 -0
- package/tests/request-handler.test.ts +292 -0
- package/tests/sandbox.test.ts +739 -0
- package/tests/sse-parser.test.ts +291 -0
- package/tests/utility-client.test.ts +339 -0
- package/tests/version.test.ts +16 -0
- package/tests/wrangler.jsonc +35 -0
- package/tsconfig.json +9 -1
- package/tsdown.config.ts +12 -0
- package/vitest.config.ts +31 -0
- package/container_src/index.ts +0 -2906
- package/container_src/package.json +0 -9
- package/src/client.ts +0 -1960
- package/tests/client.example.ts +0 -308
- package/tests/connection-test.ts +0 -81
- package/tests/simple-test.ts +0 -81
- package/tests/test1.ts +0 -281
- package/tests/test2.ts +0 -929
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
import { switchPort } from '@cloudflare/containers';
|
|
2
|
+
import { createLogger, type LogContext, TraceContext } from '@repo/shared';
|
|
3
|
+
import { getSandbox, type Sandbox } from './sandbox';
|
|
4
|
+
import { sanitizeSandboxId, validatePort } from './security';
|
|
5
|
+
|
|
6
|
+
export interface SandboxEnv {
|
|
7
|
+
Sandbox: DurableObjectNamespace<Sandbox>;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export interface RouteInfo {
|
|
11
|
+
port: number;
|
|
12
|
+
sandboxId: string;
|
|
13
|
+
path: string;
|
|
14
|
+
token: string;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export async function proxyToSandbox<E extends SandboxEnv>(
|
|
18
|
+
request: Request,
|
|
19
|
+
env: E
|
|
20
|
+
): Promise<Response | null> {
|
|
21
|
+
// Create logger context for this request
|
|
22
|
+
const traceId =
|
|
23
|
+
TraceContext.fromHeaders(request.headers) || TraceContext.generate();
|
|
24
|
+
const logger = createLogger({
|
|
25
|
+
component: 'sandbox-do',
|
|
26
|
+
traceId,
|
|
27
|
+
operation: 'proxy'
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
try {
|
|
31
|
+
const url = new URL(request.url);
|
|
32
|
+
const routeInfo = extractSandboxRoute(url);
|
|
33
|
+
|
|
34
|
+
if (!routeInfo) {
|
|
35
|
+
return null; // Not a request to an exposed container port
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const { sandboxId, port, path, token } = routeInfo;
|
|
39
|
+
const sandbox = getSandbox(env.Sandbox, sandboxId);
|
|
40
|
+
|
|
41
|
+
// Critical security check: Validate token (mandatory for all user ports)
|
|
42
|
+
// Skip check for control plane port 3000
|
|
43
|
+
if (port !== 3000) {
|
|
44
|
+
// Validate the token matches the port
|
|
45
|
+
const isValidToken = await sandbox.validatePortToken(port, token);
|
|
46
|
+
if (!isValidToken) {
|
|
47
|
+
logger.warn('Invalid token access blocked', {
|
|
48
|
+
port,
|
|
49
|
+
sandboxId,
|
|
50
|
+
path,
|
|
51
|
+
hostname: url.hostname,
|
|
52
|
+
url: request.url,
|
|
53
|
+
method: request.method,
|
|
54
|
+
userAgent: request.headers.get('User-Agent') || 'unknown'
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
return new Response(
|
|
58
|
+
JSON.stringify({
|
|
59
|
+
error: `Access denied: Invalid token or port not exposed`,
|
|
60
|
+
code: 'INVALID_TOKEN'
|
|
61
|
+
}),
|
|
62
|
+
{
|
|
63
|
+
status: 404,
|
|
64
|
+
headers: {
|
|
65
|
+
'Content-Type': 'application/json'
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// Detect WebSocket upgrade request
|
|
73
|
+
const upgradeHeader = request.headers.get('Upgrade');
|
|
74
|
+
if (upgradeHeader?.toLowerCase() === 'websocket') {
|
|
75
|
+
// WebSocket path: Must use fetch() not containerFetch()
|
|
76
|
+
// This bypasses JSRPC serialization boundary which cannot handle WebSocket upgrades
|
|
77
|
+
return await sandbox.fetch(switchPort(request, port));
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// Build proxy request with proper headers
|
|
81
|
+
let proxyUrl: string;
|
|
82
|
+
|
|
83
|
+
// Route based on the target port
|
|
84
|
+
if (port !== 3000) {
|
|
85
|
+
// Route directly to user's service on the specified port
|
|
86
|
+
proxyUrl = `http://localhost:${port}${path}${url.search}`;
|
|
87
|
+
} else {
|
|
88
|
+
// Port 3000 is our control plane - route normally
|
|
89
|
+
proxyUrl = `http://localhost:3000${path}${url.search}`;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const proxyRequest = new Request(proxyUrl, {
|
|
93
|
+
method: request.method,
|
|
94
|
+
headers: {
|
|
95
|
+
...Object.fromEntries(request.headers),
|
|
96
|
+
'X-Original-URL': request.url,
|
|
97
|
+
'X-Forwarded-Host': url.hostname,
|
|
98
|
+
'X-Forwarded-Proto': url.protocol.replace(':', ''),
|
|
99
|
+
'X-Sandbox-Name': sandboxId // Pass the friendly name
|
|
100
|
+
},
|
|
101
|
+
body: request.body,
|
|
102
|
+
// @ts-expect-error - duplex required for body streaming in modern runtimes
|
|
103
|
+
duplex: 'half'
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
return await sandbox.containerFetch(proxyRequest, port);
|
|
107
|
+
} catch (error) {
|
|
108
|
+
logger.error(
|
|
109
|
+
'Proxy routing error',
|
|
110
|
+
error instanceof Error ? error : new Error(String(error))
|
|
111
|
+
);
|
|
112
|
+
return new Response('Proxy routing error', { status: 500 });
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function extractSandboxRoute(url: URL): RouteInfo | null {
|
|
117
|
+
// Parse subdomain pattern: port-sandboxId-token.domain (tokens mandatory)
|
|
118
|
+
// Token is always exactly 16 chars (generated by generatePortToken)
|
|
119
|
+
const subdomainMatch = url.hostname.match(
|
|
120
|
+
/^(\d{4,5})-([^.-][^.]*?[^.-]|[^.-])-([a-z0-9_-]{16})\.(.+)$/
|
|
121
|
+
);
|
|
122
|
+
|
|
123
|
+
if (!subdomainMatch) {
|
|
124
|
+
return null;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
const portStr = subdomainMatch[1];
|
|
128
|
+
const sandboxId = subdomainMatch[2];
|
|
129
|
+
const token = subdomainMatch[3]; // Mandatory token
|
|
130
|
+
const domain = subdomainMatch[4];
|
|
131
|
+
|
|
132
|
+
const port = parseInt(portStr, 10);
|
|
133
|
+
if (!validatePort(port)) {
|
|
134
|
+
return null;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
let sanitizedSandboxId: string;
|
|
138
|
+
try {
|
|
139
|
+
sanitizedSandboxId = sanitizeSandboxId(sandboxId);
|
|
140
|
+
} catch (error) {
|
|
141
|
+
return null;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// DNS subdomain length limit is 63 characters
|
|
145
|
+
if (sandboxId.length > 63) {
|
|
146
|
+
return null;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
return {
|
|
150
|
+
port,
|
|
151
|
+
sandboxId: sanitizedSandboxId,
|
|
152
|
+
path: url.pathname || '/',
|
|
153
|
+
token
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
export function isLocalhostPattern(hostname: string): boolean {
|
|
158
|
+
// Handle IPv6 addresses in brackets (with or without port)
|
|
159
|
+
if (hostname.startsWith('[')) {
|
|
160
|
+
if (hostname.includes(']:')) {
|
|
161
|
+
// [::1]:port format
|
|
162
|
+
const ipv6Part = hostname.substring(0, hostname.indexOf(']:') + 1);
|
|
163
|
+
return ipv6Part === '[::1]';
|
|
164
|
+
} else {
|
|
165
|
+
// [::1] format without port
|
|
166
|
+
return hostname === '[::1]';
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
// Handle bare IPv6 without brackets
|
|
171
|
+
if (hostname === '::1') {
|
|
172
|
+
return true;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
// For IPv4 and regular hostnames, split on colon to remove port
|
|
176
|
+
const hostPart = hostname.split(':')[0];
|
|
177
|
+
|
|
178
|
+
return (
|
|
179
|
+
hostPart === 'localhost' ||
|
|
180
|
+
hostPart === '127.0.0.1' ||
|
|
181
|
+
hostPart === '0.0.0.0'
|
|
182
|
+
);
|
|
183
|
+
}
|