overvues 0.0.1 → 0.4.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (78) hide show
  1. package/README.md +58 -2
  2. package/dist/ansi.js +24 -0
  3. package/dist/attach-client.js +586 -0
  4. package/dist/auth-connect-browser.js +112 -0
  5. package/dist/auth-connect.js +306 -0
  6. package/dist/browser-bootstrap.js +190 -0
  7. package/dist/browser-loopback.js +289 -0
  8. package/dist/build-identity.js +6 -0
  9. package/dist/codex-reviewer-apparmor.json +4 -0
  10. package/dist/codex-reviewer-seccomp.json +1238 -0
  11. package/dist/codex-runtime-protocol.js +1 -0
  12. package/dist/codex-subscription.js +53 -0
  13. package/dist/config.js +297 -0
  14. package/dist/connect-comparison-code.js +26 -0
  15. package/dist/development-runner.js +109 -0
  16. package/dist/dns-doctor.js +390 -0
  17. package/dist/docker-names.js +47 -0
  18. package/dist/doctor.js +114 -0
  19. package/dist/gadget-job.js +804 -0
  20. package/dist/health-check.js +112 -0
  21. package/dist/help.js +71 -0
  22. package/dist/host-auth.js +22 -0
  23. package/dist/host-diagnostics.js +20 -0
  24. package/dist/host-runtime-identity.js +39 -0
  25. package/dist/host-runtime-metadata.js +56 -0
  26. package/dist/host-service.js +377 -0
  27. package/dist/host-stack.js +484 -0
  28. package/dist/host-supervisor.js +237 -0
  29. package/dist/host-update.js +387 -0
  30. package/dist/hub-address.js +100 -0
  31. package/dist/hub-client.js +459 -0
  32. package/dist/hub-kind.js +88 -0
  33. package/dist/identity.js +73 -0
  34. package/dist/index.js +185 -0
  35. package/dist/init.js +295 -0
  36. package/dist/isolated-run-cache.js +116 -0
  37. package/dist/local-stack.js +685 -0
  38. package/dist/machine-capacity.js +128 -0
  39. package/dist/managed-runner-image.js +13 -0
  40. package/dist/os-acted-record.js +49 -0
  41. package/dist/os-adapter.js +8190 -0
  42. package/dist/os-command.js +292 -0
  43. package/dist/os-handoff.js +274 -0
  44. package/dist/os-keychain.js +247 -0
  45. package/dist/os-machine.js +818 -0
  46. package/dist/os-owner.js +61 -0
  47. package/dist/os-private-file.js +98 -0
  48. package/dist/os-protocol.js +468 -0
  49. package/dist/os-session-store.js +133 -0
  50. package/dist/os-status.js +102 -0
  51. package/dist/picker.js +863 -0
  52. package/dist/portable-workspace-capability.js +60 -0
  53. package/dist/project-environment-capability.js +62 -0
  54. package/dist/project-environment.js +28 -0
  55. package/dist/project-preparation.js +134 -0
  56. package/dist/readiness.js +28 -0
  57. package/dist/redaction.js +27 -0
  58. package/dist/reviewer-isolation-capability.js +61 -0
  59. package/dist/runner-api-auth.js +80 -0
  60. package/dist/runner-command-fence.js +536 -0
  61. package/dist/runner-inspection.js +57 -0
  62. package/dist/runner-inventory.js +587 -0
  63. package/dist/runner-server.js +2520 -0
  64. package/dist/runner-session.js +470 -0
  65. package/dist/runner-slug-folder.js +39 -0
  66. package/dist/runner-slug.js +40 -0
  67. package/dist/setup.js +139 -0
  68. package/dist/shutdown.js +102 -0
  69. package/dist/splash.js +65 -0
  70. package/dist/staged-workspaces.js +441 -0
  71. package/dist/staging-disk.js +97 -0
  72. package/dist/terminal-lane.js +339 -0
  73. package/dist/terminal-recovery.js +502 -0
  74. package/licenses/capnweb.txt +23 -0
  75. package/licenses/moby-profiles.txt +212 -0
  76. package/licenses/ws.txt +22 -0
  77. package/package.json +28 -7
  78. package/cli.js +0 -2
@@ -0,0 +1,339 @@
1
+ import net from "node:net";
2
+ import { spawn } from "node:child_process";
3
+ import { Duplex } from "node:stream";
4
+ import { randomBytes } from "node:crypto";
5
+ import { isValidRunnerSlug } from "./runner-slug.js";
6
+ /**
7
+ * Local terminal lane: a loopback WebSocket from the dashboard in a browser on
8
+ * this machine to the harness in a container, relayed by the runner.
9
+ *
10
+ * The runner admits a browser only when all of these hold:
11
+ * - the connection arrived over loopback (the server also binds loopback only);
12
+ * - its `Origin` is exactly the Hub origin this runner is registered with;
13
+ * - it presents a ticket this runner minted at the Hub's request, at most
14
+ * 30 s ago and never used before.
15
+ * The ticket names the environment, session and viewer connection it was
16
+ * minted for. The relay forwards it to the harness, which accepts it only if
17
+ * the Hub granted the same ticket to that harness for that viewer.
18
+ *
19
+ * The runner reaches the harness through `docker exec`: a small relay inside
20
+ * the container connects to the harness on the container's own loopback and
21
+ * carries the bytes over the exec's standard input and output. No container
22
+ * port is published on the host, so no other process on this machine, and no
23
+ * other container, can reach the harness's port.
24
+ */
25
+ /** Port the harness listens on inside the container, on the container's own loopback. Never published. */
26
+ export const TERMINAL_LANE_CONTAINER_PORT = 47621;
27
+ /** The address the runner tells the harness to listen on. */
28
+ export const TERMINAL_LANE_CONTAINER_HOST = "127.0.0.1";
29
+ /**
30
+ * Node inside the session image. Every image that has a terminal lane has it;
31
+ * the absolute path keeps a repository's own `node` on PATH out of the relay.
32
+ */
33
+ export const TERMINAL_LANE_RELAY_NODE = "/usr/local/bin/tiller-controller-node";
34
+ export const TERMINAL_LANE_TICKET_TTL_MS = 30_000;
35
+ export const TERMINAL_LANE_PATH = "/terminal-lane";
36
+ const MAX_OUTSTANDING_TICKETS = 256;
37
+ const TICKET_PATTERN = /^[A-Za-z0-9_-]{43}$/;
38
+ const WEBSOCKET_KEY_PATTERN = /^[A-Za-z0-9+/]{22}==$/;
39
+ const IDENTIFIER_PATTERN = /^[A-Za-z0-9._:-]{1,200}$/;
40
+ export function isTerminalLaneIdentifier(value) {
41
+ return typeof value === "string" && IDENTIFIER_PATTERN.test(value);
42
+ }
43
+ export function parseTerminalLaneTicketRequest(slug, body) {
44
+ const record = body && typeof body === "object" ? body : {};
45
+ // The slug names the container and the relays Stop and Delete close, so it
46
+ // takes the runner's slug rule: `foo:bar` would otherwise reach the same
47
+ // container as `foo-bar` while its relays were tracked under another name.
48
+ if (!isValidRunnerSlug(slug) || !isTerminalLaneIdentifier(record.sessionId) ||
49
+ !isTerminalLaneIdentifier(record.viewerConnectionId)) {
50
+ throw new Error("Invalid terminal lane ticket request");
51
+ }
52
+ return { slug, sessionId: record.sessionId, viewerConnectionId: record.viewerConnectionId };
53
+ }
54
+ export class TerminalLaneTickets {
55
+ now;
56
+ tickets = new Map();
57
+ constructor(now = Date.now) {
58
+ this.now = now;
59
+ }
60
+ mint(request) {
61
+ this.sweep();
62
+ while (this.tickets.size >= MAX_OUTSTANDING_TICKETS) {
63
+ const oldest = this.tickets.keys().next().value;
64
+ if (oldest === undefined)
65
+ break;
66
+ this.tickets.delete(oldest);
67
+ }
68
+ const ticket = randomBytes(32).toString("base64url");
69
+ this.tickets.set(ticket, { ...request, expiresAt: this.now() + TERMINAL_LANE_TICKET_TTL_MS });
70
+ return { ticket, expiresInMs: TERMINAL_LANE_TICKET_TTL_MS };
71
+ }
72
+ /** The ticket's record, removed so it can never be used again; null when unknown or expired. */
73
+ consume(ticket) {
74
+ if (!TICKET_PATTERN.test(ticket))
75
+ return null;
76
+ const record = this.tickets.get(ticket);
77
+ if (!record)
78
+ return null;
79
+ this.tickets.delete(ticket);
80
+ return record.expiresAt > this.now() ? record : null;
81
+ }
82
+ /** Forget unused tickets for an environment that stopped or was removed. */
83
+ revoke(slug) {
84
+ for (const [ticket, record] of this.tickets)
85
+ if (record.slug === slug)
86
+ this.tickets.delete(ticket);
87
+ }
88
+ get size() {
89
+ return this.tickets.size;
90
+ }
91
+ sweep() {
92
+ const now = this.now();
93
+ for (const [ticket, record] of this.tickets)
94
+ if (record.expiresAt <= now)
95
+ this.tickets.delete(ticket);
96
+ }
97
+ }
98
+ export function isLoopbackAddress(address) {
99
+ if (!address)
100
+ return false;
101
+ const normalized = address.startsWith("::ffff:") ? address.slice(7) : address;
102
+ return normalized === "::1" || /^127\.\d{1,3}\.\d{1,3}\.\d{1,3}$/.test(normalized);
103
+ }
104
+ /**
105
+ * `docker inspect` format that prints `true lane` for a running container
106
+ * started with a terminal lane, and nothing else of its environment (which
107
+ * holds secrets). Containers started by earlier runners, which published the
108
+ * port, carry the same variable, so their lanes keep working through exec.
109
+ */
110
+ export const TERMINAL_LANE_INSPECT_FORMAT = `{{.State.Running}}{{range .Config.Env}}{{if eq . "TILLER_TERMINAL_LANE_PORT=${TERMINAL_LANE_CONTAINER_PORT}"}} lane{{end}}{{end}}`;
111
+ /** Whether `docker inspect -f TERMINAL_LANE_INSPECT_FORMAT` says the container is running with a lane. */
112
+ export function hasRunningTerminalLane(output) {
113
+ return output.trim() === "true lane";
114
+ }
115
+ /**
116
+ * The program the relay runs inside the container: connect to the harness on
117
+ * the container's loopback, then copy bytes both ways until either side ends.
118
+ */
119
+ export const TERMINAL_LANE_RELAY_SCRIPT = [
120
+ `const s=require("net").connect(${TERMINAL_LANE_CONTAINER_PORT},"127.0.0.1");`,
121
+ `s.setNoDelay(true);`,
122
+ `process.stdin.pipe(s);`,
123
+ `s.pipe(process.stdout);`,
124
+ `const quit=(code)=>process.stdout.write("",()=>process.exit(code));`,
125
+ `s.on("error",()=>quit(1));`,
126
+ `s.on("close",()=>quit(0));`,
127
+ `process.stdin.on("end",()=>setTimeout(()=>process.exit(0),2000).unref());`,
128
+ `process.stdin.on("error",()=>process.exit(0));`,
129
+ `process.stdout.on("error",()=>process.exit(0));`,
130
+ ].join("");
131
+ /** How long a stopped relay's exec may take to exit before it gets SIGKILL. */
132
+ export const TERMINAL_LANE_RELAY_KILL_GRACE_MS = 2_000;
133
+ /**
134
+ * The relay runs as `nobody` (65534:65534), not as the container's default
135
+ * user (root), from `/`, with the variables that make the loader or Node load
136
+ * other code cleared: the exec inherits the container's environment, and an
137
+ * agent could otherwise point one of them into the workspace and have its code
138
+ * loaded into a root process. Connecting to a loopback port needs no privilege.
139
+ */
140
+ export const TERMINAL_LANE_RELAY_USER = "65534:65534";
141
+ export const TERMINAL_LANE_RELAY_CLEARED_ENV = ["LD_PRELOAD", "LD_LIBRARY_PATH", "LD_AUDIT", "NODE_OPTIONS"];
142
+ /** The `docker` arguments that open a relay into `container`. */
143
+ export function dockerExecRelayArgs(container, node = TERMINAL_LANE_RELAY_NODE) {
144
+ return [
145
+ "exec", "-i",
146
+ "-u", TERMINAL_LANE_RELAY_USER,
147
+ "-w", "/",
148
+ ...TERMINAL_LANE_RELAY_CLEARED_ENV.flatMap((name) => ["-e", `${name}=`]),
149
+ container, node, "-e", TERMINAL_LANE_RELAY_SCRIPT,
150
+ ];
151
+ }
152
+ /**
153
+ * A byte stream to the harness's lane port inside `container`, carried by
154
+ * `docker exec`. Destroying it stops the exec; the exec ending ends it.
155
+ */
156
+ export function openDockerExecRelay(container, options = {}) {
157
+ const child = (options.spawn ?? spawn)(options.docker ?? "docker", dockerExecRelayArgs(container, options.node), {
158
+ stdio: ["pipe", "pipe", "pipe"],
159
+ });
160
+ return new ChildProcessRelay(child, options.log ?? (() => undefined), options.killGraceMs);
161
+ }
162
+ class ChildProcessRelay extends Duplex {
163
+ child;
164
+ killGraceMs;
165
+ outputEnded = false;
166
+ stderr = "";
167
+ constructor(child, log, killGraceMs = TERMINAL_LANE_RELAY_KILL_GRACE_MS) {
168
+ super({ allowHalfOpen: false });
169
+ this.child = child;
170
+ this.killGraceMs = killGraceMs;
171
+ child.stdin.on("error", () => undefined);
172
+ child.stdout.on("data", (chunk) => {
173
+ if (!this.push(chunk))
174
+ child.stdout.pause();
175
+ });
176
+ child.stdout.on("end", () => this.endReadable());
177
+ child.stderr.on("data", (chunk) => {
178
+ if (this.stderr.length < 500)
179
+ this.stderr += chunk.toString("utf8");
180
+ });
181
+ child.on("error", (error) => this.destroy(error));
182
+ child.on("close", (code, signal) => {
183
+ if (code && !signal)
184
+ log(`terminal lane relay exited ${code}${this.stderr.trim() ? `: ${this.stderr.trim().slice(0, 300)}` : ""}`);
185
+ this.endReadable();
186
+ this.end();
187
+ });
188
+ }
189
+ endReadable() {
190
+ if (this.outputEnded)
191
+ return;
192
+ this.outputEnded = true;
193
+ this.push(null);
194
+ }
195
+ _read() {
196
+ this.child.stdout.resume();
197
+ }
198
+ _write(chunk, _encoding, callback) {
199
+ const stdin = this.child.stdin;
200
+ if (stdin.destroyed || !stdin.writable) {
201
+ callback();
202
+ return;
203
+ }
204
+ stdin.write(chunk, () => callback());
205
+ }
206
+ _final(callback) {
207
+ this.child.stdin.end();
208
+ callback();
209
+ }
210
+ _destroy(error, callback) {
211
+ const child = this.child;
212
+ if (child.exitCode === null && child.signalCode === null) {
213
+ child.kill("SIGTERM");
214
+ // An exec that ignores SIGTERM is killed. The timer never keeps the
215
+ // runner alive and is dropped as soon as the exec exits.
216
+ const force = setTimeout(() => {
217
+ if (child.exitCode === null && child.signalCode === null)
218
+ child.kill("SIGKILL");
219
+ }, this.killGraceMs);
220
+ force.unref?.();
221
+ child.once("exit", () => clearTimeout(force));
222
+ }
223
+ callback(error);
224
+ }
225
+ }
226
+ /** Open relays per environment, so Stop and Delete can close them at once. */
227
+ export class TerminalLaneRelays {
228
+ bySlug = new Map();
229
+ add(slug, close) {
230
+ let set = this.bySlug.get(slug);
231
+ if (!set)
232
+ this.bySlug.set(slug, set = new Set());
233
+ set.add(close);
234
+ return () => {
235
+ set.delete(close);
236
+ if (set.size === 0 && this.bySlug.get(slug) === set)
237
+ this.bySlug.delete(slug);
238
+ };
239
+ }
240
+ closeAll(slug) {
241
+ const set = this.bySlug.get(slug);
242
+ if (!set)
243
+ return;
244
+ this.bySlug.delete(slug);
245
+ for (const close of [...set])
246
+ close();
247
+ }
248
+ count(slug) {
249
+ return this.bySlug.get(slug)?.size ?? 0;
250
+ }
251
+ }
252
+ function refuse(socket, status) {
253
+ const reason = { 400: "Bad Request", 403: "Forbidden", 404: "Not Found", 502: "Bad Gateway" }[status];
254
+ try {
255
+ socket.end(`HTTP/1.1 ${status} ${reason}\r\nConnection: close\r\nContent-Length: 0\r\n\r\n`);
256
+ }
257
+ catch {
258
+ // Already gone.
259
+ }
260
+ socket.destroy();
261
+ }
262
+ /**
263
+ * Handles an HTTP upgrade on the runner's loopback server. Returns false when
264
+ * the path is not the lane's, so other upgrade handlers may run.
265
+ */
266
+ export function handleTerminalLaneUpgrade(req, socket, head, deps) {
267
+ const url = new URL(req.url ?? "/", "http://127.0.0.1");
268
+ if (url.pathname !== TERMINAL_LANE_PATH)
269
+ return false;
270
+ const log = deps.log ?? (() => undefined);
271
+ if (!deps.config) {
272
+ refuse(socket, 404);
273
+ return true;
274
+ }
275
+ if (!isLoopbackAddress(req.socket.remoteAddress)) {
276
+ log("terminal lane refused: not a loopback connection");
277
+ refuse(socket, 403);
278
+ return true;
279
+ }
280
+ const key = req.headers["sec-websocket-key"];
281
+ if (req.method !== "GET" ||
282
+ String(req.headers.upgrade ?? "").toLowerCase() !== "websocket" ||
283
+ req.headers["sec-websocket-version"] !== "13" ||
284
+ typeof key !== "string" || !WEBSOCKET_KEY_PATTERN.test(key)) {
285
+ refuse(socket, 400);
286
+ return true;
287
+ }
288
+ if (req.headers.origin !== deps.config.hubOrigin) {
289
+ log("terminal lane refused: origin does not match the Hub");
290
+ refuse(socket, 403);
291
+ return true;
292
+ }
293
+ const record = deps.tickets.consume(url.searchParams.get("ticket") ?? "");
294
+ if (!record) {
295
+ log("terminal lane refused: missing, used or expired ticket");
296
+ refuse(socket, 403);
297
+ return true;
298
+ }
299
+ const ticket = url.searchParams.get("ticket");
300
+ const upstream = deps.openUpstream(record.slug);
301
+ let closed = false;
302
+ const close = () => {
303
+ if (closed)
304
+ return;
305
+ closed = true;
306
+ upstream.destroy();
307
+ socket.destroy();
308
+ };
309
+ const release = deps.relays.add(record.slug, close);
310
+ const finish = () => {
311
+ close();
312
+ release();
313
+ };
314
+ upstream.on("error", finish);
315
+ upstream.on("close", finish);
316
+ socket.on("error", finish);
317
+ socket.on("close", finish);
318
+ if (upstream instanceof net.Socket)
319
+ upstream.setNoDelay(true);
320
+ if (socket instanceof net.Socket)
321
+ socket.setNoDelay(true);
322
+ // Written at once: the stream holds it until the harness's end is connected.
323
+ upstream.write(`GET /lane HTTP/1.1\r\n` +
324
+ `Host: 127.0.0.1:${TERMINAL_LANE_CONTAINER_PORT}\r\n` +
325
+ `Upgrade: websocket\r\n` +
326
+ `Connection: Upgrade\r\n` +
327
+ `Sec-WebSocket-Key: ${key}\r\n` +
328
+ `Sec-WebSocket-Version: 13\r\n` +
329
+ `Origin: ${deps.config.hubOrigin}\r\n` +
330
+ `X-Tiller-Lane-Ticket: ${ticket}\r\n` +
331
+ `X-Tiller-Lane-Session: ${record.sessionId}\r\n` +
332
+ `X-Tiller-Lane-Viewer: ${record.viewerConnectionId}\r\n` +
333
+ `\r\n`);
334
+ if (head.length)
335
+ upstream.write(head);
336
+ socket.pipe(upstream);
337
+ upstream.pipe(socket);
338
+ return true;
339
+ }