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

+109 -3
View File
@@ -66,14 +66,25 @@ const fsLikeLocal = {
* deployment that composed no approval service at all — a case the tool pipeline
* 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 captured = { section: undefined, context: undefined, tool: undefined, preExecute: undefined }
const ctx = {
facetPolicy: policy,
fs: fsLikeLocal,
get: (name) =>
name === 'approval' && approval !== undefined ? { effectivePolicy: () => approval } : undefined,
get: (name) => {
if (name === 'approval' && approval !== undefined) return { effectivePolicy: () => approval }
if (name === 'workingDirectory' && workingDirectory !== undefined) return workingDirectory
return undefined
},
systemPrompt: {
section: (value) => {
captured.section = value
@@ -86,6 +97,13 @@ function harness({ mode = 'ask', extraRoots = [], tool = true, config, approval
register: (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) => {
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' })
})
// ── 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 ───────────────────────────────────────────────────────
//
// A session spawned or forked from a scoped one has no declaration of its own,