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:
NotExpectedYet committed 2026-11-09 08:30:07 +00:00
1 parent c48e9cabd9
commit 40574505f2
7 files changed
+309 -27

No files matched your search

+58 -9
View File
@@ -101,6 +101,49 @@ export function apply(ctx, config) {
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
// 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)
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) {
// 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
// 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
// "no opinion" there would hand the model a read the guard exists to
// refuse. The containment test is the same one; only the path is ours
// rather than the model's.
// is the current working directory, which a narrowed facet does not
// contain, and "no opinion" there would hand the model a read the guard
// exists to refuse. The containment test is the same one; only the path is
// ours rather than the model's.
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
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) {
const verdict = await inside(scope, raw, exec.signal)
const verdict = await inside(current, raw, exec.signal)
if (verdict.inside) continue
const kind = mode === 'deny' ? 'deny' : 'ask'
return {
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