@linxin666/dsh-win-bash 0.4.13 → 0.4.15
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/README.i18n.yaml +2 -2
- package/README.md +45 -2
- package/README.zh.md +29 -1
- package/lib/index.js +666 -38
- package/lib/types/background.d.ts +8 -1
- package/lib/types/dsh-home.d.ts +16 -0
- package/lib/types/gate.d.ts +1 -1
- package/lib/types/index.d.ts +14 -2
- package/lib/types/integrity.d.ts +81 -0
- package/lib/types/runtime.d.ts +170 -10
- package/lib/types/shell.d.ts +53 -1
- package/package.json +9 -9
- package/src/background.ts +20 -3
- package/src/dsh-home.ts +42 -0
- package/src/gate.ts +1 -1
- package/src/index.ts +62 -3
- package/src/integrity.ts +161 -0
- package/src/runtime.ts +477 -26
- package/src/shell.ts +182 -6
- package/src/tool.ts +3 -1
package/lib/index.js
CHANGED
|
@@ -1,10 +1,13 @@
|
|
|
1
1
|
import { createHash, randomUUID } from "node:crypto";
|
|
2
|
-
import { mkdirSync, readFileSync, statSync } from "node:fs";
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
2
|
+
import { cpSync, existsSync, mkdirSync, mkdtempSync, readFileSync, readdirSync, renameSync, rmSync, rmdirSync, statSync, unlinkSync } from "node:fs";
|
|
3
|
+
import { rm } from "node:fs/promises";
|
|
4
|
+
import { homedir, tmpdir } from "node:os";
|
|
5
|
+
import { dirname, isAbsolute, join, resolve } from "node:path";
|
|
5
6
|
import { fileURLToPath } from "node:url";
|
|
7
|
+
import { isAbsolute as isAbsolute$1, join as join$1 } from "node:path/posix";
|
|
8
|
+
import { spawnSync } from "node:child_process";
|
|
6
9
|
import { defineTool } from "@deepseek-ai/dsh-tools";
|
|
7
|
-
//#region ../../node_modules/.pnpm/@deepseek-ai+cosmokit@1.
|
|
10
|
+
//#region ../../node_modules/.pnpm/@deepseek-ai+cosmokit@1.9.0/node_modules/@deepseek-ai/cosmokit/lib/index.js
|
|
8
11
|
/** Return true when a value is `null` or `undefined`. */
|
|
9
12
|
function isNullable(value) {
|
|
10
13
|
return value === null || value === void 0;
|
|
@@ -258,7 +261,7 @@ var Time;
|
|
|
258
261
|
Time.template = template;
|
|
259
262
|
})(Time || (Time = {}));
|
|
260
263
|
//#endregion
|
|
261
|
-
//#region ../../node_modules/.pnpm/@deepseek-ai+schemastery@3.
|
|
264
|
+
//#region ../../node_modules/.pnpm/@deepseek-ai+schemastery@3.19.0/node_modules/@deepseek-ai/schemastery/lib/index.mjs
|
|
262
265
|
const kSchema = Symbol.for("schemastery");
|
|
263
266
|
const kValidationError = Symbol.for("ValidationError");
|
|
264
267
|
globalThis.__schemastery_index__ ??= 0;
|
|
@@ -1023,6 +1026,16 @@ const POLL_INTERVAL_MS = 15;
|
|
|
1023
1026
|
/** How long a freshly spawned shell gets to answer its startup handshake. */
|
|
1024
1027
|
const HANDSHAKE_TIMEOUT_MS = 15e3;
|
|
1025
1028
|
/**
|
|
1029
|
+
* Name prefix of every private per-shell temp directory. The rest of the name
|
|
1030
|
+
* is the owning process id plus a random suffix, which is what lets a later
|
|
1031
|
+
* start tell an orphan from a directory a live shell is still using.
|
|
1032
|
+
*/
|
|
1033
|
+
const SHELL_TEMP_PREFIX = "dsh-win-bash-";
|
|
1034
|
+
/** Retries granted to one reclaim, for handles a child releases late. */
|
|
1035
|
+
const TEMP_REMOVE_RETRIES = 3;
|
|
1036
|
+
/** Backoff between those retries, in milliseconds. */
|
|
1037
|
+
const TEMP_REMOVE_RETRY_DELAY_MS = 100;
|
|
1038
|
+
/**
|
|
1026
1039
|
* The argv of one pinned shell: the payload's own bash by absolute path, no rc
|
|
1027
1040
|
* file and no profile, reading its commands from stdin. This tool never
|
|
1028
1041
|
* consults PATH, and every shell it starts — the session's persistent one and
|
|
@@ -1058,13 +1071,131 @@ function shellEnvironment(runtime, temp, base = process.env) {
|
|
|
1058
1071
|
}
|
|
1059
1072
|
/**
|
|
1060
1073
|
* Create one shell process's private temp directory.
|
|
1074
|
+
* @param base - the temp root to create it under; the machine's own by default.
|
|
1061
1075
|
* @returns the absolute directory, created if absent.
|
|
1062
1076
|
*/
|
|
1063
|
-
function shellTempDir() {
|
|
1064
|
-
const temp = join(
|
|
1077
|
+
function shellTempDir(base = tmpdir()) {
|
|
1078
|
+
const temp = join(base, SHELL_TEMP_PREFIX + process.pid.toString() + "-" + randomUUID().slice(0, 8));
|
|
1065
1079
|
mkdirSync(temp, { recursive: true });
|
|
1066
1080
|
return temp;
|
|
1067
1081
|
}
|
|
1082
|
+
/**
|
|
1083
|
+
* Delete one private temp directory, reporting failure to the caller.
|
|
1084
|
+
*
|
|
1085
|
+
* The retries are the Windows half of this: a child that just exited has not
|
|
1086
|
+
* necessarily released its own handles yet, and rm reports that as EBUSY/EPERM.
|
|
1087
|
+
* A few short retries turn that into a reclaim instead of a leftover.
|
|
1088
|
+
* @param temp - the directory to delete; a missing one is not an error.
|
|
1089
|
+
*/
|
|
1090
|
+
async function deleteShellTempDir(temp) {
|
|
1091
|
+
await rm(temp, {
|
|
1092
|
+
recursive: true,
|
|
1093
|
+
force: true,
|
|
1094
|
+
maxRetries: TEMP_REMOVE_RETRIES,
|
|
1095
|
+
retryDelay: TEMP_REMOVE_RETRY_DELAY_MS
|
|
1096
|
+
});
|
|
1097
|
+
}
|
|
1098
|
+
/**
|
|
1099
|
+
* Delete one private temp directory, never letting the attempt throw.
|
|
1100
|
+
*
|
|
1101
|
+
* Teardown must not fail because a directory could not be reclaimed: the shell
|
|
1102
|
+
* is already gone, and whatever is left is named for this process's own pid, so
|
|
1103
|
+
* the next start's {@link sweepOrphanShellTempDirs} collects it once that pid is
|
|
1104
|
+
* gone.
|
|
1105
|
+
* @param temp - the directory that shell owned.
|
|
1106
|
+
*/
|
|
1107
|
+
async function removeShellTempDir(temp) {
|
|
1108
|
+
try {
|
|
1109
|
+
await deleteShellTempDir(temp);
|
|
1110
|
+
} catch {}
|
|
1111
|
+
}
|
|
1112
|
+
/**
|
|
1113
|
+
* Whether a process id still exists.
|
|
1114
|
+
*
|
|
1115
|
+
* Signal 0 performs the existence check without delivering anything. EPERM is
|
|
1116
|
+
* the one answer that means "alive": the pid is there, it just belongs to a
|
|
1117
|
+
* process this one may not signal.
|
|
1118
|
+
* @param pid - the process id taken from a directory name.
|
|
1119
|
+
* @returns whether that process is still running.
|
|
1120
|
+
*/
|
|
1121
|
+
function processAlive(pid) {
|
|
1122
|
+
try {
|
|
1123
|
+
process.kill(pid, 0);
|
|
1124
|
+
return true;
|
|
1125
|
+
} catch (error) {
|
|
1126
|
+
return error.code === "EPERM";
|
|
1127
|
+
}
|
|
1128
|
+
}
|
|
1129
|
+
/**
|
|
1130
|
+
* The owning pid of a shell temp directory, when the name is one.
|
|
1131
|
+
* @param name - a directory name inside the temp root.
|
|
1132
|
+
* @returns the pid, or undefined for every other name.
|
|
1133
|
+
*/
|
|
1134
|
+
function shellTempOwner(name) {
|
|
1135
|
+
const match = (/* @__PURE__ */ new RegExp("^dsh-win-bash-(\\d+)-[0-9a-f]{8}$")).exec(name);
|
|
1136
|
+
if (match === null) return void 0;
|
|
1137
|
+
const pid = Number.parseInt(match[1] ?? "", 10);
|
|
1138
|
+
return Number.isSafeInteger(pid) && pid > 0 ? pid : void 0;
|
|
1139
|
+
}
|
|
1140
|
+
/**
|
|
1141
|
+
* List one temp root, reporting an unreadable root as empty.
|
|
1142
|
+
*
|
|
1143
|
+
* There is nothing to reclaim when the root cannot be read, and the next start
|
|
1144
|
+
* retries with the same rules; failing the mount over it would be worse.
|
|
1145
|
+
* @param root - the temp root to list.
|
|
1146
|
+
* @returns its entries, or none.
|
|
1147
|
+
*/
|
|
1148
|
+
function tempRootEntries(root) {
|
|
1149
|
+
try {
|
|
1150
|
+
return readdirSync(root, { withFileTypes: true });
|
|
1151
|
+
} catch {
|
|
1152
|
+
return [];
|
|
1153
|
+
}
|
|
1154
|
+
}
|
|
1155
|
+
/**
|
|
1156
|
+
* Reclaim the private temp directories of shells that no longer exist.
|
|
1157
|
+
*
|
|
1158
|
+
* A crash or a hard kill ends this process before it can delete what its live
|
|
1159
|
+
* shells owned, and those directories are not small: whatever a command wrote
|
|
1160
|
+
* under TMP/TEMP/TMPDIR is still in them. Every name carries the pid that owned
|
|
1161
|
+
* it, so the ones whose pid is gone are garbage by construction, and the ones
|
|
1162
|
+
* whose pid still runs may belong to a live shell in this very process or to
|
|
1163
|
+
* another instance sharing the temp root - those are kept.
|
|
1164
|
+
*
|
|
1165
|
+
* One directory that cannot be deleted is reported and skipped; it must not
|
|
1166
|
+
* stop the round, because the usual cause (a long path, a locked file) is local
|
|
1167
|
+
* to that directory.
|
|
1168
|
+
* @param options - the seams; production passes none.
|
|
1169
|
+
* @returns what was removed, kept and failed.
|
|
1170
|
+
*/
|
|
1171
|
+
async function sweepOrphanShellTempDirs(options = {}) {
|
|
1172
|
+
const root = options.root ?? tmpdir();
|
|
1173
|
+
const isAlive = options.isAlive ?? processAlive;
|
|
1174
|
+
const remove = options.remove ?? deleteShellTempDir;
|
|
1175
|
+
const removed = [];
|
|
1176
|
+
const kept = [];
|
|
1177
|
+
const failed = [];
|
|
1178
|
+
for (const entry of tempRootEntries(root)) {
|
|
1179
|
+
if (!entry.isDirectory()) continue;
|
|
1180
|
+
const pid = shellTempOwner(entry.name);
|
|
1181
|
+
if (pid === void 0) continue;
|
|
1182
|
+
if (pid === process.pid || isAlive(pid)) {
|
|
1183
|
+
kept.push(entry.name);
|
|
1184
|
+
continue;
|
|
1185
|
+
}
|
|
1186
|
+
try {
|
|
1187
|
+
await remove(join(root, entry.name));
|
|
1188
|
+
removed.push(entry.name);
|
|
1189
|
+
} catch {
|
|
1190
|
+
failed.push(entry.name);
|
|
1191
|
+
}
|
|
1192
|
+
}
|
|
1193
|
+
return {
|
|
1194
|
+
removed,
|
|
1195
|
+
kept,
|
|
1196
|
+
failed
|
|
1197
|
+
};
|
|
1198
|
+
}
|
|
1068
1199
|
/** Failure raised when the shell cannot serve a call. */
|
|
1069
1200
|
var ShellUnavailableError = class extends Error {
|
|
1070
1201
|
/** Stable machine-readable reason. */
|
|
@@ -1240,6 +1371,7 @@ var ShellRegistry = class {
|
|
|
1240
1371
|
if (shell.exited) {
|
|
1241
1372
|
resume();
|
|
1242
1373
|
this.shells.delete(owner);
|
|
1374
|
+
await removeShellTempDir(shell.temp);
|
|
1243
1375
|
const partial = tailBytes(recoverPartial(frame.text, marker), budget.stdoutBytes);
|
|
1244
1376
|
if (shell.exitCode === null) throw new ShellUnavailableError("shell-exited", "The persistent bash shell exited without reporting a status, so this command did not complete. The next win_bash call starts a fresh shell in the session workspace with a new current directory and environment.");
|
|
1245
1377
|
const visibleErr = stderr.visible(budget.stderrBytes);
|
|
@@ -1293,6 +1425,7 @@ var ShellRegistry = class {
|
|
|
1293
1425
|
const existing = this.shells.get(owner);
|
|
1294
1426
|
if (existing !== void 0 && !existing.exited) return existing;
|
|
1295
1427
|
if (existing !== void 0) this.shells.delete(owner);
|
|
1428
|
+
const temp = shellTempDir();
|
|
1296
1429
|
const handle = this.subprocess.spawn({
|
|
1297
1430
|
argv: shellArgv(runtime),
|
|
1298
1431
|
cwd,
|
|
@@ -1308,10 +1441,11 @@ var ShellRegistry = class {
|
|
|
1308
1441
|
}
|
|
1309
1442
|
},
|
|
1310
1443
|
graceMs: GRACE_MS,
|
|
1311
|
-
env: shellEnvironment(runtime,
|
|
1444
|
+
env: shellEnvironment(runtime, temp)
|
|
1312
1445
|
});
|
|
1313
1446
|
const shell = {
|
|
1314
1447
|
handle,
|
|
1448
|
+
temp,
|
|
1315
1449
|
exited: false,
|
|
1316
1450
|
cursor: {
|
|
1317
1451
|
stdout: 0,
|
|
@@ -1428,12 +1562,20 @@ function recoverPartial(text, marker) {
|
|
|
1428
1562
|
const begin = text.lastIndexOf(marker.begin);
|
|
1429
1563
|
return begin < 0 ? text : text.slice(begin + marker.begin.length).replace(/^\n/, "");
|
|
1430
1564
|
}
|
|
1431
|
-
/**
|
|
1565
|
+
/**
|
|
1566
|
+
* Signal one shell's managed range and reclaim what it owned, never letting
|
|
1567
|
+
* teardown throw.
|
|
1568
|
+
*
|
|
1569
|
+
* The directory is deleted after the range is gone rather than before: it is
|
|
1570
|
+
* the shell's TMP/TEMP/TMPDIR, so deleting it under a live process would turn
|
|
1571
|
+
* every command still writing there into a failure.
|
|
1572
|
+
*/
|
|
1432
1573
|
async function terminate(shell, reason) {
|
|
1433
1574
|
try {
|
|
1434
1575
|
shell.handle.terminate();
|
|
1435
1576
|
await shell.handle.waitForExit();
|
|
1436
1577
|
} catch {}
|
|
1578
|
+
await removeShellTempDir(shell.temp);
|
|
1437
1579
|
}
|
|
1438
1580
|
//#endregion
|
|
1439
1581
|
//#region src/config.ts
|
|
@@ -2012,6 +2154,128 @@ function installPwshReplacement(ctx, options = {}) {
|
|
|
2012
2154
|
}, "dsh-win-bash: host pwsh replacement");
|
|
2013
2155
|
}
|
|
2014
2156
|
//#endregion
|
|
2157
|
+
//#region src/dsh-home.ts
|
|
2158
|
+
/**
|
|
2159
|
+
* DSH_HOME resolution shared by the plugin family's Host halves: the
|
|
2160
|
+
* environment override wins, the platform home fallback follows. Mirrors
|
|
2161
|
+
* what dsh-pet and dsh-liangshen each used to implement locally.
|
|
2162
|
+
*/
|
|
2163
|
+
/** Expand a leading ~ (or ~user) in a path, platform-style. */
|
|
2164
|
+
function expandHome(path, home = homedir()) {
|
|
2165
|
+
const j = home.startsWith("/") ? join$1 : join;
|
|
2166
|
+
if (path === "~") return home;
|
|
2167
|
+
if (path.startsWith("~/") || path.startsWith("~\\")) return j(home, path.slice(2));
|
|
2168
|
+
return path;
|
|
2169
|
+
}
|
|
2170
|
+
/**
|
|
2171
|
+
* Resolve the DSH home directory.
|
|
2172
|
+
* @param env - process environment to read DSH_HOME from.
|
|
2173
|
+
* @param home - platform home directory fallback (test seam).
|
|
2174
|
+
* @returns the absolute DSH home path.
|
|
2175
|
+
*/
|
|
2176
|
+
function resolveDshHome(env = process.env, home = homedir()) {
|
|
2177
|
+
const isPosix = home.startsWith("/");
|
|
2178
|
+
const j = isPosix ? join$1 : join;
|
|
2179
|
+
const isAbs = isPosix ? isAbsolute$1 : isAbsolute;
|
|
2180
|
+
const raw = env.DSH_HOME;
|
|
2181
|
+
if (raw !== void 0 && raw.trim() !== "") {
|
|
2182
|
+
const expanded = expandHome(raw.trim(), home);
|
|
2183
|
+
return isAbs(expanded) ? expanded : j(process.cwd(), expanded);
|
|
2184
|
+
}
|
|
2185
|
+
return j(home, ".dsh");
|
|
2186
|
+
}
|
|
2187
|
+
/** Wall-clock bound on one icacls query for a security descriptor. */
|
|
2188
|
+
const READ_LABEL_TIMEOUT_MS = 5e3;
|
|
2189
|
+
/** One mandatory-label ACE inside an SDDL SACL: (ML;flags;rights;;;SID). */
|
|
2190
|
+
const MANDATORY_LABEL_ACE = /\(ML;[^)]*;([^;)]+)\)/gi;
|
|
2191
|
+
/**
|
|
2192
|
+
* Classify the mandatory label ACEs of one icacls /save SDDL blob.
|
|
2193
|
+
*
|
|
2194
|
+
* A missing SACL means the object has no explicit label and inherits Medium by
|
|
2195
|
+
* default. High/System/Medium labels are harmless here; only Low or Untrusted
|
|
2196
|
+
* can turn a Medium runner's child into a Low process.
|
|
2197
|
+
* @param sddl - the UTF-16LE-decoded contents of an icacls /save file.
|
|
2198
|
+
* @returns the label verdict. `unknown` is reserved for the reader that could
|
|
2199
|
+
* not obtain or trust a descriptor at all; a parsed blob always decides.
|
|
2200
|
+
*/
|
|
2201
|
+
function parseIntegritySddl(sddl) {
|
|
2202
|
+
const sacl = /S:([^\r\n]*)/.exec(sddl)?.[1] ?? "";
|
|
2203
|
+
for (const match of sacl.matchAll(MANDATORY_LABEL_ACE)) {
|
|
2204
|
+
const sid = (match[1] ?? "").trim().toUpperCase();
|
|
2205
|
+
if (sid === "LW") return "low-or-untrusted";
|
|
2206
|
+
const numeric = /^S-1-16-(\d+)$/.exec(sid);
|
|
2207
|
+
if (numeric !== null && Number(numeric[1]) < 8192) return "low-or-untrusted";
|
|
2208
|
+
}
|
|
2209
|
+
return "medium-or-higher";
|
|
2210
|
+
}
|
|
2211
|
+
/** Absolute path of the Windows icacls binary, never resolved through PATH. */
|
|
2212
|
+
function icaclsPath(env) {
|
|
2213
|
+
return join(env.SystemRoot ?? env.windir ?? "C:\\Windows", "System32", "icacls.exe");
|
|
2214
|
+
}
|
|
2215
|
+
/**
|
|
2216
|
+
* Read one existing path's mandatory integrity label.
|
|
2217
|
+
*
|
|
2218
|
+
* On a POSIX host there is no Windows mandatory integrity control (and this
|
|
2219
|
+
* package does not mount there), so the label cannot downgrade anything. A
|
|
2220
|
+
* missing icacls, a failed query or an unreadable descriptor is reported as
|
|
2221
|
+
* `unknown` rather than guessed: the caller decides whether to fall back.
|
|
2222
|
+
* @param target - an existing file or directory to inspect.
|
|
2223
|
+
* @param env - environment supplying SystemRoot; the live one by default.
|
|
2224
|
+
* @returns the label verdict.
|
|
2225
|
+
*/
|
|
2226
|
+
function readMandatoryLabel(target, env = process.env) {
|
|
2227
|
+
if (process.platform !== "win32") return "medium-or-higher";
|
|
2228
|
+
const exe = icaclsPath(env);
|
|
2229
|
+
if (!existsSync(exe)) return "unknown";
|
|
2230
|
+
let scratch;
|
|
2231
|
+
let aclFile;
|
|
2232
|
+
try {
|
|
2233
|
+
scratch = mkdtempSync(join(tmpdir(), "dsh-win-bash-acl-"));
|
|
2234
|
+
aclFile = join(scratch, "acl-" + randomUUID().slice(0, 8) + ".txt");
|
|
2235
|
+
const run = spawnSync(exe, [
|
|
2236
|
+
target,
|
|
2237
|
+
"/save",
|
|
2238
|
+
aclFile
|
|
2239
|
+
], {
|
|
2240
|
+
windowsHide: true,
|
|
2241
|
+
timeout: READ_LABEL_TIMEOUT_MS,
|
|
2242
|
+
stdio: "ignore"
|
|
2243
|
+
});
|
|
2244
|
+
if (run.error !== void 0 || run.status !== 0 || !existsSync(aclFile)) return "unknown";
|
|
2245
|
+
return parseIntegritySddl(readFileSync(aclFile).toString("utf16le"));
|
|
2246
|
+
} catch {
|
|
2247
|
+
return "unknown";
|
|
2248
|
+
} finally {
|
|
2249
|
+
if (aclFile !== void 0) try {
|
|
2250
|
+
unlinkSync(aclFile);
|
|
2251
|
+
} catch {}
|
|
2252
|
+
if (scratch !== void 0) try {
|
|
2253
|
+
rmdirSync(scratch);
|
|
2254
|
+
} catch {}
|
|
2255
|
+
}
|
|
2256
|
+
}
|
|
2257
|
+
/**
|
|
2258
|
+
* Every existing path from `path` up to the volume root.
|
|
2259
|
+
*
|
|
2260
|
+
* A cache root can sit several levels below the data tree that a workspace
|
|
2261
|
+
* sandbox labelled Low, and an inheritable ACE on ANY of those ancestors can
|
|
2262
|
+
* still be propagated onto existing children later. Checking only the nearest
|
|
2263
|
+
* existing ancestor would miss that, so the whole chain is inspected.
|
|
2264
|
+
* @param path - the prospective path (it need not exist yet).
|
|
2265
|
+
* @returns the existing ancestors, nearest first.
|
|
2266
|
+
*/
|
|
2267
|
+
function existingAncestors(path) {
|
|
2268
|
+
const found = [];
|
|
2269
|
+
let current = resolve(path);
|
|
2270
|
+
for (let depth = 0; depth < 32; depth += 1) {
|
|
2271
|
+
if (existsSync(current)) found.push(current);
|
|
2272
|
+
const parent = dirname(current);
|
|
2273
|
+
if (parent === current) break;
|
|
2274
|
+
current = parent;
|
|
2275
|
+
}
|
|
2276
|
+
return found;
|
|
2277
|
+
}
|
|
2278
|
+
//#endregion
|
|
2015
2279
|
//#region src/runtime.ts
|
|
2016
2280
|
/**
|
|
2017
2281
|
* Payload resolution and load-time integrity verification for the vendored
|
|
@@ -2020,12 +2284,33 @@ function installPwshReplacement(ctx, options = {}) {
|
|
|
2020
2284
|
* The package ships its own Git for Windows MinGit extraction under
|
|
2021
2285
|
* runtime/win32-x64/: the tool must NEVER fall back to a bash found on PATH
|
|
2022
2286
|
* (a host Git for Windows install, WSL, or a stray MSYS), because the whole
|
|
2023
|
-
* point of vendoring is a shell whose version and bytes are pinned.
|
|
2287
|
+
* point of vendoring is a shell whose version and bytes are pinned. Four
|
|
2024
2288
|
* invariants implement that:
|
|
2025
2289
|
*
|
|
2026
|
-
* 1. the binary is addressed by absolute path
|
|
2290
|
+
* 1. the binary is addressed by absolute path, never by name,
|
|
2027
2291
|
* 2. every vendored byte is checked against the SHA256 manifest generated at
|
|
2028
|
-
* packaging time before
|
|
2292
|
+
* packaging time before it is used at all, and
|
|
2293
|
+
* 3. the tree that RUNS is a published copy under the DSH home, not the tree
|
|
2294
|
+
* inside the package.
|
|
2295
|
+
* 4. the copy's Windows mandatory integrity label is checked before it is
|
|
2296
|
+
* reused or before a freshly published copy is spawned. A Low (or lower)
|
|
2297
|
+
* label on the executable image makes Windows start a Low-integrity process
|
|
2298
|
+
* even from a Medium runner, and that process cannot write an ordinary
|
|
2299
|
+
* Medium workspace or temp directory. Such a tree is never used: the payload
|
|
2300
|
+
* is published into an isolated cache root instead, and if no candidate root
|
|
2301
|
+
* is safe the mount is refused with a specific diagnosis.
|
|
2302
|
+
*
|
|
2303
|
+
* The third invariant is why this module publishes a cache: Windows refuses to
|
|
2304
|
+
* rename a directory whose executable is running, and pnpm imports a package by
|
|
2305
|
+
* renaming a staging directory over it, so a shell spawned from inside
|
|
2306
|
+
* node_modules makes every later update or uninstall of this package fail with
|
|
2307
|
+
* ERR_PNPM_EPERM until the host stops. A persistent shell is this package's
|
|
2308
|
+
* whole point, so the payload moves out of the package instead of the process
|
|
2309
|
+
* moving out of the payload. The copy is content-addressed by the shipped
|
|
2310
|
+
* manifest: the directory name is a digest of the payload's file identities, so
|
|
2311
|
+
* a release that changes only JavaScript reuses the copy already published, and
|
|
2312
|
+
* a copy whose bytes or manifest disagree with the shipped identity is never
|
|
2313
|
+
* used.
|
|
2029
2314
|
*
|
|
2030
2315
|
* A mismatch refuses the mount with a specific message. The alternative —
|
|
2031
2316
|
* mounting and hoping — would let a tampered or half-installed payload run
|
|
@@ -2037,23 +2322,91 @@ function installPwshReplacement(ctx, options = {}) {
|
|
|
2037
2322
|
const RUNTIME_DIR = "runtime/win32-x64";
|
|
2038
2323
|
/** Manifest file name inside {@link RUNTIME_DIR}. */
|
|
2039
2324
|
const CHECKSUM_FILE = "checksums.sha256";
|
|
2325
|
+
/**
|
|
2326
|
+
* Payload cache directories under the DSH home, in order.
|
|
2327
|
+
*
|
|
2328
|
+
* The cache lives OUTSIDE the package on purpose: a running shell holds its own
|
|
2329
|
+
* executable open, Windows refuses to rename a directory that holds a running
|
|
2330
|
+
* executable, and pnpm imports a package by renaming over it. A copy here is
|
|
2331
|
+
* also why a release that only changes JavaScript never has to move 16 MiB.
|
|
2332
|
+
*/
|
|
2333
|
+
const CACHE_DIR_PARTS = ["win-bash", "runtime"];
|
|
2334
|
+
/**
|
|
2335
|
+
* Directory parts of the isolated fallback cache, used only when the preferred
|
|
2336
|
+
* cache root carries a Low mandatory integrity label. It lives outside the DSH
|
|
2337
|
+
* data tree, which a workspace sandbox can label Low with an inheritable ACE.
|
|
2338
|
+
*/
|
|
2339
|
+
const ISOLATED_CACHE_DIR_PARTS = ["dsh-win-bash", "runtime"];
|
|
2340
|
+
/**
|
|
2341
|
+
* Optional environment override for the isolated cache root's parent. An
|
|
2342
|
+
* operator whose local app data is itself labelled Low can point this at a
|
|
2343
|
+
* directory they have verified is Medium; it is not needed on a normal host.
|
|
2344
|
+
*/
|
|
2345
|
+
const RUNTIME_ROOT_ENV = "DSH_WIN_BASH_RUNTIME_HOME";
|
|
2346
|
+
/**
|
|
2347
|
+
* Name prefix of the staging directory one cache copy is built in. The rest of
|
|
2348
|
+
* the name is the payload key, this process's id and a random suffix, so a
|
|
2349
|
+
* half-written directory is never mistaken for a published payload.
|
|
2350
|
+
*/
|
|
2351
|
+
const CACHE_STAGING_PREFIX = ".staging-";
|
|
2040
2352
|
/** Absolute package root (the directory holding package.json). */
|
|
2041
2353
|
const PACKAGE_ROOT = resolve(dirname(fileURLToPath(import.meta.url)), "..");
|
|
2042
|
-
/** Absolute payload root. */
|
|
2354
|
+
/** Absolute payload root inside one package root. */
|
|
2043
2355
|
function runtimeRoot(packageRoot = PACKAGE_ROOT) {
|
|
2044
2356
|
return join(packageRoot, RUNTIME_DIR);
|
|
2045
2357
|
}
|
|
2046
|
-
/** Absolute path of the vendored shell. */
|
|
2358
|
+
/** Absolute path of the vendored shell inside one payload root. */
|
|
2359
|
+
function bashIn(root) {
|
|
2360
|
+
return join(root, "usr", "bin", "bash.exe");
|
|
2361
|
+
}
|
|
2362
|
+
/** Absolute path of the vendored POSIX-shell alias inside one payload root. */
|
|
2363
|
+
function shIn(root) {
|
|
2364
|
+
return join(root, "usr", "bin", "sh.exe");
|
|
2365
|
+
}
|
|
2366
|
+
/** Absolute path of one payload root's usr/bin directory. */
|
|
2367
|
+
function binIn(root) {
|
|
2368
|
+
return join(root, "usr", "bin");
|
|
2369
|
+
}
|
|
2370
|
+
/** Absolute path of the packaged payload's shell. */
|
|
2047
2371
|
function bashPath(packageRoot = PACKAGE_ROOT) {
|
|
2048
|
-
return
|
|
2372
|
+
return bashIn(runtimeRoot(packageRoot));
|
|
2049
2373
|
}
|
|
2050
|
-
/** Absolute path of the
|
|
2374
|
+
/** Absolute path of the packaged payload's POSIX-shell alias. */
|
|
2051
2375
|
function shPath(packageRoot = PACKAGE_ROOT) {
|
|
2052
|
-
return
|
|
2376
|
+
return shIn(runtimeRoot(packageRoot));
|
|
2053
2377
|
}
|
|
2054
|
-
/**
|
|
2055
|
-
|
|
2056
|
-
|
|
2378
|
+
/**
|
|
2379
|
+
* Absolute root of the per-user payload cache.
|
|
2380
|
+
*
|
|
2381
|
+
* It follows the DSH home rather than the package, so an isolated
|
|
2382
|
+
* `DSH_HOME` (a probe instance, a test run) gets its own cache, and an update or
|
|
2383
|
+
* uninstall of this package never touches a byte the cache holds.
|
|
2384
|
+
* @param env - environment to read DSH_HOME from; the live one by default.
|
|
2385
|
+
* @returns the directory payload copies are published under.
|
|
2386
|
+
*/
|
|
2387
|
+
function cacheRoot(env = process.env) {
|
|
2388
|
+
return join(resolveDshHome(env), ...CACHE_DIR_PARTS);
|
|
2389
|
+
}
|
|
2390
|
+
/**
|
|
2391
|
+
* Absolute root of the isolated fallback payload cache.
|
|
2392
|
+
*
|
|
2393
|
+
* The DSH home is a data tree a workspace sandbox may have labelled Low with
|
|
2394
|
+
* an inheritable ACE. A copy published below it then inherits that label and
|
|
2395
|
+
* starts a Low-integrity bash, which cannot write a Medium workspace or temp
|
|
2396
|
+
* directory. This root deliberately lives OUTSIDE the DSH home, under the
|
|
2397
|
+
* per-user application-data (or cache) directory instead.
|
|
2398
|
+
*
|
|
2399
|
+
* It still follows the probe-isolation convention: the final path component is
|
|
2400
|
+
* derived from the DSH home, so an isolated `DSH_HOME` gets its own isolated
|
|
2401
|
+
* cache too. Operators whose application-data tree is itself labelled Low can
|
|
2402
|
+
* point {@link RUNTIME_ROOT_ENV} at a directory they have verified.
|
|
2403
|
+
* @param env - environment to read the override, app-data root and DSH home from.
|
|
2404
|
+
* @returns the directory the fallback payload copies are published under.
|
|
2405
|
+
*/
|
|
2406
|
+
function isolatedCacheRoot(env = process.env) {
|
|
2407
|
+
const override = env[RUNTIME_ROOT_ENV]?.trim();
|
|
2408
|
+
if (override !== void 0 && override !== "") return resolve(override);
|
|
2409
|
+
return join(env.LOCALAPPDATA ?? (process.platform === "win32" ? join(homedir(), "AppData", "Local") : env.XDG_CACHE_HOME ?? join(homedir(), ".cache")), ...ISOLATED_CACHE_DIR_PARTS, sha256(resolveDshHome(env)).slice(0, 12));
|
|
2057
2410
|
}
|
|
2058
2411
|
/**
|
|
2059
2412
|
* Parse a sha256sum-style manifest: '<64 hex> <path>' per line. Blank lines
|
|
@@ -2082,6 +2435,18 @@ function sha256(bytes) {
|
|
|
2082
2435
|
return createHash("sha256").update(bytes).digest("hex");
|
|
2083
2436
|
}
|
|
2084
2437
|
/**
|
|
2438
|
+
* The content key of one payload: a digest over its manifest's file identities.
|
|
2439
|
+
*
|
|
2440
|
+
* Manifest formatting and row order do not move it, and any change to a
|
|
2441
|
+
* payload file does. That is what makes a cache directory name a statement
|
|
2442
|
+
* about the bytes inside it rather than about the release that shipped them.
|
|
2443
|
+
* @param entries - the parsed manifest rows.
|
|
2444
|
+
* @returns the 16-character key that names the cache directory.
|
|
2445
|
+
*/
|
|
2446
|
+
function payloadKey(entries) {
|
|
2447
|
+
return sha256(entries.map((entry) => entry.path + " " + entry.sha256).sort().join("\n")).slice(0, 16);
|
|
2448
|
+
}
|
|
2449
|
+
/**
|
|
2085
2450
|
* Verify every registered payload file against the shipped manifest.
|
|
2086
2451
|
*
|
|
2087
2452
|
* Reads and hashes the whole payload (about 16 MiB after the curated
|
|
@@ -2092,7 +2457,19 @@ function sha256(bytes) {
|
|
|
2092
2457
|
* @throws when the manifest itself is unreadable or malformed.
|
|
2093
2458
|
*/
|
|
2094
2459
|
function verifyPayload(packageRoot = PACKAGE_ROOT) {
|
|
2095
|
-
|
|
2460
|
+
return verifyPayloadRoot(runtimeRoot(packageRoot));
|
|
2461
|
+
}
|
|
2462
|
+
/**
|
|
2463
|
+
* Verify every payload file of one payload root against that root's manifest.
|
|
2464
|
+
*
|
|
2465
|
+
* The root is the directory holding `usr/` and `checksums.sha256`, which is
|
|
2466
|
+
* either the packaged payload or a published cache copy, so both go through the
|
|
2467
|
+
* same pass.
|
|
2468
|
+
* @param root - the payload root to verify.
|
|
2469
|
+
* @returns the count of verified files and every mismatch found.
|
|
2470
|
+
* @throws when the manifest itself is unreadable or malformed.
|
|
2471
|
+
*/
|
|
2472
|
+
function verifyPayloadRoot(root) {
|
|
2096
2473
|
const manifest = parseChecksums(readFileSync(join(root, CHECKSUM_FILE), "utf8"));
|
|
2097
2474
|
const mismatches = [];
|
|
2098
2475
|
let verified = 0;
|
|
@@ -2118,16 +2495,11 @@ function verifyPayload(packageRoot = PACKAGE_ROOT) {
|
|
|
2118
2495
|
};
|
|
2119
2496
|
}
|
|
2120
2497
|
/**
|
|
2121
|
-
*
|
|
2122
|
-
*
|
|
2123
|
-
* The order is deliberate: platform, then architecture, then presence, then
|
|
2124
|
-
* integrity. A checksum failure names the first mismatching path so an
|
|
2125
|
-
* operator can act on it, and no path here ever consults PATH.
|
|
2498
|
+
* Refuse a host that cannot run this payload at all.
|
|
2126
2499
|
* @param host - the running host's platform facts.
|
|
2127
|
-
* @
|
|
2128
|
-
* @returns the resolved shell paths, or the rejection reason.
|
|
2500
|
+
* @returns the rejection, or undefined when the host could serve the payload.
|
|
2129
2501
|
*/
|
|
2130
|
-
function
|
|
2502
|
+
function rejectHost(host) {
|
|
2131
2503
|
if (host.platform !== "win32") return {
|
|
2132
2504
|
code: "unsupported-platform",
|
|
2133
2505
|
message: "win_bash requires Windows: the vendored payload is a Git for Windows MinGit build for win32 and there is no PATH fallback."
|
|
@@ -2136,6 +2508,32 @@ function resolveRuntime(host, packageRoot = PACKAGE_ROOT) {
|
|
|
2136
2508
|
code: "unsupported-arch",
|
|
2137
2509
|
message: "win_bash currently ships only the x64 payload; this host is " + host.arch + "."
|
|
2138
2510
|
};
|
|
2511
|
+
}
|
|
2512
|
+
/** The three paths one payload root provides, tagged with where it came from. */
|
|
2513
|
+
function readyFrom(root, source) {
|
|
2514
|
+
return {
|
|
2515
|
+
bash: bashIn(root),
|
|
2516
|
+
bin: binIn(root),
|
|
2517
|
+
payloadRoot: root,
|
|
2518
|
+
source
|
|
2519
|
+
};
|
|
2520
|
+
}
|
|
2521
|
+
/**
|
|
2522
|
+
* Resolve the PACKAGED shell for one host, or refuse the mount.
|
|
2523
|
+
*
|
|
2524
|
+
* The order is deliberate: platform, then architecture, then presence, then
|
|
2525
|
+
* integrity. A checksum failure names the first mismatching path so an
|
|
2526
|
+
* operator can act on it, and no path here ever consults PATH.
|
|
2527
|
+
*
|
|
2528
|
+
* This is the packaged tree, not the cache: callers that intend to RUN the
|
|
2529
|
+
* shell want {@link prepareRuntime}, which publishes a copy first.
|
|
2530
|
+
* @param host - the running host's platform facts.
|
|
2531
|
+
* @param packageRoot - package root holding the payload.
|
|
2532
|
+
* @returns the resolved shell paths, or the rejection reason.
|
|
2533
|
+
*/
|
|
2534
|
+
function resolveRuntime(host, packageRoot = PACKAGE_ROOT) {
|
|
2535
|
+
const refused = rejectHost(host);
|
|
2536
|
+
if (refused !== void 0) return refused;
|
|
2139
2537
|
const bash = bashPath(packageRoot);
|
|
2140
2538
|
try {
|
|
2141
2539
|
if (!statSync(bash).isFile()) throw new Error("not a file");
|
|
@@ -2161,10 +2559,224 @@ function resolveRuntime(host, packageRoot = PACKAGE_ROOT) {
|
|
|
2161
2559
|
message: "the vendored payload failed its SHA256 manifest: " + report.mismatches.length.toString() + " file(s) differ, first " + first.path + " (expected " + first.expected.slice(0, 12) + ", found " + first.actual.slice(0, 12) + "). Reinstall the package; win_bash will not run an unverified payload."
|
|
2162
2560
|
};
|
|
2163
2561
|
}
|
|
2164
|
-
return
|
|
2165
|
-
|
|
2166
|
-
|
|
2562
|
+
return readyFrom(runtimeRoot(packageRoot), "package");
|
|
2563
|
+
}
|
|
2564
|
+
/** The recursive copy one published payload is built with. */
|
|
2565
|
+
function copyTree(source, target) {
|
|
2566
|
+
cpSync(source, target, {
|
|
2567
|
+
recursive: true,
|
|
2568
|
+
force: true,
|
|
2569
|
+
preserveTimestamps: true
|
|
2570
|
+
});
|
|
2571
|
+
}
|
|
2572
|
+
/** One error's message, for an operator-facing line. */
|
|
2573
|
+
function describe(error) {
|
|
2574
|
+
return error instanceof Error ? error.message : String(error);
|
|
2575
|
+
}
|
|
2576
|
+
/** Delete a staging directory, never letting a failed reclaim escape. */
|
|
2577
|
+
function removeStaging(staging) {
|
|
2578
|
+
try {
|
|
2579
|
+
rmSync(staging, {
|
|
2580
|
+
recursive: true,
|
|
2581
|
+
force: true
|
|
2582
|
+
});
|
|
2583
|
+
} catch {}
|
|
2584
|
+
}
|
|
2585
|
+
/**
|
|
2586
|
+
* Whether one directory is a complete copy of the payload the key names.
|
|
2587
|
+
*
|
|
2588
|
+
* The copy's own manifest must reproduce the expected key before any byte is
|
|
2589
|
+
* executed, so a directory whose manifest was rewritten alongside its files is
|
|
2590
|
+
* a miss rather than a match. This is a BYTE check only: it says nothing about
|
|
2591
|
+
* the security descriptor, which is why callers also run {@link cacheSafe}.
|
|
2592
|
+
* @param root - the candidate payload root.
|
|
2593
|
+
* @param key - the key the shipped manifest produced.
|
|
2594
|
+
* @returns whether that directory's bytes can be spawned from.
|
|
2595
|
+
*/
|
|
2596
|
+
function cacheReady(root, key) {
|
|
2597
|
+
try {
|
|
2598
|
+
if (payloadKey(parseChecksums(readFileSync(join(root, "checksums.sha256"), "utf8"))) !== key) return false;
|
|
2599
|
+
if (!statSync(bashIn(root)).isFile()) return false;
|
|
2600
|
+
const report = verifyPayloadRoot(root);
|
|
2601
|
+
return report.verified > 0 && report.mismatches.length === 0;
|
|
2602
|
+
} catch {
|
|
2603
|
+
return false;
|
|
2604
|
+
}
|
|
2605
|
+
}
|
|
2606
|
+
/**
|
|
2607
|
+
* Whether a published tree's mandatory integrity label is safe to spawn from.
|
|
2608
|
+
*
|
|
2609
|
+
* Both the directory and the executable itself are inspected: the Windows
|
|
2610
|
+
* sandbox applies an inheritable Low ACE to the directory, and the image then
|
|
2611
|
+
* carries an inherited Low ACE. Before a directory exists, the closest existing
|
|
2612
|
+
* ancestor is what decides the label a newly created file inherits.
|
|
2613
|
+
*
|
|
2614
|
+
* `unknown` is not treated as safe. A descriptor we cannot read cannot be
|
|
2615
|
+
* claimed as verified, so the caller moves on to another candidate root rather
|
|
2616
|
+
* than silently running from an unverified image.
|
|
2617
|
+
* @param root - the candidate payload root.
|
|
2618
|
+
* @param readLabel - the label reader (injectable seam).
|
|
2619
|
+
* @returns the label verdict for that tree.
|
|
2620
|
+
*/
|
|
2621
|
+
function cacheSafe(root, readLabel) {
|
|
2622
|
+
const verdicts = existingAncestors(root).map((target) => readLabel(target));
|
|
2623
|
+
const bash = bashIn(root);
|
|
2624
|
+
if (existsSync(bash)) verdicts.push(readLabel(bash));
|
|
2625
|
+
if (verdicts.includes("low-or-untrusted")) return "low-or-untrusted";
|
|
2626
|
+
if (verdicts.includes("unknown")) return "unknown";
|
|
2627
|
+
return "medium-or-higher";
|
|
2628
|
+
}
|
|
2629
|
+
/**
|
|
2630
|
+
* Publish one payload copy under its key.
|
|
2631
|
+
*
|
|
2632
|
+
* The copy is built in a staging directory beside the target and renamed into
|
|
2633
|
+
* place, so the published name only ever refers to a complete tree. A rename
|
|
2634
|
+
* that loses a race against another host publishing the same key is not a
|
|
2635
|
+
* failure: both hosts copy the same bytes by construction.
|
|
2636
|
+
* @param source - the packaged payload root to copy.
|
|
2637
|
+
* @param target - the cache directory the key names.
|
|
2638
|
+
* @param key - the payload key, carried into the staging name.
|
|
2639
|
+
* @param copy - the copy itself.
|
|
2640
|
+
*/
|
|
2641
|
+
function publishPayload(source, target, key, copy) {
|
|
2642
|
+
mkdirSync(dirname(target), { recursive: true });
|
|
2643
|
+
if (existsSync(target)) rmSync(target, {
|
|
2644
|
+
recursive: true,
|
|
2645
|
+
force: true
|
|
2646
|
+
});
|
|
2647
|
+
const staging = join(dirname(target), CACHE_STAGING_PREFIX + key + "-" + process.pid.toString() + "-" + randomUUID().slice(0, 8));
|
|
2648
|
+
try {
|
|
2649
|
+
copy(source, staging);
|
|
2650
|
+
renameSync(staging, target);
|
|
2651
|
+
} catch (error) {
|
|
2652
|
+
removeStaging(staging);
|
|
2653
|
+
if (cacheReady(target, key)) return;
|
|
2654
|
+
throw error;
|
|
2655
|
+
}
|
|
2656
|
+
}
|
|
2657
|
+
/**
|
|
2658
|
+
* Resolve the shell one host runs, publishing the payload outside the package.
|
|
2659
|
+
*
|
|
2660
|
+
* Two independent proofs gate every tree that can be spawned: the shipped
|
|
2661
|
+
* SHA256 manifest proves its BYTES, and a mandatory-integrity-label check
|
|
2662
|
+
* proves its SECURITY DESCRIPTOR cannot downgrade a Medium runner to a Low
|
|
2663
|
+
* process. A label check runs before a published copy is reused, before a new
|
|
2664
|
+
* copy is published, and again after publication (the decisive check is on the
|
|
2665
|
+
* bash.exe that will actually be spawned). A tree carrying Low or Untrusted is
|
|
2666
|
+
* never reused and never spawned from, so a hash match alone can no longer keep
|
|
2667
|
+
* a broken cache alive across restarts and updates.
|
|
2668
|
+
*
|
|
2669
|
+
* Candidate roots are tried in order. The DSH home cache keeps the #1996 fix;
|
|
2670
|
+
* when its data tree is labelled Low, the payload is published into an isolated
|
|
2671
|
+
* per-user application-data root instead, still OUTSIDE the package. An old Low
|
|
2672
|
+
* cache under the DSH home is therefore migrated by abandonment, not by a
|
|
2673
|
+
* one-off repair of one payload-key directory.
|
|
2674
|
+
*
|
|
2675
|
+
* When no candidate can be written or verified, the verified packaged tree runs
|
|
2676
|
+
* instead and the caller is told, through {@link PrepareRuntimeOptions.warn},
|
|
2677
|
+
* that updates will need this host stopped. But if a Low label was observed and
|
|
2678
|
+
* even the packaged image is not proven clean, the mount is REFUSED with
|
|
2679
|
+
* `unsafe-integrity` rather than silently executing a shell that cannot write
|
|
2680
|
+
* the session's workspace. A payload that fails its manifest is always a
|
|
2681
|
+
* refusal, cache or no cache.
|
|
2682
|
+
* @param options - host facts, roots, and the seams tests drive.
|
|
2683
|
+
* @returns the shell paths to run, or the rejection reason.
|
|
2684
|
+
*/
|
|
2685
|
+
function prepareRuntime(options = {}) {
|
|
2686
|
+
const host = options.host ?? {
|
|
2687
|
+
platform: process.platform,
|
|
2688
|
+
arch: process.arch
|
|
2167
2689
|
};
|
|
2690
|
+
const refused = rejectHost(host);
|
|
2691
|
+
if (refused !== void 0) return refused;
|
|
2692
|
+
const packageRoot = options.packageRoot ?? PACKAGE_ROOT;
|
|
2693
|
+
const source = runtimeRoot(packageRoot);
|
|
2694
|
+
const warn = options.warn ?? (() => {});
|
|
2695
|
+
const rawLabel = options.readLabel ?? readMandatoryLabel;
|
|
2696
|
+
const labels = /* @__PURE__ */ new Map();
|
|
2697
|
+
const readLabel = (target) => {
|
|
2698
|
+
const hit = labels.get(target);
|
|
2699
|
+
if (hit !== void 0) return hit;
|
|
2700
|
+
const verdict = rawLabel(target);
|
|
2701
|
+
labels.set(target, verdict);
|
|
2702
|
+
return verdict;
|
|
2703
|
+
};
|
|
2704
|
+
const copy = options.copy ?? copyTree;
|
|
2705
|
+
const roots = [options.cacheRoot ?? cacheRoot(options.env)];
|
|
2706
|
+
const isolated = options.isolatedCacheRoot ?? (options.cacheRoot === void 0 ? isolatedCacheRoot(options.env) : void 0);
|
|
2707
|
+
if (isolated !== void 0 && !roots.includes(isolated)) roots.push(isolated);
|
|
2708
|
+
let key;
|
|
2709
|
+
try {
|
|
2710
|
+
key = payloadKey(parseChecksums(readFileSync(join(source, CHECKSUM_FILE), "utf8")));
|
|
2711
|
+
} catch (error) {
|
|
2712
|
+
return {
|
|
2713
|
+
code: "manifest-unreadable",
|
|
2714
|
+
message: "the payload checksum manifest could not be read: " + describe(error)
|
|
2715
|
+
};
|
|
2716
|
+
}
|
|
2717
|
+
const diagnoses = [];
|
|
2718
|
+
let shipped;
|
|
2719
|
+
const verifiedPackage = () => {
|
|
2720
|
+
shipped ??= resolveRuntime(host, packageRoot);
|
|
2721
|
+
return shipped;
|
|
2722
|
+
};
|
|
2723
|
+
for (const cacheDir of roots) {
|
|
2724
|
+
const published = join(cacheDir, key);
|
|
2725
|
+
const before = cacheSafe(published, readLabel);
|
|
2726
|
+
if (before === "low-or-untrusted") {
|
|
2727
|
+
diagnoses.push({
|
|
2728
|
+
low: true,
|
|
2729
|
+
message: "the cache root " + cacheDir + " carries a Low or Untrusted mandatory integrity label"
|
|
2730
|
+
});
|
|
2731
|
+
continue;
|
|
2732
|
+
}
|
|
2733
|
+
if (before === "unknown") {
|
|
2734
|
+
diagnoses.push({
|
|
2735
|
+
low: false,
|
|
2736
|
+
message: "the mandatory integrity label of " + cacheDir + " could not be read"
|
|
2737
|
+
});
|
|
2738
|
+
continue;
|
|
2739
|
+
}
|
|
2740
|
+
if (cacheReady(published, key)) return readyFrom(published, "cache");
|
|
2741
|
+
const packaged = verifiedPackage();
|
|
2742
|
+
if ("code" in packaged) return packaged;
|
|
2743
|
+
try {
|
|
2744
|
+
publishPayload(source, published, key, copy);
|
|
2745
|
+
} catch (error) {
|
|
2746
|
+
diagnoses.push({
|
|
2747
|
+
low: false,
|
|
2748
|
+
message: "the verified payload could not be published to " + published + " (" + describe(error) + ")"
|
|
2749
|
+
});
|
|
2750
|
+
continue;
|
|
2751
|
+
}
|
|
2752
|
+
if (!cacheReady(published, key)) {
|
|
2753
|
+
diagnoses.push({
|
|
2754
|
+
low: false,
|
|
2755
|
+
message: "the payload copy at " + published + " did not verify against the shipped manifest"
|
|
2756
|
+
});
|
|
2757
|
+
continue;
|
|
2758
|
+
}
|
|
2759
|
+
const after = cacheSafe(published, readLabel);
|
|
2760
|
+
if (after !== "medium-or-higher") {
|
|
2761
|
+
diagnoses.push({
|
|
2762
|
+
low: after === "low-or-untrusted",
|
|
2763
|
+
message: after === "low-or-untrusted" ? "the newly published bash.exe at " + bashIn(published) + " inherited a Low or Untrusted mandatory integrity label" : "the mandatory integrity label of the newly published bash.exe at " + bashIn(published) + " could not be read"
|
|
2764
|
+
});
|
|
2765
|
+
continue;
|
|
2766
|
+
}
|
|
2767
|
+
return readyFrom(published, "cache");
|
|
2768
|
+
}
|
|
2769
|
+
const packaged = verifiedPackage();
|
|
2770
|
+
if ("code" in packaged) return packaged;
|
|
2771
|
+
const reasons = diagnoses.map((entry) => entry.message).join("; ");
|
|
2772
|
+
const sawLow = diagnoses.some((entry) => entry.low);
|
|
2773
|
+
const packagedLabel = readLabel(bashPath(packageRoot));
|
|
2774
|
+
if (sawLow && packagedLabel === "low-or-untrusted") return {
|
|
2775
|
+
code: "unsafe-integrity",
|
|
2776
|
+
message: "every runtime cache root is unsafe to spawn from and the packaged payload is not proven clean: " + reasons + ". A Low-labelled bash.exe would start a Low-integrity process on this Medium host, which cannot write an ordinary workspace or temp directory, so win_bash refuses to mount instead of silently running a broken shell. Use the host pwsh tool, or point DSH_WIN_BASH_RUNTIME_HOME at a directory you have verified has no Low Mandatory Level label. Do not lower the workspace integrity level or change the global sandbox policy to work around this."
|
|
2777
|
+
};
|
|
2778
|
+
warn(reasons + ", so this host runs the shell from " + source + "; while it runs, updating or uninstalling this package can fail with ERR_PNPM_EPERM");
|
|
2779
|
+
return packaged;
|
|
2168
2780
|
}
|
|
2169
2781
|
//#endregion
|
|
2170
2782
|
//#region src/background.ts
|
|
@@ -2264,7 +2876,7 @@ function startBackgroundJob(command) {
|
|
|
2264
2876
|
terminated = true;
|
|
2265
2877
|
process?.terminate();
|
|
2266
2878
|
},
|
|
2267
|
-
done: spawned === void 0 ? Promise.resolve(processOutcome(void 0, failedBeforeStart, false)) : spawned.done.then(async (outcome) => {
|
|
2879
|
+
done: (spawned === void 0 ? Promise.resolve(processOutcome(void 0, failedBeforeStart, false)) : spawned.done.then(async (outcome) => {
|
|
2268
2880
|
try {
|
|
2269
2881
|
await spawned.waitForExit();
|
|
2270
2882
|
} catch {}
|
|
@@ -2272,6 +2884,9 @@ function startBackgroundJob(command) {
|
|
|
2272
2884
|
}, (error) => {
|
|
2273
2885
|
failedBeforeStart = messageOf(error);
|
|
2274
2886
|
return processOutcome(void 0, failedBeforeStart, false);
|
|
2887
|
+
})).then(async (outcome) => {
|
|
2888
|
+
if (command.temp !== void 0) await removeShellTempDir(command.temp);
|
|
2889
|
+
return outcome;
|
|
2275
2890
|
})
|
|
2276
2891
|
};
|
|
2277
2892
|
}
|
|
@@ -2579,6 +3194,7 @@ function buildWinBashTool(ctx, config, runtime, shells) {
|
|
|
2579
3194
|
if (!config.enableRunInBackground) throw new Error("win_bash: run_in_background is disabled for this deployment (enableRunInBackground: false), so this call was not started.");
|
|
2580
3195
|
const registry = readJobRegistry(ctx);
|
|
2581
3196
|
if (registry === void 0) throw new Error("BACKGROUND_UNAVAILABLE: this deployment composes no background job registry (ctx.jobs), so win_bash cannot register a background command. This call was not run in the foreground instead. Run it without run_in_background, or start it in the background with the host pwsh tool, which is the native shell for this platform and stays available whenever win_bash cannot serve a call.");
|
|
3197
|
+
const temp = shellTempDir();
|
|
2582
3198
|
return {
|
|
2583
3199
|
stdout: "",
|
|
2584
3200
|
stderr: "",
|
|
@@ -2590,7 +3206,8 @@ function buildWinBashTool(ctx, config, runtime, shells) {
|
|
|
2590
3206
|
script: command,
|
|
2591
3207
|
argv: shellArgv(runtime),
|
|
2592
3208
|
cwd,
|
|
2593
|
-
env: shellEnvironment(runtime,
|
|
3209
|
+
env: shellEnvironment(runtime, temp),
|
|
3210
|
+
temp,
|
|
2594
3211
|
label: args.command,
|
|
2595
3212
|
stdoutBytes: BACKGROUND_STDOUT_BYTES,
|
|
2596
3213
|
stderrBytes: BACKGROUND_STDERR_BYTES
|
|
@@ -2733,9 +3350,14 @@ async function mountWithGate(ctx, config, runtime, options = {}) {
|
|
|
2733
3350
|
* @param config - the row's config, validated by {@link Config}.
|
|
2734
3351
|
*/
|
|
2735
3352
|
function apply(ctx, config) {
|
|
2736
|
-
const runtime =
|
|
2737
|
-
|
|
2738
|
-
|
|
3353
|
+
const runtime = prepareRuntime({
|
|
3354
|
+
host: {
|
|
3355
|
+
platform: process.platform,
|
|
3356
|
+
arch: process.arch
|
|
3357
|
+
},
|
|
3358
|
+
warn: (message) => {
|
|
3359
|
+
ctx.logger?.warn?.("dsh-win-bash: " + message);
|
|
3360
|
+
}
|
|
2739
3361
|
});
|
|
2740
3362
|
if ("code" in runtime) {
|
|
2741
3363
|
ctx.logger?.warn?.("dsh-win-bash: win_bash was not mounted (" + runtime.code + "): " + runtime.message);
|
|
@@ -2748,9 +3370,15 @@ function apply(ctx, config) {
|
|
|
2748
3370
|
ctx.logger?.error?.("dsh-win-bash: " + (error instanceof Error ? error.message : String(error)));
|
|
2749
3371
|
throw error;
|
|
2750
3372
|
}
|
|
3373
|
+
sweepOrphanShellTempDirs().then((sweep) => {
|
|
3374
|
+
if (sweep.removed.length > 0) ctx.logger?.info?.("dsh-win-bash: reclaimed " + sweep.removed.length.toString() + " orphaned shell temp directories left by earlier runs");
|
|
3375
|
+
if (sweep.failed.length > 0) ctx.logger?.warn?.("dsh-win-bash: " + sweep.failed.length.toString() + " orphaned shell temp directories could not be reclaimed and will be retried on the next start: " + sweep.failed.join(", "));
|
|
3376
|
+
}, (error) => {
|
|
3377
|
+
ctx.logger?.warn?.("dsh-win-bash: the orphaned shell temp directory sweep failed: " + (error instanceof Error ? error.message : String(error)));
|
|
3378
|
+
});
|
|
2751
3379
|
mountWithGate(ctx, resolved, runtime).catch((error) => {
|
|
2752
3380
|
ctx.logger?.error?.("dsh-win-bash: the mount continuation failed: " + (error instanceof Error ? error.message : String(error)));
|
|
2753
3381
|
});
|
|
2754
3382
|
}
|
|
2755
3383
|
//#endregion
|
|
2756
|
-
export { BACKGROUND_JOB_KIND, CONFIG_DEFAULTS, Config, DEFAULT_DESCRIPTION, DEFAULT_ENABLE_RUN_IN_BACKGROUND, DEFAULT_REPLACE_PWSH, DEFAULT_SYNC_WITH_LIANGSHEN, GATE_DECLARATION_WAIT_MS, GATE_POLL_INTERVAL_MS, GATE_READ_TIMEOUT_MS, GATE_SETTLE_TIMEOUT_MS, HOST_PWSH_TOOL, LIANGSHEN_PRESET_ID, SERVING_MODE, ShellRegistry, ShellUnavailableError, WIN_BASH_TOOL, apply, bashPath, buildFrame, declaresLiangShen, gateRefusalMessage, inject, installPwshReplacement, markerFor, mountWithGate, name, parseChecksums, parseFrame, readJobRegistry, readPresetRegistry, registerWinBash, renderResult, resolveRuntime, runtimeRoot, settleHostTree, shPath, shellArgv, shellEnvironment, shellTempDir, startBackgroundJob, startLiangShenGate, stripTerminalEscapes, truncationNotice, validateConfig, verifyPayload };
|
|
3384
|
+
export { BACKGROUND_JOB_KIND, CACHE_DIR_PARTS, CACHE_STAGING_PREFIX, CONFIG_DEFAULTS, Config, DEFAULT_DESCRIPTION, DEFAULT_ENABLE_RUN_IN_BACKGROUND, DEFAULT_REPLACE_PWSH, DEFAULT_SYNC_WITH_LIANGSHEN, GATE_DECLARATION_WAIT_MS, GATE_POLL_INTERVAL_MS, GATE_READ_TIMEOUT_MS, GATE_SETTLE_TIMEOUT_MS, HOST_PWSH_TOOL, ISOLATED_CACHE_DIR_PARTS, LIANGSHEN_PRESET_ID, RUNTIME_ROOT_ENV, SERVING_MODE, SHELL_TEMP_PREFIX, ShellRegistry, ShellUnavailableError, WIN_BASH_TOOL, apply, bashIn, bashPath, binIn, buildFrame, cacheRoot, declaresLiangShen, gateRefusalMessage, inject, installPwshReplacement, isolatedCacheRoot, markerFor, mountWithGate, name, parseChecksums, parseFrame, payloadKey, prepareRuntime, readJobRegistry, readPresetRegistry, registerWinBash, removeShellTempDir, renderResult, resolveRuntime, runtimeRoot, settleHostTree, shIn, shPath, shellArgv, shellEnvironment, shellTempDir, startBackgroundJob, startLiangShenGate, stripTerminalEscapes, sweepOrphanShellTempDirs, truncationNotice, validateConfig, verifyPayload, verifyPayloadRoot };
|