@xmoon76/dsh-pi-tui 0.5.7 → 0.5.9
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.en.md +64 -26
- package/README.md +64 -26
- package/cordis.patch.yml +13 -0
- package/dist/acp-stdio.mjs +151 -0
- package/dist/{bootstrap-C9nl6e13.mjs → bootstrap-CVyHGQDX.mjs} +22330 -6888
- package/dist/browser-CDUSHIIr.mjs +76 -0
- package/dist/builtins.mjs +2 -2
- package/dist/{client-bootstrap-CGezi85O.mjs → client-bootstrap-4UoeVIHE.mjs} +2407 -1830
- package/dist/client-runtime-BHBlVUf5.mjs +1376 -0
- package/dist/{daemon-launcher-H_1r5Cqs.mjs → daemon-launcher-DxIgjBHW.mjs} +1551 -57
- package/dist/daemon-launcher.d.mts +85 -14
- package/dist/daemon-launcher.mjs +2 -2
- package/dist/diagnostics-Db5iVcuJ.mjs +123 -0
- package/dist/{entry-control-jBfKn1qO.mjs → entry-control-BDLZgg4m.mjs} +1 -1
- package/dist/{extensions-6a5vvJUT.mjs → extensions-Dx7ncPNA.mjs} +1 -4
- package/dist/extensions.mjs +1 -1
- package/dist/handoff-BjoEy3f8.mjs +106 -0
- package/dist/history-search-limits-Cx2UvRC2.mjs +16 -0
- package/dist/history-search-remote-BHuoellG.mjs +113 -0
- package/dist/history-search-remote-BzXdvrxk.mjs +26 -0
- package/dist/history-search-seat-BcHJTpeB.mjs +639 -0
- package/dist/history-search-seat-rZcq0XV6.mjs +639 -0
- package/dist/{host-bootstrap-MVxks641.mjs → host-bootstrap-BfnufpnW.mjs} +37 -72
- package/dist/{host-foundation-probe-DK7n5jDu.mjs → host-foundation-probe-DlD8-N_Q.mjs} +2 -2
- package/dist/host-runtime.mjs +5611 -290
- package/dist/index.d.mts +13 -6
- package/dist/index.mjs +22766 -6893
- package/dist/installed-dsh-BjUiJSwR.mjs +142 -0
- package/dist/interaction-port-D1Yv9zgu.mjs +45 -0
- package/dist/local-acp-control-CC5z4Xbq.mjs +304 -0
- package/dist/local-acp-endpoint-D1jYdJdv.mjs +41 -0
- package/dist/local-acp-listener-Cmgb5M0H.mjs +9493 -0
- package/dist/local-acp-status-B_25Q6kU.mjs +101 -0
- package/dist/local-client.mjs +719 -93
- package/dist/local-daemon-control-BFJAC8Kg.mjs +2199 -0
- package/dist/local-daemon-control-Bm3imctD.mjs +2101 -0
- package/dist/{local-discovery-BPZw1wEd.mjs → local-discovery-BMhaGIiJ.mjs} +254 -544
- package/dist/{local-discovery-BMoNXZjJ.mjs → local-discovery-Ba7_-NNC.mjs} +1 -1
- package/dist/{local-discovery-Bq3jLAn1.mjs → local-discovery-QSbFpjK2.mjs} +301 -544
- package/dist/local-host.mjs +970 -501
- package/dist/local-profile-daemon-admin-otpm69R6.mjs +469 -0
- package/dist/{local-scope-DEuGAy8l.mjs → local-scope-5DEKA_lW.mjs} +1075 -31
- package/dist/{local-transport-X4ZCe2uE.mjs → local-transport-DeWmdZ5E.mjs} +14 -3
- package/dist/{local-web-control-DKWtXIkr.mjs → local-web-control-Dny93N-r.mjs} +10 -7
- package/dist/{local-workspace-object-D06GL0UO.mjs → local-workspace-object-D0RVQt8U.mjs} +33 -9
- package/dist/model-selection-CYlqg2DV.mjs +83 -0
- package/dist/native-client-domains-Dg4srzYt.mjs +2 -0
- package/dist/native-client.d.mts +735 -0
- package/dist/native-client.mjs +3534 -0
- package/dist/{open-CmOBxSjN.mjs → open-QQva8-ar.mjs} +24 -20
- package/dist/{pi-tui-remote-contribution-Do8ECrUy.mjs → pi-tui-remote-contribution-565wiLWp.mjs} +741 -150
- package/dist/preferences.mjs +1 -1
- package/dist/{profile-preferences-migration-CThz4rHH.mjs → profile-preferences-migration-CNOWIdnr.mjs} +1 -1
- package/dist/{profile-preferences-migration-BfUGdPZx.mjs → profile-preferences-migration-IrnXcJWK.mjs} +10 -8
- package/dist/{prompt-sections-DXmjB58H.mjs → prompt-sections-Bi-9_Gr_.mjs} +28 -10
- package/dist/{prompt-sections-DUKmWb8g.mjs → prompt-sections-DFS1dqsY.mjs} +342 -56
- package/dist/{dist-BFgC-5tL.mjs → recall-scope-9Nuoto_i.mjs} +353 -1
- package/dist/rolldown-runtime-8H4AJuhK.mjs +14 -0
- package/dist/schema-CCLvDa7Q.mjs +177 -0
- package/dist/{schema-qE_P09zJ.mjs → schema-t97l3aK8.mjs} +56 -3
- package/dist/{session-DC0XQr5O.mjs → session-Cs8iLCEx.mjs} +784 -342
- package/dist/{session-Cihe8PPS.mjs → session-EBRZF6Ow.mjs} +783 -341
- package/dist/session-create-facts-CkfG5DpQ.mjs +20 -0
- package/dist/{startup-CarQxWwi.d.mts → startup-Dti3Udne.d.mts} +30 -3
- package/dist/{startup-CARqn3kV.mjs → startup-UtFQSDe_.mjs} +90 -15
- package/dist/startup.d.mts +2 -2
- package/dist/startup.mjs +1 -1
- package/dist/task-read-remote-DRtkXDrZ.mjs +1083 -0
- package/dist/{terminal-progress-source-D4DHyXDo.mjs → terminal-progress-source-VsczdXA-.mjs} +610 -335
- package/dist/{transcript-view-a7cROAaI.mjs → transcript-view-B6vCnukn.mjs} +2284 -1166
- package/dist/{transcript-view-hvNVkPtv.mjs → transcript-view-DSSdE2vK.mjs} +2191 -765
- package/dist/write-failure-xNKczXH7.mjs +189 -0
- package/package.json +25 -8
- package/dist/local-acp-listener-CNkguN5-.mjs +0 -2566
- package/dist/local-daemon-control-Bg9s2aYi.mjs +0 -1424
- package/dist/local-daemon-control-Cu_4FIdd.mjs +0 -1585
- package/dist/local-profile-daemon-admin-AXgVH-6y.mjs +0 -91
- package/dist/preset-BcWqOtY7.mjs +0 -46
|
@@ -7,6 +7,960 @@ import { fileURLToPath } from "node:url";
|
|
|
7
7
|
import { createServer } from "node:net";
|
|
8
8
|
import { userInfo } from "node:os";
|
|
9
9
|
import { Buffer as Buffer$1 } from "node:buffer";
|
|
10
|
+
//#region src/runtime/native-process/installed-dsh.ts
|
|
11
|
+
/**
|
|
12
|
+
* The installed `@deepseek-ai/dsh` anchor and module-closure primitives shared
|
|
13
|
+
* by the platform-native loaders.
|
|
14
|
+
*
|
|
15
|
+
* A real Direct launcher entry (`process.argv[1]`) identifies its own installed
|
|
16
|
+
* DSH package and always wins over an inherited environment anchor: a shell or
|
|
17
|
+
* tool child of a Split Host inherits SPLIT_PROCESS_DSH_ANCHOR, but a Direct
|
|
18
|
+
* `dsh` CLI started from it must use the installation executing that CLI. A
|
|
19
|
+
* caller that already holds the official profile's `installAnchor` may supply
|
|
20
|
+
* it explicitly (the Direct composition does); only a Split Host/Client entry,
|
|
21
|
+
* whose argv[1] is the product's own local-host/local-client script, falls back
|
|
22
|
+
* to the launcher-verified SPLIT_PROCESS_DSH_ANCHOR. No NODE_PATH, profile
|
|
23
|
+
* node_modules or source tree is consulted.
|
|
24
|
+
*
|
|
25
|
+
* Module lookup must prove the resolved dependency belongs to its owner's
|
|
26
|
+
* installation closure: npm keeps sibling dependencies in the owner's nearest
|
|
27
|
+
* `node_modules`; pnpm keeps every package in a shared virtual store (`.mise`
|
|
28
|
+
* in the mise install and `.pnpm` in a project install). Any other ancestor
|
|
29
|
+
* `node_modules` is an accidental hoist and must not supply a dependency.
|
|
30
|
+
*
|
|
31
|
+
* @module runtime/native-process/installed-dsh
|
|
32
|
+
*/
|
|
33
|
+
/** One installed `@deepseek-ai/dsh` manifest, or undefined when the path is
|
|
34
|
+
* unreadable, malformed, or belongs to another package. The name check is
|
|
35
|
+
* load-bearing: `createRequire` would otherwise walk into an unrelated ancestor
|
|
36
|
+
* `node_modules` and silently borrow a dependency that does not belong to the
|
|
37
|
+
* running DSH installation. */
|
|
38
|
+
function installedDshManifest(path) {
|
|
39
|
+
try {
|
|
40
|
+
const real = realpathSync(path);
|
|
41
|
+
const manifest = JSON.parse(readFileSync(real, "utf8"));
|
|
42
|
+
if (manifest === null || typeof manifest !== "object" || Array.isArray(manifest)) return void 0;
|
|
43
|
+
const record = manifest;
|
|
44
|
+
return record.name === "@deepseek-ai/dsh" && typeof record.version === "string" && record.version !== "" ? real : void 0;
|
|
45
|
+
} catch {
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* The installed `@deepseek-ai/dsh/package.json` this process runs on.
|
|
51
|
+
* @param explicitAnchor - an already-resolved official profile install anchor;
|
|
52
|
+
* when provided and valid it outranks the launcher and environment anchors.
|
|
53
|
+
*/
|
|
54
|
+
function installedDshAnchor(explicitAnchor) {
|
|
55
|
+
if (explicitAnchor !== void 0 && explicitAnchor !== "") {
|
|
56
|
+
const provided = installedDshManifest(explicitAnchor);
|
|
57
|
+
if (provided !== void 0) return provided;
|
|
58
|
+
}
|
|
59
|
+
const entry = process.argv[1];
|
|
60
|
+
if (entry !== void 0) try {
|
|
61
|
+
let dir = dirname(realpathSync(entry));
|
|
62
|
+
for (let depth = 0; depth < 8; depth += 1) {
|
|
63
|
+
const anchor = installedDshManifest(join(dir, "package.json"));
|
|
64
|
+
if (anchor !== void 0) return anchor;
|
|
65
|
+
const parent = dirname(dir);
|
|
66
|
+
if (parent === dir) break;
|
|
67
|
+
dir = parent;
|
|
68
|
+
}
|
|
69
|
+
} catch {}
|
|
70
|
+
const provided = process.env.SPLIT_PROCESS_DSH_ANCHOR;
|
|
71
|
+
return provided !== void 0 && provided !== "" ? installedDshManifest(provided) : void 0;
|
|
72
|
+
}
|
|
73
|
+
/** The nearest `node_modules` directory above a package directory. */
|
|
74
|
+
function nearestModuleRoot(packageDir) {
|
|
75
|
+
for (let dir = packageDir;; dir = dirname(dir)) {
|
|
76
|
+
if (basename(dir) === "node_modules") return dir;
|
|
77
|
+
const parent = dirname(dir);
|
|
78
|
+
if (parent === dir) return void 0;
|
|
79
|
+
dir = parent;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
/** Whether `path` is `root` or below it. */
|
|
83
|
+
function isInside(path, root) {
|
|
84
|
+
return path === root || path.startsWith(`${root}${sep}`);
|
|
85
|
+
}
|
|
86
|
+
/** Prove a resolved dependency belongs to its owner's installation closure. */
|
|
87
|
+
function assertInstallClosure(ownerDir, dependencyDir, label) {
|
|
88
|
+
const nearest = nearestModuleRoot(ownerDir);
|
|
89
|
+
if (nearest !== void 0 && isInside(dependencyDir, nearest)) return;
|
|
90
|
+
for (let dir = ownerDir;; dir = dirname(dir)) {
|
|
91
|
+
if ((basename(dir) === ".mise" || basename(dir) === ".pnpm") && isInside(dependencyDir, dir)) return;
|
|
92
|
+
const parent = dirname(dir);
|
|
93
|
+
if (parent === dir) break;
|
|
94
|
+
dir = parent;
|
|
95
|
+
}
|
|
96
|
+
throw new Error(`installed DSH module: ${label} resolved outside the installed DSH module tree`);
|
|
97
|
+
}
|
|
98
|
+
/** Read one JSON package manifest as an object, or undefined when unreadable. */
|
|
99
|
+
function readManifestObject(path) {
|
|
100
|
+
try {
|
|
101
|
+
const manifest = JSON.parse(readFileSync(realpathSync(path), "utf8"));
|
|
102
|
+
if (manifest === null || typeof manifest !== "object" || Array.isArray(manifest)) return void 0;
|
|
103
|
+
return manifest;
|
|
104
|
+
} catch {
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
/** Require one dependency declaration from an owner manifest. */
|
|
109
|
+
function assertDeclaredDependency(manifest, name, label) {
|
|
110
|
+
const dependencies = manifest.dependencies;
|
|
111
|
+
const declared = dependencies !== null && typeof dependencies === "object" && !Array.isArray(dependencies) ? dependencies[name] : void 0;
|
|
112
|
+
if (typeof declared !== "string" || declared === "") throw new Error(`installed DSH module: ${label} does not declare ${name}`);
|
|
113
|
+
}
|
|
114
|
+
/** Require one peer declaration from an owner manifest. */
|
|
115
|
+
function assertDeclaredPeerDependency(manifest, name, label) {
|
|
116
|
+
const peerDependencies = manifest.peerDependencies;
|
|
117
|
+
const declared = peerDependencies !== null && typeof peerDependencies === "object" && !Array.isArray(peerDependencies) ? peerDependencies[name] : void 0;
|
|
118
|
+
if (typeof declared !== "string" || declared === "") throw new Error(`installed DSH module: ${label} does not declare ${name} as a peer`);
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Resolve the Koffi entry owned by the running DSH installation:
|
|
122
|
+
* DSH anchor -> `@deepseek-ai/dsh-subprocess-local` manifest -> that package's
|
|
123
|
+
* own `koffi` dependency. The TUI never resolves Koffi from its own package, a
|
|
124
|
+
* profile, or an ambient module path.
|
|
125
|
+
*
|
|
126
|
+
* The owner's declared dependency selects the implementation; this code does
|
|
127
|
+
* not pin or compare a Koffi version, because the running DSH installation owns
|
|
128
|
+
* that choice and an upgrade must not be rejected only for a different version
|
|
129
|
+
* number. The ABI/struct-size checks in the platform bridge decide actual
|
|
130
|
+
* compatibility.
|
|
131
|
+
*/
|
|
132
|
+
function resolveDshKoffi(anchor) {
|
|
133
|
+
const manifest = installedDshManifest(anchor);
|
|
134
|
+
if (manifest === void 0) throw new Error("native process identity: the DSH anchor is not an installed @deepseek-ai/dsh manifest");
|
|
135
|
+
const dshDir = dirname(manifest);
|
|
136
|
+
const subprocessManifest = realpathSync(createRequire(manifest).resolve("@deepseek-ai/dsh-subprocess-local/package.json"));
|
|
137
|
+
const subprocessDir = dirname(subprocessManifest);
|
|
138
|
+
assertInstallClosure(dshDir, subprocessDir, "@deepseek-ai/dsh-subprocess-local");
|
|
139
|
+
const subprocess = readManifestObject(subprocessManifest);
|
|
140
|
+
if (subprocess?.name !== "@deepseek-ai/dsh-subprocess-local") throw new Error("native process identity: the resolved subprocess manifest is not @deepseek-ai/dsh-subprocess-local");
|
|
141
|
+
assertDeclaredDependency(subprocess, "koffi", "@deepseek-ai/dsh-subprocess-local");
|
|
142
|
+
const koffiEntry = realpathSync(createRequire(subprocessManifest).resolve("koffi"));
|
|
143
|
+
assertInstallClosure(subprocessDir, dirname(koffiEntry), "koffi");
|
|
144
|
+
if (readManifestObject(join(dirname(koffiEntry), "package.json"))?.name !== "koffi") throw new Error("native process identity: the resolved koffi is not the koffi package");
|
|
145
|
+
return koffiEntry;
|
|
146
|
+
}
|
|
147
|
+
//#endregion
|
|
148
|
+
//#region src/runtime/native-process/win32.ts
|
|
149
|
+
/**
|
|
150
|
+
* Win32 native process identity, kernel seats, per-instance stop events and
|
|
151
|
+
* private object-security primitives.
|
|
152
|
+
*
|
|
153
|
+
* This module is the ONLY owner of the Win32 FFI layout for these operations.
|
|
154
|
+
* It lazily loads Koffi from the running DSH installation (the verified anchor
|
|
155
|
+
* -> `@deepseek-ai/dsh-subprocess-local` -> that package's own declared
|
|
156
|
+
* `koffi`), decodes exact kernel facts and never decides product policy.
|
|
157
|
+
*
|
|
158
|
+
* The module is imported on every platform (it is part of the native-process
|
|
159
|
+
* layer), but no DLL is loaded until a Win32 function is actually called.
|
|
160
|
+
*
|
|
161
|
+
* @module runtime/native-process/win32
|
|
162
|
+
*/
|
|
163
|
+
const ERROR_INVALID_PARAMETER = 87;
|
|
164
|
+
const ERROR_ACCESS_DENIED = 5;
|
|
165
|
+
const ERROR_INVALID_HANDLE = 6;
|
|
166
|
+
const ERROR_ALREADY_EXISTS = 183;
|
|
167
|
+
const WAIT_OBJECT_0 = 0;
|
|
168
|
+
const WAIT_ABANDONED = 128;
|
|
169
|
+
const WAIT_TIMEOUT = 258;
|
|
170
|
+
const WAIT_FAILED = 4294967295;
|
|
171
|
+
const TOKEN_QUERY = 8;
|
|
172
|
+
const PIPE_UNLIMITED_INSTANCES = 255;
|
|
173
|
+
const NETWORK_SID = "S-1-5-2";
|
|
174
|
+
const LOCAL_SYSTEM_SID = "S-1-5-18";
|
|
175
|
+
const BUILTIN_ADMINISTRATORS_SID = "S-1-5-32-544";
|
|
176
|
+
const ACCESS_ALLOWED_ACE_TYPE = 0;
|
|
177
|
+
const ACCESS_DENIED_ACE_TYPE = 1;
|
|
178
|
+
const INVALID_HANDLE_VALUE = 18446744073709551615n;
|
|
179
|
+
const FILE_ATTRIBUTE_DIRECTORY = 16;
|
|
180
|
+
const FILE_ATTRIBUTE_REPARSE_POINT = 1024;
|
|
181
|
+
const FILE_SHARE_ALL = 7;
|
|
182
|
+
const OPEN_EXISTING = 3;
|
|
183
|
+
const DRIVE_FIXED = 3;
|
|
184
|
+
const SE_FILE_OBJECT = 1;
|
|
185
|
+
const SE_KERNEL_OBJECT = 6;
|
|
186
|
+
const OBJECT_INHERIT_ACE = 1;
|
|
187
|
+
const CONTAINER_INHERIT_ACE = 2;
|
|
188
|
+
const SE_DACL_PRESENT = 4;
|
|
189
|
+
const SDDL_REVISION_1 = 1;
|
|
190
|
+
let loaded$1;
|
|
191
|
+
function bind(context, library, name, result, args) {
|
|
192
|
+
return library.func("__stdcall", name, result, [...args]);
|
|
193
|
+
}
|
|
194
|
+
function loadBridge$1() {
|
|
195
|
+
if (loaded$1 !== void 0) return loaded$1;
|
|
196
|
+
let result;
|
|
197
|
+
try {
|
|
198
|
+
const anchor = installedDshAnchor();
|
|
199
|
+
if (anchor === void 0) throw new Error("native win32: no installed DSH anchor");
|
|
200
|
+
const koffi = createRequire(import.meta.url)(resolveDshKoffi(anchor));
|
|
201
|
+
const kernel32 = koffi.load("kernel32.dll");
|
|
202
|
+
const advapi32 = koffi.load("advapi32.dll");
|
|
203
|
+
const context = {
|
|
204
|
+
koffi,
|
|
205
|
+
kernel32,
|
|
206
|
+
advapi32
|
|
207
|
+
};
|
|
208
|
+
result = {
|
|
209
|
+
ok: true,
|
|
210
|
+
bridge: {
|
|
211
|
+
koffi,
|
|
212
|
+
closeHandle: bind(context, kernel32, "CloseHandle", "int", ["void *"]),
|
|
213
|
+
getLastError: bind(context, kernel32, "GetLastError", "uint32", []),
|
|
214
|
+
setLastError: bind(context, kernel32, "SetLastError", "void", ["uint32"]),
|
|
215
|
+
openProcess: bind(context, kernel32, "OpenProcess", "void *", [
|
|
216
|
+
"uint32",
|
|
217
|
+
"bool",
|
|
218
|
+
"uint32"
|
|
219
|
+
]),
|
|
220
|
+
getProcessTimes: bind(context, kernel32, "GetProcessTimes", "int", [
|
|
221
|
+
"void *",
|
|
222
|
+
"void *",
|
|
223
|
+
"void *",
|
|
224
|
+
"void *",
|
|
225
|
+
"void *"
|
|
226
|
+
]),
|
|
227
|
+
waitForSingleObject: bind(context, kernel32, "WaitForSingleObject", "uint32", ["void *", "uint32"]),
|
|
228
|
+
waitForMultipleObjects: bind(context, kernel32, "WaitForMultipleObjects", "uint32", [
|
|
229
|
+
"uint32",
|
|
230
|
+
"uintptr_t *",
|
|
231
|
+
"int",
|
|
232
|
+
"uint32"
|
|
233
|
+
]),
|
|
234
|
+
openProcessToken: bind(context, kernel32, "OpenProcessToken", "int", [
|
|
235
|
+
"void *",
|
|
236
|
+
"uint32",
|
|
237
|
+
"void *"
|
|
238
|
+
]),
|
|
239
|
+
getTokenInformation: bind(context, advapi32, "GetTokenInformation", "int", [
|
|
240
|
+
"void *",
|
|
241
|
+
"uint32",
|
|
242
|
+
"void *",
|
|
243
|
+
"uint32",
|
|
244
|
+
"void *"
|
|
245
|
+
]),
|
|
246
|
+
createMutexW: bind(context, kernel32, "CreateMutexW", "void *", [
|
|
247
|
+
"void *",
|
|
248
|
+
"int",
|
|
249
|
+
"const char16_t *"
|
|
250
|
+
]),
|
|
251
|
+
releaseMutex: bind(context, kernel32, "ReleaseMutex", "int", ["void *"]),
|
|
252
|
+
createEventW: bind(context, kernel32, "CreateEventW", "void *", [
|
|
253
|
+
"void *",
|
|
254
|
+
"int",
|
|
255
|
+
"int",
|
|
256
|
+
"const char16_t *"
|
|
257
|
+
]),
|
|
258
|
+
openEventW: bind(context, kernel32, "OpenEventW", "void *", [
|
|
259
|
+
"uint32",
|
|
260
|
+
"int",
|
|
261
|
+
"const char16_t *"
|
|
262
|
+
]),
|
|
263
|
+
setEvent: bind(context, kernel32, "SetEvent", "int", ["void *"]),
|
|
264
|
+
createNamedPipeW: bind(context, kernel32, "CreateNamedPipeW", "void *", [
|
|
265
|
+
"const char16_t *",
|
|
266
|
+
"uint32",
|
|
267
|
+
"uint32",
|
|
268
|
+
"uint32",
|
|
269
|
+
"uint32",
|
|
270
|
+
"uint32",
|
|
271
|
+
"uint32",
|
|
272
|
+
"void *"
|
|
273
|
+
]),
|
|
274
|
+
createDirectoryW: bind(context, kernel32, "CreateDirectoryW", "int", ["const char16_t *", "void *"]),
|
|
275
|
+
createFileW: bind(context, kernel32, "CreateFileW", "void *", [
|
|
276
|
+
"const char16_t *",
|
|
277
|
+
"uint32",
|
|
278
|
+
"uint32",
|
|
279
|
+
"void *",
|
|
280
|
+
"uint32",
|
|
281
|
+
"uint32",
|
|
282
|
+
"void *"
|
|
283
|
+
]),
|
|
284
|
+
getFileAttributesW: bind(context, kernel32, "GetFileAttributesW", "uint32", ["const char16_t *"]),
|
|
285
|
+
getDriveTypeW: bind(context, kernel32, "GetDriveTypeW", "uint32", ["const char16_t *"]),
|
|
286
|
+
getVolumeInformationW: bind(context, kernel32, "GetVolumeInformationW", "int", [
|
|
287
|
+
"const char16_t *",
|
|
288
|
+
"void *",
|
|
289
|
+
"uint32",
|
|
290
|
+
"void *",
|
|
291
|
+
"void *",
|
|
292
|
+
"void *",
|
|
293
|
+
"void *",
|
|
294
|
+
"uint32"
|
|
295
|
+
]),
|
|
296
|
+
getSystemDirectoryW: bind(context, kernel32, "GetSystemDirectoryW", "uint32", ["void *", "uint32"]),
|
|
297
|
+
convertStringSecurityDescriptor: bind(context, advapi32, "ConvertStringSecurityDescriptorToSecurityDescriptorW", "int", [
|
|
298
|
+
"const char16_t *",
|
|
299
|
+
"uint32",
|
|
300
|
+
"void *",
|
|
301
|
+
"void *"
|
|
302
|
+
]),
|
|
303
|
+
getSecurityDescriptorDacl: bind(context, advapi32, "GetSecurityDescriptorDacl", "int", [
|
|
304
|
+
"void *",
|
|
305
|
+
"void *",
|
|
306
|
+
"void *",
|
|
307
|
+
"void *"
|
|
308
|
+
]),
|
|
309
|
+
setSecurityInfo: bind(context, advapi32, "SetSecurityInfo", "uint32", [
|
|
310
|
+
"void *",
|
|
311
|
+
"int",
|
|
312
|
+
"uint32",
|
|
313
|
+
"void *",
|
|
314
|
+
"void *",
|
|
315
|
+
"void *",
|
|
316
|
+
"void *"
|
|
317
|
+
]),
|
|
318
|
+
getSecurityInfo: bind(context, advapi32, "GetSecurityInfo", "uint32", [
|
|
319
|
+
"void *",
|
|
320
|
+
"int",
|
|
321
|
+
"uint32",
|
|
322
|
+
"void *",
|
|
323
|
+
"void *",
|
|
324
|
+
"void *",
|
|
325
|
+
"void *",
|
|
326
|
+
"void *"
|
|
327
|
+
]),
|
|
328
|
+
getSecurityDescriptorLength: bind(context, advapi32, "GetSecurityDescriptorLength", "uint32", ["void *"]),
|
|
329
|
+
getSecurityDescriptorControl: bind(context, advapi32, "GetSecurityDescriptorControl", "int", [
|
|
330
|
+
"void *",
|
|
331
|
+
"void *",
|
|
332
|
+
"void *"
|
|
333
|
+
]),
|
|
334
|
+
localFree: bind(context, kernel32, "LocalFree", "void *", ["void *"])
|
|
335
|
+
}
|
|
336
|
+
};
|
|
337
|
+
} catch {
|
|
338
|
+
result = {
|
|
339
|
+
ok: false,
|
|
340
|
+
code: "NATIVE_UNAVAILABLE"
|
|
341
|
+
};
|
|
342
|
+
}
|
|
343
|
+
loaded$1 = result;
|
|
344
|
+
return result;
|
|
345
|
+
}
|
|
346
|
+
/** `GetLastError()` from the live bridge, or 0 when the native addon is unavailable. */
|
|
347
|
+
function lastError(bridge) {
|
|
348
|
+
return bridge.getLastError();
|
|
349
|
+
}
|
|
350
|
+
/** A wide (UTF-16RO) Koffi string is passed through the `const char16_t *` type. */
|
|
351
|
+
function parseSid(bytes, offset) {
|
|
352
|
+
const revision = bytes[offset];
|
|
353
|
+
const count = bytes[offset + 1];
|
|
354
|
+
let authority = 0n;
|
|
355
|
+
for (let i = 0; i < 6; i += 1) authority = authority << 8n | BigInt(bytes[offset + 2 + i]);
|
|
356
|
+
let value = `S-${revision}-${authority}`;
|
|
357
|
+
for (let i = 0; i < count; i += 1) value += `-${bytes.readUInt32LE(offset + 8 + i * 4)}`;
|
|
358
|
+
return value;
|
|
359
|
+
}
|
|
360
|
+
function filetimeFrom(buffer, offset) {
|
|
361
|
+
const low = buffer.readUInt32LE(offset);
|
|
362
|
+
const high = buffer.readUInt32LE(offset + 4);
|
|
363
|
+
return (BigInt(high) << 32n | BigInt(low)).toString(10);
|
|
364
|
+
}
|
|
365
|
+
/** The real Windows system directory reported by kernel32, never an environment value. */
|
|
366
|
+
function win32SystemDirectory() {
|
|
367
|
+
const load = loadBridge$1();
|
|
368
|
+
if (!load.ok) return void 0;
|
|
369
|
+
const buffer = Buffer.alloc(65536);
|
|
370
|
+
const length = load.bridge.getSystemDirectoryW(buffer, Math.floor(buffer.length / 2));
|
|
371
|
+
if (length === 0 || length >= Math.floor(buffer.length / 2)) return void 0;
|
|
372
|
+
const value = buffer.toString("utf16le", 0, length * 2).replace(/\0.*$/u, "");
|
|
373
|
+
return value === "" ? void 0 : value;
|
|
374
|
+
}
|
|
375
|
+
/** The SID of the token's real user (TokenUser), never a username or environment value. */
|
|
376
|
+
function win32CurrentTokenSid() {
|
|
377
|
+
const load = loadBridge$1();
|
|
378
|
+
if (!load.ok) return void 0;
|
|
379
|
+
const bridge = load.bridge;
|
|
380
|
+
const processHandle = bridge.openProcess(1052672, false, process.pid);
|
|
381
|
+
if (processHandle === null || processHandle === void 0) return void 0;
|
|
382
|
+
const token = Buffer.alloc(8);
|
|
383
|
+
if (!bridge.openProcessToken(processHandle, TOKEN_QUERY, token)) {
|
|
384
|
+
bridge.closeHandle(processHandle);
|
|
385
|
+
return;
|
|
386
|
+
}
|
|
387
|
+
const tokenHandle = token.readBigUInt64LE(0);
|
|
388
|
+
try {
|
|
389
|
+
const needed = Buffer.alloc(4);
|
|
390
|
+
bridge.getTokenInformation(tokenHandle, 1, Buffer.alloc(0), 0, needed);
|
|
391
|
+
const length = needed.readUInt32LE(0);
|
|
392
|
+
if (length === 0) return void 0;
|
|
393
|
+
const info = Buffer.alloc(length);
|
|
394
|
+
if (!bridge.getTokenInformation(tokenHandle, 1, info, length, needed)) return void 0;
|
|
395
|
+
const sidAddress = info.readBigUInt64LE(0);
|
|
396
|
+
return parseSid(info, Number(sidAddress - bridge.koffi.address(info)));
|
|
397
|
+
} finally {
|
|
398
|
+
bridge.closeHandle(tokenHandle);
|
|
399
|
+
bridge.closeHandle(processHandle);
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
/** Read the current kernel identity of one pid through Win32. */
|
|
403
|
+
function readWin32KernelProcessObservation(pid) {
|
|
404
|
+
if (process.platform !== "win32") return {
|
|
405
|
+
kind: "unknown",
|
|
406
|
+
code: "NATIVE_UNAVAILABLE"
|
|
407
|
+
};
|
|
408
|
+
const opened = openWin32Process(pid);
|
|
409
|
+
if (opened.kind !== "ok") return opened;
|
|
410
|
+
try {
|
|
411
|
+
return {
|
|
412
|
+
kind: "alive",
|
|
413
|
+
identity: opened.capture.identity
|
|
414
|
+
};
|
|
415
|
+
} finally {
|
|
416
|
+
closeWin32Handle(opened.capture.handle);
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
/** Open a process handle and return its exact identity after one live observation. */
|
|
420
|
+
function openWin32Process(pid) {
|
|
421
|
+
const load = loadBridge$1();
|
|
422
|
+
if (!load.ok) return {
|
|
423
|
+
kind: "unknown",
|
|
424
|
+
code: load.code
|
|
425
|
+
};
|
|
426
|
+
const bridge = load.bridge;
|
|
427
|
+
const handle = bridge.openProcess(1052672, false, pid);
|
|
428
|
+
if (!handle) {
|
|
429
|
+
const code = lastError(bridge);
|
|
430
|
+
if (code === ERROR_INVALID_PARAMETER) return { kind: "gone" };
|
|
431
|
+
if (code === ERROR_ACCESS_DENIED) return {
|
|
432
|
+
kind: "unknown",
|
|
433
|
+
code: "ACCESS_DENIED"
|
|
434
|
+
};
|
|
435
|
+
return {
|
|
436
|
+
kind: "unknown",
|
|
437
|
+
code: "READ_FAILED"
|
|
438
|
+
};
|
|
439
|
+
}
|
|
440
|
+
const fail = (code) => {
|
|
441
|
+
bridge.closeHandle(handle);
|
|
442
|
+
return {
|
|
443
|
+
kind: "unknown",
|
|
444
|
+
code
|
|
445
|
+
};
|
|
446
|
+
};
|
|
447
|
+
try {
|
|
448
|
+
const times = Buffer.alloc(32);
|
|
449
|
+
if (!bridge.getProcessTimes(handle, times, Buffer.alloc(32), Buffer.alloc(32), Buffer.alloc(32))) return fail(lastError(bridge) === ERROR_ACCESS_DENIED ? "ACCESS_DENIED" : "READ_FAILED");
|
|
450
|
+
const token = Buffer.alloc(8);
|
|
451
|
+
if (!bridge.openProcessToken(handle, TOKEN_QUERY, token)) return fail(lastError(bridge) === ERROR_ACCESS_DENIED ? "ACCESS_DENIED" : "READ_FAILED");
|
|
452
|
+
const tokenHandle = token.readBigUInt64LE(0);
|
|
453
|
+
let sid;
|
|
454
|
+
try {
|
|
455
|
+
const needed = Buffer.alloc(4);
|
|
456
|
+
bridge.getTokenInformation(tokenHandle, 1, Buffer.alloc(0), 0, needed);
|
|
457
|
+
const length = needed.readUInt32LE(0);
|
|
458
|
+
if (length === 0) return fail("READ_FAILED");
|
|
459
|
+
const info = Buffer.alloc(length);
|
|
460
|
+
if (!bridge.getTokenInformation(tokenHandle, 1, info, length, needed)) return fail("READ_FAILED");
|
|
461
|
+
const sidAddress = info.readBigUInt64LE(0);
|
|
462
|
+
sid = parseSid(info, Number(sidAddress - bridge.koffi.address(info)));
|
|
463
|
+
} finally {
|
|
464
|
+
bridge.closeHandle(tokenHandle);
|
|
465
|
+
}
|
|
466
|
+
if (sid === void 0) return fail("READ_FAILED");
|
|
467
|
+
const wait = bridge.waitForSingleObject(handle, 0);
|
|
468
|
+
if (wait === WAIT_OBJECT_0) {
|
|
469
|
+
bridge.closeHandle(handle);
|
|
470
|
+
return { kind: "gone" };
|
|
471
|
+
}
|
|
472
|
+
if (wait !== WAIT_TIMEOUT) return fail("READ_FAILED");
|
|
473
|
+
return {
|
|
474
|
+
kind: "ok",
|
|
475
|
+
capture: {
|
|
476
|
+
handle,
|
|
477
|
+
identity: {
|
|
478
|
+
platform: "win32",
|
|
479
|
+
pid,
|
|
480
|
+
ownerSid: sid,
|
|
481
|
+
creationFiletime: filetimeFrom(times, 0)
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
};
|
|
485
|
+
} catch {
|
|
486
|
+
bridge.closeHandle(handle);
|
|
487
|
+
return {
|
|
488
|
+
kind: "unknown",
|
|
489
|
+
code: "READ_FAILED"
|
|
490
|
+
};
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
/** Close one Win32 handle; callers track ownership exactly once. */
|
|
494
|
+
function closeWin32Handle(handle) {
|
|
495
|
+
const load = loadBridge$1();
|
|
496
|
+
if (!load.ok) return;
|
|
497
|
+
load.bridge.closeHandle(handle);
|
|
498
|
+
}
|
|
499
|
+
/** Wait for one held process handle to become terminal within a bound. */
|
|
500
|
+
async function waitWin32Handle(handle, timeoutMs) {
|
|
501
|
+
const load = loadBridge$1();
|
|
502
|
+
if (!load.ok) return {
|
|
503
|
+
kind: "failed",
|
|
504
|
+
code: WAIT_FAILED
|
|
505
|
+
};
|
|
506
|
+
return await new Promise((resolve) => {
|
|
507
|
+
load.bridge.waitForSingleObject.async(handle, timeoutMs, (error, code) => {
|
|
508
|
+
if (error !== null) resolve({
|
|
509
|
+
kind: "failed",
|
|
510
|
+
code: WAIT_FAILED
|
|
511
|
+
});
|
|
512
|
+
else if (code === WAIT_OBJECT_0) resolve({
|
|
513
|
+
kind: "terminal",
|
|
514
|
+
code
|
|
515
|
+
});
|
|
516
|
+
else if (code === WAIT_TIMEOUT) resolve({
|
|
517
|
+
kind: "timeout",
|
|
518
|
+
code
|
|
519
|
+
});
|
|
520
|
+
else resolve({
|
|
521
|
+
kind: "failed",
|
|
522
|
+
code
|
|
523
|
+
});
|
|
524
|
+
});
|
|
525
|
+
});
|
|
526
|
+
}
|
|
527
|
+
/** Read the security descriptor of one open kernel/file handle. */
|
|
528
|
+
function readSecurityInfo(handle, objectType) {
|
|
529
|
+
const load = loadBridge$1();
|
|
530
|
+
if (!load.ok) return void 0;
|
|
531
|
+
const bridge = load.bridge;
|
|
532
|
+
const owner = Buffer.alloc(8);
|
|
533
|
+
const group = Buffer.alloc(8);
|
|
534
|
+
const dacl = Buffer.alloc(8);
|
|
535
|
+
const sacl = Buffer.alloc(8);
|
|
536
|
+
const sd = Buffer.alloc(8);
|
|
537
|
+
if (bridge.getSecurityInfo(handle, objectType, 5, owner, group, dacl, sacl, sd) !== 0) return void 0;
|
|
538
|
+
if (sd.readBigUInt64LE(0) === 0n) return void 0;
|
|
539
|
+
const sdPtr = sd.readBigUInt64LE(0);
|
|
540
|
+
try {
|
|
541
|
+
const length = bridge.getSecurityDescriptorLength(sdPtr);
|
|
542
|
+
if (length === 0) return void 0;
|
|
543
|
+
const bytes = Buffer.from(bridge.koffi.decode(sdPtr, "uint8", length));
|
|
544
|
+
const control = Buffer.alloc(2);
|
|
545
|
+
const revision = Buffer.alloc(4);
|
|
546
|
+
if (!bridge.getSecurityDescriptorControl(sdPtr, control, revision)) return void 0;
|
|
547
|
+
const flags = control.readUInt16LE(0);
|
|
548
|
+
const ownerOffset = bytes.readUInt32LE(4);
|
|
549
|
+
const daclOffset = bytes.readUInt32LE(16);
|
|
550
|
+
const aces = [];
|
|
551
|
+
if (daclOffset !== 0) {
|
|
552
|
+
const aceCount = bytes.readUInt16LE(daclOffset + 4);
|
|
553
|
+
let offset = daclOffset + 8;
|
|
554
|
+
for (let i = 0; i < aceCount; i += 1) {
|
|
555
|
+
const type = bytes[offset];
|
|
556
|
+
const aceFlags = bytes[offset + 1];
|
|
557
|
+
const size = bytes.readUInt16LE(offset + 2);
|
|
558
|
+
const mask = bytes.readUInt32LE(offset + 4);
|
|
559
|
+
aces.push({
|
|
560
|
+
sid: parseSid(bytes, offset + 8),
|
|
561
|
+
mask,
|
|
562
|
+
type,
|
|
563
|
+
flags: aceFlags
|
|
564
|
+
});
|
|
565
|
+
offset += size;
|
|
566
|
+
}
|
|
567
|
+
}
|
|
568
|
+
return {
|
|
569
|
+
ownerSid: ownerOffset === 0 ? null : parseSid(bytes, ownerOffset),
|
|
570
|
+
daclPresent: (flags & SE_DACL_PRESENT) !== 0,
|
|
571
|
+
daclNull: daclOffset === 0,
|
|
572
|
+
aces
|
|
573
|
+
};
|
|
574
|
+
} finally {
|
|
575
|
+
bridge.localFree(sdPtr);
|
|
576
|
+
}
|
|
577
|
+
}
|
|
578
|
+
/** Turn one SDDL into a SECURITY_ATTRIBUTES buffer for a new kernel object. */
|
|
579
|
+
function securityAttributesForSddl(bridge, sddl) {
|
|
580
|
+
const sd = Buffer.alloc(8);
|
|
581
|
+
const size = Buffer.alloc(4);
|
|
582
|
+
if (!bridge.convertStringSecurityDescriptor(sddl, SDDL_REVISION_1, sd, size)) throw new Error(`native win32: invalid security descriptor (${String(lastError(bridge))})`);
|
|
583
|
+
const sdPtr = sd.readBigUInt64LE(0);
|
|
584
|
+
const attributes = Buffer.alloc(24);
|
|
585
|
+
attributes.writeUInt32LE(24, 0);
|
|
586
|
+
attributes.writeBigUInt64LE(sd.readBigUInt64LE(0), 8);
|
|
587
|
+
attributes.writeUInt32LE(0, 16);
|
|
588
|
+
return {
|
|
589
|
+
attributes,
|
|
590
|
+
sd: sdPtr
|
|
591
|
+
};
|
|
592
|
+
}
|
|
593
|
+
/**
|
|
594
|
+
* The ACL contract for an EXISTING private product object: owned by the current
|
|
595
|
+
* SID, with a present non-null DACL that grants only to the current SID or to
|
|
596
|
+
* LocalSystem / BUILTIN Administrators (outside the ordinary-account threat
|
|
597
|
+
* model). Deny ACEs never widen access. ACE count, order, protected bit and
|
|
598
|
+
* explicit-vs-inherited representation are ordinary ACL representations, not a
|
|
599
|
+
* security boundary. An ACE type this reader cannot interpret is not proof of
|
|
600
|
+
* safety, so it fails closed.
|
|
601
|
+
*/
|
|
602
|
+
function isOwnerPrivateAcl(view, sid) {
|
|
603
|
+
if (view === void 0 || !view.daclPresent || view.daclNull || view.ownerSid !== sid) return false;
|
|
604
|
+
for (const ace of view.aces) {
|
|
605
|
+
if (ace.type === ACCESS_DENIED_ACE_TYPE) continue;
|
|
606
|
+
if (ace.type !== ACCESS_ALLOWED_ACE_TYPE) return false;
|
|
607
|
+
if (ace.sid !== sid && ace.sid !== LOCAL_SYSTEM_SID && ace.sid !== BUILTIN_ADMINISTRATORS_SID) return false;
|
|
608
|
+
}
|
|
609
|
+
return true;
|
|
610
|
+
}
|
|
611
|
+
/** A private directory must additionally pass a current-SID grant to ordinary
|
|
612
|
+
* files and subdirectories. OI/CI can be distributed across multiple ACEs;
|
|
613
|
+
* inherit-only entries still express child inheritance and are not treated as
|
|
614
|
+
* proof of current-directory access. Actual OS access decides usability. */
|
|
615
|
+
function isOwnerPrivateDirectoryAcl(view, sid) {
|
|
616
|
+
if (!isOwnerPrivateAcl(view, sid)) return false;
|
|
617
|
+
let objectInherit = false;
|
|
618
|
+
let containerInherit = false;
|
|
619
|
+
for (const ace of view.aces) {
|
|
620
|
+
if (ace.type !== ACCESS_ALLOWED_ACE_TYPE || ace.sid !== sid) continue;
|
|
621
|
+
objectInherit ||= (ace.flags & OBJECT_INHERIT_ACE) !== 0;
|
|
622
|
+
containerInherit ||= (ace.flags & CONTAINER_INHERIT_ACE) !== 0;
|
|
623
|
+
}
|
|
624
|
+
return objectInherit && containerInherit;
|
|
625
|
+
}
|
|
626
|
+
/** A held named mutex seat with one idempotent release. */
|
|
627
|
+
var NamedMutexSeat = class {
|
|
628
|
+
handle;
|
|
629
|
+
bridge;
|
|
630
|
+
name;
|
|
631
|
+
constructor(bridge, handle, name) {
|
|
632
|
+
this.bridge = bridge;
|
|
633
|
+
this.handle = handle;
|
|
634
|
+
this.name = name;
|
|
635
|
+
}
|
|
636
|
+
assertCurrent() {
|
|
637
|
+
if (this.handle === void 0) throw new Error(`native win32: seat ${this.name} is already closed`);
|
|
638
|
+
}
|
|
639
|
+
async close() {
|
|
640
|
+
const handle = this.handle;
|
|
641
|
+
if (handle === void 0) return;
|
|
642
|
+
this.handle = void 0;
|
|
643
|
+
this.bridge.releaseMutex(handle);
|
|
644
|
+
this.bridge.closeHandle(handle);
|
|
645
|
+
}
|
|
646
|
+
};
|
|
647
|
+
/**
|
|
648
|
+
* Acquire one named `Global\` mutex seat. `conflictCode` is thrown when another
|
|
649
|
+
* live process holds it. A newly created object gets the supplied protected
|
|
650
|
+
* descriptor; only an already existing object needs its descriptor proved
|
|
651
|
+
* before this process adopts it.
|
|
652
|
+
*/
|
|
653
|
+
async function acquireWin32KernelSeat(name, conflictCode) {
|
|
654
|
+
const load = loadBridge$1();
|
|
655
|
+
if (!load.ok) throw new Error(`native win32: ${load.code}`);
|
|
656
|
+
const bridge = load.bridge;
|
|
657
|
+
const sid = win32CurrentTokenSid();
|
|
658
|
+
if (sid === void 0) throw new Error("native win32: the current token SID is unavailable");
|
|
659
|
+
const security = securityAttributesForSddl(bridge, `D:P(A;;GA;;;${sid})`);
|
|
660
|
+
let handle;
|
|
661
|
+
let createError = 0;
|
|
662
|
+
try {
|
|
663
|
+
bridge.setLastError(0);
|
|
664
|
+
handle = bridge.createMutexW(security.attributes, 0, name);
|
|
665
|
+
createError = lastError(bridge);
|
|
666
|
+
} finally {
|
|
667
|
+
bridge.localFree(security.sd);
|
|
668
|
+
}
|
|
669
|
+
if (handle === null || handle === void 0) {
|
|
670
|
+
if (createError === ERROR_ACCESS_DENIED) throw new Error("native win32: the named seat is owned by another account");
|
|
671
|
+
if (createError === ERROR_INVALID_HANDLE) throw new Error("native win32: the named seat is another object type");
|
|
672
|
+
throw new Error(`native win32: the named seat could not be created (${String(createError)})`);
|
|
673
|
+
}
|
|
674
|
+
try {
|
|
675
|
+
if (createError === ERROR_ALREADY_EXISTS && !isOwnerPrivateAcl(readSecurityInfo(handle, SE_KERNEL_OBJECT), sid)) throw new Error("native win32: the named seat has a foreign or broad security descriptor");
|
|
676
|
+
const wait = bridge.waitForSingleObject(handle, 0);
|
|
677
|
+
if (wait === WAIT_TIMEOUT) throw Object.assign(/* @__PURE__ */ new Error("native win32: the named seat is already held"), { code: conflictCode });
|
|
678
|
+
if (wait !== WAIT_OBJECT_0 && wait !== WAIT_ABANDONED) throw new Error(`native win32: the named seat wait failed (${String(wait)})`);
|
|
679
|
+
return new NamedMutexSeat(bridge, handle, name);
|
|
680
|
+
} catch (error) {
|
|
681
|
+
bridge.closeHandle(handle);
|
|
682
|
+
throw error;
|
|
683
|
+
}
|
|
684
|
+
}
|
|
685
|
+
/** The deterministic Host/admin mutex name: no secret, bound to scopeId only. */
|
|
686
|
+
function win32SeatName(scopeId, purpose) {
|
|
687
|
+
return `Global\\dsh-pi-tui-${purpose}-${scopeId.slice(0, 48)}`;
|
|
688
|
+
}
|
|
689
|
+
/** The deterministic per-instance stop event name. */
|
|
690
|
+
function win32StopEventName(scopeId, instanceId) {
|
|
691
|
+
return `Global\\dsh-pi-tui-stop-${scopeId.slice(0, 24)}-${instanceId}`;
|
|
692
|
+
}
|
|
693
|
+
/** Open one control-side per-instance stop event and verify its existing-object private boundary. */
|
|
694
|
+
function openWin32StopSignal(scopeId, instanceId) {
|
|
695
|
+
const load = loadBridge$1();
|
|
696
|
+
if (!load.ok) throw new Error(`native win32: ${load.code}`);
|
|
697
|
+
const bridge = load.bridge;
|
|
698
|
+
const sid = win32CurrentTokenSid();
|
|
699
|
+
if (sid === void 0) throw new Error("native win32: the current token SID is unavailable");
|
|
700
|
+
const handle = bridge.openEventW(1179650, 0, win32StopEventName(scopeId, instanceId));
|
|
701
|
+
if (handle === null || handle === void 0) {
|
|
702
|
+
const code = lastError(bridge);
|
|
703
|
+
if (code === 5) throw new Error("native win32: the stop event is owned by another account");
|
|
704
|
+
throw new Error(`native win32: the stop event is unavailable (${String(code)})`);
|
|
705
|
+
}
|
|
706
|
+
if (!isOwnerPrivateAcl(readSecurityInfo(handle, SE_KERNEL_OBJECT), sid)) {
|
|
707
|
+
bridge.closeHandle(handle);
|
|
708
|
+
throw new Error("native win32: the stop event has a foreign or broad security descriptor");
|
|
709
|
+
}
|
|
710
|
+
let set = false;
|
|
711
|
+
let closed = false;
|
|
712
|
+
return {
|
|
713
|
+
setOnce() {
|
|
714
|
+
if (set || closed) return;
|
|
715
|
+
set = true;
|
|
716
|
+
if (!bridge.setEvent(handle)) throw new Error(`native win32: SetEvent failed (${String(lastError(bridge))})`);
|
|
717
|
+
},
|
|
718
|
+
close() {
|
|
719
|
+
if (closed) return;
|
|
720
|
+
closed = true;
|
|
721
|
+
bridge.closeHandle(handle);
|
|
722
|
+
}
|
|
723
|
+
};
|
|
724
|
+
}
|
|
725
|
+
/**
|
|
726
|
+
* Create the Host-side per-instance stop event pair and start one asynchronous
|
|
727
|
+
* native wait. `stopped` resolves only on the stop event; cancellation wakes the
|
|
728
|
+
* native wait without resolving it and is used by `close()`.
|
|
729
|
+
*/
|
|
730
|
+
function createWin32StopWaiter(scopeId, instanceId) {
|
|
731
|
+
const load = loadBridge$1();
|
|
732
|
+
if (!load.ok) throw new Error(`native win32: ${load.code}`);
|
|
733
|
+
const bridge = load.bridge;
|
|
734
|
+
const sid = win32CurrentTokenSid();
|
|
735
|
+
if (sid === void 0) throw new Error("native win32: the current token SID is unavailable");
|
|
736
|
+
const security = securityAttributesForSddl(bridge, `D:P(A;;GA;;;${sid})`);
|
|
737
|
+
const eventName = win32StopEventName(scopeId, instanceId);
|
|
738
|
+
let stopHandle;
|
|
739
|
+
let cancelHandle;
|
|
740
|
+
let stopExists = false;
|
|
741
|
+
let createError = 0;
|
|
742
|
+
let cancelError = 0;
|
|
743
|
+
try {
|
|
744
|
+
bridge.setLastError(0);
|
|
745
|
+
stopHandle = bridge.createEventW(security.attributes, 0, 0, eventName);
|
|
746
|
+
createError = lastError(bridge);
|
|
747
|
+
stopExists = stopHandle !== null && createError === ERROR_ALREADY_EXISTS;
|
|
748
|
+
bridge.setLastError(0);
|
|
749
|
+
cancelHandle = bridge.createEventW(null, 0, 0, null);
|
|
750
|
+
cancelError = cancelHandle === null ? lastError(bridge) : 0;
|
|
751
|
+
} finally {
|
|
752
|
+
bridge.localFree(security.sd);
|
|
753
|
+
}
|
|
754
|
+
if (stopExists) {
|
|
755
|
+
if (stopHandle !== null && stopHandle !== void 0) bridge.closeHandle(stopHandle);
|
|
756
|
+
if (cancelHandle !== null && cancelHandle !== void 0) bridge.closeHandle(cancelHandle);
|
|
757
|
+
throw new Error("native win32: a stale per-instance stop event already exists");
|
|
758
|
+
}
|
|
759
|
+
if (stopHandle === null || stopHandle === void 0 || cancelHandle === null || cancelHandle === void 0) {
|
|
760
|
+
if (stopHandle !== null && stopHandle !== void 0) bridge.closeHandle(stopHandle);
|
|
761
|
+
if (cancelHandle !== null && cancelHandle !== void 0) bridge.closeHandle(cancelHandle);
|
|
762
|
+
throw new Error(`native win32: the per-instance stop event could not be created (${String(stopHandle === null ? createError : cancelError)})`);
|
|
763
|
+
}
|
|
764
|
+
const forcedFailure = process.env.SPLIT_PROCESS_HOST_FORCE_NATIVE_WAITER_FAILURE === "1";
|
|
765
|
+
let stopResolve;
|
|
766
|
+
let stopReject;
|
|
767
|
+
const stopped = new Promise((resolve, reject) => {
|
|
768
|
+
stopResolve = resolve;
|
|
769
|
+
stopReject = reject;
|
|
770
|
+
});
|
|
771
|
+
let closed = false;
|
|
772
|
+
let handlesClosed = false;
|
|
773
|
+
const closeHandles = () => {
|
|
774
|
+
if (handlesClosed) return;
|
|
775
|
+
handlesClosed = true;
|
|
776
|
+
bridge.closeHandle(stopHandle);
|
|
777
|
+
bridge.closeHandle(cancelHandle);
|
|
778
|
+
};
|
|
779
|
+
const loopSettled = (async () => {
|
|
780
|
+
try {
|
|
781
|
+
if (forcedFailure) throw new Error("native win32: qualification forced stop-waiter failure");
|
|
782
|
+
for (;;) {
|
|
783
|
+
const code = await new Promise((resolve, reject) => {
|
|
784
|
+
bridge.waitForMultipleObjects.async(2, [stopHandle, cancelHandle], 0, 4294967295, (error, value) => {
|
|
785
|
+
if (error !== null) reject(error);
|
|
786
|
+
else resolve(value);
|
|
787
|
+
});
|
|
788
|
+
});
|
|
789
|
+
if (code === WAIT_OBJECT_0) {
|
|
790
|
+
stopResolve?.();
|
|
791
|
+
return;
|
|
792
|
+
}
|
|
793
|
+
if (code === 1) {
|
|
794
|
+
if (closed) return;
|
|
795
|
+
continue;
|
|
796
|
+
}
|
|
797
|
+
throw new Error(`native win32: the stop-event wait returned ${String(code)}`);
|
|
798
|
+
}
|
|
799
|
+
} catch (error) {
|
|
800
|
+
stopReject?.(error instanceof Error ? error : new Error(String(error)));
|
|
801
|
+
throw error;
|
|
802
|
+
}
|
|
803
|
+
})().catch(() => {});
|
|
804
|
+
return {
|
|
805
|
+
eventName,
|
|
806
|
+
stopped,
|
|
807
|
+
async close() {
|
|
808
|
+
if (closed) {
|
|
809
|
+
await loopSettled;
|
|
810
|
+
closeHandles();
|
|
811
|
+
return;
|
|
812
|
+
}
|
|
813
|
+
closed = true;
|
|
814
|
+
bridge.setEvent(cancelHandle);
|
|
815
|
+
await loopSettled;
|
|
816
|
+
closeHandles();
|
|
817
|
+
}
|
|
818
|
+
};
|
|
819
|
+
}
|
|
820
|
+
/** Whether a path is on a local fixed NTFS volume (the PR2 supported domain). */
|
|
821
|
+
function isWin32LocalFixedNtfs(path) {
|
|
822
|
+
const load = loadBridge$1();
|
|
823
|
+
if (!load.ok) return false;
|
|
824
|
+
const bridge = load.bridge;
|
|
825
|
+
const root = path.slice(0, 2) + "\\";
|
|
826
|
+
if (!/^[A-Za-z]:\\/u.test(root)) return false;
|
|
827
|
+
if (bridge.getDriveTypeW(root) !== DRIVE_FIXED) return false;
|
|
828
|
+
const fsNameChars = 32;
|
|
829
|
+
const fsName = Buffer.alloc(64);
|
|
830
|
+
const flags = Buffer.alloc(4);
|
|
831
|
+
return bridge.getVolumeInformationW(root, null, 0, null, null, flags, fsName, fsNameChars) && fsName.toString("utf16le").replace(/\0.*$/u, "").toUpperCase() === "NTFS";
|
|
832
|
+
}
|
|
833
|
+
/** Read Win32 file attributes; undefined when the path is absent or unreadable. */
|
|
834
|
+
function win32FileAttributes(path) {
|
|
835
|
+
const load = loadBridge$1();
|
|
836
|
+
if (!load.ok) return void 0;
|
|
837
|
+
const value = load.bridge.getFileAttributesW(path) >>> 0;
|
|
838
|
+
if (value === 4294967295) return void 0;
|
|
839
|
+
return {
|
|
840
|
+
directory: (value & FILE_ATTRIBUTE_DIRECTORY) !== 0,
|
|
841
|
+
reparse: (value & FILE_ATTRIBUTE_REPARSE_POINT) !== 0
|
|
842
|
+
};
|
|
843
|
+
}
|
|
844
|
+
/** Open one existing path for a security-descriptor read without following a final reparse. */
|
|
845
|
+
function openExistingPath(path) {
|
|
846
|
+
const load = loadBridge$1();
|
|
847
|
+
if (!load.ok) return void 0;
|
|
848
|
+
const handle = load.bridge.createFileW(path, 131200, FILE_SHARE_ALL, null, OPEN_EXISTING, 35651584, null);
|
|
849
|
+
return handle === null || handle === void 0 ? void 0 : handle;
|
|
850
|
+
}
|
|
851
|
+
/** Verify one existing product directory is a real, non-reparse, private object. */
|
|
852
|
+
function win32PrivateDirectorySafe(path) {
|
|
853
|
+
const attributes = win32FileAttributes(path);
|
|
854
|
+
if (attributes === void 0 || !attributes.directory || attributes.reparse) return false;
|
|
855
|
+
const handle = openExistingPath(path);
|
|
856
|
+
if (handle === void 0) return false;
|
|
857
|
+
try {
|
|
858
|
+
const sid = win32CurrentTokenSid();
|
|
859
|
+
return sid !== void 0 && isOwnerPrivateDirectoryAcl(readSecurityInfo(handle, SE_FILE_OBJECT), sid);
|
|
860
|
+
} finally {
|
|
861
|
+
closeWin32Handle(handle);
|
|
862
|
+
}
|
|
863
|
+
}
|
|
864
|
+
/** Verify one existing product file is a real, non-reparse, private object. */
|
|
865
|
+
function win32PrivateFileSafe(path) {
|
|
866
|
+
const attributes = win32FileAttributes(path);
|
|
867
|
+
if (attributes === void 0 || attributes.directory || attributes.reparse) return false;
|
|
868
|
+
const handle = openExistingPath(path);
|
|
869
|
+
if (handle === void 0) return false;
|
|
870
|
+
try {
|
|
871
|
+
const sid = win32CurrentTokenSid();
|
|
872
|
+
return sid !== void 0 && isOwnerPrivateAcl(readSecurityInfo(handle, SE_FILE_OBJECT), sid);
|
|
873
|
+
} finally {
|
|
874
|
+
closeWin32Handle(handle);
|
|
875
|
+
}
|
|
876
|
+
}
|
|
877
|
+
/** Classify one existing product file before any write/append/cleanup. Only a
|
|
878
|
+
* true missing path is `missing`; every other lstat/open/type/ACL failure is
|
|
879
|
+
* `unsafe` so a caller cannot treat a dangling link or unreadable target as
|
|
880
|
+
* absent. */
|
|
881
|
+
function win32PrivateFileStatus(path) {
|
|
882
|
+
let info;
|
|
883
|
+
try {
|
|
884
|
+
info = lstatSync(path);
|
|
885
|
+
} catch (error) {
|
|
886
|
+
return error.code === "ENOENT" ? "missing" : "unsafe";
|
|
887
|
+
}
|
|
888
|
+
if (info.isSymbolicLink() || !info.isFile()) return "unsafe";
|
|
889
|
+
return win32PrivateFileSafe(path) ? "private" : "unsafe";
|
|
890
|
+
}
|
|
891
|
+
/** Create one directory with a protected owner-only DACL; an already existing
|
|
892
|
+
* directory is reused only after its current owner SID, DACL and child-inherit
|
|
893
|
+
* grants are proved. Created files inherit the directory's ACL and are checked
|
|
894
|
+
* by the shared private-file predicate; the file itself need not carry the
|
|
895
|
+
* protected bit. */
|
|
896
|
+
function ensureWin32PrivateDirectory(path) {
|
|
897
|
+
const load = loadBridge$1();
|
|
898
|
+
if (!load.ok) throw new Error(`native win32: ${load.code}`);
|
|
899
|
+
const bridge = load.bridge;
|
|
900
|
+
const sid = win32CurrentTokenSid();
|
|
901
|
+
if (sid === void 0) throw new Error("native win32: the current token SID is unavailable");
|
|
902
|
+
const security = securityAttributesForSddl(bridge, `D:P(A;OICI;FA;;;${sid})`);
|
|
903
|
+
let created;
|
|
904
|
+
let createError = 0;
|
|
905
|
+
try {
|
|
906
|
+
created = bridge.createDirectoryW(path, security.attributes);
|
|
907
|
+
if (!created) createError = lastError(bridge);
|
|
908
|
+
} finally {
|
|
909
|
+
bridge.localFree(security.sd);
|
|
910
|
+
}
|
|
911
|
+
if (created) return;
|
|
912
|
+
if (createError !== ERROR_ALREADY_EXISTS) throw new Error(`native win32: the private directory could not be created (${String(createError)})`);
|
|
913
|
+
if (!win32PrivateDirectorySafe(path)) throw new Error("native win32: an existing private directory is not private");
|
|
914
|
+
}
|
|
915
|
+
/** A valid Win32 handle; `CreateNamedPipeW` reports failure as INVALID_HANDLE_VALUE. */
|
|
916
|
+
function validWin32Handle(handle) {
|
|
917
|
+
return handle !== null && handle !== void 0 && handle !== INVALID_HANDLE_VALUE;
|
|
918
|
+
}
|
|
919
|
+
/** Create ONE auxiliary instance handle on an existing pipe name for DACL work. */
|
|
920
|
+
function createAcpPipeAuxiliary(bridge, pipeName) {
|
|
921
|
+
bridge.setLastError(0);
|
|
922
|
+
const handle = bridge.createNamedPipeW(pipeName, 1074003971, 0, PIPE_UNLIMITED_INSTANCES, 65536, 65536, 0, null);
|
|
923
|
+
if (!validWin32Handle(handle)) throw new Error(`native win32: the ACP named-pipe auxiliary instance could not be created (${String(lastError(bridge))})`);
|
|
924
|
+
return handle;
|
|
925
|
+
}
|
|
926
|
+
/**
|
|
927
|
+
* Apply the one-shot owner-only + local-only DACL to an already-created Node
|
|
928
|
+
* Named Pipe listener.
|
|
929
|
+
*
|
|
930
|
+
* The auxiliary instance is never connected, accepted or read; it only carries
|
|
931
|
+
* the WRITE_DAC access needed for `SetSecurityInfo`, and it is closed before
|
|
932
|
+
* this function returns. `SetSecurityInfo` on the named pipe object constrains
|
|
933
|
+
* the Node server's subsequent instances; a successful return is the commit
|
|
934
|
+
* point. Any failure throws: callers must not report the listener enabled.
|
|
935
|
+
*/
|
|
936
|
+
function secureWin32AcpPipe(pipeName) {
|
|
937
|
+
const load = loadBridge$1();
|
|
938
|
+
if (!load.ok) throw new Error(`native win32: ${load.code}`);
|
|
939
|
+
const bridge = load.bridge;
|
|
940
|
+
const sid = win32CurrentTokenSid();
|
|
941
|
+
if (sid === void 0) throw new Error("native win32: the current token SID is unavailable");
|
|
942
|
+
const writeAux = createAcpPipeAuxiliary(bridge, pipeName);
|
|
943
|
+
try {
|
|
944
|
+
const sddl = `D:P(D;;GA;;;${NETWORK_SID})(A;;GA;;;${sid})`;
|
|
945
|
+
const sd = Buffer.alloc(8);
|
|
946
|
+
const size = Buffer.alloc(4);
|
|
947
|
+
if (!bridge.convertStringSecurityDescriptor(sddl, SDDL_REVISION_1, sd, size)) throw new Error(`native win32: the ACP named-pipe security descriptor is invalid (${String(lastError(bridge))})`);
|
|
948
|
+
const sdPtr = sd.readBigUInt64LE(0);
|
|
949
|
+
try {
|
|
950
|
+
const present = Buffer.alloc(4);
|
|
951
|
+
const dacl = Buffer.alloc(8);
|
|
952
|
+
const defaulted = Buffer.alloc(4);
|
|
953
|
+
if (!bridge.getSecurityDescriptorDacl(sdPtr, present, dacl, defaulted) || present.readInt32LE(0) === 0) throw new Error("native win32: the ACP named-pipe DACL is unavailable");
|
|
954
|
+
const daclPtr = dacl.readBigUInt64LE(0);
|
|
955
|
+
const result = bridge.setSecurityInfo(writeAux, SE_FILE_OBJECT, -2147483644, null, null, daclPtr, null);
|
|
956
|
+
if (result !== 0) throw new Error(`native win32: the ACP named-pipe DACL could not be applied (${String(result)})`);
|
|
957
|
+
} finally {
|
|
958
|
+
bridge.localFree(sdPtr);
|
|
959
|
+
}
|
|
960
|
+
} finally {
|
|
961
|
+
bridge.closeHandle(writeAux);
|
|
962
|
+
}
|
|
963
|
+
}
|
|
10
964
|
/** `<sys/proc_info.h>`: PROC_PIDTBSDINFO. */
|
|
11
965
|
const PROC_PIDTBSDINFO_FLAVOR = 3;
|
|
12
966
|
/** XNU's private flavor 17 (PROC_PIDUNIQIDENTIFIERINFO), not in the public SDK. */
|
|
@@ -61,7 +1015,9 @@ function loadBridge() {
|
|
|
61
1015
|
if (loaded !== void 0) return loaded;
|
|
62
1016
|
let result;
|
|
63
1017
|
try {
|
|
64
|
-
const
|
|
1018
|
+
const anchor = installedDshAnchor();
|
|
1019
|
+
if (anchor === void 0) throw new Error("native process identity: no installed DSH anchor");
|
|
1020
|
+
const koffi = createRequire(import.meta.url)(resolveDshKoffi(anchor));
|
|
65
1021
|
const libproc = koffi.load(LIBPROC);
|
|
66
1022
|
const libsystem = koffi.load(LIBSYSTEM);
|
|
67
1023
|
const bsdInfo = koffi.struct({
|
|
@@ -497,6 +1453,7 @@ function readDarwinKernelProcess(pid) {
|
|
|
497
1453
|
function readKernelProcess(pid) {
|
|
498
1454
|
if (process.platform === "linux") return readLinuxKernelProcess(pid);
|
|
499
1455
|
if (process.platform === "darwin") return readDarwinKernelProcess(pid);
|
|
1456
|
+
if (process.platform === "win32") return readWin32KernelProcessObservation(pid);
|
|
500
1457
|
return {
|
|
501
1458
|
kind: "unknown",
|
|
502
1459
|
code: "NATIVE_UNAVAILABLE"
|
|
@@ -514,12 +1471,14 @@ function sameKernelProcess(expected, observed) {
|
|
|
514
1471
|
if (actual.platform !== expected.platform || actual.pid !== expected.pid) return "different";
|
|
515
1472
|
if (expected.platform === "linux" && actual.platform === "linux") return actual.startTicks === expected.startTicks ? "same" : "different";
|
|
516
1473
|
if (expected.platform === "darwin" && actual.platform === "darwin") return actual.bootUUID === expected.bootUUID && actual.uniqueId === expected.uniqueId && actual.processUUID === expected.processUUID && actual.startSec === expected.startSec && actual.startUsec === expected.startUsec ? "same" : "different";
|
|
1474
|
+
if (expected.platform === "win32" && actual.platform === "win32") return actual.ownerSid === expected.ownerSid && actual.creationFiletime === expected.creationFiletime ? "same" : "different";
|
|
517
1475
|
return "different";
|
|
518
1476
|
}
|
|
519
1477
|
/** Whether one identity is a member of BOTH the target process group and the
|
|
520
1478
|
* session the owner created for it (`setsid`: sid === pgid). A member that
|
|
521
1479
|
* changed its group/session no longer proves ownership of the captured group. */
|
|
522
1480
|
function isOwnedGroupMember(identity, pgid) {
|
|
1481
|
+
if (identity.platform === "win32") return false;
|
|
523
1482
|
return identity.pgid === pgid && identity.sid === pgid;
|
|
524
1483
|
}
|
|
525
1484
|
/**
|
|
@@ -661,19 +1620,22 @@ function assertDarwin() {
|
|
|
661
1620
|
function processSeatPath(scopeDir, scopeId, kind) {
|
|
662
1621
|
return join(scopeDir, `seat-${kind}-${scopeId}.lock`);
|
|
663
1622
|
}
|
|
664
|
-
/**
|
|
665
|
-
*
|
|
666
|
-
|
|
1623
|
+
/** Read and validate one seat file: a real regular file, owned by this user,
|
|
1624
|
+
* readable/writable by this user ONLY, with exactly one link. Absence is the
|
|
1625
|
+
* normal create path; every other read or validation failure is unsafe. */
|
|
1626
|
+
function readSafeSeatFile(path, uid) {
|
|
667
1627
|
let info;
|
|
668
1628
|
try {
|
|
669
1629
|
info = lstatSync(path);
|
|
670
1630
|
} catch (error) {
|
|
1631
|
+
if (error.code === "ENOENT") return void 0;
|
|
671
1632
|
throw new LocalScopeError("SCOPE_DIRECTORY_UNSAFE", `local scope: seat ${path} is not accessible`, error);
|
|
672
1633
|
}
|
|
673
1634
|
if (info.isSymbolicLink()) throw new LocalScopeError("SCOPE_DIRECTORY_UNSAFE", `local scope: seat ${path} must not be a symlink`);
|
|
674
1635
|
if (!info.isFile()) throw new LocalScopeError("SCOPE_DIRECTORY_UNSAFE", `local scope: seat ${path} must be a regular file`);
|
|
675
1636
|
if (info.uid !== uid || (info.mode & 511) !== DARWIN_SEAT_FILE_MODE) throw new LocalScopeError("SCOPE_DIRECTORY_UNSAFE", `local scope: seat ${path} must be owned by uid ${String(uid)} with mode 0600`);
|
|
676
1637
|
if (info.nlink !== 1) throw new LocalScopeError("SCOPE_DIRECTORY_UNSAFE", `local scope: seat ${path} must have exactly one link`);
|
|
1638
|
+
return info;
|
|
677
1639
|
}
|
|
678
1640
|
/** Validate the private 0700 directory the seat file lives in. */
|
|
679
1641
|
function assertSeatDirectorySafe(dir, uid) {
|
|
@@ -715,9 +1677,9 @@ var DarwinKernelSeat = class {
|
|
|
715
1677
|
if (this.fd === void 0) throw new LocalScopeError("HOST_IDENTITY_MISMATCH", `local scope: seat ${this.path} was already closed`);
|
|
716
1678
|
const directory = assertSeatDirectorySafe(this.dir, this.uid);
|
|
717
1679
|
if (directory.dev !== this.dirDev || directory.ino !== this.dirIno) throw new LocalScopeError("SCOPE_DIRECTORY_UNSAFE", `local scope: seat directory ${this.dir} was replaced`);
|
|
718
|
-
|
|
1680
|
+
const pathInfo = readSafeSeatFile(this.path, this.uid);
|
|
1681
|
+
if (pathInfo === void 0) throw new LocalScopeError("SCOPE_DIRECTORY_UNSAFE", `local scope: seat ${this.path} is no longer present`);
|
|
719
1682
|
const held = fstatSync(this.fd);
|
|
720
|
-
const pathInfo = lstatSync(this.path);
|
|
721
1683
|
if (held.dev !== pathInfo.dev || held.ino !== pathInfo.ino || held.uid !== pathInfo.uid || (held.mode & 511) !== (pathInfo.mode & 511) || held.nlink !== pathInfo.nlink) throw new LocalScopeError("HOST_IDENTITY_MISMATCH", `local scope: seat ${this.path} no longer matches the held inode`);
|
|
722
1684
|
}
|
|
723
1685
|
/**
|
|
@@ -742,14 +1704,7 @@ async function bindDarwinKernelSeat(scopeDir, scopeId, kind, conflictCode) {
|
|
|
742
1704
|
const uid = assertDarwin();
|
|
743
1705
|
const dirIdentity = assertSeatDirectorySafe(scopeDir, uid);
|
|
744
1706
|
const path = processSeatPath(scopeDir, scopeId, kind);
|
|
745
|
-
|
|
746
|
-
try {
|
|
747
|
-
lstatSync(path);
|
|
748
|
-
} catch (error) {
|
|
749
|
-
if (error.code !== "ENOENT") throw new LocalScopeError("SCOPE_DIRECTORY_UNSAFE", `local scope: seat ${path} is not accessible`, error);
|
|
750
|
-
existing = false;
|
|
751
|
-
}
|
|
752
|
-
if (existing) assertSeatFileSafe(path, uid);
|
|
1707
|
+
readSafeSeatFile(path, uid);
|
|
753
1708
|
let fd;
|
|
754
1709
|
try {
|
|
755
1710
|
fd = openSync(path, DARWIN_SEAT_FLAGS, DARWIN_SEAT_FILE_MODE);
|
|
@@ -1040,7 +1995,8 @@ function listInstalledDistFiles(packageRoot) {
|
|
|
1040
1995
|
* Fingerprint the actual installed payload. Every regular `dist/**` `.mjs`
|
|
1041
1996
|
* contributes `relative-posix-name NUL sha256(content) LF` in UTF-8 byte
|
|
1042
1997
|
* order; the bundle and DSH versions close the stream. The value is computed
|
|
1043
|
-
* once by the Host at startup and
|
|
1998
|
+
* once by the Host at startup and by surface Client startup/reexec paths that
|
|
1999
|
+
* need exact process identity.
|
|
1044
2000
|
*/
|
|
1045
2001
|
function computeBuildId(packageRoot, dshVersion) {
|
|
1046
2002
|
if (dshVersion === "") throw new Error("local scope: the installed DSH version must not be empty");
|
|
@@ -1063,6 +2019,54 @@ function computeBuildId(packageRoot, dshVersion) {
|
|
|
1063
2019
|
hash.update("\n");
|
|
1064
2020
|
return hash.digest("hex");
|
|
1065
2021
|
}
|
|
2022
|
+
/** Canonicalize one official Windows directory to the local fixed-NTFS domain. */
|
|
2023
|
+
function canonicalWin32Directory(pathValue, label) {
|
|
2024
|
+
let real;
|
|
2025
|
+
try {
|
|
2026
|
+
real = realpathSync.native(pathValue);
|
|
2027
|
+
} catch (error) {
|
|
2028
|
+
throw new LocalScopeError("SCOPE_DIRECTORY_UNSAFE", `local scope: ${label} is not a readable directory`, error);
|
|
2029
|
+
}
|
|
2030
|
+
if (!/^[A-Za-z]:\\/u.test(real)) throw new LocalScopeError("SCOPE_DIRECTORY_UNSAFE", `local scope: ${label} must be a local drive path`);
|
|
2031
|
+
const canonical = real.slice(0, 1).toUpperCase() + real.slice(1);
|
|
2032
|
+
if (!isWin32LocalFixedNtfs(canonical)) throw new LocalScopeError("SCOPE_DIRECTORY_UNSAFE", `local scope: ${label} must be on a local fixed NTFS volume`);
|
|
2033
|
+
if (!statSync(canonical).isDirectory()) throw new LocalScopeError("SCOPE_DIRECTORY_UNSAFE", `local scope: ${label} must be a directory`);
|
|
2034
|
+
return canonical;
|
|
2035
|
+
}
|
|
2036
|
+
/** The read-only Win32 scope: SID + canonical profile path, fixed hash domain. */
|
|
2037
|
+
function inspectWin32ProfileScope(facts) {
|
|
2038
|
+
const dshHome = canonicalWin32Directory(facts.dshHome, "DSH home");
|
|
2039
|
+
const profileRealpath = canonicalWin32Directory(facts.profileDir, "profile directory");
|
|
2040
|
+
const sid = win32CurrentTokenSid();
|
|
2041
|
+
if (sid === void 0) throw new LocalScopeError("SCOPE_DIRECTORY_UNSAFE", "local scope: the current token SID is unavailable");
|
|
2042
|
+
const bundleVersion = readBundleVersion(facts.packageRoot);
|
|
2043
|
+
const scopeId = createHash("sha256").update(`dsh-pi-tui/local-scope/win32/1\0${sid}\0${profileRealpath}`).digest("hex");
|
|
2044
|
+
return Object.freeze({
|
|
2045
|
+
scopeId,
|
|
2046
|
+
dshHome,
|
|
2047
|
+
profileRealpath,
|
|
2048
|
+
dir: join(dshHome, RUN_ROOT_NAME, SCOPE_ROOT_NAME, scopeId),
|
|
2049
|
+
bundleVersion,
|
|
2050
|
+
dshVersion: facts.dshVersion
|
|
2051
|
+
});
|
|
2052
|
+
}
|
|
2053
|
+
/** Create the Win32 private run chain with protected owner-only DACLs on new
|
|
2054
|
+
* directories; existing directories are reused under the shared private-object
|
|
2055
|
+
* predicate, and files inside inherit the directory ACL and are admitted by the
|
|
2056
|
+
* shared private-file predicate. */
|
|
2057
|
+
function ensureWin32ScopeDirectory(dshHome, scopeId) {
|
|
2058
|
+
const dir = join(dshHome, RUN_ROOT_NAME, SCOPE_ROOT_NAME, scopeId);
|
|
2059
|
+
for (const path of [
|
|
2060
|
+
join(dshHome, RUN_ROOT_NAME),
|
|
2061
|
+
join(dshHome, RUN_ROOT_NAME, SCOPE_ROOT_NAME),
|
|
2062
|
+
dir
|
|
2063
|
+
]) try {
|
|
2064
|
+
ensureWin32PrivateDirectory(path);
|
|
2065
|
+
} catch (error) {
|
|
2066
|
+
throw new LocalScopeError("SCOPE_DIRECTORY_UNSAFE", `local scope: ${path} is not a private product directory`, error);
|
|
2067
|
+
}
|
|
2068
|
+
return dir;
|
|
2069
|
+
}
|
|
1066
2070
|
/**
|
|
1067
2071
|
* Compute the READ-ONLY identity of one installed profile scope. The caller
|
|
1068
2072
|
* supplies the official DSH home/profile resolution results; this module never
|
|
@@ -1073,9 +2077,10 @@ function computeBuildId(packageRoot, dshVersion) {
|
|
|
1073
2077
|
* the exact same computation with the one directory-creation step.
|
|
1074
2078
|
*/
|
|
1075
2079
|
function inspectInstalledProfileScope(facts) {
|
|
2080
|
+
if (process.platform === "win32") return inspectWin32ProfileScope(facts);
|
|
1076
2081
|
const uid = assertSupportedPosixScopePlatform();
|
|
1077
2082
|
const dshHome = process.platform === "darwin" ? canonicalOwnedDirectory(facts.dshHome, uid, "DSH home") : canonicalDirectory(facts.dshHome, "DSH home");
|
|
1078
|
-
const profileRealpath =
|
|
2083
|
+
const profileRealpath = canonicalDirectory(facts.profileDir, "profile directory");
|
|
1079
2084
|
const bundleVersion = readBundleVersion(facts.packageRoot);
|
|
1080
2085
|
const scopeId = createHash("sha256").update(`dsh-pi-tui/local-scope/1\0${String(uid)}\0${profileRealpath}`).digest("hex");
|
|
1081
2086
|
return Object.freeze({
|
|
@@ -1085,28 +2090,50 @@ function inspectInstalledProfileScope(facts) {
|
|
|
1085
2090
|
dir: join(dshHome, RUN_ROOT_NAME, SCOPE_ROOT_NAME, scopeId),
|
|
1086
2091
|
hostSocketName: `${HOST_SEAT_PREFIX}${scopeId.slice(0, 40)}`,
|
|
1087
2092
|
clientSeatSocketName: `${CLIENT_SEAT_PREFIX}${scopeId.slice(0, 40)}`,
|
|
1088
|
-
buildId: computeBuildId(facts.packageRoot, facts.dshVersion),
|
|
1089
2093
|
bundleVersion,
|
|
1090
2094
|
dshVersion: facts.dshVersion
|
|
1091
2095
|
});
|
|
1092
2096
|
}
|
|
1093
2097
|
/**
|
|
1094
|
-
*
|
|
1095
|
-
*
|
|
1096
|
-
*
|
|
1097
|
-
*
|
|
1098
|
-
* shapes stay unchanged.
|
|
2098
|
+
* Strictly materialize the current installed build for one previously resolved
|
|
2099
|
+
* scope identity. Callers that publish a Host or start a detached process call
|
|
2100
|
+
* this explicitly; a failed walk cannot be hidden
|
|
2101
|
+
* by a placeholder or replaced by a version/mtime.
|
|
1099
2102
|
*/
|
|
1100
|
-
function
|
|
1101
|
-
|
|
2103
|
+
function materializeBuild(identity, packageRoot) {
|
|
2104
|
+
return Object.freeze({
|
|
2105
|
+
...identity,
|
|
2106
|
+
buildId: computeBuildId(packageRoot, identity.dshVersion)
|
|
2107
|
+
});
|
|
2108
|
+
}
|
|
2109
|
+
/**
|
|
2110
|
+
* Create/prove the private run directory for one scope identity, preserving the
|
|
2111
|
+
* caller's exact scope type. This directory step does not materialize a build.
|
|
2112
|
+
*/
|
|
2113
|
+
function ensureInstalledProfileScopeDirectory(scope) {
|
|
2114
|
+
if (process.platform === "win32") return Object.freeze({
|
|
2115
|
+
...scope,
|
|
2116
|
+
dir: ensureWin32ScopeDirectory(scope.dshHome, scope.scopeId)
|
|
2117
|
+
});
|
|
1102
2118
|
const uid = assertSupportedPosixScopePlatform();
|
|
1103
|
-
const dir = ensureScopeDirectory(
|
|
2119
|
+
const dir = ensureScopeDirectory(scope.dshHome, scope.scopeId, uid);
|
|
1104
2120
|
return Object.freeze({
|
|
1105
|
-
...
|
|
2121
|
+
...scope,
|
|
1106
2122
|
dir
|
|
1107
2123
|
});
|
|
1108
2124
|
}
|
|
1109
2125
|
/**
|
|
2126
|
+
* Build the one scope for an installed profile AND create/prove its private
|
|
2127
|
+
* 0700 run directory, before any process seat or Host Context exists. The
|
|
2128
|
+
* scope identity comes from the same read-only computation above; only the
|
|
2129
|
+
* strict build materialization and directory creation are added here, so Host
|
|
2130
|
+
* admission and stdout/qualification shapes stay unchanged. A build failure
|
|
2131
|
+
* happens before any directory creation.
|
|
2132
|
+
*/
|
|
2133
|
+
function scopeFromInstalledProfile(facts) {
|
|
2134
|
+
return ensureInstalledProfileScopeDirectory(materializeBuild(inspectInstalledProfileScope(facts), facts.packageRoot));
|
|
2135
|
+
}
|
|
2136
|
+
/**
|
|
1110
2137
|
* Strict Linux process-start identity. B1 identity comparison never falls
|
|
1111
2138
|
* back to a bare PID: a missing `/proc` value is a hard failure.
|
|
1112
2139
|
*/
|
|
@@ -1124,6 +2151,11 @@ function readLinuxStarttime(pid = process.pid) {
|
|
|
1124
2151
|
*/
|
|
1125
2152
|
function readProcessStartDiagnostic(pid = process.pid) {
|
|
1126
2153
|
if (process.platform === "linux") return readProcessStarttime(pid);
|
|
2154
|
+
if (process.platform === "win32") {
|
|
2155
|
+
const observed = readKernelProcess(pid);
|
|
2156
|
+
if (observed.kind !== "alive" || observed.identity.platform !== "win32") return void 0;
|
|
2157
|
+
return `${observed.identity.ownerSid}:${observed.identity.creationFiletime}`;
|
|
2158
|
+
}
|
|
1127
2159
|
if (process.platform === "darwin") {
|
|
1128
2160
|
const observed = readKernelProcess(pid);
|
|
1129
2161
|
if (observed.kind !== "alive" || observed.identity.platform !== "darwin") return void 0;
|
|
@@ -1194,16 +2226,28 @@ function bindProcessSeat(name, conflictCode) {
|
|
|
1194
2226
|
}, conflictCode);
|
|
1195
2227
|
}
|
|
1196
2228
|
async function bindProfileKernelSeat(scope, kind, conflictCode, options = {}) {
|
|
1197
|
-
if (process.platform === "linux")
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
2229
|
+
if (process.platform === "linux") {
|
|
2230
|
+
if (kind === "admin") throw new LocalScopeError("B1_PLATFORM_UNSUPPORTED", "local scope: the Linux admin seat uses bindProcessSeat");
|
|
2231
|
+
return bindKernelSeat({
|
|
2232
|
+
claim: "linux-abstract",
|
|
2233
|
+
name: kind === "host" ? scope.hostSocketName : scope.clientSeatSocketName
|
|
2234
|
+
}, conflictCode);
|
|
2235
|
+
}
|
|
1201
2236
|
if (process.platform === "darwin") return bindKernelSeat({
|
|
1202
2237
|
claim: "darwin-file",
|
|
1203
2238
|
dir: kind === "host" ? darwinHostProfileSeatDir(scope.scopeId, options.accountHome) : scope.dir,
|
|
1204
2239
|
scopeId: scope.scopeId,
|
|
1205
2240
|
kind
|
|
1206
2241
|
}, conflictCode);
|
|
2242
|
+
if (process.platform === "win32") {
|
|
2243
|
+
if (kind === "qualification-client") throw new LocalScopeError("B1_PLATFORM_UNSUPPORTED", "local scope: the Windows qualification Client seat is not available");
|
|
2244
|
+
try {
|
|
2245
|
+
return await acquireWin32KernelSeat(win32SeatName(scope.scopeId, kind), conflictCode);
|
|
2246
|
+
} catch (error) {
|
|
2247
|
+
if (error.code === conflictCode) throw new LocalScopeError(conflictCode, `local scope: the ${kind} seat of this profile is already held by another live process`, error);
|
|
2248
|
+
throw new LocalScopeError("SCOPE_DIRECTORY_UNSAFE", `local scope: the ${kind} seat could not be acquired`, error);
|
|
2249
|
+
}
|
|
2250
|
+
}
|
|
1207
2251
|
throw new LocalScopeError("B1_PLATFORM_UNSUPPORTED", `local scope: no profile kernel seat is implemented for ${process.platform}`);
|
|
1208
2252
|
}
|
|
1209
2253
|
/**
|
|
@@ -1220,4 +2264,4 @@ function removeOwnedScratch(scopeDir, scratchDir) {
|
|
|
1220
2264
|
});
|
|
1221
2265
|
}
|
|
1222
2266
|
//#endregion
|
|
1223
|
-
export {
|
|
2267
|
+
export { secureWin32AcpPipe as A, installedDshManifest as B, sameKernelProcess as C, isWin32LocalFixedNtfs as D, ensureWin32PrivateDirectory as E, win32SystemDirectory as F, assertDeclaredDependency as I, assertDeclaredPeerDependency as L, win32CurrentTokenSid as M, win32PrivateDirectorySafe as N, openWin32Process as O, win32PrivateFileStatus as P, assertInstallClosure as R, readProcessStarttime as S, createWin32StopWaiter as T, readManifestObject as V, scopeFromInstalledProfile as _, bindProcessSeat as a, parseProcessStatStarttime as b, darwinHostProfileSeatDir as c, installedPackageRootFromEntry as d, materializeBuild as f, removeOwnedScratch as g, readProcessStartDiagnostic as h, bindDarwinKernelSeat as i, waitWin32Handle as j, openWin32StopSignal as k, ensureInstalledProfileScopeDirectory as l, readLinuxStarttime as m, LocalScopeError as n, bindProfileKernelSeat as o, readInstalledDshVersion as p, NO_PROCESS_SEAT as r, computeBuildId as s, LOCAL_PROTOCOL as t, inspectInstalledProfileScope as u, listKernelGroupMembers as v, closeWin32Handle as w, readKernelProcess as x, parseProcessStat as y, installedDshAnchor as z };
|