@hasna/hooks 0.12.6 → 0.12.7
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/README.md +10 -4
- package/bin/hooks-mcp.js +3160 -1130
- package/bin/index.js +3503 -1475
- package/bin/native-safety-entry.js +25 -20
- package/bin/native-safety-worker.cjs +25 -20
- package/bin/serve.js +2 -2
- package/dist/index.js +3347 -1301
- package/dist/lib/claude-skills-coordination.d.ts +16 -2
- package/dist/lib/installer.d.ts +11 -0
- package/dist/lib/native-safety-registration.d.ts +8 -3
- package/hooks/hook-trash-guard/README.md +49 -15
- package/hooks/hook-trash-guard/src/hook.ts +741 -125
- package/hooks/hook-workspace-repos-guard/src/hook.ts +274 -79
- package/hooks/shell-words.ts +368 -0
- package/package.json +2 -2
|
@@ -39,6 +39,12 @@
|
|
|
39
39
|
* groups are unwrapped. A here-document body is data unless something in the
|
|
40
40
|
* command can run it (see dataHeredocs): its words are then never resolved
|
|
41
41
|
* against the cwd, and only protected paths it names explicitly count.
|
|
42
|
+
* Wrapper commands (timeout, env, nice, nohup, command, exec, builtin,
|
|
43
|
+
* stdbuf, time, ionice, setsid) and their own arguments are skipped before
|
|
44
|
+
* the command and its operands are read (see unwrapCommand); arguments they
|
|
45
|
+
* cannot be parsed from fail closed. A Trash capture is read the way
|
|
46
|
+
* @hasna/trash parses it, with options before, between or after its paths
|
|
47
|
+
* until `--` (see trashOperands).
|
|
42
48
|
*
|
|
43
49
|
* Allowed orgs default to hasna,hasnaxyz,hasna-products and are overridable
|
|
44
50
|
* with the WORKSPACE_REPOS_GUARD_ORGS env var (comma-separated) or, where
|
|
@@ -72,6 +78,9 @@ import {
|
|
|
72
78
|
type CodewithHookInput,
|
|
73
79
|
type CodewithHookOutput,
|
|
74
80
|
} from "../../codewith-native-common";
|
|
81
|
+
import { ASSIGNMENT, baseName, expandBraces, splitsOrMoves, unwrapCommand, type ShellWord } from "../../shell-words";
|
|
82
|
+
|
|
83
|
+
export { expandBraces, unwrapCommand };
|
|
75
84
|
|
|
76
85
|
const FILE_WRITE_TOOLS = new Set(["Write", "Edit", "MultiEdit", "NotebookEdit"]);
|
|
77
86
|
|
|
@@ -294,17 +303,13 @@ export function pipelineStages(segment: string): string[] {
|
|
|
294
303
|
return stages;
|
|
295
304
|
}
|
|
296
305
|
|
|
297
|
-
interface ShellWord {
|
|
298
|
-
text: string;
|
|
299
|
-
opaque: boolean;
|
|
300
|
-
}
|
|
301
|
-
|
|
302
306
|
/**
|
|
303
307
|
* Split a command into shell words, honouring single and double quotes and
|
|
304
308
|
* backslash escapes, and removing the quotes. A word is opaque when its
|
|
305
309
|
* value cannot be read statically: it holds an expansion other than a
|
|
306
|
-
* leading `$HOME`/`${HOME}`, a command substitution, a backtick, a glob
|
|
307
|
-
* an unbalanced
|
|
310
|
+
* leading `$HOME`/`${HOME}`, a command substitution, a backtick, a glob, a
|
|
311
|
+
* brace expansion, a named tilde prefix (splitsOrMoves) or an unbalanced
|
|
312
|
+
* quote.
|
|
308
313
|
*/
|
|
309
314
|
function shellWords(command: string): ShellWord[] {
|
|
310
315
|
const words: ShellWord[] = [];
|
|
@@ -319,7 +324,7 @@ function shellWords(command: string): ShellWord[] {
|
|
|
319
324
|
// unquoted `/`) is unquoted and unescaped. `~"/x"`, `~\/x` and `~""`
|
|
320
325
|
// name an entry called `~` in the current directory.
|
|
321
326
|
const literalTilde = text.startsWith("~") && !/^~(?:\/|$)/.test(raw);
|
|
322
|
-
words.push({ text: literalTilde ? `./${text}` : text, opaque: /[$\x60*?[]/.test(body) || /[()]/.test(body) });
|
|
327
|
+
words.push({ text: literalTilde ? `./${text}` : text, opaque: /[$\x60*?[]/.test(body) || /[()]/.test(body) || splitsOrMoves(raw) });
|
|
323
328
|
text = "";
|
|
324
329
|
raw = "";
|
|
325
330
|
inWord = false;
|
|
@@ -377,6 +382,63 @@ function asksForHelp(words: string[], at: number, short: boolean): boolean {
|
|
|
377
382
|
return word === "--help" || word === "--version" || (short && (word === "-h" || word === "-V"));
|
|
378
383
|
}
|
|
379
384
|
|
|
385
|
+
const OPERATION_RANK: Record<Operation, number> = { read: 0, write: 1, delete: 2 };
|
|
386
|
+
|
|
387
|
+
function strongest(...operations: Operation[]): Operation {
|
|
388
|
+
return operations.reduce((a, b) => (OPERATION_RANK[b] > OPERATION_RANK[a] ? b : a), "read");
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
/** Trash verbs that act on recorded entries or metadata, never on a path given to them. */
|
|
392
|
+
const TRASH_READ_VERBS = new Set([
|
|
393
|
+
"status", "doctor", "list", "info", "get", "pending", "station", "setup", "backup", "storage", "retrieval",
|
|
394
|
+
"backup-storage", "backup-inspect", "backup-refresh", "help",
|
|
395
|
+
]);
|
|
396
|
+
|
|
397
|
+
/** Trash capture verbs, whose operands are paths to remove. */
|
|
398
|
+
const TRASH_DELETE_VERBS = new Set(["put", "rm", "delete", "remove"]);
|
|
399
|
+
|
|
400
|
+
/**
|
|
401
|
+
* @hasna/trash verbs whose `--help`/`-h`, right after the verb, prints usage
|
|
402
|
+
* and acts on nothing (hosted `parseArgs` answers help for every verb; local
|
|
403
|
+
* mode refuses `-h` or reads `--help` as a missing entry id). `guard` is left
|
|
404
|
+
* out: its arguments are an rm command line.
|
|
405
|
+
*/
|
|
406
|
+
const TRASH_HELP_VERBS = new Set([
|
|
407
|
+
...TRASH_DELETE_VERBS, "restore", "restore-capsule", "recover", "release", "locks", "retention",
|
|
408
|
+
"hold", "unhold", "pin", "unpin", "retrieve", "backup-retrieve",
|
|
409
|
+
]);
|
|
410
|
+
|
|
411
|
+
/** find primaries that delete, run a command or write a file. */
|
|
412
|
+
const FIND_ACTIONS = new Set(["-delete", "-exec", "-execdir", "-ok", "-okdir", "-fls", "-fprint", "-fprint0", "-fprintf"]);
|
|
413
|
+
|
|
414
|
+
/**
|
|
415
|
+
* Whether the shell can turn this raw word into other text or other words: a
|
|
416
|
+
* parameter or command expansion, a glob or a brace list outside quotes, or
|
|
417
|
+
* an unterminated quote.
|
|
418
|
+
*/
|
|
419
|
+
function expandsAtRuntime(raw: string): boolean {
|
|
420
|
+
let quote: "'" | '"' | null = null;
|
|
421
|
+
for (let i = 0; i < raw.length; i++) {
|
|
422
|
+
const ch = raw[i];
|
|
423
|
+
if (quote === "'") {
|
|
424
|
+
if (ch === "'") quote = null;
|
|
425
|
+
continue;
|
|
426
|
+
}
|
|
427
|
+
if (ch === "\\") {
|
|
428
|
+
i++;
|
|
429
|
+
continue;
|
|
430
|
+
}
|
|
431
|
+
if (ch === "$" || ch === "\x60") return true;
|
|
432
|
+
if (quote === '"') {
|
|
433
|
+
if (ch === '"') quote = null;
|
|
434
|
+
continue;
|
|
435
|
+
}
|
|
436
|
+
if (ch === "'" || ch === '"') quote = ch;
|
|
437
|
+
else if ("*?[{".includes(ch)) return true;
|
|
438
|
+
}
|
|
439
|
+
return quote !== null;
|
|
440
|
+
}
|
|
441
|
+
|
|
380
442
|
/**
|
|
381
443
|
* Classify the operation of one command segment (a `&&`/`||`/`;`-delimited
|
|
382
444
|
* unit). Git is handled by its subcommand: clean|rm delete, clone|init write,
|
|
@@ -386,6 +448,13 @@ function asksForHelp(words: string[], at: number, short: boolean): boolean {
|
|
|
386
448
|
* are treated as writes — conservative: they only matter when a path under
|
|
387
449
|
* the protected root is also present. An output redirection is a write;
|
|
388
450
|
* descriptor duplication (`2>&1`) and `&` are not.
|
|
451
|
+
*
|
|
452
|
+
* The command word is located twice: by the shared command-position walk
|
|
453
|
+
* (commandWordIndex) and by this guard's strict wrapper model
|
|
454
|
+
* (unwrapCommand). The walk reads an option cluster (`timeout -vk 5 30 rm`)
|
|
455
|
+
* or a long-option abbreviation (`nice --adj 5 rm`) as the command, so the
|
|
456
|
+
* stronger of the two readings stands, and a wrapper whose arguments the
|
|
457
|
+
* strict model cannot parse falls back to the opaque reading.
|
|
389
458
|
*/
|
|
390
459
|
function segmentOperation(segment: string): Operation {
|
|
391
460
|
const trimmed = segment.trim();
|
|
@@ -393,52 +462,90 @@ function segmentOperation(segment: string): Operation {
|
|
|
393
462
|
const parsed = lexCommand(trimmed);
|
|
394
463
|
// Preserve conservative classification where the shell body is opaque.
|
|
395
464
|
if (!parsed.trustworthy || parsed.substitutions.length || /\$\{[^}]*[|;()]|[<>]\(/.test(trimmed)) return opaqueOperation(trimmed);
|
|
396
|
-
const operations = parsed.segments.
|
|
397
|
-
const
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
//
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
if (
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
while (at < words.length && words[at].startsWith("-")) {
|
|
410
|
-
const flag = words[at++];
|
|
411
|
-
if (["--help", "-h", "--version", "-V", "--identity"].includes(flag)) return "read";
|
|
412
|
-
if (["--agent", "--spool", "--files", "--info", "--config"].includes(flag)) at++;
|
|
413
|
-
else if (!["--json", "--local"].includes(flag)) return "delete";
|
|
414
|
-
}
|
|
415
|
-
if (at === words.length || ["status", "doctor", "list", "info", "get", "pending", "station", "setup", "backup", "storage", "retrieval", "backup-storage", "backup-inspect", "backup-refresh", "help"].includes(words[at])) return "read";
|
|
416
|
-
if (["restore", "restore-capsule"].includes(words[at])) return "write";
|
|
417
|
-
if (["put", "rm", "delete", "remove"].includes(words[at]) && asksForHelp(words, at + 1, true)) return "read";
|
|
418
|
-
return "delete";
|
|
419
|
-
}
|
|
420
|
-
if (tool === "git") {
|
|
421
|
-
const sub = gitSubcommand(part, index);
|
|
422
|
-
// Honoured only for `git rm|clean <request>`: a global option before the
|
|
423
|
-
// subcommand is not modelled here, so it keeps the delete.
|
|
424
|
-
if (sub === "clean" || sub === "rm") return words[1] === sub && asksForHelp(words, 2, true) ? "read" : "delete";
|
|
425
|
-
return sub === "clone" || sub === "init" ? "write" : "read";
|
|
426
|
-
}
|
|
427
|
-
if (tool === "env" && mentionsDeleteVerb(envSplitCommandText(words))) return "delete";
|
|
428
|
-
if (["sh", "bash", "zsh", "dash", "ksh", "ash", "eval", "source", ".", "su", "runuser", "env", "xargs", "find", "busybox", "toybox"].includes(tool ?? "")) {
|
|
429
|
-
return opaqueOperation(words.join(" "));
|
|
430
|
-
}
|
|
431
|
-
if (["mkdir", "mkfile", "touch", "mv", "cp", "ln", "tee", "install", "dd", "rsync", "scp", "truncate"].includes(tool ?? "")) return "write";
|
|
432
|
-
if (tool === "curl" || tool === "wget") return WRITE_FLAGS.test(words.join(" ")) ? "write" : "read";
|
|
433
|
-
if (tool === "sed" && words.slice(1).some((word) => /^-i|^--in-place(?:=|$)/.test(word))) return "write";
|
|
434
|
-
if (["python", "python3", "node", "bun"].includes(tool ?? "") && words.some((word) => word === "-c" || word === "-e")) return "write";
|
|
435
|
-
return "read";
|
|
465
|
+
const operations = parsed.segments.flatMap((part): Operation[] => {
|
|
466
|
+
const operation = partOperation(part);
|
|
467
|
+
// Brace expansion comes before every other expansion, so a brace word
|
|
468
|
+
// can supply the command word or shift a wrapper's arguments
|
|
469
|
+
// (`nice -n {5,rm} -rf build` runs rm). The expanded words are read as
|
|
470
|
+
// well and the stronger reading stands; an expansion past BRACE_LIMIT
|
|
471
|
+
// words falls back to the opaque reading of its alternatives.
|
|
472
|
+
const raws = part.words.filter((_, at) => !part.redirects.has(at)).map((word) => word.text);
|
|
473
|
+
const expanded = raws.map(expandBraces);
|
|
474
|
+
if (expanded.every((words, k) => words !== null && words.length === 1 && words[0] === raws[k])) return [operation];
|
|
475
|
+
if (expanded.some((words) => words === null)) return [operation, opaqueOperation(raws.join(" "))];
|
|
476
|
+
const relexed = lexCommand(expanded.flatMap((words) => words ?? []).join(" "));
|
|
477
|
+
return [operation, ...(relexed.trustworthy ? relexed.segments.map(partOperation) : [opaqueOperation(raws.join(" "))])];
|
|
436
478
|
});
|
|
437
|
-
return
|
|
479
|
+
return strongest(...operations);
|
|
438
480
|
}
|
|
439
481
|
|
|
440
|
-
|
|
482
|
+
/** The operation of one lexed simple command, read by both command-position models. */
|
|
483
|
+
function partOperation(part: ReturnType<typeof lexCommand>["segments"][number]): Operation {
|
|
484
|
+
const kept = part.words.map((word, at) => ({ word, at })).filter(({ at }) => !part.redirects.has(at));
|
|
485
|
+
const index = commandWordIndex(part);
|
|
486
|
+
const shared = index < 0 ? "read" : commandOperation(part, index);
|
|
487
|
+
const strict = unwrapCommand(kept.map(({ word }) => ({ text: decodeWord(word.text), opaque: /[$\x60]/.test(word.text) })));
|
|
488
|
+
if (strict === null) return strongest(shared, opaqueOperation(kept.map(({ word }) => decodeWord(word.text)).join(" ")));
|
|
489
|
+
if (strict.index < 0 || kept[strict.index].at === index) return shared;
|
|
490
|
+
return strongest(shared, commandOperation(part, kept[strict.index].at));
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
/** The operation of the command whose word is at `index` of `part`. */
|
|
494
|
+
function commandOperation(part: ReturnType<typeof lexCommand>["segments"][number], index: number): Operation {
|
|
495
|
+
const kept = part.words.filter((_, at) => at >= index && !part.redirects.has(at));
|
|
496
|
+
const words = kept.map((word) => decodeWord(word.text));
|
|
497
|
+
const tool = words[0].split("/").pop();
|
|
498
|
+
if (["rm", "rmdir", "shred"].includes(tool ?? "")) return asksForHelp(words, 1, false) ? "read" : "delete";
|
|
499
|
+
// POSIX/BSD unlink takes no options, so `unlink --help` removes a file
|
|
500
|
+
// named `--help`; rmtree and del are not modelled. Always deletes.
|
|
501
|
+
if (["unlink", "rmtree", "del"].includes(tool ?? "")) return "delete";
|
|
502
|
+
if (tool === "trash") {
|
|
503
|
+
// Hasna metadata/setup verbs take identifiers, not paths to remove.
|
|
504
|
+
// Keep legacy positional Trash clients and capture verbs guarded.
|
|
505
|
+
let at = 1;
|
|
506
|
+
while (at < words.length && words[at].startsWith("-")) {
|
|
507
|
+
const flag = words[at++];
|
|
508
|
+
if (["--help", "-h", "--version", "-V", "--identity"].includes(flag)) return "read";
|
|
509
|
+
if (["--agent", "--spool", "--files", "--info", "--config"].includes(flag)) at++;
|
|
510
|
+
else if (!["--json", "--local"].includes(flag)) return "delete";
|
|
511
|
+
}
|
|
512
|
+
if (at === words.length || TRASH_READ_VERBS.has(words[at])) return "read";
|
|
513
|
+
if (TRASH_HELP_VERBS.has(words[at]) && asksForHelp(words, at + 1, true)) return "read";
|
|
514
|
+
if (["restore", "restore-capsule"].includes(words[at])) return "write";
|
|
515
|
+
return "delete";
|
|
516
|
+
}
|
|
517
|
+
if (tool === "git") {
|
|
518
|
+
const sub = gitSubcommand(part, index);
|
|
519
|
+
// Honoured only for `git rm|clean <request>`: a global option before the
|
|
520
|
+
// subcommand is not modelled here, so it keeps the delete.
|
|
521
|
+
if (sub === "clean" || sub === "rm") return words[1] === sub && asksForHelp(words, 2, true) ? "read" : "delete";
|
|
522
|
+
return sub === "clone" || sub === "init" ? "write" : "read";
|
|
523
|
+
}
|
|
524
|
+
// find only reads unless an action primary deletes, runs a command or
|
|
525
|
+
// writes a file. A word the shell can expand may become such a primary, so
|
|
526
|
+
// only a find written out literally is read here; any other keeps the
|
|
527
|
+
// opaque reading below.
|
|
528
|
+
if (tool === "find" && !kept.some((word) => expandsAtRuntime(word.text)) && !words.some((word) => FIND_ACTIONS.has(word))) return "read";
|
|
529
|
+
if (tool === "env" && mentionsDeleteVerb(envSplitCommandText(words))) return "delete";
|
|
530
|
+
if (["sh", "bash", "zsh", "dash", "ksh", "ash", "eval", "source", ".", "su", "runuser", "env", "xargs", "find", "busybox", "toybox"].includes(tool ?? "")) {
|
|
531
|
+
return opaqueOperation(words.join(" "));
|
|
532
|
+
}
|
|
533
|
+
if (["mkdir", "mkfile", "touch", "mv", "cp", "ln", "tee", "install", "dd", "rsync", "scp", "truncate"].includes(tool ?? "")) return "write";
|
|
534
|
+
if (tool === "curl" || tool === "wget") return WRITE_FLAGS.test(words.join(" ")) ? "write" : "read";
|
|
535
|
+
if (tool === "sed" && words.slice(1).some((word) => /^-i|^--in-place(?:=|$)/.test(word))) return "write";
|
|
536
|
+
if (["python", "python3", "node", "bun"].includes(tool ?? "") && words.some((word) => word === "-c" || word === "-e")) return "write";
|
|
537
|
+
return "read";
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
// Retain the prior conservative behavior for embedded shell bodies. Braces
|
|
541
|
+
// and commas are also read as word breaks, so a verb that only brace
|
|
542
|
+
// expansion produces (`bash -c '{rm,-rf,build}'`) is still seen.
|
|
441
543
|
function opaqueOperation(segment: string): Operation {
|
|
544
|
+
const split = segment.replace(/[{},]/g, " ");
|
|
545
|
+
return split === segment ? opaqueReading(segment) : strongest(opaqueReading(segment), opaqueReading(split));
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
function opaqueReading(segment: string): Operation {
|
|
442
549
|
const trimmed = segment.trim();
|
|
443
550
|
if (!trimmed) return "read";
|
|
444
551
|
|
|
@@ -501,12 +608,14 @@ export function expandHomeSpelling(token: string, home: string): string {
|
|
|
501
608
|
/**
|
|
502
609
|
* Strip one level of wrapping shell-group punctuation from a segment so a
|
|
503
610
|
* subshell group like `(cd ~/.hasna/repos/clones && rm -rf hasna)` is analysed as
|
|
504
|
-
* its parts after the `&&` split.
|
|
611
|
+
* its parts after the `&&` split. `{` and `}` delimit a group only as words
|
|
612
|
+
* of their own: `{rm,-rf,build}` and `x{a,b}` are brace expansions, and
|
|
613
|
+
* `${HOME}` a parameter, so their braces stay.
|
|
505
614
|
*/
|
|
506
615
|
function unwrapSegment(segment: string): string {
|
|
507
616
|
let s = segment.trim();
|
|
508
|
-
while (s.startsWith("(") || s
|
|
509
|
-
while (s.endsWith(")") || s
|
|
617
|
+
while (s.startsWith("(") || /^\{(?:\s|$)/.test(s)) s = s.slice(1).trim();
|
|
618
|
+
while (s.endsWith(")") || /(?:^|\s)\}$/.test(s)) s = s.slice(0, -1).trim();
|
|
510
619
|
return s;
|
|
511
620
|
}
|
|
512
621
|
|
|
@@ -530,6 +639,110 @@ function cdDestinations(operand: string | undefined, bases: string[], home: stri
|
|
|
530
639
|
|
|
531
640
|
const REL_OPERAND = /(?:^|\s)(\.\.?|[^\s"';&|<>()\x60/]+(?:\/[^\s"';&|<>()\x60]*)?)\s*$/;
|
|
532
641
|
|
|
642
|
+
/**
|
|
643
|
+
* Index into `words` (the stage's shell words) of the command word the
|
|
644
|
+
* operands follow. It is the strict wrapper model's command (unwrapCommand)
|
|
645
|
+
* only where that agrees with the shared command-position walk that
|
|
646
|
+
* classified the stage, on the same words; otherwise 0, the first word.
|
|
647
|
+
*/
|
|
648
|
+
function commandStart(stage: string, words: ShellWord[]): number {
|
|
649
|
+
const strict = unwrapCommand(words);
|
|
650
|
+
if (!strict || strict.index <= 0 || words[strict.index].opaque) return 0;
|
|
651
|
+
const lexed = lexCommand(stage);
|
|
652
|
+
if (!lexed.trustworthy || lexed.substitutions.length > 0 || lexed.segments.length !== 1) return 0;
|
|
653
|
+
const [segment] = lexed.segments;
|
|
654
|
+
if (segment.redirects.size > 0 || segment.words.length !== words.length) return 0;
|
|
655
|
+
if (segment.words.some((word, at) => decodeWord(word.text) !== words[at].text)) return 0;
|
|
656
|
+
return commandWordIndex(segment) === strict.index ? strict.index : 0;
|
|
657
|
+
}
|
|
658
|
+
|
|
659
|
+
/**
|
|
660
|
+
* Operands after a command's own options: options end at `--` or, as BSD
|
|
661
|
+
* getopt does, at the first operand, so every later word is an operand even
|
|
662
|
+
* when it starts with `-`. A word built from an expansion is never an option:
|
|
663
|
+
* `-$X` can become `-` and a path. git's rm or clean subcommand word is not
|
|
664
|
+
* an operand: `git [-C dir] rm|clean ...` acts on the words after it.
|
|
665
|
+
*/
|
|
666
|
+
function positionalOperands(tool: string, args: ShellWord[]): ShellWord[] {
|
|
667
|
+
let from = 0;
|
|
668
|
+
if (tool === "git") from = args.findIndex((word) => word.text === "rm" || word.text === "clean") + 1;
|
|
669
|
+
let endOfOptions = false;
|
|
670
|
+
return args.slice(from).filter((word) => {
|
|
671
|
+
if (!endOfOptions && word.text === "--") {
|
|
672
|
+
endOfOptions = true;
|
|
673
|
+
return false;
|
|
674
|
+
}
|
|
675
|
+
if (!endOfOptions && word.text.startsWith("-") && !word.opaque) return false;
|
|
676
|
+
endOfOptions = true;
|
|
677
|
+
return true;
|
|
678
|
+
});
|
|
679
|
+
}
|
|
680
|
+
|
|
681
|
+
/** Global Trash flags before the verb that take the next word as their value, whatever it is. */
|
|
682
|
+
const TRASH_GLOBAL_VALUES = new Set(["--agent", "--spool", "--files", "--info", "--config"]);
|
|
683
|
+
/** Of those, the ones whose value is a path: kept as a target. */
|
|
684
|
+
const TRASH_GLOBAL_PATHS = new Set(["--spool", "--files", "--info", "--config"]);
|
|
685
|
+
const TRASH_GLOBAL_FLAGS = new Set(["--json", "--local", "--help", "-h", "--version", "--identity"]);
|
|
686
|
+
/** Capture-verb options whose value is never a path: `--agent NAME`, `--retention DAYS`. */
|
|
687
|
+
const TRASH_VALUE_OPTIONS: Record<string, RegExp> = { "--agent": /^[^-]/, "--retention": /^(?:\d+(?:\.\d+)?|never)$/ };
|
|
688
|
+
/** Capture-verb options whose value is a path (local mode's `--to`): kept as a target. */
|
|
689
|
+
const TRASH_PATH_OPTIONS = new Set(["--to"]);
|
|
690
|
+
|
|
691
|
+
/**
|
|
692
|
+
* The paths a Trash capture removes, from the words after `trash`, read the
|
|
693
|
+
* way @hasna/trash parses them: global flags before the verb take their next
|
|
694
|
+
* word as the value; after `put|rm|delete|remove`, options may come before,
|
|
695
|
+
* between or after the paths until `--` (Node's parseArgs in hosted mode,
|
|
696
|
+
* the same order in local mode), and every word after `--` is a path. Only
|
|
697
|
+
* the values of `--agent` and `--retention` are skipped, and only when the
|
|
698
|
+
* value is a plain word of the form the option takes; any other option is a
|
|
699
|
+
* flag that takes no word with it, so a word after it stays a path. A value
|
|
700
|
+
* that is or may be a path (`--spool`, `--to`) stays a target, and a word
|
|
701
|
+
* built from an expansion is a path, since it may become several words.
|
|
702
|
+
* Null when the words are not a capture-verb call (another verb, an unknown
|
|
703
|
+
* global flag, an expanded verb): the caller then reads them positionally.
|
|
704
|
+
*/
|
|
705
|
+
function trashOperands(args: ShellWord[]): ShellWord[] | null {
|
|
706
|
+
const operands: ShellWord[] = [];
|
|
707
|
+
let at = 0;
|
|
708
|
+
for (; at < args.length && args[at].text.startsWith("-"); at++) {
|
|
709
|
+
const flag = args[at];
|
|
710
|
+
if (flag.opaque) return null;
|
|
711
|
+
if (TRASH_GLOBAL_VALUES.has(flag.text)) {
|
|
712
|
+
// An expanded value may be no word or several, which moves the verb.
|
|
713
|
+
const value = args[++at];
|
|
714
|
+
if (!value || value.opaque) return null;
|
|
715
|
+
if (TRASH_GLOBAL_PATHS.has(flag.text)) operands.push(value);
|
|
716
|
+
} else if (!TRASH_GLOBAL_FLAGS.has(flag.text)) {
|
|
717
|
+
return null;
|
|
718
|
+
}
|
|
719
|
+
}
|
|
720
|
+
const verb = args[at];
|
|
721
|
+
if (!verb || verb.opaque || !TRASH_DELETE_VERBS.has(verb.text)) return null;
|
|
722
|
+
let endOfOptions = false;
|
|
723
|
+
for (at++; at < args.length; at++) {
|
|
724
|
+
const word = args[at];
|
|
725
|
+
if (endOfOptions || word.opaque || word.text === "-" || !word.text.startsWith("-")) {
|
|
726
|
+
operands.push(word);
|
|
727
|
+
continue;
|
|
728
|
+
}
|
|
729
|
+
if (word.text === "--") {
|
|
730
|
+
endOfOptions = true;
|
|
731
|
+
continue;
|
|
732
|
+
}
|
|
733
|
+
const eq = word.text.indexOf("=");
|
|
734
|
+
const name = eq < 0 ? word.text : word.text.slice(0, eq);
|
|
735
|
+
if (TRASH_PATH_OPTIONS.has(name)) {
|
|
736
|
+
if (eq >= 0) operands.push({ text: word.text.slice(eq + 1), opaque: false });
|
|
737
|
+
else if (args[at + 1] && !args[at + 1].text.startsWith("-")) operands.push(args[++at]);
|
|
738
|
+
continue;
|
|
739
|
+
}
|
|
740
|
+
const value = args[at + 1];
|
|
741
|
+
if (eq < 0 && Object.hasOwn(TRASH_VALUE_OPTIONS, name) && value && !value.opaque && TRASH_VALUE_OPTIONS[name].test(value.text)) at++;
|
|
742
|
+
}
|
|
743
|
+
return operands;
|
|
744
|
+
}
|
|
745
|
+
|
|
533
746
|
interface HeredocSpan {
|
|
534
747
|
/** Line of the `<<` operator, and the operator's columns on it. */
|
|
535
748
|
line: number;
|
|
@@ -701,8 +914,6 @@ const GIT_DATA_SUBCOMMANDS = new Set([
|
|
|
701
914
|
/** gh commands that take a body only as text or a request payload. */
|
|
702
915
|
const GH_DATA_COMMANDS = new Set(["pr", "issue", "api", "release", "gist"]);
|
|
703
916
|
|
|
704
|
-
const ASSIGNMENT = /^[A-Za-z_][A-Za-z0-9_]*\+?=/;
|
|
705
|
-
|
|
706
917
|
/**
|
|
707
918
|
* Whether the command word at `index` of `words` is a data command. git is
|
|
708
919
|
* accepted only with `-C <dir>`, `--no-pager` or `-P` before a data
|
|
@@ -723,8 +934,6 @@ function dataCommand(words: string[], index: number): boolean {
|
|
|
723
934
|
return false;
|
|
724
935
|
}
|
|
725
936
|
|
|
726
|
-
const baseName = (word: string): string => word.split("/").pop() ?? "";
|
|
727
|
-
|
|
728
937
|
/**
|
|
729
938
|
* Index of the `"` closing the one at `open`, past escapes and the
|
|
730
939
|
* substitutions inside it; -1 when there is none.
|
|
@@ -1038,28 +1247,14 @@ function segmentTargets(command: string, home: string, cwd: string): PathTarget[
|
|
|
1038
1247
|
// ...) act on their last one. A word that cannot be read statically
|
|
1039
1248
|
// fails closed to the directory.
|
|
1040
1249
|
const words = shellWords(stage.commandPart);
|
|
1041
|
-
// Operands after the command word
|
|
1042
|
-
//
|
|
1043
|
-
//
|
|
1044
|
-
//
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
const
|
|
1048
|
-
|
|
1049
|
-
if (tool === "git") {
|
|
1050
|
-
const sub = words.findIndex((word, index) => index > 0 && (word.text === "rm" || word.text === "clean"));
|
|
1051
|
-
if (sub > 0) operandStart = sub + 1;
|
|
1052
|
-
}
|
|
1053
|
-
let endOfOptions = false;
|
|
1054
|
-
const nonFlag = words.slice(operandStart).flatMap((word) => {
|
|
1055
|
-
if (!endOfOptions && word.text === "--") {
|
|
1056
|
-
endOfOptions = true;
|
|
1057
|
-
return [];
|
|
1058
|
-
}
|
|
1059
|
-
if (!endOfOptions && word.text.startsWith("-")) return [];
|
|
1060
|
-
endOfOptions = true;
|
|
1061
|
-
return [word.opaque ? "." : word.text];
|
|
1062
|
-
});
|
|
1250
|
+
// Operands after the command word, past any wrapper (`timeout 30`,
|
|
1251
|
+
// `env X=1`, `nice -n 5`) and its own arguments (commandStart). When
|
|
1252
|
+
// the wrappers cannot be read with certainty, the first word is taken
|
|
1253
|
+
// as the command and every later word can be an operand.
|
|
1254
|
+
const start = commandStart(stage.commandPart, words);
|
|
1255
|
+
const tool = baseName(words[start]?.text ?? "");
|
|
1256
|
+
const args = words.slice(start + 1);
|
|
1257
|
+
const nonFlag = ((tool === "trash" ? trashOperands(args) : null) ?? positionalOperands(tool, args)).map((word) => (word.opaque ? "." : word.text));
|
|
1063
1258
|
// mv changes its sources too, so every mv operand counts as a write.
|
|
1064
1259
|
const everyOperand =
|
|
1065
1260
|
stage.op === "delete" ||
|