fix: judge the working directory and the arguments a tool declares
The guard resolved relative paths against the session directory the conversation started in, but the file tools resolve them against the session's *current* working directory — which `working_directory` can change with no guard opinion. In a `deny` facet, `cd /etc` followed by `read` of a relative path read from /etc. The same mismatch made a shadowed tool invisible: this deployment installs `dsh-better-edit`, which replaces `read` (argument `path`) and `edit` (argument `file`), so a table written against the built-in `file_path` left the busiest read tool unguarded — an outside file could be read with `deny` in force. Now: - `working_directory` is in the path table, so a `cd` outside the facet is refused (or put to the user, by mode) like any other out-of-facet path; - a relative argument is judged against the directory the tool will actually use, read lazily from `ctx.workingDirectory`, and the implicit-search rule judges that same directory; - path parameter names come from the tool's own declaration when the registry can supply one, so a shadowed tool is judged by the arguments it really takes; - a delegated session started with a `subagent` `cwd` outside the parent's facet is clamped to the parent root instead of being scoped to the outside directory. `undo_last_edit` (write-like) and the delegation tools join the table. Verified against a profile with dsh-better-edit: in `deny` mode the outside `read`, `edit`, `cd /etc` and `subagent cwd /etc` are all refused, while an inside read still works — and the relative-read bypass from the same profile now stays inside.
This commit is contained in:
1 parent
c48e9cabd9
commit
40574505f2
7 files changed
+309
-27
No files matched your search
@@ -234,7 +234,10 @@ its default is not 8192 but `headroomTokens`, 65536.
|
|||||||
|
|
||||||
If you want that budget on *every* session rather than only scoped ones,
|
If you want that budget on *every* session rather than only scoped ones,
|
||||||
`presets/standard-compaction.patch.yml` is an opt-in overlay that applies the
|
`presets/standard-compaction.patch.yml` is an opt-in overlay that applies the
|
||||||
same row to the shipped `standard` preset. It is not in the bundle's default
|
same row to the shipped `standard` preset. It overrides that row by id rather
|
||||||
|
than declaring a second one — with two declarations the mounted one would be
|
||||||
|
decided by the loader's duplicate-id handling rather than by the patch. It is not
|
||||||
|
in the bundle's default
|
||||||
patch list, and the header of that file explains why: a preset declaration
|
patch list, and the header of that file explains why: a preset declaration
|
||||||
replaces the whole plugin list rather than merging into it, so the file is a
|
replaces the whole plugin list rather than merging into it, so the file is a
|
||||||
frozen copy of `standard` that will not follow the harness when `standard`
|
frozen copy of `standard` that will not follow the harness when `standard`
|
||||||
@@ -276,6 +279,22 @@ If a *deployment* still fails with that message on a non-facet preset, the
|
|||||||
reference is in the deployment's own `system-prompt` config (`personaSuffix`) or
|
reference is in the deployment's own `system-prompt` config (`personaSuffix`) or
|
||||||
in a copy of the shipped `standard` patch, not in this bundle.
|
in a copy of the shipped `standard` patch, not in this bundle.
|
||||||
|
|
||||||
|
### The working directory is part of the scope
|
||||||
|
|
||||||
|
The file tools resolve a relative argument against the session's *current*
|
||||||
|
directory, and `working_directory` can change that. So the scope follows it: a
|
||||||
|
`cd` outside the facet is refused (or put to the user, by mode) like any other
|
||||||
|
out-of-facet path, and a relative `read`, `write` or `edit` is judged against the
|
||||||
|
directory the tool will actually use — not the one the conversation started in.
|
||||||
|
A delegated session started with its own `cwd` is clamped to the parent's facet;
|
||||||
|
it cannot be handed a wider scope by pointing it somewhere else.
|
||||||
|
|
||||||
|
A deployment can also shadow a built-in tool with one that names its path
|
||||||
|
argument differently — `dsh-better-edit` replaces `read` with `path` and `edit`
|
||||||
|
with `file`. The guard reads the argument names the tool actually declares for
|
||||||
|
the operations it guards, so a shadow is still judged; the table in
|
||||||
|
`lib/scope.js` is the fallback when the declaration cannot be read.
|
||||||
|
|
||||||
## What it does not cover
|
## What it does not cover
|
||||||
|
|
||||||
Honest boundaries, because each one is a decision rather than an oversight:
|
Honest boundaries, because each one is a decision rather than an oversight:
|
||||||
@@ -288,8 +307,12 @@ Honest boundaries, because each one is a decision rather than an oversight:
|
|||||||
outside its facet, having been told in the same prompt not to. A prompt is not
|
outside its facet, having been told in the same prompt not to. A prompt is not
|
||||||
a guard, which is what the rest of this plugin is for.
|
a guard, which is what the rest of this plugin is for.
|
||||||
- **Tools that name a path in a shape the table cannot read are not guessed at** —
|
- **Tools that name a path in a shape the table cannot read are not guessed at** —
|
||||||
including MCP tools. See `PATH_ARGUMENTS` in `lib/scope.js`; it is a list of
|
including MCP tools, and `present`'s nested `files[].path` (it shows the user a
|
||||||
tools whose path argument is a plain top-level string.
|
file rather than reading it back to the model). See `PATH_ARGUMENTS` in
|
||||||
|
`lib/scope.js`; it is a list of tools whose path argument is a plain top-level
|
||||||
|
string. `apply_patch` is switched off in this bundle's preset for the same
|
||||||
|
reason — its paths live inside patch text — so a deployment that turns it on
|
||||||
|
gets no guard on it.
|
||||||
- **This guards the tool pipeline, not the filesystem.** There is no read seam in
|
- **This guards the tool pipeline, not the filesystem.** There is no read seam in
|
||||||
the harness — `dsh-fs-sandbox` states plainly that "reads pass through
|
the harness — `dsh-fs-sandbox` states plainly that "reads pass through
|
||||||
untouched: every mode permits reading" — so anything that reads through
|
untouched: every mode permits reading" — so anything that reads through
|
||||||
@@ -303,7 +326,7 @@ Honest boundaries, because each one is a decision rather than an oversight:
|
|||||||
|
|
||||||
Verified:
|
Verified:
|
||||||
|
|
||||||
- 98 tests (`npm test`) over the scope algebra, the policy state, the guard, and
|
- 107 tests (`npm test`) over the scope algebra, the policy state, the guard, and
|
||||||
the browser half — including that containment is decided on canonical targets
|
the browser half — including that containment is decided on canonical targets
|
||||||
(a symlink inside the facet pointing outside it is refused), that a prefix is
|
(a symlink inside the facet pointing outside it is refused), that a prefix is
|
||||||
not a parent (`/a/room` does not contain `/a/roommate`), that a malformed tool
|
not a parent (`/a/room` does not contain `/a/roommate`), that a malformed tool
|
||||||
@@ -318,8 +341,13 @@ Verified:
|
|||||||
preset or contributed prompt text references a prompt variable the harness
|
preset or contributed prompt text references a prompt variable the harness
|
||||||
does not register, that each shipped preset is the installed harness's
|
does not register, that each shipped preset is the installed harness's
|
||||||
`standard` composition plus only its documented changes and names only plugins
|
`standard` composition plus only its documented changes and names only plugins
|
||||||
the harness provides, and that every `facet` result satisfies the tool's own
|
the harness provides, that a `cd` outside the facet is refused while a
|
||||||
declared output schema.
|
relative path is judged against the directory the tool will use, that a tool
|
||||||
|
shadowed onto different argument names is still guarded, that a delegated
|
||||||
|
session cannot be started outside its parent's facet, that the optional
|
||||||
|
compaction overlay replaces the shipped preset rather than adding a second
|
||||||
|
declaration, and that every `facet` result satisfies the tool's own declared
|
||||||
|
output schema.
|
||||||
- The bundle's patch layers parse, merge and land correctly against the real
|
- The bundle's patch layers parse, merge and land correctly against the real
|
||||||
profile: `dsh --profile web --patch ./cordis.patch.yml --patch
|
profile: `dsh --profile web --patch ./cordis.patch.yml --patch
|
||||||
./presets/facet.patch.yml --dump-config` shows `facet-policy`, `preset-facet`,
|
./presets/facet.patch.yml --dump-config` shows `facet-policy`, `preset-facet`,
|
||||||
@@ -399,7 +427,7 @@ lib/scope.js the pure half — argument extraction, text, reason
|
|||||||
lib/guard.js preset row: rule, binding, tool, pre-execute guard
|
lib/guard.js preset row: rule, binding, tool, pre-execute guard
|
||||||
presets/facet.patch.yml the `facet` agent preset
|
presets/facet.patch.yml the `facet` agent preset
|
||||||
presets/standard-compaction.patch.yml optional: the same budget for `standard`
|
presets/standard-compaction.patch.yml optional: the same budget for `standard`
|
||||||
test/ 98 tests
|
test/ 107 tests
|
||||||
```
|
```
|
||||||
|
|
||||||
`client/client.js` is a browser half rather than a module the host imports, and
|
`client/client.js` is a browser half rather than a module the host imports, and
|
||||||
|
|||||||
+58
-9
@@ -101,6 +101,49 @@ export function apply(ctx, config) {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
* The directory a relative argument will actually resolve against.
|
||||||
|
*
|
||||||
|
* The file tools resolve against the session's *current* working directory
|
||||||
|
* (`ctx.workingDirectory`), which the `working_directory` tool can change; the
|
||||||
|
* session header keeps the directory the conversation started in. Judging
|
||||||
|
* `../../x` against the header would ask the wrong question — after a `cd`, a
|
||||||
|
* relative read resolves somewhere else entirely — so the guard asks the same
|
||||||
|
* service the tools ask. Read lazily and tolerantly: a deployment without the
|
||||||
|
* service keeps the header's directory, and a transient failure must not turn
|
||||||
|
* into a denial.
|
||||||
|
*/
|
||||||
|
const cwdNow = (agent) => {
|
||||||
|
const service = ctx.get('workingDirectory')
|
||||||
|
if (service === undefined || agent?.session === undefined) return undefined
|
||||||
|
try {
|
||||||
|
const cwd = service.get(agent.session)
|
||||||
|
return typeof cwd === 'string' && cwd !== '' ? cwd : undefined
|
||||||
|
} catch {
|
||||||
|
return undefined
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The property names the tool declares, when the guard can see them.
|
||||||
|
*
|
||||||
|
* A tool can be shadowed per agent (`dsh-better-edit` replaces `read` and
|
||||||
|
* `edit` on the agent's own layer), and the shadow may name its path argument
|
||||||
|
* differently. Reading the declaration is what keeps the guard judging the
|
||||||
|
* argument the tool will actually use rather than the one the built-in took.
|
||||||
|
* An unreadable schema is not evidence that the path went away; the table
|
||||||
|
* still applies.
|
||||||
|
*/
|
||||||
|
const declaredParameterNames = (exec) => {
|
||||||
|
try {
|
||||||
|
const declared = exec.schema?.parameters ?? ctx.tools.get(exec.name, exec.agent)?.parameters
|
||||||
|
const properties = declared?.properties
|
||||||
|
if (properties === null || typeof properties !== 'object' || Array.isArray(properties)) return undefined
|
||||||
|
return Object.keys(properties)
|
||||||
|
} catch {
|
||||||
|
return undefined
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// `off` is the one part that stays a mount-time decision. Deciding it here
|
// `off` is the one part that stays a mount-time decision. Deciding it here
|
||||||
// means an `off` deployment contributes no rule, no binding and no tool —
|
// means an `off` deployment contributes no rule, no binding and no tool —
|
||||||
@@ -182,29 +225,35 @@ export function apply(ctx, config) {
|
|||||||
const scope = policy.scopeFor(exec.agent)
|
const scope = policy.scopeFor(exec.agent)
|
||||||
if (scope === undefined) return undefined
|
if (scope === undefined) return undefined
|
||||||
|
|
||||||
const paths = pathArgumentsFor(exec.name, exec.arguments)
|
// Relative arguments are resolved by the tool against its current working
|
||||||
|
// directory, which `working_directory` can move; judge them against the same
|
||||||
|
// directory or the check answers for a path the tool will not use.
|
||||||
|
const cwd = cwdNow(exec.agent)
|
||||||
|
const current = cwd === undefined || cwd === scope.cwd ? scope : { ...scope, cwd }
|
||||||
|
|
||||||
|
const paths = pathArgumentsFor(exec.name, exec.arguments, declaredParameterNames(exec))
|
||||||
if (paths.length === 0) {
|
if (paths.length === 0) {
|
||||||
// A tool that names no path is judged against the path it will actually
|
// A tool that names no path is judged against the path it will actually
|
||||||
// use. For most of them that is nothing — a tool with no path argument
|
// use. For most of them that is nothing — a tool with no path argument
|
||||||
// cannot leave the facet. For the ones whose own default is a directory it
|
// cannot leave the facet. For the ones whose own default is a directory it
|
||||||
// is the session directory, which a narrowed facet does not contain, and
|
// is the current working directory, which a narrowed facet does not
|
||||||
// "no opinion" there would hand the model a read the guard exists to
|
// contain, and "no opinion" there would hand the model a read the guard
|
||||||
// refuse. The containment test is the same one; only the path is ours
|
// exists to refuse. The containment test is the same one; only the path is
|
||||||
// rather than the model's.
|
// ours rather than the model's.
|
||||||
if (!IMPLICIT_SESSION_PATHS.includes(exec.name)) return undefined
|
if (!IMPLICIT_SESSION_PATHS.includes(exec.name)) return undefined
|
||||||
const verdict = await inside(scope, scope.cwd, exec.signal)
|
const verdict = await inside(current, current.cwd, exec.signal)
|
||||||
if (verdict.inside) return undefined
|
if (verdict.inside) return undefined
|
||||||
const kind = mode === 'deny' ? 'deny' : 'ask'
|
const kind = mode === 'deny' ? 'deny' : 'ask'
|
||||||
return { kind, reason: implicitSearchReason({ cwd: scope.cwd, root: scope.root }) }
|
return { kind, reason: implicitSearchReason({ cwd: current.cwd, root: scope.root }) }
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const raw of paths) {
|
for (const raw of paths) {
|
||||||
const verdict = await inside(scope, raw, exec.signal)
|
const verdict = await inside(current, raw, exec.signal)
|
||||||
if (verdict.inside) continue
|
if (verdict.inside) continue
|
||||||
const kind = mode === 'deny' ? 'deny' : 'ask'
|
const kind = mode === 'deny' ? 'deny' : 'ask'
|
||||||
return {
|
return {
|
||||||
kind,
|
kind,
|
||||||
reason: outsideReason({ cwd: scope.cwd, path: verdict.display, root: scope.root, mode: kind }),
|
reason: outsideReason({ cwd: current.cwd, path: verdict.display, root: scope.root, mode: kind }),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return undefined
|
return undefined
|
||||||
|
|||||||
+8
-2
@@ -165,7 +165,12 @@ export function createFacetPolicy(config) {
|
|||||||
*
|
*
|
||||||
* Inheritance is the same kind of test, and it is skipped when the user made a
|
* Inheritance is the same kind of test, and it is skipped when the user made a
|
||||||
* declaration of their own: that is their decision about *this* session, and
|
* declaration of their own: that is their decision about *this* session, and
|
||||||
* the point of inheriting is to answer the case where they made none.
|
* the point of inheriting is to answer the case where they made none. A child
|
||||||
|
* whose own directory is *outside* the inherited root is clamped to the
|
||||||
|
* inherited root rather than trusted with its own: a delegated session may be
|
||||||
|
* started anywhere, and "somewhere else" is the one direction the invariant
|
||||||
|
* forbids. A child started inside the inherited root keeps its own, narrower
|
||||||
|
* directory.
|
||||||
*
|
*
|
||||||
* @param {string|undefined} sessionId - the session.
|
* @param {string|undefined} sessionId - the session.
|
||||||
* @param {string} sessionCwd - its resolved working directory.
|
* @param {string} sessionCwd - its resolved working directory.
|
||||||
@@ -176,7 +181,8 @@ export function createFacetPolicy(config) {
|
|||||||
const declared = declaredScope(sessionId)?.focus
|
const declared = declaredScope(sessionId)?.focus
|
||||||
const stated = typeof declared === 'string' && declared.trim() !== ''
|
const stated = typeof declared === 'string' && declared.trim() !== ''
|
||||||
const own = stated ? resolve(sessionCwd, declared) : sessionCwd
|
const own = stated ? resolve(sessionCwd, declared) : sessionCwd
|
||||||
const base = !stated && inherited !== undefined && within(inherited, own) ? inherited : own
|
const base =
|
||||||
|
!stated && inherited !== undefined ? (within(own, inherited) ? own : inherited) : own
|
||||||
const agentFocus = sessionId === undefined ? undefined : narrowed.get(sessionId)
|
const agentFocus = sessionId === undefined ? undefined : narrowed.get(sessionId)
|
||||||
if (agentFocus !== undefined && within(agentFocus, base)) return agentFocus
|
if (agentFocus !== undefined && within(agentFocus, base)) return agentFocus
|
||||||
return base
|
return base
|
||||||
|
|||||||
+57
-6
@@ -42,16 +42,33 @@
|
|||||||
* reliably from argv, so the facet is enforced on the file tools and `bash`
|
* reliably from argv, so the facet is enforced on the file tools and `bash`
|
||||||
* remains a legitimate way out. The prompt says so rather than pretending
|
* remains a legitimate way out. The prompt says so rather than pretending
|
||||||
* otherwise.
|
* otherwise.
|
||||||
|
*
|
||||||
|
* The names are the union of what the harness's own tools declare and what the
|
||||||
|
* overrides this deployment installs declare. `dsh-better-edit` shadows `read`
|
||||||
|
* with `path` and `edit` with `file`, so a table written only against the
|
||||||
|
* built-in `file_path` left the busiest read tool unguarded. Where the tool's
|
||||||
|
* declared schema is available, {@link pathArgumentsFor} narrows this list to
|
||||||
|
* the names the tool actually declares and adds any name in
|
||||||
|
* {@link PATH_PARAMETER_NAMES}; the table is the fallback for a schema the
|
||||||
|
* guard cannot see. `working_directory` and the delegation tools are here
|
||||||
|
* because they change the directory later calls resolve against, and the child
|
||||||
|
* they start — both are part of the scope, not a separate one.
|
||||||
*/
|
*/
|
||||||
|
export const PATH_PARAMETER_NAMES = Object.freeze(['file_path', 'path', 'file', 'dir', 'directory'])
|
||||||
|
|
||||||
export const PATH_ARGUMENTS = Object.freeze({
|
export const PATH_ARGUMENTS = Object.freeze({
|
||||||
read: ['file_path'],
|
read: ['file_path', 'path'],
|
||||||
write: ['file_path'],
|
write: ['file_path', 'path'],
|
||||||
edit: ['file_path'],
|
edit: ['file_path', 'file', 'path'],
|
||||||
read_image: ['file_path'],
|
read_image: ['file_path', 'path'],
|
||||||
|
undo_last_edit: ['path'],
|
||||||
grep: ['path'],
|
grep: ['path'],
|
||||||
glob: ['path'],
|
glob: ['path'],
|
||||||
str_replace_editor: ['path'],
|
str_replace_editor: ['path'],
|
||||||
recall: ['path', 'focus'],
|
recall: ['path', 'focus'],
|
||||||
|
working_directory: ['cd'],
|
||||||
|
subagent: ['cwd'],
|
||||||
|
subagent_fork: ['cwd'],
|
||||||
})
|
})
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -80,6 +97,38 @@ export const IMPLICIT_SESSION_PATHS = Object.freeze(['grep', 'glob', 'recall'])
|
|||||||
export const FACET_TOOL = 'facet'
|
export const FACET_TOOL = 'facet'
|
||||||
export const WIDENING_ACTION = 'add'
|
export const WIDENING_ACTION = 'add'
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The parameter names the guard should read for one tool call.
|
||||||
|
*
|
||||||
|
* The table is the intent; the tool's declaration is the truth. When the call
|
||||||
|
* carries a schema (a PTC inner call) or the registry can supply one, the names
|
||||||
|
* are restricted to what the tool declares — that is what keeps a shadowed tool
|
||||||
|
* honest — and any declared parameter in {@link PATH_PARAMETER_NAMES} is added
|
||||||
|
* even when the table did not list it. With no readable schema the table is
|
||||||
|
* used unchanged: a declaration the guard cannot see is not evidence that the
|
||||||
|
* path went away.
|
||||||
|
*
|
||||||
|
* @param {string} toolName - the tool being called.
|
||||||
|
* @param {readonly string[]|undefined} declaredNames - property names from the
|
||||||
|
* tool's own parameter schema, when the guard can read it.
|
||||||
|
* @returns {readonly string[]|undefined} names to read, or undefined for an
|
||||||
|
* unguarded tool.
|
||||||
|
*/
|
||||||
|
export function pathParameterNames(toolName, declaredNames) {
|
||||||
|
const fallback = PATH_ARGUMENTS[toolName]
|
||||||
|
if (fallback === undefined) return undefined
|
||||||
|
if (declaredNames === undefined) return fallback
|
||||||
|
|
||||||
|
const declared = new Set(declaredNames)
|
||||||
|
const names = [
|
||||||
|
...new Set([
|
||||||
|
...fallback.filter((name) => declared.has(name)),
|
||||||
|
...declaredNames.filter((name) => PATH_PARAMETER_NAMES.includes(name)),
|
||||||
|
]),
|
||||||
|
]
|
||||||
|
return names.length > 0 ? names : fallback
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Every path a tool call names, as the model wrote them.
|
* Every path a tool call names, as the model wrote them.
|
||||||
*
|
*
|
||||||
@@ -95,13 +144,15 @@ export const WIDENING_ACTION = 'add'
|
|||||||
*
|
*
|
||||||
* @param {string} toolName - the tool being called.
|
* @param {string} toolName - the tool being called.
|
||||||
* @param {unknown} args - the raw model arguments.
|
* @param {unknown} args - the raw model arguments.
|
||||||
|
* @param {readonly string[]|undefined} [declaredNames] - property names from the
|
||||||
|
* tool's own parameter schema, when the guard can read it.
|
||||||
* @returns {string[]} raw paths, in argument order.
|
* @returns {string[]} raw paths, in argument order.
|
||||||
*/
|
*/
|
||||||
export function pathArgumentsFor(toolName, args) {
|
export function pathArgumentsFor(toolName, args, declaredNames) {
|
||||||
if (args === null || typeof args !== 'object') return []
|
if (args === null || typeof args !== 'object') return []
|
||||||
const record = /** @type {Record<string, unknown>} */ (args)
|
const record = /** @type {Record<string, unknown>} */ (args)
|
||||||
|
|
||||||
const names = PATH_ARGUMENTS[toolName]
|
const names = pathParameterNames(toolName, declaredNames)
|
||||||
if (names === undefined) return []
|
if (names === undefined) return []
|
||||||
|
|
||||||
const out = []
|
const out = []
|
||||||
|
|||||||
+109
-3
@@ -66,14 +66,25 @@ const fsLikeLocal = {
|
|||||||
* deployment that composed no approval service at all — a case the tool pipeline
|
* deployment that composed no approval service at all — a case the tool pipeline
|
||||||
* denies outright, so the guard must treat it as no channel.
|
* denies outright, so the guard must treat it as no channel.
|
||||||
*/
|
*/
|
||||||
function harness({ mode = 'ask', extraRoots = [], tool = true, config, approval } = {}) {
|
function harness({
|
||||||
|
mode = 'ask',
|
||||||
|
extraRoots = [],
|
||||||
|
tool = true,
|
||||||
|
config,
|
||||||
|
approval,
|
||||||
|
workingDirectory,
|
||||||
|
toolSchemas,
|
||||||
|
} = {}) {
|
||||||
const policy = createFacetPolicy(config ?? { mode, extraRoots })
|
const policy = createFacetPolicy(config ?? { mode, extraRoots })
|
||||||
const captured = { section: undefined, context: undefined, tool: undefined, preExecute: undefined }
|
const captured = { section: undefined, context: undefined, tool: undefined, preExecute: undefined }
|
||||||
const ctx = {
|
const ctx = {
|
||||||
facetPolicy: policy,
|
facetPolicy: policy,
|
||||||
fs: fsLikeLocal,
|
fs: fsLikeLocal,
|
||||||
get: (name) =>
|
get: (name) => {
|
||||||
name === 'approval' && approval !== undefined ? { effectivePolicy: () => approval } : undefined,
|
if (name === 'approval' && approval !== undefined) return { effectivePolicy: () => approval }
|
||||||
|
if (name === 'workingDirectory' && workingDirectory !== undefined) return workingDirectory
|
||||||
|
return undefined
|
||||||
|
},
|
||||||
systemPrompt: {
|
systemPrompt: {
|
||||||
section: (value) => {
|
section: (value) => {
|
||||||
captured.section = value
|
captured.section = value
|
||||||
@@ -86,6 +97,13 @@ function harness({ mode = 'ask', extraRoots = [], tool = true, config, approval
|
|||||||
register: (value) => {
|
register: (value) => {
|
||||||
captured.tool = value
|
captured.tool = value
|
||||||
},
|
},
|
||||||
|
// The registry lookup the guard uses to learn a shadowed tool's argument
|
||||||
|
// names; `toolSchemas` is a name-to-parameter-names map.
|
||||||
|
get: (name) => {
|
||||||
|
const names = toolSchemas?.[name]
|
||||||
|
if (names === undefined) return undefined
|
||||||
|
return { parameters: { properties: Object.fromEntries(names.map((parameter) => [parameter, {}])) } }
|
||||||
|
},
|
||||||
},
|
},
|
||||||
on: (event, listener) => {
|
on: (event, listener) => {
|
||||||
if (event === 'tools/pre-execute') captured.preExecute = listener
|
if (event === 'tools/pre-execute') captured.preExecute = listener
|
||||||
@@ -241,6 +259,94 @@ test('an agent with no working directory is not scoped', async () => {
|
|||||||
assert.deepEqual(await dispatch(captured, { name: 'read', args: { file_path: '/etc/hosts' } }), { kind: 'allow' })
|
assert.deepEqual(await dispatch(captured, { name: 'read', args: { file_path: '/etc/hosts' } }), { kind: 'allow' })
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// ── the working directory is part of the scope ──────────────────────────────
|
||||||
|
//
|
||||||
|
// The file tools resolve relative arguments against the session's *current*
|
||||||
|
// directory, not the header it started with. A guard that asked the header would
|
||||||
|
// answer for a path the tool never touches, and one that ignored
|
||||||
|
// `working_directory` would let a `cd` do the stepping out.
|
||||||
|
|
||||||
|
test('a working directory outside the facet cannot be entered', async () => {
|
||||||
|
const { captured } = harness({ mode: 'deny' })
|
||||||
|
const decision = await dispatch(captured, { name: 'working_directory', args: { cd: '../../shared' }, agent: agent() })
|
||||||
|
assert.equal(decision.kind, 'deny')
|
||||||
|
assert.match(decision.reason, /src\/shared/)
|
||||||
|
|
||||||
|
// Inside the facet is ordinary work, and reading the directory changes nothing.
|
||||||
|
assert.deepEqual(
|
||||||
|
await dispatch(captured, { name: 'working_directory', args: { cd: 'inner' }, agent: agent() }),
|
||||||
|
{ kind: 'allow' },
|
||||||
|
)
|
||||||
|
assert.deepEqual(await dispatch(captured, { name: 'working_directory', args: {}, agent: agent() }), {
|
||||||
|
kind: 'allow',
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
test('a relative path is judged against the directory the tool will use', async () => {
|
||||||
|
// The session header says `room`; the tool will resolve `types.ts` under
|
||||||
|
// `shared`, which is outside. Judging the header would call this inside.
|
||||||
|
const { captured } = harness({ mode: 'deny', workingDirectory: { get: () => join(root, 'src', 'shared') } })
|
||||||
|
const decision = await dispatch(captured, { name: 'read', args: { file_path: 'types.ts' }, agent: agent() })
|
||||||
|
assert.equal(decision.kind, 'deny')
|
||||||
|
// The reason is shown relative to the directory the tool will use.
|
||||||
|
assert.match(decision.reason, /types\.ts/)
|
||||||
|
assert.match(decision.reason, /src\/controllers\/room/, 'the reason names the facet')
|
||||||
|
})
|
||||||
|
|
||||||
|
test('a search with no path follows the current directory into the facet', async () => {
|
||||||
|
// The session is the facet's parent and the user narrowed to `inner`; the
|
||||||
|
// current directory is `inner`, so a bare grep searches inside the facet even
|
||||||
|
// though the session directory is outside it.
|
||||||
|
const { captured } = harness({
|
||||||
|
mode: 'deny',
|
||||||
|
config: { scopes: { 'session-1': { focus: 'inner' } } },
|
||||||
|
workingDirectory: { get: () => join(facet, 'inner') },
|
||||||
|
})
|
||||||
|
assert.deepEqual(await dispatch(captured, { name: 'grep', args: { pattern: 'x' }, agent: agent() }), {
|
||||||
|
kind: 'allow',
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
// ── shadowed tools ───────────────────────────────────────────────────────────
|
||||||
|
//
|
||||||
|
// A deployment plugin can replace a built-in tool with one that names its path
|
||||||
|
// argument differently — `dsh-better-edit` shadows `read` (path) and `edit`
|
||||||
|
// (file). The guard reads the declaration, so the shadow stays guarded.
|
||||||
|
|
||||||
|
test('a tool shadowed with a different argument name is still judged', async () => {
|
||||||
|
const { captured } = harness({ mode: 'deny', toolSchemas: { read: ['path', 'offset'], edit: ['file', 'edits'] } })
|
||||||
|
|
||||||
|
const read = await dispatch(captured, { name: 'read', args: { path: '../../shared/types.ts' }, agent: agent() })
|
||||||
|
assert.equal(read.kind, 'deny')
|
||||||
|
assert.match(read.reason, /src\/shared\/types\.ts/)
|
||||||
|
|
||||||
|
const edit = await dispatch(captured, {
|
||||||
|
name: 'edit',
|
||||||
|
args: { file: '../../shared/types.ts', edits: [] },
|
||||||
|
agent: agent(),
|
||||||
|
})
|
||||||
|
assert.equal(edit.kind, 'deny')
|
||||||
|
})
|
||||||
|
|
||||||
|
test('a delegated session cannot be started outside the facet', async () => {
|
||||||
|
const { captured } = harness({ mode: 'deny', toolSchemas: { subagent: ['cwd', 'description', 'prompt'] } })
|
||||||
|
assert.equal(
|
||||||
|
(
|
||||||
|
await dispatch(captured, {
|
||||||
|
name: 'subagent',
|
||||||
|
args: { cwd: '../../shared', description: 'x', prompt: 'y' },
|
||||||
|
agent: agent(),
|
||||||
|
})
|
||||||
|
).kind,
|
||||||
|
'deny',
|
||||||
|
)
|
||||||
|
// Omitted means the child inherits this directory, which is inside.
|
||||||
|
assert.deepEqual(
|
||||||
|
await dispatch(captured, { name: 'subagent', args: { description: 'x', prompt: 'y' }, agent: agent() }),
|
||||||
|
{ kind: 'allow' },
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
// ── delegated sessions ───────────────────────────────────────────────────────
|
// ── delegated sessions ───────────────────────────────────────────────────────
|
||||||
//
|
//
|
||||||
// A session spawned or forked from a scoped one has no declaration of its own,
|
// A session spawned or forked from a scoped one has no declaration of its own,
|
||||||
|
|||||||
@@ -222,6 +222,23 @@ test('a child is never wider than its parent is now, narrowing included', () =>
|
|||||||
assert.equal(policy.scopeFor(spawned('c1', 's1')).root, '/w/src/room')
|
assert.equal(policy.scopeFor(spawned('c1', 's1')).root, '/w/src/room')
|
||||||
})
|
})
|
||||||
|
|
||||||
|
test('a child started outside its parent facet is clamped to the parent root', () => {
|
||||||
|
const policy = createFacetPolicy({ mode: 'deny', scopes: { s1: { focus: 'src/room' } } })
|
||||||
|
assert.equal(policy.scopeFor(agentAt('/w', 's1')).root, '/w/src/room')
|
||||||
|
|
||||||
|
// The subagent tool takes a starting directory, and an agent can name one
|
||||||
|
// outside the facet. The child's scope must not widen to it: the invariant is
|
||||||
|
// one-directional, so a disjoint directory is clamped rather than trusted.
|
||||||
|
const escaped = spawned('c1', 's1', '/etc')
|
||||||
|
assert.equal(policy.scopeFor(escaped).root, '/w/src/room')
|
||||||
|
// Its own directory still governs how its relative paths resolve; that is the
|
||||||
|
// guard's question, decided against the inherited root.
|
||||||
|
assert.equal(policy.scopeFor(escaped).cwd, '/etc')
|
||||||
|
|
||||||
|
// A child started inside the parent's root keeps its own, narrower directory.
|
||||||
|
assert.equal(policy.scopeFor(spawned('c2', 's1', '/w/src/room/deep')).root, '/w/src/room/deep')
|
||||||
|
})
|
||||||
|
|
||||||
test('a declaration for the child itself stands over what it would inherit', () => {
|
test('a declaration for the child itself stands over what it would inherit', () => {
|
||||||
const policy = createFacetPolicy({ mode: 'deny', scopes: { s1: { focus: 'src/room' }, c1: { focus: '.' } } })
|
const policy = createFacetPolicy({ mode: 'deny', scopes: { s1: { focus: 'src/room' }, c1: { focus: '.' } } })
|
||||||
policy.scopeFor(agentAt('/w', 's1'))
|
policy.scopeFor(agentAt('/w', 's1'))
|
||||||
|
|||||||
@@ -40,6 +40,31 @@ test('pathArgumentsFor has no opinion where it cannot have one', () => {
|
|||||||
assert.deepEqual(pathArgumentsFor('grep', { path: { nested: '/etc' } }), [])
|
assert.deepEqual(pathArgumentsFor('grep', { path: { nested: '/etc' } }), [])
|
||||||
})
|
})
|
||||||
|
|
||||||
|
test('a shadowed tool is judged by the arguments its own schema declares', () => {
|
||||||
|
// `dsh-better-edit` replaces read/edit on the agent's layer with different
|
||||||
|
// argument names; trusting the built-in's `file_path` left the busiest read
|
||||||
|
// tool unguarded.
|
||||||
|
assert.deepEqual(pathArgumentsFor('read', { path: 'src/a.ts', limit: 10 }, ['path', 'limit']), ['src/a.ts'])
|
||||||
|
assert.deepEqual(pathArgumentsFor('edit', { file: 'src/a.ts', edits: [] }, ['file', 'edits']), ['src/a.ts'])
|
||||||
|
// A declared path-shaped name the table does not list is still found.
|
||||||
|
assert.deepEqual(pathArgumentsFor('write', { path: 'src/a.ts', content: 'x' }, ['path', 'content']), ['src/a.ts'])
|
||||||
|
// The table is the fallback when the schema cannot be read.
|
||||||
|
assert.deepEqual(pathArgumentsFor('read', { path: 'src/a.ts' }), ['src/a.ts'])
|
||||||
|
})
|
||||||
|
|
||||||
|
test('the tools that move the scope are judged like any other path', () => {
|
||||||
|
assert.deepEqual(pathArgumentsFor('working_directory', { cd: '../elsewhere' }, ['cd']), ['../elsewhere'])
|
||||||
|
assert.deepEqual(
|
||||||
|
pathArgumentsFor('subagent', { cwd: '../elsewhere', prompt: 'x' }, ['cwd', 'prompt']),
|
||||||
|
['../elsewhere'],
|
||||||
|
)
|
||||||
|
assert.deepEqual(pathArgumentsFor('subagent_fork', { cwd: 'sub', prompt: 'x' }, ['cwd', 'prompt']), ['sub'])
|
||||||
|
// Reading the directory names nothing, and moves nothing.
|
||||||
|
assert.deepEqual(pathArgumentsFor('working_directory', {}, ['cd']), [])
|
||||||
|
// The undo tool writes a file back, so it is a path like any other.
|
||||||
|
assert.deepEqual(pathArgumentsFor('undo_last_edit', { path: 'src/a.ts' }), ['src/a.ts'])
|
||||||
|
})
|
||||||
|
|
||||||
test('isWideningCall is true only for the one act that needs the user', () => {
|
test('isWideningCall is true only for the one act that needs the user', () => {
|
||||||
assert.equal(isWideningCall('facet', { action: 'add', paths: ['x'] }), true)
|
assert.equal(isWideningCall('facet', { action: 'add', paths: ['x'] }), true)
|
||||||
// Narrowing and reading cannot show the agent anything new.
|
// Narrowing and reading cannot show the agent anything new.
|
||||||
|
|||||||
Reference in new issue
Block a user