@linxin666/dsh-win-bash 0.4.14 → 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.
@@ -5,7 +5,7 @@
5
5
  * The package ships its own Git for Windows MinGit extraction under
6
6
  * runtime/win32-x64/: the tool must NEVER fall back to a bash found on PATH
7
7
  * (a host Git for Windows install, WSL, or a stray MSYS), because the whole
8
- * point of vendoring is a shell whose version and bytes are pinned. Three
8
+ * point of vendoring is a shell whose version and bytes are pinned. Four
9
9
  * invariants implement that:
10
10
  *
11
11
  * 1. the binary is addressed by absolute path, never by name,
@@ -13,6 +13,13 @@
13
13
  * packaging time before it is used at all, and
14
14
  * 3. the tree that RUNS is a published copy under the DSH home, not the tree
15
15
  * inside the package.
16
+ * 4. the copy's Windows mandatory integrity label is checked before it is
17
+ * reused or before a freshly published copy is spawned. A Low (or lower)
18
+ * label on the executable image makes Windows start a Low-integrity process
19
+ * even from a Medium runner, and that process cannot write an ordinary
20
+ * Medium workspace or temp directory. Such a tree is never used: the payload
21
+ * is published into an isolated cache root instead, and if no candidate root
22
+ * is safe the mount is refused with a specific diagnosis.
16
23
  *
17
24
  * The third invariant is why this module publishes a cache: Windows refuses to
18
25
  * rename a directory whose executable is running, and pnpm imports a package by
@@ -32,6 +39,7 @@
32
39
  * prevent.
33
40
  * @module @linxin666/dsh-win-bash/runtime
34
41
  */
42
+ import { type LabelReader } from './integrity.ts';
35
43
  /** Directory holding the shipped payload, relative to the package root. */
36
44
  export declare const RUNTIME_DIR = "runtime/win32-x64";
37
45
  /** Manifest file name inside {@link RUNTIME_DIR}. */
@@ -49,6 +57,18 @@ export declare const RUNTIME_ARCH = "x64";
49
57
  * also why a release that only changes JavaScript never has to move 16 MiB.
50
58
  */
51
59
  export declare const CACHE_DIR_PARTS: readonly ["win-bash", "runtime"];
60
+ /**
61
+ * Directory parts of the isolated fallback cache, used only when the preferred
62
+ * cache root carries a Low mandatory integrity label. It lives outside the DSH
63
+ * data tree, which a workspace sandbox can label Low with an inheritable ACE.
64
+ */
65
+ export declare const ISOLATED_CACHE_DIR_PARTS: readonly ["dsh-win-bash", "runtime"];
66
+ /**
67
+ * Optional environment override for the isolated cache root's parent. An
68
+ * operator whose local app data is itself labelled Low can point this at a
69
+ * directory they have verified is Medium; it is not needed on a normal host.
70
+ */
71
+ export declare const RUNTIME_ROOT_ENV = "DSH_WIN_BASH_RUNTIME_HOME";
52
72
  /**
53
73
  * Name prefix of the staging directory one cache copy is built in. The rest of
54
74
  * the name is the payload key, this process's id and a random suffix, so a
@@ -81,6 +101,23 @@ export declare function binDir(packageRoot?: string): string;
81
101
  * @returns the directory payload copies are published under.
82
102
  */
83
103
  export declare function cacheRoot(env?: NodeJS.ProcessEnv): string;
104
+ /**
105
+ * Absolute root of the isolated fallback payload cache.
106
+ *
107
+ * The DSH home is a data tree a workspace sandbox may have labelled Low with
108
+ * an inheritable ACE. A copy published below it then inherits that label and
109
+ * starts a Low-integrity bash, which cannot write a Medium workspace or temp
110
+ * directory. This root deliberately lives OUTSIDE the DSH home, under the
111
+ * per-user application-data (or cache) directory instead.
112
+ *
113
+ * It still follows the probe-isolation convention: the final path component is
114
+ * derived from the DSH home, so an isolated `DSH_HOME` gets its own isolated
115
+ * cache too. Operators whose application-data tree is itself labelled Low can
116
+ * point {@link RUNTIME_ROOT_ENV} at a directory they have verified.
117
+ * @param env - environment to read the override, app-data root and DSH home from.
118
+ * @returns the directory the fallback payload copies are published under.
119
+ */
120
+ export declare function isolatedCacheRoot(env?: NodeJS.ProcessEnv): string;
84
121
  /** One manifest row: a payload-relative path and its digest. */
85
122
  export interface ChecksumEntry {
86
123
  /** Payload-relative path, using forward slashes. */
@@ -150,7 +187,7 @@ export declare function verifyPayloadRoot(root: string): ChecksumReport;
150
187
  /** Why this deployment cannot serve the vendored shell. */
151
188
  export interface RuntimeRejection {
152
189
  /** Machine-readable reason. */
153
- readonly code: 'unsupported-platform' | 'unsupported-arch' | 'missing-binary' | 'checksum-mismatch' | 'manifest-unreadable';
190
+ readonly code: 'unsupported-platform' | 'unsupported-arch' | 'missing-binary' | 'checksum-mismatch' | 'manifest-unreadable' | 'unsafe-integrity';
154
191
  /** One-sentence, operator-facing explanation. */
155
192
  readonly message: string;
156
193
  }
@@ -196,8 +233,12 @@ export interface PrepareRuntimeOptions {
196
233
  readonly packageRoot?: string;
197
234
  /** Cache root to publish into; the DSH home's own by default. */
198
235
  readonly cacheRoot?: string;
199
- /** Environment the default cache root resolves from; process.env by default. */
236
+ /** Isolated fallback root; the per-user app-data cache by default. */
237
+ readonly isolatedCacheRoot?: string;
238
+ /** Environment the default roots resolve from; process.env by default. */
200
239
  readonly env?: NodeJS.ProcessEnv;
240
+ /** Mandatory-integrity-label reader; the Windows icacls reader by default. */
241
+ readonly readLabel?: LabelReader;
201
242
  /** Operator-facing notes; a copy that could not be published is reported here. */
202
243
  readonly warn?: (message: string) => void;
203
244
  /** The copy itself; a real recursive copy by default. */
@@ -206,18 +247,28 @@ export interface PrepareRuntimeOptions {
206
247
  /**
207
248
  * Resolve the shell one host runs, publishing the payload outside the package.
208
249
  *
209
- * The verification order is the whole point: the shipped manifest identifies
210
- * the payload, a published copy of that identity is reused when it verifies,
211
- * and otherwise the packaged tree is verified first and only then copied. What
212
- * gets spawned is therefore always a tree whose bytes match the shipped
213
- * manifest, and it lives under the DSH home where an update or uninstall of
214
- * this package cannot be blocked by a running shell.
215
- *
216
- * A cache that cannot be written is not a reason to withhold the tool: the
217
- * verified packaged tree runs instead and the caller is told, through
218
- * {@link PrepareRuntimeOptions.warn}, that updates will need this host stopped.
219
- * A payload that fails its manifest is still a refusal — running unverified
220
- * bytes is the outcome the manifest exists to prevent, cache or no cache.
250
+ * Two independent proofs gate every tree that can be spawned: the shipped
251
+ * SHA256 manifest proves its BYTES, and a mandatory-integrity-label check
252
+ * proves its SECURITY DESCRIPTOR cannot downgrade a Medium runner to a Low
253
+ * process. A label check runs before a published copy is reused, before a new
254
+ * copy is published, and again after publication (the decisive check is on the
255
+ * bash.exe that will actually be spawned). A tree carrying Low or Untrusted is
256
+ * never reused and never spawned from, so a hash match alone can no longer keep
257
+ * a broken cache alive across restarts and updates.
258
+ *
259
+ * Candidate roots are tried in order. The DSH home cache keeps the #1996 fix;
260
+ * when its data tree is labelled Low, the payload is published into an isolated
261
+ * per-user application-data root instead, still OUTSIDE the package. An old Low
262
+ * cache under the DSH home is therefore migrated by abandonment, not by a
263
+ * one-off repair of one payload-key directory.
264
+ *
265
+ * When no candidate can be written or verified, the verified packaged tree runs
266
+ * instead and the caller is told, through {@link PrepareRuntimeOptions.warn},
267
+ * that updates will need this host stopped. But if a Low label was observed and
268
+ * even the packaged image is not proven clean, the mount is REFUSED with
269
+ * `unsafe-integrity` rather than silently executing a shell that cannot write
270
+ * the session's workspace. A payload that fails its manifest is always a
271
+ * refusal, cache or no cache.
221
272
  * @param options - host facts, roots, and the seams tests drive.
222
273
  * @returns the shell paths to run, or the rejection reason.
223
274
  */
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@linxin666/dsh-win-bash",
3
3
  "description": "Pinned private bash for Windows x64 DSH sessions: one persistent bash process per session behind the win_bash tool, carrying a digested Git for Windows MinGit payload and no PATH fallback.",
4
- "version": "0.4.14",
4
+ "version": "0.4.15",
5
5
  "icon": "icon.svg",
6
6
  "type": "module",
7
7
  "main": "lib/index.js",
@@ -18,22 +18,22 @@
18
18
  },
19
19
  "dsh": {
20
20
  "engines": {
21
- "dsh": ">=0.2.1-rc.3"
21
+ "dsh": ">=0.2.2-rc.1"
22
22
  },
23
23
  "bundle": {
24
24
  "patch": "./cordis.patch.yml"
25
25
  }
26
26
  },
27
27
  "peerDependencies": {
28
- "@deepseek-ai/dsh": ">=0.2.1-rc.3"
28
+ "@deepseek-ai/dsh": ">=0.2.2-rc.1"
29
29
  },
30
30
  "devDependencies": {
31
- "@deepseek-ai/cordis": "^4.0.5",
32
- "@deepseek-ai/dsh-agent": "^0.2.1-rc.3",
33
- "@deepseek-ai/dsh-sandbox": "^0.2.1-rc.3",
34
- "@deepseek-ai/dsh-subprocess": "^0.2.1-rc.3",
35
- "@deepseek-ai/dsh-tools": "^0.2.1-rc.3",
36
- "@deepseek-ai/schemastery": "^3.18.5",
31
+ "@deepseek-ai/cordis": "^4.1.0",
32
+ "@deepseek-ai/dsh-agent": "^0.2.2-rc.1",
33
+ "@deepseek-ai/dsh-sandbox": "^0.2.2-rc.1",
34
+ "@deepseek-ai/dsh-subprocess": "^0.2.2-rc.1",
35
+ "@deepseek-ai/dsh-tools": "^0.2.2-rc.1",
36
+ "@deepseek-ai/schemastery": "^3.19.0",
37
37
  "@types/node": "^22.20.0",
38
38
  "tsdown": "0.22.2",
39
39
  "typescript": "~5.7.2",
package/src/background.ts CHANGED
@@ -15,7 +15,7 @@
15
15
  * STRUCTURALLY: a leaf plugin takes no dependency on @deepseek-ai/dsh-jobs to
16
16
  * call one method on an optional service, and the same decision keeps this
17
17
  * package out of the job registry's implementation. The members below are
18
- * mirrored from the published declaration of @deepseek-ai/dsh-jobs@0.2.1-rc.3:
18
+ * mirrored from the published declaration of @deepseek-ai/dsh-jobs@0.2.2-rc.1:
19
19
  *
20
20
  * lib/types/index.d.ts
21
21
  * abstract class JobRegistry {
package/src/gate.ts CHANGED
@@ -12,7 +12,7 @@
12
12
  * on @deepseek-ai/dsh-agent-preset-registry (a leaf plugin takes no SDK
13
13
  * dependency to read one optional service), so the single method consumed is
14
14
  * mirrored from the published declaration of
15
- * @deepseek-ai/dsh-agent-preset-registry@0.2.1-rc.3, lib/types/index.d.ts:
15
+ * @deepseek-ai/dsh-agent-preset-registry@0.2.2-rc.1, lib/types/index.d.ts:
16
16
  *
17
17
  * export declare class AgentPresetRegistry {
18
18
  * list(): Promise<AgentPreset[]>
package/src/index.ts CHANGED
@@ -13,14 +13,17 @@
13
13
  * and on a Windows host that is not x64, because no PATH bash is ever used as
14
14
  * a substitute.
15
15
  *
16
- * Mount-time publication: the tree that is spawned is a verified copy under the
17
- * DSH home, not the tree inside this package. A persistent shell holds its own
18
- * executable open, and Windows refuses to rename a directory that holds a
19
- * running executable, so a shell started inside node_modules makes pnpm's
16
+ * Mount-time publication: the tree that is spawned is a verified copy outside
17
+ * this package, not the tree inside node_modules. A persistent shell holds its
18
+ * own executable open, and Windows refuses to rename a directory that holds a
19
+ * running executable, so a shell started inside the package makes pnpm's
20
20
  * import of this package fail with ERR_PNPM_EPERM. Publishing the payload
21
21
  * outside the package is what lets an update or uninstall run while sessions
22
- * are live; a cache that cannot be written keeps the tool working from the
23
- * packaged tree and logs that updates will need this host stopped.
22
+ * are live. The copy's mandatory integrity label is checked before reuse and
23
+ * before execution: a Low-labelled cache is abandoned for an isolated root
24
+ * instead of starting a Low-integrity shell. A cache that cannot be written,
25
+ * or no safe root at all, keeps the tool working from the packaged tree and
26
+ * logs that updates will need this host stopped.
24
27
  *
25
28
  * Availability gate: win_bash serves sessions that run the LiangShen (梁神)
26
29
  * preset, so its registration follows that preset's DECLARATION in the
@@ -123,10 +126,13 @@ export {
123
126
  export {
124
127
  CACHE_DIR_PARTS,
125
128
  CACHE_STAGING_PREFIX,
129
+ ISOLATED_CACHE_DIR_PARTS,
130
+ RUNTIME_ROOT_ENV,
126
131
  bashIn,
127
132
  bashPath,
128
133
  binIn,
129
134
  cacheRoot,
135
+ isolatedCacheRoot,
130
136
  parseChecksums,
131
137
  payloadKey,
132
138
  prepareRuntime,
@@ -0,0 +1,161 @@
1
+ /**
2
+ * Windows mandatory-integrity-label inspection for the published payload cache.
3
+ *
4
+ * A cache under a data tree that carries an inheritable Low Mandatory Level ACE
5
+ * produces a bash.exe that inherits that label. Windows then starts a
6
+ * Low-integrity process even from a Medium-integrity runner
7
+ * (TOKEN_MANDATORY_POLICY_NEW_PROCESS_MIN takes the LOWER of parent token and
8
+ * executable image), and a Low process cannot write an ordinary Medium
9
+ * workspace or temp directory. The bytes and SHA256 manifest are identical, so
10
+ * a checksum-only cache check cannot see this: the defect is in the security
11
+ * descriptor, not the file content.
12
+ *
13
+ * Node's fs API cannot read a SACL. The Windows built-in `icacls <path> /save`
14
+ * writes a security descriptor as language-independent SDDL in UTF-16LE;
15
+ * `S:...` carries the mandatory label ACEs, e.g. `(ML;OICI;NW;;;LW)` for an
16
+ * inheritable Low label or `(ML;ID;NW;;;LW)` for an inherited one. `LW` is
17
+ * the SDDL abbreviation of S-1-16-4096 (Low); Untrusted is S-1-16-0. Anything
18
+ * below Medium (S-1-16-8192) can downgrade the spawned shell.
19
+ *
20
+ * The binary is addressed by absolute path under SystemRoot, never through
21
+ * PATH. This module only reads; it never changes a label, a DACL, a token or a
22
+ * user's data directory.
23
+ * @module @linxin666/dsh-win-bash/integrity
24
+ */
25
+
26
+ import { spawnSync } from 'node:child_process'
27
+ import { randomUUID } from 'node:crypto'
28
+ import { existsSync, mkdtempSync, readFileSync, rmdirSync, unlinkSync } from 'node:fs'
29
+ import { tmpdir } from 'node:os'
30
+ import { dirname, join, resolve } from 'node:path'
31
+
32
+ /** Integrity RID of Medium; a label below it downgrades a spawned process. */
33
+ export const MEDIUM_INTEGRITY_RID = 8192
34
+
35
+ /** Wall-clock bound on one icacls query for a security descriptor. */
36
+ export const READ_LABEL_TIMEOUT_MS = 5000
37
+
38
+ /** What one mandatory-integrity-label inspection established. */
39
+ export type IntegrityLabel = 'medium-or-higher' | 'low-or-untrusted' | 'unknown'
40
+
41
+ /**
42
+ * A reader for one path's mandatory integrity label. The seam exists so tests
43
+ * can drive Low/Unknown verdicts without touching the host's ACLs.
44
+ */
45
+ export type LabelReader = (target: string) => IntegrityLabel
46
+
47
+ /** One mandatory-label ACE inside an SDDL SACL: (ML;flags;rights;;;SID). */
48
+ const MANDATORY_LABEL_ACE = /\(ML;[^)]*;([^;)]+)\)/gi
49
+
50
+ /**
51
+ * Classify the mandatory label ACEs of one icacls /save SDDL blob.
52
+ *
53
+ * A missing SACL means the object has no explicit label and inherits Medium by
54
+ * default. High/System/Medium labels are harmless here; only Low or Untrusted
55
+ * can turn a Medium runner's child into a Low process.
56
+ * @param sddl - the UTF-16LE-decoded contents of an icacls /save file.
57
+ * @returns the label verdict. `unknown` is reserved for the reader that could
58
+ * not obtain or trust a descriptor at all; a parsed blob always decides.
59
+ */
60
+ export function parseIntegritySddl(sddl: string): IntegrityLabel {
61
+ // ML (SYSTEM_MANDATORY_LABEL_ACE_TYPE) only ever appears in the SACL. Taking
62
+ // everything after the S: marker keeps control flags such as AI/P/AR out of
63
+ // the match while still scanning every ACE in that section.
64
+ const sacl = /S:([^\r\n]*)/.exec(sddl)?.[1] ?? ''
65
+ for (const match of sacl.matchAll(MANDATORY_LABEL_ACE)) {
66
+ const sid = (match[1] ?? '').trim().toUpperCase()
67
+ if (sid === 'LW') return 'low-or-untrusted'
68
+ const numeric = /^S-1-16-(\d+)$/.exec(sid)
69
+ if (numeric !== null && Number(numeric[1]) < MEDIUM_INTEGRITY_RID) return 'low-or-untrusted'
70
+ }
71
+ return 'medium-or-higher'
72
+ }
73
+
74
+ /** Absolute path of the Windows icacls binary, never resolved through PATH. */
75
+ function icaclsPath(env: NodeJS.ProcessEnv): string {
76
+ return join(env.SystemRoot ?? env.windir ?? 'C:\\Windows', 'System32', 'icacls.exe')
77
+ }
78
+
79
+ /**
80
+ * Read one existing path's mandatory integrity label.
81
+ *
82
+ * On a POSIX host there is no Windows mandatory integrity control (and this
83
+ * package does not mount there), so the label cannot downgrade anything. A
84
+ * missing icacls, a failed query or an unreadable descriptor is reported as
85
+ * `unknown` rather than guessed: the caller decides whether to fall back.
86
+ * @param target - an existing file or directory to inspect.
87
+ * @param env - environment supplying SystemRoot; the live one by default.
88
+ * @returns the label verdict.
89
+ */
90
+ export function readMandatoryLabel(target: string, env: NodeJS.ProcessEnv = process.env): IntegrityLabel {
91
+ if (process.platform !== 'win32') return 'medium-or-higher'
92
+ const exe = icaclsPath(env)
93
+ if (!existsSync(exe)) return 'unknown'
94
+ let scratch: string | undefined
95
+ let aclFile: string | undefined
96
+ try {
97
+ scratch = mkdtempSync(join(tmpdir(), 'dsh-win-bash-acl-'))
98
+ aclFile = join(scratch, 'acl-' + randomUUID().slice(0, 8) + '.txt')
99
+ const run = spawnSync(exe, [target, '/save', aclFile], {
100
+ windowsHide: true,
101
+ timeout: READ_LABEL_TIMEOUT_MS,
102
+ stdio: 'ignore',
103
+ })
104
+ if (run.error !== undefined || run.status !== 0 || !existsSync(aclFile)) return 'unknown'
105
+ return parseIntegritySddl(readFileSync(aclFile).toString('utf16le'))
106
+ } catch {
107
+ return 'unknown'
108
+ } finally {
109
+ // Delete the two specific paths this call created; never a recursive delete.
110
+ if (aclFile !== undefined) {
111
+ try { unlinkSync(aclFile) } catch { /* already gone */ }
112
+ }
113
+ if (scratch !== undefined) {
114
+ try { rmdirSync(scratch) } catch { /* already gone */ }
115
+ }
116
+ }
117
+ }
118
+
119
+ /**
120
+ * The nearest existing path at or above `path`.
121
+ *
122
+ * A directory that does not exist yet has no descriptor of its own; a new file
123
+ * created below it inherits the label of the closest existing ancestor. That
124
+ * ancestor is what a pre-publication check has to inspect.
125
+ * @param path - the prospective path.
126
+ * @returns itself when it exists, otherwise its closest existing ancestor.
127
+ */
128
+ export function nearestExistingAncestor(path: string): string {
129
+ let current = resolve(path)
130
+ for (;;) {
131
+ if (existsSync(current)) return current
132
+ const parent = dirname(current)
133
+ if (parent === current) return current
134
+ current = parent
135
+ }
136
+ }
137
+
138
+ /** Bound on the directory chain walked, so a pathological path cannot spin. */
139
+ export const MAX_ANCESTOR_DEPTH = 32
140
+
141
+ /**
142
+ * Every existing path from `path` up to the volume root.
143
+ *
144
+ * A cache root can sit several levels below the data tree that a workspace
145
+ * sandbox labelled Low, and an inheritable ACE on ANY of those ancestors can
146
+ * still be propagated onto existing children later. Checking only the nearest
147
+ * existing ancestor would miss that, so the whole chain is inspected.
148
+ * @param path - the prospective path (it need not exist yet).
149
+ * @returns the existing ancestors, nearest first.
150
+ */
151
+ export function existingAncestors(path: string): string[] {
152
+ const found: string[] = []
153
+ let current = resolve(path)
154
+ for (let depth = 0; depth < MAX_ANCESTOR_DEPTH; depth += 1) {
155
+ if (existsSync(current)) found.push(current)
156
+ const parent = dirname(current)
157
+ if (parent === current) break
158
+ current = parent
159
+ }
160
+ return found
161
+ }