@cloudflare/sandbox 0.0.0-871f813 → 0.0.0-89632aa
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 +234 -0
- package/Dockerfile +173 -89
- package/README.md +92 -707
- 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 -8
- 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 +94 -44
- package/src/interpreter.ts +62 -44
- package/src/request-handler.ts +94 -55
- package/src/sandbox.ts +887 -397
- package/src/security.ts +34 -28
- package/src/sse-parser.ts +8 -11
- 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/bun.lock +0 -122
- package/container_src/circuit-breaker.ts +0 -121
- package/container_src/handler/exec.ts +0 -340
- 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 -656
- package/container_src/jupyter-server.ts +0 -579
- package/container_src/jupyter-service.ts +0 -458
- package/container_src/jupyter_config.py +0 -48
- package/container_src/mime-processor.ts +0 -255
- package/container_src/package.json +0 -18
- package/container_src/startup.sh +0 -84
- package/container_src/types.ts +0 -108
- package/src/client.ts +0 -1021
- package/src/errors.ts +0 -218
- package/src/interpreter-types.ts +0 -383
- package/src/jupyter-client.ts +0 -349
- package/src/types.ts +0 -401
package/src/request-handler.ts
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
validatePort
|
|
6
|
-
} from "./security";
|
|
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';
|
|
7
5
|
|
|
8
6
|
export interface SandboxEnv {
|
|
9
7
|
Sandbox: DurableObjectNamespace<Sandbox>;
|
|
@@ -13,12 +11,22 @@ export interface RouteInfo {
|
|
|
13
11
|
port: number;
|
|
14
12
|
sandboxId: string;
|
|
15
13
|
path: string;
|
|
14
|
+
token: string;
|
|
16
15
|
}
|
|
17
16
|
|
|
18
17
|
export async function proxyToSandbox<E extends SandboxEnv>(
|
|
19
18
|
request: Request,
|
|
20
19
|
env: E
|
|
21
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
|
+
|
|
22
30
|
try {
|
|
23
31
|
const url = new URL(request.url);
|
|
24
32
|
const routeInfo = extractSandboxRoute(url);
|
|
@@ -27,9 +35,48 @@ export async function proxyToSandbox<E extends SandboxEnv>(
|
|
|
27
35
|
return null; // Not a request to an exposed container port
|
|
28
36
|
}
|
|
29
37
|
|
|
30
|
-
const { sandboxId, port, path } = routeInfo;
|
|
38
|
+
const { sandboxId, port, path, token } = routeInfo;
|
|
31
39
|
const sandbox = getSandbox(env.Sandbox, sandboxId);
|
|
32
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
|
+
|
|
33
80
|
// Build proxy request with proper headers
|
|
34
81
|
let proxyUrl: string;
|
|
35
82
|
|
|
@@ -49,46 +96,41 @@ export async function proxyToSandbox<E extends SandboxEnv>(
|
|
|
49
96
|
'X-Original-URL': request.url,
|
|
50
97
|
'X-Forwarded-Host': url.hostname,
|
|
51
98
|
'X-Forwarded-Proto': url.protocol.replace(':', ''),
|
|
52
|
-
'X-Sandbox-Name': sandboxId
|
|
99
|
+
'X-Sandbox-Name': sandboxId // Pass the friendly name
|
|
53
100
|
},
|
|
54
101
|
body: request.body,
|
|
102
|
+
// @ts-expect-error - duplex required for body streaming in modern runtimes
|
|
103
|
+
duplex: 'half'
|
|
55
104
|
});
|
|
56
105
|
|
|
57
|
-
return sandbox.containerFetch(proxyRequest, port);
|
|
106
|
+
return await sandbox.containerFetch(proxyRequest, port);
|
|
58
107
|
} catch (error) {
|
|
59
|
-
|
|
108
|
+
logger.error(
|
|
109
|
+
'Proxy routing error',
|
|
110
|
+
error instanceof Error ? error : new Error(String(error))
|
|
111
|
+
);
|
|
60
112
|
return new Response('Proxy routing error', { status: 500 });
|
|
61
113
|
}
|
|
62
114
|
}
|
|
63
115
|
|
|
64
116
|
function extractSandboxRoute(url: URL): RouteInfo | null {
|
|
65
|
-
// Parse subdomain pattern: port-sandboxId.domain
|
|
66
|
-
|
|
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
|
+
);
|
|
67
122
|
|
|
68
123
|
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
124
|
return null;
|
|
77
125
|
}
|
|
78
126
|
|
|
79
127
|
const portStr = subdomainMatch[1];
|
|
80
128
|
const sandboxId = subdomainMatch[2];
|
|
81
|
-
const
|
|
129
|
+
const token = subdomainMatch[3]; // Mandatory token
|
|
130
|
+
const domain = subdomainMatch[4];
|
|
82
131
|
|
|
83
132
|
const port = parseInt(portStr, 10);
|
|
84
133
|
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
134
|
return null;
|
|
93
135
|
}
|
|
94
136
|
|
|
@@ -96,49 +138,46 @@ function extractSandboxRoute(url: URL): RouteInfo | null {
|
|
|
96
138
|
try {
|
|
97
139
|
sanitizedSandboxId = sanitizeSandboxId(sandboxId);
|
|
98
140
|
} 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
141
|
return null;
|
|
107
142
|
}
|
|
108
143
|
|
|
109
144
|
// DNS subdomain length limit is 63 characters
|
|
110
145
|
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
146
|
return null;
|
|
118
147
|
}
|
|
119
148
|
|
|
120
|
-
logSecurityEvent('SANDBOX_ROUTE_EXTRACTED', {
|
|
121
|
-
port,
|
|
122
|
-
sandboxId: sanitizedSandboxId,
|
|
123
|
-
domain,
|
|
124
|
-
path: url.pathname || "/",
|
|
125
|
-
hostname: url.hostname
|
|
126
|
-
}, 'low');
|
|
127
|
-
|
|
128
149
|
return {
|
|
129
150
|
port,
|
|
130
151
|
sandboxId: sanitizedSandboxId,
|
|
131
|
-
path: url.pathname ||
|
|
152
|
+
path: url.pathname || '/',
|
|
153
|
+
token
|
|
132
154
|
};
|
|
133
155
|
}
|
|
134
156
|
|
|
135
157
|
export function isLocalhostPattern(hostname: string): boolean {
|
|
136
|
-
|
|
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
|
+
|
|
137
178
|
return (
|
|
138
|
-
hostPart ===
|
|
139
|
-
hostPart ===
|
|
140
|
-
hostPart ===
|
|
141
|
-
hostPart === "[::1]" ||
|
|
142
|
-
hostPart === "0.0.0.0"
|
|
179
|
+
hostPart === 'localhost' ||
|
|
180
|
+
hostPart === '127.0.0.1' ||
|
|
181
|
+
hostPart === '0.0.0.0'
|
|
143
182
|
);
|
|
144
183
|
}
|