@deepseek-ai/dsh-fs-sandbox 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.
package/lib/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { LocalFileSystem } from "@deepseek-ai/dsh-fs-local";
2
- import { FsError } from "@deepseek-ai/dsh-fs";
2
+ import { FsError, fsErrorFromErrno } from "@deepseek-ai/dsh-fs";
3
3
  import { writableRoots } from "@deepseek-ai/dsh-sandbox";
4
4
  import { stat } from "node:fs/promises";
5
5
  import { dirname, sep } from "node:path";
@@ -29,10 +29,8 @@ async function statIfPresent(path) {
29
29
  try {
30
30
  return await stat(path, { bigint: true });
31
31
  } catch (error) {
32
- /* v8 ignore else -- a non-missing stat failure requires a host permission or I/O fault after resolve reached this ancestor. */
33
32
  if (isMissing(error)) return void 0;
34
- /* v8 ignore next -- requires a host permission or I/O fault after resolve already reached this ancestor. */
35
- throw error;
33
+ throw fsErrorFromErrno("stat", path, error);
36
34
  }
37
35
  }
38
36
  function sameIdentity(left, right) {
@@ -49,6 +47,7 @@ function sameIdentity(left, right) {
49
47
  * @param caseSensitive - whether lexical comparison preserves case; defaults
50
48
  * to the host filesystem convention used by supported platforms.
51
49
  * @returns whether the target is the root or a descendant of it.
50
+ * @throws FsError when an ancestor or the root cannot be inspected, coded by the Node errno.
52
51
  */
53
52
  async function isPathUnder(path, root, caseSensitive = process.platform !== "win32") {
54
53
  if (isLexicallyUnder(path, root, caseSensitive)) return true;
@@ -15,6 +15,7 @@
15
15
  * @param caseSensitive - whether lexical comparison preserves case; defaults
16
16
  * to the host filesystem convention used by supported platforms.
17
17
  * @returns whether the target is the root or a descendant of it.
18
+ * @throws FsError when an ancestor or the root cannot be inspected, coded by the Node errno.
18
19
  */
19
20
  export declare function isPathUnder(path: string, root: string, caseSensitive?: boolean): Promise<boolean>;
20
21
  //# sourceMappingURL=containment.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"containment.d.ts","sourceRoot":"","sources":["../../src/containment.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAuCH;;;;;;;;;;;;GAYG;AACH,wBAAsB,WAAW,CAC/B,IAAI,EAAE,MAAM,EACZ,IAAI,EAAE,MAAM,EACZ,aAAa,UAA+B,GAC3C,OAAO,CAAC,OAAO,CAAC,CAclB"}
@@ -0,0 +1 @@
1
+ {"version":3,"file":"containment.js","sourceRoot":"","sources":["../../src/containment.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAGH,OAAO,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAA;AACvC,OAAO,EAAE,OAAO,EAAE,GAAG,EAAE,MAAM,WAAW,CAAA;AACxC,OAAO,EAAE,gBAAgB,EAAE,MAAM,qBAAqB,CAAA;AAEtD,MAAM,aAAa,GAA+C,IAAI,GAAG,CAAC,CAAC,QAAQ,EAAE,SAAS,CAAC,CAAC,CAAA;AAEhG,SAAS,SAAS,CAAC,KAAc;IAC/B,MAAM,IAAI,GAAI,KAA+B,CAAC,IAAI,CAAA;IAClD,OAAO,aAAa,CAAC,GAAG,CAAC,IAAI,CAAC,CAAA;AAChC,CAAC;AAED,SAAS,cAAc,CAAC,IAAY,EAAE,aAAsB;IAC1D,OAAO,aAAa,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,WAAW,EAAE,CAAA;AAClD,CAAC;AAED,SAAS,gBAAgB,CAAC,IAAY,EAAE,IAAY,EAAE,aAAsB;IAC1E,MAAM,gBAAgB,GAAG,cAAc,CAAC,IAAI,EAAE,aAAa,CAAC,CAAA;IAC5D,MAAM,cAAc,GAAG,cAAc,CAAC,IAAI,EAAE,aAAa,CAAC,CAAA;IAC1D,IAAI,gBAAgB,KAAK,cAAc;QAAE,OAAO,IAAI,CAAA;IACpD,MAAM,MAAM,GAAG,cAAc,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,cAAc,CAAC,CA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,EAAE,IAAI,CAAC,EAAE,CAAC;gBAC7C,SAAS,GAAG,IAAI,CAAA;gBAChB,MAAK;YACP,CAAC;QACH,CAAC;QACD,IAAI,CAAC,SAAS,EAAE,CAAC;YACf,MAAM,IAAI,OAAO,CAAC,iBAAiB,MAAM,CAAC,WAAW,kDAAkD,EAAE,mBAAmB,CAAC,CAAA;QAC/H,CAAC;QACD,OAAO,KAAK,CAAA;IACd,CAAC;;AAGH,eAAe,mBAAmB,CAAA"}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@deepseek-ai/dsh-fs-sandbox",
3
3
  "description": "Sandbox-enforcing implementation of the DeepSeek Harness filesystem seam: fences write/edit by the per-call sandbox mode (read-only denies mutation, workspace-write contains it to the workspace + temp roots) while reads pass through",
4
- "version": "0.2.2-alpha.3",
4
+ "version": "0.2.2-rc.2",
5
5
  "publishConfig": {
6
6
  "access": "public"
7
7
  },
@@ -25,22 +25,26 @@
25
25
  "files": [
26
26
  "locale/*.json",
27
27
  "lib/index.js",
28
- "lib/types/**/*.d.ts"
28
+ "lib/types/**/*.d.ts",
29
+ "lib/types/**/*.d.ts.map",
30
+ "lib/**/*.js.map",
31
+ "src"
29
32
  ],
30
33
  "license": "MIT",
31
34
  "peerDependencies": {
32
- "@deepseek-ai/dsh-fs": "0.2.2-alpha.3",
33
- "@deepseek-ai/dsh-fs-local": "0.2.2-alpha.3",
34
- "@deepseek-ai/dsh-sandbox": "0.2.2-alpha.3",
35
- "@deepseek-ai/dsh-sandbox-policy": "0.2.2-alpha.3",
36
- "@deepseek-ai/cordis": "~4.0.6-alpha.2"
35
+ "@deepseek-ai/dsh-fs": "0.2.2-rc.2",
36
+ "@deepseek-ai/dsh-fs-local": "0.2.2-rc.2",
37
+ "@deepseek-ai/dsh-sandbox": "0.2.2-rc.2",
38
+ "@deepseek-ai/dsh-sandbox-policy": "0.2.2-rc.2",
39
+ "@deepseek-ai/cordis": "~4.1.0"
37
40
  },
38
41
  "devDependencies": {
39
- "@deepseek-ai/dsh-fs": "0.2.2-alpha.3",
40
- "@deepseek-ai/dsh-fs-local": "0.2.2-alpha.3",
41
- "@deepseek-ai/dsh-sandbox": "0.2.2-alpha.3",
42
- "@deepseek-ai/dsh-sandbox-policy": "0.2.2-alpha.3",
43
- "@deepseek-ai/cordis": "~4.0.6-alpha.2",
44
- "@deepseek-ai/dsh-session-projection": "0.2.2-alpha.3"
42
+ "@deepseek-ai/dsh-session": "0.2.2-rc.2",
43
+ "@deepseek-ai/dsh-fs": "0.2.2-rc.2",
44
+ "@deepseek-ai/dsh-fs-local": "0.2.2-rc.2",
45
+ "@deepseek-ai/dsh-sandbox": "0.2.2-rc.2",
46
+ "@deepseek-ai/dsh-sandbox-policy": "0.2.2-rc.2",
47
+ "@deepseek-ai/cordis": "~4.1.0",
48
+ "@deepseek-ai/dsh-session-projection": "0.2.2-rc.2"
45
49
  }
46
50
  }
@@ -0,0 +1,76 @@
1
+ /**
2
+ * Path-containment mechanics for the filesystem sandbox. Canonical spellings
3
+ * take the fast lexical path; filesystem identity supplies the conservative
4
+ * fallback for alias-equivalent roots such as Windows 8.3 names and casing.
5
+ * @module @deepseek-ai/dsh-fs-sandbox/containment
6
+ */
7
+
8
+ import type { BigIntStats } from 'node:fs'
9
+ import { stat } from 'node:fs/promises'
10
+ import { dirname, sep } from 'node:path'
11
+ import { fsErrorFromErrno } from '@deepseek-ai/dsh-fs'
12
+
13
+ const MISSING_CODES: ReadonlySet<NodeJS.ErrnoException['code']> = new Set(['ENOENT', 'ENOTDIR'])
14
+
15
+ function isMissing(error: unknown): boolean {
16
+ const code = (error as NodeJS.ErrnoException).code
17
+ return MISSING_CODES.has(code)
18
+ }
19
+
20
+ function comparablePath(path: string, caseSensitive: boolean): string {
21
+ return caseSensitive ? path : path.toLowerCase()
22
+ }
23
+
24
+ function isLexicallyUnder(path: string, root: string, caseSensitive: boolean): boolean {
25
+ const comparableTarget = comparablePath(path, caseSensitive)
26
+ const comparableRoot = comparablePath(root, caseSensitive)
27
+ if (comparableTarget === comparableRoot) return true
28
+ const prefix = comparableRoot.endsWith(sep) ? comparableRoot : comparableRoot + sep
29
+ return comparableTarget.startsWith(prefix)
30
+ }
31
+
32
+ async function statIfPresent(path: string): Promise<BigIntStats | undefined> {
33
+ try {
34
+ return await stat(path, { bigint: true })
35
+ } catch (error: unknown) {
36
+ if (isMissing(error)) return undefined
37
+ throw fsErrorFromErrno('stat', path, error)
38
+ }
39
+ }
40
+
41
+ function sameIdentity(left: BigIntStats, right: BigIntStats): boolean {
42
+ return left.dev === right.dev && left.ino === right.ino
43
+ }
44
+
45
+ /**
46
+ * Determine whether a canonical target is a writable root or lies beneath it.
47
+ * The lexical fast path handles normal canonical spellings. When spellings
48
+ * differ, walk the target's existing ancestors and compare filesystem identity
49
+ * with the root; this recognizes Windows long-name/8.3 aliases and casing
50
+ * without weakening containment to a textual approximation.
51
+ * @param path - canonical target key, which may end in a missing suffix.
52
+ * @param root - canonical writable root.
53
+ * @param caseSensitive - whether lexical comparison preserves case; defaults
54
+ * to the host filesystem convention used by supported platforms.
55
+ * @returns whether the target is the root or a descendant of it.
56
+ * @throws FsError when an ancestor or the root cannot be inspected, coded by the Node errno.
57
+ */
58
+ export async function isPathUnder(
59
+ path: string,
60
+ root: string,
61
+ caseSensitive = process.platform !== 'win32',
62
+ ): Promise<boolean> {
63
+ if (isLexicallyUnder(path, root, caseSensitive)) return true
64
+
65
+ const rootInfo = await statIfPresent(root)
66
+ if (!rootInfo) return false
67
+
68
+ let ancestor = path
69
+ while (true) {
70
+ const ancestorInfo = await statIfPresent(ancestor)
71
+ if (ancestorInfo && sameIdentity(ancestorInfo, rootInfo)) return true
72
+ const parent = dirname(ancestor)
73
+ if (parent === ancestor) return false
74
+ ancestor = parent
75
+ }
76
+ }
package/src/index.ts ADDED
@@ -0,0 +1,181 @@
1
+ /**
2
+ * `SandboxedFileSystem`: the sandbox-enforcing implementation of the
3
+ * `@deepseek-ai/dsh-fs` Service Definition. It extends `LocalFileSystem` so all
4
+ * text-storage mechanics — resolve, stat, read/stream, list, the atomic
5
+ * write and the read-match-write edit critical section — are the local
6
+ * implementation's, verbatim; this package adds only the per-call POLICY fence
7
+ * on all five mutations. Reads pass through untouched: every mode permits
8
+ * reading.
9
+ *
10
+ * The fence is a policy check in TRUSTED code over a MODEL-CONTROLLED path,
11
+ * NOT a kernel boundary — the operations are the seam's own (open, rename),
12
+ * and only the target path is untrusted, so canonicalize-then-contain is the
13
+ * complete answer to this surface. This is containment, not a security
14
+ * boundary; kernel-grade isolation of untrusted CODE stays `ctx.shell`'s job
15
+ * (`@deepseek-ai/dsh-bash-sandbox`). The residual
16
+ * TOCTOU (an ancestor symlink swapped between the containment re-check and the
17
+ * syscall) is narrowed by re-canonicalizing immediately before delegating and
18
+ * is accepted for this threat model.
19
+ *
20
+ * Per-call policy: `read-only` denies every mutation; `workspace-write` allows
21
+ * a mutation only when the target canonicalizes under the policy's workspace
22
+ * root, an explicit additional writable root, or a platform temp area from the
23
+ * shared `writableRoots` policy; `danger-full-access` delegates unfenced. A
24
+ * denial throws the structured `FS_SANDBOX_DENIED`.
25
+ *
26
+ * @module @deepseek-ai/dsh-fs-sandbox
27
+ */
28
+
29
+ import { Context } from '@deepseek-ai/cordis'
30
+ import { LocalFileSystem } from '@deepseek-ai/dsh-fs-local'
31
+ import type { Config as LocalConfig } from '@deepseek-ai/dsh-fs-local'
32
+ import { FsError } from '@deepseek-ai/dsh-fs'
33
+ import type { FsDeleteOutcome, FsMoveOutcome, FsEditOutcome, FsEditRequest, FsTarget, FsVersion, FsWriteIntent, FsWriteOutcome } from '@deepseek-ai/dsh-fs'
34
+ import { writableRoots } from '@deepseek-ai/dsh-sandbox'
35
+ import type { SandboxExecutionPolicy, SandboxMode } from '@deepseek-ai/dsh-sandbox'
36
+ import type {} from '@deepseek-ai/dsh-sandbox-policy'
37
+ import { isPathUnder } from './containment.ts'
38
+
39
+ /**
40
+ * Plugin config: the local backend's knobs verbatim (`cwd` resolution default
41
+ * and `diffBasisMaxBytes` overwrite-presentation bound). The sandbox default
42
+ * (mode + `workspace-write` fallback root) is NOT here — `ctx.sandboxPolicy`
43
+ * resolves each calling session for every enforcing capability.
44
+ */
45
+ export type Config = LocalConfig
46
+
47
+ /**
48
+ * Sandbox-enforcing filesystem backend. Registers as `ctx.fs` (loading it
49
+ * INSTEAD OF `dsh-fs-local`, together with a `ctx.sandboxPolicy`, is the whole
50
+ * swap — the model-facing tools are untouched). Its configured default mode is
51
+ * the capability fact exposed by {@link sandboxMode}; `dsh-tool-fs` resolves
52
+ * each session's mode and cwd into a policy for every mutation, while an
53
+ * approved escalation may stamp a strictly wider mode for one call.
54
+ */
55
+ export class SandboxedFileSystem extends LocalFileSystem {
56
+ static inject = ['sandboxPolicy']
57
+
58
+ private readonly defaultMode: SandboxMode
59
+ constructor(ctx: Context, config: Config) {
60
+ super(ctx, config)
61
+ this.defaultMode = ctx.sandboxPolicy.defaultMode
62
+ }
63
+
64
+ /** The deployment default mode — the capability fact the tool layer reads to advertise escalation. */
65
+ override get sandboxMode(): SandboxMode {
66
+ return this.defaultMode
67
+ }
68
+
69
+ /**
70
+ * Fence the write by the per-call policy, then delegate to the inherited
71
+ * atomic write. See {@link checkedTarget}.
72
+ * @param target - the resolved target to write.
73
+ * @param content - the full new file content.
74
+ * @param expected - the write intent guarding the write; omit for unconditional.
75
+ * @param signal - aborts before atomic publication takes effect.
76
+ * @param sandboxPolicy - the per-call mode and workspace root; omit to use
77
+ * the deployment fallback.
78
+ * @returns the write outcome from the inherited backend.
79
+ */
80
+ override async writeText(
81
+ target: FsTarget,
82
+ content: string,
83
+ expected?: FsWriteIntent,
84
+ signal?: AbortSignal,
85
+ sandboxPolicy?: SandboxExecutionPolicy,
86
+ ): Promise<FsWriteOutcome> {
87
+ return super.writeText(await this.checkedTarget(target, sandboxPolicy), content, expected, signal)
88
+ }
89
+
90
+ /**
91
+ * Fence the edit by the per-call policy, then delegate to the inherited
92
+ * atomic edit. See {@link checkedTarget}.
93
+ * @param target - the resolved target to edit.
94
+ * @param edit - the literal search/replace request.
95
+ * @param expected - the version guard; omit for an unconditional edit.
96
+ * @param signal - aborts before atomic publication takes effect.
97
+ * @param sandboxPolicy - the per-call mode and workspace root; omit to use
98
+ * the deployment fallback.
99
+ * @returns the edit outcome from the inherited backend.
100
+ */
101
+ override async editText(
102
+ target: FsTarget,
103
+ edit: FsEditRequest,
104
+ expected?: { version: FsVersion },
105
+ signal?: AbortSignal,
106
+ sandboxPolicy?: SandboxExecutionPolicy,
107
+ ): Promise<FsEditOutcome> {
108
+ return super.editText(await this.checkedTarget(target, sandboxPolicy), edit, expected, signal)
109
+ }
110
+
111
+ /**
112
+ * Fence the mutation before delegating to the local provider.
113
+ * @param sandboxPolicy - per-call mode and workspace root; omit for the deployment fallback.
114
+ */
115
+ override async patchText(
116
+ target: FsTarget, hunks: Parameters<LocalFileSystem['patchText']>[1], expected?: { version: FsVersion },
117
+ signal?: AbortSignal, sandboxPolicy?: SandboxExecutionPolicy,
118
+ ): Promise<FsEditOutcome> {
119
+ return super.patchText(await this.checkedTarget(target, sandboxPolicy), hunks, expected, signal)
120
+ }
121
+
122
+ /**
123
+ * Fence the mutation before delegating to the local provider.
124
+ * @param sandboxPolicy - per-call mode and workspace root; omit for the deployment fallback.
125
+ */
126
+ override async deleteFile(
127
+ target: FsTarget, expected?: { version: FsVersion }, signal?: AbortSignal, sandboxPolicy?: SandboxExecutionPolicy,
128
+ ): Promise<FsDeleteOutcome> {
129
+ return super.deleteFile(await this.checkedTarget(target, sandboxPolicy), expected, signal)
130
+ }
131
+
132
+ /**
133
+ * Fence the mutation before delegating to the local provider.
134
+ * @param sandboxPolicy - per-call mode and workspace root; omit for the deployment fallback.
135
+ */
136
+ override async moveFile(
137
+ source: FsTarget, destination: FsTarget, options: Parameters<LocalFileSystem['moveFile']>[2],
138
+ signal?: AbortSignal, sandboxPolicy?: SandboxExecutionPolicy,
139
+ ): Promise<FsMoveOutcome> {
140
+ const checkedSource = await this.checkedTarget(source, sandboxPolicy)
141
+ const checkedDestination = await this.checkedTarget(destination, sandboxPolicy)
142
+ return super.moveFile(checkedSource, checkedDestination, options, signal)
143
+ }
144
+
145
+ /**
146
+ * Enforce the per-call policy against `target` and return the EXACT target the
147
+ * mutation must use, so the checked identity is the mutated one (no
148
+ * check-here-write-there TOCTOU). `read-only` denies; `workspace-write`
149
+ * re-canonicalizes NOW (`resolve` realpaths the deepest existing ancestor,
150
+ * reflecting a concurrently swapped symlink), requires containment under a
151
+ * writable root, and returns THAT fresh target; `danger-full-access` returns
152
+ * the caller's target unfenced. Throws the structured `FS_SANDBOX_DENIED` on
153
+ * refusal — the tool layer maps it to the model-facing `[sandbox: …]` marker
154
+ * and the escalation hint.
155
+ */
156
+ private async checkedTarget(target: FsTarget, sandboxPolicy?: SandboxExecutionPolicy): Promise<FsTarget> {
157
+ const policy = sandboxPolicy ?? this.ctx.sandboxPolicy.resolve()
158
+ const { mode } = policy
159
+ if (mode === 'danger-full-access') return target
160
+ if (mode === 'read-only') {
161
+ throw new FsError(`cannot write "${target.displayPath}": file access denied under read-only mode`, 'FS_SANDBOX_DENIED')
162
+ }
163
+ // workspace-write: containment on the FRESH canonical path (catches a
164
+ // symlink ancestor swapped since the tool resolved this target), and the
165
+ // mutation delegates with THIS fresh target — never the stale one.
166
+ const fresh = await this.resolve(target.displayPath)
167
+ let contained = false
168
+ for (const root of writableRoots(policy)) {
169
+ if (await isPathUnder(fresh.targetKey, root)) {
170
+ contained = true
171
+ break
172
+ }
173
+ }
174
+ if (!contained) {
175
+ throw new FsError(`cannot write "${target.displayPath}": file access denied under workspace-write mode`, 'FS_SANDBOX_DENIED')
176
+ }
177
+ return fresh
178
+ }
179
+ }
180
+
181
+ export default SandboxedFileSystem