@phren/agent 0.3.57 → 0.3.59
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.
|
@@ -1,10 +1,12 @@
|
|
|
1
|
+
import { execFileSync } from "child_process";
|
|
1
2
|
import * as fs from "fs";
|
|
2
3
|
import * as os from "os";
|
|
3
4
|
import * as path from "path";
|
|
4
|
-
import { checkShellSafety } from "./shell-safety.js";
|
|
5
|
-
import { isAutoApprovableCommand,
|
|
5
|
+
import { checkShellSafety, shellEnv } from "./shell-safety.js";
|
|
6
|
+
import { commandPaths, gitRead, isAutoApprovableCommand, recursiveReads, splitCommandWords } from "./shell-classify.js";
|
|
7
|
+
import { GIT_SPECS, own, parseOptions, READ_ONLY_SPECS } from "./shell-options.js";
|
|
6
8
|
import { evaluateRules } from "./rules.js";
|
|
7
|
-
import { validatePath, checkSensitivePath, checkSensitiveTarget } from "./sandbox.js";
|
|
9
|
+
import { validatePath, checkSensitivePath, checkSensitiveTarget, isPathInSandbox } from "./sandbox.js";
|
|
8
10
|
import { isAllowed } from "./allowlist.js";
|
|
9
11
|
import { parsePatch, patchPaths } from "../tools/apply-patch.js";
|
|
10
12
|
/** Tools that are safe in all modes — read-only, no side effects. */
|
|
@@ -54,21 +56,49 @@ const PATTERN_OPTION = /^(-e|--regexp)$/;
|
|
|
54
56
|
const PATTERN_OPTION_JOINED = /^(-e.|--regexp=)/;
|
|
55
57
|
/** Options whose value is a file of patterns: a path. */
|
|
56
58
|
const PATTERN_FILE_OPTION = /^(-f|--file)$/;
|
|
57
|
-
/** Is this path-like argument a secret file, under its own name or where it leads?
|
|
59
|
+
/** Is this path-like argument a secret file, under its own name or where it leads? A `~` in it is literal
|
|
60
|
+
* here: the words given are already as bash expands them (`commandWordsExpanded`). */
|
|
58
61
|
function secretPath(value, cwd) {
|
|
59
62
|
const looksLikePath = /[/\\]/.test(value) || value.startsWith(".") || value.startsWith("~")
|
|
60
63
|
|| fs.existsSync(path.resolve(cwd, value));
|
|
61
64
|
if (!looksLikePath)
|
|
62
65
|
return false;
|
|
63
|
-
|
|
64
|
-
//
|
|
65
|
-
const abs = path.resolve(cwd,
|
|
66
|
-
|
|
66
|
+
// Resolved against cwd, so `.aws/config` matches "/.aws/" like the absolute spelling; with a separator after
|
|
67
|
+
// it too, so the directory `~/.ssh` itself matches "/.ssh/" (`git diff /var/empty ~/.ssh` prints its files).
|
|
68
|
+
const abs = path.resolve(cwd, value), real = realTarget(cwd, value);
|
|
69
|
+
return checkSensitivePath(value).sensitive || checkSensitiveTarget(abs, real).sensitive
|
|
70
|
+
|| checkSensitiveTarget(abs + path.sep, real === undefined ? undefined : real + path.sep).sensitive;
|
|
71
|
+
}
|
|
72
|
+
/** Device files a read may name anywhere: they hold nothing of the owner's. */
|
|
73
|
+
const DEVICE_PATHS = new Set(["/dev/null", "/dev/stdin", "/dev/stdout", "/dev/stderr", "/dev/zero", "/dev/random", "/dev/urandom", "/dev/tty"]);
|
|
74
|
+
/** Whether a path a command reads is outside the project (and every allowed path), as written or where it really
|
|
75
|
+
* leads: `cat /tmp/x`, and `cat link` whose link points out. `-` (stdin) and the device files are not. */
|
|
76
|
+
function outsideProject(value, cwd, config) {
|
|
77
|
+
if (value === "-" || DEVICE_PATHS.has(value))
|
|
78
|
+
return false;
|
|
79
|
+
const abs = path.resolve(cwd, value);
|
|
80
|
+
if (DEVICE_PATHS.has(abs))
|
|
81
|
+
return false;
|
|
82
|
+
const real = realTarget(cwd, value) ?? abs;
|
|
83
|
+
return !validatePath(abs, config.projectRoot, config.allowedPaths).ok || !validatePath(real, config.projectRoot, config.allowedPaths).ok;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Where a path argument leads, resolved as the kernel does: each symlink before the `..` after it, so
|
|
87
|
+
* `link/../public.txt` is the public.txt beside link's target, not the one in cwd (path.resolve, and
|
|
88
|
+
* fs.realpathSync, collapse the `..` first). A path that isn't there yet resolves through its folder.
|
|
89
|
+
*/
|
|
90
|
+
export function realTarget(cwd, value) {
|
|
91
|
+
const raw = path.isAbsolute(value) ? value : `${cwd}${path.sep}${value}`;
|
|
67
92
|
try {
|
|
68
|
-
|
|
93
|
+
return fs.realpathSync.native(raw);
|
|
69
94
|
}
|
|
70
95
|
catch { /* not there (yet) */ }
|
|
71
|
-
|
|
96
|
+
try {
|
|
97
|
+
return path.join(fs.realpathSync.native(path.dirname(raw)), path.basename(raw));
|
|
98
|
+
}
|
|
99
|
+
catch {
|
|
100
|
+
return undefined;
|
|
101
|
+
}
|
|
72
102
|
}
|
|
73
103
|
/**
|
|
74
104
|
* Whether a simple command names a secret file. Only arguments that look like
|
|
@@ -76,7 +106,12 @@ function secretPath(value, cwd) {
|
|
|
76
106
|
* a search tool's pattern is skipped, so `rg 'credentials.json' src` doesn't
|
|
77
107
|
* ask. After `--` every argument is an operand.
|
|
78
108
|
*/
|
|
79
|
-
function namesSecretFile(tokens, cwd) {
|
|
109
|
+
function namesSecretFile(tokens, cwd, config) {
|
|
110
|
+
// A command this reader knows: exactly the files its own options parser reads (`rg -neTOKEN x` searches x),
|
|
111
|
+
// each inside the project. A git revision (`HEAD~1`) resolves inside it like a relative path.
|
|
112
|
+
const known = commandPaths(tokens);
|
|
113
|
+
if (known)
|
|
114
|
+
return known.some((p) => secretPath(p, cwd) || outsideProject(p, cwd, config));
|
|
80
115
|
const [binary, ...args] = tokens;
|
|
81
116
|
const end = args.indexOf("--");
|
|
82
117
|
const options = end === -1 ? args : args.slice(0, end);
|
|
@@ -111,6 +146,280 @@ function namesSecretFile(tokens, cwd) {
|
|
|
111
146
|
}
|
|
112
147
|
return false;
|
|
113
148
|
}
|
|
149
|
+
/** git config that makes even a read (`git diff`, `git status`) run a configured program, and that no
|
|
150
|
+
* override can switch off without knowing its driver's name: an external diff, a diff driver's command or
|
|
151
|
+
* textconv, a clean/smudge/process filter. The rest (fsmonitor, the pager, signature checks) every agent
|
|
152
|
+
* shell switches off itself (GIT_HARDENING, shell-safety.ts). */
|
|
153
|
+
const GIT_HELPER_KEYS = "^(diff\\.external|diff\\..+\\.(command|textconv)|filter\\..+\\.(clean|smudge|process)|format\\.pretty|pretty\\..+)$";
|
|
154
|
+
/** The helper config git-lfs and git-crypt install, by key and exact value: the same program each time,
|
|
155
|
+
* never a shell line someone wrote into the repository's config. */
|
|
156
|
+
const KNOWN_HELPERS = [
|
|
157
|
+
[/^filter\.lfs\.clean$/, /^git-lfs clean -- %f$/],
|
|
158
|
+
[/^filter\.lfs\.smudge$/, /^git-lfs smudge( --skip)? -- %f$/],
|
|
159
|
+
[/^filter\.lfs\.process$/, /^git-lfs filter-process( --skip)?$/],
|
|
160
|
+
[/^filter\.git-crypt(-[\w-]+)?\.(clean|smudge)$/, /^"?(git-crypt|\/[\w./-]+\/git-crypt)"? (clean|smudge)( --key-name=[\w-]+)?$/],
|
|
161
|
+
[/^diff\.git-crypt(-[\w-]+)?\.textconv$/, /^"?(git-crypt|\/[\w./-]+\/git-crypt)"? diff( --key-name=[\w-]+)?$/],
|
|
162
|
+
];
|
|
163
|
+
/** Whether `bin` is, where it really leads, inside `root` (case-folded where the file system usually is). */
|
|
164
|
+
function realInside(bin, root) {
|
|
165
|
+
const real = (p) => { try {
|
|
166
|
+
return fs.realpathSync.native(p);
|
|
167
|
+
}
|
|
168
|
+
catch {
|
|
169
|
+
return undefined;
|
|
170
|
+
} };
|
|
171
|
+
const target = real(bin), base = real(root) ?? root;
|
|
172
|
+
if (target === undefined)
|
|
173
|
+
return true;
|
|
174
|
+
const fold = (p) => (process.platform === "darwin" || process.platform === "win32" ? p.toLowerCase() : p);
|
|
175
|
+
return isPathInSandbox(fold(target), fold(base), []);
|
|
176
|
+
}
|
|
177
|
+
/** The config keys of each kind of helper (gitRead's `helpers`). */
|
|
178
|
+
const HELPER_KINDS = { diff: /^diff\./, filter: /^filter\./, format: /^(format|pretty)\./ };
|
|
179
|
+
/** Whether a git read, run in `cwd`, has config that makes it run a program: a helper of a kind its operation
|
|
180
|
+
* uses (gitRead: `git log -5` prints no patch, so a textconv never runs for it), other than git-lfs's and
|
|
181
|
+
* git-crypt's own, installed outside the project; or a configured log format that verifies signatures (`%G`).
|
|
182
|
+
* A git that can't be asked can't vouch; with no cwd there (the command can't start) nothing runs. */
|
|
183
|
+
export function gitRunsHelpers(cwd, projectRoot, kinds) {
|
|
184
|
+
const patterns = kinds.flatMap((kind) => own(HELPER_KINDS, kind) ?? []);
|
|
185
|
+
if (!patterns.length)
|
|
186
|
+
return false;
|
|
187
|
+
const runs = { test: (key) => patterns.some((p) => p.test(key)) };
|
|
188
|
+
let out;
|
|
189
|
+
try {
|
|
190
|
+
out = execFileSync("git", ["config", "--get-regexp", GIT_HELPER_KEYS], {
|
|
191
|
+
cwd, env: shellEnv(), encoding: "utf-8", timeout: 5_000, stdio: ["ignore", "pipe", "ignore"]
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
catch (err) {
|
|
195
|
+
// Exit 1: no such key; ENOENT: no git, so nothing runs. Anything else (a timeout) can't vouch for the config.
|
|
196
|
+
return err.status !== 1 && err.code !== "ENOENT";
|
|
197
|
+
}
|
|
198
|
+
return out.split("\n").filter(Boolean).some((line) => {
|
|
199
|
+
const at = line.indexOf(" ");
|
|
200
|
+
const key = at === -1 ? line : line.slice(0, at), value = at === -1 ? "" : line.slice(at + 1);
|
|
201
|
+
if (!runs.test(key))
|
|
202
|
+
return false;
|
|
203
|
+
if (/^(format|pretty)\./.test(key))
|
|
204
|
+
return value.includes("%G");
|
|
205
|
+
const known = KNOWN_HELPERS.some(([k, v]) => k.test(key) && v.test(value));
|
|
206
|
+
const bin = /^"?(\/[^"\s]+)/.exec(value)?.[1];
|
|
207
|
+
return !known || (bin !== undefined && realInside(bin, projectRoot));
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
/** Most files looked at below one directory a recursive read starts from; past it the command asks. */
|
|
211
|
+
export const RECURSIVE_WALK_LIMIT = 20_000;
|
|
212
|
+
/** As RECURSIVE_WALK_LIMIT, for a list git gives (cheap to check: no directory reads). */
|
|
213
|
+
const GIT_LIST_LIMIT = 200_000;
|
|
214
|
+
/** A list a program prints, NUL- or newline-separated, or undefined when it fails. Exit 1 is an empty list,
|
|
215
|
+
* and so is a program that isn't installed: the command can't run it either. git never fetches a missing
|
|
216
|
+
* object for the check (a partial clone's lazy fetch would reach the network and its credential helper). */
|
|
217
|
+
function listed(program, args, cwd, separator, quietOnEmpty = false) {
|
|
218
|
+
try {
|
|
219
|
+
const out = execFileSync(program, args, { cwd, env: { ...shellEnv(), GIT_NO_LAZY_FETCH: "1" }, encoding: "utf-8", timeout: 10_000,
|
|
220
|
+
maxBuffer: 256 * 1024 * 1024, stdio: ["ignore", "pipe", quietOnEmpty ? "pipe" : "ignore"] });
|
|
221
|
+
return out.split(separator).filter(Boolean);
|
|
222
|
+
}
|
|
223
|
+
catch (err) {
|
|
224
|
+
const { status, code, stderr } = err;
|
|
225
|
+
// `quietOnEmpty`: the program says nothing when it only finds nothing (grep -s), so exit 1 with a message is
|
|
226
|
+
// a refused option, not an empty list (some greps exit 1 on a bad option value).
|
|
227
|
+
if (status === 1 && quietOnEmpty && typeof stderr === "string" && stderr.trim())
|
|
228
|
+
return undefined;
|
|
229
|
+
return status === 1 || code === "ENOENT" ? [] : undefined;
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
/** Whether any of `files` (relative to `cwd`) is a secret file, by name or, when `follow`, where it leads (which
|
|
233
|
+
* must also be inside the project); too many to look at counts. */
|
|
234
|
+
function secretAmong(files, cwd, follow, config) {
|
|
235
|
+
if (files.length > GIT_LIST_LIMIT)
|
|
236
|
+
return true;
|
|
237
|
+
return files.some((file) => {
|
|
238
|
+
const full = path.resolve(cwd, file);
|
|
239
|
+
if (checkSensitivePath(full).sensitive)
|
|
240
|
+
return true;
|
|
241
|
+
if (!follow)
|
|
242
|
+
return false;
|
|
243
|
+
let real;
|
|
244
|
+
try {
|
|
245
|
+
real = fs.realpathSync.native(full);
|
|
246
|
+
}
|
|
247
|
+
catch {
|
|
248
|
+
return false;
|
|
249
|
+
}
|
|
250
|
+
return checkSensitivePath(real).sensitive || !validatePath(real, config.projectRoot, config.allowedPaths).ok;
|
|
251
|
+
});
|
|
252
|
+
}
|
|
253
|
+
/**
|
|
254
|
+
* Whether a recursive read could reach a secret file. rg and grep say themselves which files they read
|
|
255
|
+
* (`rg --files` and `grep -l` with the run's own selecting options: ignore files, hidden files, globs,
|
|
256
|
+
* `--include` and `--exclude` are theirs to apply). git grep reads the tracked files matching its pathspecs
|
|
257
|
+
* (`git ls-files`), and asks for an operand that is not a path (a revision's tree, deleted secrets included).
|
|
258
|
+
* Anything else is walked: every entry below each root, where the root really leads (a symlink before the `..`
|
|
259
|
+
* after it); a symlink there asks when it follows symlinks, and so do too many entries. A program that can't
|
|
260
|
+
* list counts as reaching one.
|
|
261
|
+
*/
|
|
262
|
+
function recursiveReachesSecret(read, cwd, config) {
|
|
263
|
+
// No cwd there: the command can't start.
|
|
264
|
+
if (!fs.existsSync(cwd))
|
|
265
|
+
return false;
|
|
266
|
+
if (read.scope === "rg") {
|
|
267
|
+
const files = listed("rg", ["--files", "--null", ...(read.listArgs ?? []), "--", ...read.roots], cwd, "\0");
|
|
268
|
+
return files === undefined || secretAmong(files, cwd, read.follow, config);
|
|
269
|
+
}
|
|
270
|
+
if (read.scope === "grep") {
|
|
271
|
+
// Every file grep's selection reads matches an empty pattern (an empty file has nothing to show).
|
|
272
|
+
const files = listed("grep", ["-l", "--null", "-s", ...(read.listArgs ?? []), "-e", "", "--", ...read.roots], cwd, "\0", true);
|
|
273
|
+
return files === undefined || secretAmong(files, cwd, read.follow, config);
|
|
274
|
+
}
|
|
275
|
+
if (read.scope === "tracked") {
|
|
276
|
+
if (read.roots.some((root) => !fs.existsSync(path.resolve(cwd, root))))
|
|
277
|
+
return true;
|
|
278
|
+
const files = listed("git", ["ls-files", "-z", "--cached", "--", ...read.roots], cwd, "\0");
|
|
279
|
+
return files === undefined || secretAmong(files, cwd, false, config);
|
|
280
|
+
}
|
|
281
|
+
for (const root of read.roots) {
|
|
282
|
+
const start = realTarget(cwd, root);
|
|
283
|
+
let directory = false;
|
|
284
|
+
try {
|
|
285
|
+
directory = start !== undefined && fs.statSync(start).isDirectory();
|
|
286
|
+
}
|
|
287
|
+
catch { /* not there */ }
|
|
288
|
+
if (!directory || start === undefined)
|
|
289
|
+
continue;
|
|
290
|
+
if (checkSensitivePath(start + path.sep).sensitive)
|
|
291
|
+
return true;
|
|
292
|
+
const pending = [start];
|
|
293
|
+
let seen = 0;
|
|
294
|
+
while (pending.length) {
|
|
295
|
+
const dir = pending.pop();
|
|
296
|
+
let entries;
|
|
297
|
+
try {
|
|
298
|
+
entries = fs.readdirSync(dir, { withFileTypes: true });
|
|
299
|
+
}
|
|
300
|
+
catch {
|
|
301
|
+
continue;
|
|
302
|
+
}
|
|
303
|
+
for (const entry of entries) {
|
|
304
|
+
if (++seen > RECURSIVE_WALK_LIMIT)
|
|
305
|
+
return true;
|
|
306
|
+
const full = path.join(dir, entry.name);
|
|
307
|
+
if (checkSensitivePath(full).sensitive || checkSensitivePath(full + path.sep).sensitive)
|
|
308
|
+
return true;
|
|
309
|
+
if (entry.isSymbolicLink() && read.follow)
|
|
310
|
+
return true;
|
|
311
|
+
if (entry.isDirectory())
|
|
312
|
+
pending.push(full);
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
return false;
|
|
317
|
+
}
|
|
318
|
+
/**
|
|
319
|
+
* Whether a git read prints the contents of a secret file it doesn't name: the paths a `git diff`, `git log -p`
|
|
320
|
+
* or `git show` would print a patch of, as git lists them for the same revisions, pathspecs and selecting
|
|
321
|
+
* options (gitRead's `exposure`), relative to the repository's top. Outside a repository nothing is listed
|
|
322
|
+
* (a `git diff` there compares the paths it names, which are checked). A listing that fails counts.
|
|
323
|
+
*/
|
|
324
|
+
function gitExposesSecret(args, cwd, config) {
|
|
325
|
+
const top = listed("git", ["rev-parse", "--show-toplevel"], cwd, "\n")?.[0];
|
|
326
|
+
if (top === undefined)
|
|
327
|
+
return false;
|
|
328
|
+
const files = listed("git", args, cwd, "\0");
|
|
329
|
+
return files === undefined || secretAmong(files, top, false, config);
|
|
330
|
+
}
|
|
331
|
+
/**
|
|
332
|
+
* Whether a `REV:PATH` a git read names (gitRead's `blobs`) is a secret file or outside the project. git reads
|
|
333
|
+
* such a path from the repository's top, not from cwd, unless it starts with `./` or `../`: in a project that
|
|
334
|
+
* is a folder of a bigger repository, `HEAD:other/secret.json` is that folder's sibling.
|
|
335
|
+
*/
|
|
336
|
+
function gitBlobsReachSecret(blobs, cwd, config) {
|
|
337
|
+
if (!blobs.length)
|
|
338
|
+
return false;
|
|
339
|
+
const top = listed("git", ["rev-parse", "--show-toplevel"], cwd, "\n")?.[0];
|
|
340
|
+
return blobs.some((blob) => {
|
|
341
|
+
const from = blob.startsWith("./") || blob.startsWith("../") || top === undefined ? cwd : top;
|
|
342
|
+
const full = path.resolve(from, blob);
|
|
343
|
+
return checkSensitivePath(full).sensitive || checkSensitivePath(blob).sensitive || outsideProject(full, cwd, config);
|
|
344
|
+
});
|
|
345
|
+
}
|
|
346
|
+
/**
|
|
347
|
+
* Whether a shell command may reach a secret file or leave the sandbox: an argument names one or a path
|
|
348
|
+
* outside the project, a word the shell expands into file names (`cat config/*`) hides the names, a recursive
|
|
349
|
+
* read has one below it, git's config runs a program on a read, a git read prints a secret file's patch, or a
|
|
350
|
+
* `cd` goes outside the sandbox. Each segment's paths resolve where the `cd`s before it left the shell
|
|
351
|
+
* (`cd ~/.aws && cat credentials`): `cd -P` moves to where the path really leads, and a later `cd ..` from
|
|
352
|
+
* there. A `cd` whose effect this can't tell asks: no target or `-`; one that may be skipped (after `&&` or
|
|
353
|
+
* `||`: `false && cd src; grep -r .`) or runs in a subshell (a pipeline); one followed by `||`, or by a later
|
|
354
|
+
* `||` (what comes after may run where the shell started); and one to a directory it may not enter (not there,
|
|
355
|
+
* not searchable) unless only `&&` and `|` follow it (when it fails, nothing after it runs; after `;` the rest
|
|
356
|
+
* runs where the shell was).
|
|
357
|
+
*/
|
|
358
|
+
function commandTouchesSecret(command, cwd, config) {
|
|
359
|
+
const segments = splitCommandWords(command);
|
|
360
|
+
if (!segments)
|
|
361
|
+
return false;
|
|
362
|
+
let here = cwd;
|
|
363
|
+
let moved = false;
|
|
364
|
+
for (const [index, segment] of segments.entries()) {
|
|
365
|
+
const { globbed, before, after } = segment;
|
|
366
|
+
// A leading `~` only where bash expands it (`'~/x'` is a file named so in cwd).
|
|
367
|
+
const words = segment.words.map((w, i) => (segment.home?.[i] ? os.homedir() + w.slice(1) : w));
|
|
368
|
+
if (globbed.some(Boolean))
|
|
369
|
+
return true;
|
|
370
|
+
if (moved && before === "||")
|
|
371
|
+
return true;
|
|
372
|
+
if (words[0] === "cd") {
|
|
373
|
+
if ((before !== "" && before !== ";") || after === "|" || after === "||")
|
|
374
|
+
return true;
|
|
375
|
+
const target = commandPaths(words)?.[0];
|
|
376
|
+
if (target === undefined || target === "-")
|
|
377
|
+
return true;
|
|
378
|
+
// The last of -L and -P decides; -L (the default) keeps the path as written.
|
|
379
|
+
const modes = parseOptions(words.slice(1), own(READ_ONLY_SPECS, "cd"))?.options.filter((o) => o.name === "-P" || o.name === "-L") ?? [];
|
|
380
|
+
const physical = modes.at(-1)?.name === "-P";
|
|
381
|
+
const next = path.resolve(here, target);
|
|
382
|
+
const real = realTarget(here, target) ?? next;
|
|
383
|
+
let enterable = false;
|
|
384
|
+
try {
|
|
385
|
+
enterable = fs.statSync(real).isDirectory();
|
|
386
|
+
fs.accessSync(real, fs.constants.X_OK);
|
|
387
|
+
}
|
|
388
|
+
catch {
|
|
389
|
+
enterable = false;
|
|
390
|
+
}
|
|
391
|
+
const stopsOnFailure = segments.slice(index + 1).every((s) => s.before === "&&" || s.before === "|");
|
|
392
|
+
if (!enterable && !stopsOnFailure)
|
|
393
|
+
return true;
|
|
394
|
+
if (!validatePath(next, config.projectRoot, config.allowedPaths).ok || !validatePath(real, config.projectRoot, config.allowedPaths).ok)
|
|
395
|
+
return true;
|
|
396
|
+
if (checkSensitiveTarget(next + path.sep, real + path.sep).sensitive)
|
|
397
|
+
return true;
|
|
398
|
+
here = physical ? real : next;
|
|
399
|
+
moved = true;
|
|
400
|
+
continue;
|
|
401
|
+
}
|
|
402
|
+
if (namesSecretFile(words, here, config))
|
|
403
|
+
return true;
|
|
404
|
+
// Before git lists anything: the listing itself must not start a configured program. A git read whose
|
|
405
|
+
// options this reader can't read (`--ext-diff`, run after the session approved `git log`) can't be vouched for.
|
|
406
|
+
const git = gitRead(words);
|
|
407
|
+
if (words[0] === "git" && !git && own(GIT_SPECS, words.slice(1).find((w) => !w.startsWith("-")) ?? ""))
|
|
408
|
+
return true;
|
|
409
|
+
if (git?.ask)
|
|
410
|
+
return true;
|
|
411
|
+
if (git && gitRunsHelpers(here, config.projectRoot, git.helpers))
|
|
412
|
+
return true;
|
|
413
|
+
if (git && gitBlobsReachSecret(git.blobs, here, config))
|
|
414
|
+
return true;
|
|
415
|
+
if (git?.exposure && gitExposesSecret(git.exposure, here, config))
|
|
416
|
+
return true;
|
|
417
|
+
const recursive = recursiveReads(words);
|
|
418
|
+
if (recursive && recursiveReachesSecret(recursive, here, config))
|
|
419
|
+
return true;
|
|
420
|
+
}
|
|
421
|
+
return false;
|
|
422
|
+
}
|
|
114
423
|
/**
|
|
115
424
|
* Check whether a tool call should be allowed, asked about, or denied.
|
|
116
425
|
*/
|
|
@@ -210,8 +519,15 @@ export function checkPermission(config, toolName, input) {
|
|
|
210
519
|
}
|
|
211
520
|
// Session allowlist — user previously approved this tool+pattern via (a)llow-tool or (s)ession-allow.
|
|
212
521
|
// Placed after deny-list, shell-safety blocks, and sensitive-path denials so those are never bypassed.
|
|
522
|
+
// An approved shell command still asks outside the sandbox or before it reads a secret file: approving
|
|
523
|
+
// `cat` once is not approving `cat .env`.
|
|
213
524
|
if (isAllowed(toolName, input)) {
|
|
214
|
-
|
|
525
|
+
const shellOk = toolName !== "shell" || (() => {
|
|
526
|
+
const cwd = input.cwd || config.projectRoot;
|
|
527
|
+
return validatePath(cwd, config.projectRoot, config.allowedPaths).ok && !commandTouchesSecret(input.command || "", cwd, config);
|
|
528
|
+
})();
|
|
529
|
+
if (shellOk)
|
|
530
|
+
return { verdict: "allow", reason: "Session allowlist." };
|
|
215
531
|
}
|
|
216
532
|
// Mode-specific logic
|
|
217
533
|
switch (config.mode) {
|
|
@@ -220,6 +536,11 @@ export function checkPermission(config, toolName, input) {
|
|
|
220
536
|
return { verdict: "ask", reason: `Suggest mode requires confirmation for "${toolName}".` };
|
|
221
537
|
case "auto-confirm":
|
|
222
538
|
if (AUTO_CONFIRM_TOOLS.has(toolName)) {
|
|
539
|
+
// An edit under .git (its config, hooks) can make the next auto-approved git read run a program.
|
|
540
|
+
const edited = toolName === "apply_patch" ? patchFiles : typeof input.path === "string" ? [input.path] : [];
|
|
541
|
+
const inGitDir = edited.some((p) => path.resolve(config.projectRoot, p).split(path.sep).includes(".git"));
|
|
542
|
+
if (inGitDir)
|
|
543
|
+
return { verdict: "ask", reason: `Auto-confirm mode asks before "${toolName}" edits git's own files.` };
|
|
223
544
|
// Auto-confirm tools are allowed if path is in sandbox
|
|
224
545
|
return { verdict: "allow", reason: `Auto-confirm mode allows "${toolName}".` };
|
|
225
546
|
}
|
|
@@ -231,8 +552,8 @@ export function checkPermission(config, toolName, input) {
|
|
|
231
552
|
// A read-only command still asks before it touches a secret file
|
|
232
553
|
// (`cat .env`), as read_file would be refused.
|
|
233
554
|
const command = input.command || "";
|
|
234
|
-
|
|
235
|
-
if (cwdResult.ok &&
|
|
555
|
+
// The cheap checks first: the secret check may walk a directory or ask git for its config.
|
|
556
|
+
if (cwdResult.ok && isAutoApprovableCommand(command) && !commandTouchesSecret(command, cwd, config)) {
|
|
236
557
|
return { verdict: "allow", reason: "Read, build or test command within sandbox." };
|
|
237
558
|
}
|
|
238
559
|
}
|