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

+57 -6
View File
@@ -42,16 +42,33 @@
* 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
* 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({
read: ['file_path'],
write: ['file_path'],
edit: ['file_path'],
read_image: ['file_path'],
read: ['file_path', 'path'],
write: ['file_path', 'path'],
edit: ['file_path', 'file', 'path'],
read_image: ['file_path', 'path'],
undo_last_edit: ['path'],
grep: ['path'],
glob: ['path'],
str_replace_editor: ['path'],
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 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.
*
@@ -95,13 +144,15 @@ export const WIDENING_ACTION = 'add'
*
* @param {string} toolName - the tool being called.
* @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.
*/
export function pathArgumentsFor(toolName, args) {
export function pathArgumentsFor(toolName, args, declaredNames) {
if (args === null || typeof args !== 'object') return []
const record = /** @type {Record<string, unknown>} */ (args)
const names = PATH_ARGUMENTS[toolName]
const names = pathParameterNames(toolName, declaredNames)
if (names === undefined) return []
const out = []