dsh-extra-paths 0.0.0-stage → 0.1.0
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 +79 -2
- package/cordis.patch.yml +6 -0
- package/lib/argv.js +45 -0
- package/lib/argv.js.map +1 -0
- package/lib/client.js +905 -0
- package/lib/config.js +123 -0
- package/lib/config.js.map +1 -0
- package/lib/context.js +30 -0
- package/lib/context.js.map +1 -0
- package/lib/grant.js +21 -0
- package/lib/grant.js.map +1 -0
- package/lib/index.js +173 -0
- package/lib/index.js.map +1 -0
- package/lib/listener.js +57 -0
- package/lib/listener.js.map +1 -0
- package/lib/paths.js +70 -0
- package/lib/paths.js.map +1 -0
- package/lib/pattern.js +206 -0
- package/lib/pattern.js.map +1 -0
- package/lib/policy.js +17 -0
- package/lib/policy.js.map +1 -0
- package/lib/preview.js +126 -0
- package/lib/preview.js.map +1 -0
- package/lib/resolve.js +153 -0
- package/lib/resolve.js.map +1 -0
- package/lib/schema.js +54 -0
- package/lib/schema.js.map +1 -0
- package/lib/types/argv.d.ts +17 -0
- package/lib/types/config.d.ts +55 -0
- package/lib/types/context.d.ts +13 -0
- package/lib/types/grant.d.ts +17 -0
- package/lib/types/index.d.ts +86 -0
- package/lib/types/listener.d.ts +40 -0
- package/lib/types/paths.d.ts +32 -0
- package/lib/types/pattern.d.ts +88 -0
- package/lib/types/policy.d.ts +21 -0
- package/lib/types/preview.d.ts +77 -0
- package/lib/types/resolve.d.ts +63 -0
- package/lib/types/schema.d.ts +42 -0
- package/lib/types/wrap-fs.d.ts +50 -0
- package/lib/types/wrap-sandbox.d.ts +60 -0
- package/lib/wrap-fs.js +75 -0
- package/lib/wrap-fs.js.map +1 -0
- package/lib/wrap-sandbox.js +89 -0
- package/lib/wrap-sandbox.js.map +1 -0
- package/package.json +113 -4
package/lib/config.js
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The plugin's configuration vocabulary: a plugin-level default scope and
|
|
3
|
+
* surface set, plus per-path entries that override them. Everything here is
|
|
4
|
+
* pure so the settings schema, the CLI-facing validator, and the enforcement
|
|
5
|
+
* layers share one normalisation.
|
|
6
|
+
* @module dsh-extra-paths/config
|
|
7
|
+
*/
|
|
8
|
+
/** All scopes, in escalation order. */
|
|
9
|
+
export const SCOPES = ['write', 'always-write'];
|
|
10
|
+
/** All surfaces. `fs` is the write/edit tool family; `shell` is the subprocess sandbox. */
|
|
11
|
+
export const SURFACES = ['fs', 'shell'];
|
|
12
|
+
const DEFAULT_SCOPE = 'write';
|
|
13
|
+
const DEFAULT_SURFACES = [...SURFACES];
|
|
14
|
+
function readScope(value, fallback, label) {
|
|
15
|
+
if (value === undefined)
|
|
16
|
+
return fallback;
|
|
17
|
+
if (typeof value === 'string' && SCOPES.includes(value))
|
|
18
|
+
return value;
|
|
19
|
+
throw new Error(`${label}: scope must be one of ${SCOPES.join(' | ')}, received ${JSON.stringify(value)}`);
|
|
20
|
+
}
|
|
21
|
+
function readSurfaces(value, fallback, label) {
|
|
22
|
+
if (value === undefined)
|
|
23
|
+
return [...fallback];
|
|
24
|
+
if (!Array.isArray(value))
|
|
25
|
+
throw new Error(`${label}: surfaces must be an array, received ${JSON.stringify(value)}`);
|
|
26
|
+
const surfaces = [];
|
|
27
|
+
for (const item of value) {
|
|
28
|
+
if (typeof item !== 'string' || !SURFACES.includes(item)) {
|
|
29
|
+
throw new Error(`${label}: unknown surface ${JSON.stringify(item)}; expected ${SURFACES.join(' | ')}`);
|
|
30
|
+
}
|
|
31
|
+
if (!surfaces.includes(item))
|
|
32
|
+
surfaces.push(item);
|
|
33
|
+
}
|
|
34
|
+
return surfaces;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Normalise untrusted configuration, applying defaults and rejecting anything
|
|
38
|
+
* the enforcement layers could not act on.
|
|
39
|
+
* @param raw - the settings section or entry config.
|
|
40
|
+
* @returns the normalised configuration.
|
|
41
|
+
* @throws {Error} on a malformed entry, unknown vocabulary, or empty surfaces.
|
|
42
|
+
*/
|
|
43
|
+
export function normalizeConfig(raw) {
|
|
44
|
+
const defaultScope = readScope(raw?.defaultScope, DEFAULT_SCOPE, 'defaultScope');
|
|
45
|
+
const defaultSurfaces = readSurfaces(raw?.defaultSurfaces, DEFAULT_SURFACES, 'defaultSurfaces');
|
|
46
|
+
const list = raw?.paths;
|
|
47
|
+
if (list !== undefined && !Array.isArray(list)) {
|
|
48
|
+
throw new Error(`paths: expected an array, received ${JSON.stringify(list)}`);
|
|
49
|
+
}
|
|
50
|
+
const paths = (list ?? []).map((item, index) => {
|
|
51
|
+
const label = `paths[${index}]`;
|
|
52
|
+
const entry = (typeof item === 'string' ? { path: item } : item ?? {});
|
|
53
|
+
if (typeof entry.path !== 'string' || entry.path.trim() === '') {
|
|
54
|
+
throw new Error(`${label}: path must be a non-empty string, received ${JSON.stringify(entry.path)}`);
|
|
55
|
+
}
|
|
56
|
+
const surfaces = readSurfaces(entry.surfaces, defaultSurfaces, label);
|
|
57
|
+
if (surfaces.length === 0) {
|
|
58
|
+
const cause = entry.surfaces === undefined && defaultSurfaces.length === 0 ? ' (defaultSurfaces is empty)' : '';
|
|
59
|
+
throw new Error(`${label}: at least one surface is required${cause}`);
|
|
60
|
+
}
|
|
61
|
+
return { path: entry.path.trim(), scope: readScope(entry.scope, defaultScope, label), surfaces };
|
|
62
|
+
});
|
|
63
|
+
return { defaultScope, defaultSurfaces, paths };
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Whether a path names a filesystem root, which would disable the sandbox
|
|
67
|
+
* outright. A bare drive (`C:`) is drive-RELATIVE rather than a root, but it is
|
|
68
|
+
* rejected conservatively: on Windows it can still resolve to a whole volume,
|
|
69
|
+
* and the message says so instead of mislabelling it.
|
|
70
|
+
*/
|
|
71
|
+
export function isFilesystemRoot(path) {
|
|
72
|
+
return path === '/' || /^[A-Za-z]:[\\/]?$/.test(path);
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* The settings service's `validate` hook: refuse a write the plugin could never
|
|
76
|
+
* act on, so the user learns at save time instead of storing a dead section.
|
|
77
|
+
* @param raw - the resolved section the write would store.
|
|
78
|
+
* @throws {Error} on a duplicate literal entry or a filesystem root.
|
|
79
|
+
*/
|
|
80
|
+
export function validateConfig(raw) {
|
|
81
|
+
const config = normalizeConfig(raw);
|
|
82
|
+
// Trailing separators are stripped for the duplicate key so `/a` and `/a/`
|
|
83
|
+
// collide here rather than silently merging later; `/` keeps its meaning.
|
|
84
|
+
const firstSeen = new Map();
|
|
85
|
+
config.paths.forEach((entry, index) => {
|
|
86
|
+
// The root check runs FIRST: for ['//', '/'] the duplicate key would
|
|
87
|
+
// otherwise report the less useful of two true reasons.
|
|
88
|
+
if (isFilesystemRoot(entry.path)) {
|
|
89
|
+
throw new Error(`paths[${index}]: ${JSON.stringify(entry.path)} names a filesystem root (or a bare drive) `
|
|
90
|
+
+ 'and would disable the sandbox');
|
|
91
|
+
}
|
|
92
|
+
// Both separator kinds are stripped so the rule matches its own name on
|
|
93
|
+
// either platform's spelling; a spelling that strips to empty is the root
|
|
94
|
+
// itself (already rejected above).
|
|
95
|
+
const key = entry.path.replace(/[\\/]+$/, '') || '/';
|
|
96
|
+
const first = firstSeen.get(key);
|
|
97
|
+
if (first !== undefined) {
|
|
98
|
+
throw new Error(`paths[${index}]: duplicate path entry ${JSON.stringify(entry.path)} (first configured at paths[${first}])`);
|
|
99
|
+
}
|
|
100
|
+
firstSeen.set(key, index);
|
|
101
|
+
// A PATTERN (see `pattern.ts`) is only walkable when its literal anchor is
|
|
102
|
+
// deep enough to bound the search: `~/projects/**` is fine, `~/**` and `/**`
|
|
103
|
+
// would walk a whole home directory or the filesystem and are refused here
|
|
104
|
+
// rather than silently yielding nothing at resolve time.
|
|
105
|
+
if (entry.path.includes('*')) {
|
|
106
|
+
const segments = entry.path.split('/').filter(segment => segment !== '');
|
|
107
|
+
const firstWildcard = segments.findIndex(segment => segment.includes('*'));
|
|
108
|
+
const anchorDepth = segments.slice(0, firstWildcard).length;
|
|
109
|
+
if (anchorDepth < 2) {
|
|
110
|
+
throw new Error(`paths[${index}]: pattern ${JSON.stringify(entry.path)} needs a literal anchor of at least two segments `
|
|
111
|
+
+ '(for example ~/projects/**/.worktrees/..)');
|
|
112
|
+
}
|
|
113
|
+
const walk = segments.slice(firstWildcard);
|
|
114
|
+
let last = walk.length - 1;
|
|
115
|
+
while (last >= 0 && walk[last] === '..')
|
|
116
|
+
last -= 1;
|
|
117
|
+
if (walk.slice(0, last + 1).includes('..')) {
|
|
118
|
+
throw new Error(`paths[${index}]: '..' may only appear at the END of a pattern, received ${JSON.stringify(entry.path)}`);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
//# sourceMappingURL=config.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"config.js","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAQH,uCAAuC;AACvC,MAAM,CAAC,MAAM,MAAM,GAAqB,CAAC,OAAO,EAAE,cAAc,CAAC,CAAA;AAEjE,2FAA2F;AAC3F,MAAM,CAAC,MAAM,QAAQ,GAAuB,CAAC,IAAI,EAAE,OAAO,CAAC,CAAA;AAuB3D,MAAM,aAAa,GAAU,OAAO,CAAA;AACpC,MAAM,gBAAgB,GAAuB,CAAC,GAAG,QAAQ,CAAC,CAAA;AAE1D,SAAS,SAAS,CAAC,KAAc,EAAE,QAAe,EAAE,KAAa;IAC/D,IAAI,KAAK,KAAK,SAAS;QAAE,OAAO,QAAQ,CAAA;IACxC,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAK,MAA4B,CAAC,QAAQ,CAAC,KAAK,CAAC;QAAE,OAAO,KAAc,CAAA;IACrG,MAAM,IAAI,KAAK,CAAC,GAAG,KAAK,0BAA0B,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,cAAc,IAAI,CAAC,SAAS,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roots this
|
|
3
|
+
* session may write under, so it neither refuses nor escalates pre-emptively.
|
|
4
|
+
* @module dsh-extra-paths/context
|
|
5
|
+
*/
|
|
6
|
+
import { resolveRoots } from './resolve.js';
|
|
7
|
+
/**
|
|
8
|
+
* Render the section text, or an empty string when there is nothing to say.
|
|
9
|
+
* @param readConfig - current configuration snapshot.
|
|
10
|
+
* @param cwd - the calling session's workspace (undefined outside a session).
|
|
11
|
+
* @returns the prompt text.
|
|
12
|
+
*/
|
|
13
|
+
export function renderExtraPathsContext(readConfig, cwd) {
|
|
14
|
+
if (cwd === undefined || cwd === '')
|
|
15
|
+
return '';
|
|
16
|
+
const report = resolveRoots(readConfig(), cwd);
|
|
17
|
+
if (report.roots.length === 0)
|
|
18
|
+
return '';
|
|
19
|
+
const lines = report.roots.map(root => {
|
|
20
|
+
const reach = root.scope === 'always-write'
|
|
21
|
+
? 'writable regardless of the standing sandbox mode'
|
|
22
|
+
: 'writable while the policy is workspace-write';
|
|
23
|
+
return ` - ${root.root} — ${reach} (surfaces: ${root.surfaces.join('+')})`;
|
|
24
|
+
});
|
|
25
|
+
return [
|
|
26
|
+
'Additional configured writable roots for this session (no approval is required to write under them):',
|
|
27
|
+
...lines,
|
|
28
|
+
].join('\n');
|
|
29
|
+
}
|
|
30
|
+
//# sourceMappingURL=context.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context.js","sourceRoot":"","sources":["../src/context.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAGH,OAAO,EAAE,YAAY,EAAE,MAAM,cAAc,CAAA;AAE3C;;;;;GAKG;AACH,MAAM,UAAU,uBAAuB,CAAC,UAAkC,EAAE,GAAuB;IACjG,IAAI,GAAG,KAAK,SAAS,IAAI,GAAG,KAAK,EAAE;QAAE,OAAO,EAAE,CAAA;IAC9C,MAAM,MAAM,GAAG,YAAY,CAAC,UAAU,EAAE,EAAE,GAAG,CAAC,CAAA;IAC9C,IAAI,MAAM,CAAC,KAAK,CAAC,MAAM,KAAK,CAAC;QAAE,OAAO,EAAE,CAAA;IACxC,MAAM,KAAK,GAAG,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE;QACpC,MAAM,KAAK,GAAG,IAAI,CAAC,KAAK,KAAK,cAAc;YACzC,CAAC,CAAC,kDAAkD;YACpD,CAAC,CAAC,8CAA8C,CAAA;QAClD,OAAO,OAAO,IAAI,CAAC,IAAI,MAAM,KAAK,eAAe,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,CAAA;IAC7E,CAAC,CAAC,CAAA;IACF,OAAO;QACL,sGAAsG;QACtG,GAAG,KAAK;KACT,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;AACd,CAAC"}
|
package/lib/grant.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The mode gate: which configured roots one call may write under.
|
|
3
|
+
* `write` rides the session mode; `always-write` survives `read-only` too.
|
|
4
|
+
* @module dsh-extra-paths/grant
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* Select the roots a call under `mode` may write under for one surface.
|
|
8
|
+
* @param mode - the resolved per-call sandbox mode.
|
|
9
|
+
* @param roots - effective roots for this session workspace.
|
|
10
|
+
* @param surface - the enforcement surface asking.
|
|
11
|
+
* @returns the roots to grant; empty means "delegate unchanged".
|
|
12
|
+
*/
|
|
13
|
+
export function grantFor(mode, roots, surface) {
|
|
14
|
+
if (mode === 'danger-full-access')
|
|
15
|
+
return [];
|
|
16
|
+
const candidates = roots.filter(root => root.surfaces.includes(surface));
|
|
17
|
+
if (mode === 'workspace-write')
|
|
18
|
+
return candidates;
|
|
19
|
+
return candidates.filter(root => root.scope === 'always-write');
|
|
20
|
+
}
|
|
21
|
+
//# sourceMappingURL=grant.js.map
|
package/lib/grant.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"grant.js","sourceRoot":"","sources":["../src/grant.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAQH;;;;;;GAMG;AACH,MAAM,UAAU,QAAQ,CACtB,IAAqB,EACrB,KAA8B,EAC9B,OAAgB;IAEhB,IAAI,IAAI,KAAK,oBAAoB;QAAE,OAAO,EAAE,CAAA;IAC5C,MAAM,UAAU,GAAG,KAAK,CAAC,MAAM,CAAC,IAAI,CAAC,EAAE,CAAC,IAAI,CAAC,QAAQ,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC,CAAA;IACxE,IAAI,IAAI,KAAK,iBAAiB;QAAE,OAAO,UAAU,CAAA;IACjD,OAAO,UAAU,CAAC,MAAM,CAAC,IAAI,CAAC,EAAE,CAAC,IAAI,CAAC,KAAK,KAAK,cAAc,CAAC,CAAA;AACjE,CAAC"}
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* dsh-extra-paths: configured extra writable roots for the sandboxed fs tools
|
|
3
|
+
* and the sandboxed shell, plus the settings surface and its preview route.
|
|
4
|
+
*
|
|
5
|
+
* Host half only. No `@deepseek-ai/*` VALUE import except the schema builder:
|
|
6
|
+
* every failure below degrades to "no grant + warning", never to a failed boot.
|
|
7
|
+
*
|
|
8
|
+
* Two host generations are supported from one body, because the settings seam
|
|
9
|
+
* changed shape in 0.2.0 and the plugin must not throw on either:
|
|
10
|
+
* - 0.1.5: `ctx.settings.register(ns, schema, { base, applies, validate })`
|
|
11
|
+
* returns a scope whose `get()`/`watch()` drive `current`.
|
|
12
|
+
* - 0.2.x: that API is GONE. A namespace IS the plugin's loader entry, its
|
|
13
|
+
* schema is this module's exported {@link Config}, and live values are the
|
|
14
|
+
* `volatile()` fields of the config the loader hands to {@link apply} — which
|
|
15
|
+
* are re-read on every enforcement decision, so an edit in the framework's
|
|
16
|
+
* settings form applies without a restart.
|
|
17
|
+
* @module dsh-extra-paths
|
|
18
|
+
*/
|
|
19
|
+
import { normalizeConfig, validateConfig } from './config.js';
|
|
20
|
+
import { renderExtraPathsContext } from './context.js';
|
|
21
|
+
import { installListener, fallbackPolicyOf } from './listener.js';
|
|
22
|
+
import { registerPreviewRoute } from './preview.js';
|
|
23
|
+
import { extraPathsSchema, extraPathsConfigSchema } from './schema.js';
|
|
24
|
+
/** The settings namespace the client page binds to. */
|
|
25
|
+
export const SETTINGS_NAMESPACE = 'extra-paths';
|
|
26
|
+
/**
|
|
27
|
+
* The plugin's configuration schema as the 0.2.x loader expects to find it: a
|
|
28
|
+
* module-level `Config` export, read through `entry.fiber.runtime.Config`. Fields
|
|
29
|
+
* are `volatile()` because a settings form only describes volatile fields.
|
|
30
|
+
*/
|
|
31
|
+
export const Config = extraPathsConfigSchema;
|
|
32
|
+
/** Config keys this plugin owns, in schema order. */
|
|
33
|
+
const CONFIG_KEYS = ['defaultScope', 'defaultSurfaces', 'paths'];
|
|
34
|
+
/**
|
|
35
|
+
* Normalise the entry config, degrading to the documented empty configuration
|
|
36
|
+
* on any problem. This is the plugin's only untrusted-input boundary that runs
|
|
37
|
+
* before the settings service can validate anything, so it must not throw.
|
|
38
|
+
* @param raw - the entry config as loaded from the composition.
|
|
39
|
+
* @param warn - warning sink.
|
|
40
|
+
* @returns a normalised configuration safe to read on every enforcement decision.
|
|
41
|
+
*/
|
|
42
|
+
export function safeEntryConfig(raw, warn) {
|
|
43
|
+
try {
|
|
44
|
+
return normalizeConfig(raw);
|
|
45
|
+
}
|
|
46
|
+
catch (error) {
|
|
47
|
+
warn(`extra-paths: entry config rejected (${String(error)}); continuing with the empty configuration`);
|
|
48
|
+
return normalizeConfig(undefined);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Read the 0.2.x live config: every `volatile()` field is a wrapper with `get()`,
|
|
53
|
+
* so this returns the CURRENT value rather than the value captured at load. A
|
|
54
|
+
* plain object (0.1.5, or a hand-written patch without volatile fields) is
|
|
55
|
+
* returned as-is so both generations share one reader.
|
|
56
|
+
* @param config - the config the loader passed to {@link apply}.
|
|
57
|
+
* @returns a raw section, or undefined when nothing usable is present.
|
|
58
|
+
*/
|
|
59
|
+
export function readLiveConfig(config) {
|
|
60
|
+
if (config === null || typeof config !== 'object')
|
|
61
|
+
return undefined;
|
|
62
|
+
const source = config;
|
|
63
|
+
const out = {};
|
|
64
|
+
let found = false;
|
|
65
|
+
for (const key of CONFIG_KEYS) {
|
|
66
|
+
const field = source[key];
|
|
67
|
+
if (field === undefined)
|
|
68
|
+
continue;
|
|
69
|
+
const getter = field.get;
|
|
70
|
+
// `volatile()` (and `defined()`) fields expose get(); plain values do not.
|
|
71
|
+
out[key] = typeof getter === 'function' ? getter.call(field) : field;
|
|
72
|
+
found = true;
|
|
73
|
+
}
|
|
74
|
+
return found ? out : undefined;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Plugin body.
|
|
78
|
+
* @param ctx - the entry context.
|
|
79
|
+
* @param config - the entry config: the settings `base` layer on 0.1.5, and the
|
|
80
|
+
* live (volatile) settings themselves on 0.2.x.
|
|
81
|
+
*/
|
|
82
|
+
export function apply(ctx, config) {
|
|
83
|
+
const warn = (message) => { ctx.logger.warn(message); };
|
|
84
|
+
// The entry config is UNTRUSTED (a typo in cordis.patch.yml must never stop
|
|
85
|
+
// the boot): sanitise once and use the sanitised value for both the live
|
|
86
|
+
// snapshot and the settings `base` layer.
|
|
87
|
+
const entryConfig = safeEntryConfig(config, warn);
|
|
88
|
+
let current = entryConfig;
|
|
89
|
+
// A rejected volatile config would otherwise warn on every single call.
|
|
90
|
+
let lastRejection = '';
|
|
91
|
+
// Which source owns the configuration is decided ONCE, from the shape of the
|
|
92
|
+
// config the loader handed us: `volatile()` holders only exist on 0.2.x, where
|
|
93
|
+
// they ARE the settings. On 0.1.5 the config is a plain object that is merely
|
|
94
|
+
// the settings' `base` layer, so letting it win would silently override every
|
|
95
|
+
// edit made in the settings form.
|
|
96
|
+
const volatile = (() => {
|
|
97
|
+
const source = config;
|
|
98
|
+
return CONFIG_KEYS.some((key) => {
|
|
99
|
+
const field = source?.[key];
|
|
100
|
+
return field !== null && typeof field === 'object' && typeof field.get === 'function';
|
|
101
|
+
});
|
|
102
|
+
})();
|
|
103
|
+
/**
|
|
104
|
+
* The configuration in force right now.
|
|
105
|
+
*
|
|
106
|
+
* On 0.2.x the volatile fields are the live settings, so they are read per
|
|
107
|
+
* decision — that is what makes an edit in the framework's settings form take
|
|
108
|
+
* effect without a restart. On 0.1.5 the settings scope (below) keeps `current`
|
|
109
|
+
* up to date instead.
|
|
110
|
+
*/
|
|
111
|
+
const readConfig = () => {
|
|
112
|
+
if (!volatile)
|
|
113
|
+
return current;
|
|
114
|
+
const live = readLiveConfig(config);
|
|
115
|
+
if (live === undefined)
|
|
116
|
+
return current;
|
|
117
|
+
try {
|
|
118
|
+
return normalizeConfig(live);
|
|
119
|
+
}
|
|
120
|
+
catch (error) {
|
|
121
|
+
const message = String(error);
|
|
122
|
+
if (message !== lastRejection) {
|
|
123
|
+
lastRejection = message;
|
|
124
|
+
warn(`extra-paths: settings value rejected (${message}); continuing with the empty configuration`);
|
|
125
|
+
}
|
|
126
|
+
return normalizeConfig(undefined);
|
|
127
|
+
}
|
|
128
|
+
};
|
|
129
|
+
const listener = installListener(ctx, { readConfig, fallbackPolicy: fallbackPolicyOf, warn });
|
|
130
|
+
ctx.inject(['settings'], (scope) => {
|
|
131
|
+
const settings = scope.settings;
|
|
132
|
+
// 0.2.x: no registration API at all. The namespace is this entry, its schema
|
|
133
|
+
// is the exported `Config`, and `readConfig` above already reads the live
|
|
134
|
+
// volatile fields, so there is nothing to wire here.
|
|
135
|
+
if (typeof settings.register !== 'function')
|
|
136
|
+
return;
|
|
137
|
+
const registered = settings.register(SETTINGS_NAMESPACE, extraPathsSchema, {
|
|
138
|
+
base: entryConfig,
|
|
139
|
+
applies: 'live',
|
|
140
|
+
validate: (value) => { validateConfig(value); },
|
|
141
|
+
});
|
|
142
|
+
current = normalizeConfig(registered.get());
|
|
143
|
+
registered.watch((value) => { current = normalizeConfig(value); });
|
|
144
|
+
});
|
|
145
|
+
ctx.inject(['systemPrompt'], (scope) => {
|
|
146
|
+
const systemPrompt = scope.systemPrompt;
|
|
147
|
+
systemPrompt.context({
|
|
148
|
+
name: 'extra-paths:writable',
|
|
149
|
+
order: systemPrompt.getContextOrder('SANDBOX_POLICY') + 2,
|
|
150
|
+
text: context => renderExtraPathsContext(readConfig, context.agent?.session?.header?.cwd),
|
|
151
|
+
});
|
|
152
|
+
});
|
|
153
|
+
ctx.inject(['webServer', 'connection'], (scope) => {
|
|
154
|
+
registerPreviewRoute(scope, { readConfig, warn });
|
|
155
|
+
});
|
|
156
|
+
// Load-time self-test: this read MUST travel through internal/get. Zero
|
|
157
|
+
// observed reads means the interception seam is gone in this DSH version —
|
|
158
|
+
// and a service that is never wrapped looks exactly like "writes are denied",
|
|
159
|
+
// so both counters are asserted, not just the read counter.
|
|
160
|
+
void ctx.sandboxPolicy;
|
|
161
|
+
if (listener.reads === 0) {
|
|
162
|
+
warn('extra-paths: internal/get interception is unavailable on this DSH version — '
|
|
163
|
+
+ 'configured extra paths will NOT be granted. The plugin stays inert and boot is unaffected.');
|
|
164
|
+
}
|
|
165
|
+
// NOTE: `fsWraps`/`sandboxWraps` are deliberately NOT asserted here. At apply
|
|
166
|
+
// time no consumer has read `ctx.fs`/`ctx.sandbox` yet, so both are always 0 —
|
|
167
|
+
// a check on them here fires on every boot while the feature works fine. They
|
|
168
|
+
// stay on ListenerState as after-the-fact observability only.
|
|
169
|
+
}
|
|
170
|
+
/** Property carrier so the loader waits for the policy service. */
|
|
171
|
+
const plugin = Object.assign(apply, { inject: ['sandboxPolicy'], Config });
|
|
172
|
+
export default plugin;
|
|
173
|
+
//# sourceMappingURL=index.js.map
|
package/lib/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AAEH,OAAO,EAAE,eA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al/get` interception: wraps `ctx.fs` and `ctx.sandbox` as they are
|
|
3
|
+
* read. The callback must never change the semantics of a service read — it
|
|
4
|
+
* always calls `next()`, rethrows a resolution failure unchanged, and falls
|
|
5
|
+
* back to the unwrapped value if its own wrapping throws.
|
|
6
|
+
* @module dsh-extra-paths/listener
|
|
7
|
+
*/
|
|
8
|
+
import { isConfiningFs, wrapFs } from './wrap-fs.js';
|
|
9
|
+
import { isConfiningSandbox, wrapSandbox } from './wrap-sandbox.js';
|
|
10
|
+
/**
|
|
11
|
+
* Resolve `ctx.sandboxPolicy.resolve()` through a reading context without
|
|
12
|
+
* requiring an injection — `reflect.get` is exactly the non-strict read.
|
|
13
|
+
* @param readCtx - the context whose proxy served the service read.
|
|
14
|
+
* @returns the deployment policy, or undefined when the service is unavailable.
|
|
15
|
+
*/
|
|
16
|
+
export function fallbackPolicyOf(readCtx) {
|
|
17
|
+
const get = readCtx?.get;
|
|
18
|
+
if (typeof get !== 'function')
|
|
19
|
+
return undefined;
|
|
20
|
+
const service = get.call(readCtx, 'sandboxPolicy', false);
|
|
21
|
+
return typeof service?.resolve === 'function' ? service.resolve() : undefined;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Install the interception listener on the calling fiber.
|
|
25
|
+
* @param ctx - the plugin context (the listener is disposed with its fiber).
|
|
26
|
+
* @param deps - configuration and logging sources.
|
|
27
|
+
* @returns counters the caller uses for the load-time self-test.
|
|
28
|
+
*/
|
|
29
|
+
export function installListener(ctx, deps) {
|
|
30
|
+
const state = { reads: 0, fsWraps: 0, sandboxWraps: 0 };
|
|
31
|
+
ctx.on('internal/get', (readCtx, name, _error, next) => {
|
|
32
|
+
const value = next();
|
|
33
|
+
state.reads += 1;
|
|
34
|
+
try {
|
|
35
|
+
if (name === 'fs' && isConfiningFs(value)) {
|
|
36
|
+
state.fsWraps += 1;
|
|
37
|
+
return wrapFs(value, {
|
|
38
|
+
// Both are closures evaluated PER CALL: hoisting either one would
|
|
39
|
+
// freeze the session's workspace (and therefore which relative
|
|
40
|
+
// entries are live) at wrap time.
|
|
41
|
+
readConfig: deps.readConfig,
|
|
42
|
+
fallbackPolicy: () => deps.fallbackPolicy(readCtx),
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
if (name === 'sandbox' && isConfiningSandbox(value)) {
|
|
46
|
+
state.sandboxWraps += 1;
|
|
47
|
+
return wrapSandbox(value, { readConfig: deps.readConfig, warn: deps.warn });
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
catch (error) {
|
|
51
|
+
deps.warn(`extra-paths: wrapping ctx.${String(name)} failed; it stays unwrapped: ${String(error)}`);
|
|
52
|
+
}
|
|
53
|
+
return value;
|
|
54
|
+
});
|
|
55
|
+
return state;
|
|
56
|
+
}
|
|
57
|
+
//# sourceMappingURL=listener.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"listener.js","sourceRoot":"","sources":["../src/listener.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAIH,OAAO,EAAE,aAAa,EAAE,MAAM,EAAoB,MAAM,cAAc,CAAA;AACtE,OAAO,EAAE,kBAAkB,EAAE,WAAW,EAAyB,MAAM,mBAAmB,CAAA;AAsB1F;;;;;GAKG;AACH,MAAM,UAAU,gBAAgB,CAAC,OAAgB;IAC/C,MAAM,GAAG,GAAI,OAA6E,EAAE,GAAG,CAAA;IAC/F,IAAI,OAAO,GAAG,KAAK,UAAU;QAAE,OAAO,SAAS,CAAA;IAC/C,MAAM,OAAO,GAAG,GAAG,CAAC,IAAI,CAAC,OAAO,EAAE,eAAe,EAAE,KAAK,CAA+C,CAAA;IACvG,OAAO,OAAO,OAAO,EAAE,OAAO,KAAK,UAAU,CAAC,CAAC,CAAC,OAAO,CAAC,OAAO,EAAE,CAAC,CAAC,CAAC,SAAS,CAAA;AAC/E,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,eAAe,CAAC,GAAoB,EAAE,IAAkB;IACtE,MAAM,KAAK,GAA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every root is canonicalised the same
|
|
3
|
+
* way the filesystem service canonicalises a write target — realpath of the
|
|
4
|
+
* deepest EXISTING ancestor plus the remaining suffix — so a not-yet-created
|
|
5
|
+
* directory still compares equal to the path the enforcement layer computes.
|
|
6
|
+
* @module dsh-extra-paths/paths
|
|
7
|
+
*/
|
|
8
|
+
import { existsSync, realpathSync } from 'node:fs';
|
|
9
|
+
import { homedir } from 'node:os';
|
|
10
|
+
import { basename, dirname, isAbsolute, join, relative, resolve as resolvePath, sep } from 'node:path';
|
|
11
|
+
/**
|
|
12
|
+
* Resolve one configured path spelling to an absolute path.
|
|
13
|
+
* @param raw - the configured spelling: absolute, `~`-anchored, or relative.
|
|
14
|
+
* @param workspaceRoot - the session workspace a relative spelling is resolved against.
|
|
15
|
+
* @param home - home directory seam (tests).
|
|
16
|
+
* @returns the absolute path, not yet canonicalised.
|
|
17
|
+
*/
|
|
18
|
+
export function expandPath(raw, workspaceRoot, home = homedir()) {
|
|
19
|
+
if (raw === '~')
|
|
20
|
+
return home;
|
|
21
|
+
if (raw.startsWith(`~${sep}`) || raw.startsWith('~/'))
|
|
22
|
+
return join(home, raw.slice(2));
|
|
23
|
+
if (isAbsolute(raw))
|
|
24
|
+
return raw;
|
|
25
|
+
return resolvePath(workspaceRoot, raw);
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Canonical spelling of a configured root. Symlinked ancestors are resolved
|
|
29
|
+
* (so `/tmp` and `/private/tmp` cannot silently fail to match), while a suffix
|
|
30
|
+
* that does not exist yet is appended verbatim.
|
|
31
|
+
* @param path - an absolute path.
|
|
32
|
+
* @returns the canonical spelling.
|
|
33
|
+
*/
|
|
34
|
+
export function canonicalizeRoot(path) {
|
|
35
|
+
const resolved = resolvePath(path);
|
|
36
|
+
const suffix = [];
|
|
37
|
+
let probe = resolved;
|
|
38
|
+
while (!existsSync(probe)) {
|
|
39
|
+
const parent = dirname(probe);
|
|
40
|
+
if (parent === probe)
|
|
41
|
+
return resolved;
|
|
42
|
+
suffix.unshift(basename(probe));
|
|
43
|
+
probe = parent;
|
|
44
|
+
}
|
|
45
|
+
let real;
|
|
46
|
+
try {
|
|
47
|
+
real = realpathSync.native(probe);
|
|
48
|
+
}
|
|
49
|
+
catch {
|
|
50
|
+
real = probe;
|
|
51
|
+
}
|
|
52
|
+
return suffix.length === 0 ? real : join(real, ...suffix);
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Whether a canonical target is a root or lies beneath it. Both sides must
|
|
56
|
+
* already be canonical; containment is then purely lexical, which is why the
|
|
57
|
+
* caller canonicalises before comparing.
|
|
58
|
+
* @param targetKey - canonical write target.
|
|
59
|
+
* @param root - canonical configured root.
|
|
60
|
+
* @returns true for the root itself and every descendant.
|
|
61
|
+
*/
|
|
62
|
+
export function isUnder(targetKey, root) {
|
|
63
|
+
if (targetKey === root)
|
|
64
|
+
return true;
|
|
65
|
+
const rel = relative(root, targetKey);
|
|
66
|
+
// `..foo` is a legitimate CHILD whose name starts with dots; only the `..`
|
|
67
|
+
// segment itself and a `../` prefix escape the root.
|
|
68
|
+
return rel !== '' && rel !== '..' && !rel.startsWith(`..${sep}`) && !isAbsolute(rel);
|
|
69
|
+
}
|
|
70
|
+
//# sourceMappingURL=paths.js.map
|
package/lib/paths.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"paths.js","sourceRoot":"","sources":["../src/paths.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAE,UAAU,EAAE,YAAY,EAAE,MAAM,SAAS,CAAA;AAClD,OAAO,EAAE,OAAO,EAAE,MAAM,SAAS,CAAA;AACjC,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAE,UAAU,EAAE,IAAI,EAAE,QAAQ,EAAE,OAAO,IAAI,WAAW,EAAE,GAAG,EAAE,MAAM,WAAW,CAAA;AAEtG;;;;;;GAMG;AACH,MAAM,UAAU,UAAU,CAAC,GAAW,EAAE,aAAqB,EAAE,OAAe,O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