dsh-extra-paths 0.2.1 → 0.3.1
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.md +192 -28
- package/README.zh.md +165 -21
- package/lib/argv.js +96 -24
- package/lib/argv.js.map +1 -1
- package/lib/client.js +1175 -467
- package/lib/config.js +140 -44
- package/lib/config.js.map +1 -1
- package/lib/context.js +48 -12
- package/lib/context.js.map +1 -1
- package/lib/denial.js +132 -0
- package/lib/denial.js.map +1 -0
- package/lib/index.js +40 -5
- package/lib/index.js.map +1 -1
- package/lib/name-rule.js +217 -0
- package/lib/name-rule.js.map +1 -0
- package/lib/paths.js +155 -27
- package/lib/paths.js.map +1 -1
- package/lib/pattern.js +128 -62
- package/lib/pattern.js.map +1 -1
- package/lib/platform.js +325 -0
- package/lib/platform.js.map +1 -0
- package/lib/preview.js +47 -20
- package/lib/preview.js.map +1 -1
- package/lib/resolve.js +345 -64
- package/lib/resolve.js.map +1 -1
- package/lib/schema.js +19 -6
- package/lib/schema.js.map +1 -1
- package/lib/shell-dialect.js +92 -0
- package/lib/shell-dialect.js.map +1 -0
- package/lib/tool-guard.js +214 -0
- package/lib/tool-guard.js.map +1 -0
- package/lib/types/argv.d.ts +50 -12
- package/lib/types/config.d.ts +29 -13
- package/lib/types/context.d.ts +6 -1
- package/lib/types/denial.d.ts +88 -0
- package/lib/types/index.d.ts +8 -4
- package/lib/types/name-rule.d.ts +112 -0
- package/lib/types/paths.d.ts +55 -10
- package/lib/types/pattern.d.ts +43 -5
- package/lib/types/platform.d.ts +148 -0
- package/lib/types/preview.d.ts +39 -3
- package/lib/types/resolve.d.ts +129 -23
- package/lib/types/schema.d.ts +24 -8
- package/lib/types/shell-dialect.d.ts +60 -0
- package/lib/types/tool-guard.d.ts +103 -0
- package/lib/types/wrap-fs.d.ts +86 -4
- package/lib/types/wrap-sandbox.d.ts +26 -6
- package/lib/wrap-fs.js +323 -47
- package/lib/wrap-fs.js.map +1 -1
- package/lib/wrap-sandbox.js +149 -12
- package/lib/wrap-sandbox.js.map +1 -1
- package/package.json +1 -1
package/lib/types/resolve.d.ts
CHANGED
|
@@ -1,28 +1,98 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* The one place a configuration becomes
|
|
3
|
-
*
|
|
4
|
-
*
|
|
2
|
+
* The one place a configuration becomes the effective access rules. Enforcement
|
|
3
|
+
* (both wrappers) and the settings preview call this, so what the page shows and
|
|
4
|
+
* what the sandbox grants cannot drift.
|
|
5
|
+
*
|
|
6
|
+
* 0.3.0 widened the report from "writable roots" to four levels: `deny`, `read`,
|
|
7
|
+
* `write`, `always-write`. The aggregation is deliberately NOT "the most
|
|
8
|
+
* specific path wins": a narrower rule must never widen a broader grant by
|
|
9
|
+
* accident, so every rule that covers one canonical root is merged with the
|
|
10
|
+
* STRICTER level winning (`deny > read > write`), and `write`/`always-write`
|
|
11
|
+
* merge to the wider `always-write`. Surfaces are aggregated separately, which
|
|
12
|
+
* is what lets one root be writable for `fs` while the shell may only read it.
|
|
13
|
+
*
|
|
14
|
+
* A BARE NAME (`.git`, `.env`, `node_modules`) is a name rule: it matches that
|
|
15
|
+
* component at any depth under the session workspace plus every configured
|
|
16
|
+
* writable root (see {@link module:dsh-extra-paths/name-rule}). It is resolved
|
|
17
|
+
* in two deliberately different ways:
|
|
18
|
+
* - The report ALWAYS carries `nameRules` (lexical, no filesystem access), which
|
|
19
|
+
* is all the fs surface needs: it matches the target's components on every
|
|
20
|
+
* call, so it even covers a directory created a moment ago.
|
|
21
|
+
* - `nameMatches` — the concrete paths a shell profile must deny/grant — needs
|
|
22
|
+
* a bounded directory walk, so it is filled ONLY when the caller asks for it
|
|
23
|
+
* (`expandNameRules`). The fs hot path runs once per write and must not walk.
|
|
24
|
+
*
|
|
5
25
|
* @module dsh-extra-paths/resolve
|
|
6
26
|
*/
|
|
7
|
-
import type { ExtraPathsConfig, Scope, Surface } from './config.js';
|
|
8
|
-
|
|
27
|
+
import type { Access, ExtraPathsConfig, Scope, Surface } from './config.js';
|
|
28
|
+
import { type PlatformFacts } from './paths.js';
|
|
29
|
+
/** One effective writable root after canonicalisation and merging. */
|
|
9
30
|
export interface ResolvedRoot {
|
|
10
31
|
/** The configured spelling that first produced this root (for messages). */
|
|
11
32
|
readonly raw: string;
|
|
12
33
|
/** The canonical absolute root. */
|
|
13
34
|
readonly root: string;
|
|
14
|
-
/** The widest
|
|
35
|
+
/** The widest writable level across the surfaces that grant write. */
|
|
15
36
|
readonly scope: Scope;
|
|
16
|
-
/** The
|
|
37
|
+
/** The surfaces this root is writable on. */
|
|
38
|
+
readonly surfaces: readonly Surface[];
|
|
39
|
+
}
|
|
40
|
+
/** One effective restriction: the surfaces on which a root is read-only or denied. */
|
|
41
|
+
export interface ResolvedRule {
|
|
42
|
+
/** The configured spelling that first produced this root (for messages). */
|
|
43
|
+
readonly raw: string;
|
|
44
|
+
/** The canonical absolute root. */
|
|
45
|
+
readonly root: string;
|
|
46
|
+
/** `read` keeps writes out; `deny` keeps reads AND writes out. */
|
|
47
|
+
readonly access: 'read' | 'deny';
|
|
48
|
+
/** The surfaces the restriction applies to. */
|
|
17
49
|
readonly surfaces: readonly Surface[];
|
|
18
50
|
}
|
|
19
51
|
/** Per-entry outcome, in configured order (what the settings preview renders). */
|
|
20
52
|
export interface EntryResolution {
|
|
21
53
|
readonly raw: string;
|
|
22
54
|
readonly resolved: string;
|
|
23
|
-
readonly
|
|
55
|
+
readonly access: Access;
|
|
24
56
|
readonly surfaces: readonly Surface[];
|
|
25
|
-
readonly status: 'ok' | 'missing' | 'rejected';
|
|
57
|
+
readonly status: 'ok' | 'missing' | 'rejected' | 'foreign';
|
|
58
|
+
/**
|
|
59
|
+
* How many concrete paths a NAME RULE currently matches (the paths actually
|
|
60
|
+
* enforced on the shell surface). Present only when the caller asked for
|
|
61
|
+
* `expandNameRules`; a partially walked rule reports the matches it FOUND and
|
|
62
|
+
* carries a warning about the roots it could not finish. A name rule with no
|
|
63
|
+
* `matches` at all is still live for the fs surface: it needs no walk.
|
|
64
|
+
*/
|
|
65
|
+
readonly matches?: number;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* One configured BARE NAME (a "name rule"): a single-component spelling such as
|
|
69
|
+
* `.git` or `.env`. It matches that component at any depth under the session
|
|
70
|
+
* workspace plus every configured writable root, and covers the matched path's
|
|
71
|
+
* whole subtree.
|
|
72
|
+
*/
|
|
73
|
+
export interface ResolvedNameRule {
|
|
74
|
+
/** The configured spelling, kept for messages (no path form exists). */
|
|
75
|
+
readonly raw: string;
|
|
76
|
+
/** The component name the matchers compare against (case-folded on Windows). */
|
|
77
|
+
readonly name: string;
|
|
78
|
+
readonly access: Access;
|
|
79
|
+
readonly surfaces: readonly Surface[];
|
|
80
|
+
}
|
|
81
|
+
/** One concrete path a name rule currently matches (only produced by an expansion). */
|
|
82
|
+
export interface NameMatch {
|
|
83
|
+
readonly rule: ResolvedNameRule;
|
|
84
|
+
/** The matched path; `rule` covers this path and its whole subtree. */
|
|
85
|
+
readonly root: string;
|
|
86
|
+
}
|
|
87
|
+
/** Optional knobs of one resolution pass. */
|
|
88
|
+
export interface ResolveOptions {
|
|
89
|
+
/**
|
|
90
|
+
* Enumerate concrete paths for name rules (`nameMatches`). Default false: the
|
|
91
|
+
* fs wrapper resolves on EVERY read/write and matches names lexically, so it
|
|
92
|
+
* must not pay for a directory walk; the shell surface (a sandbox profile can
|
|
93
|
+
* only name concrete paths) and the settings preview pass true.
|
|
94
|
+
*/
|
|
95
|
+
readonly expandNameRules?: boolean;
|
|
26
96
|
}
|
|
27
97
|
/**
|
|
28
98
|
* One resolution pass.
|
|
@@ -31,33 +101,69 @@ export interface EntryResolution {
|
|
|
31
101
|
* - **Re-resolve per decision, never cache.** The result depends on the
|
|
32
102
|
* filesystem's CURRENT state (existence) and on `$HOME`, so a cached report
|
|
33
103
|
* would freeze an entry inactive — or worse, keep granting a root that moved.
|
|
34
|
-
* The one deliberate exception is a pattern's
|
|
35
|
-
*
|
|
36
|
-
* would be wasteful; a literal entry is never cached.
|
|
37
|
-
* - **Grants are a union, and
|
|
38
|
-
* kept: a narrower child cannot narrow a broader parent,
|
|
39
|
-
*
|
|
40
|
-
*
|
|
104
|
+
* The one deliberate exception is a directory EXPANSION (a pattern's, or a
|
|
105
|
+
* name rule's concrete matches), which is cached for its TTL because walking
|
|
106
|
+
* a tree on every write would be wasteful; a literal entry is never cached.
|
|
107
|
+
* - **Grants are a union, and every rule is a hard rule.** Nested roots are both
|
|
108
|
+
* kept: a narrower child cannot narrow a broader parent, and a `read`/`deny`
|
|
109
|
+
* rule is never overridden by a broader writable one — the strictest level
|
|
110
|
+
* covering the same canonical root wins per surface.
|
|
41
111
|
* - A root that exists but is not a directory is still reported `ok`; writes
|
|
42
112
|
* beneath it fail at the filesystem layer (ENOTDIR), not here.
|
|
113
|
+
* - A name rule is always a hard fs rule (matched lexically per call). Its shell
|
|
114
|
+
* form is the bounded enumeration in `nameMatches`; a partial enumeration
|
|
115
|
+
* keeps the found paths (denying more is safe) and warns about the roots it
|
|
116
|
+
* could not finish, instead of discarding protection for the whole rule.
|
|
43
117
|
*/
|
|
44
118
|
export interface ResolveReport {
|
|
119
|
+
/** Writable grants only; `grantFor` and the prompt section consume these. */
|
|
45
120
|
readonly roots: readonly ResolvedRoot[];
|
|
121
|
+
/** `read`: readable but not writable. */
|
|
122
|
+
readonly readOnly: readonly ResolvedRule[];
|
|
123
|
+
/** `deny`: neither readable nor writable. */
|
|
124
|
+
readonly denied: readonly ResolvedRule[];
|
|
46
125
|
readonly entries: readonly EntryResolution[];
|
|
126
|
+
/** Every configured name rule, in configured order (deduplicated by name). */
|
|
127
|
+
readonly nameRules: readonly ResolvedNameRule[];
|
|
128
|
+
/**
|
|
129
|
+
* Concrete paths the name rules match. EMPTY unless `expandNameRules` was
|
|
130
|
+
* requested. A rule whose walk was only PARTIAL contributes the matches it did
|
|
131
|
+
* find and adds a warning naming the roots it could not finish: dropping them
|
|
132
|
+
* would remove protection from the very tree the user configured.
|
|
133
|
+
*/
|
|
134
|
+
readonly nameMatches: readonly NameMatch[];
|
|
47
135
|
readonly warnings: readonly string[];
|
|
48
136
|
}
|
|
49
137
|
/**
|
|
50
|
-
*
|
|
51
|
-
*
|
|
52
|
-
*
|
|
138
|
+
* Drop every cached expansion (a fresh directory should not wait out the TTL).
|
|
139
|
+
* Name-rule walks go too: tests use this as the one "forget the walk caches"
|
|
140
|
+
* hook, and a stale name list is the same class of surprise as a stale pattern
|
|
141
|
+
* list.
|
|
53
142
|
*/
|
|
54
|
-
export declare const PATTERN_CACHE_MS = 30000;
|
|
55
|
-
/** Drop every cached expansion (a fresh directory should not wait out the TTL). */
|
|
56
143
|
export declare function clearPatternCache(): void;
|
|
144
|
+
/** Drop only the cached name-rule walks (the fs hot path never fills them). */
|
|
145
|
+
export declare function clearNameRuleCache(): void;
|
|
57
146
|
/**
|
|
58
147
|
* Resolve every configured entry against one session workspace.
|
|
59
148
|
* @param config - the normalised configuration.
|
|
60
149
|
* @param workspaceRoot - the session workspace a relative entry resolves against.
|
|
61
|
-
* @
|
|
150
|
+
* @param facts - host facts (defaults to the discovered ones).
|
|
151
|
+
* @param options - pass `{ expandNameRules: true }` to fill `nameMatches` with
|
|
152
|
+
* concrete paths (shell surface and preview); the default leaves the walk out.
|
|
153
|
+
* @returns effective rules, per-entry detail and warnings.
|
|
154
|
+
*/
|
|
155
|
+
export declare function resolveRoots(config: ExtraPathsConfig, workspaceRoot: string, facts?: PlatformFacts, options?: ResolveOptions): ResolveReport;
|
|
156
|
+
/**
|
|
157
|
+
* The walk scope of the name rules in one report: the session workspace plus
|
|
158
|
+
* every configured writable root, nested roots collapsed.
|
|
159
|
+
*
|
|
160
|
+
* Exported because the fs wrapper resolves a report WITHOUT an expansion (its
|
|
161
|
+
* per-call test is lexical) but must still use the SAME scope the shell
|
|
162
|
+
* expansion and the preview used — two scope definitions would be a way for one
|
|
163
|
+
* rule to apply on one surface but not the other.
|
|
164
|
+
* @param report - a report from {@link resolveRoots}, expanded or not.
|
|
165
|
+
* @param workspaceRoot - the session workspace the report was resolved against.
|
|
166
|
+
* @param facts - host facts.
|
|
167
|
+
* @returns scope roots in walk order (workspace first).
|
|
62
168
|
*/
|
|
63
|
-
export declare function
|
|
169
|
+
export declare function nameScopeRoots(report: ResolveReport, workspaceRoot: string, facts: PlatformFacts): readonly string[];
|
package/lib/types/schema.d.ts
CHANGED
|
@@ -2,18 +2,32 @@
|
|
|
2
2
|
* The settings namespace schema. The resolved section is what the enforcement
|
|
3
3
|
* layers read; `base` (the entry config) supplies deployment defaults beneath
|
|
4
4
|
* the user layer.
|
|
5
|
+
*
|
|
6
|
+
* 0.3.0 adds `defaultAccess` and a per-entry `access` while KEEPING the legacy
|
|
7
|
+
* `defaultScope` / `scope` spellings, so an older settings document keeps
|
|
8
|
+
* working. Neither access alias carries a schema default: the 0.2.x framework
|
|
9
|
+
* MATERIALISES schema defaults before the plugin sees the section, so a
|
|
10
|
+
* `defaultAccess` default of `write` would appear alongside a legacy
|
|
11
|
+
* `defaultScope: always-write` and `normalizeConfig`'s ambiguity guard would
|
|
12
|
+
* reject the whole configuration. `normalizeConfig` supplies the documented
|
|
13
|
+
* `write` default itself, which is unambiguous.
|
|
5
14
|
* @module dsh-extra-paths/schema
|
|
6
15
|
*/
|
|
7
16
|
import z from '@deepseek-ai/schemastery';
|
|
8
|
-
import type { Scope, Surface } from './config.js';
|
|
17
|
+
import type { Access, Scope, Surface } from './config.js';
|
|
9
18
|
/** One configured path as stored in the settings document. */
|
|
10
19
|
export interface ExtraPathsSectionEntry {
|
|
11
20
|
path: string;
|
|
21
|
+
/** New spelling; conflicts with `scope` when both are present and differ. */
|
|
22
|
+
access?: Access;
|
|
23
|
+
/** Legacy alias for `access`; still accepted from 0.2.x documents. */
|
|
12
24
|
scope?: Scope;
|
|
13
25
|
surfaces?: Surface[];
|
|
14
26
|
}
|
|
15
|
-
/** The `extra-paths` settings section as stored (the schema supplies
|
|
27
|
+
/** The `extra-paths` settings section as stored (the schema supplies defaults). */
|
|
16
28
|
export interface ExtraPathsSection {
|
|
29
|
+
defaultAccess?: Access;
|
|
30
|
+
/** Legacy alias for `defaultAccess`; still accepted from 0.2.x documents. */
|
|
17
31
|
defaultScope?: Scope;
|
|
18
32
|
defaultSurfaces?: Surface[];
|
|
19
33
|
paths?: (string | ExtraPathsSectionEntry)[];
|
|
@@ -23,20 +37,22 @@ export declare const extraPathsSchema: z<ExtraPathsSection>;
|
|
|
23
37
|
/**
|
|
24
38
|
* The same section for 0.2.x, where a plugin's settings ARE its exported
|
|
25
39
|
* `Config` and the framework's settings form only describes `volatile()` fields:
|
|
26
|
-
* a schema without one yields no descriptor at all, so
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
*
|
|
40
|
+
* a schema without one yields no descriptor at all, so every field is volatile
|
|
41
|
+
* here. `volatile()` wraps a field in a value holder (`get()`), and the host half
|
|
42
|
+
* re-reads those per enforcement decision — that is what makes an edit in the
|
|
43
|
+
* settings form apply without a restart.
|
|
30
44
|
*
|
|
31
45
|
* Kept separate from {@link extraPathsSchema} because the 0.1.5 settings service
|
|
32
46
|
* consumes plain values, not volatile wrappers.
|
|
33
47
|
*/
|
|
34
48
|
export declare const extraPathsConfigSchema: z<Schemastery.ObjectS<NoInfer<{
|
|
35
|
-
|
|
49
|
+
defaultAccess: z<Access>;
|
|
50
|
+
defaultScope: z<Scope>;
|
|
36
51
|
defaultSurfaces: z<Surface[], Surface[], "defined">;
|
|
37
52
|
paths: z<(string | ExtraPathsSectionEntry)[], (string | ExtraPathsSectionEntry)[], "defined">;
|
|
38
53
|
}>>, Schemastery.ObjectT<NoInfer<{
|
|
39
|
-
|
|
54
|
+
defaultAccess: z<Access>;
|
|
55
|
+
defaultScope: z<Scope>;
|
|
40
56
|
defaultSurfaces: z<Surface[], Surface[], "defined">;
|
|
41
57
|
paths: z<(string | ExtraPathsSectionEntry)[], (string | ExtraPathsSectionEntry)[], "defined">;
|
|
42
58
|
}>>, "plain">;
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Which shell-sandbox dialect the host actually uses, and what that dialect can
|
|
3
|
+
* express. The plugin cannot know the runner from configuration — `sandbox-local`
|
|
4
|
+
* picks it per platform and per functional probe — so the wrapper records the
|
|
5
|
+
* runner of every `confine()` it observes, and the settings page reports the
|
|
6
|
+
* resulting capability matrix.
|
|
7
|
+
*
|
|
8
|
+
* Enforcement capability differs sharply per dialect, and pretending otherwise
|
|
9
|
+
* would be a security bug:
|
|
10
|
+
* - seatbelt (macOS): trailing SBPL rules win, so deny-read / deny-write /
|
|
11
|
+
* read-only are all expressible by APPENDING rules;
|
|
12
|
+
* - bwrap (Linux): later mounts override earlier ones, so `--mask` and
|
|
13
|
+
* `--ro-bind` are expressible by APPENDING mount ops;
|
|
14
|
+
* - landlock (Linux fallback): a pure allow-list (`readOnly: ['/']`); a subpath
|
|
15
|
+
* cannot be subtracted, so NONE of the restrictions are expressible;
|
|
16
|
+
* - windows-acl: a write-restricted token; reads are not restricted at all and
|
|
17
|
+
* unknown runner flags abort the command, so NONE are expressible.
|
|
18
|
+
* @module dsh-extra-paths/shell-dialect
|
|
19
|
+
*/
|
|
20
|
+
/** The shell-sandbox runner dialect. */
|
|
21
|
+
export type ShellDialect = 'seatbelt' | 'bwrap' | 'landlock' | 'windows-acl' | 'unknown';
|
|
22
|
+
/** What the current host can enforce on the SHELL surface. */
|
|
23
|
+
export interface ShellCapability {
|
|
24
|
+
/** Shell reads can be denied for a configured path. */
|
|
25
|
+
readonly shellReadDeny: boolean;
|
|
26
|
+
/** Shell writes can be denied for a configured path (deny and read levels). */
|
|
27
|
+
readonly shellWriteDeny: boolean;
|
|
28
|
+
/** A read-only (`read` access) rule can keep shell writes out of the path. */
|
|
29
|
+
readonly shellReadOnly: boolean;
|
|
30
|
+
/** The dialect the decision is based on (`unknown` before the first confinement). */
|
|
31
|
+
readonly shellDialect: ShellDialect;
|
|
32
|
+
}
|
|
33
|
+
/** Reset the observation (tests and a fresh plugin load). */
|
|
34
|
+
export declare function resetObservedShellDialect(): void;
|
|
35
|
+
/** The dialect last observed on a real confinement. */
|
|
36
|
+
export declare function observedShellDialect(): ShellDialect;
|
|
37
|
+
/**
|
|
38
|
+
* Classify a confined argv by the runner that produced it. Detection is
|
|
39
|
+
* structural (the same shape `widenArgv` relies on), never a name grep on the
|
|
40
|
+
* user's command: the head is whatever the host prepended.
|
|
41
|
+
* @param argv - the confined argv the sandbox provider returned.
|
|
42
|
+
* @returns the runner dialect, or `unknown` for an unrecognised shape.
|
|
43
|
+
*/
|
|
44
|
+
export declare function classifyRunner(argv: readonly string[]): ShellDialect;
|
|
45
|
+
/**
|
|
46
|
+
* Record the dialect of one confinement (last observation wins).
|
|
47
|
+
* @param argv - the confined argv the sandbox provider returned.
|
|
48
|
+
* @returns the classified dialect, for the caller's warning paths.
|
|
49
|
+
*/
|
|
50
|
+
export declare function recordShellDialect(argv: readonly string[]): ShellDialect;
|
|
51
|
+
/**
|
|
52
|
+
* The capability matrix for one platform, refined by the observed dialect.
|
|
53
|
+
* Before the first confinement a Linux host cannot be classified yet, so the
|
|
54
|
+
* conservative answer is used and the page says "unknown" rather than promising
|
|
55
|
+
* an enforcement it may not have.
|
|
56
|
+
* @param platform - `process.platform`.
|
|
57
|
+
* @param dialect - the observed dialect, or undefined to use the observation.
|
|
58
|
+
* @returns what the shell surface can enforce.
|
|
59
|
+
*/
|
|
60
|
+
export declare function shellCapabilityOf(platform: string, dialect?: ShellDialect): ShellCapability;
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The tool-layer deny guard: the second, honest seam beside the fs proxy.
|
|
3
|
+
*
|
|
4
|
+
* Some host tools read files WITHOUT going through `ctx.fs`:
|
|
5
|
+
* `grep`/`glob` spawn the host's bundled ripgrep, MCP/LSP children have their own
|
|
6
|
+
* filesystem access, and a service that cached `ctx.fs` at load time keeps the
|
|
7
|
+
* unwrapped instance. Those paths are documented boundaries (§11 of the 0.3.0
|
|
8
|
+
* plan) — the plugin does not pretend to intercept them. What it CAN do is ask
|
|
9
|
+
* the tool runtime to refuse a call whose own arguments name a denied path:
|
|
10
|
+
* `ctx.tools.guard((exec) => reason | undefined)` runs synchronously before
|
|
11
|
+
* dispatch and a returned string becomes `Error: <reason>` for the model. This
|
|
12
|
+
* reliably catches `grep`/`glob` with an explicit `path`; a default workspace
|
|
13
|
+
* scan can still surface a denied SUBdirectory (documented, not silently denied).
|
|
14
|
+
*
|
|
15
|
+
* The guard is deliberately conservative: it only refuses on a positive
|
|
16
|
+
* containment match, and any failure to resolve a candidate means "no opinion"
|
|
17
|
+
* (never throw from a guard — a bug in the plugin must not brick every tool).
|
|
18
|
+
*
|
|
19
|
+
* NAME RULES (`.git`, `.env`) apply here too: a tool call whose own argument
|
|
20
|
+
* names a path containing a denied name component is refused exactly like a
|
|
21
|
+
* literal denial. The lexical test is shared with the fs wrapper
|
|
22
|
+
* ({@link matchNameRule}) so a tool guard and the fs proxy cannot disagree about
|
|
23
|
+
* what a bare name covers; the walk the shell surface needs is never performed
|
|
24
|
+
* here.
|
|
25
|
+
* @module dsh-extra-paths/tool-guard
|
|
26
|
+
*/
|
|
27
|
+
import type { ExtraPathsConfig } from './config.js';
|
|
28
|
+
/**
|
|
29
|
+
* The fields of one tool execution this guard reads. A structural subset of the
|
|
30
|
+
* host's `ToolExecution` (name/arguments/agent) so this module imports no
|
|
31
|
+
* `@deepseek-ai/*` value and stays testable without a runtime.
|
|
32
|
+
*/
|
|
33
|
+
export interface ToolExecutionLike {
|
|
34
|
+
/** The tool being dispatched (`grep`, `glob`, `read`, `bash`, …). */
|
|
35
|
+
readonly name?: string;
|
|
36
|
+
/** The raw, untrusted tool arguments. */
|
|
37
|
+
readonly arguments?: unknown;
|
|
38
|
+
/** The owning agent, whose session header carries the workspace cwd. */
|
|
39
|
+
readonly agent?: {
|
|
40
|
+
readonly session?: {
|
|
41
|
+
readonly header?: {
|
|
42
|
+
readonly cwd?: string;
|
|
43
|
+
};
|
|
44
|
+
};
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
/** What the guard needs from the plugin. */
|
|
48
|
+
export interface ToolGuardDeps {
|
|
49
|
+
readConfig(): ExtraPathsConfig;
|
|
50
|
+
warn(message: string): void;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Collect the candidate path strings of one tool call.
|
|
54
|
+
*
|
|
55
|
+
* Strings and string arrays under one of {@link PATH_KEYS} are candidates; the
|
|
56
|
+
* walk descends at most {@link MAX_ARGUMENT_DEPTH} levels so batch shapes such as
|
|
57
|
+
* `files: [{ path }]` or `targets: [{ file_path }]` are covered without turning
|
|
58
|
+
* into a full recursive scan of every tool's payload. Duplicates are removed so
|
|
59
|
+
* one path named twice resolves once.
|
|
60
|
+
* @param args - the raw `execution.arguments`.
|
|
61
|
+
* @returns the deduplicated candidate spellings, in argument order.
|
|
62
|
+
*/
|
|
63
|
+
export declare function extractPathCandidates(args: unknown): readonly string[];
|
|
64
|
+
/**
|
|
65
|
+
* The potential refusal reason for one tool execution.
|
|
66
|
+
*
|
|
67
|
+
* Pure and synchronous (the host's guard contract): resolve the calling
|
|
68
|
+
* session's cwd, normalise every candidate path the way a configured entry is
|
|
69
|
+
* normalised, and refuse when a `deny` rule covers it — literally or as a NAME
|
|
70
|
+
* rule on one of the path's components (the candidate's fs-surface equivalent of
|
|
71
|
+
* `repo/.git/config` under a `.git | deny` rule). `read` rules do NOT deny a tool
|
|
72
|
+
* call — they only stop writes, which the fs proxy already enforces; a
|
|
73
|
+
* parameter-level guard cannot tell whether a tool will read or write.
|
|
74
|
+
* @param execution - the tool call about to be dispatched.
|
|
75
|
+
* @param deps - live configuration source.
|
|
76
|
+
* @returns the denial reason for the tool layer, or undefined for "no opinion".
|
|
77
|
+
*/
|
|
78
|
+
export declare function guardReason(execution: ToolExecutionLike, deps: ToolGuardDeps): string | undefined;
|
|
79
|
+
/** The host tools-service surface this module needs (feature-detected, never imported). */
|
|
80
|
+
export interface ToolServiceLike {
|
|
81
|
+
/** The synchronous deny registration seam (`@deepseek-ai/dsh-tools`). */
|
|
82
|
+
guard?(guard: (execution: Readonly<ToolExecutionLike>) => string | undefined): unknown;
|
|
83
|
+
}
|
|
84
|
+
/** The context shape `src/index.ts` can pass (structurally a cordis entry context). */
|
|
85
|
+
export interface ToolGuardInstallContext {
|
|
86
|
+
/** Cordis injection: runs `callback` once the `tools` service is available. */
|
|
87
|
+
inject?(names: readonly string[], callback: (scope: unknown) => void): unknown;
|
|
88
|
+
/** Already-available service (tests / an entry that injected directly). */
|
|
89
|
+
tools?: ToolServiceLike;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Register the deny guard on the tools service, feature-detected.
|
|
93
|
+
*
|
|
94
|
+
* Registration goes through `ctx.inject(['tools'], …)` because the tool runtime
|
|
95
|
+
* may not be composed yet when the plugin applies — and because an older host
|
|
96
|
+
* generation may not have `guard` at all, in which case the plugin silently
|
|
97
|
+
* keeps the fs-only enforcement instead of failing to load.
|
|
98
|
+
* @param ctx - the plugin/entry context.
|
|
99
|
+
* @param deps - live configuration source.
|
|
100
|
+
* @returns the disposer that unregisters the guard (no-op when nothing was
|
|
101
|
+
* registered).
|
|
102
|
+
*/
|
|
103
|
+
export declare function installToolGuard(ctx: ToolGuardInstallContext, deps: ToolGuardDeps): () => void;
|
package/lib/types/wrap-fs.d.ts
CHANGED
|
@@ -1,6 +1,22 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* The filesystem half of the plugin: a transparent Proxy over `ctx.fs` that
|
|
3
|
-
*
|
|
3
|
+
*
|
|
4
|
+
* 1. RE-FENCES every mutation whose target lies under a configured writable root
|
|
5
|
+
* (re-stamps ONE call's policy), and
|
|
6
|
+
* 2. ENFORCES the hard rules — a `deny` rule refuses content reads, listings,
|
|
7
|
+
* watching AND writes; a `read` rule refuses writes (a downgrade) even when the
|
|
8
|
+
* target is inside the writable workspace. Hard rules are user-written, so
|
|
9
|
+
* they hold under `danger-full-access` too.
|
|
10
|
+
*
|
|
11
|
+
* BARE NAMES (`.git`, `.env`, `node_modules`) are NAME RULES: they apply to that
|
|
12
|
+
* component at any depth under the session workspace plus every configured
|
|
13
|
+
* writable root. The fs surface matches them LEXICALLY on every call
|
|
14
|
+
* ({@link matchNameRule} over the target's components), never by walking the
|
|
15
|
+
* filesystem: a write may run once per tool call, so the enforcement layer must
|
|
16
|
+
* not pay for a directory tree and must also catch a directory created a moment
|
|
17
|
+
* ago. The shell surface is the only caller of the expensive bounded walk
|
|
18
|
+
* (`resolve.ts` with `expandNameRules`), because a sandbox profile can only name
|
|
19
|
+
* concrete paths.
|
|
4
20
|
*
|
|
5
21
|
* The host generation decides how many mutations this surface has. 0.2.0/0.2.1
|
|
6
22
|
* fence `writeText`/`editText`; 0.2.2 additionally fences `patchText`,
|
|
@@ -9,6 +25,13 @@
|
|
|
9
25
|
* actually exposes, and never invents one: on a 0.2.1 service the three newer
|
|
10
26
|
* methods stay absent, exactly as the base class declared them.
|
|
11
27
|
*
|
|
28
|
+
* Reads are guarded on the six content/listing/watch entry points
|
|
29
|
+
* (`readText`, `streamText`, `readBytes`, `readByteRange`, `listDir`, `watch`).
|
|
30
|
+
* The documented metadata boundary is deliberate: `stat`, `lstat`, `resolve`,
|
|
31
|
+
* `contains`, `processPath` and `fileUrl` expose existence/size/identity without
|
|
32
|
+
* reading content, and the host itself relies on them to probe paths — denying
|
|
33
|
+
* them would break the tool layer without protecting any content.
|
|
34
|
+
*
|
|
12
35
|
* Transparency rules (verified against cordis 4.0.2 by
|
|
13
36
|
* spike/cordis-get-wrap-baseline.mjs): forward every property with the inner
|
|
14
37
|
* value as the receiver, bind methods to the inner instance — except
|
|
@@ -16,19 +39,43 @@
|
|
|
16
39
|
* (`Service[Symbol.hasInstance]` walks `instance.constructor`).
|
|
17
40
|
* @module dsh-extra-paths/wrap-fs
|
|
18
41
|
*/
|
|
19
|
-
import type { ExtraPathsConfig } from './config.js';
|
|
42
|
+
import type { ExtraPathsConfig, Surface } from './config.js';
|
|
43
|
+
import { type PlatformFacts } from './paths.js';
|
|
20
44
|
import { type PolicyLike } from './policy.js';
|
|
45
|
+
import { type ResolvedNameRule, type ResolvedRule } from './resolve.js';
|
|
21
46
|
/** A resolved filesystem target (the fs service's own shape). */
|
|
22
47
|
export interface FsTargetLike {
|
|
23
48
|
readonly targetKey: unknown;
|
|
24
49
|
readonly displayPath: string;
|
|
25
50
|
}
|
|
26
|
-
/** The
|
|
51
|
+
/** The read methods whose CONTENT/listing the deny rules must intercept. */
|
|
52
|
+
export type FsReadMethod = 'readText' | 'streamText' | 'readBytes' | 'readByteRange' | 'listDir' | 'watch';
|
|
53
|
+
/**
|
|
54
|
+
* The subset of the filesystem service this wrapper intercepts.
|
|
55
|
+
*
|
|
56
|
+
* The read methods are optional: a stub or an older host that does not declare
|
|
57
|
+
* one keeps that exact surface (the wrapper only overrides what exists).
|
|
58
|
+
*/
|
|
27
59
|
export interface FsInnerLike {
|
|
28
60
|
resolve(path: string, opts?: {
|
|
29
61
|
cwd?: string;
|
|
30
62
|
signal?: AbortSignal;
|
|
31
63
|
}): Promise<FsTargetLike>;
|
|
64
|
+
/** DSH 0.2.x: whole-text read. */
|
|
65
|
+
readText?(target: FsTargetLike, signal?: AbortSignal): Promise<unknown>;
|
|
66
|
+
/** DSH 0.2.x: chunked text read. */
|
|
67
|
+
streamText?(target: FsTargetLike, signal?: AbortSignal): Promise<unknown>;
|
|
68
|
+
/** DSH 0.2.x: raw byte read. */
|
|
69
|
+
readBytes?(target: FsTargetLike, signal: AbortSignal | undefined, maxBytes: number): Promise<unknown>;
|
|
70
|
+
/** DSH 0.2.x: one byte window. */
|
|
71
|
+
readByteRange?(target: FsTargetLike, range: {
|
|
72
|
+
offset: number;
|
|
73
|
+
length: number;
|
|
74
|
+
}, signal?: AbortSignal): Promise<unknown>;
|
|
75
|
+
/** DSH 0.2.x: directory listing. */
|
|
76
|
+
listDir?(target: FsTargetLike, signal?: AbortSignal): Promise<unknown>;
|
|
77
|
+
/** DSH 0.2.x: change observation. */
|
|
78
|
+
watch?(target: FsTargetLike, changed: (error?: Error) => void, signal: AbortSignal): Promise<unknown>;
|
|
32
79
|
writeText(target: FsTargetLike, content: string, expected?: unknown, signal?: AbortSignal, policy?: PolicyLike): Promise<unknown>;
|
|
33
80
|
editText(target: FsTargetLike, edit: unknown, expected?: unknown, signal?: AbortSignal, policy?: PolicyLike): Promise<unknown>;
|
|
34
81
|
/** DSH 0.2.2+: apply parsed hunks (the `apply_patch` tool's surface). */
|
|
@@ -46,6 +93,40 @@ export interface FsWrapDeps {
|
|
|
46
93
|
/** Deployment policy for a caller that stamped none; resolved per call. */
|
|
47
94
|
fallbackPolicy(): PolicyLike | undefined;
|
|
48
95
|
}
|
|
96
|
+
/**
|
|
97
|
+
* One name rule that covers a target, plus the component path it matched (the
|
|
98
|
+
* rule's effective root, which is what a message and a synthetic policy name).
|
|
99
|
+
*/
|
|
100
|
+
export interface NameRuleMatch {
|
|
101
|
+
/** The configured name rule that matched. */
|
|
102
|
+
readonly rule: ResolvedNameRule;
|
|
103
|
+
/** The canonical matched component path; the rule covers this path and its subtree. */
|
|
104
|
+
readonly root: string;
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* The first name rule in `rules` that covers `targetKey` on `surface`.
|
|
108
|
+
*
|
|
109
|
+
* This is the ONE lexical name-rule test both enforcement surfaces share (the
|
|
110
|
+
* fs wrapper per call and the tool-layer guard per tool execution): a second
|
|
111
|
+
* implementation could let one surface's scope drift from the other. Callers pass
|
|
112
|
+
* the rule list prefiltered by access level (deny, read, or the grant set), so
|
|
113
|
+
* one rule list maps to one decision.
|
|
114
|
+
* @param rules - name rules to test, already filtered by access and intent.
|
|
115
|
+
* @param surface - the enforcement surface asking.
|
|
116
|
+
* @param targetKey - the fresh canonical target path (may name a missing suffix).
|
|
117
|
+
* @param scopeRoots - {@link nameScopeRoots} output for the same report.
|
|
118
|
+
* @param facts - host facts.
|
|
119
|
+
* @returns the matched rule and component path, or undefined (no opinion).
|
|
120
|
+
*/
|
|
121
|
+
export declare function matchNameRule(rules: readonly ResolvedNameRule[], surface: Surface, targetKey: string, scopeRoots: readonly string[], facts: PlatformFacts): NameRuleMatch | undefined;
|
|
122
|
+
/**
|
|
123
|
+
* A rule-shaped view of a name-rule hit, so `denial.ts`'s message helpers apply
|
|
124
|
+
* to name rules unchanged. Callers pass only deny/read rule lists (a grant is
|
|
125
|
+
* never a refusal, and `write` maps to the `read` message level).
|
|
126
|
+
* @param hit - the matched name rule and component path.
|
|
127
|
+
* @returns the `ResolvedRule`-like value the message builders take.
|
|
128
|
+
*/
|
|
129
|
+
export declare function nameRuleHitAsResolvedRule(hit: NameRuleMatch): ResolvedRule;
|
|
49
130
|
/**
|
|
50
131
|
* Whether a service is the sandboxed filesystem: mutating methods plus the
|
|
51
132
|
* capability fact (`sandboxMode`) that only a confining backend exposes.
|
|
@@ -58,6 +139,7 @@ export declare function isConfiningFs(value: unknown): value is FsInnerLike;
|
|
|
58
139
|
* Wrap one filesystem service.
|
|
59
140
|
* @param inner - the service instance read from the context.
|
|
60
141
|
* @param deps - configuration and policy sources.
|
|
61
|
-
* @returns a transparent proxy that
|
|
142
|
+
* @returns a transparent proxy that refuses hard-rule violations and grants
|
|
143
|
+
* configured writable roots on mutation.
|
|
62
144
|
*/
|
|
63
145
|
export declare function wrapFs(inner: FsInnerLike, deps: FsWrapDeps): FsInnerLike;
|
|
@@ -1,8 +1,18 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* The subprocess half of the plugin: a transparent Proxy over `ctx.sandbox`
|
|
3
|
-
* that appends
|
|
4
|
-
*
|
|
5
|
-
*
|
|
3
|
+
* that appends the configured shell-surface rules to the argv the base provider
|
|
4
|
+
* already wrapped — write grants AND the deny/read-only restrictions the runner
|
|
5
|
+
* dialect can express (see {@link widenArgv}).
|
|
6
|
+
*
|
|
7
|
+
* A sandbox profile can only name CONCRETE paths, so a BARE NAME rule
|
|
8
|
+
* (`.git`, `.env`, `node_modules`) is deliberately NOT enforced here: expanding
|
|
9
|
+
* a name into "the paths it currently matches" means walking the workspace plus
|
|
10
|
+
* every writable root before commands — costly, stale by up to the cache TTL,
|
|
11
|
+
* and silently partial when one root is deep or unreadable (the real host hit
|
|
12
|
+
* exactly that). Name rules therefore protect the fs tools only, where the
|
|
13
|
+
* match is lexical, exact and live on every call. A configured `shell` surface
|
|
14
|
+
* on a name rule gets an explicit one-time notice (see
|
|
15
|
+
* {@link warnNameRulesAreFsOnly}) instead of a stale or partial mask set.
|
|
6
16
|
*
|
|
7
17
|
* THE WRAPPER MIRRORS THE INNER PROVIDER'S SYNC-NESS, because that changed
|
|
8
18
|
* between host generations and both must work:
|
|
@@ -19,8 +29,15 @@
|
|
|
19
29
|
* widened in a continuation instead of being forced through `async` — the same
|
|
20
30
|
* code satisfies a caller that awaits and one that does not.
|
|
21
31
|
*
|
|
22
|
-
*
|
|
23
|
-
*
|
|
32
|
+
* Every confinement also records the OBSERVED runner dialect (shell-dialect.ts),
|
|
33
|
+
* which drives the settings page's capability badges. A dialect that cannot
|
|
34
|
+
* express the rules must never be silently mis-claimed:
|
|
35
|
+
* - `danger-full-access` has no runner to inject into, so the record is returned
|
|
36
|
+
* untouched (the fs half still enforces the hard rules);
|
|
37
|
+
* - `landlock` can grant but cannot subtract, and `windows-acl` can do neither:
|
|
38
|
+
* both warn ONCE per dialect + rule signature and the command still runs.
|
|
39
|
+
* Warning only once is the fix for a real log-flood: a denied rule sits in the
|
|
40
|
+
* configuration and every single command would otherwise re-report it.
|
|
24
41
|
* @module dsh-extra-paths/wrap-sandbox
|
|
25
42
|
*/
|
|
26
43
|
import type { ExtraPathsConfig } from './config.js';
|
|
@@ -41,6 +58,8 @@ export interface SandboxWrapDeps {
|
|
|
41
58
|
readConfig(): ExtraPathsConfig;
|
|
42
59
|
warn(message: string): void;
|
|
43
60
|
}
|
|
61
|
+
/** Test-only: forget the capability warnings that have been logged. */
|
|
62
|
+
export declare function resetShellWarnings(): void;
|
|
44
63
|
/**
|
|
45
64
|
* Whether a service is a process-sandbox provider.
|
|
46
65
|
*
|
|
@@ -55,6 +74,7 @@ export declare function isConfiningSandbox(value: unknown): value is SandboxInne
|
|
|
55
74
|
* Wrap one sandbox provider.
|
|
56
75
|
* @param inner - the provider read from the context.
|
|
57
76
|
* @param deps - configuration source and warning sink.
|
|
58
|
-
* @returns a transparent proxy that widens profiles for configured roots
|
|
77
|
+
* @returns a transparent proxy that widens profiles for configured roots and
|
|
78
|
+
* appends the shell-surface restrictions the runner can express.
|
|
59
79
|
*/
|
|
60
80
|
export declare function wrapSandbox(inner: SandboxInnerLike, deps: SandboxWrapDeps): SandboxInnerLike;
|