@operad-io/access-match 0.1.1 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/access-grants.schema.json +54 -0
- package/package.json +3 -2
- package/src/command.ts +100 -0
- package/src/index.ts +446 -24
- package/src/outward.ts +302 -0
- package/src/readonly.ts +71 -9
- package/src/summary.ts +176 -1
- package/src/target-grants.ts +258 -0
package/src/index.ts
CHANGED
|
@@ -2,12 +2,192 @@
|
|
|
2
2
|
* The access profile matcher. `README.md` holds the tables the tests follow.
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
|
+
import { readFileSync } from "node:fs";
|
|
5
6
|
import { homedir } from "node:os";
|
|
6
|
-
import { posix } from "node:path";
|
|
7
|
+
import { dirname, posix } from "node:path";
|
|
8
|
+
import { writeJsonAtomic } from "@operad-io/engine/platform";
|
|
7
9
|
export { isReadOnlyCommand } from "./readonly.ts";
|
|
8
10
|
export { COMMAND_SUMMARY_CHARS, SUMMARY_CHARS, COMMAND_NOT_SHOWN, REFUSED_IN_LOG, summarizeCall, firstErrorLine, messageSummary, secretValuesOf, type SummaryOptions } from "./summary.ts";
|
|
11
|
+
export { OUTWARD_ALLOW_TARGET, OUTWARD_REJECT_ONCE, OUTWARD_SEND_ONCE, declaredOutward, guardDeclaredOutward, guardOutward, outwardAllowed, outwardCommandWrites, outwardQuestion, personStarted, type OutwardGuardContext, type OutwardQuestionOptions } from "./outward.ts";
|
|
12
|
+
export { ACCESS_GRANTS_FILE, accessGrantsPath, appendTargetGrant, flowDefinitionDigest, readTargetGrants, revokeTargetGrants, targetGrantFor, type FlowDefinitionIdentity, type TargetGrant, type TargetGrantFile, type TargetGrantRead } from "./target-grants.ts";
|
|
13
|
+
import type { OutwardWrite } from "@operad-io/engine";
|
|
14
|
+
export type { OutwardWrite } from "@operad-io/engine";
|
|
15
|
+
import { outwardAllowed, outwardCommandWrites } from "./outward.ts";
|
|
9
16
|
import { isReadOnlyCommand } from "./readonly.ts";
|
|
10
|
-
import { findDenied, inlineFormOf, parseRule, scan, type Part, type Rule } from "./command.ts";
|
|
17
|
+
import { findDenied, inlineFormOf, parseRule, scan, unreadableContentsOf, type Part, type Rule } from "./command.ts";
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* The rules of the built-in default profile. A chat and a generated step start
|
|
21
|
+
* from these; a bot, a repository or a flow may add to them, never remove them
|
|
22
|
+
* (ADR 0025).
|
|
23
|
+
*/
|
|
24
|
+
const DEFAULT_DENY_PATHS = [
|
|
25
|
+
"~/.ssh",
|
|
26
|
+
"~/.gnupg",
|
|
27
|
+
"~/.aws",
|
|
28
|
+
"~/.config/gh",
|
|
29
|
+
"~/.radian",
|
|
30
|
+
"~/Library/Keychains",
|
|
31
|
+
"~/Library/Application Support/Google/Chrome",
|
|
32
|
+
"~/Library/Application Support/Firefox",
|
|
33
|
+
"~/Library/Safari",
|
|
34
|
+
".git/config",
|
|
35
|
+
".git/hooks",
|
|
36
|
+
] as const;
|
|
37
|
+
|
|
38
|
+
const DEFAULT_DENY_COMMANDS = [
|
|
39
|
+
"sudo",
|
|
40
|
+
"su",
|
|
41
|
+
"rm -rf /",
|
|
42
|
+
"rm -rf ~",
|
|
43
|
+
"mkfs",
|
|
44
|
+
"dd",
|
|
45
|
+
"diskutil",
|
|
46
|
+
"shutdown",
|
|
47
|
+
"reboot",
|
|
48
|
+
"launchctl",
|
|
49
|
+
"crontab -r",
|
|
50
|
+
"git push --force*",
|
|
51
|
+
"git push --delete",
|
|
52
|
+
"curl … | sh",
|
|
53
|
+
"wget … | sh",
|
|
54
|
+
"pkill",
|
|
55
|
+
"killall",
|
|
56
|
+
] as const;
|
|
57
|
+
|
|
58
|
+
const DEFAULT_ASK_COMMANDS = ["git push", "brew install", "npm install -g", "pip install", "osascript", "open"] as const;
|
|
59
|
+
|
|
60
|
+
const DEFAULT_ASK_PATHS = ["~/.zshrc", "~/.*profile"] as const;
|
|
61
|
+
|
|
62
|
+
/** Shell commands that write outside operad; a git push target is its remote and branch. */
|
|
63
|
+
const DEFAULT_OUTWARD_COMMANDS = [
|
|
64
|
+
"git push",
|
|
65
|
+
"gh pr create",
|
|
66
|
+
"gh pr comment",
|
|
67
|
+
"gh issue create",
|
|
68
|
+
"gh issue comment",
|
|
69
|
+
"gh release create",
|
|
70
|
+
"curl",
|
|
71
|
+
"wget",
|
|
72
|
+
"mail",
|
|
73
|
+
"mailx",
|
|
74
|
+
"sendmail",
|
|
75
|
+
] as const;
|
|
76
|
+
|
|
77
|
+
/** Targets a background run may write to without asking; `owner` is the owner's own channels. */
|
|
78
|
+
const DEFAULT_OUTWARD_ALLOW = ["owner"] as const;
|
|
79
|
+
|
|
80
|
+
/** The access levels a chat may set, in the order a menu shows them. */
|
|
81
|
+
export const ACCESS_LEVELS = ["plan", "ask", "edits", "full"] as const;
|
|
82
|
+
|
|
83
|
+
export type AccessLevel = (typeof ACCESS_LEVELS)[number];
|
|
84
|
+
|
|
85
|
+
/** Whether a value names one of the access levels. */
|
|
86
|
+
export function isAccessLevel(value: unknown): value is AccessLevel {
|
|
87
|
+
return typeof value === "string" && (ACCESS_LEVELS as readonly string[]).includes(value);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* A chat's profile with its level composed into it. The profile is first
|
|
92
|
+
* tightened with the built-in default, so every level, `full` included, keeps
|
|
93
|
+
* its deny rules. Below `full` it also keeps the default's ask rules; at `full`
|
|
94
|
+
* the profile's asks and outward questions do not apply.
|
|
95
|
+
*/
|
|
96
|
+
export function withAccessLevel<T extends AccessProfile>(
|
|
97
|
+
profile: T,
|
|
98
|
+
level: AccessLevel,
|
|
99
|
+
): T & { readonly level: AccessLevel } {
|
|
100
|
+
return {
|
|
101
|
+
...withDefaultAccess(profile),
|
|
102
|
+
level,
|
|
103
|
+
...(level === "plan" ? { readOnly: true } : {}),
|
|
104
|
+
} as T & { readonly level: AccessLevel };
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/** What a chat keeps in its live-level file: the profile without a level, and the level now in force. */
|
|
108
|
+
export interface LiveAccessFile {
|
|
109
|
+
readonly access: Readonly<Record<string, unknown>>;
|
|
110
|
+
readonly level: AccessLevel;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function liveAccessFileOf(value: unknown): LiveAccessFile | undefined {
|
|
114
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) return undefined;
|
|
115
|
+
const access: unknown = Reflect.get(value, "access");
|
|
116
|
+
const level: unknown = Reflect.get(value, "level");
|
|
117
|
+
if (typeof access !== "object" || access === null || Array.isArray(access)) return undefined;
|
|
118
|
+
if (!isAccessLevel(level)) return undefined;
|
|
119
|
+
return { access: access as Readonly<Record<string, unknown>>, level };
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** The live-level file at `path`, or undefined when it is absent or unreadable. */
|
|
123
|
+
export function readLiveAccess(path: string): LiveAccessFile | undefined {
|
|
124
|
+
try {
|
|
125
|
+
return liveAccessFileOf(JSON.parse(readFileSync(path, "utf8")));
|
|
126
|
+
} catch {
|
|
127
|
+
return undefined;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** Whether this profile has a live level a harness reads again for each call. */
|
|
132
|
+
export function hasLiveAccess(profile: unknown): boolean {
|
|
133
|
+
return (
|
|
134
|
+
typeof profile === "object" &&
|
|
135
|
+
profile !== null &&
|
|
136
|
+
!Array.isArray(profile) &&
|
|
137
|
+
typeof Reflect.get(profile, "levelFile") === "string" &&
|
|
138
|
+
String(Reflect.get(profile, "levelFile")).length > 0
|
|
139
|
+
);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* The profile with the level its chat has now. The level file holds the base
|
|
144
|
+
* profile, so a level change can add or remove the level's own asks; the run's
|
|
145
|
+
* one-time grants, its always-allowed entries and its background flag come
|
|
146
|
+
* from the profile the harness was started with and stay.
|
|
147
|
+
*/
|
|
148
|
+
export function liveAccess<T extends AccessProfile>(profile: T): T & AccessProfile {
|
|
149
|
+
const path = profile.levelFile;
|
|
150
|
+
if (path === undefined) return profile;
|
|
151
|
+
const file = readLiveAccess(path);
|
|
152
|
+
if (file === undefined) return profile;
|
|
153
|
+
// The chat's own directory is denied in the base profile. A file that no
|
|
154
|
+
// longer holds that deny was not written by `chat:access`, so its level is
|
|
155
|
+
// not taken and the profile the harness started with stays in force.
|
|
156
|
+
const requiredDeny = dirname(path);
|
|
157
|
+
const denied: unknown = file.access["denyPaths"];
|
|
158
|
+
if (!Array.isArray(denied) || !denied.some((entry) => entry === requiredDeny || entry === path)) {
|
|
159
|
+
return profile;
|
|
160
|
+
}
|
|
161
|
+
return {
|
|
162
|
+
...withAccessLevel(file.access, file.level),
|
|
163
|
+
levelFile: path,
|
|
164
|
+
...(profile.background === true ? { background: true } : {}),
|
|
165
|
+
...(profile.grants === undefined ? {} : { grants: profile.grants }),
|
|
166
|
+
...(profile.alwaysAllow === undefined ? {} : { alwaysAllow: profile.alwaysAllow }),
|
|
167
|
+
} as T & AccessProfile;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/** Writes the base profile and the level now in force, replacing the file whole. */
|
|
171
|
+
export async function writeLiveAccess(
|
|
172
|
+
path: string,
|
|
173
|
+
access: Readonly<Record<string, unknown>>,
|
|
174
|
+
level: AccessLevel,
|
|
175
|
+
): Promise<void> {
|
|
176
|
+
await writeJsonAtomic(
|
|
177
|
+
path,
|
|
178
|
+
{ access, level },
|
|
179
|
+
{ space: 2, newline: false, mode: 0o600 },
|
|
180
|
+
);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/** Changes only the level of a live-level file, keeping the base profile it already holds. */
|
|
184
|
+
export async function updateLiveAccessLevel(
|
|
185
|
+
path: string,
|
|
186
|
+
level: AccessLevel,
|
|
187
|
+
fallback: Readonly<Record<string, unknown>> = {},
|
|
188
|
+
): Promise<void> {
|
|
189
|
+
await writeLiveAccess(path, readLiveAccess(path)?.access ?? fallback, level);
|
|
190
|
+
}
|
|
11
191
|
|
|
12
192
|
/** A one-time permission for the exact call a person was asked about. */
|
|
13
193
|
export interface AccessGrant {
|
|
@@ -33,6 +213,18 @@ export interface AlwaysAllowEntry {
|
|
|
33
213
|
}
|
|
34
214
|
|
|
35
215
|
export interface AccessProfile {
|
|
216
|
+
/**
|
|
217
|
+
* A chat's access level. Below `full` it adds asks to the profile; at `full`
|
|
218
|
+
* only a deny, `readOnly` and a write under `readPaths` remain. See the level
|
|
219
|
+
* rules in `REFERENCE.md`.
|
|
220
|
+
*/
|
|
221
|
+
readonly level?: AccessLevel | undefined;
|
|
222
|
+
/**
|
|
223
|
+
* A file a chat keeps its live level in, internal to operad. A harness reads
|
|
224
|
+
* it again for each call, so a level change reaches the calls after it even
|
|
225
|
+
* while the reply keeps its worker. It is not a user setting.
|
|
226
|
+
*/
|
|
227
|
+
readonly levelFile?: string | undefined;
|
|
36
228
|
readonly denyPaths?: readonly string[] | undefined;
|
|
37
229
|
readonly denyCommands?: readonly string[] | undefined;
|
|
38
230
|
/** Command rules in the shape of `denyCommands`; a match asks a person instead of refusing. */
|
|
@@ -43,6 +235,14 @@ export interface AccessProfile {
|
|
|
43
235
|
* `edit` tools, and for a `bash` redirect (`>`, `>>`) to such a path.
|
|
44
236
|
*/
|
|
45
237
|
readonly askPaths?: readonly string[] | undefined;
|
|
238
|
+
/**
|
|
239
|
+
* False when the profile starts from the built-in default's deny rules only:
|
|
240
|
+
* its `askCommands` and `askPaths` are not added. The profile's own asks
|
|
241
|
+
* still hold. A flow profile still only adds; there is no way to drop a
|
|
242
|
+
* default deny. Every step a dev task runs uses this so its scripted edits
|
|
243
|
+
* run without a question (ADR 0025 D9).
|
|
244
|
+
*/
|
|
245
|
+
readonly defaultAsks?: boolean | undefined;
|
|
46
246
|
/**
|
|
47
247
|
* The working directory is read-only: a write there (the `write` and `edit`
|
|
48
248
|
* tools, a `bash` redirect) asks, as if the directory were in `askPaths`; a
|
|
@@ -64,6 +264,21 @@ export interface AccessProfile {
|
|
|
64
264
|
* `docs/backlog/operad-sandbox.md`, not in an ADR yet).
|
|
65
265
|
*/
|
|
66
266
|
readonly allowMcp?: readonly string[] | undefined;
|
|
267
|
+
/**
|
|
268
|
+
* Targets a background run may write to without asking: a channel and
|
|
269
|
+
* conversation (`telegram:123`), an address, a repository and branch
|
|
270
|
+
* (`origin/main`), or a service. `owner` stands for the owner's own channels.
|
|
271
|
+
*/
|
|
272
|
+
readonly outwardAllow?: readonly string[] | undefined;
|
|
273
|
+
/** Shell commands that write outside operad; a git push names its remote and branch. */
|
|
274
|
+
readonly outwardCommands?: readonly string[] | undefined;
|
|
275
|
+
/**
|
|
276
|
+
* Set by a harness on a run no person's message started. Below `full` it
|
|
277
|
+
* makes an outward shell command or MCP tool ask even when no other rule
|
|
278
|
+
* does, and it makes the matcher ignore `alwaysAllow` (ADR 0025 D3 and D10).
|
|
279
|
+
* At `full` it adds no outward question.
|
|
280
|
+
*/
|
|
281
|
+
readonly background?: boolean | undefined;
|
|
67
282
|
/** Permissions not yet used; the caller uses one up before it lets a call through. */
|
|
68
283
|
readonly grants?: readonly AccessGrant[] | undefined;
|
|
69
284
|
/**
|
|
@@ -75,6 +290,75 @@ export interface AccessProfile {
|
|
|
75
290
|
readonly alwaysAllow?: readonly AlwaysAllowEntry[] | undefined;
|
|
76
291
|
}
|
|
77
292
|
|
|
293
|
+
/** The built-in default access profile every chat and generated step starts from. */
|
|
294
|
+
export function defaultAccess(): AccessProfile {
|
|
295
|
+
return {
|
|
296
|
+
denyPaths: [...DEFAULT_DENY_PATHS],
|
|
297
|
+
denyCommands: [...DEFAULT_DENY_COMMANDS],
|
|
298
|
+
askCommands: [...DEFAULT_ASK_COMMANDS],
|
|
299
|
+
askPaths: [...DEFAULT_ASK_PATHS],
|
|
300
|
+
outwardCommands: [...DEFAULT_OUTWARD_COMMANDS],
|
|
301
|
+
outwardAllow: [...DEFAULT_OUTWARD_ALLOW],
|
|
302
|
+
};
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
/**
|
|
306
|
+
* The built-in default's deny rules only. Every agent a dev task runs starts
|
|
307
|
+
* from these and not from the default's asks, so its scripted edits run
|
|
308
|
+
* without a question (ADR 0025 D9).
|
|
309
|
+
*/
|
|
310
|
+
export function defaultDenies(): AccessProfile {
|
|
311
|
+
return {
|
|
312
|
+
denyPaths: [...DEFAULT_DENY_PATHS],
|
|
313
|
+
denyCommands: [...DEFAULT_DENY_COMMANDS],
|
|
314
|
+
};
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
function combined(
|
|
318
|
+
base: readonly string[] | undefined,
|
|
319
|
+
extra: readonly string[] | undefined,
|
|
320
|
+
): string[] | undefined {
|
|
321
|
+
if (base === undefined && extra === undefined) return undefined;
|
|
322
|
+
const list = [...(base ?? [])];
|
|
323
|
+
for (const entry of extra ?? []) if (!list.includes(entry)) list.push(entry);
|
|
324
|
+
return list;
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
/**
|
|
328
|
+
* The built-in default with `profile`'s rules added. It keeps every deny and
|
|
329
|
+
* ask of the default; a profile with `defaultAsks: false` starts from the
|
|
330
|
+
* default's denies only, and its own ask rules still hold. A field the default
|
|
331
|
+
* does not set is kept as given.
|
|
332
|
+
*/
|
|
333
|
+
export function withDefaultAccess<T extends AccessProfile>(profile?: T | undefined): T & AccessProfile {
|
|
334
|
+
const base = profile?.defaultAsks === false ? defaultDenies() : defaultAccess();
|
|
335
|
+
const denyPaths = combined(base.denyPaths, profile?.denyPaths);
|
|
336
|
+
const denyCommands = combined(base.denyCommands, profile?.denyCommands);
|
|
337
|
+
const askCommands = combined(base.askCommands, profile?.askCommands);
|
|
338
|
+
const askPaths = combined(base.askPaths, profile?.askPaths);
|
|
339
|
+
const readPaths = combined(base.readPaths, profile?.readPaths);
|
|
340
|
+
const outwardCommands = combined(base.outwardCommands, profile?.outwardCommands);
|
|
341
|
+
const outwardAllow = combined(base.outwardAllow, profile?.outwardAllow);
|
|
342
|
+
const readOnly = base.readOnly === true || profile?.readOnly === true;
|
|
343
|
+
return {
|
|
344
|
+
...(profile ?? {}),
|
|
345
|
+
denyPaths,
|
|
346
|
+
denyCommands,
|
|
347
|
+
askCommands,
|
|
348
|
+
askPaths,
|
|
349
|
+
...(readPaths === undefined ? {} : { readPaths }),
|
|
350
|
+
...(outwardCommands === undefined ? {} : { outwardCommands }),
|
|
351
|
+
...(outwardAllow === undefined ? {} : { outwardAllow }),
|
|
352
|
+
...(profile?.background === true ? { background: true } : {}),
|
|
353
|
+
...(readOnly ? { readOnly: true } : {}),
|
|
354
|
+
...(base.allowMcp === undefined
|
|
355
|
+
? profile?.allowMcp === undefined
|
|
356
|
+
? {}
|
|
357
|
+
: { allowMcp: profile.allowMcp }
|
|
358
|
+
: { allowMcp: profile?.allowMcp ?? base.allowMcp }),
|
|
359
|
+
} as T & AccessProfile;
|
|
360
|
+
}
|
|
361
|
+
|
|
78
362
|
export interface ToolCall {
|
|
79
363
|
readonly tool: string;
|
|
80
364
|
readonly input: Readonly<Record<string, unknown>>;
|
|
@@ -111,6 +395,8 @@ export type Verdict =
|
|
|
111
395
|
readonly kind: "command" | "path";
|
|
112
396
|
readonly value: string;
|
|
113
397
|
readonly tool?: string;
|
|
398
|
+
/** The outward write this question is about, when the run is a background run. */
|
|
399
|
+
readonly outward?: OutwardWrite | undefined;
|
|
114
400
|
};
|
|
115
401
|
|
|
116
402
|
export interface MatchOptions {
|
|
@@ -127,13 +413,37 @@ export function readProfile(value: unknown): ProfileRead {
|
|
|
127
413
|
if (typeof value !== "object" || value === null || Array.isArray(value)) {
|
|
128
414
|
return { ok: false, message: "the access profile must be an object" };
|
|
129
415
|
}
|
|
130
|
-
const found: { denyPaths?: string[]; denyCommands?: string[]; askCommands?: string[]; askPaths?: string[]; readPaths?: string[]; allowMcp?: string[]; readOnly?: boolean; grants?: AccessGrant[]; alwaysAllow?: AlwaysAllowEntry[] } = {};
|
|
416
|
+
const found: { level?: AccessLevel; levelFile?: string; defaultAsks?: boolean; denyPaths?: string[]; denyCommands?: string[]; askCommands?: string[]; askPaths?: string[]; readPaths?: string[]; allowMcp?: string[]; outwardAllow?: string[]; outwardCommands?: string[]; readOnly?: boolean; background?: boolean; grants?: AccessGrant[]; alwaysAllow?: AlwaysAllowEntry[] } = {};
|
|
417
|
+
const levelFile: unknown = Reflect.get(value, "levelFile");
|
|
418
|
+
if (levelFile !== undefined) {
|
|
419
|
+
if (typeof levelFile !== "string" || levelFile.length === 0) {
|
|
420
|
+
return { ok: false, message: "levelFile must be a non-empty string" };
|
|
421
|
+
}
|
|
422
|
+
found.levelFile = levelFile;
|
|
423
|
+
}
|
|
424
|
+
const level: unknown = Reflect.get(value, "level");
|
|
425
|
+
if (level !== undefined) {
|
|
426
|
+
if (!isAccessLevel(level)) {
|
|
427
|
+
return { ok: false, message: `level must be one of ${ACCESS_LEVELS.join(", ")}` };
|
|
428
|
+
}
|
|
429
|
+
found.level = level;
|
|
430
|
+
}
|
|
431
|
+
const defaultAsks: unknown = Reflect.get(value, "defaultAsks");
|
|
432
|
+
if (defaultAsks !== undefined) {
|
|
433
|
+
if (typeof defaultAsks !== "boolean") return { ok: false, message: "defaultAsks must be true or false" };
|
|
434
|
+
found.defaultAsks = defaultAsks;
|
|
435
|
+
}
|
|
131
436
|
const readOnly: unknown = Reflect.get(value, "readOnly");
|
|
132
437
|
if (readOnly !== undefined) {
|
|
133
438
|
if (typeof readOnly !== "boolean") return { ok: false, message: "readOnly must be true or false" };
|
|
134
439
|
found.readOnly = readOnly;
|
|
135
440
|
}
|
|
136
|
-
|
|
441
|
+
const background: unknown = Reflect.get(value, "background");
|
|
442
|
+
if (background !== undefined) {
|
|
443
|
+
if (typeof background !== "boolean") return { ok: false, message: "background must be true or false" };
|
|
444
|
+
found.background = background;
|
|
445
|
+
}
|
|
446
|
+
for (const field of ["denyPaths", "denyCommands", "askCommands", "askPaths", "readPaths", "allowMcp", "outwardAllow", "outwardCommands"] as const) {
|
|
137
447
|
const entry: unknown = Reflect.get(value, field);
|
|
138
448
|
if (entry === undefined) continue;
|
|
139
449
|
if (!Array.isArray(entry) || !entry.every((item): item is string => typeof item === "string")) {
|
|
@@ -396,9 +706,15 @@ function checkBash(profile: AccessProfile, command: string, denied: Denied | und
|
|
|
396
706
|
.map((text) => parseRule(text))
|
|
397
707
|
.filter((rule): rule is Rule => rule !== undefined);
|
|
398
708
|
const { parts, substitution, extglob, ansiC } = scan(command);
|
|
709
|
+
// A task worker and a chat at full are never stopped by what the matcher
|
|
710
|
+
// cannot follow: only a plain match of a deny refuses them (ADR 0025 D8, D9).
|
|
711
|
+
const unreadableAllowed = profile.defaultAsks === false || profile.level === "full";
|
|
399
712
|
|
|
400
713
|
if ((profile.denyCommands ?? []).length > 0) {
|
|
401
|
-
|
|
714
|
+
// A command that hides what it runs cannot be matched: when the profile
|
|
715
|
+
// also asks, askBash asks about it instead of refusing it here.
|
|
716
|
+
const asksInline = (profile.askCommands ?? []).length > 0;
|
|
717
|
+
if (substitution !== undefined && !asksInline && !unreadableAllowed) {
|
|
402
718
|
return {
|
|
403
719
|
allow: false,
|
|
404
720
|
reason: `command "${command}" runs inline code (${substitution}); it is refused while denyCommands is set`,
|
|
@@ -406,7 +722,7 @@ function checkBash(profile: AccessProfile, command: string, denied: Denied | und
|
|
|
406
722
|
}
|
|
407
723
|
for (const part of parts) {
|
|
408
724
|
const form = inlineFormOf(part);
|
|
409
|
-
if (form !== undefined) {
|
|
725
|
+
if (form !== undefined && !asksInline && !unreadableAllowed) {
|
|
410
726
|
return {
|
|
411
727
|
allow: false,
|
|
412
728
|
reason: `part "${part.text}" runs inline code (${form}); it is refused while denyCommands is set`,
|
|
@@ -419,6 +735,15 @@ function checkBash(profile: AccessProfile, command: string, denied: Denied | und
|
|
|
419
735
|
}
|
|
420
736
|
}
|
|
421
737
|
|
|
738
|
+
// When the inline code or expansion itself is let through, a deny it plainly
|
|
739
|
+
// shows still refuses it (ADR 0025 D8, D9).
|
|
740
|
+
if (unreadableAllowed && (rules.length > 0 || denied !== undefined)) {
|
|
741
|
+
for (const content of unreadableContentsOf(command, parts)) {
|
|
742
|
+
const inner = checkBash(profile, content, denied);
|
|
743
|
+
if (!inner.allow) return inner;
|
|
744
|
+
}
|
|
745
|
+
}
|
|
746
|
+
|
|
422
747
|
if (denied !== undefined) {
|
|
423
748
|
const { words, unresolvedCd } = bashWords(parts, denied.cwd, denied.home);
|
|
424
749
|
let unresolved = unresolvedCd ? "a cd target" : substitution === undefined ? undefined : `a command substitution (${substitution})`;
|
|
@@ -441,7 +766,7 @@ function checkBash(profile: AccessProfile, command: string, denied: Denied | und
|
|
|
441
766
|
}
|
|
442
767
|
}
|
|
443
768
|
// A deny wins over this ask: it is only reached when no path was denied.
|
|
444
|
-
if (unresolved !== undefined) {
|
|
769
|
+
if (unresolved !== undefined && !unreadableAllowed) {
|
|
445
770
|
return {
|
|
446
771
|
allow: false,
|
|
447
772
|
ask: true,
|
|
@@ -489,10 +814,13 @@ function redirectTarget(word: string, next: string | undefined): string | undefi
|
|
|
489
814
|
/**
|
|
490
815
|
* The ask verdict of a command, when no deny applied. A command that hides what
|
|
491
816
|
* it runs (inline code) cannot be matched, so it asks while `askCommands` is
|
|
492
|
-
* set (ADR 0025 D3).
|
|
817
|
+
* set (ADR 0025 D3). At `full` the profile's ask rules and the inline fallback
|
|
818
|
+
* do not apply (ADR 0025 D8); only the readOnly and readPaths entries passed in
|
|
819
|
+
* `asked` still ask. A task worker keeps the rule of D9.
|
|
493
820
|
*/
|
|
494
821
|
function askBash(profile: AccessProfile, command: string, asked: AskedPaths | undefined, cwd: string, home: string): Verdict {
|
|
495
|
-
const
|
|
822
|
+
const full = profile.level === "full";
|
|
823
|
+
const texts = full ? [] : profile.askCommands ?? [];
|
|
496
824
|
let found: { readonly target: string; readonly rule: string } | undefined;
|
|
497
825
|
if (texts.length === 0 && asked === undefined) return ALLOW;
|
|
498
826
|
const rules = texts.map((text) => parseRule(text)).filter((rule): rule is Rule => rule !== undefined);
|
|
@@ -522,17 +850,17 @@ function askBash(profile: AccessProfile, command: string, asked: AskedPaths | un
|
|
|
522
850
|
if (found !== undefined) return ask(`command "${command}" writes "${found.target}", which is under askPaths entry "${found.rule}"`, found.rule);
|
|
523
851
|
if (redirects && walked.unresolvedCd) unresolved ??= "a cd target";
|
|
524
852
|
if (redirects && substitution !== undefined) unresolved ??= `a command substitution (${substitution})`;
|
|
525
|
-
if (unresolved !== undefined) {
|
|
853
|
+
if (unresolved !== undefined && !full) {
|
|
526
854
|
return ask(`command "${command}" writes to ${unresolved}, which holds a shell expansion that cannot be matched against askPaths; it asks`, "inline code");
|
|
527
855
|
}
|
|
528
856
|
}
|
|
529
857
|
if (texts.length === 0) return ALLOW;
|
|
530
|
-
if (substitution !== undefined) {
|
|
858
|
+
if (substitution !== undefined && !full) {
|
|
531
859
|
return ask(`command "${command}" runs inline code (${substitution}); it asks while askCommands is set`, "inline code");
|
|
532
860
|
}
|
|
533
861
|
for (const part of parts) {
|
|
534
862
|
const form = inlineFormOf(part);
|
|
535
|
-
if (form !== undefined) {
|
|
863
|
+
if (form !== undefined && !full) {
|
|
536
864
|
return ask(`part "${part.text}" runs inline code (${form}); it asks while askCommands is set`, "inline code");
|
|
537
865
|
}
|
|
538
866
|
}
|
|
@@ -574,14 +902,30 @@ function matchMcp(profile: AccessProfile, call: ToolCall): Verdict {
|
|
|
574
902
|
* over ask and ask over allow: the deny rules and paths are checked first, and
|
|
575
903
|
* `askCommands` only when nothing denied the call.
|
|
576
904
|
*/
|
|
905
|
+
/**
|
|
906
|
+
* Whether the level alone makes a write ask. A level never lifts an ask from
|
|
907
|
+
* the profile; the caller checks the profile's own asks first.
|
|
908
|
+
*/
|
|
909
|
+
function levelAsksWrite(level: AccessLevel | undefined, normal: string, cwd: string): boolean {
|
|
910
|
+
if (level === "ask") return true;
|
|
911
|
+
if (level === "edits") return !isUnder(normal, posix.resolve(cwd));
|
|
912
|
+
return false;
|
|
913
|
+
}
|
|
914
|
+
|
|
577
915
|
export function match(given: AccessProfile, call: ToolCall, options: MatchOptions = {}): Verdict {
|
|
578
916
|
const home = options.home ?? homedir();
|
|
579
917
|
// A read-only working directory is a directory whose writes ask.
|
|
580
918
|
// A directory that may be read is one whose writes ask.
|
|
581
919
|
const readPaths = (given.readPaths ?? []).filter((written) => written.length > 0);
|
|
582
|
-
const
|
|
583
|
-
const
|
|
584
|
-
|
|
920
|
+
const readOnly = given.readOnly === true || given.level === "plan";
|
|
921
|
+
const full = given.level === "full";
|
|
922
|
+
// These asks come from `readOnly` and `readPaths`, not from the profile's own
|
|
923
|
+
// ask list, so they still ask at `full`.
|
|
924
|
+
const structuralAsks = [...(readOnly ? [call.cwd] : []), ...readPaths];
|
|
925
|
+
const profile: AccessProfile = {
|
|
926
|
+
...given,
|
|
927
|
+
askPaths: full ? structuralAsks : [...(given.askPaths ?? []), ...structuralAsks],
|
|
928
|
+
};
|
|
585
929
|
const entries = (profile.denyPaths ?? [])
|
|
586
930
|
.filter((written) => written.length > 0)
|
|
587
931
|
.map((written) => ({ written, path: normalise(written, call.cwd, home) }));
|
|
@@ -599,20 +943,86 @@ export function match(given: AccessProfile, call: ToolCall, options: MatchOption
|
|
|
599
943
|
const verdict = checkBash(profile, command, denied);
|
|
600
944
|
// A deny stays a deny; only an ask can be lifted by an always-allowed entry.
|
|
601
945
|
if (!verdict.allow && verdict.ask !== true) return verdict;
|
|
602
|
-
|
|
603
|
-
if (
|
|
946
|
+
// A background run ignores the user layer; see ADR 0025 D3 and D10.
|
|
947
|
+
if (given.background !== true && alwaysAllowed(profile, call)) return ALLOW;
|
|
948
|
+
// An outward write not on the allowlist asks even without a matching rule,
|
|
949
|
+
// below `full`; at `full` an outward question does not apply.
|
|
950
|
+
const outward =
|
|
951
|
+
given.background === true && !full
|
|
952
|
+
? outwardCommandWrites(profile, command).find((write) => !outwardAllowed(profile, write))
|
|
953
|
+
: undefined;
|
|
954
|
+
const withOutward = (found: Verdict): Verdict =>
|
|
955
|
+
found.ask === true && outward !== undefined ? { ...found, outward } : found;
|
|
956
|
+
if (!verdict.allow) return withOutward(verdict);
|
|
604
957
|
const asked = askBash(profile, command, askedPaths(profile, call.cwd, home), call.cwd, home);
|
|
605
|
-
if (!asked.allow) return asked;
|
|
958
|
+
if (!asked.allow) return withOutward(asked);
|
|
606
959
|
// A read-only directory runs only commands known to read.
|
|
607
|
-
if (
|
|
608
|
-
return { allow: false, ask: true, reason: "the working directory is read-only and this command is not known to only read", rule: "readOnly", kind: "command", value: command };
|
|
960
|
+
if (readOnly && !isReadOnlyCommand(command)) {
|
|
961
|
+
return withOutward({ allow: false, ask: true, reason: "the working directory is read-only and this command is not known to only read", rule: "readOnly", kind: "command", value: command });
|
|
962
|
+
}
|
|
963
|
+
// The level asks about a command not known to only read.
|
|
964
|
+
if ((given.level === "ask" || given.level === "edits") && !isReadOnlyCommand(command)) {
|
|
965
|
+
return withOutward({
|
|
966
|
+
allow: false,
|
|
967
|
+
ask: true,
|
|
968
|
+
reason: `the chat's access level "${given.level}" asks about a command not known to only read`,
|
|
969
|
+
rule: "level",
|
|
970
|
+
kind: "command",
|
|
971
|
+
value: command,
|
|
972
|
+
});
|
|
973
|
+
}
|
|
974
|
+
if (outward !== undefined) {
|
|
975
|
+
return {
|
|
976
|
+
allow: false,
|
|
977
|
+
ask: true,
|
|
978
|
+
reason: `the command writes outside operad to "${outward.target}" and the run was not started by a person`,
|
|
979
|
+
rule: "outward",
|
|
980
|
+
kind: "command",
|
|
981
|
+
value: command,
|
|
982
|
+
outward,
|
|
983
|
+
};
|
|
609
984
|
}
|
|
610
985
|
return asked;
|
|
611
986
|
}
|
|
612
987
|
if (call.tool === MCP_TOOL) {
|
|
613
988
|
// An allow_always answer to an unlisted tool is its exact command entry.
|
|
614
|
-
if (alwaysAllowed(profile, call)) return ALLOW;
|
|
615
|
-
|
|
989
|
+
if (given.background !== true && alwaysAllowed(profile, call)) return ALLOW;
|
|
990
|
+
const server: unknown = call.input["server"];
|
|
991
|
+
const tool: unknown = call.input["tool"];
|
|
992
|
+
// In a background run below `full` an MCP tool is outward even when
|
|
993
|
+
// `allowMcp` is absent: the default profile has no list, so it cannot be a pass.
|
|
994
|
+
if (!full && given.background === true && profile.allowMcp === undefined) {
|
|
995
|
+
if (typeof server !== "string" || typeof tool !== "string") {
|
|
996
|
+
return { allow: false, reason: "an MCP call that names no server and tool cannot be checked" };
|
|
997
|
+
}
|
|
998
|
+
const write: OutwardWrite = {
|
|
999
|
+
kind: "service",
|
|
1000
|
+
target: mcpCallValue(server, tool),
|
|
1001
|
+
content: `the MCP tool "${server}.${tool}"`,
|
|
1002
|
+
};
|
|
1003
|
+
if (outwardAllowed(profile, write)) return ALLOW;
|
|
1004
|
+
return {
|
|
1005
|
+
allow: false,
|
|
1006
|
+
ask: true,
|
|
1007
|
+
reason: `the MCP tool "${server}.${tool}" writes outside operad and the run was not started by a person`,
|
|
1008
|
+
rule: "outward",
|
|
1009
|
+
kind: "command",
|
|
1010
|
+
value: write.target,
|
|
1011
|
+
outward: write,
|
|
1012
|
+
};
|
|
1013
|
+
}
|
|
1014
|
+
const found = matchMcp(profile, call);
|
|
1015
|
+
// At `full` an unlisted MCP tool is not an ask.
|
|
1016
|
+
if (full && found.ask === true) return ALLOW;
|
|
1017
|
+
if (!full && given.background === true && found.ask === true) {
|
|
1018
|
+
const write: OutwardWrite = {
|
|
1019
|
+
kind: "service",
|
|
1020
|
+
target: found.value,
|
|
1021
|
+
content: `the MCP tool "${String(call.input["server"])}.${String(call.input["tool"])}"`,
|
|
1022
|
+
};
|
|
1023
|
+
if (!outwardAllowed(profile, write)) return { ...found, outward: write };
|
|
1024
|
+
}
|
|
1025
|
+
return found;
|
|
616
1026
|
}
|
|
617
1027
|
if (PATH_TOOLS.has(call.tool)) {
|
|
618
1028
|
const path: unknown = call.input["path"] ?? call.input["file_path"];
|
|
@@ -622,13 +1032,25 @@ export function match(given: AccessProfile, call: ToolCall, options: MatchOption
|
|
|
622
1032
|
if (!verdict.allow) return verdict;
|
|
623
1033
|
}
|
|
624
1034
|
// An always-allowed entry lifts the ask, never a deny.
|
|
625
|
-
if (alwaysAllowed(profile, call)) return ALLOW;
|
|
1035
|
+
if (given.background !== true && alwaysAllowed(profile, call)) return ALLOW;
|
|
626
1036
|
// Ask comes after deny: a write to a path that is asked about asks.
|
|
1037
|
+
const normal = normalise(path, call.cwd, home);
|
|
627
1038
|
const asked = call.tool === "write" || call.tool === "edit" ? askedPaths(profile, call.cwd, home) : undefined;
|
|
628
|
-
const hit = asked === undefined ? undefined : askedRule(asked,
|
|
1039
|
+
const hit = asked === undefined ? undefined : askedRule(asked, normal);
|
|
629
1040
|
if (hit !== undefined) {
|
|
630
1041
|
return { allow: false, ask: true, reason: `path "${path}" is under askPaths entry "${hit}"`, rule: hit, kind: "path", value: path, tool: call.tool };
|
|
631
1042
|
}
|
|
1043
|
+
if ((call.tool === "write" || call.tool === "edit") && levelAsksWrite(given.level, normal, call.cwd)) {
|
|
1044
|
+
return {
|
|
1045
|
+
allow: false,
|
|
1046
|
+
ask: true,
|
|
1047
|
+
reason: `the chat's access level "${given.level}" asks about this write`,
|
|
1048
|
+
rule: "level",
|
|
1049
|
+
kind: "path",
|
|
1050
|
+
value: path,
|
|
1051
|
+
tool: call.tool,
|
|
1052
|
+
};
|
|
1053
|
+
}
|
|
632
1054
|
}
|
|
633
1055
|
}
|
|
634
1056
|
return ALLOW;
|