@deepseek-ai/dsh-sandbox-windows-acl 0.2.2-alpha.3 → 0.2.2-rc.2

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.
@@ -0,0 +1,54 @@
1
+ /**
2
+ * The per-workspace write identity: a deterministic `S-1-4-x-y` SID derived
3
+ * from the canonical workspace path, whose ACEs form that workspace's write
4
+ * allowlist. Every confined execution of the same workspace — across
5
+ * sessions, server restarts, and calls — carries the SAME write SID, so the
6
+ * workspace-root ACE materializes once per workspace per machine (the
7
+ * grant's exact-ACE skip then makes every later provision O(1)) instead of
8
+ * once per session. The SID's power is defined solely by the ACEs that name
9
+ * it (which exist only on the workspace tree and the session's private temp
10
+ * directory), and only tokens minted for that workspace carry it — the SID
11
+ * string itself is not a secret. Temporary directories use a separate,
12
+ * per-directory identity from {@link tempWriteSid}; sharing the workspace
13
+ * identity with temp would let sibling sessions write one another's temp
14
+ * trees.
15
+ *
16
+ * The input MUST be the canonical workspace path (`realpathSync.native` on
17
+ * Windows — the sandbox-policy `resolveWorkspaceRoot` already applies it):
18
+ * canonicalization converges case/alias spellings, so two spellings of one
19
+ * workspace derive one SID; an as-spelled fallback path would mint a second
20
+ * identity for the same directory (self-healing, at the cost of one extra
21
+ * tree propagation). Renaming the workspace directory derives a new SID —
22
+ * the old standing ACEs are inert residue, and the next session re-propagates
23
+ * once.
24
+ * @module @deepseek-ai/dsh-sandbox-windows-acl/workspace-sid
25
+ */
26
+
27
+ import { createHash } from 'node:crypto'
28
+
29
+ /**
30
+ * Derive the workspace's write SID (`S-1-4-x-y`; subauthorities 30-bit,
31
+ * matching the workspace-capability shape the token and ACE layers carry).
32
+ * @param workspaceRoot - the canonical workspace path.
33
+ * @returns the SDDL string form.
34
+ */
35
+ export function workspaceWriteSid(workspaceRoot: string): string {
36
+ const digest = createHash('sha256').update(workspaceRoot, 'utf8').digest()
37
+ const first = (digest.readUInt32LE(0) % (2 ** 30 - 1)) + 1
38
+ const second = (digest.readUInt32LE(4) % (2 ** 30 - 1)) + 1
39
+ return `S-1-4-${first}-${second}`
40
+ }
41
+
42
+ /**
43
+ * Derive one private temp directory's write SID. The random directory path
44
+ * is the capability identity; a fixed third subauthority domain-separates
45
+ * the result from every two-subauthority workspace SID.
46
+ * @param tempDir - the private temp directory's absolute path.
47
+ * @returns the SDDL string form.
48
+ */
49
+ export function tempWriteSid(tempDir: string): string {
50
+ const digest = createHash('sha256').update('temp\0', 'utf8').update(tempDir, 'utf8').digest()
51
+ const first = (digest.readUInt32LE(0) % (2 ** 30 - 1)) + 1
52
+ const second = (digest.readUInt32LE(4) % (2 ** 30 - 1)) + 1
53
+ return `S-1-4-${first}-${second}-1`
54
+ }