@capybara-hq/plugin-device-e2b 0.0.0-stage → 0.1.0-rc.21
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/.capybara-plugin/plugin.json +152 -0
- package/LICENSE +202 -0
- package/NOTICE +2 -0
- package/README.md +407 -2
- package/dist/index.mjs +3612 -0
- package/guides/connect.md +7 -0
- package/guides/connect.zh.md +5 -0
- package/icon.svg +1 -0
- package/package.json +19 -4
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,3612 @@
|
|
|
1
|
+
import { createRequire as __capybaraCreateRequire } from "node:module";
|
|
2
|
+
import { fileURLToPath as __capybaraFileURLToPath } from "node:url";
|
|
3
|
+
import { dirname as __capybaraPathDirname } from "node:path";
|
|
4
|
+
const require = __capybaraCreateRequire(import.meta.url);
|
|
5
|
+
const __capybara_filename = __capybaraFileURLToPath(import.meta.url);
|
|
6
|
+
const __capybara_dirname = __capybaraPathDirname(__capybara_filename);
|
|
7
|
+
|
|
8
|
+
// src/index.ts
|
|
9
|
+
import {
|
|
10
|
+
DEVICE_PROVIDER_EXTENSION_POINT,
|
|
11
|
+
defineExtension,
|
|
12
|
+
secret,
|
|
13
|
+
ui
|
|
14
|
+
} from "@capybara-hq/plugin-sdk";
|
|
15
|
+
import { z } from "zod";
|
|
16
|
+
|
|
17
|
+
// src/provider.ts
|
|
18
|
+
import { randomUUID } from "node:crypto";
|
|
19
|
+
import {
|
|
20
|
+
FileSystemGoneError,
|
|
21
|
+
SANDBOX_TAG_RE,
|
|
22
|
+
SandboxGoneError,
|
|
23
|
+
SandboxOperationOutcomeUnknownError as SandboxOperationOutcomeUnknownError2,
|
|
24
|
+
validateAllowHost,
|
|
25
|
+
validateSandboxPathShape as validateSandboxPathShape3
|
|
26
|
+
} from "@capybara-hq/plugin-sdk";
|
|
27
|
+
|
|
28
|
+
// src/e2b-api.ts
|
|
29
|
+
import { OPEN_PUBLIC_DENIED_CIDRS } from "@capybara-hq/plugin-sdk";
|
|
30
|
+
var ENVD_PORT = 49983;
|
|
31
|
+
var API_TIMEOUT_MS = 6e4;
|
|
32
|
+
var KEEPALIVE_SECONDS = 50;
|
|
33
|
+
var ALL_TRAFFIC = "0.0.0.0/0";
|
|
34
|
+
var IPV6_DENIED = ["fc00::/7", "fe80::/10", "::1/128"];
|
|
35
|
+
var OPEN_PUBLIC_DENIED = Object.freeze([
|
|
36
|
+
...OPEN_PUBLIC_DENIED_CIDRS.filter((cidr) => !cidr.includes(":") && cidr !== "0.0.0.0/8"),
|
|
37
|
+
...IPV6_DENIED
|
|
38
|
+
]);
|
|
39
|
+
async function updateNetwork(api, id, body, signal) {
|
|
40
|
+
await api.json("PUT", `/sandboxes/${encodeURIComponent(id)}/network`, { body, signal });
|
|
41
|
+
}
|
|
42
|
+
var E2BHttpError = class extends Error {
|
|
43
|
+
constructor(status, message, code) {
|
|
44
|
+
super(message);
|
|
45
|
+
this.status = status;
|
|
46
|
+
this.code = code;
|
|
47
|
+
this.name = "E2BHttpError";
|
|
48
|
+
}
|
|
49
|
+
status;
|
|
50
|
+
code;
|
|
51
|
+
};
|
|
52
|
+
function isStatus(e, ...status) {
|
|
53
|
+
return e instanceof E2BHttpError && status.includes(e.status);
|
|
54
|
+
}
|
|
55
|
+
function isE2BNotFound(e) {
|
|
56
|
+
return e instanceof E2BHttpError && e.status === 404 && e.code === 404;
|
|
57
|
+
}
|
|
58
|
+
function abortReason(signal) {
|
|
59
|
+
return signal.reason ?? new DOMException("The operation was aborted", "AbortError");
|
|
60
|
+
}
|
|
61
|
+
function rethrow(e, signal) {
|
|
62
|
+
if (signal.aborted) throw abortReason(signal);
|
|
63
|
+
throw e;
|
|
64
|
+
}
|
|
65
|
+
async function errorOf(res, what) {
|
|
66
|
+
let message = "";
|
|
67
|
+
let code;
|
|
68
|
+
try {
|
|
69
|
+
const text = await res.text();
|
|
70
|
+
try {
|
|
71
|
+
const j = JSON.parse(text);
|
|
72
|
+
message = typeof j.message === "string" ? j.message : text;
|
|
73
|
+
if (typeof j.code === "number" || typeof j.code === "string") code = j.code;
|
|
74
|
+
} catch {
|
|
75
|
+
message = text;
|
|
76
|
+
}
|
|
77
|
+
} catch {
|
|
78
|
+
}
|
|
79
|
+
return new E2BHttpError(
|
|
80
|
+
res.status,
|
|
81
|
+
`E2B ${what} failed: HTTP ${res.status}${message ? `: ${message.slice(0, 300)}` : ""}`,
|
|
82
|
+
code
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
function createE2BApi(apiUrl, apiKey) {
|
|
86
|
+
const base = apiUrl.replace(/\/+$/, "");
|
|
87
|
+
return {
|
|
88
|
+
async json(method, path, opts) {
|
|
89
|
+
opts.signal.throwIfAborted();
|
|
90
|
+
const url = new URL(base + path);
|
|
91
|
+
for (const [k, v] of Object.entries(opts.query ?? {})) {
|
|
92
|
+
if (v !== void 0) url.searchParams.set(k, v);
|
|
93
|
+
}
|
|
94
|
+
let res;
|
|
95
|
+
try {
|
|
96
|
+
res = await fetch(url, {
|
|
97
|
+
method,
|
|
98
|
+
headers: {
|
|
99
|
+
"X-API-KEY": apiKey,
|
|
100
|
+
...opts.body !== void 0 && { "content-type": "application/json" }
|
|
101
|
+
},
|
|
102
|
+
...opts.body !== void 0 && { body: JSON.stringify(opts.body) },
|
|
103
|
+
redirect: "error",
|
|
104
|
+
signal: AbortSignal.any([opts.signal, AbortSignal.timeout(API_TIMEOUT_MS)])
|
|
105
|
+
});
|
|
106
|
+
} catch (e) {
|
|
107
|
+
rethrow(e, opts.signal);
|
|
108
|
+
}
|
|
109
|
+
if (!res.ok) throw await errorOf(res, `${method} ${path}`);
|
|
110
|
+
const text = await res.text().catch((e) => rethrow(e, opts.signal));
|
|
111
|
+
return {
|
|
112
|
+
status: res.status,
|
|
113
|
+
data: text === "" ? void 0 : JSON.parse(text),
|
|
114
|
+
headers: res.headers
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
var STDIN_CHUNK = 256 * 1024;
|
|
120
|
+
var basic = (user) => `Basic ${Buffer.from(`${user}:`).toString("base64")}`;
|
|
121
|
+
function envdHeaders(t, extra = {}) {
|
|
122
|
+
return {
|
|
123
|
+
"E2b-Sandbox-Id": t.sandboxId,
|
|
124
|
+
"E2b-Sandbox-Port": String(ENVD_PORT),
|
|
125
|
+
"X-Access-Token": t.accessToken,
|
|
126
|
+
...extra
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
function envelope(msg) {
|
|
130
|
+
const json = Buffer.from(JSON.stringify(msg), "utf8");
|
|
131
|
+
const out = Buffer.alloc(5 + json.byteLength);
|
|
132
|
+
out.writeUInt8(0, 0);
|
|
133
|
+
out.writeUInt32BE(json.byteLength, 1);
|
|
134
|
+
json.copy(out, 5);
|
|
135
|
+
return out;
|
|
136
|
+
}
|
|
137
|
+
async function envdUpload(t, path, user, data, signal) {
|
|
138
|
+
const url = new URL(`${t.url}/files`);
|
|
139
|
+
url.searchParams.set("path", path);
|
|
140
|
+
url.searchParams.set("username", user);
|
|
141
|
+
const form = new FormData();
|
|
142
|
+
form.append("file", new Blob([new Uint8Array(data)]), path.split("/").pop() ?? "file");
|
|
143
|
+
let res;
|
|
144
|
+
try {
|
|
145
|
+
res = await fetch(url, {
|
|
146
|
+
method: "POST",
|
|
147
|
+
headers: envdHeaders(t),
|
|
148
|
+
body: form,
|
|
149
|
+
redirect: "error",
|
|
150
|
+
signal
|
|
151
|
+
});
|
|
152
|
+
} catch (e) {
|
|
153
|
+
rethrow(e, signal);
|
|
154
|
+
}
|
|
155
|
+
if (!res.ok) throw await errorOf(res, "file upload");
|
|
156
|
+
await res.body?.cancel().catch(() => void 0);
|
|
157
|
+
}
|
|
158
|
+
async function envdKill(t, pid, signal) {
|
|
159
|
+
let res;
|
|
160
|
+
try {
|
|
161
|
+
res = await fetch(`${t.url}/process.Process/SendSignal`, {
|
|
162
|
+
method: "POST",
|
|
163
|
+
headers: envdHeaders(t, {
|
|
164
|
+
"content-type": "application/json",
|
|
165
|
+
"Connect-Protocol-Version": "1"
|
|
166
|
+
}),
|
|
167
|
+
body: JSON.stringify({ process: { pid }, signal: "SIGNAL_SIGKILL" }),
|
|
168
|
+
redirect: "error",
|
|
169
|
+
signal
|
|
170
|
+
});
|
|
171
|
+
} catch (e) {
|
|
172
|
+
rethrow(e, signal);
|
|
173
|
+
}
|
|
174
|
+
if (!res.ok) {
|
|
175
|
+
const e = await errorOf(res, "process signal");
|
|
176
|
+
if (e.status === 404 && e.code === "not_found") return;
|
|
177
|
+
throw e;
|
|
178
|
+
}
|
|
179
|
+
await res.body?.cancel().catch(() => void 0);
|
|
180
|
+
}
|
|
181
|
+
async function envdStart(t, user, req, onEvent, signal, stdin = false) {
|
|
182
|
+
let res;
|
|
183
|
+
try {
|
|
184
|
+
res = await fetch(`${t.url}/process.Process/Start`, {
|
|
185
|
+
method: "POST",
|
|
186
|
+
headers: envdHeaders(t, {
|
|
187
|
+
"content-type": "application/connect+json",
|
|
188
|
+
"Connect-Protocol-Version": "1",
|
|
189
|
+
Authorization: basic(user),
|
|
190
|
+
"Keepalive-Ping-Interval": String(KEEPALIVE_SECONDS)
|
|
191
|
+
}),
|
|
192
|
+
body: envelope({ process: req, stdin }),
|
|
193
|
+
redirect: "error",
|
|
194
|
+
signal
|
|
195
|
+
});
|
|
196
|
+
} catch (e) {
|
|
197
|
+
rethrow(e, signal);
|
|
198
|
+
}
|
|
199
|
+
if (res.status !== 200 || !res.body) throw await errorOf(res, "process start");
|
|
200
|
+
const reader = res.body.getReader();
|
|
201
|
+
const cancel = () => void reader.cancel().catch(() => void 0);
|
|
202
|
+
signal.addEventListener("abort", cancel, { once: true });
|
|
203
|
+
let buf = Buffer.alloc(0);
|
|
204
|
+
try {
|
|
205
|
+
for (; ; ) {
|
|
206
|
+
signal.throwIfAborted();
|
|
207
|
+
const { done, value } = await reader.read();
|
|
208
|
+
signal.throwIfAborted();
|
|
209
|
+
if (done) break;
|
|
210
|
+
buf = buf.byteLength === 0 ? Buffer.from(value) : Buffer.concat([buf, value]);
|
|
211
|
+
while (buf.byteLength >= 5) {
|
|
212
|
+
const len = buf.readUInt32BE(1);
|
|
213
|
+
if (buf.byteLength < 5 + len) break;
|
|
214
|
+
const flags = buf.readUInt8(0);
|
|
215
|
+
const msg = JSON.parse(buf.subarray(5, 5 + len).toString("utf8"));
|
|
216
|
+
buf = buf.subarray(5 + len);
|
|
217
|
+
if (flags & 2) {
|
|
218
|
+
if (msg.error) {
|
|
219
|
+
throw new Error(
|
|
220
|
+
`E2B process stream failed: ${msg.error.code ?? "unknown"}: ${msg.error.message ?? ""}`
|
|
221
|
+
);
|
|
222
|
+
}
|
|
223
|
+
return;
|
|
224
|
+
}
|
|
225
|
+
if (msg.event) onEvent(msg.event);
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
} catch (e) {
|
|
229
|
+
await reader.cancel().catch(() => void 0);
|
|
230
|
+
rethrow(e, signal);
|
|
231
|
+
} finally {
|
|
232
|
+
signal.removeEventListener("abort", cancel);
|
|
233
|
+
}
|
|
234
|
+
throw new Error("E2B process stream ended without an end-of-stream message");
|
|
235
|
+
}
|
|
236
|
+
async function envdUnary(t, method, body, what, signal) {
|
|
237
|
+
let res;
|
|
238
|
+
try {
|
|
239
|
+
res = await fetch(`${t.url}/process.Process/${method}`, {
|
|
240
|
+
method: "POST",
|
|
241
|
+
headers: envdHeaders(t, {
|
|
242
|
+
"content-type": "application/json",
|
|
243
|
+
"Connect-Protocol-Version": "1"
|
|
244
|
+
}),
|
|
245
|
+
body: JSON.stringify(body),
|
|
246
|
+
redirect: "error",
|
|
247
|
+
signal
|
|
248
|
+
});
|
|
249
|
+
} catch (e) {
|
|
250
|
+
rethrow(e, signal);
|
|
251
|
+
}
|
|
252
|
+
if (!res.ok) throw await errorOf(res, what);
|
|
253
|
+
await res.body?.cancel().catch(() => void 0);
|
|
254
|
+
}
|
|
255
|
+
async function envdSendInput(t, pid, data, signal) {
|
|
256
|
+
for (let at = 0; at < data.byteLength; at += STDIN_CHUNK) {
|
|
257
|
+
const part = Buffer.from(data.subarray(at, at + STDIN_CHUNK)).toString("base64");
|
|
258
|
+
await envdUnary(
|
|
259
|
+
t,
|
|
260
|
+
"SendInput",
|
|
261
|
+
{ process: { pid }, input: { stdin: part } },
|
|
262
|
+
"process stdin",
|
|
263
|
+
signal
|
|
264
|
+
);
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
async function envdCloseStdin(t, pid, signal) {
|
|
268
|
+
await envdUnary(t, "CloseStdin", { process: { pid } }, "process stdin close", signal);
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
// src/listing.ts
|
|
272
|
+
import { posix } from "node:path";
|
|
273
|
+
import {
|
|
274
|
+
DEVICE_SCAN_SKIPPED_BASE_DIRS,
|
|
275
|
+
DEVICE_SCAN_SKIPPED_DIRS,
|
|
276
|
+
isDeviceProjectName,
|
|
277
|
+
MAX_DEVICE_LIST_ENTRIES,
|
|
278
|
+
MAX_DEVICE_SCAN_DEPTH,
|
|
279
|
+
MAX_DEVICE_SCAN_DIRS,
|
|
280
|
+
MAX_DEVICE_SCAN_RESULTS,
|
|
281
|
+
validateSandboxPathShape
|
|
282
|
+
} from "@capybara-hq/plugin-sdk";
|
|
283
|
+
var LIST_AREA = "/workspace";
|
|
284
|
+
var LIST_TIMEOUT_SECONDS = 30;
|
|
285
|
+
var LIST_PATH = "/usr/local/bin:/usr/bin:/bin";
|
|
286
|
+
var LIST_MAX_OUTPUT_BYTES = 8 * 1024 * 1024;
|
|
287
|
+
var LIST_SCRIPT = `
|
|
288
|
+
import errno, json, os, stat, sys
|
|
289
|
+
op, p, limit = sys.argv[1], sys.argv[2], int(sys.argv[3])
|
|
290
|
+
L = "${LIST_AREA}"
|
|
291
|
+
W = os.path.realpath(L)
|
|
292
|
+
def out(o):
|
|
293
|
+
sys.stdout.write(json.dumps(o)); sys.exit(0)
|
|
294
|
+
def key(s):
|
|
295
|
+
return s.encode("utf-16-be", "surrogatepass")
|
|
296
|
+
def under(r, a):
|
|
297
|
+
return r == a or r.startswith(a + "/")
|
|
298
|
+
def fail(e):
|
|
299
|
+
out({"e": "read", "c": errno.errorcode.get(getattr(e, "errno", None) or 0, "")})
|
|
300
|
+
def plain_dir(e):
|
|
301
|
+
try:
|
|
302
|
+
return e.is_dir(follow_symlinks=False)
|
|
303
|
+
except OSError:
|
|
304
|
+
return False
|
|
305
|
+
q = os.path.normpath(p)
|
|
306
|
+
if not os.path.isabs(q) or not under(q, L):
|
|
307
|
+
out({"e": "policy"})
|
|
308
|
+
try:
|
|
309
|
+
r = os.path.realpath(q)
|
|
310
|
+
except OSError as e:
|
|
311
|
+
fail(e)
|
|
312
|
+
if not under(r, W):
|
|
313
|
+
out({"e": "policy"})
|
|
314
|
+
try:
|
|
315
|
+
st = os.stat(r)
|
|
316
|
+
except (FileNotFoundError, NotADirectoryError):
|
|
317
|
+
out({"r": None if op == "list" else []})
|
|
318
|
+
except OSError as e:
|
|
319
|
+
fail(e)
|
|
320
|
+
if not stat.S_ISDIR(st.st_mode):
|
|
321
|
+
out({"e": "notdir"})
|
|
322
|
+
try:
|
|
323
|
+
with os.scandir(r) as it:
|
|
324
|
+
ents = sorted(it, key=lambda e: key(e.name))
|
|
325
|
+
except OSError as e:
|
|
326
|
+
fail(e)
|
|
327
|
+
def is_dir(d, e):
|
|
328
|
+
try:
|
|
329
|
+
if e.is_dir(follow_symlinks=False):
|
|
330
|
+
return True
|
|
331
|
+
if not e.is_symlink():
|
|
332
|
+
return False
|
|
333
|
+
t = os.path.realpath(os.path.join(d, e.name))
|
|
334
|
+
return under(t, W) and os.path.isdir(t)
|
|
335
|
+
except OSError:
|
|
336
|
+
return False
|
|
337
|
+
if op == "list":
|
|
338
|
+
out({"r": {"entries": [{"name": e.name, "isDir": is_dir(r, e)} for e in ents[:limit]], "truncated": len(ents) > limit}})
|
|
339
|
+
skip_any = ${JSON.stringify(DEVICE_SCAN_SKIPPED_DIRS)}
|
|
340
|
+
skip_base = ${JSON.stringify(DEVICE_SCAN_SKIPPED_BASE_DIRS)}
|
|
341
|
+
found = []
|
|
342
|
+
def below(d, rel, es, depth):
|
|
343
|
+
if any(e.name == ".git" or (e.name == "AGENTS.md" and not plain_dir(e)) for e in es):
|
|
344
|
+
found.append(rel)
|
|
345
|
+
if depth >= ${MAX_DEVICE_SCAN_DEPTH}:
|
|
346
|
+
return []
|
|
347
|
+
return [(os.path.join(d, e.name), e.name if rel == "" else rel + "/" + e.name) for e in es
|
|
348
|
+
if plain_dir(e) and not e.name.startswith(".")
|
|
349
|
+
and e.name not in skip_any and not (depth == 0 and e.name in skip_base)]
|
|
350
|
+
visits = 1
|
|
351
|
+
level = below(r, "", ents, 0)
|
|
352
|
+
depth = 1
|
|
353
|
+
while level and visits < ${MAX_DEVICE_SCAN_DIRS}:
|
|
354
|
+
level.sort(key=lambda n: key(n[1]))
|
|
355
|
+
nxt = []
|
|
356
|
+
for d, rel in level:
|
|
357
|
+
if visits >= ${MAX_DEVICE_SCAN_DIRS}:
|
|
358
|
+
break
|
|
359
|
+
visits += 1
|
|
360
|
+
try:
|
|
361
|
+
with os.scandir(d) as it:
|
|
362
|
+
es = list(it)
|
|
363
|
+
except OSError:
|
|
364
|
+
continue
|
|
365
|
+
nxt.extend(below(d, rel, es, depth))
|
|
366
|
+
level = nxt
|
|
367
|
+
depth += 1
|
|
368
|
+
out({"r": found})
|
|
369
|
+
`;
|
|
370
|
+
function checkListPath(path) {
|
|
371
|
+
if (!path.startsWith("/")) {
|
|
372
|
+
throw new Error(`invalid sandbox path ${JSON.stringify(path)}: path must be absolute`);
|
|
373
|
+
}
|
|
374
|
+
let p = posix.normalize(path);
|
|
375
|
+
if (p.length > 1 && p.endsWith("/")) p = p.slice(0, -1);
|
|
376
|
+
const errors = validateSandboxPathShape(p);
|
|
377
|
+
if (errors.length > 0) {
|
|
378
|
+
throw new Error(`invalid sandbox path ${JSON.stringify(path)}: ${errors.join("; ")}`);
|
|
379
|
+
}
|
|
380
|
+
if (p !== LIST_AREA && !p.startsWith(`${LIST_AREA}/`)) throw outside(path);
|
|
381
|
+
return p;
|
|
382
|
+
}
|
|
383
|
+
function listLimit(limit) {
|
|
384
|
+
if (!Number.isInteger(limit) || limit < 0) {
|
|
385
|
+
throw new Error(`list limit must be a non-negative integer, got ${String(limit)}`);
|
|
386
|
+
}
|
|
387
|
+
return Math.min(limit, MAX_DEVICE_LIST_ENTRIES);
|
|
388
|
+
}
|
|
389
|
+
var outside = (path) => new Error(
|
|
390
|
+
`${path} is outside the sandbox's workspace (${LIST_AREA}); a cloud device offers only that, in either mode`
|
|
391
|
+
);
|
|
392
|
+
function answerOf(op, path, run, noPython) {
|
|
393
|
+
const text = (b) => Buffer.from(b).toString("utf8").trim().slice(0, 300);
|
|
394
|
+
if (run.exitCode === 127) throw new Error(noPython);
|
|
395
|
+
if (run.exitCode === 137) {
|
|
396
|
+
throw new Error(
|
|
397
|
+
`${op} ${path} took longer than ${LIST_TIMEOUT_SECONDS} s and was stopped; try a smaller directory or a base further down`
|
|
398
|
+
);
|
|
399
|
+
}
|
|
400
|
+
let res;
|
|
401
|
+
if (run.exitCode === 0 && !run.truncated) {
|
|
402
|
+
try {
|
|
403
|
+
res = JSON.parse(Buffer.from(run.stdout).toString("utf8"));
|
|
404
|
+
} catch {
|
|
405
|
+
res = void 0;
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
if (res === void 0 || typeof res !== "object" || res === null) {
|
|
409
|
+
const why = run.exitCode === 0 ? "an unreadable answer" : `exit code ${String(run.exitCode)}`;
|
|
410
|
+
throw new Error(`${op} ${path} failed in the sandbox (${why}): ${text(run.stderr)}`);
|
|
411
|
+
}
|
|
412
|
+
if (res.e === "policy") throw outside(path);
|
|
413
|
+
if (res.e === "notdir") throw new Error(`${path} is not a directory`);
|
|
414
|
+
if (res.e === "read") {
|
|
415
|
+
const code = typeof res.c === "string" && /^[A-Z0-9_]+$/.test(res.c) ? ` (${res.c})` : "";
|
|
416
|
+
throw new Error(`${path} cannot be read${code}`);
|
|
417
|
+
}
|
|
418
|
+
if (res.e !== void 0) throw new Error(`${op} ${path} failed in the sandbox`);
|
|
419
|
+
return res.r ?? null;
|
|
420
|
+
}
|
|
421
|
+
function finishList(path, run, noPython) {
|
|
422
|
+
const r = answerOf("list", path, run, noPython);
|
|
423
|
+
if (r === null) return null;
|
|
424
|
+
const v = r;
|
|
425
|
+
if (!Array.isArray(v.entries) || typeof v.truncated !== "boolean" || !v.entries.every(
|
|
426
|
+
(e) => typeof e?.name === "string" && typeof e.isDir === "boolean"
|
|
427
|
+
)) {
|
|
428
|
+
throw new Error(`list ${path} answered an invalid result`);
|
|
429
|
+
}
|
|
430
|
+
return {
|
|
431
|
+
entries: v.entries.map((e) => ({
|
|
432
|
+
name: e.name,
|
|
433
|
+
isDir: e.isDir
|
|
434
|
+
})),
|
|
435
|
+
truncated: v.truncated
|
|
436
|
+
};
|
|
437
|
+
}
|
|
438
|
+
var byCodeUnit = (a, b) => a < b ? -1 : a > b ? 1 : 0;
|
|
439
|
+
function finishScan(base, run, noPython) {
|
|
440
|
+
const r = answerOf("scan", base, run, noPython);
|
|
441
|
+
if (!Array.isArray(r) || !r.every((x) => typeof x === "string")) {
|
|
442
|
+
throw new Error(`scan ${base} answered an invalid result`);
|
|
443
|
+
}
|
|
444
|
+
const out = [];
|
|
445
|
+
const seen = /* @__PURE__ */ new Set();
|
|
446
|
+
for (const rel of r) {
|
|
447
|
+
const path = rel === "" ? base : `${base}/${rel}`;
|
|
448
|
+
const name = path.slice(path.lastIndexOf("/") + 1);
|
|
449
|
+
if (seen.has(path) || !isDeviceProjectName(name)) continue;
|
|
450
|
+
if (validateSandboxPathShape(path).length > 0) continue;
|
|
451
|
+
seen.add(path);
|
|
452
|
+
out.push({ path, suggestedName: name });
|
|
453
|
+
}
|
|
454
|
+
return out.sort((a, b) => byCodeUnit(a.path, b.path)).slice(0, MAX_DEVICE_SCAN_RESULTS);
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
// src/operations.ts
|
|
458
|
+
import { createHash as createHash2, randomBytes } from "node:crypto";
|
|
459
|
+
import { posix as posix2 } from "node:path";
|
|
460
|
+
import {
|
|
461
|
+
parseSandboxBrowseScope,
|
|
462
|
+
parseSandboxExecutionRef,
|
|
463
|
+
parseSandboxOperationRef,
|
|
464
|
+
parseSandboxOperationScope,
|
|
465
|
+
parseSandboxPreparationScope,
|
|
466
|
+
SandboxOperationOutcomeUnknownError,
|
|
467
|
+
SandboxPermissionUnsupportedError,
|
|
468
|
+
SandboxScopeDeniedError,
|
|
469
|
+
SandboxScopeValidationError,
|
|
470
|
+
validateSandboxPathShape as validateSandboxPathShape2
|
|
471
|
+
} from "@capybara-hq/plugin-sdk";
|
|
472
|
+
|
|
473
|
+
// src/runtime-source.ts
|
|
474
|
+
import { createHash } from "node:crypto";
|
|
475
|
+
import { SANDBOX_RUNTIME_SOURCE } from "@capybara-hq/plugin-sdk";
|
|
476
|
+
|
|
477
|
+
// src/operation-agent-source.ts
|
|
478
|
+
var OPERATION_AGENT_SHA256 = "4808452ede810522b3134bc83d395867460c423471f6afc1cace0f65854b8749";
|
|
479
|
+
var OPERATION_AGENT_SOURCE = `# Trusted in-VM consumer for the sandbox-e2b provider (staged by the provider as root,
|
|
480
|
+
# root-owned and hash-checked, in a directory named after this file's digest, next to the SDK's
|
|
481
|
+
# native-files.py and linux-sandbox.py). An adapted copy of sandbox-docker's agent: it never
|
|
482
|
+
# decides authority; every request carries the operation's immutable scope, already resolved by
|
|
483
|
+
# the kernel.
|
|
484
|
+
#
|
|
485
|
+
# Restricted execution is not part of this agent. The SDK launcher needs Landlock ABI 6, which
|
|
486
|
+
# the tested E2B template kernel does not have (Linux 6.1, Landlock ABI 2); the provider reports
|
|
487
|
+
# restricted support unavailable
|
|
488
|
+
# and refuses scoped grants before any effect. "probe" still runs the launcher's own probe so the
|
|
489
|
+
# report names the template's actual facts.
|
|
490
|
+
#
|
|
491
|
+
# direct native work without the launcher (Full access on "/", owner browsing of
|
|
492
|
+
# read roots, trusted preparation); descriptor-relative, no-follow traversal.
|
|
493
|
+
# full Full access exec: checks admission, then execs the provider's ordinary wrapper.
|
|
494
|
+
# bg-* Full access background executions: a detached supervisor per operation, with
|
|
495
|
+
# a root recorder owning an append-only output spool and a status record.
|
|
496
|
+
# probe the launcher's own probe, plus the template's kernel release.
|
|
497
|
+
# consume/revoke (root) the durable operation records, tombstones and cancellation markers in
|
|
498
|
+
# cancel-mark the root-owned record directory on the VM's persistent disk.
|
|
499
|
+
import base64
|
|
500
|
+
import ctypes
|
|
501
|
+
import errno
|
|
502
|
+
import hashlib
|
|
503
|
+
import hmac
|
|
504
|
+
import importlib.util
|
|
505
|
+
import json
|
|
506
|
+
import os
|
|
507
|
+
import re
|
|
508
|
+
import shutil
|
|
509
|
+
import signal
|
|
510
|
+
import socket
|
|
511
|
+
import stat
|
|
512
|
+
import struct
|
|
513
|
+
import subprocess
|
|
514
|
+
import sys
|
|
515
|
+
import threading
|
|
516
|
+
import time
|
|
517
|
+
|
|
518
|
+
RUNTIME = os.path.dirname(os.path.abspath(__file__))
|
|
519
|
+
LAUNCHER = os.path.join(RUNTIME, "linux-sandbox.py")
|
|
520
|
+
NATIVE = os.path.join(RUNTIME, "native-files.py")
|
|
521
|
+
# The record directory on the VM's disk (kept while the VM is paused without memory): created and
|
|
522
|
+
# written only by root (0711, records 0600; markers 0644 so the sandbox user can test for them).
|
|
523
|
+
OPS_ROOT = "/opt/capybara-e2b-ops"
|
|
524
|
+
OPS_VIEW = OPS_ROOT
|
|
525
|
+
# Paths reserved for the provider: its records, runtime, root staging area and control files.
|
|
526
|
+
PROTECTED = ("/tmp/.capybara-exec-", OPS_ROOT, "/opt/capybara", "/opt/capybara-e2b-runtime")
|
|
527
|
+
MARKER = "capybara-exec"
|
|
528
|
+
CHUNK = 65536
|
|
529
|
+
libc = ctypes.CDLL(None, use_errno=True)
|
|
530
|
+
|
|
531
|
+
|
|
532
|
+
class Refused(Exception):
|
|
533
|
+
def __init__(self, code, message):
|
|
534
|
+
super().__init__(message)
|
|
535
|
+
self.code = code
|
|
536
|
+
|
|
537
|
+
|
|
538
|
+
# ---- framing (stdout is the provider's only channel) ---------------------------------------
|
|
539
|
+
|
|
540
|
+
out_lock = threading.Lock()
|
|
541
|
+
|
|
542
|
+
|
|
543
|
+
def frame(kind, data):
|
|
544
|
+
with out_lock:
|
|
545
|
+
sys.stdout.buffer.write(kind + len(data).to_bytes(4, "big") + data)
|
|
546
|
+
sys.stdout.buffer.flush()
|
|
547
|
+
|
|
548
|
+
|
|
549
|
+
def finish(result):
|
|
550
|
+
frame(b"R", json.dumps(result).encode())
|
|
551
|
+
|
|
552
|
+
|
|
553
|
+
def read_request():
|
|
554
|
+
line = sys.stdin.buffer.readline()
|
|
555
|
+
size = int(line)
|
|
556
|
+
if size < 0 or size > 64 * 1024 * 1024:
|
|
557
|
+
raise ValueError("request too large")
|
|
558
|
+
data = bytearray()
|
|
559
|
+
while len(data) < size:
|
|
560
|
+
chunk = sys.stdin.buffer.read(size - len(data))
|
|
561
|
+
if not chunk:
|
|
562
|
+
raise ValueError("truncated request")
|
|
563
|
+
data += chunk
|
|
564
|
+
return json.loads(data)
|
|
565
|
+
|
|
566
|
+
|
|
567
|
+
def load(path, name):
|
|
568
|
+
spec = importlib.util.spec_from_file_location(name, path)
|
|
569
|
+
module = importlib.util.module_from_spec(spec)
|
|
570
|
+
spec.loader.exec_module(module)
|
|
571
|
+
return module
|
|
572
|
+
|
|
573
|
+
|
|
574
|
+
# ---- admission: tombstones, deadlines and staged bytes --------------------------------------
|
|
575
|
+
|
|
576
|
+
def op_key(operation_id):
|
|
577
|
+
return hashlib.sha256(operation_id.encode()).hexdigest()
|
|
578
|
+
|
|
579
|
+
|
|
580
|
+
def ops_dir():
|
|
581
|
+
return OPS_ROOT if os.getuid() == 0 else OPS_VIEW
|
|
582
|
+
|
|
583
|
+
|
|
584
|
+
def admit(request):
|
|
585
|
+
"""Refuses a revoked operation, a passed admission deadline or a requested cancellation.
|
|
586
|
+
Tombstones and cancellation markers are root-written; the sandbox user cannot forge them."""
|
|
587
|
+
key = request.get("opKey")
|
|
588
|
+
if key and os.path.exists(os.path.join(ops_dir(), key + ".revoked")):
|
|
589
|
+
raise Refused("REVOKED", "The operation was revoked")
|
|
590
|
+
deadline = request.get("admitBeforeMs")
|
|
591
|
+
if deadline is not None and time.time() * 1000 > deadline:
|
|
592
|
+
raise Refused("EXPIRED", "The operation's admission deadline passed")
|
|
593
|
+
if key and os.path.exists(os.path.join(ops_dir(), key + ".cancelled")):
|
|
594
|
+
raise Refused("CANCELLED", "The operation was cancelled before it started")
|
|
595
|
+
|
|
596
|
+
|
|
597
|
+
def protected(path, write):
|
|
598
|
+
"""A write root containing (or inside) the provider's records or control files, or a read
|
|
599
|
+
root inside the records, is refused (defence in depth beside the root-owned records)."""
|
|
600
|
+
path = os.path.normpath(path)
|
|
601
|
+
for entry in PROTECTED:
|
|
602
|
+
inside = path == entry.rstrip("-") or path.startswith(entry.rstrip("/") + "/")
|
|
603
|
+
if write and (inside or entry.startswith(path.rstrip("/") + "/")):
|
|
604
|
+
return True
|
|
605
|
+
if not write and inside and entry != "/tmp/.capybara-exec-":
|
|
606
|
+
return True
|
|
607
|
+
return False
|
|
608
|
+
|
|
609
|
+
|
|
610
|
+
def verify_runtime(expected):
|
|
611
|
+
"""The staged SDK bytes must match the digests the provider was built against."""
|
|
612
|
+
for name, digest in expected.items():
|
|
613
|
+
path = os.path.join(RUNTIME, name)
|
|
614
|
+
info = os.lstat(path)
|
|
615
|
+
if not stat.S_ISREG(info.st_mode) or info.st_uid != 0 or info.st_mode & 0o022:
|
|
616
|
+
raise Refused("UNAVAILABLE", "The staged runtime is not root-owned and read-only")
|
|
617
|
+
with open(path, "rb") as handle:
|
|
618
|
+
if hashlib.sha256(handle.read()).hexdigest() != digest:
|
|
619
|
+
raise Refused("UNAVAILABLE", "The staged runtime does not match its digest")
|
|
620
|
+
info = os.lstat(RUNTIME)
|
|
621
|
+
if not stat.S_ISDIR(info.st_mode) or info.st_uid != 0 or info.st_mode & 0o022:
|
|
622
|
+
raise Refused("UNAVAILABLE", "The runtime directory is writable by the operation user")
|
|
623
|
+
|
|
624
|
+
|
|
625
|
+
# ---- pinned roots -------------------------------------------------------------------------
|
|
626
|
+
|
|
627
|
+
def close_pinned(pinned):
|
|
628
|
+
for entry in pinned:
|
|
629
|
+
try:
|
|
630
|
+
os.close(entry["fd"])
|
|
631
|
+
except OSError:
|
|
632
|
+
pass
|
|
633
|
+
|
|
634
|
+
|
|
635
|
+
def pin(paths, write=False):
|
|
636
|
+
"""Opens each existing granted root once (no-follow, canonical) and records its identity.
|
|
637
|
+
|
|
638
|
+
A root that does not exist grants nothing and is left out; a root that is not a real
|
|
639
|
+
directory at its canonical path (a swapped symlink, for example) refuses the operation."""
|
|
640
|
+
pinned = []
|
|
641
|
+
try:
|
|
642
|
+
for path in paths:
|
|
643
|
+
if protected(path, write):
|
|
644
|
+
raise Refused("SCOPE_DENIED", "A granted root contains the provider's records")
|
|
645
|
+
try:
|
|
646
|
+
fd = os.open(path, os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW | os.O_CLOEXEC)
|
|
647
|
+
except FileNotFoundError:
|
|
648
|
+
continue
|
|
649
|
+
except OSError:
|
|
650
|
+
raise Refused("SCOPE_DENIED", "A granted root is not a directory at its path")
|
|
651
|
+
info = os.fstat(fd)
|
|
652
|
+
pinned.append({"path": path, "fd": fd, "dev": info.st_dev, "ino": info.st_ino})
|
|
653
|
+
if os.readlink("/proc/self/fd/%d" % fd) != path:
|
|
654
|
+
raise Refused("SCOPE_DENIED", "A granted root is not canonical")
|
|
655
|
+
except BaseException:
|
|
656
|
+
close_pinned(pinned)
|
|
657
|
+
raise
|
|
658
|
+
return pinned
|
|
659
|
+
|
|
660
|
+
|
|
661
|
+
def pin_under(base, relative, create):
|
|
662
|
+
"""Pins base/relative component by component without following links (trusted staging)."""
|
|
663
|
+
fd = os.open(base, os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW | os.O_CLOEXEC)
|
|
664
|
+
try:
|
|
665
|
+
for name in [part for part in relative.split("/") if part]:
|
|
666
|
+
if name in (".", ".."):
|
|
667
|
+
raise Refused("SCOPE_DENIED", "Invalid preparation root")
|
|
668
|
+
if create:
|
|
669
|
+
try:
|
|
670
|
+
os.mkdir(name, 0o755, dir_fd=fd)
|
|
671
|
+
except FileExistsError:
|
|
672
|
+
pass
|
|
673
|
+
try:
|
|
674
|
+
child = os.open(name, os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW | os.O_CLOEXEC,
|
|
675
|
+
dir_fd=fd)
|
|
676
|
+
except OSError:
|
|
677
|
+
raise Refused("SCOPE_DENIED", "A preparation root is not a directory")
|
|
678
|
+
os.close(fd)
|
|
679
|
+
fd = child
|
|
680
|
+
except BaseException:
|
|
681
|
+
os.close(fd)
|
|
682
|
+
raise
|
|
683
|
+
return fd
|
|
684
|
+
|
|
685
|
+
|
|
686
|
+
def native_error(native, error):
|
|
687
|
+
code = ("SCOPE_DENIED" if isinstance(error, native.ScopeDenied)
|
|
688
|
+
else errno.errorcode.get(getattr(error, "errno", None), "INVALID_NATIVE_REQUEST"))
|
|
689
|
+
return {"error": {"code": code, "message": "Native operation failed"}}
|
|
690
|
+
|
|
691
|
+
|
|
692
|
+
def direct(request):
|
|
693
|
+
"""Native work on pinned descriptors without the launcher (no execution happens here)."""
|
|
694
|
+
admit(request)
|
|
695
|
+
verify_runtime(request["expect"])
|
|
696
|
+
native = load(NATIVE, "capybara_native_files")
|
|
697
|
+
kind = request["kind"]
|
|
698
|
+
roots = {"read": [], "write": []}
|
|
699
|
+
pinned = []
|
|
700
|
+
try:
|
|
701
|
+
if kind == "device":
|
|
702
|
+
fd = os.open("/", os.O_RDONLY | os.O_DIRECTORY | os.O_CLOEXEC)
|
|
703
|
+
pinned.append({"fd": fd})
|
|
704
|
+
roots["write"].append({"path": "/", "fd": fd})
|
|
705
|
+
elif kind == "scoped":
|
|
706
|
+
if request.get("requireRestricted") or request["writeRoots"]:
|
|
707
|
+
# Only read-only owner browsing runs here; restricted grants are not supported.
|
|
708
|
+
raise Refused("UNAVAILABLE", "Restricted execution is not supported on this VM")
|
|
709
|
+
write = pin(request["writeRoots"], True)
|
|
710
|
+
pinned += write
|
|
711
|
+
read = pin([p for p in request["readRoots"] if p not in request["writeRoots"]])
|
|
712
|
+
pinned += read
|
|
713
|
+
roots = {"read": [{"path": e["path"], "fd": e["fd"]} for e in read],
|
|
714
|
+
"write": [{"path": e["path"], "fd": e["fd"]} for e in write]}
|
|
715
|
+
elif kind == "prepare":
|
|
716
|
+
if protected(request["native"]["path"], False):
|
|
717
|
+
raise Refused("SCOPE_DENIED", "The path is reserved for the provider")
|
|
718
|
+
# Trusted staging: roots are mapped below a protected base, created when missing.
|
|
719
|
+
for entry in request["prepareRoots"]:
|
|
720
|
+
fd = pin_under(entry["base"], entry["relative"], True)
|
|
721
|
+
pinned.append({"fd": fd})
|
|
722
|
+
roots["write"].append({"path": entry["path"], "fd": fd})
|
|
723
|
+
else:
|
|
724
|
+
raise ValueError("unknown native kind")
|
|
725
|
+
try:
|
|
726
|
+
files = native.NativeFiles(roots)
|
|
727
|
+
reply = {"result": files.run(request["native"])}
|
|
728
|
+
if request.get("publicFiles") and request["native"]["method"] == "writeFile":
|
|
729
|
+
# Skills are written by root and read by the sandbox user: 0644, like the
|
|
730
|
+
# provider's ordinary skill writes, applied through the same pinned parent.
|
|
731
|
+
parent, name = files.parent(request["native"]["path"], True)
|
|
732
|
+
try:
|
|
733
|
+
fd = os.open(name, os.O_RDONLY | os.O_NOFOLLOW | os.O_NONBLOCK, dir_fd=parent)
|
|
734
|
+
try:
|
|
735
|
+
os.fchmod(fd, 0o644)
|
|
736
|
+
finally:
|
|
737
|
+
os.close(fd)
|
|
738
|
+
finally:
|
|
739
|
+
os.close(parent)
|
|
740
|
+
except Exception as error:
|
|
741
|
+
reply = native_error(native, error)
|
|
742
|
+
finish({"native": reply, "events": []})
|
|
743
|
+
finally:
|
|
744
|
+
close_pinned(pinned)
|
|
745
|
+
|
|
746
|
+
|
|
747
|
+
# ---- Full access: the provider's ordinary wrapper -----------------------------------------
|
|
748
|
+
|
|
749
|
+
NONCE_ENV = "CAPYBARA_ADMISSION_NONCE"
|
|
750
|
+
admission_nonce = [None]
|
|
751
|
+
|
|
752
|
+
|
|
753
|
+
def full(argv):
|
|
754
|
+
# The nonce authenticates a refusal frame; it is removed before the command can see it.
|
|
755
|
+
admission_nonce[0] = os.environ.pop(NONCE_ENV, None)
|
|
756
|
+
request = {"opKey": argv[0], "admitBeforeMs": int(argv[1])}
|
|
757
|
+
admit(request)
|
|
758
|
+
if argv[3] != "--" or argv[4] != "setsid":
|
|
759
|
+
raise ValueError("unexpected Full access command")
|
|
760
|
+
os.execv("/usr/bin/setsid", argv[4:])
|
|
761
|
+
|
|
762
|
+
|
|
763
|
+
# ---- background executions ----------------------------------------------------------------
|
|
764
|
+
#
|
|
765
|
+
# Background executions run Full access work only (restricted grants are refused). Every
|
|
766
|
+
# trusted background record lives in the root-owned record directory (only root, the
|
|
767
|
+
# provider's own consumer steps, writes it; the sandbox user cannot even list it): the record,
|
|
768
|
+
# the output spool, the stop claim and the channel. A detached root recorder per execution owns
|
|
769
|
+
# them. The sandbox-user supervisor reaches it only through a root-owned UNIX socket whose random
|
|
770
|
+
# name the provider hands to the supervisor on stdin; the recorder accepts frames only on the one
|
|
771
|
+
# connection whose SO_PEERCRED pid is the supervisor that identified itself, and the supervisor is
|
|
772
|
+
# not dumpable, so restricted work cannot reach its channel and same-uid processes cannot reopen
|
|
773
|
+
# its descriptors once it runs. The identity frame carries a second random token (so the name
|
|
774
|
+
# that /proc/net/unix lists is not enough) and the digest of the request the supervisor runs; root
|
|
775
|
+
# recorded the digest of the request the provider admitted, and only a match is accepted. The
|
|
776
|
+
# token protects against discovery of the channel through /proc/net/unix; it is not unreadable:
|
|
777
|
+
# during bg-start's interpreter start-up (until PR_SET_DUMPABLE) same-uid processes can read its
|
|
778
|
+
# stdin through /proc. With it the real agent binds only a supervisor running exactly the granted
|
|
779
|
+
# request; a same-uid process running its own code can lie about the digest (the M5 residual). The
|
|
780
|
+
# provider aborts (stops whatever the record names) a refused start, a start whose bg-start did not
|
|
781
|
+
# exit 0 with one result frame, and an unacknowledged one whose record is contested (a second
|
|
782
|
+
# valid identity after the bind). A worker crash before the exec ended, followed by the same-ID
|
|
783
|
+
# retry, and a forger that ptraces the genuine bg-start stay open (M5, Task 14).
|
|
784
|
+
|
|
785
|
+
UUID = re.compile(r"[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}")
|
|
786
|
+
SECRET = re.compile(r"[0-9a-f]{32}")
|
|
787
|
+
READ_MAX = 65536
|
|
788
|
+
CANCEL_SIGNALS = {signal.SIGTERM, signal.SIGINT, signal.SIGHUP}
|
|
789
|
+
|
|
790
|
+
|
|
791
|
+
def bg_directory(execution, base=None):
|
|
792
|
+
if not isinstance(execution, str) or not UUID.fullmatch(execution):
|
|
793
|
+
raise ValueError("invalid execution id")
|
|
794
|
+
return os.path.join(base or os.path.join(ops_dir(), "bg"), execution)
|
|
795
|
+
|
|
796
|
+
|
|
797
|
+
def write_json(path, value, mode=0o600):
|
|
798
|
+
temporary = path + ".tmp"
|
|
799
|
+
fd = os.open(temporary, os.O_WRONLY | os.O_CREAT | os.O_TRUNC | os.O_NOFOLLOW, mode)
|
|
800
|
+
with os.fdopen(fd, "w") as handle:
|
|
801
|
+
json.dump(value, handle)
|
|
802
|
+
os.replace(temporary, path)
|
|
803
|
+
|
|
804
|
+
|
|
805
|
+
def read_json(path):
|
|
806
|
+
try:
|
|
807
|
+
with open(path) as handle:
|
|
808
|
+
return json.load(handle)
|
|
809
|
+
except (OSError, ValueError):
|
|
810
|
+
return None
|
|
811
|
+
|
|
812
|
+
|
|
813
|
+
def proc_start(pid):
|
|
814
|
+
"""A live (non-zombie) process's start time, or None."""
|
|
815
|
+
try:
|
|
816
|
+
with open("/proc/%d/stat" % pid) as handle:
|
|
817
|
+
data = handle.read()
|
|
818
|
+
fields = data[data.rindex(")") + 2:].split()
|
|
819
|
+
return None if fields[0] in ("Z", "X") else int(fields[19])
|
|
820
|
+
except (OSError, ValueError, IndexError, TypeError):
|
|
821
|
+
return None
|
|
822
|
+
|
|
823
|
+
|
|
824
|
+
def write_all(fd, data):
|
|
825
|
+
view = memoryview(data)
|
|
826
|
+
while view:
|
|
827
|
+
view = view[os.write(fd, view):]
|
|
828
|
+
|
|
829
|
+
|
|
830
|
+
SPOOL_CAP = 64 * 1024 * 1024
|
|
831
|
+
PYTHON = "/usr/bin/python3"
|
|
832
|
+
|
|
833
|
+
|
|
834
|
+
def start_digest(start):
|
|
835
|
+
"""The digest of the canonical start request: every field the supervisor runs (command,
|
|
836
|
+
scope, cwd, env, limits, output path...), i.e. the request without its channel name and token."""
|
|
837
|
+
body = {key: value for key, value in start.items() if key not in ("secret", "token")}
|
|
838
|
+
canonical = json.dumps(body, sort_keys=True, separators=(",", ":"), ensure_ascii=True)
|
|
839
|
+
return hashlib.sha256(canonical.encode()).hexdigest()
|
|
840
|
+
|
|
841
|
+
|
|
842
|
+
def bg_prepare(request):
|
|
843
|
+
"""(root) Creates the execution's record, channel and recorder before anything launches."""
|
|
844
|
+
records()
|
|
845
|
+
directory = bg_directory(request["executionId"])
|
|
846
|
+
secret = request["secret"]
|
|
847
|
+
if not SECRET.fullmatch(secret):
|
|
848
|
+
raise ValueError("invalid channel name")
|
|
849
|
+
try:
|
|
850
|
+
os.mkdir(directory, 0o711)
|
|
851
|
+
except FileExistsError:
|
|
852
|
+
return {"prepared": False, "existing": True}
|
|
853
|
+
os.chmod(directory, 0o711)
|
|
854
|
+
# A second random value that never appears in a path (unlike the socket name, which
|
|
855
|
+
# /proc/net/unix lists): the supervisor's identity frame must carry it.
|
|
856
|
+
token = request["token"]
|
|
857
|
+
if not isinstance(token, str) or not SECRET.fullmatch(token):
|
|
858
|
+
raise ValueError("invalid channel token")
|
|
859
|
+
# The exact request the provider admitted: only a supervisor running it may bind the record.
|
|
860
|
+
start = request["start"]
|
|
861
|
+
if not isinstance(start, dict) or "secret" in start or "token" in start \\
|
|
862
|
+
or start.get("executionId") != request["executionId"] \\
|
|
863
|
+
or start.get("taskId") != request["taskId"] or start.get("opKey") != request.get("opKey"):
|
|
864
|
+
raise ValueError("invalid start request")
|
|
865
|
+
cap = request.get("spoolCapBytes") or SPOOL_CAP
|
|
866
|
+
if type(cap) is not int or not 0 < cap <= SPOOL_CAP:
|
|
867
|
+
raise ValueError("invalid spool cap")
|
|
868
|
+
record = {"state": "starting", "generation": generation(), "taskId": request["taskId"],
|
|
869
|
+
"opKey": request.get("opKey"), "retainUntilMs": request["retainUntilMs"],
|
|
870
|
+
"expiresAtMs": request["retainUntilMs"] + 3600_000, "spoolCapBytes": cap,
|
|
871
|
+
"startDigest": start_digest(start)}
|
|
872
|
+
write_json(os.path.join(directory, "record.json"), record)
|
|
873
|
+
ready_r, ready_w = os.pipe()
|
|
874
|
+
pid = os.fork()
|
|
875
|
+
if pid == 0:
|
|
876
|
+
try:
|
|
877
|
+
os.close(ready_r)
|
|
878
|
+
os.setsid()
|
|
879
|
+
if os.fork() != 0:
|
|
880
|
+
os._exit(0)
|
|
881
|
+
null = os.open("/dev/null", os.O_RDWR)
|
|
882
|
+
for fd in (0, 1, 2):
|
|
883
|
+
os.dup2(null, fd)
|
|
884
|
+
bg_recorder(directory, secret, token, record, ready_w)
|
|
885
|
+
finally:
|
|
886
|
+
os._exit(0)
|
|
887
|
+
os.close(ready_w)
|
|
888
|
+
os.waitpid(pid, 0)
|
|
889
|
+
ready = os.read(ready_r, 1)
|
|
890
|
+
os.close(ready_r)
|
|
891
|
+
if ready != b"R":
|
|
892
|
+
raise Refused("INVALID_REQUEST", "The background recorder did not start")
|
|
893
|
+
return {"prepared": True, "generation": record["generation"]}
|
|
894
|
+
|
|
895
|
+
|
|
896
|
+
def peer(conn):
|
|
897
|
+
"""The connecting process's (pid, uid) from SO_PEERCRED, fixed by the kernel at connect."""
|
|
898
|
+
data = conn.getsockopt(socket.SOL_SOCKET, socket.SO_PEERCRED, 12)
|
|
899
|
+
pid, uid, _ = struct.unpack("3i", data)
|
|
900
|
+
return pid, uid
|
|
901
|
+
|
|
902
|
+
|
|
903
|
+
def is_supervisor(pid):
|
|
904
|
+
"""The process runs this agent's bg-start exactly as the provider calls it (the supervisor
|
|
905
|
+
it forks keeps that argv). A same-uid process can run its own code under this argv, so this
|
|
906
|
+
is a filter; the token and the request digest bind the real agent to the admitted request."""
|
|
907
|
+
try:
|
|
908
|
+
with open("/proc/%d/cmdline" % pid, "rb") as handle:
|
|
909
|
+
args = handle.read().split(b"\\0")
|
|
910
|
+
except OSError:
|
|
911
|
+
return False
|
|
912
|
+
return args == [os.fsencode(PYTHON), b"-I", os.fsencode(os.path.abspath(__file__)),
|
|
913
|
+
b"bg-start", b""]
|
|
914
|
+
|
|
915
|
+
|
|
916
|
+
def bg_recorder(directory, secret, token, record, ready_w):
|
|
917
|
+
"""(root, detached) Appends output and records the supervisor's identity and exit. The
|
|
918
|
+
channel is a root-owned UNIX socket: frames count only on the one connection whose
|
|
919
|
+
SO_PEERCRED pid is the supervisor that identified itself (pid, start time, argv, token and
|
|
920
|
+
the digest of the request it runs, which must be the admitted one); no other peer, and no
|
|
921
|
+
writer that reopens a descriptor, can add frames to it."""
|
|
922
|
+
import select as select_module
|
|
923
|
+
os.chdir(directory) # bind relative: the absolute path would exceed sun_path
|
|
924
|
+
listener = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
|
|
925
|
+
listener.bind(secret)
|
|
926
|
+
os.chmod(secret, 0o622) # the sandbox user may connect by its exact name only
|
|
927
|
+
listener.listen(8)
|
|
928
|
+
spool = os.open("output", os.O_WRONLY | os.O_CREAT | os.O_EXCL | os.O_APPEND | os.O_CLOEXEC,
|
|
929
|
+
0o600)
|
|
930
|
+
path = os.path.join(directory, "record.json")
|
|
931
|
+
os.write(ready_w, b"R")
|
|
932
|
+
os.close(ready_w)
|
|
933
|
+
cap = record["spoolCapBytes"]
|
|
934
|
+
written = 0
|
|
935
|
+
bound = None
|
|
936
|
+
buffer = bytearray()
|
|
937
|
+
pending = {} # unverified connections: fd -> (socket, buffer, deadline)
|
|
938
|
+
connect_by = time.monotonic() + 120
|
|
939
|
+
poller = select_module.poll()
|
|
940
|
+
poller.register(listener.fileno(), select_module.POLLIN)
|
|
941
|
+
|
|
942
|
+
def drop(fd):
|
|
943
|
+
conn = pending.pop(fd, (None,))[0]
|
|
944
|
+
if conn is not None:
|
|
945
|
+
poller.unregister(fd)
|
|
946
|
+
conn.close()
|
|
947
|
+
try:
|
|
948
|
+
while True:
|
|
949
|
+
events = dict(poller.poll(1000))
|
|
950
|
+
if listener.fileno() in events:
|
|
951
|
+
conn, _ = listener.accept()
|
|
952
|
+
if len(pending) >= 8:
|
|
953
|
+
conn.close()
|
|
954
|
+
else:
|
|
955
|
+
# Also after the bind: a later valid identity (one supervisor per execution
|
|
956
|
+
# binds) marks the record contested, since one of the two is not genuine.
|
|
957
|
+
conn.setblocking(False)
|
|
958
|
+
pending[conn.fileno()] = (conn, bytearray(), time.monotonic() + 10)
|
|
959
|
+
poller.register(conn.fileno(), select_module.POLLIN)
|
|
960
|
+
for fd in list(pending):
|
|
961
|
+
conn, data, deadline = pending[fd]
|
|
962
|
+
if fd in events:
|
|
963
|
+
try:
|
|
964
|
+
chunk = conn.recv(4096)
|
|
965
|
+
except BlockingIOError:
|
|
966
|
+
chunk = None
|
|
967
|
+
if chunk == b"":
|
|
968
|
+
drop(fd)
|
|
969
|
+
continue
|
|
970
|
+
if chunk:
|
|
971
|
+
data += chunk
|
|
972
|
+
if len(data) >= 5 and data[0:1] == b"P":
|
|
973
|
+
size = int.from_bytes(data[1:5], "big")
|
|
974
|
+
if size > 4096:
|
|
975
|
+
drop(fd)
|
|
976
|
+
continue
|
|
977
|
+
if len(data) < 5 + size:
|
|
978
|
+
continue
|
|
979
|
+
try:
|
|
980
|
+
identity = json.loads(bytes(data[5:5 + size]))
|
|
981
|
+
claimed, start = int(identity["pid"]), int(identity["start"])
|
|
982
|
+
offered = str(identity["token"]).encode()
|
|
983
|
+
digest = str(identity["digest"]).encode()
|
|
984
|
+
pid, uid = peer(conn)
|
|
985
|
+
except (ValueError, KeyError, OSError, struct.error):
|
|
986
|
+
drop(fd)
|
|
987
|
+
continue
|
|
988
|
+
cancelled = record.get("opKey") and os.path.exists(
|
|
989
|
+
os.path.join(OPS_ROOT, record["opKey"] + ".cancelled"))
|
|
990
|
+
valid = hmac.compare_digest(offered, token.encode()) \\
|
|
991
|
+
and hmac.compare_digest(digest, record["startDigest"].encode()) \\
|
|
992
|
+
and pid == claimed and uid != 0 and proc_start(pid) == start \\
|
|
993
|
+
and is_supervisor(pid)
|
|
994
|
+
if valid and bound is not None and not record.get("contested"):
|
|
995
|
+
# Two supervisors presented the token and the admitted digest: the bound
|
|
996
|
+
# one may be a forgery. The provider stops a contested start whose
|
|
997
|
+
# acknowledgement it did not receive.
|
|
998
|
+
record = dict(record, contested=True)
|
|
999
|
+
write_json(path, record)
|
|
1000
|
+
if not valid or bound is not None or record["state"] != "starting" \\
|
|
1001
|
+
or cancelled:
|
|
1002
|
+
# Refused (or a stop already cancelled this execution): close without
|
|
1003
|
+
# the acceptance byte, so that supervisor never acknowledges or launches.
|
|
1004
|
+
drop(fd)
|
|
1005
|
+
continue
|
|
1006
|
+
pending.pop(fd)
|
|
1007
|
+
try:
|
|
1008
|
+
conn.send(b"A") # acceptance: only now may the supervisor proceed
|
|
1009
|
+
except OSError:
|
|
1010
|
+
write_json(path, dict(record, state="lost"))
|
|
1011
|
+
return
|
|
1012
|
+
bound = conn
|
|
1013
|
+
buffer = bytearray(data[5 + size:])
|
|
1014
|
+
record = dict(record, state="running", pid=pid, start=start,
|
|
1015
|
+
boundDigest=digest.decode())
|
|
1016
|
+
write_json(path, record)
|
|
1017
|
+
elif len(data) >= 1 and data[0:1] != b"P" or time.monotonic() > deadline:
|
|
1018
|
+
drop(fd)
|
|
1019
|
+
if bound is not None and bound.fileno() in events:
|
|
1020
|
+
try:
|
|
1021
|
+
chunk = bound.recv(CHUNK + 5)
|
|
1022
|
+
except BlockingIOError:
|
|
1023
|
+
chunk = None
|
|
1024
|
+
if chunk == b"":
|
|
1025
|
+
# The supervisor closed its channel without reporting an exit.
|
|
1026
|
+
write_json(path, dict(record, state="lost"))
|
|
1027
|
+
return
|
|
1028
|
+
if chunk:
|
|
1029
|
+
buffer += chunk
|
|
1030
|
+
while bound is not None and len(buffer) >= 5:
|
|
1031
|
+
size = int.from_bytes(buffer[1:5], "big")
|
|
1032
|
+
if size > CHUNK or buffer[0:1] not in (b"O", b"X"):
|
|
1033
|
+
write_json(path, dict(record, state="lost"))
|
|
1034
|
+
return
|
|
1035
|
+
if len(buffer) < 5 + size:
|
|
1036
|
+
break
|
|
1037
|
+
kind, body = bytes(buffer[0:1]), bytes(buffer[5:5 + size])
|
|
1038
|
+
del buffer[:5 + size]
|
|
1039
|
+
if kind == b"O":
|
|
1040
|
+
keep = body[:max(cap - written, 0)]
|
|
1041
|
+
if keep:
|
|
1042
|
+
write_all(spool, keep)
|
|
1043
|
+
written += len(keep)
|
|
1044
|
+
if len(keep) < len(body) and not record.get("outputTruncated"):
|
|
1045
|
+
record = dict(record, outputTruncated=True)
|
|
1046
|
+
write_json(path, record)
|
|
1047
|
+
else:
|
|
1048
|
+
result = json.loads(body)
|
|
1049
|
+
record = dict(record, state="exited", exitCode=result.get("exitCode"),
|
|
1050
|
+
timedOut=bool(result.get("timedOut")),
|
|
1051
|
+
cancelled=bool(result.get("cancelled")),
|
|
1052
|
+
refused=result.get("refused"))
|
|
1053
|
+
write_json(path, record)
|
|
1054
|
+
return
|
|
1055
|
+
if record["state"] == "starting" and (time.monotonic() > connect_by or (
|
|
1056
|
+
record.get("opKey") and os.path.exists(
|
|
1057
|
+
os.path.join(OPS_ROOT, record["opKey"] + ".cancelled")))):
|
|
1058
|
+
# No supervisor in time, or the start was cancelled: nothing may bind any more.
|
|
1059
|
+
write_json(path, dict(record, state="lost"))
|
|
1060
|
+
return
|
|
1061
|
+
if record["state"] == "running" and not events and \\
|
|
1062
|
+
proc_start(record["pid"]) != record["start"]:
|
|
1063
|
+
write_json(path, dict(record, state="lost"))
|
|
1064
|
+
return
|
|
1065
|
+
finally:
|
|
1066
|
+
try:
|
|
1067
|
+
os.unlink(os.path.join(directory, secret))
|
|
1068
|
+
except OSError:
|
|
1069
|
+
pass
|
|
1070
|
+
|
|
1071
|
+
|
|
1072
|
+
def open_output(request):
|
|
1073
|
+
"""Opens the requested output artifact inside the scope (pinned roots, no-follow)."""
|
|
1074
|
+
native = load(NATIVE, "capybara_native_files")
|
|
1075
|
+
pinned = []
|
|
1076
|
+
try:
|
|
1077
|
+
if request["kind"] == "device":
|
|
1078
|
+
fd = os.open("/", os.O_RDONLY | os.O_DIRECTORY | os.O_CLOEXEC)
|
|
1079
|
+
pinned.append({"fd": fd})
|
|
1080
|
+
roots = {"read": [], "write": [{"path": "/", "fd": fd}]}
|
|
1081
|
+
else:
|
|
1082
|
+
raise Refused("UNAVAILABLE", "Restricted execution is not supported on this VM")
|
|
1083
|
+
if protected(request["outputPath"], True):
|
|
1084
|
+
raise Refused("SCOPE_DENIED", "The output path is reserved for the provider")
|
|
1085
|
+
try:
|
|
1086
|
+
out = native.NativeFiles(roots).open(request["outputPath"], write=True)
|
|
1087
|
+
except (native.ScopeDenied, OSError, ValueError):
|
|
1088
|
+
raise Refused("SCOPE_DENIED", "The output path is outside the operation scope")
|
|
1089
|
+
try:
|
|
1090
|
+
if not stat.S_ISREG(os.fstat(out).st_mode):
|
|
1091
|
+
raise Refused("SCOPE_DENIED", "The output path is not a regular file")
|
|
1092
|
+
os.ftruncate(out, 0)
|
|
1093
|
+
os.set_inheritable(out, False)
|
|
1094
|
+
except BaseException:
|
|
1095
|
+
os.close(out)
|
|
1096
|
+
raise
|
|
1097
|
+
return out
|
|
1098
|
+
finally:
|
|
1099
|
+
close_pinned(pinned)
|
|
1100
|
+
|
|
1101
|
+
|
|
1102
|
+
class Channel:
|
|
1103
|
+
"""The supervisor's side of the recorder socket: whole frames under one lock."""
|
|
1104
|
+
|
|
1105
|
+
def __init__(self, fd):
|
|
1106
|
+
self.fd = fd
|
|
1107
|
+
self.lock = threading.Lock()
|
|
1108
|
+
|
|
1109
|
+
def send(self, kind, data):
|
|
1110
|
+
with self.lock:
|
|
1111
|
+
try:
|
|
1112
|
+
for at in range(0, max(len(data), 1), CHUNK):
|
|
1113
|
+
part = data[at:at + CHUNK]
|
|
1114
|
+
write_all(self.fd, kind + len(part).to_bytes(4, "big") + part)
|
|
1115
|
+
except OSError:
|
|
1116
|
+
pass # A lost recorder records the execution as lost; the work continues.
|
|
1117
|
+
|
|
1118
|
+
|
|
1119
|
+
def bg_start(request):
|
|
1120
|
+
"""(sandbox user) Starts the detached supervisor; acknowledges once it is recorded. The
|
|
1121
|
+
process turns undumpable before it reads the request, so once it runs other processes of the
|
|
1122
|
+
same user cannot reopen its descriptors through /proc; during interpreter start-up they can
|
|
1123
|
+
(stdin included), which the request digest and the provider's stop on failure cover."""
|
|
1124
|
+
admit(request)
|
|
1125
|
+
verify_runtime(request["expect"])
|
|
1126
|
+
address = os.path.join(bg_directory(request["executionId"], os.path.join(OPS_VIEW, "bg")),
|
|
1127
|
+
request["secret"])
|
|
1128
|
+
if not SECRET.fullmatch(request["secret"]):
|
|
1129
|
+
raise ValueError("invalid channel name")
|
|
1130
|
+
out = open_output(request)
|
|
1131
|
+
ready_r, ready_w = os.pipe()
|
|
1132
|
+
pid = os.fork()
|
|
1133
|
+
if pid == 0:
|
|
1134
|
+
try:
|
|
1135
|
+
os.close(ready_r)
|
|
1136
|
+
os.setsid()
|
|
1137
|
+
if os.fork() != 0:
|
|
1138
|
+
os._exit(0)
|
|
1139
|
+
null = os.open("/dev/null", os.O_RDWR)
|
|
1140
|
+
for fd in (0, 1, 2):
|
|
1141
|
+
os.dup2(null, fd)
|
|
1142
|
+
# The supervisor itself connects, so the recorder's SO_PEERCRED names exactly it.
|
|
1143
|
+
connection = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
|
|
1144
|
+
connection.connect(address)
|
|
1145
|
+
bg_supervise(request, Channel(connection.detach()), out, ready_w)
|
|
1146
|
+
finally:
|
|
1147
|
+
os._exit(0)
|
|
1148
|
+
os.close(ready_w)
|
|
1149
|
+
os.close(out)
|
|
1150
|
+
os.waitpid(pid, 0)
|
|
1151
|
+
ready = os.read(ready_r, 1)
|
|
1152
|
+
os.close(ready_r)
|
|
1153
|
+
if ready != b"R":
|
|
1154
|
+
raise Refused("INVALID_REQUEST", "The background supervisor did not start")
|
|
1155
|
+
finish({"started": True})
|
|
1156
|
+
|
|
1157
|
+
|
|
1158
|
+
def unblock():
|
|
1159
|
+
signal.pthread_sigmask(signal.SIG_UNBLOCK, CANCEL_SIGNALS)
|
|
1160
|
+
|
|
1161
|
+
|
|
1162
|
+
def bg_supervise(request, channel, out, ready_w):
|
|
1163
|
+
"""The detached supervisor: blocks cancellation, reports its identity to the recorder before
|
|
1164
|
+
acknowledging, re-checks admission, then launches with its handlers in place."""
|
|
1165
|
+
signal.pthread_sigmask(signal.SIG_BLOCK, CANCEL_SIGNALS)
|
|
1166
|
+
me = os.getpid()
|
|
1167
|
+
# The digest of the request this supervisor runs: the recorder accepts only the admitted one.
|
|
1168
|
+
channel.send(b"P", json.dumps({"pid": me, "start": proc_start(me), "token": request["token"],
|
|
1169
|
+
"digest": start_digest(request)}).encode())
|
|
1170
|
+
# Fail closed: without the recorder's acceptance on this very connection (another peer was
|
|
1171
|
+
# bound, or the execution was cancelled), never acknowledge and never launch.
|
|
1172
|
+
import select as select_module
|
|
1173
|
+
ready, _, _ = select_module.select([channel.fd], [], [], 30)
|
|
1174
|
+
try:
|
|
1175
|
+
accepted = os.read(channel.fd, 1) if ready else b""
|
|
1176
|
+
except OSError:
|
|
1177
|
+
accepted = b""
|
|
1178
|
+
if accepted != b"A":
|
|
1179
|
+
os._exit(0)
|
|
1180
|
+
os.write(ready_w, b"R")
|
|
1181
|
+
os.close(ready_w)
|
|
1182
|
+
|
|
1183
|
+
def sink(data):
|
|
1184
|
+
channel.send(b"O", data)
|
|
1185
|
+
try:
|
|
1186
|
+
write_all(out, data)
|
|
1187
|
+
except OSError:
|
|
1188
|
+
pass
|
|
1189
|
+
|
|
1190
|
+
def done(result):
|
|
1191
|
+
events = result.get("events", [])
|
|
1192
|
+
exit_event = next((e for e in events if e.get("event") == "exit"), None) or {}
|
|
1193
|
+
refused = next((e for e in events if e.get("event") == "refused"), None)
|
|
1194
|
+
code = exit_event.get("exitCode")
|
|
1195
|
+
if isinstance(code, int) and code < 0:
|
|
1196
|
+
code = 128 - code
|
|
1197
|
+
timed_out = bool(exit_event.get("timedOut"))
|
|
1198
|
+
channel.send(b"X", json.dumps({
|
|
1199
|
+
"exitCode": None if timed_out or refused else code, "timedOut": timed_out,
|
|
1200
|
+
"cancelled": bool(result.get("cancelled")),
|
|
1201
|
+
"refused": refused.get("code") if refused else None}).encode())
|
|
1202
|
+
try:
|
|
1203
|
+
# A revocation or cancellation recorded after the start acknowledgement still wins.
|
|
1204
|
+
admit(request)
|
|
1205
|
+
if request["kind"] != "device":
|
|
1206
|
+
raise Refused("UNAVAILABLE", "Restricted execution is not supported on this VM")
|
|
1207
|
+
full_supervise(request, sink, done)
|
|
1208
|
+
except Refused as error:
|
|
1209
|
+
done({"events": [{"event": "refused", "code": error.code}], "cancelled": False})
|
|
1210
|
+
except Exception:
|
|
1211
|
+
done({"events": [{"event": "refused", "code": "INVALID_REQUEST"}], "cancelled": False})
|
|
1212
|
+
|
|
1213
|
+
|
|
1214
|
+
def full_supervise(request, sink, done):
|
|
1215
|
+
"""Full access in the background: the existing OS user and environment, no restricted filter;
|
|
1216
|
+
the owned process group ends on stop, timeout and exit, and an in-sandbox time limit bounds
|
|
1217
|
+
it even if this supervisor is lost."""
|
|
1218
|
+
env = dict(os.environ)
|
|
1219
|
+
env.update(request.get("env") or {})
|
|
1220
|
+
cancelled = []
|
|
1221
|
+
seconds = str(int(request["timeoutMs"] / 1000) + 6)
|
|
1222
|
+
child = subprocess.Popen(["/usr/bin/timeout", "-s", "KILL", seconds, "/bin/bash", "-c",
|
|
1223
|
+
request["command"]], cwd=request.get("cwd") or None, env=env,
|
|
1224
|
+
stdin=subprocess.DEVNULL, stdout=subprocess.PIPE,
|
|
1225
|
+
stderr=subprocess.STDOUT, start_new_session=True, preexec_fn=unblock)
|
|
1226
|
+
|
|
1227
|
+
def cancel(signum, _frame):
|
|
1228
|
+
cancelled.append(signum)
|
|
1229
|
+
try:
|
|
1230
|
+
os.killpg(child.pid, signal.SIGKILL)
|
|
1231
|
+
except OSError:
|
|
1232
|
+
pass
|
|
1233
|
+
for number in CANCEL_SIGNALS:
|
|
1234
|
+
signal.signal(number, cancel)
|
|
1235
|
+
# A stop that arrived while blocked is delivered now, with the child known.
|
|
1236
|
+
unblock()
|
|
1237
|
+
|
|
1238
|
+
def copy():
|
|
1239
|
+
while True:
|
|
1240
|
+
data = child.stdout.read1(CHUNK)
|
|
1241
|
+
if not data:
|
|
1242
|
+
return
|
|
1243
|
+
sink(data)
|
|
1244
|
+
reader = threading.Thread(target=copy, daemon=True)
|
|
1245
|
+
reader.start()
|
|
1246
|
+
timed_out = False
|
|
1247
|
+
try:
|
|
1248
|
+
code = child.wait(timeout=request["timeoutMs"] / 1000)
|
|
1249
|
+
except subprocess.TimeoutExpired:
|
|
1250
|
+
timed_out = True
|
|
1251
|
+
try:
|
|
1252
|
+
os.killpg(child.pid, signal.SIGKILL)
|
|
1253
|
+
except OSError:
|
|
1254
|
+
pass
|
|
1255
|
+
code = child.wait()
|
|
1256
|
+
try:
|
|
1257
|
+
os.killpg(child.pid, signal.SIGKILL)
|
|
1258
|
+
except OSError:
|
|
1259
|
+
pass
|
|
1260
|
+
# A detached descendant may keep the pipe; the record does not wait for it.
|
|
1261
|
+
reader.join(5)
|
|
1262
|
+
done({"events": [{"event": "exit", "exitCode": None if timed_out or cancelled else code,
|
|
1263
|
+
"timedOut": timed_out}], "cancelled": bool(cancelled)})
|
|
1264
|
+
|
|
1265
|
+
|
|
1266
|
+
def bg_lookup(request, any_generation=False):
|
|
1267
|
+
directory = bg_directory(request["executionId"])
|
|
1268
|
+
record = read_json(os.path.join(directory, "record.json"))
|
|
1269
|
+
if record is None:
|
|
1270
|
+
return directory, None
|
|
1271
|
+
if not any_generation and record.get("generation") != request.get("generation"):
|
|
1272
|
+
return None, None
|
|
1273
|
+
stop = read_json(os.path.join(directory, "stop.json"))
|
|
1274
|
+
bound = record.get("state") in ("running", "exited")
|
|
1275
|
+
if (stop is not None and stop.get("aborted")) or (bound and not hmac.compare_digest(
|
|
1276
|
+
str(record.get("boundDigest", "")).encode(),
|
|
1277
|
+
str(record.get("startDigest", "-")).encode())):
|
|
1278
|
+
# A start the provider aborted (refused or never acknowledged), or a record bound to any
|
|
1279
|
+
# request but the admitted one: never reported as the task, its spool never served.
|
|
1280
|
+
record = dict(record, state="lost", withheld=True)
|
|
1281
|
+
return directory, record
|
|
1282
|
+
|
|
1283
|
+
|
|
1284
|
+
def bg_state(record):
|
|
1285
|
+
if record is None:
|
|
1286
|
+
return {"state": "lost"}
|
|
1287
|
+
state = record.get("state")
|
|
1288
|
+
if state in ("starting", "running"):
|
|
1289
|
+
if state == "running" and proc_start(record["pid"]) != record["start"]:
|
|
1290
|
+
return {"state": "lost"}
|
|
1291
|
+
return {"state": "running"}
|
|
1292
|
+
if state == "exited":
|
|
1293
|
+
return {"state": "exited", "exitCode": record.get("exitCode"),
|
|
1294
|
+
"timedOut": bool(record.get("timedOut"))}
|
|
1295
|
+
return {"state": "lost"}
|
|
1296
|
+
|
|
1297
|
+
|
|
1298
|
+
def bg_status(request):
|
|
1299
|
+
_, record = bg_lookup(request)
|
|
1300
|
+
return bg_state(record)
|
|
1301
|
+
|
|
1302
|
+
|
|
1303
|
+
def bg_contested(request):
|
|
1304
|
+
"""(root) Whether two supervisors presented this execution's token and admitted digest."""
|
|
1305
|
+
_, record = bg_lookup(request)
|
|
1306
|
+
return {"contested": bool(record is not None and record.get("contested"))}
|
|
1307
|
+
|
|
1308
|
+
|
|
1309
|
+
def bg_expired(record):
|
|
1310
|
+
return record is not None and record.get("state") == "exited" \\
|
|
1311
|
+
and time.time() * 1000 > record.get("retainUntilMs", 0)
|
|
1312
|
+
|
|
1313
|
+
|
|
1314
|
+
def bg_output(request, tail):
|
|
1315
|
+
directory, record = bg_lookup(request)
|
|
1316
|
+
if directory is None:
|
|
1317
|
+
if tail:
|
|
1318
|
+
return {"unavailable": "lost"}
|
|
1319
|
+
raise Refused("SCOPE_DENIED", "The reference names another instance generation")
|
|
1320
|
+
if bg_expired(record):
|
|
1321
|
+
return {"unavailable": "expired"}
|
|
1322
|
+
if record is None or record.get("withheld"):
|
|
1323
|
+
return {"unavailable": "lost"}
|
|
1324
|
+
try:
|
|
1325
|
+
fd = os.open(os.path.join(directory, "output"), os.O_RDONLY | os.O_NOFOLLOW | os.O_CLOEXEC)
|
|
1326
|
+
except OSError:
|
|
1327
|
+
return {"unavailable": "lost"}
|
|
1328
|
+
try:
|
|
1329
|
+
total = os.fstat(fd).st_size
|
|
1330
|
+
if tail:
|
|
1331
|
+
want = min(request["maxBytes"], total)
|
|
1332
|
+
data = os.pread(fd, want, total - want)
|
|
1333
|
+
truncated = total > want
|
|
1334
|
+
cut = 0
|
|
1335
|
+
while cut < len(data) and cut < 4 and data[cut] & 0xC0 == 0x80:
|
|
1336
|
+
cut += 1
|
|
1337
|
+
return {"dataB64": base64.b64encode(data[cut:]).decode(), "truncated": truncated}
|
|
1338
|
+
offset = request["offset"]
|
|
1339
|
+
want = min(request["maxBytes"], max(total - offset, 0))
|
|
1340
|
+
data = b""
|
|
1341
|
+
while len(data) < want:
|
|
1342
|
+
chunk = os.pread(fd, want - len(data), offset + len(data))
|
|
1343
|
+
if not chunk:
|
|
1344
|
+
return {"unavailable": "lost"}
|
|
1345
|
+
data += chunk
|
|
1346
|
+
return {"dataB64": base64.b64encode(data).decode(), "offset": offset,
|
|
1347
|
+
"nextOffset": offset + len(data), "totalBytes": total}
|
|
1348
|
+
finally:
|
|
1349
|
+
os.close(fd)
|
|
1350
|
+
|
|
1351
|
+
|
|
1352
|
+
def bg_stop_plan(request):
|
|
1353
|
+
"""(root) The claimed outcome, or the recorded identity the stop must signal."""
|
|
1354
|
+
directory, record = bg_lookup(request, request.get("anyGeneration", False))
|
|
1355
|
+
if directory is None or record is None:
|
|
1356
|
+
return {"outcome": {"outcome": "lost"}}
|
|
1357
|
+
prior = read_json(os.path.join(directory, "stop.json"))
|
|
1358
|
+
if prior is not None:
|
|
1359
|
+
return {"outcome": prior}
|
|
1360
|
+
deadline = time.monotonic() + 5
|
|
1361
|
+
while record is not None and record.get("state") == "starting" and time.monotonic() < deadline:
|
|
1362
|
+
time.sleep(0.02)
|
|
1363
|
+
record = read_json(os.path.join(directory, "record.json"))
|
|
1364
|
+
state = bg_state(record)
|
|
1365
|
+
if state["state"] == "running" and record.get("state") == "running":
|
|
1366
|
+
return {"signal": {"pid": record["pid"], "start": record["start"]}}
|
|
1367
|
+
return {"state": state}
|
|
1368
|
+
|
|
1369
|
+
|
|
1370
|
+
def bg_signal(request):
|
|
1371
|
+
"""(sandbox user) Ends one recorded supervisor: SIGTERM, then SIGKILL; never a reused pid."""
|
|
1372
|
+
pid, start = int(request["pid"]), int(request["start"])
|
|
1373
|
+
if proc_start(pid) != start:
|
|
1374
|
+
return {"signalled": False}
|
|
1375
|
+
try:
|
|
1376
|
+
handle = os.pidfd_open(pid)
|
|
1377
|
+
except OSError:
|
|
1378
|
+
return {"signalled": False}
|
|
1379
|
+
try:
|
|
1380
|
+
if proc_start(pid) != start:
|
|
1381
|
+
return {"signalled": False}
|
|
1382
|
+
signal.pidfd_send_signal(handle, signal.SIGTERM)
|
|
1383
|
+
deadline = time.monotonic() + min(request.get("graceMs", 0) / 1000, 5) + 6
|
|
1384
|
+
while proc_start(pid) == start and time.monotonic() < deadline:
|
|
1385
|
+
time.sleep(0.02)
|
|
1386
|
+
if proc_start(pid) == start:
|
|
1387
|
+
signal.pidfd_send_signal(handle, signal.SIGKILL)
|
|
1388
|
+
return {"signalled": True}
|
|
1389
|
+
except OSError:
|
|
1390
|
+
return {"signalled": False}
|
|
1391
|
+
finally:
|
|
1392
|
+
os.close(handle)
|
|
1393
|
+
|
|
1394
|
+
|
|
1395
|
+
def bg_stop_claim(request):
|
|
1396
|
+
"""(root) Atomically claims the terminal outcome; a retry returns the same outcome."""
|
|
1397
|
+
directory, record = bg_lookup(request, request.get("anyGeneration", False))
|
|
1398
|
+
if directory is None or record is None:
|
|
1399
|
+
return {"outcome": "lost"}
|
|
1400
|
+
stop_path = os.path.join(directory, "stop.json")
|
|
1401
|
+
prior = read_json(stop_path)
|
|
1402
|
+
if prior is not None:
|
|
1403
|
+
return prior
|
|
1404
|
+
deadline = time.monotonic() + 5
|
|
1405
|
+
while record is not None and record.get("state") in ("starting", "running") \\
|
|
1406
|
+
and time.monotonic() < deadline:
|
|
1407
|
+
time.sleep(0.02)
|
|
1408
|
+
record = read_json(os.path.join(directory, "record.json"))
|
|
1409
|
+
if record is not None and record.get("state") == "starting" and record.get("opKey"):
|
|
1410
|
+
# No supervisor reported in time: a late one must not launch after this claim.
|
|
1411
|
+
marker({"opKey": record["opKey"], "expiresAtMs": record.get("expiresAtMs", 0)},
|
|
1412
|
+
".cancelled")
|
|
1413
|
+
record = dict(record, state="lost")
|
|
1414
|
+
write_json(os.path.join(directory, "record.json"), record)
|
|
1415
|
+
state = bg_state(record)
|
|
1416
|
+
if record is not None and record.get("state") == "exited" and not record.get("cancelled") \\
|
|
1417
|
+
and not request.get("signalled"):
|
|
1418
|
+
outcome = {"outcome": "already-exited", "exitCode": state.get("exitCode"),
|
|
1419
|
+
"timedOut": state.get("timedOut", False)}
|
|
1420
|
+
elif request.get("signalled") or (record is not None and record.get("cancelled")):
|
|
1421
|
+
outcome = {"outcome": "stopped"}
|
|
1422
|
+
elif state["state"] == "exited":
|
|
1423
|
+
outcome = {"outcome": "already-exited", "exitCode": state.get("exitCode"),
|
|
1424
|
+
"timedOut": state.get("timedOut", False)}
|
|
1425
|
+
else:
|
|
1426
|
+
outcome = {"outcome": "lost"}
|
|
1427
|
+
if request.get("aborted"):
|
|
1428
|
+
# The provider aborts a start it did not see acknowledged: from now on the reference
|
|
1429
|
+
# reports the execution as lost and serves none of its output.
|
|
1430
|
+
outcome = dict(outcome, aborted=True)
|
|
1431
|
+
try:
|
|
1432
|
+
write_new(stop_path, outcome, 0o600)
|
|
1433
|
+
except FileExistsError:
|
|
1434
|
+
return read_json(stop_path) or outcome
|
|
1435
|
+
return outcome
|
|
1436
|
+
|
|
1437
|
+
|
|
1438
|
+
# ---- durable records (root) ---------------------------------------------------------------
|
|
1439
|
+
|
|
1440
|
+
def generation():
|
|
1441
|
+
with open("/proc/sys/kernel/random/boot_id") as handle:
|
|
1442
|
+
boot = handle.read().strip()
|
|
1443
|
+
with open("/proc/1/stat") as handle:
|
|
1444
|
+
data = handle.read()
|
|
1445
|
+
started = data[data.rindex(")") + 2:].split()[19]
|
|
1446
|
+
return hashlib.sha256(("%s/%s" % (boot, started)).encode()).hexdigest()[:24]
|
|
1447
|
+
|
|
1448
|
+
|
|
1449
|
+
def records():
|
|
1450
|
+
"""(root) The record directory on the VM's persistent disk: the sandbox user can neither
|
|
1451
|
+
create, list nor change it (a planted path is refused, never trusted)."""
|
|
1452
|
+
for path in (OPS_ROOT, os.path.join(OPS_ROOT, "bg")):
|
|
1453
|
+
try:
|
|
1454
|
+
os.mkdir(path, 0o711)
|
|
1455
|
+
except FileExistsError:
|
|
1456
|
+
pass
|
|
1457
|
+
info = os.lstat(path)
|
|
1458
|
+
if not stat.S_ISDIR(info.st_mode) or info.st_uid != 0 or info.st_mode & 0o7777 != 0o711:
|
|
1459
|
+
raise Refused("UNAVAILABLE", "The operation record directory is not trusted")
|
|
1460
|
+
now = time.time() * 1000
|
|
1461
|
+
for name in os.listdir(OPS_ROOT):
|
|
1462
|
+
path = os.path.join(OPS_ROOT, name)
|
|
1463
|
+
if name == "bg":
|
|
1464
|
+
continue
|
|
1465
|
+
try:
|
|
1466
|
+
with open(path) as handle:
|
|
1467
|
+
expires = json.load(handle).get("expiresAtMs", 0)
|
|
1468
|
+
if now > expires + 3600_000:
|
|
1469
|
+
os.unlink(path)
|
|
1470
|
+
except (OSError, ValueError):
|
|
1471
|
+
pass
|
|
1472
|
+
base = os.path.join(OPS_ROOT, "bg")
|
|
1473
|
+
for name in os.listdir(base):
|
|
1474
|
+
record = read_json(os.path.join(base, name, "record.json"))
|
|
1475
|
+
stale = record is not None and (record.get("state") in ("exited", "lost")
|
|
1476
|
+
or record.get("generation") != generation())
|
|
1477
|
+
if stale and now > record.get("expiresAtMs", 0):
|
|
1478
|
+
shutil.rmtree(os.path.join(base, name), ignore_errors=True)
|
|
1479
|
+
|
|
1480
|
+
|
|
1481
|
+
def write_new(path, record, mode):
|
|
1482
|
+
fd = os.open(path, os.O_WRONLY | os.O_CREAT | os.O_EXCL | os.O_NOFOLLOW, mode)
|
|
1483
|
+
with os.fdopen(fd, "w") as handle:
|
|
1484
|
+
json.dump(record, handle)
|
|
1485
|
+
|
|
1486
|
+
|
|
1487
|
+
def consume(request):
|
|
1488
|
+
records()
|
|
1489
|
+
key = request["opKey"]
|
|
1490
|
+
if os.path.exists(os.path.join(OPS_ROOT, key + ".revoked")):
|
|
1491
|
+
return {"state": "revoked"}
|
|
1492
|
+
path = os.path.join(OPS_ROOT, key + ".json")
|
|
1493
|
+
record = {"generation": generation(), "method": request["method"],
|
|
1494
|
+
"inputHash": request["inputHash"], "expiresAtMs": request["expiresAtMs"]}
|
|
1495
|
+
if time.time() * 1000 > request["admitBeforeMs"]:
|
|
1496
|
+
if os.path.exists(path):
|
|
1497
|
+
return lookup(path, record)
|
|
1498
|
+
return {"state": "expired"}
|
|
1499
|
+
try:
|
|
1500
|
+
write_new(path, record, 0o600)
|
|
1501
|
+
return {"state": "new", "generation": record["generation"]}
|
|
1502
|
+
except FileExistsError:
|
|
1503
|
+
return lookup(path, record)
|
|
1504
|
+
|
|
1505
|
+
|
|
1506
|
+
def lookup(path, record):
|
|
1507
|
+
with open(path) as handle:
|
|
1508
|
+
existing = json.load(handle)
|
|
1509
|
+
# The same method and input is a duplicate in any generation (an unknown outcome, never a
|
|
1510
|
+
# re-run); a different method or input under the ID is invalid.
|
|
1511
|
+
if (existing.get("method"), existing.get("inputHash")) != (
|
|
1512
|
+
record["method"], record["inputHash"]):
|
|
1513
|
+
return {"state": "mismatch"}
|
|
1514
|
+
return {"state": "duplicate", "record": existing}
|
|
1515
|
+
|
|
1516
|
+
|
|
1517
|
+
def marker(request, suffix):
|
|
1518
|
+
"""(root) A tombstone or cancellation marker the sandbox-user agent checks before launching."""
|
|
1519
|
+
records()
|
|
1520
|
+
try:
|
|
1521
|
+
write_new(os.path.join(OPS_ROOT, request["opKey"] + suffix),
|
|
1522
|
+
{"expiresAtMs": request["expiresAtMs"]}, 0o644)
|
|
1523
|
+
except FileExistsError:
|
|
1524
|
+
pass
|
|
1525
|
+
return {"state": "revoked" if suffix == ".revoked" else "cancelled",
|
|
1526
|
+
"generation": generation()}
|
|
1527
|
+
|
|
1528
|
+
|
|
1529
|
+
# ---- entry --------------------------------------------------------------------------------
|
|
1530
|
+
|
|
1531
|
+
ROOT_COMMANDS = {"init", "consume", "revoke", "cancel-mark", "bg-prepare", "bg-status", "bg-tail",
|
|
1532
|
+
"bg-read", "bg-stop-plan", "bg-stop-claim", "bg-contested"}
|
|
1533
|
+
|
|
1534
|
+
|
|
1535
|
+
def main():
|
|
1536
|
+
command = sys.argv[1] if len(sys.argv) > 1 else ""
|
|
1537
|
+
try:
|
|
1538
|
+
if command == "full":
|
|
1539
|
+
try:
|
|
1540
|
+
full(sys.argv[2:])
|
|
1541
|
+
except Refused as error:
|
|
1542
|
+
# Nothing ran: the only output is this refusal frame (with the provider's nonce),
|
|
1543
|
+
# and the exit code is 125.
|
|
1544
|
+
finish({"nonce": admission_nonce[0], "events": [
|
|
1545
|
+
{"event": "refused", "code": error.code, "message": str(error)}]})
|
|
1546
|
+
except Exception as error:
|
|
1547
|
+
# Any other failure before exec is a refusal too, never an apparent success.
|
|
1548
|
+
finish({"nonce": admission_nonce[0], "events": [
|
|
1549
|
+
{"event": "refused", "code": "INVALID_REQUEST",
|
|
1550
|
+
"message": type(error).__name__}]})
|
|
1551
|
+
return 125
|
|
1552
|
+
if command == "bg-start":
|
|
1553
|
+
libc.prctl(4, 0, 0, 0, 0) # PR_SET_DUMPABLE 0, before the request is read
|
|
1554
|
+
if command == "probe":
|
|
1555
|
+
done = subprocess.run([sys.executable, "-I", LAUNCHER, "probe"], capture_output=True)
|
|
1556
|
+
finish({"probe": json.loads(done.stdout), "kernel": os.uname().release})
|
|
1557
|
+
return 0
|
|
1558
|
+
request = read_request()
|
|
1559
|
+
if command in ROOT_COMMANDS and os.getuid() != 0:
|
|
1560
|
+
raise ValueError("root-only command")
|
|
1561
|
+
if command == "direct":
|
|
1562
|
+
direct(request)
|
|
1563
|
+
elif command == "init":
|
|
1564
|
+
records() # also prunes expired records and spools when the instance starts
|
|
1565
|
+
finish({"ready": True})
|
|
1566
|
+
elif command == "consume":
|
|
1567
|
+
finish(consume(request))
|
|
1568
|
+
elif command == "revoke":
|
|
1569
|
+
finish(marker(request, ".revoked"))
|
|
1570
|
+
elif command == "cancel-mark":
|
|
1571
|
+
finish(marker(request, ".cancelled"))
|
|
1572
|
+
elif command == "bg-prepare":
|
|
1573
|
+
finish(bg_prepare(request))
|
|
1574
|
+
elif command == "bg-start":
|
|
1575
|
+
bg_start(request)
|
|
1576
|
+
elif command == "bg-status":
|
|
1577
|
+
finish(bg_status(request))
|
|
1578
|
+
elif command == "bg-contested":
|
|
1579
|
+
finish(bg_contested(request))
|
|
1580
|
+
elif command == "bg-tail":
|
|
1581
|
+
finish(bg_output(request, True))
|
|
1582
|
+
elif command == "bg-read":
|
|
1583
|
+
finish(bg_output(request, False))
|
|
1584
|
+
elif command == "bg-stop-plan":
|
|
1585
|
+
finish(bg_stop_plan(request))
|
|
1586
|
+
elif command == "bg-signal":
|
|
1587
|
+
finish(bg_signal(request))
|
|
1588
|
+
elif command == "bg-stop-claim":
|
|
1589
|
+
finish(bg_stop_claim(request))
|
|
1590
|
+
else:
|
|
1591
|
+
raise ValueError("unknown command")
|
|
1592
|
+
return 0
|
|
1593
|
+
except Refused as error:
|
|
1594
|
+
finish({"events": [{"event": "refused", "code": error.code, "message": str(error)}]})
|
|
1595
|
+
return 0
|
|
1596
|
+
except Exception as error:
|
|
1597
|
+
finish({"events": [{"event": "refused", "code": "INVALID_REQUEST",
|
|
1598
|
+
"message": "%s" % type(error).__name__}]})
|
|
1599
|
+
return 0
|
|
1600
|
+
|
|
1601
|
+
|
|
1602
|
+
if __name__ == "__main__":
|
|
1603
|
+
sys.exit(main())
|
|
1604
|
+
`;
|
|
1605
|
+
|
|
1606
|
+
// src/runtime-source.ts
|
|
1607
|
+
var RUNTIME_BASE = "/opt/capybara-e2b-runtime";
|
|
1608
|
+
var OPS_DIR = "/opt/capybara-e2b-ops";
|
|
1609
|
+
var RUNTIME_BUNDLE_VERSION = 2;
|
|
1610
|
+
var SDK_RUNTIME_FILES = Object.freeze(["native-files.py", "linux-sandbox.py"]);
|
|
1611
|
+
function sdkRuntimeDigests() {
|
|
1612
|
+
if (SANDBOX_RUNTIME_SOURCE.version !== RUNTIME_BUNDLE_VERSION) {
|
|
1613
|
+
throw new Error(
|
|
1614
|
+
`device-e2b stages runtime bundle v${RUNTIME_BUNDLE_VERSION}, the SDK has v${SANDBOX_RUNTIME_SOURCE.version}`
|
|
1615
|
+
);
|
|
1616
|
+
}
|
|
1617
|
+
const out = {};
|
|
1618
|
+
for (const name of SDK_RUNTIME_FILES) {
|
|
1619
|
+
const file = SANDBOX_RUNTIME_SOURCE.files.find((f) => f.name === name);
|
|
1620
|
+
if (!file) throw new Error(`the SDK runtime bundle has no ${name}`);
|
|
1621
|
+
const digest = createHash("sha256").update(file.source).digest("hex");
|
|
1622
|
+
if (digest !== file.sha256)
|
|
1623
|
+
throw new Error(`the SDK runtime ${name} does not match its digest`);
|
|
1624
|
+
out[name] = digest;
|
|
1625
|
+
}
|
|
1626
|
+
return out;
|
|
1627
|
+
}
|
|
1628
|
+
function runtimeFiles() {
|
|
1629
|
+
const sdk = sdkRuntimeDigests();
|
|
1630
|
+
const agent = Buffer.from(OPERATION_AGENT_SOURCE, "utf8");
|
|
1631
|
+
if (createHash("sha256").update(agent).digest("hex") !== OPERATION_AGENT_SHA256) {
|
|
1632
|
+
throw new Error("the operation agent does not match its digest");
|
|
1633
|
+
}
|
|
1634
|
+
const files = [
|
|
1635
|
+
{ name: "capybara-operation.py", data: agent, sha256: OPERATION_AGENT_SHA256 }
|
|
1636
|
+
];
|
|
1637
|
+
for (const name of SDK_RUNTIME_FILES) {
|
|
1638
|
+
const file = SANDBOX_RUNTIME_SOURCE.files.find((f) => f.name === name);
|
|
1639
|
+
files.push({ name, data: Buffer.from(file?.source ?? "", "utf8"), sha256: sdk[name] });
|
|
1640
|
+
}
|
|
1641
|
+
return files.sort((a, b) => a.name < b.name ? -1 : a.name > b.name ? 1 : 0);
|
|
1642
|
+
}
|
|
1643
|
+
function runtimeChecksums() {
|
|
1644
|
+
return runtimeFiles().map((f) => `${f.sha256} ${f.name}
|
|
1645
|
+
`).join("");
|
|
1646
|
+
}
|
|
1647
|
+
var RUNTIME_DIR = `${RUNTIME_BASE}/${createHash("sha256").update(runtimeChecksums()).digest("hex").slice(0, 16)}`;
|
|
1648
|
+
var OPERATION_AGENT = `${RUNTIME_DIR}/capybara-operation.py`;
|
|
1649
|
+
|
|
1650
|
+
// src/operations.ts
|
|
1651
|
+
var PROTECTED = Object.freeze(["/tmp/.capybara-exec-", OPS_DIR, "/opt/capybara", RUNTIME_BASE]);
|
|
1652
|
+
function protectedRoot(path, write) {
|
|
1653
|
+
const p = posix2.normalize(path);
|
|
1654
|
+
for (const entry of PROTECTED) {
|
|
1655
|
+
const bare = entry.replace(/-$/, "");
|
|
1656
|
+
const inside = p === bare || p.startsWith(`${entry.replace(/\/$/, "")}/`);
|
|
1657
|
+
if (write && (inside || entry.startsWith(`${p.replace(/\/$/, "")}/`))) return true;
|
|
1658
|
+
if (!write && inside && entry !== "/tmp/.capybara-exec-") return true;
|
|
1659
|
+
}
|
|
1660
|
+
return false;
|
|
1661
|
+
}
|
|
1662
|
+
var PYTHON = "/usr/bin/python3";
|
|
1663
|
+
var NATIVE_MAX_OUTPUT = 64 * 1024 * 1024;
|
|
1664
|
+
var RECORD_RETENTION_MS = 60 * 60 * 1e3;
|
|
1665
|
+
var STEP_TIMEOUT_MS = 12e4;
|
|
1666
|
+
function operationUuid(operationId) {
|
|
1667
|
+
const h = createHash2("sha256").update(`capybara-operation\0${operationId}`).digest("hex");
|
|
1668
|
+
return `${h.slice(0, 8)}-${h.slice(8, 12)}-${h.slice(12, 16)}-${h.slice(16, 20)}-${h.slice(20, 32)}`;
|
|
1669
|
+
}
|
|
1670
|
+
function operationControlPath(tag, operationId) {
|
|
1671
|
+
return `/tmp/.capybara-exec-${tag}-${operationUuid(operationId)}`;
|
|
1672
|
+
}
|
|
1673
|
+
var opKey = (operationId) => createHash2("sha256").update(operationId).digest("hex");
|
|
1674
|
+
function canonical(value) {
|
|
1675
|
+
if (Array.isArray(value)) return `[${value.map(canonical).join(",")}]`;
|
|
1676
|
+
if (value !== null && typeof value === "object") {
|
|
1677
|
+
const entries = Object.entries(value).filter(([, v]) => v !== void 0).sort(([a], [b]) => a < b ? -1 : a > b ? 1 : 0);
|
|
1678
|
+
return `{${entries.map(([k, v]) => `${JSON.stringify(k)}:${canonical(v)}`).join(",")}}`;
|
|
1679
|
+
}
|
|
1680
|
+
return JSON.stringify(value);
|
|
1681
|
+
}
|
|
1682
|
+
function inputHash(method, scope, input) {
|
|
1683
|
+
return createHash2("sha256").update(canonical({ method, scope, input })).digest("hex");
|
|
1684
|
+
}
|
|
1685
|
+
function request(body) {
|
|
1686
|
+
const json = Buffer.from(JSON.stringify(body));
|
|
1687
|
+
return Buffer.concat([Buffer.from(`${json.byteLength}
|
|
1688
|
+
`), json]);
|
|
1689
|
+
}
|
|
1690
|
+
function parseFrames(raw) {
|
|
1691
|
+
const frames = { out: [], err: [], result: null };
|
|
1692
|
+
let at = 0;
|
|
1693
|
+
while (at + 5 <= raw.byteLength) {
|
|
1694
|
+
const kind = String.fromCharCode(raw[at]);
|
|
1695
|
+
const size = raw.readUInt32BE(at + 1);
|
|
1696
|
+
if (at + 5 + size > raw.byteLength) break;
|
|
1697
|
+
const data = raw.subarray(at + 5, at + 5 + size);
|
|
1698
|
+
if (kind === "O") frames.out.push(data);
|
|
1699
|
+
else if (kind === "E") frames.err.push(data);
|
|
1700
|
+
else if (kind === "R") {
|
|
1701
|
+
try {
|
|
1702
|
+
frames.result = JSON.parse(data.toString("utf8"));
|
|
1703
|
+
} catch {
|
|
1704
|
+
frames.result = null;
|
|
1705
|
+
}
|
|
1706
|
+
}
|
|
1707
|
+
at += 5 + size;
|
|
1708
|
+
}
|
|
1709
|
+
return frames;
|
|
1710
|
+
}
|
|
1711
|
+
function soleResult(raw) {
|
|
1712
|
+
if (raw.byteLength === 0) return null;
|
|
1713
|
+
if (raw.byteLength < 5 || String.fromCharCode(raw[0]) !== "R") return "malformed";
|
|
1714
|
+
if (5 + raw.readUInt32BE(1) !== raw.byteLength) return "malformed";
|
|
1715
|
+
try {
|
|
1716
|
+
return JSON.parse(raw.subarray(5).toString("utf8"));
|
|
1717
|
+
} catch {
|
|
1718
|
+
return "malformed";
|
|
1719
|
+
}
|
|
1720
|
+
}
|
|
1721
|
+
function refusalError(result) {
|
|
1722
|
+
const refused = result?.events?.find((e) => e.event === "refused");
|
|
1723
|
+
if (!refused) return null;
|
|
1724
|
+
switch (refused.code) {
|
|
1725
|
+
case "UNAVAILABLE":
|
|
1726
|
+
return new SandboxPermissionUnsupportedError(
|
|
1727
|
+
`Restricted execution is unavailable on this device: ${refused.message ?? "unsupported"}`
|
|
1728
|
+
);
|
|
1729
|
+
case "SCOPE_DENIED":
|
|
1730
|
+
case "ROOT_CHANGED":
|
|
1731
|
+
return new SandboxScopeDeniedError(
|
|
1732
|
+
"A granted root is missing its pinned identity or the path is outside the operation scope"
|
|
1733
|
+
);
|
|
1734
|
+
case "REVOKED":
|
|
1735
|
+
return new SandboxScopeDeniedError("The operation was revoked before it started");
|
|
1736
|
+
case "EXPIRED":
|
|
1737
|
+
return new SandboxScopeDeniedError("The operation's admission deadline passed");
|
|
1738
|
+
case "CANCELLED":
|
|
1739
|
+
return null;
|
|
1740
|
+
default:
|
|
1741
|
+
return new Error(`Operation refused (${refused.code ?? "unknown"})`);
|
|
1742
|
+
}
|
|
1743
|
+
}
|
|
1744
|
+
function refusedFrame(raw, nonce) {
|
|
1745
|
+
const result = parseFrames(raw).result;
|
|
1746
|
+
if (!result || result.nonce !== nonce) return null;
|
|
1747
|
+
if (result.events?.some((e) => e.event === "refused" && e.code === "CANCELLED")) {
|
|
1748
|
+
return "cancelled";
|
|
1749
|
+
}
|
|
1750
|
+
return refusalError(result);
|
|
1751
|
+
}
|
|
1752
|
+
function checkPath(p) {
|
|
1753
|
+
const errors = validateSandboxPathShape2(p);
|
|
1754
|
+
if (errors.length > 0) {
|
|
1755
|
+
throw new SandboxScopeDeniedError(`Invalid native path: ${errors.join("; ")}`);
|
|
1756
|
+
}
|
|
1757
|
+
}
|
|
1758
|
+
var ENV_KEY_RE = /^[A-Za-z_][A-Za-z0-9_]*$/;
|
|
1759
|
+
function checkedEnv(env) {
|
|
1760
|
+
const out = {};
|
|
1761
|
+
for (const [k, v] of Object.entries(env ?? {})) {
|
|
1762
|
+
if (!ENV_KEY_RE.test(k)) throw new Error(`invalid environment variable name: ${k}`);
|
|
1763
|
+
out[k] = v;
|
|
1764
|
+
}
|
|
1765
|
+
return out;
|
|
1766
|
+
}
|
|
1767
|
+
var NO_RUNTIME = "The device has no operation runtime (device-e2b stages it as root on first use; check that the template provides /usr/bin/python3 and sha256sum)";
|
|
1768
|
+
function nativeReply(result, scoped) {
|
|
1769
|
+
const error = refusalError(result);
|
|
1770
|
+
if (error) throw error;
|
|
1771
|
+
if (!result) throw new SandboxPermissionUnsupportedError(NO_RUNTIME);
|
|
1772
|
+
const reply = result.native;
|
|
1773
|
+
if (!reply) throw new Error("The native helper gave no reply");
|
|
1774
|
+
if (reply.error) {
|
|
1775
|
+
if (reply.error.code === "SCOPE_DENIED" || scoped && reply.error.code === "EACCES") {
|
|
1776
|
+
throw new SandboxScopeDeniedError("The native path is outside the operation scope");
|
|
1777
|
+
}
|
|
1778
|
+
throw new Error(`Native operation failed (${reply.error.code})`);
|
|
1779
|
+
}
|
|
1780
|
+
return reply.result;
|
|
1781
|
+
}
|
|
1782
|
+
function nativeResult(method, value) {
|
|
1783
|
+
if (method === "readFile") {
|
|
1784
|
+
const v = value;
|
|
1785
|
+
return v && { data: new Uint8Array(Buffer.from(v.dataB64, "base64")), truncated: v.truncated };
|
|
1786
|
+
}
|
|
1787
|
+
if (method === "writeFile") return void 0;
|
|
1788
|
+
return value;
|
|
1789
|
+
}
|
|
1790
|
+
var OUTSIDE_UNSUPPORTED = "device-e2b has no outside of its instance: the microVM is the sandbox, so an escalated (device) operation cannot run here";
|
|
1791
|
+
var NARROWER_UNSUPPORTED = "device-e2b runs operations on the whole instance only (the microVM is the sandbox): a narrower filesystem grant needs restricted filters, and the SDK launcher's Landlock ABI 6 is not available on this template";
|
|
1792
|
+
function appliedWithin(network, applied) {
|
|
1793
|
+
if (applied === null) return false;
|
|
1794
|
+
if (network.kind === "open-public") return true;
|
|
1795
|
+
if (network.kind === "environment") return false;
|
|
1796
|
+
if (applied.mode === "none") return true;
|
|
1797
|
+
if (network.kind === "none" || applied.mode === "open-public") return false;
|
|
1798
|
+
const granted = new Set(network.hosts.map((host) => host.toLowerCase()));
|
|
1799
|
+
return applied.allowHosts.every((host) => granted.has(host.toLowerCase()));
|
|
1800
|
+
}
|
|
1801
|
+
function isInstanceScope(scope) {
|
|
1802
|
+
return scope.filesystem.kind === "scoped" && scope.network.kind !== "environment" && // writable roots are also readable (SandboxFileScope): a writable `/` is the whole instance
|
|
1803
|
+
scope.filesystem.writeRoots.includes("/");
|
|
1804
|
+
}
|
|
1805
|
+
function createOperations(deps) {
|
|
1806
|
+
const { transport: t } = deps;
|
|
1807
|
+
const checkNetwork = async (h, scope, signal) => {
|
|
1808
|
+
if (!appliedWithin(scope.network, await t.appliedNetwork(h, signal))) {
|
|
1809
|
+
throw new SandboxScopeDeniedError(
|
|
1810
|
+
"The VM's network allows more than this operation's network (or is not this provider version's); device-e2b cannot narrow it per operation, so the operation is refused. Changing the device's network or resetting its files applies the current policy"
|
|
1811
|
+
);
|
|
1812
|
+
}
|
|
1813
|
+
};
|
|
1814
|
+
const rootStep = async (h, command, body, signal, started = false) => {
|
|
1815
|
+
const o = {
|
|
1816
|
+
cmd: [PYTHON, "-I", OPERATION_AGENT, command],
|
|
1817
|
+
user: "root",
|
|
1818
|
+
stdin: request(body),
|
|
1819
|
+
maxOutputBytes: 1 << 20,
|
|
1820
|
+
timeoutMs: STEP_TIMEOUT_MS,
|
|
1821
|
+
signal
|
|
1822
|
+
};
|
|
1823
|
+
const r = started ? await t.run(h, o) : await t.runIfRunning(h, o);
|
|
1824
|
+
if (r === "not-running") return r;
|
|
1825
|
+
if (r.aborted) signal.throwIfAborted();
|
|
1826
|
+
const result = parseFrames(r.stdout).result;
|
|
1827
|
+
if (!result) throw new Error(`The operation record step ${command} gave no result`);
|
|
1828
|
+
const error = refusalError(result);
|
|
1829
|
+
if (error) throw error;
|
|
1830
|
+
return result;
|
|
1831
|
+
};
|
|
1832
|
+
const record = async (h, ref, method, hash, signal, retainUntilMs, runner) => {
|
|
1833
|
+
const exec = runner ?? ((o) => t.run(h, o));
|
|
1834
|
+
const r = await exec({
|
|
1835
|
+
cmd: [PYTHON, "-I", OPERATION_AGENT, "consume"],
|
|
1836
|
+
user: "root",
|
|
1837
|
+
stdin: request({
|
|
1838
|
+
opKey: opKey(ref.operationId),
|
|
1839
|
+
method,
|
|
1840
|
+
inputHash: hash,
|
|
1841
|
+
admitBeforeMs: ref.admitBeforeMs,
|
|
1842
|
+
expiresAtMs: Math.max(ref.admitBeforeMs, retainUntilMs ?? 0) + RECORD_RETENTION_MS
|
|
1843
|
+
}),
|
|
1844
|
+
maxOutputBytes: 1 << 20,
|
|
1845
|
+
timeoutMs: STEP_TIMEOUT_MS,
|
|
1846
|
+
signal
|
|
1847
|
+
});
|
|
1848
|
+
if (r.aborted) signal.throwIfAborted();
|
|
1849
|
+
const result = parseFrames(r.stdout).result;
|
|
1850
|
+
if (r.exitCode !== 0 || !result) throw new SandboxPermissionUnsupportedError(NO_RUNTIME);
|
|
1851
|
+
const error = refusalError(result);
|
|
1852
|
+
if (error) throw error;
|
|
1853
|
+
switch (result.state) {
|
|
1854
|
+
case "new":
|
|
1855
|
+
return { state: "new", generation: String(result.generation) };
|
|
1856
|
+
case "duplicate":
|
|
1857
|
+
return {
|
|
1858
|
+
state: "duplicate",
|
|
1859
|
+
generation: String(result.record?.generation)
|
|
1860
|
+
};
|
|
1861
|
+
case "revoked":
|
|
1862
|
+
throw new SandboxScopeDeniedError("The operation was revoked");
|
|
1863
|
+
case "expired":
|
|
1864
|
+
throw new SandboxScopeDeniedError("The operation's admission deadline passed");
|
|
1865
|
+
case "mismatch":
|
|
1866
|
+
throw new SandboxScopeValidationError(
|
|
1867
|
+
"This operation ID was consumed with a different method or input"
|
|
1868
|
+
);
|
|
1869
|
+
default:
|
|
1870
|
+
throw new Error("Unexpected operation record state");
|
|
1871
|
+
}
|
|
1872
|
+
};
|
|
1873
|
+
const consume = async (h, ref, method, hash, signal, runner) => {
|
|
1874
|
+
const r = await record(h, ref, method, hash, signal, void 0, runner);
|
|
1875
|
+
if (r.state === "duplicate") {
|
|
1876
|
+
throw new SandboxOperationOutcomeUnknownError(
|
|
1877
|
+
"This operation ID was already consumed; its outcome is not retried"
|
|
1878
|
+
);
|
|
1879
|
+
}
|
|
1880
|
+
};
|
|
1881
|
+
const direct = async (h, user, body, signal, runner) => {
|
|
1882
|
+
const exec = runner ?? ((o) => t.run(h, o));
|
|
1883
|
+
const r = await exec({
|
|
1884
|
+
cmd: [PYTHON, "-I", OPERATION_AGENT, "direct"],
|
|
1885
|
+
user,
|
|
1886
|
+
stdin: request(body),
|
|
1887
|
+
maxOutputBytes: NATIVE_MAX_OUTPUT,
|
|
1888
|
+
timeoutMs: STEP_TIMEOUT_MS,
|
|
1889
|
+
signal
|
|
1890
|
+
});
|
|
1891
|
+
if (r.aborted) signal.throwIfAborted();
|
|
1892
|
+
if (r.timedOut) throw new Error("The native operation timed out");
|
|
1893
|
+
return parseFrames(r.stdout).result;
|
|
1894
|
+
};
|
|
1895
|
+
const nativeOp = async (h, scope, method, native, signal) => {
|
|
1896
|
+
signal.throwIfAborted();
|
|
1897
|
+
checkPath(String(native.path));
|
|
1898
|
+
const input = { ...native, dataB64: void 0, data: digestOf(native.dataB64) };
|
|
1899
|
+
const runner = await t.session(h, signal);
|
|
1900
|
+
await consume(h, scope, method, inputHash(method, scope, input), signal, runner);
|
|
1901
|
+
const result = await direct(
|
|
1902
|
+
h,
|
|
1903
|
+
"sandbox",
|
|
1904
|
+
{
|
|
1905
|
+
opKey: opKey(scope.operationId),
|
|
1906
|
+
admitBeforeMs: scope.admitBeforeMs,
|
|
1907
|
+
expect: sdkRuntimeDigests(),
|
|
1908
|
+
kind: "device",
|
|
1909
|
+
native: { method, ...native }
|
|
1910
|
+
},
|
|
1911
|
+
signal,
|
|
1912
|
+
runner
|
|
1913
|
+
);
|
|
1914
|
+
signal.throwIfAborted();
|
|
1915
|
+
return nativeResult(method, nativeReply(result, false));
|
|
1916
|
+
};
|
|
1917
|
+
const execOp = async (h, scope, args) => {
|
|
1918
|
+
args.signal.throwIfAborted();
|
|
1919
|
+
const input = { ...args, signal: void 0, env: args.env };
|
|
1920
|
+
await checkNetwork(h, scope, args.signal);
|
|
1921
|
+
await consume(h, scope, "exec", inputHash("exec", scope, input), args.signal);
|
|
1922
|
+
const ctl = operationControlPath(scope.tag, scope.operationId);
|
|
1923
|
+
return t.execFull(
|
|
1924
|
+
h,
|
|
1925
|
+
{ ...args, tag: scope.tag },
|
|
1926
|
+
ctl,
|
|
1927
|
+
[
|
|
1928
|
+
PYTHON,
|
|
1929
|
+
"-I",
|
|
1930
|
+
OPERATION_AGENT,
|
|
1931
|
+
"full",
|
|
1932
|
+
opKey(scope.operationId),
|
|
1933
|
+
String(scope.admitBeforeMs),
|
|
1934
|
+
ctl,
|
|
1935
|
+
"--"
|
|
1936
|
+
],
|
|
1937
|
+
randomBytes(16).toString("hex")
|
|
1938
|
+
);
|
|
1939
|
+
};
|
|
1940
|
+
const checkScope = (scope) => {
|
|
1941
|
+
if (scope.filesystem.kind === "device" || scope.network.kind === "environment") {
|
|
1942
|
+
throw new SandboxPermissionUnsupportedError(OUTSIDE_UNSUPPORTED);
|
|
1943
|
+
}
|
|
1944
|
+
if (!isInstanceScope(scope)) throw new SandboxPermissionUnsupportedError(NARROWER_UNSUPPORTED);
|
|
1945
|
+
};
|
|
1946
|
+
const checkReadRoots = (readRoots) => {
|
|
1947
|
+
for (const root of readRoots) {
|
|
1948
|
+
if (posix2.normalize(root) !== root) {
|
|
1949
|
+
throw new SandboxScopeDeniedError("Granted roots must be canonical absolute paths");
|
|
1950
|
+
}
|
|
1951
|
+
if (protectedRoot(root, false)) {
|
|
1952
|
+
throw new SandboxScopeDeniedError(
|
|
1953
|
+
"A readable root may not be inside the provider's records"
|
|
1954
|
+
);
|
|
1955
|
+
}
|
|
1956
|
+
}
|
|
1957
|
+
};
|
|
1958
|
+
const exactRef = (h, input) => {
|
|
1959
|
+
const ref = parseSandboxExecutionRef(input);
|
|
1960
|
+
if (ref.instanceId !== h.id || ref.executionId !== operationUuid(ref.operationId)) {
|
|
1961
|
+
throw new SandboxScopeDeniedError("The execution reference names another instance");
|
|
1962
|
+
}
|
|
1963
|
+
return ref;
|
|
1964
|
+
};
|
|
1965
|
+
const bgCall = (h, command, ref, extra, signal) => rootStep(
|
|
1966
|
+
h,
|
|
1967
|
+
command,
|
|
1968
|
+
{ executionId: ref.executionId, generation: ref.instanceGeneration, ...extra },
|
|
1969
|
+
signal
|
|
1970
|
+
);
|
|
1971
|
+
const stopExecution = async (h, executionId, generation, graceMs, signal, aborted = false) => {
|
|
1972
|
+
const key = { executionId, generation, anyGeneration: generation === null };
|
|
1973
|
+
const plan = await rootStep(h, "bg-stop-plan", key, signal);
|
|
1974
|
+
if (plan === "not-running") return { outcome: "lost" };
|
|
1975
|
+
if (plan.outcome) return toStopResult(plan.outcome);
|
|
1976
|
+
let signalled = false;
|
|
1977
|
+
const target = plan.signal;
|
|
1978
|
+
if (target) {
|
|
1979
|
+
const r = await t.runIfRunning(h, {
|
|
1980
|
+
cmd: [PYTHON, "-I", OPERATION_AGENT, "bg-signal"],
|
|
1981
|
+
user: "sandbox",
|
|
1982
|
+
stdin: request({ ...target, graceMs }),
|
|
1983
|
+
maxOutputBytes: 1 << 20,
|
|
1984
|
+
timeoutMs: STEP_TIMEOUT_MS,
|
|
1985
|
+
signal
|
|
1986
|
+
});
|
|
1987
|
+
if (r === "not-running") return { outcome: "lost" };
|
|
1988
|
+
signalled = parseFrames(r.stdout).result?.signalled === true;
|
|
1989
|
+
}
|
|
1990
|
+
const claim = await rootStep(
|
|
1991
|
+
h,
|
|
1992
|
+
"bg-stop-claim",
|
|
1993
|
+
{ ...key, signalled, ...aborted && { aborted } },
|
|
1994
|
+
signal
|
|
1995
|
+
);
|
|
1996
|
+
if (claim === "not-running") return { outcome: "lost" };
|
|
1997
|
+
return toStopResult(claim);
|
|
1998
|
+
};
|
|
1999
|
+
const abortStart = async (h, scope, ref) => {
|
|
2000
|
+
const cleanup = AbortSignal.timeout(3e4);
|
|
2001
|
+
await rootStep(
|
|
2002
|
+
h,
|
|
2003
|
+
"cancel-mark",
|
|
2004
|
+
{ opKey: opKey(scope.operationId), expiresAtMs: scope.admitBeforeMs + RECORD_RETENTION_MS },
|
|
2005
|
+
cleanup
|
|
2006
|
+
).then(() => stopExecution(h, ref.executionId, ref.instanceGeneration, 0, cleanup, true)).catch(() => void 0);
|
|
2007
|
+
};
|
|
2008
|
+
const abortIfContested = async (h, scope, ref) => {
|
|
2009
|
+
const check = await rootStep(
|
|
2010
|
+
h,
|
|
2011
|
+
"bg-contested",
|
|
2012
|
+
{ executionId: ref.executionId, generation: ref.instanceGeneration },
|
|
2013
|
+
AbortSignal.timeout(15e3)
|
|
2014
|
+
).catch(() => "not-running");
|
|
2015
|
+
if (check !== "not-running" && check.contested === true) await abortStart(h, scope, ref);
|
|
2016
|
+
};
|
|
2017
|
+
const MAX_OUTPUT_READ = 65536;
|
|
2018
|
+
const bytes = (result) => new Uint8Array(Buffer.from(String(result.dataB64 ?? ""), "base64"));
|
|
2019
|
+
const checkMaxBytes = (maxBytes) => {
|
|
2020
|
+
if (!Number.isInteger(maxBytes) || maxBytes < 0 || maxBytes > MAX_OUTPUT_READ) {
|
|
2021
|
+
throw new SandboxScopeDeniedError("Supply maxBytes as an integer from 0 to 65536");
|
|
2022
|
+
}
|
|
2023
|
+
};
|
|
2024
|
+
const background = {
|
|
2025
|
+
async start(h, input, args) {
|
|
2026
|
+
args.signal.throwIfAborted();
|
|
2027
|
+
const scope = parseSandboxOperationScope(input);
|
|
2028
|
+
checkScope(scope);
|
|
2029
|
+
if (typeof args.taskId !== "string" || args.taskId.length === 0 || args.taskId.length > 128) {
|
|
2030
|
+
throw new SandboxScopeValidationError("Supply a task id of 1 to 128 characters");
|
|
2031
|
+
}
|
|
2032
|
+
if (!Number.isSafeInteger(args.retainUntilMs) || args.retainUntilMs <= 0) {
|
|
2033
|
+
throw new SandboxScopeValidationError("Supply retainUntilMs as a positive safe integer");
|
|
2034
|
+
}
|
|
2035
|
+
checkPath(args.outputPath);
|
|
2036
|
+
if (protectedRoot(args.outputPath, true)) {
|
|
2037
|
+
throw new SandboxScopeDeniedError("The output path is reserved for the provider");
|
|
2038
|
+
}
|
|
2039
|
+
const input2 = { ...args, signal: void 0 };
|
|
2040
|
+
await checkNetwork(h, scope, args.signal);
|
|
2041
|
+
const consumed = await record(
|
|
2042
|
+
h,
|
|
2043
|
+
scope,
|
|
2044
|
+
"backgroundStart",
|
|
2045
|
+
inputHash("backgroundStart", scope, input2),
|
|
2046
|
+
args.signal,
|
|
2047
|
+
args.retainUntilMs
|
|
2048
|
+
);
|
|
2049
|
+
const ref = Object.freeze({
|
|
2050
|
+
executionId: operationUuid(scope.operationId),
|
|
2051
|
+
operationId: scope.operationId,
|
|
2052
|
+
instanceId: h.id,
|
|
2053
|
+
instanceGeneration: consumed.generation
|
|
2054
|
+
});
|
|
2055
|
+
if (consumed.state === "duplicate") {
|
|
2056
|
+
await abortIfContested(h, scope, ref);
|
|
2057
|
+
return ref;
|
|
2058
|
+
}
|
|
2059
|
+
const launch = {
|
|
2060
|
+
opKey: opKey(scope.operationId),
|
|
2061
|
+
admitBeforeMs: scope.admitBeforeMs,
|
|
2062
|
+
expect: sdkRuntimeDigests(),
|
|
2063
|
+
executionId: ref.executionId,
|
|
2064
|
+
kind: "device",
|
|
2065
|
+
writeRoots: [],
|
|
2066
|
+
readRoots: [],
|
|
2067
|
+
network: scope.network,
|
|
2068
|
+
command: args.command,
|
|
2069
|
+
cwd: args.cwd ?? "/workspace",
|
|
2070
|
+
// the ordinary exec's environment (provider.ts execWith), then the caller's variables
|
|
2071
|
+
env: { HOME: "/workspace", LANG: "C.UTF-8", ...checkedEnv(args.env) },
|
|
2072
|
+
timeoutMs: args.timeoutMs,
|
|
2073
|
+
maxOutputBytes: args.maxOutputBytes,
|
|
2074
|
+
taskId: args.taskId,
|
|
2075
|
+
outputPath: args.outputPath,
|
|
2076
|
+
retainUntilMs: args.retainUntilMs
|
|
2077
|
+
};
|
|
2078
|
+
const secret2 = randomBytes(16).toString("hex");
|
|
2079
|
+
const token = randomBytes(16).toString("hex");
|
|
2080
|
+
const prepared = await rootStep(
|
|
2081
|
+
h,
|
|
2082
|
+
"bg-prepare",
|
|
2083
|
+
{
|
|
2084
|
+
executionId: ref.executionId,
|
|
2085
|
+
secret: secret2,
|
|
2086
|
+
token,
|
|
2087
|
+
start: launch,
|
|
2088
|
+
opKey: opKey(scope.operationId),
|
|
2089
|
+
taskId: args.taskId,
|
|
2090
|
+
retainUntilMs: args.retainUntilMs
|
|
2091
|
+
},
|
|
2092
|
+
args.signal,
|
|
2093
|
+
true
|
|
2094
|
+
);
|
|
2095
|
+
if (prepared === "not-running" || prepared.existing) {
|
|
2096
|
+
throw new SandboxOperationOutcomeUnknownError(
|
|
2097
|
+
"This execution was already prepared; reconcile it by its reference"
|
|
2098
|
+
);
|
|
2099
|
+
}
|
|
2100
|
+
let outcome = "failed";
|
|
2101
|
+
try {
|
|
2102
|
+
const cmd = [PYTHON, "-I", OPERATION_AGENT, "bg-start"];
|
|
2103
|
+
const r = await t.run(h, {
|
|
2104
|
+
cmd: t.backgroundStartCommand?.(args.taskId, cmd) ?? cmd,
|
|
2105
|
+
user: "sandbox",
|
|
2106
|
+
stdin: request({ secret: secret2, token, ...launch }),
|
|
2107
|
+
maxOutputBytes: 1 << 20,
|
|
2108
|
+
timeoutMs: STEP_TIMEOUT_MS,
|
|
2109
|
+
signal: args.signal
|
|
2110
|
+
});
|
|
2111
|
+
const sole = soleResult(r.stdout);
|
|
2112
|
+
if (r.exitCode !== 0 || r.aborted || r.timedOut || sole === "malformed") {
|
|
2113
|
+
throw new SandboxOperationOutcomeUnknownError(
|
|
2114
|
+
"The background start did not finish and was aborted; reconcile it by its reference"
|
|
2115
|
+
);
|
|
2116
|
+
}
|
|
2117
|
+
outcome = "unknown";
|
|
2118
|
+
const result = t.loseBackgroundAck?.(args.taskId) ? null : sole;
|
|
2119
|
+
const error = refusalError(result);
|
|
2120
|
+
if (error) {
|
|
2121
|
+
outcome = "refused";
|
|
2122
|
+
throw error;
|
|
2123
|
+
}
|
|
2124
|
+
if (!result?.started) {
|
|
2125
|
+
throw new SandboxOperationOutcomeUnknownError(
|
|
2126
|
+
"The background start was not acknowledged; reconcile it by its reference"
|
|
2127
|
+
);
|
|
2128
|
+
}
|
|
2129
|
+
outcome = "acknowledged";
|
|
2130
|
+
return ref;
|
|
2131
|
+
} finally {
|
|
2132
|
+
if (outcome === "refused" || outcome === "failed") await abortStart(h, scope, ref);
|
|
2133
|
+
if (outcome === "unknown") await abortIfContested(h, scope, ref);
|
|
2134
|
+
}
|
|
2135
|
+
},
|
|
2136
|
+
async status(h, input, signal) {
|
|
2137
|
+
signal.throwIfAborted();
|
|
2138
|
+
const ref = exactRef(h, input);
|
|
2139
|
+
const result = await bgCall(h, "bg-status", ref, {}, signal);
|
|
2140
|
+
if (result === "not-running" || result.state === "lost") return { state: "lost" };
|
|
2141
|
+
if (result.state === "running") return { state: "running" };
|
|
2142
|
+
return {
|
|
2143
|
+
state: "exited",
|
|
2144
|
+
exitCode: typeof result.exitCode === "number" ? result.exitCode : null,
|
|
2145
|
+
timedOut: result.timedOut === true
|
|
2146
|
+
};
|
|
2147
|
+
},
|
|
2148
|
+
async tail(h, input, opts) {
|
|
2149
|
+
opts.signal.throwIfAborted();
|
|
2150
|
+
const ref = exactRef(h, input);
|
|
2151
|
+
checkMaxBytes(opts.maxBytes);
|
|
2152
|
+
const result = await bgCall(h, "bg-tail", ref, { maxBytes: opts.maxBytes }, opts.signal);
|
|
2153
|
+
if (result === "not-running") return { unavailable: "lost" };
|
|
2154
|
+
if (result.unavailable === "lost" || result.unavailable === "expired") {
|
|
2155
|
+
return { unavailable: result.unavailable };
|
|
2156
|
+
}
|
|
2157
|
+
return { data: bytes(result), truncated: result.truncated === true };
|
|
2158
|
+
},
|
|
2159
|
+
async readOutput(h, input, opts) {
|
|
2160
|
+
opts.signal.throwIfAborted();
|
|
2161
|
+
const ref = exactRef(h, input);
|
|
2162
|
+
if (!Number.isSafeInteger(opts.offset) || opts.offset < 0) {
|
|
2163
|
+
throw new SandboxScopeDeniedError("Supply offset as a nonnegative safe integer");
|
|
2164
|
+
}
|
|
2165
|
+
checkMaxBytes(opts.maxBytes);
|
|
2166
|
+
const result = await bgCall(
|
|
2167
|
+
h,
|
|
2168
|
+
"bg-read",
|
|
2169
|
+
ref,
|
|
2170
|
+
{ offset: opts.offset, maxBytes: opts.maxBytes },
|
|
2171
|
+
opts.signal
|
|
2172
|
+
);
|
|
2173
|
+
if (result === "not-running") return { unavailable: "lost" };
|
|
2174
|
+
if (result.unavailable === "lost" || result.unavailable === "expired") {
|
|
2175
|
+
return { unavailable: result.unavailable };
|
|
2176
|
+
}
|
|
2177
|
+
const data = bytes(result);
|
|
2178
|
+
return {
|
|
2179
|
+
data,
|
|
2180
|
+
offset: opts.offset,
|
|
2181
|
+
nextOffset: opts.offset + data.byteLength,
|
|
2182
|
+
totalBytes: Number(result.totalBytes)
|
|
2183
|
+
};
|
|
2184
|
+
},
|
|
2185
|
+
async stop(h, input, opts) {
|
|
2186
|
+
opts.signal.throwIfAborted();
|
|
2187
|
+
const ref = exactRef(h, input);
|
|
2188
|
+
return stopExecution(h, ref.executionId, ref.instanceGeneration, opts.graceMs, opts.signal);
|
|
2189
|
+
}
|
|
2190
|
+
};
|
|
2191
|
+
return {
|
|
2192
|
+
/**
|
|
2193
|
+
* Support v2 of this VM (approval-only profiles design, "SDK and protocol"): the microVM is the
|
|
2194
|
+
* sandbox (`boundary: "instance"`, no restricted filters, no outside), its network policy
|
|
2195
|
+
* offers open-public and (unless domain allowlists are off) allowlists, never per-operation
|
|
2196
|
+
* hosts, and background work is supervised exactly. The agent's probe stages the runtime when
|
|
2197
|
+
* this VM has none yet; the staged runtime must then be root-owned, read-only and match its
|
|
2198
|
+
* digests, otherwise nothing is offered and the reason says why. Never connects or resumes
|
|
2199
|
+
* the VM.
|
|
2200
|
+
*/
|
|
2201
|
+
async permissionSupport(h, signal) {
|
|
2202
|
+
signal.throwIfAborted();
|
|
2203
|
+
const unavailable = (reason) => ({
|
|
2204
|
+
version: 2,
|
|
2205
|
+
boundary: "instance",
|
|
2206
|
+
restricted: null,
|
|
2207
|
+
network: { openPublic: false, allowlist: false, perOperationHosts: false },
|
|
2208
|
+
outsideSandbox: false,
|
|
2209
|
+
background: false,
|
|
2210
|
+
unavailableReason: reason
|
|
2211
|
+
});
|
|
2212
|
+
const probe = await t.runIfRunning(h, {
|
|
2213
|
+
cmd: [PYTHON, "-I", OPERATION_AGENT, "probe"],
|
|
2214
|
+
user: "sandbox",
|
|
2215
|
+
maxOutputBytes: 1 << 20,
|
|
2216
|
+
timeoutMs: STEP_TIMEOUT_MS,
|
|
2217
|
+
signal
|
|
2218
|
+
});
|
|
2219
|
+
if (probe === "not-running") {
|
|
2220
|
+
throw new Error("The device instance is not running; probe it during execution");
|
|
2221
|
+
}
|
|
2222
|
+
const files = runtimeFiles();
|
|
2223
|
+
const names = files.map((f) => f.name);
|
|
2224
|
+
const staged = await t.runIfRunning(h, {
|
|
2225
|
+
cmd: [
|
|
2226
|
+
"/bin/sh",
|
|
2227
|
+
"-c",
|
|
2228
|
+
'cd "$1" && shift && stat -c "%u %a %F %n" . .. "$@" && sha256sum -- "$@"',
|
|
2229
|
+
"capybara-probe",
|
|
2230
|
+
RUNTIME_DIR,
|
|
2231
|
+
...names
|
|
2232
|
+
],
|
|
2233
|
+
user: "sandbox",
|
|
2234
|
+
env: { PATH: "/usr/bin:/bin" },
|
|
2235
|
+
maxOutputBytes: 64 * 1024,
|
|
2236
|
+
timeoutMs: STEP_TIMEOUT_MS,
|
|
2237
|
+
signal
|
|
2238
|
+
});
|
|
2239
|
+
if (staged === "not-running") {
|
|
2240
|
+
throw new Error("The device instance is not running; probe it during execution");
|
|
2241
|
+
}
|
|
2242
|
+
if (staged.exitCode !== 0) return unavailable("The VM has no staged operation runtime");
|
|
2243
|
+
const lines = staged.stdout.toString("utf8").trim().split("\n");
|
|
2244
|
+
for (const line of lines.slice(0, names.length + 2)) {
|
|
2245
|
+
const m = line.match(/^(\d+) ([0-7]+) (.+?) (\S+)$/);
|
|
2246
|
+
const writable = (Number.parseInt(m?.[2] ?? "777", 8) & 18) !== 0;
|
|
2247
|
+
if (m?.[1] !== "0" || writable || /symbolic/.test(m[3] ?? "")) {
|
|
2248
|
+
return unavailable("The staged operation runtime is not root-owned and read-only");
|
|
2249
|
+
}
|
|
2250
|
+
}
|
|
2251
|
+
const expect = Object.fromEntries(files.map((f) => [f.name, f.sha256]));
|
|
2252
|
+
const digests = lines.slice(names.length + 2);
|
|
2253
|
+
if (digests.length !== names.length) {
|
|
2254
|
+
return unavailable("The staged operation runtime is incomplete");
|
|
2255
|
+
}
|
|
2256
|
+
for (const line of digests) {
|
|
2257
|
+
const [digest, name] = line.split(/\s+/);
|
|
2258
|
+
if (!name || expect[name] !== digest) {
|
|
2259
|
+
return unavailable(`The staged ${name ?? "runtime"} does not match its digest`);
|
|
2260
|
+
}
|
|
2261
|
+
}
|
|
2262
|
+
return {
|
|
2263
|
+
version: 2,
|
|
2264
|
+
boundary: "instance",
|
|
2265
|
+
restricted: null,
|
|
2266
|
+
network: {
|
|
2267
|
+
openPublic: true,
|
|
2268
|
+
allowlist: deps.network.allowlist,
|
|
2269
|
+
perOperationHosts: false
|
|
2270
|
+
},
|
|
2271
|
+
outsideSandbox: false,
|
|
2272
|
+
background: true
|
|
2273
|
+
};
|
|
2274
|
+
},
|
|
2275
|
+
async operation(h, input, signal) {
|
|
2276
|
+
signal.throwIfAborted();
|
|
2277
|
+
const scope = parseSandboxOperationScope(input);
|
|
2278
|
+
checkScope(scope);
|
|
2279
|
+
const both = (s) => AbortSignal.any([signal, s]);
|
|
2280
|
+
return {
|
|
2281
|
+
exec: (args) => execOp(h, scope, { ...args, signal: both(args.signal) }),
|
|
2282
|
+
readFile: async (path, o) => nativeOp(
|
|
2283
|
+
h,
|
|
2284
|
+
scope,
|
|
2285
|
+
"readFile",
|
|
2286
|
+
{ path, maxBytes: o.maxBytes },
|
|
2287
|
+
both(o.signal)
|
|
2288
|
+
),
|
|
2289
|
+
writeFile: async (path, data, s) => {
|
|
2290
|
+
await nativeOp(
|
|
2291
|
+
h,
|
|
2292
|
+
scope,
|
|
2293
|
+
"writeFile",
|
|
2294
|
+
{ path, dataB64: Buffer.from(data).toString("base64") },
|
|
2295
|
+
both(s)
|
|
2296
|
+
);
|
|
2297
|
+
},
|
|
2298
|
+
stat: async (path, s) => nativeOp(h, scope, "stat", { path }, both(s)),
|
|
2299
|
+
list: async (path, o) => nativeOp(h, scope, "list", { path, limit: o.limit }, both(o.signal)),
|
|
2300
|
+
scan: async (base, o) => nativeOp(h, scope, "scan", { path: base }, both(o.signal))
|
|
2301
|
+
};
|
|
2302
|
+
},
|
|
2303
|
+
/**
|
|
2304
|
+
* Owner metadata browsing on pinned read roots (no launcher: metadata only, descriptor-relative
|
|
2305
|
+
* and no-follow, as the device user).
|
|
2306
|
+
*/
|
|
2307
|
+
async browse(h, input, signal) {
|
|
2308
|
+
signal.throwIfAborted();
|
|
2309
|
+
const scope = parseSandboxBrowseScope(input);
|
|
2310
|
+
checkReadRoots(scope.readRoots);
|
|
2311
|
+
const run = async (method, native, s) => {
|
|
2312
|
+
const sig = AbortSignal.any([signal, s]);
|
|
2313
|
+
sig.throwIfAborted();
|
|
2314
|
+
checkPath(String(native.path));
|
|
2315
|
+
const runner = await t.session(h, sig);
|
|
2316
|
+
const key = `browse-${method}`;
|
|
2317
|
+
await consume(h, scope, key, inputHash(key, scope, native), sig, runner);
|
|
2318
|
+
const result = await direct(
|
|
2319
|
+
h,
|
|
2320
|
+
"sandbox",
|
|
2321
|
+
{
|
|
2322
|
+
opKey: opKey(scope.operationId),
|
|
2323
|
+
admitBeforeMs: scope.admitBeforeMs,
|
|
2324
|
+
expect: sdkRuntimeDigests(),
|
|
2325
|
+
kind: "scoped",
|
|
2326
|
+
readRoots: [...scope.readRoots],
|
|
2327
|
+
writeRoots: [],
|
|
2328
|
+
native: { method, ...native }
|
|
2329
|
+
},
|
|
2330
|
+
sig,
|
|
2331
|
+
runner
|
|
2332
|
+
);
|
|
2333
|
+
sig.throwIfAborted();
|
|
2334
|
+
return nativeReply(result, true);
|
|
2335
|
+
};
|
|
2336
|
+
return {
|
|
2337
|
+
stat: async (path, s) => await run("stat", { path }, s),
|
|
2338
|
+
list: async (path, o) => await run("list", { path, limit: o.limit }, o.signal),
|
|
2339
|
+
scan: async (base, o) => await run("scan", { path: base }, o.signal)
|
|
2340
|
+
};
|
|
2341
|
+
},
|
|
2342
|
+
/**
|
|
2343
|
+
* Trusted staging: skills only under /skills (written by root, files 0644), attachments only
|
|
2344
|
+
* under /workspace/attachments/<x> (as the device user), through pinned, no-follow roots.
|
|
2345
|
+
*/
|
|
2346
|
+
async prepare(h, input, signal) {
|
|
2347
|
+
signal.throwIfAborted();
|
|
2348
|
+
const scope = parseSandboxPreparationScope(input);
|
|
2349
|
+
const roots = scope.writeRoots.map((root) => {
|
|
2350
|
+
if (posix2.normalize(root) !== root) {
|
|
2351
|
+
throw new SandboxScopeDeniedError("Preparation roots must be canonical absolute paths");
|
|
2352
|
+
}
|
|
2353
|
+
if (scope.purpose === "skills") {
|
|
2354
|
+
if (root !== "/skills" && !root.startsWith("/skills/")) {
|
|
2355
|
+
throw new SandboxScopeDeniedError("Skills are prepared only under /skills");
|
|
2356
|
+
}
|
|
2357
|
+
return { path: root, base: "/skills", relative: root.slice("/skills".length) };
|
|
2358
|
+
}
|
|
2359
|
+
const m = root.match(/^\/workspace\/attachments\/[^/]+(\/.*)?$/);
|
|
2360
|
+
if (!m) {
|
|
2361
|
+
throw new SandboxScopeDeniedError(
|
|
2362
|
+
"Attachments are prepared only under /workspace/attachments/<conversation>"
|
|
2363
|
+
);
|
|
2364
|
+
}
|
|
2365
|
+
return { path: root, base: "/workspace", relative: root.slice("/workspace".length) };
|
|
2366
|
+
});
|
|
2367
|
+
const user = scope.purpose === "skills" ? "root" : "sandbox";
|
|
2368
|
+
const run = async (method, native, s) => {
|
|
2369
|
+
const sig = AbortSignal.any([signal, s]);
|
|
2370
|
+
sig.throwIfAborted();
|
|
2371
|
+
checkPath(String(native.path));
|
|
2372
|
+
const result = await direct(
|
|
2373
|
+
h,
|
|
2374
|
+
user,
|
|
2375
|
+
{
|
|
2376
|
+
expect: sdkRuntimeDigests(),
|
|
2377
|
+
kind: "prepare",
|
|
2378
|
+
prepareRoots: roots,
|
|
2379
|
+
publicFiles: scope.purpose === "skills",
|
|
2380
|
+
native: { method, ...native }
|
|
2381
|
+
},
|
|
2382
|
+
sig
|
|
2383
|
+
);
|
|
2384
|
+
sig.throwIfAborted();
|
|
2385
|
+
return nativeResult(method, nativeReply(result, true));
|
|
2386
|
+
};
|
|
2387
|
+
return {
|
|
2388
|
+
readFile: async (path, o) => await run("readFile", { path, maxBytes: o.maxBytes }, o.signal),
|
|
2389
|
+
writeFile: async (path, data, s) => {
|
|
2390
|
+
await run("writeFile", { path, dataB64: Buffer.from(data).toString("base64") }, s);
|
|
2391
|
+
},
|
|
2392
|
+
stat: async (path, s) => await run("stat", { path }, s)
|
|
2393
|
+
};
|
|
2394
|
+
},
|
|
2395
|
+
background,
|
|
2396
|
+
async revokeOperation(h, input, signal) {
|
|
2397
|
+
signal.throwIfAborted();
|
|
2398
|
+
const ref = parseSandboxOperationRef(input);
|
|
2399
|
+
const marker = {
|
|
2400
|
+
opKey: opKey(ref.operationId),
|
|
2401
|
+
expiresAtMs: ref.admitBeforeMs + RECORD_RETENTION_MS
|
|
2402
|
+
};
|
|
2403
|
+
const first = await rootStep(h, "revoke", marker, signal);
|
|
2404
|
+
if (first === "not-running") {
|
|
2405
|
+
throw new Error("The device instance is not running; the revocation stays pending");
|
|
2406
|
+
}
|
|
2407
|
+
const uuid = operationUuid(ref.operationId);
|
|
2408
|
+
await t.killOperation(h, uuid, signal);
|
|
2409
|
+
await stopExecution(h, uuid, null, 0, signal);
|
|
2410
|
+
const second = await rootStep(h, "revoke", marker, signal);
|
|
2411
|
+
if (second === "not-running" || second.generation !== first.generation) {
|
|
2412
|
+
throw new Error("The device instance restarted during the revocation; retry it");
|
|
2413
|
+
}
|
|
2414
|
+
}
|
|
2415
|
+
};
|
|
2416
|
+
}
|
|
2417
|
+
function toStopResult(result) {
|
|
2418
|
+
switch (result.outcome) {
|
|
2419
|
+
case "already-exited":
|
|
2420
|
+
return {
|
|
2421
|
+
outcome: "already-exited",
|
|
2422
|
+
exitCode: typeof result.exitCode === "number" ? result.exitCode : null,
|
|
2423
|
+
timedOut: result.timedOut === true
|
|
2424
|
+
};
|
|
2425
|
+
case "stopped":
|
|
2426
|
+
return { outcome: "stopped" };
|
|
2427
|
+
default:
|
|
2428
|
+
return { outcome: "lost" };
|
|
2429
|
+
}
|
|
2430
|
+
}
|
|
2431
|
+
function digestOf(data) {
|
|
2432
|
+
return typeof data === "string" ? createHash2("sha256").update(data).digest("hex") : void 0;
|
|
2433
|
+
}
|
|
2434
|
+
|
|
2435
|
+
// src/provider.ts
|
|
2436
|
+
var METADATA_NAME = "capybara-sandbox-name";
|
|
2437
|
+
function publishedImage(version) {
|
|
2438
|
+
return `ghcr.io/capybara-hq/capybara-sandbox:${version}`;
|
|
2439
|
+
}
|
|
2440
|
+
function templateAlias(image) {
|
|
2441
|
+
const name = image.slice(image.lastIndexOf("/") + 1);
|
|
2442
|
+
const colon = name.lastIndexOf(":");
|
|
2443
|
+
const tag = colon >= 0 ? name.slice(colon + 1) : "latest";
|
|
2444
|
+
return `capybara-sandbox-${tag.toLowerCase().replace(/[^a-z0-9-]/g, "-")}`;
|
|
2445
|
+
}
|
|
2446
|
+
var TEMPLATE_CPU_COUNT = 1;
|
|
2447
|
+
var TEMPLATE_MEMORY_MB = 2048;
|
|
2448
|
+
var TEMPLATE_POLL_MS = 1e3;
|
|
2449
|
+
var TEMPLATE_BUILD_TIMEOUT_MS = 30 * 6e4;
|
|
2450
|
+
var SANDBOX_USER = "sandbox";
|
|
2451
|
+
var ROOT_USER = "root";
|
|
2452
|
+
var UPLOAD_DIR = "/opt/capybara/upload";
|
|
2453
|
+
var MAX_TIMEOUT_SECONDS = 86400;
|
|
2454
|
+
var TIMEOUT_MARGIN_SECONDS = 120;
|
|
2455
|
+
var KILL_GRACE_MS = 3e3;
|
|
2456
|
+
var STDERR_ROOM = 64 * 1024;
|
|
2457
|
+
var FILE_OP_TIMEOUT_MS = 12e4;
|
|
2458
|
+
var CONNECT_CONFLICT_RETRIES = 5;
|
|
2459
|
+
var CONNECT_CONFLICT_WAIT_MS = 500;
|
|
2460
|
+
var LIST_LIMIT = 100;
|
|
2461
|
+
var NAME_RE = /^[a-zA-Z0-9][a-zA-Z0-9_.-]{0,100}$/;
|
|
2462
|
+
var ENV_KEY_RE2 = /^[A-Za-z_][A-Za-z0-9_]*$/;
|
|
2463
|
+
var HOSTNAME_RE = /^[a-z0-9]([a-z0-9-]*[a-z0-9])?(\.[a-z0-9]([a-z0-9-]*[a-z0-9])?)+$/;
|
|
2464
|
+
var SANDBOX_ID_RE = /^[a-zA-Z0-9-]{1,128}$/;
|
|
2465
|
+
var TEMPLATE_ID_RE = /^[a-zA-Z0-9_-]{1,128}$/;
|
|
2466
|
+
var IN_SANDBOX_GRACE_SECONDS = 5;
|
|
2467
|
+
var EXEC_MARKER = "CAPYBARA_EXEC";
|
|
2468
|
+
var MARKED = 'm() { for p in $(cd /proc 2>/dev/null && echo [0-9]*); do [ "$p" = "$$" ] && continue; tr "\\0" "\\n" < "/proc/$p/environ" 2>/dev/null | grep -q"x$1" -- "$2" && echo "$p"; done; }; ';
|
|
2469
|
+
var KILL_MARKED = `${MARKED}km() { for _ in 1 2 3; do l=$(m "$1" "$2"); [ -z "$l" ] && return 0; for p in $l; do kill -9 "$p" 2>/dev/null; done; sleep 0.05; done; [ -z "$(m "$1" "$2")" ] || exit 1; }; `;
|
|
2470
|
+
var EXEC_WRAPPER = `c=$1; echo $$ > "$c.pid" 2>/dev/null; if [ -e "$c.kill" ]; then exit 137; fi; timeout -s KILL "$3" /bin/bash -c "$2"; s=$?; if [ $s -eq 124 ] || [ $s -eq 137 ]; then for p in $(pgrep -s $$); do [ "$p" = $$ ] || kill -9 "$p" 2>/dev/null; done; env -u ${EXEC_MARKER} /bin/sh -c '${KILL_MARKED}km F "$0"' "${EXEC_MARKER}=$c"; fi; rm -f "$c.pid"; exit $s`;
|
|
2471
|
+
function execArgs(ctl, command, timeoutMs) {
|
|
2472
|
+
const seconds = Math.ceil(timeoutMs / 1e3) + IN_SANDBOX_GRACE_SECONDS;
|
|
2473
|
+
return ["-w", "/bin/sh", "-c", EXEC_WRAPPER, "capybara-exec", ctl, command, String(seconds)];
|
|
2474
|
+
}
|
|
2475
|
+
var KILL_TREE = 'k() { for t in $(pgrep -P "$1"); do kill -9 -"$t" 2>/dev/null; done; kill -9 -"$1" 2>/dev/null; pkill -9 -s "$1" 2>/dev/null; }; ';
|
|
2476
|
+
var KILL_ALL_SCRIPT = `${KILL_TREE}${KILL_MARKED}for q in $(pgrep -f -- "capybara-exec /tmp/[.]capybara-exec-"); do k "$q"; kill -9 "$q" 2>/dev/null; done; km E "${EXEC_MARKER}=/tmp/[.]capybara-exec-.*"; exit 0`;
|
|
2477
|
+
var KILL_TAGGED_SCRIPT = `${KILL_TREE}${KILL_MARKED}u='[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}'; for t in "$@"; do for q in $(pgrep -f -- "capybara-exec /tmp/[.]capybara-exec-$t-$u( |$)"); do k "$q"; kill -9 "$q" 2>/dev/null; done; km E "${EXEC_MARKER}=/tmp/[.]capybara-exec-$t-$u"; done; exit 0`;
|
|
2478
|
+
var KILL_OPERATION_SCRIPT = `${KILL_TREE}${KILL_MARKED}for q in $(pgrep -f -- "capybara-exec /tmp/[.]capybara-exec-[a-z0-9-]+-$1( |$)"); do k "$q"; kill -9 "$q" 2>/dev/null; done; km E "${EXEC_MARKER}=/tmp/[.]capybara-exec-[a-z0-9-]+-$1"; exit 0`;
|
|
2479
|
+
var STAGE_RUNTIME_SCRIPT = `set -eu; umask 022; u=$1; d=$2; b=$(dirname -- "$d"); k="$u.sums"; trap 'rm -rf -- "$u" "$k"' EXIT; cp -- "$u/SHA256SUMS" "$k"; t() { [ -d "$1" ] && [ ! -L "$1" ] && [ -z "$(find "$1" -maxdepth 1 \\( ! -user 0 -o -perm /022 -o -type l \\) -print -quit)" ] && (cd -- "$1" && sha256sum -c --quiet "$k") >/dev/null 2>&1; }; if [ -L "$b" ] || { [ -e "$b" ] && [ ! -d "$b" ]; }; then rm -f -- "$b"; fi; mkdir -p -- "$b"; chown 0:0 "$b"; chmod 0755 "$b"; cd -- "$u"; sha256sum -c --quiet SHA256SUMS; chown -R 0:0 .; chmod 0644 ./*; chmod 0755 .; cd /; if [ ! -e "$d" ] && [ ! -L "$d" ]; then mv -T -- "$u" "$d" 2>/dev/null || true; elif ! t "$d"; then rm -rf -- "$d"; mv -T -- "$u" "$d" 2>/dev/null || true; fi; t "$d"`;
|
|
2480
|
+
var ROOT_AGENT_PRECHECK = [
|
|
2481
|
+
"import hashlib, os, stat, sys",
|
|
2482
|
+
"agent, digest, rest = sys.argv[1], sys.argv[2], sys.argv[3:]",
|
|
2483
|
+
"rt = os.path.dirname(agent); base = os.path.dirname(rt)",
|
|
2484
|
+
"chain = ['/', os.path.dirname(base), base, rt, agent]",
|
|
2485
|
+
"try:",
|
|
2486
|
+
" infos = [os.lstat(p) for p in chain]",
|
|
2487
|
+
"except OSError:",
|
|
2488
|
+
" sys.stderr.write('capybara-precheck: runtime missing\\n'); sys.exit(3)",
|
|
2489
|
+
"for p, i in zip(chain, infos):",
|
|
2490
|
+
" kind = stat.S_ISREG(i.st_mode) if p == agent else stat.S_ISDIR(i.st_mode)",
|
|
2491
|
+
" if i.st_uid != 0 or i.st_mode & 0o022 or not kind:",
|
|
2492
|
+
" sys.stderr.write('capybara-precheck: untrusted runtime %s\\n' % p); sys.exit(4)",
|
|
2493
|
+
"with open(agent, 'rb') as f:",
|
|
2494
|
+
" if hashlib.sha256(f.read()).hexdigest() != digest:",
|
|
2495
|
+
" sys.stderr.write('capybara-precheck: untrusted runtime digest\\n'); sys.exit(4)",
|
|
2496
|
+
"if rest == ['--probe-only']:",
|
|
2497
|
+
" sys.exit(0)",
|
|
2498
|
+
"os.execv(sys.executable, [sys.executable, '-I', agent] + rest)"
|
|
2499
|
+
].join("\n");
|
|
2500
|
+
var AGENT_PYTHON = "/usr/bin/python3";
|
|
2501
|
+
function rootAgentArgs(cmd) {
|
|
2502
|
+
if (cmd[0] !== AGENT_PYTHON || cmd[1] !== "-I" || cmd[2] !== OPERATION_AGENT || !cmd[3]) {
|
|
2503
|
+
throw new Error("device-e2b: root runs only the operation agent, through its verification");
|
|
2504
|
+
}
|
|
2505
|
+
return [
|
|
2506
|
+
"-I",
|
|
2507
|
+
"-c",
|
|
2508
|
+
ROOT_AGENT_PRECHECK,
|
|
2509
|
+
OPERATION_AGENT,
|
|
2510
|
+
OPERATION_AGENT_SHA256,
|
|
2511
|
+
...cmd.slice(3)
|
|
2512
|
+
];
|
|
2513
|
+
}
|
|
2514
|
+
function execControlPath(tag) {
|
|
2515
|
+
if (tag !== void 0 && !SANDBOX_TAG_RE.test(tag)) throw new Error("invalid sandbox tag");
|
|
2516
|
+
return tag === void 0 ? `/tmp/.capybara-exec-${randomUUID()}` : `/tmp/.capybara-exec-${tag}-${randomUUID()}`;
|
|
2517
|
+
}
|
|
2518
|
+
var KILL_SCRIPT = `${KILL_TREE}${KILL_MARKED}c=$1; : > "$c.kill" 2>/dev/null; i=0; while [ ! -s "$c.pid" ] && [ $i -lt 20 ]; do sleep 0.05; i=$((i+1)); done; p=$(cat "$c.pid" 2>/dev/null); if [ -n "$p" ]; then k "$p"; rm -f "$c.kill"; fi; for q in $(pgrep -f -- "capybara-exec $c"); do k "$q"; kill -9 "$q" 2>/dev/null; done; km F "${EXEC_MARKER}=$c"; rm -f "$c.pid"; exit 0`;
|
|
2519
|
+
var READ_SCRIPT = 'if [ ! -e "$1" ]; then exit 3; fi; if [ -d "$1" ]; then exit 4; fi; exec head -c "$2" -- "$1"';
|
|
2520
|
+
var STAT_SCRIPT = 'if [ ! -e "$1" ]; then exit 3; fi; s=$(stat -L -c %s -- "$1" 2>/dev/null) || s=$(($(wc -c < "$1"))); if [ -d "$1" ]; then echo "$s directory"; else echo "$s file"; fi';
|
|
2521
|
+
var USER_WRITE_SCRIPT = `s=1; runuser -u ${SANDBOX_USER} -- /bin/sh -c 'set -e; mkdir -p -- "$(dirname -- "$1")"; cat > "$1"' capybara-write "$1" < "$2" && s=0; rm -f -- "$2"; exit $s`;
|
|
2522
|
+
var SKILLS_WRITE_SCRIPT = 'umask 022; s=1; mkdir -p -- "$(dirname -- "$1")" && cat -- "$2" > "$1" && chmod 0644 "$1" && s=0; rm -f -- "$2"; exit $s';
|
|
2523
|
+
var LIST_WRAPPER = 'command -v timeout >/dev/null 2>&1 && command -v python3 >/dev/null 2>&1 || exit 127; exec timeout -s KILL "$1" python3 -I -c "$2" "$3" "$4" "$5"';
|
|
2524
|
+
var NO_PYTHON = "device-e2b: the sandbox template has no python3 or no timeout (looked up in /usr/local/bin, /usr/bin, /bin), which list and scan need; build the template from the published image or add them to your own";
|
|
2525
|
+
var processGone = (e) => e instanceof E2BHttpError && e.status === 404 && e.code === "not_found";
|
|
2526
|
+
function after(ms, fn) {
|
|
2527
|
+
const t = AbortSignal.timeout(ms);
|
|
2528
|
+
t.addEventListener("abort", fn, { once: true });
|
|
2529
|
+
return { cancel: () => t.removeEventListener("abort", fn) };
|
|
2530
|
+
}
|
|
2531
|
+
function sleep(ms, signal) {
|
|
2532
|
+
return new Promise((resolve, reject) => {
|
|
2533
|
+
if (signal.aborted) {
|
|
2534
|
+
reject(signal.reason);
|
|
2535
|
+
return;
|
|
2536
|
+
}
|
|
2537
|
+
const onAbort = () => {
|
|
2538
|
+
timer.cancel();
|
|
2539
|
+
reject(signal.reason);
|
|
2540
|
+
};
|
|
2541
|
+
const timer = after(ms, () => {
|
|
2542
|
+
signal.removeEventListener("abort", onAbort);
|
|
2543
|
+
resolve();
|
|
2544
|
+
});
|
|
2545
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
2546
|
+
});
|
|
2547
|
+
}
|
|
2548
|
+
function untilAborted(p, signal) {
|
|
2549
|
+
return new Promise((resolve, reject) => {
|
|
2550
|
+
if (signal.aborted) {
|
|
2551
|
+
reject(abortReason(signal));
|
|
2552
|
+
return;
|
|
2553
|
+
}
|
|
2554
|
+
const onAbort = () => reject(abortReason(signal));
|
|
2555
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
2556
|
+
p.then(
|
|
2557
|
+
(v) => {
|
|
2558
|
+
signal.removeEventListener("abort", onAbort);
|
|
2559
|
+
resolve(v);
|
|
2560
|
+
},
|
|
2561
|
+
(e) => {
|
|
2562
|
+
signal.removeEventListener("abort", onAbort);
|
|
2563
|
+
reject(e);
|
|
2564
|
+
}
|
|
2565
|
+
);
|
|
2566
|
+
});
|
|
2567
|
+
}
|
|
2568
|
+
function checkPath2(p) {
|
|
2569
|
+
const errors = validateSandboxPathShape3(p);
|
|
2570
|
+
if (errors.length > 0)
|
|
2571
|
+
throw new Error(`invalid sandbox path ${JSON.stringify(p)}: ${errors.join("; ")}`);
|
|
2572
|
+
}
|
|
2573
|
+
function appliedNetworkOf(info) {
|
|
2574
|
+
if (info.allowInternetAccess === false) return { mode: "none", allowHosts: [] };
|
|
2575
|
+
const allowOut = info.network?.allowOut ?? [];
|
|
2576
|
+
if (allowOut.length > 0) return { mode: "allowlist", allowHosts: [...allowOut] };
|
|
2577
|
+
const denied = new Set(info.network?.denyOut ?? []);
|
|
2578
|
+
if (OPEN_PUBLIC_DENIED.every((cidr) => denied.has(cidr))) {
|
|
2579
|
+
return { mode: "open-public", allowHosts: [] };
|
|
2580
|
+
}
|
|
2581
|
+
return null;
|
|
2582
|
+
}
|
|
2583
|
+
function outdatedOpenPublic(info) {
|
|
2584
|
+
return info.allowInternetAccess !== false && (info.network?.allowOut ?? []).length === 0 && appliedNetworkOf(info) === null;
|
|
2585
|
+
}
|
|
2586
|
+
function createNetworkOf(policy) {
|
|
2587
|
+
switch (policy.kind) {
|
|
2588
|
+
case "allowlist":
|
|
2589
|
+
return { network: { allowOut: policy.hosts, denyOut: [ALL_TRAFFIC, ...IPV6_DENIED] } };
|
|
2590
|
+
case "open-public":
|
|
2591
|
+
return { allow_internet_access: true, network: { denyOut: [...OPEN_PUBLIC_DENIED] } };
|
|
2592
|
+
case "none":
|
|
2593
|
+
return { allow_internet_access: false };
|
|
2594
|
+
}
|
|
2595
|
+
}
|
|
2596
|
+
function updateNetworkOf(policy) {
|
|
2597
|
+
switch (policy.kind) {
|
|
2598
|
+
case "allowlist":
|
|
2599
|
+
return { allowOut: policy.hosts, denyOut: [ALL_TRAFFIC, ...IPV6_DENIED] };
|
|
2600
|
+
case "open-public":
|
|
2601
|
+
return { allow_internet_access: true, denyOut: [...OPEN_PUBLIC_DENIED] };
|
|
2602
|
+
case "none":
|
|
2603
|
+
return { allow_internet_access: false };
|
|
2604
|
+
}
|
|
2605
|
+
}
|
|
2606
|
+
function createE2BSandbox(cfg, deps, test = {}) {
|
|
2607
|
+
const api = createE2BApi(cfg.apiUrl ?? `https://api.${cfg.domain}`, cfg.apiKey);
|
|
2608
|
+
const enc = encodeURIComponent;
|
|
2609
|
+
const checkId = (id) => {
|
|
2610
|
+
if (!SANDBOX_ID_RE.test(id)) throw new Error(`invalid E2B sandbox id: ${JSON.stringify(id)}`);
|
|
2611
|
+
};
|
|
2612
|
+
const targetOf = (info) => {
|
|
2613
|
+
checkId(info.sandboxID);
|
|
2614
|
+
const domain = info.domain || cfg.domain;
|
|
2615
|
+
if (!HOSTNAME_RE.test(domain)) {
|
|
2616
|
+
throw new Error(
|
|
2617
|
+
`E2B returned an invalid sandbox domain: ${JSON.stringify(domain).slice(0, 100)}`
|
|
2618
|
+
);
|
|
2619
|
+
}
|
|
2620
|
+
if (typeof info.envdAccessToken !== "string" || info.envdAccessToken === "") {
|
|
2621
|
+
throw new SandboxGoneError(
|
|
2622
|
+
`E2B returned no envd access token for sandbox ${info.sandboxID}; refusing to use an unauthenticated sandbox (reset the device's files to recreate it)`
|
|
2623
|
+
);
|
|
2624
|
+
}
|
|
2625
|
+
return {
|
|
2626
|
+
url: test.envdUrl ?? `https://${ENVD_PORT}-${info.sandboxID}.${domain}`,
|
|
2627
|
+
sandboxId: info.sandboxID,
|
|
2628
|
+
accessToken: info.envdAccessToken
|
|
2629
|
+
};
|
|
2630
|
+
};
|
|
2631
|
+
const attach = async (id, signal, minSeconds = 0) => {
|
|
2632
|
+
signal.throwIfAborted();
|
|
2633
|
+
checkId(id);
|
|
2634
|
+
const timeout = Math.min(MAX_TIMEOUT_SECONDS, Math.max(cfg.timeoutSeconds, minSeconds));
|
|
2635
|
+
for (let i = 0; ; i++) {
|
|
2636
|
+
try {
|
|
2637
|
+
const r = await api.json("POST", `/v2/sandboxes/${enc(id)}/connect`, {
|
|
2638
|
+
body: { timeout },
|
|
2639
|
+
signal
|
|
2640
|
+
});
|
|
2641
|
+
return targetOf({ ...r.data, sandboxID: r.data?.sandboxID ?? id });
|
|
2642
|
+
} catch (e) {
|
|
2643
|
+
if (isE2BNotFound(e)) {
|
|
2644
|
+
throw new SandboxGoneError(`E2B sandbox ${id} no longer exists`, { cause: e });
|
|
2645
|
+
}
|
|
2646
|
+
if (!isStatus(e, 409) || i >= CONNECT_CONFLICT_RETRIES) throw e;
|
|
2647
|
+
await sleep(CONNECT_CONFLICT_WAIT_MS, signal);
|
|
2648
|
+
}
|
|
2649
|
+
}
|
|
2650
|
+
};
|
|
2651
|
+
const idsByName = async (name, state, signal) => {
|
|
2652
|
+
signal.throwIfAborted();
|
|
2653
|
+
const ids = [];
|
|
2654
|
+
let nextToken;
|
|
2655
|
+
do {
|
|
2656
|
+
const r = await api.json(
|
|
2657
|
+
"GET",
|
|
2658
|
+
"/v2/sandboxes",
|
|
2659
|
+
{
|
|
2660
|
+
query: {
|
|
2661
|
+
metadata: new URLSearchParams({ [enc(METADATA_NAME)]: enc(name) }).toString(),
|
|
2662
|
+
state,
|
|
2663
|
+
limit: String(LIST_LIMIT),
|
|
2664
|
+
nextToken
|
|
2665
|
+
},
|
|
2666
|
+
signal
|
|
2667
|
+
}
|
|
2668
|
+
);
|
|
2669
|
+
for (const s of r.data ?? []) {
|
|
2670
|
+
if (s.metadata?.[METADATA_NAME] === name) ids.push(s.sandboxID);
|
|
2671
|
+
}
|
|
2672
|
+
nextToken = r.headers.get("x-next-token") || void 0;
|
|
2673
|
+
} while (nextToken);
|
|
2674
|
+
return ids;
|
|
2675
|
+
};
|
|
2676
|
+
const kill = async (id, signal) => {
|
|
2677
|
+
checkId(id);
|
|
2678
|
+
try {
|
|
2679
|
+
await api.json("DELETE", `/sandboxes/${enc(id)}`, { signal });
|
|
2680
|
+
} catch (e) {
|
|
2681
|
+
if (!isE2BNotFound(e)) throw e;
|
|
2682
|
+
}
|
|
2683
|
+
};
|
|
2684
|
+
const pause = async (id, signal, missingOk) => {
|
|
2685
|
+
checkId(id);
|
|
2686
|
+
try {
|
|
2687
|
+
await api.json("POST", `/sandboxes/${enc(id)}/pause`, { body: { memory: false }, signal });
|
|
2688
|
+
} catch (e) {
|
|
2689
|
+
if (isStatus(e, 409) || missingOk && isE2BNotFound(e)) return;
|
|
2690
|
+
throw e;
|
|
2691
|
+
}
|
|
2692
|
+
};
|
|
2693
|
+
const alias = templateAlias(cfg.defaultImage);
|
|
2694
|
+
const ownTemplateHint = 'set the device-e2b setting `template` to a template of your own (README "Template")';
|
|
2695
|
+
const foreignAlias = (cause) => new Error(
|
|
2696
|
+
`E2B template alias ${alias} is owned by another E2B team or is public; ${ownTemplateHint}`,
|
|
2697
|
+
{ cause }
|
|
2698
|
+
);
|
|
2699
|
+
const provisionTemplate = async (rebuild) => {
|
|
2700
|
+
const timeout = AbortSignal.timeout(TEMPLATE_BUILD_TIMEOUT_MS);
|
|
2701
|
+
const signal = deps.signal ? AbortSignal.any([timeout, deps.signal]) : timeout;
|
|
2702
|
+
const checkTemplateId = (id, what) => {
|
|
2703
|
+
if (typeof id !== "string" || !TEMPLATE_ID_RE.test(id)) {
|
|
2704
|
+
throw new Error(`E2B returned an invalid template ${what}: ${JSON.stringify(id)}`);
|
|
2705
|
+
}
|
|
2706
|
+
return id;
|
|
2707
|
+
};
|
|
2708
|
+
if (!rebuild) {
|
|
2709
|
+
let found;
|
|
2710
|
+
try {
|
|
2711
|
+
found = (await api.json(
|
|
2712
|
+
"GET",
|
|
2713
|
+
`/templates/aliases/${enc(alias)}`,
|
|
2714
|
+
{ signal }
|
|
2715
|
+
)).data;
|
|
2716
|
+
} catch (e) {
|
|
2717
|
+
if (isStatus(e, 403)) throw foreignAlias(e);
|
|
2718
|
+
if (!isE2BNotFound(e)) throw e;
|
|
2719
|
+
}
|
|
2720
|
+
if (found !== void 0) {
|
|
2721
|
+
if (found.public !== false) throw foreignAlias();
|
|
2722
|
+
return checkTemplateId(found.templateID, "id");
|
|
2723
|
+
}
|
|
2724
|
+
}
|
|
2725
|
+
deps.log.info("device-e2b: building the E2B template from the published image", {
|
|
2726
|
+
alias,
|
|
2727
|
+
image: cfg.defaultImage
|
|
2728
|
+
});
|
|
2729
|
+
let req;
|
|
2730
|
+
try {
|
|
2731
|
+
req = (await api.json(
|
|
2732
|
+
"POST",
|
|
2733
|
+
"/v3/templates",
|
|
2734
|
+
{
|
|
2735
|
+
body: { name: alias, cpuCount: TEMPLATE_CPU_COUNT, memoryMB: TEMPLATE_MEMORY_MB },
|
|
2736
|
+
signal
|
|
2737
|
+
}
|
|
2738
|
+
)).data;
|
|
2739
|
+
} catch (e) {
|
|
2740
|
+
if (isStatus(e, 403, 409)) throw foreignAlias(e);
|
|
2741
|
+
throw e;
|
|
2742
|
+
}
|
|
2743
|
+
if (req?.public !== false) throw foreignAlias();
|
|
2744
|
+
const templateId = checkTemplateId(req.templateID, "id");
|
|
2745
|
+
const buildId = checkTemplateId(req.buildID, "build id");
|
|
2746
|
+
const build = `/templates/${enc(templateId)}/builds/${enc(buildId)}`;
|
|
2747
|
+
const failed = (detail, cause) => new Error(
|
|
2748
|
+
`E2B template build of ${cfg.defaultImage} (alias ${alias}) failed: ${detail}; ${ownTemplateHint}`,
|
|
2749
|
+
{ cause }
|
|
2750
|
+
);
|
|
2751
|
+
try {
|
|
2752
|
+
await api.json("POST", `/v2${build}`, {
|
|
2753
|
+
body: { fromImage: cfg.defaultImage, steps: [], force: false },
|
|
2754
|
+
signal
|
|
2755
|
+
});
|
|
2756
|
+
} catch (e) {
|
|
2757
|
+
if (signal.aborted) throw e;
|
|
2758
|
+
throw failed(e instanceof Error ? e.message : String(e), e);
|
|
2759
|
+
}
|
|
2760
|
+
let logsOffset = 0;
|
|
2761
|
+
for (; ; ) {
|
|
2762
|
+
const r = await api.json("GET", `${build}/status`, { query: { logsOffset: String(logsOffset) }, signal });
|
|
2763
|
+
logsOffset += Array.isArray(r.data?.logEntries) ? r.data.logEntries.length : 0;
|
|
2764
|
+
const status = r.data?.status;
|
|
2765
|
+
if (status === "ready") break;
|
|
2766
|
+
if (status !== "waiting" && status !== "building") {
|
|
2767
|
+
throw failed(
|
|
2768
|
+
`${String(status)}: ${String(r.data?.reason?.message ?? "no reason given").slice(0, 300)}`
|
|
2769
|
+
);
|
|
2770
|
+
}
|
|
2771
|
+
await sleep(TEMPLATE_POLL_MS, signal);
|
|
2772
|
+
}
|
|
2773
|
+
deps.log.info("device-e2b: E2B template ready", { alias, templateId });
|
|
2774
|
+
return templateId;
|
|
2775
|
+
};
|
|
2776
|
+
let template;
|
|
2777
|
+
const ensureTemplate = (stale) => {
|
|
2778
|
+
if (cfg.template) return Promise.resolve(cfg.template);
|
|
2779
|
+
if (template === void 0 || template === stale) {
|
|
2780
|
+
const p = provisionTemplate(stale !== void 0).catch((e) => {
|
|
2781
|
+
if (template === p) template = void 0;
|
|
2782
|
+
throw e;
|
|
2783
|
+
});
|
|
2784
|
+
template = p;
|
|
2785
|
+
}
|
|
2786
|
+
return template;
|
|
2787
|
+
};
|
|
2788
|
+
async function run(t, o) {
|
|
2789
|
+
o.signal.throwIfAborted();
|
|
2790
|
+
const out = [];
|
|
2791
|
+
const err = [];
|
|
2792
|
+
let used = 0;
|
|
2793
|
+
let truncated = false;
|
|
2794
|
+
let timedOut = false;
|
|
2795
|
+
let aborted = false;
|
|
2796
|
+
let stopping = false;
|
|
2797
|
+
let pid;
|
|
2798
|
+
let exitCode;
|
|
2799
|
+
const collect = (kind, b64) => {
|
|
2800
|
+
const data = Buffer.from(b64, "base64");
|
|
2801
|
+
const room = o.maxOutputBytes - used;
|
|
2802
|
+
if (data.byteLength > room) truncated = true;
|
|
2803
|
+
if (room <= 0) return;
|
|
2804
|
+
const keep = data.byteLength > room ? data.subarray(0, room) : data;
|
|
2805
|
+
used += keep.byteLength;
|
|
2806
|
+
(kind === "out" ? out : err).push(keep);
|
|
2807
|
+
};
|
|
2808
|
+
const conn = new AbortController();
|
|
2809
|
+
let grace;
|
|
2810
|
+
let killing;
|
|
2811
|
+
let wedged = false;
|
|
2812
|
+
let killedWithoutPid = false;
|
|
2813
|
+
let lateKill;
|
|
2814
|
+
const stop = () => {
|
|
2815
|
+
if (stopping) return;
|
|
2816
|
+
stopping = true;
|
|
2817
|
+
const kill2 = o.kill;
|
|
2818
|
+
if (!kill2) {
|
|
2819
|
+
conn.abort();
|
|
2820
|
+
return;
|
|
2821
|
+
}
|
|
2822
|
+
killedWithoutPid = pid === void 0;
|
|
2823
|
+
killing = (async () => {
|
|
2824
|
+
const ok = await kill2(pid).catch(() => false);
|
|
2825
|
+
if (!ok) {
|
|
2826
|
+
wedged = true;
|
|
2827
|
+
conn.abort();
|
|
2828
|
+
return;
|
|
2829
|
+
}
|
|
2830
|
+
grace = after(KILL_GRACE_MS, () => {
|
|
2831
|
+
wedged = true;
|
|
2832
|
+
conn.abort();
|
|
2833
|
+
});
|
|
2834
|
+
})();
|
|
2835
|
+
};
|
|
2836
|
+
const timer = o.timeoutMs === void 0 ? void 0 : after(o.timeoutMs, () => {
|
|
2837
|
+
timedOut = true;
|
|
2838
|
+
stop();
|
|
2839
|
+
});
|
|
2840
|
+
const onAbort = () => {
|
|
2841
|
+
aborted = true;
|
|
2842
|
+
stop();
|
|
2843
|
+
};
|
|
2844
|
+
o.signal.addEventListener("abort", onAbort, { once: true });
|
|
2845
|
+
const req = {
|
|
2846
|
+
cmd: o.cmd,
|
|
2847
|
+
args: o.args,
|
|
2848
|
+
...o.env && { envs: o.env },
|
|
2849
|
+
...o.cwd !== void 0 && { cwd: o.cwd }
|
|
2850
|
+
};
|
|
2851
|
+
let streamError;
|
|
2852
|
+
let feeding;
|
|
2853
|
+
const feed = (p) => {
|
|
2854
|
+
const data = o.stdin;
|
|
2855
|
+
if (data === void 0 || feeding) return;
|
|
2856
|
+
feeding = (async () => {
|
|
2857
|
+
if (p === void 0) throw new Error("E2B started a process without a pid");
|
|
2858
|
+
await envdSendInput(t, p, data, conn.signal);
|
|
2859
|
+
void envdCloseStdin(t, p, AbortSignal.timeout(15e3)).catch(() => void 0);
|
|
2860
|
+
})().catch(async (e) => {
|
|
2861
|
+
if (p !== void 0 && !conn.signal.aborted && !processGone(e)) {
|
|
2862
|
+
await envdKill(t, p, AbortSignal.timeout(15e3)).catch(() => void 0);
|
|
2863
|
+
}
|
|
2864
|
+
});
|
|
2865
|
+
};
|
|
2866
|
+
try {
|
|
2867
|
+
await envdStart(
|
|
2868
|
+
t,
|
|
2869
|
+
o.user,
|
|
2870
|
+
req,
|
|
2871
|
+
(e) => {
|
|
2872
|
+
if ("start" in e) {
|
|
2873
|
+
pid = e.start.pid;
|
|
2874
|
+
if (killedWithoutPid && pid !== void 0) {
|
|
2875
|
+
const late = pid;
|
|
2876
|
+
lateKill = envdKill(t, late, AbortSignal.timeout(15e3)).catch(() => {
|
|
2877
|
+
wedged = true;
|
|
2878
|
+
conn.abort();
|
|
2879
|
+
});
|
|
2880
|
+
}
|
|
2881
|
+
feed(pid);
|
|
2882
|
+
} else if ("data" in e) {
|
|
2883
|
+
if (e.data.stdout) collect("out", e.data.stdout);
|
|
2884
|
+
if (e.data.stderr) collect("err", e.data.stderr);
|
|
2885
|
+
} else if ("end" in e) exitCode = exitCodeOf(e.end);
|
|
2886
|
+
},
|
|
2887
|
+
conn.signal,
|
|
2888
|
+
o.stdin !== void 0
|
|
2889
|
+
);
|
|
2890
|
+
} catch (e) {
|
|
2891
|
+
streamError = e;
|
|
2892
|
+
}
|
|
2893
|
+
await feeding;
|
|
2894
|
+
await killing;
|
|
2895
|
+
await lateKill;
|
|
2896
|
+
grace?.cancel();
|
|
2897
|
+
if (wedged && o.recover) await o.recover().catch(() => void 0);
|
|
2898
|
+
timer?.cancel();
|
|
2899
|
+
o.signal.removeEventListener("abort", onAbort);
|
|
2900
|
+
const result = {
|
|
2901
|
+
stdout: Buffer.concat(out),
|
|
2902
|
+
stderr: Buffer.concat(err),
|
|
2903
|
+
truncated,
|
|
2904
|
+
timedOut,
|
|
2905
|
+
aborted
|
|
2906
|
+
};
|
|
2907
|
+
if (timedOut || aborted) return { ...result, exitCode: null };
|
|
2908
|
+
if (streamError !== void 0) throw streamError;
|
|
2909
|
+
if (exitCode === void 0) throw new Error("E2B process stream ended without an exit status");
|
|
2910
|
+
return { ...result, exitCode };
|
|
2911
|
+
}
|
|
2912
|
+
const killer = (t, ctl) => async (pid) => {
|
|
2913
|
+
const ok = await run(t, {
|
|
2914
|
+
user: SANDBOX_USER,
|
|
2915
|
+
cmd: "/bin/sh",
|
|
2916
|
+
args: ["-c", KILL_SCRIPT, "capybara-kill", ctl],
|
|
2917
|
+
cwd: "/",
|
|
2918
|
+
maxOutputBytes: 4096,
|
|
2919
|
+
timeoutMs: 15e3,
|
|
2920
|
+
signal: AbortSignal.timeout(2e4)
|
|
2921
|
+
}).then(
|
|
2922
|
+
(r) => r.exitCode === 0,
|
|
2923
|
+
() => false
|
|
2924
|
+
);
|
|
2925
|
+
if (pid !== void 0)
|
|
2926
|
+
await envdKill(t, pid, AbortSignal.timeout(15e3)).catch(() => void 0);
|
|
2927
|
+
return ok;
|
|
2928
|
+
};
|
|
2929
|
+
const reboot = async (id) => {
|
|
2930
|
+
deps.log.warn("device-e2b: could not stop a command cleanly; rebooting the sandbox", {
|
|
2931
|
+
sandboxId: id
|
|
2932
|
+
});
|
|
2933
|
+
const signal = AbortSignal.timeout(12e4);
|
|
2934
|
+
await pause(id, signal, false);
|
|
2935
|
+
await api.json("POST", `/v2/sandboxes/${enc(id)}/connect`, {
|
|
2936
|
+
body: { timeout: cfg.timeoutSeconds, memory: false },
|
|
2937
|
+
signal
|
|
2938
|
+
});
|
|
2939
|
+
};
|
|
2940
|
+
const fileOp = async (t, o, signal) => {
|
|
2941
|
+
const r = await run(t, {
|
|
2942
|
+
...o,
|
|
2943
|
+
cmd: "/bin/sh",
|
|
2944
|
+
cwd: "/",
|
|
2945
|
+
timeoutMs: FILE_OP_TIMEOUT_MS,
|
|
2946
|
+
signal
|
|
2947
|
+
});
|
|
2948
|
+
if (r.aborted) throw abortReason(signal);
|
|
2949
|
+
if (r.timedOut) throw new Error("sandbox file operation timed out");
|
|
2950
|
+
return r;
|
|
2951
|
+
};
|
|
2952
|
+
const listOp = async (id, op, path, limit, signal) => {
|
|
2953
|
+
const t = await attach(id, signal);
|
|
2954
|
+
return fileOp(
|
|
2955
|
+
t,
|
|
2956
|
+
{
|
|
2957
|
+
user: SANDBOX_USER,
|
|
2958
|
+
env: { PATH: test.listPath ?? LIST_PATH },
|
|
2959
|
+
args: [
|
|
2960
|
+
"-c",
|
|
2961
|
+
LIST_WRAPPER,
|
|
2962
|
+
"capybara-list",
|
|
2963
|
+
String(LIST_TIMEOUT_SECONDS),
|
|
2964
|
+
LIST_SCRIPT,
|
|
2965
|
+
op,
|
|
2966
|
+
path,
|
|
2967
|
+
String(limit)
|
|
2968
|
+
],
|
|
2969
|
+
maxOutputBytes: LIST_MAX_OUTPUT_BYTES
|
|
2970
|
+
},
|
|
2971
|
+
signal
|
|
2972
|
+
);
|
|
2973
|
+
};
|
|
2974
|
+
const stderrOf = (r) => r.stderr.toString("utf8").trim().slice(0, 500);
|
|
2975
|
+
const networkOf = (limits, name) => {
|
|
2976
|
+
const runtime = limits;
|
|
2977
|
+
const network = runtime.network;
|
|
2978
|
+
const kind = network?.kind;
|
|
2979
|
+
const hosts = network?.hosts;
|
|
2980
|
+
if (kind === "open-public") return { kind: "open-public" };
|
|
2981
|
+
if (kind !== "allowlist" || !Array.isArray(hosts) || hosts.some((h) => typeof h !== "string")) {
|
|
2982
|
+
throw new Error(
|
|
2983
|
+
'device-e2b: the sandbox limits carry no valid network (expected { kind: "allowlist", hosts } or { kind: "open-public", hosts }); this plugin implements the device-provider@1 limits of the approval-only profiles kernel (limits.network, no mode). An older kernel sent tier / egress / allowHosts instead; upgrade the kernel and the sandbox plugins together, and never open the network as a fallback.'
|
|
2984
|
+
);
|
|
2985
|
+
}
|
|
2986
|
+
for (const host of hosts) {
|
|
2987
|
+
const errors = validateAllowHost(host);
|
|
2988
|
+
if (errors.length > 0)
|
|
2989
|
+
throw new Error(`invalid network host ${JSON.stringify(host)}: ${errors.join("; ")}`);
|
|
2990
|
+
}
|
|
2991
|
+
if (!cfg.domainAllowlist) {
|
|
2992
|
+
deps.log.warn(
|
|
2993
|
+
"device-e2b: domain allowlists are disabled for this deployment (domainAllowlist is false); network allowlist degraded to none",
|
|
2994
|
+
{ name }
|
|
2995
|
+
);
|
|
2996
|
+
return { kind: "none" };
|
|
2997
|
+
}
|
|
2998
|
+
return hosts.length > 0 ? { kind: "allowlist", hosts: [...hosts] } : { kind: "none" };
|
|
2999
|
+
};
|
|
3000
|
+
const currentNetwork = async (id, signal, connected) => {
|
|
3001
|
+
signal.throwIfAborted();
|
|
3002
|
+
checkId(id);
|
|
3003
|
+
let info;
|
|
3004
|
+
try {
|
|
3005
|
+
info = (await api.json("GET", `/sandboxes/${enc(id)}`, {
|
|
3006
|
+
signal
|
|
3007
|
+
})).data ?? {};
|
|
3008
|
+
} catch (e) {
|
|
3009
|
+
if (isE2BNotFound(e))
|
|
3010
|
+
throw new SandboxGoneError(`sandbox ${id} no longer exists`, { cause: e });
|
|
3011
|
+
throw e;
|
|
3012
|
+
}
|
|
3013
|
+
if (!outdatedOpenPublic(info)) return appliedNetworkOf(info);
|
|
3014
|
+
if (!connected) await attach(id, signal);
|
|
3015
|
+
try {
|
|
3016
|
+
await updateNetwork(api, id, updateNetworkOf({ kind: "open-public" }), signal);
|
|
3017
|
+
} catch (e) {
|
|
3018
|
+
if (isE2BNotFound(e))
|
|
3019
|
+
throw new SandboxGoneError(`sandbox ${id} no longer exists`, { cause: e });
|
|
3020
|
+
throw new Error(
|
|
3021
|
+
"device-e2b: the VM's open-public network predates this plugin version and re-applying the current policy failed; the next use retries it, and resetting the device's files applies it too",
|
|
3022
|
+
{ cause: e }
|
|
3023
|
+
);
|
|
3024
|
+
}
|
|
3025
|
+
deps.log.info("device-e2b: re-applied the current open-public network to an older VM", {
|
|
3026
|
+
sandboxId: id
|
|
3027
|
+
});
|
|
3028
|
+
return { mode: "open-public", allowHosts: [] };
|
|
3029
|
+
};
|
|
3030
|
+
const targetIfRunning = async (id, signal) => {
|
|
3031
|
+
signal.throwIfAborted();
|
|
3032
|
+
checkId(id);
|
|
3033
|
+
let info;
|
|
3034
|
+
try {
|
|
3035
|
+
info = (await api.json("GET", `/sandboxes/${enc(id)}`, { signal })).data;
|
|
3036
|
+
} catch (e) {
|
|
3037
|
+
if (isE2BNotFound(e)) return void 0;
|
|
3038
|
+
throw e;
|
|
3039
|
+
}
|
|
3040
|
+
if (info?.state !== "running") return void 0;
|
|
3041
|
+
return targetOf({ ...info, sandboxID: info.sandboxID ?? id });
|
|
3042
|
+
};
|
|
3043
|
+
const stageRuntime = async (t, signal) => {
|
|
3044
|
+
deps.log.info("device-e2b: staging the operation runtime", {
|
|
3045
|
+
sandboxId: t.sandboxId,
|
|
3046
|
+
dir: RUNTIME_DIR
|
|
3047
|
+
});
|
|
3048
|
+
const dir = `${UPLOAD_DIR}/${randomUUID()}`;
|
|
3049
|
+
for (const f of runtimeFiles())
|
|
3050
|
+
await envdUpload(t, `${dir}/${f.name}`, ROOT_USER, f.data, signal);
|
|
3051
|
+
await envdUpload(t, `${dir}/SHA256SUMS`, ROOT_USER, Buffer.from(runtimeChecksums()), signal);
|
|
3052
|
+
const r = await run(t, {
|
|
3053
|
+
user: ROOT_USER,
|
|
3054
|
+
cmd: "/bin/sh",
|
|
3055
|
+
args: ["-c", STAGE_RUNTIME_SCRIPT, "capybara-stage", dir, RUNTIME_DIR],
|
|
3056
|
+
cwd: "/",
|
|
3057
|
+
maxOutputBytes: STDERR_ROOM,
|
|
3058
|
+
timeoutMs: 6e4,
|
|
3059
|
+
signal
|
|
3060
|
+
});
|
|
3061
|
+
if (r.exitCode !== 0) {
|
|
3062
|
+
throw new Error(`device-e2b: cannot stage the operation runtime: ${stderrOf(r)}`);
|
|
3063
|
+
}
|
|
3064
|
+
};
|
|
3065
|
+
const agentMissing = (o, r) => o.cmd.includes(OPERATION_AGENT) && r.exitCode === 2 && r.stderr.toString("utf8").includes("can't open file") && r.stdout.byteLength === 0;
|
|
3066
|
+
const killAgent = (t) => async (pid) => {
|
|
3067
|
+
if (pid === void 0) return true;
|
|
3068
|
+
return envdKill(t, pid, AbortSignal.timeout(15e3)).then(
|
|
3069
|
+
() => true,
|
|
3070
|
+
() => false
|
|
3071
|
+
);
|
|
3072
|
+
};
|
|
3073
|
+
const recoverAgent = async (id, resume) => {
|
|
3074
|
+
try {
|
|
3075
|
+
if (!resume) {
|
|
3076
|
+
const live = await targetIfRunning(id, AbortSignal.timeout(3e4));
|
|
3077
|
+
if (live === void 0) return false;
|
|
3078
|
+
}
|
|
3079
|
+
await reboot(id);
|
|
3080
|
+
return true;
|
|
3081
|
+
} catch (e) {
|
|
3082
|
+
deps.log.warn("device-e2b: sandbox reboot failed; an agent command may still be running", {
|
|
3083
|
+
sandboxId: id,
|
|
3084
|
+
err: (e instanceof Error ? e.message : String(e)).slice(0, 500)
|
|
3085
|
+
});
|
|
3086
|
+
return false;
|
|
3087
|
+
}
|
|
3088
|
+
};
|
|
3089
|
+
const runAgent = async (t, o, resume) => {
|
|
3090
|
+
const root = o.user === "root";
|
|
3091
|
+
const args = root ? rootAgentArgs(o.cmd) : o.cmd.slice(1);
|
|
3092
|
+
let unconfirmed = false;
|
|
3093
|
+
const once = () => run(t, {
|
|
3094
|
+
user: o.user === "root" ? ROOT_USER : SANDBOX_USER,
|
|
3095
|
+
cmd: o.cmd[0],
|
|
3096
|
+
args,
|
|
3097
|
+
...o.env && { env: o.env },
|
|
3098
|
+
...o.stdin && { stdin: o.stdin },
|
|
3099
|
+
cwd: "/",
|
|
3100
|
+
maxOutputBytes: o.maxOutputBytes,
|
|
3101
|
+
...o.timeoutMs !== void 0 && { timeoutMs: o.timeoutMs },
|
|
3102
|
+
signal: o.signal,
|
|
3103
|
+
kill: killAgent(t),
|
|
3104
|
+
recover: async () => {
|
|
3105
|
+
unconfirmed = !await recoverAgent(t.sandboxId, resume);
|
|
3106
|
+
}
|
|
3107
|
+
});
|
|
3108
|
+
const checked = async () => {
|
|
3109
|
+
const r2 = await once();
|
|
3110
|
+
if (unconfirmed) {
|
|
3111
|
+
throw new SandboxOperationOutcomeUnknownError2(
|
|
3112
|
+
"device-e2b: an interrupted agent command could not be confirmed stopped and the VM was not rebooted (it is not running); its outcome is unknown"
|
|
3113
|
+
);
|
|
3114
|
+
}
|
|
3115
|
+
return r2;
|
|
3116
|
+
};
|
|
3117
|
+
const r = await checked();
|
|
3118
|
+
const unverified = root && (r.exitCode === 3 || r.exitCode === 4) && r.stderr.toString("utf8").includes("capybara-precheck:");
|
|
3119
|
+
if (!agentMissing(o, r) && !unverified) return r;
|
|
3120
|
+
await stageRuntime(t, o.signal);
|
|
3121
|
+
return checked();
|
|
3122
|
+
};
|
|
3123
|
+
const pausedMeanwhile = (e) => isStatus(e, 502);
|
|
3124
|
+
const execWith = async (h, args, ctl, prefix = [], nonce) => {
|
|
3125
|
+
args.signal.throwIfAborted();
|
|
3126
|
+
const env = { HOME: "/workspace", LANG: "C.UTF-8" };
|
|
3127
|
+
for (const [k, v] of Object.entries(args.env ?? {})) {
|
|
3128
|
+
if (!ENV_KEY_RE2.test(k)) throw new Error(`invalid environment variable name: ${k}`);
|
|
3129
|
+
env[k] = v;
|
|
3130
|
+
}
|
|
3131
|
+
if (nonce !== void 0) env.CAPYBARA_ADMISSION_NONCE = nonce;
|
|
3132
|
+
env[EXEC_MARKER] = ctl;
|
|
3133
|
+
const t = await attach(
|
|
3134
|
+
h.id,
|
|
3135
|
+
args.signal,
|
|
3136
|
+
Math.ceil(args.timeoutMs / 1e3) + TIMEOUT_MARGIN_SECONDS
|
|
3137
|
+
);
|
|
3138
|
+
const wrapper = execArgs(ctl, args.command, args.timeoutMs);
|
|
3139
|
+
const r = await run(t, {
|
|
3140
|
+
user: SANDBOX_USER,
|
|
3141
|
+
cmd: prefix.length > 0 ? prefix[0] : "setsid",
|
|
3142
|
+
args: prefix.length > 0 ? [...prefix.slice(1), "setsid", ...wrapper] : wrapper,
|
|
3143
|
+
env,
|
|
3144
|
+
cwd: args.cwd ?? "/workspace",
|
|
3145
|
+
maxOutputBytes: args.maxOutputBytes,
|
|
3146
|
+
timeoutMs: args.timeoutMs,
|
|
3147
|
+
signal: args.signal,
|
|
3148
|
+
kill: killer(t, ctl),
|
|
3149
|
+
// 重启也失败时只记日志(错误信息来自 API 正文与状态码,不含 apiKey 与访问令牌),照常返回本次结果
|
|
3150
|
+
recover: () => reboot(h.id).catch((e) => {
|
|
3151
|
+
deps.log.warn(
|
|
3152
|
+
"device-e2b: sandbox reboot failed; the stuck command may still be running",
|
|
3153
|
+
{
|
|
3154
|
+
sandboxId: h.id,
|
|
3155
|
+
err: (e instanceof Error ? e.message : String(e)).slice(0, 500)
|
|
3156
|
+
}
|
|
3157
|
+
);
|
|
3158
|
+
})
|
|
3159
|
+
});
|
|
3160
|
+
if (prefix.length > 0 && r.exitCode === 125 && r.stdout[0] === 82) {
|
|
3161
|
+
const refused = refusedFrame(r.stdout, nonce ?? "");
|
|
3162
|
+
if (refused === "cancelled") {
|
|
3163
|
+
return { exitCode: null, stdout: "", stderr: "", truncated: false, timedOut: false };
|
|
3164
|
+
}
|
|
3165
|
+
if (refused) throw refused;
|
|
3166
|
+
}
|
|
3167
|
+
return {
|
|
3168
|
+
exitCode: r.exitCode,
|
|
3169
|
+
stdout: decodeOutput(r.stdout, r.truncated),
|
|
3170
|
+
stderr: decodeOutput(r.stderr, r.truncated),
|
|
3171
|
+
truncated: r.truncated,
|
|
3172
|
+
timedOut: r.timedOut
|
|
3173
|
+
};
|
|
3174
|
+
};
|
|
3175
|
+
const transport = {
|
|
3176
|
+
async run(h, o) {
|
|
3177
|
+
const t = await attach(h.id, o.signal);
|
|
3178
|
+
return runAgent(t, o, true);
|
|
3179
|
+
},
|
|
3180
|
+
async session(h, signal) {
|
|
3181
|
+
const t = await attach(h.id, signal);
|
|
3182
|
+
return (o) => runAgent(t, o, true);
|
|
3183
|
+
},
|
|
3184
|
+
async runIfRunning(h, o) {
|
|
3185
|
+
const t = await targetIfRunning(h.id, o.signal);
|
|
3186
|
+
if (t === void 0) return "not-running";
|
|
3187
|
+
try {
|
|
3188
|
+
return await runAgent(t, o, false);
|
|
3189
|
+
} catch (e) {
|
|
3190
|
+
if (pausedMeanwhile(e)) return "not-running";
|
|
3191
|
+
throw e;
|
|
3192
|
+
}
|
|
3193
|
+
},
|
|
3194
|
+
execFull: (h, args, ctl, prefix, nonce) => execWith(h, args, ctl, prefix, nonce),
|
|
3195
|
+
appliedNetwork: (h, signal) => currentNetwork(h.id, signal, false),
|
|
3196
|
+
async killOperation(h, uuid, signal) {
|
|
3197
|
+
if (!/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/.test(uuid)) {
|
|
3198
|
+
throw new Error("invalid operation control id");
|
|
3199
|
+
}
|
|
3200
|
+
const t = await targetIfRunning(h.id, signal);
|
|
3201
|
+
if (t === void 0) return;
|
|
3202
|
+
const r = await run(t, {
|
|
3203
|
+
user: SANDBOX_USER,
|
|
3204
|
+
cmd: "/bin/sh",
|
|
3205
|
+
args: ["-c", KILL_OPERATION_SCRIPT, "capybara-killop", uuid],
|
|
3206
|
+
cwd: "/",
|
|
3207
|
+
maxOutputBytes: STDERR_ROOM,
|
|
3208
|
+
timeoutMs: 15e3,
|
|
3209
|
+
signal: AbortSignal.any([signal, AbortSignal.timeout(2e4)])
|
|
3210
|
+
}).catch(() => null);
|
|
3211
|
+
if (r?.exitCode !== 0) await reboot(h.id);
|
|
3212
|
+
},
|
|
3213
|
+
loseBackgroundAck: test.loseBackgroundAck,
|
|
3214
|
+
backgroundStartCommand: test.backgroundStartCommand
|
|
3215
|
+
};
|
|
3216
|
+
const operations = createOperations({
|
|
3217
|
+
transport,
|
|
3218
|
+
decode: decodeOutput,
|
|
3219
|
+
network: { allowlist: cfg.domainAllowlist }
|
|
3220
|
+
});
|
|
3221
|
+
return {
|
|
3222
|
+
// envd runs every process.Start independently; connect on a running sandbox only extends its
|
|
3223
|
+
// timeout, so concurrent auto-starts are idempotent (a 409 from a resume in flight is retried)
|
|
3224
|
+
concurrentExec: true,
|
|
3225
|
+
// the instance is the file system's sandbox resumed: create-time limits are those of
|
|
3226
|
+
// createFileSystem; `update` is the only way to change them without a file reset
|
|
3227
|
+
limitsApplyAt: "fileSystem",
|
|
3228
|
+
// allowOut + denyOut 0.0.0.0/0; without domainAllowlist an allowlist degrades to no network
|
|
3229
|
+
// (closed, never open), so an allowlist is always enforced
|
|
3230
|
+
networkAllowlist: true,
|
|
3231
|
+
// The device report: what the sandbox is, no home, no limits of its own (the template fixes
|
|
3232
|
+
// cpu, memory and disk), and files can be reset. The revision names the plugin version, the
|
|
3233
|
+
// template and the instance.
|
|
3234
|
+
async report(device, signal) {
|
|
3235
|
+
signal.throwIfAborted();
|
|
3236
|
+
const template2 = cfg.template || templateAlias(cfg.defaultImage);
|
|
3237
|
+
return {
|
|
3238
|
+
revision: `${cfg.version ?? "unversioned"}:${template2}:${device.handle?.id ?? "none"}`,
|
|
3239
|
+
description: {
|
|
3240
|
+
en: "An E2B sandbox (microVM) whose files are kept by pausing it; the microVM is the sandbox. E2B's metadata service stays reachable (see README).",
|
|
3241
|
+
zh: "E2B 沙箱(微型虚拟机),暂停时保留文件;虚拟机本身就是沙箱。E2B 元数据端点仍可达(见 README)。"
|
|
3242
|
+
},
|
|
3243
|
+
home: null,
|
|
3244
|
+
limits: [],
|
|
3245
|
+
reset: true
|
|
3246
|
+
};
|
|
3247
|
+
},
|
|
3248
|
+
async createFileSystem({ name, limits, signal }) {
|
|
3249
|
+
signal.throwIfAborted();
|
|
3250
|
+
if (!NAME_RE.test(name)) throw new Error(`invalid sandbox name: ${name}`);
|
|
3251
|
+
checkLimits(limits);
|
|
3252
|
+
const policy = networkOf(limits, name);
|
|
3253
|
+
const [existing] = await idsByName(name, "running,paused", signal);
|
|
3254
|
+
if (existing !== void 0) {
|
|
3255
|
+
checkId(existing);
|
|
3256
|
+
return { id: existing };
|
|
3257
|
+
}
|
|
3258
|
+
const network = createNetworkOf(policy);
|
|
3259
|
+
const createFrom = (templateID) => api.json("POST", "/v2/sandboxes", {
|
|
3260
|
+
body: {
|
|
3261
|
+
templateID,
|
|
3262
|
+
metadata: { [METADATA_NAME]: name },
|
|
3263
|
+
timeout: cfg.timeoutSeconds,
|
|
3264
|
+
// paused rather than killed at the timeout, with the file system only (like
|
|
3265
|
+
// suspend): the files survive even when the kernel never gets to suspend
|
|
3266
|
+
autoPause: true,
|
|
3267
|
+
autoPauseMemory: false,
|
|
3268
|
+
// envd requires an access token (the e2b SDK's default); targetOf refuses without one
|
|
3269
|
+
secure: true,
|
|
3270
|
+
...network
|
|
3271
|
+
},
|
|
3272
|
+
signal
|
|
3273
|
+
});
|
|
3274
|
+
const first = ensureTemplate();
|
|
3275
|
+
let r;
|
|
3276
|
+
try {
|
|
3277
|
+
r = await createFrom(await untilAborted(first, signal));
|
|
3278
|
+
} catch (e) {
|
|
3279
|
+
if (cfg.template || !isE2BNotFound(e)) throw e;
|
|
3280
|
+
r = await createFrom(await untilAborted(ensureTemplate(first), signal));
|
|
3281
|
+
}
|
|
3282
|
+
if (!r.data?.sandboxID) throw new Error("E2B create sandbox returned no sandboxID");
|
|
3283
|
+
checkId(r.data.sandboxID);
|
|
3284
|
+
await pause(r.data.sandboxID, signal, false);
|
|
3285
|
+
return { id: r.data.sandboxID };
|
|
3286
|
+
},
|
|
3287
|
+
/**
|
|
3288
|
+
* Applies the network policy of `limits` to the running sandbox (`PUT /sandboxes/{id}/network`,
|
|
3289
|
+
* which replaces the whole network configuration). E2B updates only running sandboxes (a paused
|
|
3290
|
+
* one answers E2B's 404), so the sandbox is connected (resumed) first; the kernel calls `update`
|
|
3291
|
+
* right after `resume` anyway. A refused policy throws before any call. A sandbox that is gone →
|
|
3292
|
+
* SandboxGoneError. cpu / memory / disk are fixed by the template and never change here.
|
|
3293
|
+
*/
|
|
3294
|
+
async update(h, limits, signal) {
|
|
3295
|
+
signal.throwIfAborted();
|
|
3296
|
+
checkLimits(limits);
|
|
3297
|
+
const policy = networkOf(limits, h.id);
|
|
3298
|
+
await attach(h.id, signal);
|
|
3299
|
+
try {
|
|
3300
|
+
await updateNetwork(api, h.id, updateNetworkOf(policy), signal);
|
|
3301
|
+
} catch (e) {
|
|
3302
|
+
if (isE2BNotFound(e)) {
|
|
3303
|
+
throw new SandboxGoneError(`E2B sandbox ${h.id} no longer exists`, { cause: e });
|
|
3304
|
+
}
|
|
3305
|
+
if (signal.aborted || isStatus(e, 401) || isStatus(e, 403)) throw e;
|
|
3306
|
+
deps.log.warn("device-e2b: network update failed; answering recreate", {
|
|
3307
|
+
sandboxId: h.id,
|
|
3308
|
+
err: (e instanceof Error ? e.message : String(e)).slice(0, 300)
|
|
3309
|
+
});
|
|
3310
|
+
return "recreate";
|
|
3311
|
+
}
|
|
3312
|
+
deps.log.info("device-e2b: sandbox network updated", {
|
|
3313
|
+
sandboxId: h.id,
|
|
3314
|
+
network: policy.kind
|
|
3315
|
+
});
|
|
3316
|
+
return "updated";
|
|
3317
|
+
},
|
|
3318
|
+
async destroyFileSystem(fs, signal) {
|
|
3319
|
+
signal.throwIfAborted();
|
|
3320
|
+
await kill(fs.id, signal);
|
|
3321
|
+
},
|
|
3322
|
+
async destroyFileSystemByName(name, signal) {
|
|
3323
|
+
for (const id of await idsByName(name, "running,paused", signal)) await kill(id, signal);
|
|
3324
|
+
},
|
|
3325
|
+
async create({ name, fileSystem, signal }) {
|
|
3326
|
+
signal.throwIfAborted();
|
|
3327
|
+
if (!NAME_RE.test(name)) throw new Error(`invalid sandbox name: ${name}`);
|
|
3328
|
+
try {
|
|
3329
|
+
await attach(fileSystem.id, signal);
|
|
3330
|
+
} catch (e) {
|
|
3331
|
+
if (e instanceof SandboxGoneError && isE2BNotFound(e.cause)) {
|
|
3332
|
+
throw new FileSystemGoneError(
|
|
3333
|
+
`E2B sandbox ${fileSystem.id} holding the device's files no longer exists`,
|
|
3334
|
+
{ cause: e }
|
|
3335
|
+
);
|
|
3336
|
+
}
|
|
3337
|
+
throw e;
|
|
3338
|
+
}
|
|
3339
|
+
return { id: fileSystem.id };
|
|
3340
|
+
},
|
|
3341
|
+
async resume(h, signal) {
|
|
3342
|
+
try {
|
|
3343
|
+
await attach(h.id, signal);
|
|
3344
|
+
await currentNetwork(h.id, signal, true).catch((e) => {
|
|
3345
|
+
if (signal.aborted) throw e;
|
|
3346
|
+
deps.log.warn("device-e2b: could not re-apply the current network on resume", {
|
|
3347
|
+
sandboxId: h.id,
|
|
3348
|
+
err: (e instanceof Error ? e.message : String(e)).slice(0, 300)
|
|
3349
|
+
});
|
|
3350
|
+
});
|
|
3351
|
+
} catch (e) {
|
|
3352
|
+
if (e instanceof SandboxGoneError) return "gone";
|
|
3353
|
+
throw e;
|
|
3354
|
+
}
|
|
3355
|
+
return "resumed";
|
|
3356
|
+
},
|
|
3357
|
+
async suspend(h, signal) {
|
|
3358
|
+
signal.throwIfAborted();
|
|
3359
|
+
await pause(h.id, signal, true);
|
|
3360
|
+
},
|
|
3361
|
+
async destroy(h, signal) {
|
|
3362
|
+
signal.throwIfAborted();
|
|
3363
|
+
await pause(h.id, signal, true);
|
|
3364
|
+
},
|
|
3365
|
+
async destroyByName(name, signal) {
|
|
3366
|
+
for (const id of await idsByName(name, "running", signal)) await pause(id, signal, true);
|
|
3367
|
+
},
|
|
3368
|
+
async killAll(h, signal) {
|
|
3369
|
+
signal.throwIfAborted();
|
|
3370
|
+
const t = await attach(h.id, signal);
|
|
3371
|
+
const r = await fileOp(
|
|
3372
|
+
t,
|
|
3373
|
+
{
|
|
3374
|
+
user: SANDBOX_USER,
|
|
3375
|
+
args: ["-c", KILL_ALL_SCRIPT, "capybara-killall"],
|
|
3376
|
+
maxOutputBytes: STDERR_ROOM
|
|
3377
|
+
},
|
|
3378
|
+
signal
|
|
3379
|
+
);
|
|
3380
|
+
if (r.exitCode !== 0) throw new Error(`cannot stop leftover commands: ${stderrOf(r)}`);
|
|
3381
|
+
},
|
|
3382
|
+
async killTagged(h, tags, signal) {
|
|
3383
|
+
signal.throwIfAborted();
|
|
3384
|
+
for (const t2 of tags) {
|
|
3385
|
+
if (!SANDBOX_TAG_RE.test(t2)) throw new Error("invalid sandbox tag");
|
|
3386
|
+
}
|
|
3387
|
+
if (tags.length === 0) return;
|
|
3388
|
+
const t = await attach(h.id, signal);
|
|
3389
|
+
const r = await fileOp(
|
|
3390
|
+
t,
|
|
3391
|
+
{
|
|
3392
|
+
user: SANDBOX_USER,
|
|
3393
|
+
args: ["-c", KILL_TAGGED_SCRIPT, "capybara-killtagged", ...tags],
|
|
3394
|
+
maxOutputBytes: STDERR_ROOM
|
|
3395
|
+
},
|
|
3396
|
+
signal
|
|
3397
|
+
);
|
|
3398
|
+
if (r.exitCode !== 0)
|
|
3399
|
+
throw new Error(`cannot stop crashed holders' commands: ${stderrOf(r)}`);
|
|
3400
|
+
},
|
|
3401
|
+
async exec(h, args) {
|
|
3402
|
+
return execWith(h, args, execControlPath(args.tag));
|
|
3403
|
+
},
|
|
3404
|
+
async readFile(h, path, opts) {
|
|
3405
|
+
opts.signal.throwIfAborted();
|
|
3406
|
+
checkPath2(path);
|
|
3407
|
+
const t = await attach(h.id, opts.signal);
|
|
3408
|
+
const want = opts.maxBytes + 1;
|
|
3409
|
+
const r = await fileOp(
|
|
3410
|
+
t,
|
|
3411
|
+
{
|
|
3412
|
+
user: SANDBOX_USER,
|
|
3413
|
+
args: ["-c", READ_SCRIPT, "capybara-read", path, String(want)],
|
|
3414
|
+
maxOutputBytes: want + STDERR_ROOM
|
|
3415
|
+
},
|
|
3416
|
+
opts.signal
|
|
3417
|
+
);
|
|
3418
|
+
if (r.exitCode === 3 || r.exitCode === 4) return null;
|
|
3419
|
+
if (r.exitCode !== 0) throw new Error(`cannot read ${path}: ${stderrOf(r)}`);
|
|
3420
|
+
const truncated = r.stdout.byteLength > opts.maxBytes;
|
|
3421
|
+
const data = truncated ? r.stdout.subarray(0, opts.maxBytes) : r.stdout;
|
|
3422
|
+
return { data: new Uint8Array(data), truncated };
|
|
3423
|
+
},
|
|
3424
|
+
async writeFile(h, path, data, signal) {
|
|
3425
|
+
signal.throwIfAborted();
|
|
3426
|
+
checkPath2(path);
|
|
3427
|
+
const t = await attach(h.id, signal);
|
|
3428
|
+
const staging = `${UPLOAD_DIR}/${randomUUID()}`;
|
|
3429
|
+
await envdUpload(t, staging, ROOT_USER, data, signal);
|
|
3430
|
+
const skills = path === "/skills" || path.startsWith("/skills/");
|
|
3431
|
+
const r = await fileOp(
|
|
3432
|
+
t,
|
|
3433
|
+
{
|
|
3434
|
+
user: ROOT_USER,
|
|
3435
|
+
args: [
|
|
3436
|
+
"-c",
|
|
3437
|
+
skills ? SKILLS_WRITE_SCRIPT : USER_WRITE_SCRIPT,
|
|
3438
|
+
"capybara-write",
|
|
3439
|
+
path,
|
|
3440
|
+
staging
|
|
3441
|
+
],
|
|
3442
|
+
maxOutputBytes: STDERR_ROOM
|
|
3443
|
+
},
|
|
3444
|
+
signal
|
|
3445
|
+
);
|
|
3446
|
+
if (r.exitCode !== 0) throw new Error(`cannot write ${path}: ${stderrOf(r)}`);
|
|
3447
|
+
},
|
|
3448
|
+
async stat(h, path, signal) {
|
|
3449
|
+
signal.throwIfAborted();
|
|
3450
|
+
checkPath2(path);
|
|
3451
|
+
const t = await attach(h.id, signal);
|
|
3452
|
+
const r = await fileOp(
|
|
3453
|
+
t,
|
|
3454
|
+
{
|
|
3455
|
+
user: SANDBOX_USER,
|
|
3456
|
+
args: ["-c", STAT_SCRIPT, "capybara-stat", path],
|
|
3457
|
+
maxOutputBytes: STDERR_ROOM
|
|
3458
|
+
},
|
|
3459
|
+
signal
|
|
3460
|
+
);
|
|
3461
|
+
if (r.exitCode === 3) return null;
|
|
3462
|
+
const m = r.stdout.toString("utf8").match(/^(\d+) (file|directory)\n?$/);
|
|
3463
|
+
if (r.exitCode !== 0 || !m) throw new Error(`cannot stat ${path}: ${stderrOf(r)}`);
|
|
3464
|
+
return { size: Number(m[1]), isDir: m[2] === "directory" };
|
|
3465
|
+
},
|
|
3466
|
+
// the sandbox's workspace is the whole area a cloud device offers (core spec 2026-11-14 §4.2):
|
|
3467
|
+
// the microVM is the sandbox, and the kernel scopes a cloud device's projects to it
|
|
3468
|
+
async list(h, path, opts) {
|
|
3469
|
+
opts.signal.throwIfAborted();
|
|
3470
|
+
const p = checkListPath(path);
|
|
3471
|
+
const r = await listOp(h.id, "list", p, listLimit(opts.limit), opts.signal);
|
|
3472
|
+
return finishList(p, r, NO_PYTHON);
|
|
3473
|
+
},
|
|
3474
|
+
async scan(h, base, opts) {
|
|
3475
|
+
opts.signal.throwIfAborted();
|
|
3476
|
+
const b = checkListPath(base);
|
|
3477
|
+
const r = await listOp(h.id, "scan", b, 0, opts.signal);
|
|
3478
|
+
return finishScan(b, r, NO_PYTHON);
|
|
3479
|
+
},
|
|
3480
|
+
// The operation contract (operations.ts): fresh immutable scopes per invocation, on the
|
|
3481
|
+
// whole instance (the microVM is the sandbox, see permissionSupport).
|
|
3482
|
+
permissionSupport: operations.permissionSupport,
|
|
3483
|
+
operation: operations.operation,
|
|
3484
|
+
prepare: operations.prepare,
|
|
3485
|
+
browse: operations.browse,
|
|
3486
|
+
revokeOperation: operations.revokeOperation,
|
|
3487
|
+
background: operations.background
|
|
3488
|
+
};
|
|
3489
|
+
}
|
|
3490
|
+
var SIGNALS = {
|
|
3491
|
+
hangup: 1,
|
|
3492
|
+
interrupt: 2,
|
|
3493
|
+
quit: 3,
|
|
3494
|
+
"illegal instruction": 4,
|
|
3495
|
+
"trace/breakpoint trap": 5,
|
|
3496
|
+
aborted: 6,
|
|
3497
|
+
"bus error": 7,
|
|
3498
|
+
"floating point exception": 8,
|
|
3499
|
+
killed: 9,
|
|
3500
|
+
"user defined signal 1": 10,
|
|
3501
|
+
"segmentation fault": 11,
|
|
3502
|
+
"user defined signal 2": 12,
|
|
3503
|
+
"broken pipe": 13,
|
|
3504
|
+
"alarm clock": 14,
|
|
3505
|
+
terminated: 15
|
|
3506
|
+
};
|
|
3507
|
+
function exitCodeOf(end) {
|
|
3508
|
+
const status = end.status ?? "";
|
|
3509
|
+
if (end.exited === true || status.startsWith("exit status ")) return end.exitCode ?? 0;
|
|
3510
|
+
const m = /^signal: (.+?)(?: \(core dumped\))?$/.exec(status);
|
|
3511
|
+
const n = m ? SIGNALS[m[1]] : void 0;
|
|
3512
|
+
return n !== void 0 ? 128 + n : null;
|
|
3513
|
+
}
|
|
3514
|
+
function decodeOutput(buf, truncated) {
|
|
3515
|
+
let end = buf.byteLength;
|
|
3516
|
+
if (truncated) {
|
|
3517
|
+
for (let i = end - 1; i >= 0 && i >= end - 4; i--) {
|
|
3518
|
+
const b = buf[i];
|
|
3519
|
+
if ((b & 192) === 128) continue;
|
|
3520
|
+
const need = b >= 240 ? 4 : b >= 224 ? 3 : b >= 192 ? 2 : 1;
|
|
3521
|
+
if (end - i < need) end = i;
|
|
3522
|
+
break;
|
|
3523
|
+
}
|
|
3524
|
+
}
|
|
3525
|
+
return buf.subarray(0, end).toString("utf8");
|
|
3526
|
+
}
|
|
3527
|
+
function checkLimits(l) {
|
|
3528
|
+
if (!(l.cpu > 0) || !(l.memoryMb > 0) || !(l.diskMb > 0)) {
|
|
3529
|
+
throw new Error("sandbox limits cpu, memoryMb and diskMb must be positive");
|
|
3530
|
+
}
|
|
3531
|
+
}
|
|
3532
|
+
|
|
3533
|
+
// src/index.ts
|
|
3534
|
+
var MAX_TIMEOUT_SECONDS2 = 86400;
|
|
3535
|
+
var DeploymentConfig = z.strictObject(
|
|
3536
|
+
{
|
|
3537
|
+
/** E2B API key(`e2b_…`)。 */
|
|
3538
|
+
apiKey: ui(secret(z.string().min(1)), { label: { zh: "API 密钥", en: "API key" } }),
|
|
3539
|
+
/** 控制面 API 地址;不填为 `https://api.<domain>`。自托管 E2B 填自己的地址。 */
|
|
3540
|
+
apiUrl: ui(z.url({ protocol: /^https?$/ }).optional(), {
|
|
3541
|
+
label: { zh: "接口地址", en: "API URL" }
|
|
3542
|
+
}),
|
|
3543
|
+
/** 沙箱域名(envd 地址为 `https://49983-<沙箱 id>.<domain>`);API 返回了沙箱自己的域名时以返回的为准。 */
|
|
3544
|
+
domain: ui(
|
|
3545
|
+
z.string().regex(HOSTNAME_RE, {
|
|
3546
|
+
message: "domain must be a lowercase hostname such as e2b.app"
|
|
3547
|
+
}).default("e2b.app"),
|
|
3548
|
+
{ label: { zh: "沙箱域名", en: "Sandbox domain" } }
|
|
3549
|
+
),
|
|
3550
|
+
/**
|
|
3551
|
+
* Template name or id. Unset: the plugin builds the template `capybara-sandbox-<tag>` from the
|
|
3552
|
+
* published sandbox image on first use and reuses it (README "Template").
|
|
3553
|
+
*/
|
|
3554
|
+
template: ui(z.string().min(1).optional(), { label: { zh: "模板", en: "Template" } }),
|
|
3555
|
+
/**
|
|
3556
|
+
* 沙箱的超时(秒,默认 3600):到点 E2B 自动暂停(不是删除)沙箱,文件保留。内核每次用沙箱时把它往后推,
|
|
3557
|
+
* 长命令会按命令超时再加余量推后;空闲休眠与过期销毁由内核负责。
|
|
3558
|
+
*/
|
|
3559
|
+
timeoutSeconds: ui(z.number().int().min(60).max(MAX_TIMEOUT_SECONDS2).default(3600), {
|
|
3560
|
+
label: { zh: "沙箱超时", en: "Sandbox timeout" }
|
|
3561
|
+
}),
|
|
3562
|
+
/**
|
|
3563
|
+
* Whether E2B supports a per-domain network allowlist (E2B cloud: yes). `false` (self-hosted
|
|
3564
|
+
* versions without it) degrades an allowlist to no network with a warning. `open-public` does
|
|
3565
|
+
* not depend on it (internet access with the local ranges denied).
|
|
3566
|
+
*/
|
|
3567
|
+
domainAllowlist: ui(z.boolean().default(true), {
|
|
3568
|
+
label: { zh: "按域名出站白名单", en: "Domain egress allowlist" }
|
|
3569
|
+
})
|
|
3570
|
+
},
|
|
3571
|
+
{
|
|
3572
|
+
// removed keys fail with a pointer to the network policy (pre-0.1.0: no compatibility layer)
|
|
3573
|
+
error: (iss) => iss.code === "unrecognized_keys" && (iss.keys.includes("openEgress") || iss.keys.includes("allowOpenEgress")) ? 'openEgress / allowOpenEgress were removed: the org cloud network decides (open-public or an allowlist, sent as limits.network); see the device-e2b README "Network policy".' : void 0
|
|
3574
|
+
}
|
|
3575
|
+
);
|
|
3576
|
+
function providerConfig(c, version = plugin.version) {
|
|
3577
|
+
return { ...c, defaultImage: publishedImage(version), version };
|
|
3578
|
+
}
|
|
3579
|
+
var plugin = defineExtension({
|
|
3580
|
+
name: "device-e2b",
|
|
3581
|
+
version: "0.1.0-rc.21",
|
|
3582
|
+
// device-provider@1 (report, networkAllowlist, scopes with projectRoot / writeRoots) is the
|
|
3583
|
+
// rc.51 SDK's.
|
|
3584
|
+
sdk: "^0.1.0-rc.51",
|
|
3585
|
+
provides: [DEVICE_PROVIDER_EXTENSION_POINT],
|
|
3586
|
+
// Only the deployment config and E2B's API: no platform capability to declare
|
|
3587
|
+
permissions: {},
|
|
3588
|
+
config: { deployment: DeploymentConfig },
|
|
3589
|
+
setup(ctx) {
|
|
3590
|
+
ctx.provide(
|
|
3591
|
+
DEVICE_PROVIDER_EXTENSION_POINT,
|
|
3592
|
+
createE2BSandbox(providerConfig(ctx.config), {
|
|
3593
|
+
log: ctx.log,
|
|
3594
|
+
signal: ctx.signal
|
|
3595
|
+
})
|
|
3596
|
+
);
|
|
3597
|
+
}
|
|
3598
|
+
});
|
|
3599
|
+
var index_default = plugin;
|
|
3600
|
+
export {
|
|
3601
|
+
DeploymentConfig,
|
|
3602
|
+
IN_SANDBOX_GRACE_SECONDS,
|
|
3603
|
+
METADATA_NAME,
|
|
3604
|
+
createE2BSandbox,
|
|
3605
|
+
decodeOutput,
|
|
3606
|
+
index_default as default,
|
|
3607
|
+
execArgs,
|
|
3608
|
+
exitCodeOf,
|
|
3609
|
+
providerConfig,
|
|
3610
|
+
publishedImage,
|
|
3611
|
+
templateAlias
|
|
3612
|
+
};
|