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
+109
-3
@@ -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,
|
||||
|
||||
@@ -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')
|
||||
})
|
||||
|
||||
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', () => {
|
||||
const policy = createFacetPolicy({ mode: 'deny', scopes: { s1: { focus: 'src/room' }, c1: { focus: '.' } } })
|
||||
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' } }), [])
|
||||
})
|
||||
|
||||
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', () => {
|
||||
assert.equal(isWideningCall('facet', { action: 'add', paths: ['x'] }), true)
|
||||
// Narrowing and reading cannot show the agent anything new.
|
||||
|
||||
Reference in new issue
Block a user