dsh-plugin-wsl-env 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +165 -0
- package/README.md +2 -0
- package/agent/wsl-agent.sh +368 -26
- package/cordis.patch.yml +7 -0
- package/docs/ARCHITECTURE.md +37 -1
- package/docs/CONFIGURATION.md +4 -1
- package/docs/LIMITATIONS.md +60 -0
- package/docs/PEER-PARITY.md +60 -0
- package/docs/README.zh.md +2 -0
- package/docs/UPSTREAM-FSIO-EXPORT.md +87 -0
- package/lib/agent-confined.js +81 -0
- package/lib/agent-exec.js +22 -12
- package/lib/agent-protocol.js +65 -7
- package/lib/agent.js +211 -61
- package/lib/bwrap.js +74 -0
- package/lib/fs-substrate.js +285 -0
- package/lib/fsio-agent.js +481 -0
- package/lib/fsio-text.js +153 -0
- package/lib/index.js +239 -6
- package/lib/sandbox.js +32 -27
- package/package.json +5 -2
package/lib/agent.js
CHANGED
|
@@ -5,16 +5,25 @@
|
|
|
5
5
|
* distro. The lifetime rules, in order of importance:
|
|
6
6
|
*
|
|
7
7
|
* 1. **Correctness before speed.** Any agent failure is answered by at most
|
|
8
|
-
* one rebuild; the requests that were in flight ride
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
8
|
+
* one rebuild; the requests that were in flight ride it ONLY when their
|
|
9
|
+
* first execution provably changed nothing (the read-only FS ops). A
|
|
10
|
+
* command or a write that was in flight has UNKNOWN state — replaying it
|
|
11
|
+
* would run non-idempotent work twice or report a create that actually
|
|
12
|
+
* succeeded as a refusal — so it fails instead, and the caller re-reads
|
|
13
|
+
* before retrying. A second failure marks the agent permanently dead and
|
|
14
|
+
* every future call rejects with {@link AgentUnavailableError} — the
|
|
15
|
+
* executor's cue to fall back to the one-shot `wsl.exe` path it shipped
|
|
16
|
+
* with, so the worst case of the agent is "today's behaviour, plus one
|
|
17
|
+
* failed round trip".
|
|
13
18
|
* 2. **The in-distro timeout is the real one.** The EXEC frame carries the
|
|
14
19
|
* timeout in whole seconds and the agent SIGTERMs the child itself, so a
|
|
15
20
|
* timed-out command dies where it lives instead of leaving an orphan
|
|
16
|
-
* behind a dead `wsl.exe`.
|
|
17
|
-
*
|
|
21
|
+
* behind a dead `wsl.exe`. The host-side watchdog is armed on the agent's
|
|
22
|
+
* `ACK` — the dequeue signal — so it measures EXECUTION, never the queue
|
|
23
|
+
* wait: a short-budget request queued behind a long one can no longer
|
|
24
|
+
* kill the shared transport for a stall that was never its own. It still
|
|
25
|
+
* catches an agent that accepted a request and then stopped answering at
|
|
26
|
+
* all.
|
|
18
27
|
* 3. **Idle agents go away.** `idleMs` after the last exchange the process is
|
|
19
28
|
* killed; the next call starts a fresh one. WSL VMs are shared, but every
|
|
20
29
|
* process is a promise someone has to keep.
|
|
@@ -26,9 +35,8 @@
|
|
|
26
35
|
*/
|
|
27
36
|
|
|
28
37
|
import { spawn } from "node:child_process";
|
|
29
|
-
import { createInterface } from "node:readline";
|
|
30
38
|
import { AgentUnavailableError, CwdError } from "./agent-errors.js";
|
|
31
|
-
import { AGENT_NAME, PROTOCOL_VERSION, encodeExecFrame, encodeKill, encodeSetEnv, parseAgentLine } from "./agent-protocol.js";
|
|
39
|
+
import { AGENT_NAME, MAX_FRAME_BYTES, PROTOCOL_VERSION, encodeExecFrame, encodeFsFrame, encodeKill, encodeSetEnv, parseAgentLine } from "./agent-protocol.js";
|
|
32
40
|
|
|
33
41
|
/** How long a rebuilt agent has to re-answer the handshake before giving up. */
|
|
34
42
|
const HANDSHAKE_TIMEOUT_MS = 15000;
|
|
@@ -45,8 +53,8 @@ const WATCHDOG_GRACE_MS = 13000;
|
|
|
45
53
|
* `agent/wsl-agent.sh`).
|
|
46
54
|
* @returns {import("node:child_process").ChildProcess} the spawned process.
|
|
47
55
|
*/
|
|
48
|
-
function spawnDefault({ wslPath, distro, scriptPath }) {
|
|
49
|
-
return spawn(wslPath, ["-d", distro, "--exec", "sh", scriptPath], {
|
|
56
|
+
function spawnDefault({ wslPath, distro, scriptPath, argvPrefix = [] }) {
|
|
57
|
+
return spawn(wslPath, ["-d", distro, "--exec", ...argvPrefix, "sh", scriptPath], {
|
|
50
58
|
stdio: ["pipe", "pipe", "pipe"],
|
|
51
59
|
windowsHide: true,
|
|
52
60
|
});
|
|
@@ -63,14 +71,22 @@ export class WslAgent {
|
|
|
63
71
|
* inside the distro (see {@link spawnDefault}).
|
|
64
72
|
* @param {string} [config.wslPath] - the `wsl.exe` path.
|
|
65
73
|
* @param {number} [config.idleMs] - idle shutdown delay; 0 disables.
|
|
66
|
-
* @param {
|
|
74
|
+
* @param {string[]} [config.argvPrefix] - arguments inserted between
|
|
75
|
+
* `wsl.exe --exec` and the `sh script` pair — the confinement wrapper
|
|
76
|
+
* (`bwrap … --`) a confined agent runs its whole lifetime under, so every
|
|
77
|
+
* filesystem op it performs is kernel-enforced. Empty for the plain agent.
|
|
78
|
+
* @param {(options: {wslPath: string, distro: string, scriptPath: string, argvPrefix: string[]}) => import("node:child_process").ChildProcess} [config.spawnTransport]
|
|
67
79
|
* injectable process factory for tests.
|
|
68
80
|
*/
|
|
69
81
|
constructor(config) {
|
|
70
82
|
this.distro = config.distro;
|
|
71
83
|
this.scriptPath = config.scriptPath;
|
|
72
84
|
this.wslPath = config.wslPath ?? "wsl.exe";
|
|
85
|
+
this.argvPrefix = config.argvPrefix ?? [];
|
|
73
86
|
this.idleMs = config.idleMs ?? 600000;
|
|
87
|
+
/** Hard ceiling on one stdout protocol line; a longer line is a broken or
|
|
88
|
+
* hostile agent, not data to hold — readline used to accumulate it all. */
|
|
89
|
+
this.frameCapBytes = config.frameCapBytes ?? MAX_FRAME_BYTES;
|
|
74
90
|
this.configuredSpawn = config.spawnTransport;
|
|
75
91
|
/** @type {import("node:child_process").ChildProcess|null} */
|
|
76
92
|
this.process = null;
|
|
@@ -117,6 +133,7 @@ export class WslAgent {
|
|
|
117
133
|
wslPath: this.wslPath,
|
|
118
134
|
distro: this.distro,
|
|
119
135
|
scriptPath: this.scriptPath,
|
|
136
|
+
argvPrefix: this.argvPrefix,
|
|
120
137
|
}));
|
|
121
138
|
const handshakeTimer = setTimeout(() => {
|
|
122
139
|
this.lastError = `agent handshake timed out after ${HANDSHAKE_TIMEOUT_MS}ms`;
|
|
@@ -145,8 +162,34 @@ export class WslAgent {
|
|
|
145
162
|
}
|
|
146
163
|
this.handleProcessExit(code, signalName);
|
|
147
164
|
});
|
|
148
|
-
|
|
149
|
-
|
|
165
|
+
// A bounded line splitter instead of readline: readline accumulates an
|
|
166
|
+
// over-long line whole before anyone can object, which is exactly the
|
|
167
|
+
// unbounded-capture hole. Protocol lines are ASCII by construction
|
|
168
|
+
// (ids, numbers, base64), so chunk-boundary decoding is not a concern.
|
|
169
|
+
let buffered = "";
|
|
170
|
+
child.stdout.on("data", (chunk) => {
|
|
171
|
+
buffered += chunk.toString("utf8");
|
|
172
|
+
for (;;) {
|
|
173
|
+
const newline = buffered.indexOf("\n");
|
|
174
|
+
if (newline === -1) {
|
|
175
|
+
if (buffered.length > this.frameCapBytes) {
|
|
176
|
+
this.lastError = `agent frame of ${buffered.length} bytes exceeded the ${this.frameCapBytes}-byte protocol cap`;
|
|
177
|
+
buffered = "";
|
|
178
|
+
this.process?.kill();
|
|
179
|
+
}
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
const line = buffered.slice(0, newline);
|
|
183
|
+
buffered = buffered.slice(newline + 1);
|
|
184
|
+
if (line.length > this.frameCapBytes) {
|
|
185
|
+
this.lastError = `agent frame of ${line.length} bytes exceeded the ${this.frameCapBytes}-byte protocol cap`;
|
|
186
|
+
buffered = "";
|
|
187
|
+
this.process?.kill();
|
|
188
|
+
return;
|
|
189
|
+
}
|
|
190
|
+
this.handleLine(line, settleStart, handshakeTimer);
|
|
191
|
+
}
|
|
192
|
+
});
|
|
150
193
|
child.stderr?.on("data", (chunk) => {
|
|
151
194
|
// Diagnostics only; `wsl.exe` and the shell chatter here on failure.
|
|
152
195
|
this.lastError = String(chunk).trim().slice(0, 2000) || this.lastError;
|
|
@@ -173,13 +216,22 @@ export class WslAgent {
|
|
|
173
216
|
settleStart();
|
|
174
217
|
break;
|
|
175
218
|
}
|
|
219
|
+
case "ack": {
|
|
220
|
+
// Dequeue signal: start this request's own budget clock NOW. Armed
|
|
221
|
+
// here instead of at submission, a queued request can no longer time
|
|
222
|
+
// out on a queue it never chose to join — and its watchdog can never
|
|
223
|
+
// kill the shared transport for a stall that was never its own.
|
|
224
|
+
const pending = this.pending.get(message.id);
|
|
225
|
+
pending?.armWatchdog?.();
|
|
226
|
+
break;
|
|
227
|
+
}
|
|
176
228
|
case "result": {
|
|
177
229
|
const pending = this.pending.get(message.id);
|
|
178
230
|
if (!pending) break; // answered after its caller gave up — noise
|
|
179
231
|
this.clearPending(pending);
|
|
180
232
|
this.pending.delete(message.id);
|
|
181
233
|
this.rearmIdleTimer();
|
|
182
|
-
pending.resolve({ exitCode: message.exitCode, stdout: message.stdout, stderr: message.stderr });
|
|
234
|
+
pending.resolve({ exitCode: message.exitCode, stdout: message.stdout, stderr: message.stderr, truncated: message.truncated });
|
|
183
235
|
break;
|
|
184
236
|
}
|
|
185
237
|
case "agentError": {
|
|
@@ -223,16 +275,45 @@ export class WslAgent {
|
|
|
223
275
|
this.startPromise = null;
|
|
224
276
|
const carrying = [...this.pending.values()];
|
|
225
277
|
this.pending.clear();
|
|
278
|
+
// Only provably read-only FS requests ride the rebuild: whatever their
|
|
279
|
+
// first execution did, it changed nothing. A command or a write that was
|
|
280
|
+
// in flight has UNKNOWN state — replaying it would run non-idempotent
|
|
281
|
+
// work a second time, or report a guarded create that actually
|
|
282
|
+
// succeeded as a refusal — so it fails with that fact instead. A stale
|
|
283
|
+
// watchdog must not survive either: its closure kills `this.process`,
|
|
284
|
+
// which after the rebuild is the NEW transport.
|
|
285
|
+
const relayable = [];
|
|
286
|
+
for (const pending of carrying) {
|
|
287
|
+
if (pending.signal?.aborted) {
|
|
288
|
+
// Already rejected to its caller by the abort listener; do not
|
|
289
|
+
// resurrect it on the rebuilt agent.
|
|
290
|
+
this.clearPending(pending);
|
|
291
|
+
continue;
|
|
292
|
+
}
|
|
293
|
+
if (isReplayableFrame(pending.lines)) {
|
|
294
|
+
if (pending.watchdog) {
|
|
295
|
+
clearTimeout(pending.watchdog);
|
|
296
|
+
pending.watchdog = null;
|
|
297
|
+
}
|
|
298
|
+
relayable.push(pending);
|
|
299
|
+
} else {
|
|
300
|
+
this.clearPending(pending);
|
|
301
|
+
pending.reject(new AgentUnavailableError(
|
|
302
|
+
`agent exited while request ${pending.id} was in flight; its state is unknown — inspect the distro before retrying`,
|
|
303
|
+
));
|
|
304
|
+
}
|
|
305
|
+
}
|
|
226
306
|
this.start()
|
|
227
307
|
.then(() => {
|
|
228
|
-
for (const pending of
|
|
308
|
+
for (const pending of relayable) {
|
|
229
309
|
const id = `r${this.nextRequestId++}`;
|
|
310
|
+
pending.id = id;
|
|
230
311
|
this.pending.set(id, pending);
|
|
231
312
|
this.writeLines(relabelFrame(pending.lines, id));
|
|
232
313
|
}
|
|
233
314
|
})
|
|
234
315
|
.catch((error) => {
|
|
235
|
-
for (const pending of
|
|
316
|
+
for (const pending of relayable) {
|
|
236
317
|
this.clearPending(pending);
|
|
237
318
|
pending.reject(error);
|
|
238
319
|
}
|
|
@@ -249,6 +330,61 @@ export class WslAgent {
|
|
|
249
330
|
this.pending.clear();
|
|
250
331
|
}
|
|
251
332
|
|
|
333
|
+
/**
|
|
334
|
+
* Register one request's pending entry and write its frame: the shared
|
|
335
|
+
* request machinery (abort, submission, the ACK-armed watchdog). The
|
|
336
|
+
* watchdog is deliberately NOT armed here — it starts at the agent's
|
|
337
|
+
* dequeue `ACK`, so time spent queued behind an earlier request never
|
|
338
|
+
* spends this request's budget and never kills the shared transport for a
|
|
339
|
+
* stall that was not its own.
|
|
340
|
+
* @private
|
|
341
|
+
*/
|
|
342
|
+
track({ id, lines, resolve, reject, timeoutMs, signal }) {
|
|
343
|
+
const pending = {
|
|
344
|
+
id,
|
|
345
|
+
lines,
|
|
346
|
+
resolve,
|
|
347
|
+
reject,
|
|
348
|
+
watchdog: null,
|
|
349
|
+
signal: signal ?? null,
|
|
350
|
+
onAbort: null,
|
|
351
|
+
armWatchdog:
|
|
352
|
+
timeoutMs > 0
|
|
353
|
+
? () => {
|
|
354
|
+
if (pending.watchdog) return;
|
|
355
|
+
// The in-distro timeout does the killing; this only catches an
|
|
356
|
+
// agent that accepted the request and then stopped answering
|
|
357
|
+
// even to its own timer.
|
|
358
|
+
pending.watchdog = setTimeout(() => {
|
|
359
|
+
this.lastError = `agent did not answer request ${pending.id} within ${timeoutMs + WATCHDOG_GRACE_MS}ms of its dispatch`;
|
|
360
|
+
this.process?.kill();
|
|
361
|
+
}, timeoutMs + WATCHDOG_GRACE_MS);
|
|
362
|
+
}
|
|
363
|
+
: null,
|
|
364
|
+
};
|
|
365
|
+
this.pending.set(id, pending);
|
|
366
|
+
if (signal) {
|
|
367
|
+
if (signal.aborted) {
|
|
368
|
+
this.pending.delete(id);
|
|
369
|
+
reject(signal.reason instanceof Error ? signal.reason : new Error("aborted"));
|
|
370
|
+
return;
|
|
371
|
+
}
|
|
372
|
+
pending.onAbort = () => {
|
|
373
|
+
// Address by the entry's CURRENT id: a request relayed across a
|
|
374
|
+
// rebuild was re-registered under a fresh one, and a stale KILL (or a
|
|
375
|
+
// stale map delete) would strand the entry and leave the command
|
|
376
|
+
// running to its own timeout.
|
|
377
|
+
this.writeLines([encodeKill(pending.id)]);
|
|
378
|
+
this.clearPending(pending);
|
|
379
|
+
this.pending.delete(pending.id);
|
|
380
|
+
reject(signal.reason instanceof Error ? signal.reason : new Error("aborted"));
|
|
381
|
+
};
|
|
382
|
+
signal.addEventListener("abort", pending.onAbort, { once: true });
|
|
383
|
+
}
|
|
384
|
+
this.writeLines(lines);
|
|
385
|
+
this.rearmIdleTimer();
|
|
386
|
+
}
|
|
387
|
+
|
|
252
388
|
/**
|
|
253
389
|
* Run one command inside the distro through the agent.
|
|
254
390
|
* @param {object} request - the execution request.
|
|
@@ -256,14 +392,18 @@ export class WslAgent {
|
|
|
256
392
|
* @param {string[]} request.argv - the argv, already bwrap-wrapped when the
|
|
257
393
|
* caller confines.
|
|
258
394
|
* @param {number} [request.timeoutMs] - in-distro timeout, whole seconds
|
|
259
|
-
* upward; `<= 0` or omitted means
|
|
395
|
+
* upward, measured from the agent's dequeue ACK; `<= 0` or omitted means
|
|
396
|
+
* none. Queue time ahead of the dispatch is never counted.
|
|
260
397
|
* @param {Record<string, string>} [request.env] - extra environment for this
|
|
261
398
|
* request only; values lose trailing newlines (protocol limitation).
|
|
399
|
+
* @param {number} [request.maxOutputBytes] - per-stream capture ceiling the
|
|
400
|
+
* agent enforces (carried on the EXEC frame); the RES line reports which
|
|
401
|
+
* streams were cut. 0 (the default) keeps the unbounded capture.
|
|
262
402
|
* @param {AbortSignal} [request.signal] - cancels the request (KILL + reject).
|
|
263
|
-
* @returns {Promise<{exitCode: number, stdout: Buffer, stderr: Buffer}>}
|
|
403
|
+
* @returns {Promise<{exitCode: number, stdout: Buffer, stderr: Buffer, truncated: {stdout: boolean, stderr: boolean}}>}
|
|
264
404
|
* @throws {AgentUnavailableError} when the agent is out for this process.
|
|
265
405
|
*/
|
|
266
|
-
async exec({ cwd, argv, timeoutMs = 0, env, signal }) {
|
|
406
|
+
async exec({ cwd, argv, timeoutMs = 0, maxOutputBytes = 0, env, signal }) {
|
|
267
407
|
if (this.state === "dead") {
|
|
268
408
|
throw new AgentUnavailableError(this.lastError);
|
|
269
409
|
}
|
|
@@ -271,35 +411,39 @@ export class WslAgent {
|
|
|
271
411
|
const id = `r${this.nextRequestId++}`;
|
|
272
412
|
const lines = [
|
|
273
413
|
...(env ? Object.entries(env).map(([key, value]) => encodeSetEnv(key, value)) : []),
|
|
274
|
-
...encodeExecFrame({ id, cwd, argv, timeoutMs }),
|
|
414
|
+
...encodeExecFrame({ id, cwd, argv, timeoutMs, maxOutputBytes }),
|
|
275
415
|
];
|
|
276
416
|
return new Promise((resolve, reject) => {
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
417
|
+
this.track({ id, lines, resolve, reject, timeoutMs, signal });
|
|
418
|
+
});
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
/**
|
|
422
|
+
* Run one filesystem-substrate op inside the distro through the agent.
|
|
423
|
+
* Responses reuse the EXEC `ACK`/`RES` lines, so cancellation and the
|
|
424
|
+
* watchdog are exactly the command path's. The rebuild relay differs by op:
|
|
425
|
+
* a read-only op rides a rebuild, a `write` in flight fails with an
|
|
426
|
+
* unknown-state error instead of being replayed.
|
|
427
|
+
* @param {object} request - the filesystem request.
|
|
428
|
+
* @param {string} request.op - op name (`stat`, `lstat`, `list`, `realpath`,
|
|
429
|
+
* `read`, `write`), dispatched by `agent/wsl-agent.sh`.
|
|
430
|
+
* @param {string[]} request.args - op arguments; each rides one base64 line.
|
|
431
|
+
* @param {number} [request.timeoutMs] - in-distro timeout, whole seconds
|
|
432
|
+
* upward, measured from the agent's dequeue ACK; `<= 0` or omitted means
|
|
433
|
+
* none.
|
|
434
|
+
* @param {AbortSignal} [request.signal] - cancels the request (KILL + reject).
|
|
435
|
+
* @returns {Promise<{exitCode: number, stdout: Buffer, stderr: Buffer}>}
|
|
436
|
+
* @throws {AgentUnavailableError} when the agent is out for this process.
|
|
437
|
+
*/
|
|
438
|
+
async fs({ op, args, timeoutMs = 0, signal }) {
|
|
439
|
+
if (this.state === "dead") {
|
|
440
|
+
throw new AgentUnavailableError(this.lastError);
|
|
441
|
+
}
|
|
442
|
+
await this.start();
|
|
443
|
+
const id = `r${this.nextRequestId++}`;
|
|
444
|
+
const lines = encodeFsFrame({ id, op, args, timeoutMs });
|
|
445
|
+
return new Promise((resolve, reject) => {
|
|
446
|
+
this.track({ id, lines, resolve, reject, timeoutMs, signal });
|
|
303
447
|
});
|
|
304
448
|
}
|
|
305
449
|
|
|
@@ -315,19 +459,7 @@ export class WslAgent {
|
|
|
315
459
|
return new Promise((resolve, reject) => {
|
|
316
460
|
const id = `r${this.nextRequestId++}`;
|
|
317
461
|
const lines = encodeExecFrame({ id, cwd: "/", argv: ["true"], timeoutMs: 0 });
|
|
318
|
-
|
|
319
|
-
lines,
|
|
320
|
-
resolve: () => resolve(),
|
|
321
|
-
reject,
|
|
322
|
-
watchdog: setTimeout(() => {
|
|
323
|
-
this.lastError = "agent did not answer ping";
|
|
324
|
-
this.process?.kill();
|
|
325
|
-
}, HANDSHAKE_TIMEOUT_MS),
|
|
326
|
-
signal: null,
|
|
327
|
-
onAbort: null,
|
|
328
|
-
};
|
|
329
|
-
this.pending.set(id, pending);
|
|
330
|
-
this.writeLines(lines);
|
|
462
|
+
this.track({ id, lines, resolve: () => resolve(), reject, timeoutMs: HANDSHAKE_TIMEOUT_MS, signal });
|
|
331
463
|
});
|
|
332
464
|
}
|
|
333
465
|
|
|
@@ -390,6 +522,24 @@ export class WslAgent {
|
|
|
390
522
|
}
|
|
391
523
|
}
|
|
392
524
|
|
|
525
|
+
/** The read-only FS ops: replaying them across a rebuild changes nothing. */
|
|
526
|
+
const REPLAYABLE_FS_OPS = new Set(["stat", "lstat", "list", "realpath", "read"]);
|
|
527
|
+
|
|
528
|
+
/**
|
|
529
|
+
* Whether a stored request frame's first execution provably had no side
|
|
530
|
+
* effects, so the frame may ride the one permitted rebuild. An EXEC never
|
|
531
|
+
* qualifies — a command's effects are unknowable after the agent died — and
|
|
532
|
+
* neither does an FS `write`. (A SETENV prefix line never decides: the frame
|
|
533
|
+
* header sits at `lines[0]` only for FS frames, which carry no SETENV.)
|
|
534
|
+
* @param {string[]} lines - the stored frame.
|
|
535
|
+
* @returns {boolean}
|
|
536
|
+
*/
|
|
537
|
+
function isReplayableFrame(lines) {
|
|
538
|
+
const header = lines[0] ?? "";
|
|
539
|
+
if (!header.startsWith("FS|")) return false;
|
|
540
|
+
return REPLAYABLE_FS_OPS.has(header.split("|")[2]);
|
|
541
|
+
}
|
|
542
|
+
|
|
393
543
|
/**
|
|
394
544
|
* Rewrite the request id inside a stored frame so a resent request does not
|
|
395
545
|
* collide with ids the new process has already seen. SETENV lines pass
|
|
@@ -403,7 +553,7 @@ export function relabelFrame(lines, newId) {
|
|
|
403
553
|
const separator = line.indexOf("|");
|
|
404
554
|
if (separator === -1) return line;
|
|
405
555
|
const kind = line.slice(0, separator);
|
|
406
|
-
if (kind !== "EXEC" && kind !== "KILL") return line;
|
|
556
|
+
if (kind !== "EXEC" && kind !== "FS" && kind !== "KILL") return line;
|
|
407
557
|
const rest = line.slice(separator + 1);
|
|
408
558
|
const pipe = rest.indexOf("|");
|
|
409
559
|
const tail = pipe === -1 ? "" : rest.slice(pipe);
|
package/lib/bwrap.js
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The distro-side `bwrap` command line, shared by every confinement site.
|
|
3
|
+
*
|
|
4
|
+
* Two callers compose a confined command and they must agree on it exactly:
|
|
5
|
+
* `lib/sandbox.js` wraps a *single* command (`confine()`, and the usability
|
|
6
|
+
* probe), and `lib/agent-confined.js` wraps a *resident* whose whole lifetime
|
|
7
|
+
* runs inside the profile — it hands the prefix to the transport, which inserts
|
|
8
|
+
* it between `wsl.exe --exec` and the `sh <script>` pair.
|
|
9
|
+
*
|
|
10
|
+
* That split is why this module exists. The two sites used to assemble the argv
|
|
11
|
+
* themselves, and only one of them prepended the program: the confined resident's
|
|
12
|
+
* prefix began at `--ro-bind`, so `wsl.exe --exec` tried to run a `--ro-bind`
|
|
13
|
+
* executable, the agent exited 1 during its handshake, and every confined
|
|
14
|
+
* mutation failed with `FS_IO_ERROR: … agent exited during handshake`. The
|
|
15
|
+
* verified probe could not see it because it inlined its own copy of the argv.
|
|
16
|
+
*
|
|
17
|
+
* This module imports nothing, so a bare checkout can assert the argv both sites
|
|
18
|
+
* actually produce.
|
|
19
|
+
*
|
|
20
|
+
* @module dsh-plugin-wsl/bwrap
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Build the bwrap profile arguments for one file-effect policy, exactly as
|
|
25
|
+
* upstream's `bwrapProfileArgs` does.
|
|
26
|
+
*
|
|
27
|
+
* `--ro-bind / /` makes the whole distro read-only, `--dev /dev` supplies the
|
|
28
|
+
* `/dev/null` sink a shell needs, `--unshare-pid` keeps the sandbox's process
|
|
29
|
+
* view its own, and `--proc /proc` gives it a matching `/proc`. Under
|
|
30
|
+
* `workspace-write` the workspace is bound read-write and `/tmp` becomes an
|
|
31
|
+
* ephemeral tmpfs — the temp area the mode promises, without exposing the
|
|
32
|
+
* distro's real `/tmp`.
|
|
33
|
+
*
|
|
34
|
+
* `options.maskWindowsDrive` mounts an empty tmpfs over `/mnt` AFTER the
|
|
35
|
+
* read-only root, shadowing the Windows drive: the drive's files disappear
|
|
36
|
+
* from the command's view, which severs interop as a side effect — a Windows
|
|
37
|
+
* executable on the drive can no longer be reached to launch, and the drive's
|
|
38
|
+
* data cannot be read or exfiltrated. It is a narrowing, not a closure: a
|
|
39
|
+
* command may still WRITE an executable into the workspace and run it
|
|
40
|
+
* (binfmt interop dispatches on file content, not location), so `enforcement`
|
|
41
|
+
* stays `partial` — see `lib/sandbox.js`.
|
|
42
|
+
*
|
|
43
|
+
* @param policy - the policy to express as bwrap mounts.
|
|
44
|
+
* @param options - profile options.
|
|
45
|
+
* @param options.maskWindowsDrive - shadow `/mnt` with an empty tmpfs.
|
|
46
|
+
* @returns profile arguments, before the `--` separator and the command argv.
|
|
47
|
+
*/
|
|
48
|
+
export function bwrapProfileArgs(policy, options = {}) {
|
|
49
|
+
const args = ["--ro-bind", "/", "/", "--dev", "/dev", "--unshare-pid", "--proc", "/proc", "--die-with-parent"];
|
|
50
|
+
if (policy.mode === "workspace-write") {
|
|
51
|
+
args.push("--tmpfs", "/tmp");
|
|
52
|
+
args.push("--bind", policy.workspaceRoot, policy.workspaceRoot);
|
|
53
|
+
}
|
|
54
|
+
if (options.maskWindowsDrive) {
|
|
55
|
+
args.push("--tmpfs", "/mnt");
|
|
56
|
+
}
|
|
57
|
+
return args;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* The argv prefix that runs one command (or one resident) inside the profile.
|
|
62
|
+
*
|
|
63
|
+
* The prefix is a complete command from the distro's point of view: it names the
|
|
64
|
+
* program (`bwrap`), the profile, and the `--` separator that ends bwrap's own
|
|
65
|
+
* options. `workspaceRoot` must already be spelled as a Linux path inside the
|
|
66
|
+
* distro; the callers own that translation.
|
|
67
|
+
*
|
|
68
|
+
* @param policy - the resolved policy: `mode`, plus the Linux `workspaceRoot`.
|
|
69
|
+
* @param options - profile options, passed through to {@link bwrapProfileArgs}.
|
|
70
|
+
* @returns argv to insert before the command, as the transport receives it.
|
|
71
|
+
*/
|
|
72
|
+
export function bwrapArgvPrefix(policy, options = {}) {
|
|
73
|
+
return ["bwrap", ...bwrapProfileArgs(policy, options), "--"];
|
|
74
|
+
}
|