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
+58
-9
@@ -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
|
||||
|
||||
+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
|
||||
* 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} sessionCwd - its resolved working directory.
|
||||
@@ -176,7 +181,8 @@ export function createFacetPolicy(config) {
|
||||
const declared = declaredScope(sessionId)?.focus
|
||||
const stated = typeof declared === 'string' && declared.trim() !== ''
|
||||
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)
|
||||
if (agentFocus !== undefined && within(agentFocus, base)) return agentFocus
|
||||
return base
|
||||
|
||||
+57
-6
@@ -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 = []
|
||||
|
||||
Reference in new issue
Block a user