@hasna/hooks 0.12.10 → 0.12.16
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 +69 -6
- package/bin/hooks/stop-review.js +1235 -0
- package/bin/hooks-mcp.js +298 -85
- package/bin/index.js +3348 -1086
- package/bin/native-readiness-entry.js +5 -1
- package/bin/native-safety-entry.js +27 -25
- package/bin/native-safety-worker.cjs +27 -25
- package/bin/serve.js +1362 -70
- package/dist/db/remote-storage.d.ts +7 -1
- package/dist/index.d.ts +4 -1
- package/dist/index.js +1665 -257
- package/dist/lib/codex-safety-check.d.ts +1 -1
- package/dist/lib/hosted-registry.d.ts +70 -0
- package/dist/lib/legacy-registry-artifact.d.ts +23 -0
- package/dist/lib/manifest.d.ts +7 -0
- package/dist/lib/native-safety.d.ts +1 -0
- package/dist/lib/native-target.d.ts +7 -0
- package/dist/lib/opencode-stop-plugin.d.ts +55 -0
- package/dist/lib/registry-artifact.d.ts +36 -0
- package/dist/lib/registry.d.ts +4 -0
- package/dist/lib/stop-review-context.d.ts +12 -0
- package/dist/lib/store.d.ts +4 -0
- package/dist/lib/sumi-stop-plugin.d.ts +129 -0
- package/dist/lib/sync.d.ts +10 -0
- package/dist/lib/transport.d.ts +1 -1
- package/dist/native-readiness.d.ts +1 -1
- package/dist/native-readiness.js +5 -1
- package/dist/openapi.d.ts +160 -16
- package/dist/opencode-stop-plugin.js +384 -0
- package/dist/registry-auth.d.ts +5 -0
- package/dist/registry-http.d.ts +4 -0
- package/dist/sdk/generated.d.ts +32 -12
- package/dist/sdk/index.d.ts +28 -5
- package/dist/sdk/index.js +49 -24
- package/dist/sdk/registry-client.d.ts +1 -1
- package/dist/serve.d.ts +4 -4
- package/dist/storage.js +33 -2
- package/dist/sumi-stop-plugin.js +380 -0
- package/hooks/hook-stop-review/README.md +109 -0
- package/hooks/hook-stop-review/manifest.json +10 -0
- package/hooks/hook-stop-review/package.json +11 -0
- package/hooks/hook-stop-review/src/hook.ts +59 -0
- package/hooks/hook-stop-review/src/index.ts +3 -0
- package/hooks/hook-stop-review/src/jev.ts +121 -0
- package/hooks/hook-stop-review/src/native.ts +246 -0
- package/hooks/hook-stop-review/src/policy.ts +145 -0
- package/hooks/hook-stop-review/src/runtime.ts +309 -0
- package/hooks/hook-stop-review/src/types.ts +121 -0
- package/hooks/hook-trash-guard/src/hook.ts +96 -0
- package/hooks/hook-workspace-repos-guard/src/hook.ts +116 -3
- package/hooks/local-executors.ts +841 -0
- package/package.json +22 -4
- package/scripts/build-bundled-hooks.ts +40 -0
- package/scripts/validate-bundled-hooks.ts +78 -0
- package/scripts/validate-opencode-stop-plugin.ts +42 -0
- package/scripts/validate-package.ts +9 -0
- package/scripts/validate-sumi-stop-plugin.ts +47 -0
|
@@ -90,6 +90,7 @@ import {
|
|
|
90
90
|
type WrapperSpec,
|
|
91
91
|
} from "../../shell-words";
|
|
92
92
|
export { expandBraces } from "../../shell-words";
|
|
93
|
+
import { launchedCommands, localExecutor, shellProgram } from "../../local-executors";
|
|
93
94
|
|
|
94
95
|
const RULE = "trash-guard";
|
|
95
96
|
|
|
@@ -1399,6 +1400,17 @@ function readCommandAt(segment: Segment, index: number, cwd: string, ctx: ScanCo
|
|
|
1399
1400
|
|
|
1400
1401
|
if (Object.hasOwn(REFUSED_VERBS, verb)) return { kind: "refuse", reason: REFUSED_VERBS[verb] };
|
|
1401
1402
|
|
|
1403
|
+
// Local executors (interpreters with an inline program, ssh/scp/sftp
|
|
1404
|
+
// local-exec options, rsync -e, git sshCommand, sed e/s///e) run a command
|
|
1405
|
+
// this hook cannot rewrite into trash. UNDER a protected repo-checkout root a
|
|
1406
|
+
// delete they could run — or a program we cannot read (fail closed) — is
|
|
1407
|
+
// refused; outside those roots the behaviour is unchanged, so this never
|
|
1408
|
+
// broadens refusals beyond a checkout. Checked before the verb-specific
|
|
1409
|
+
// branches so git/find/xargs do not short-circuit it. rm is never an
|
|
1410
|
+
// executor here, so the rewrite path below is unaffected.
|
|
1411
|
+
const execReading = readLocalExecutor(segment, index, cwd, ctx);
|
|
1412
|
+
if (execReading) return execReading;
|
|
1413
|
+
|
|
1402
1414
|
if (verb === "rm") {
|
|
1403
1415
|
const operands = rmOperands(segment, index);
|
|
1404
1416
|
// `rm` with no operand deletes nothing (GNU `rm` exits "missing
|
|
@@ -1511,9 +1523,93 @@ function readCommandAt(segment: Segment, index: number, cwd: string, ctx: ScanCo
|
|
|
1511
1523
|
};
|
|
1512
1524
|
}
|
|
1513
1525
|
}
|
|
1526
|
+
|
|
1514
1527
|
return { kind: "none" };
|
|
1515
1528
|
}
|
|
1516
1529
|
|
|
1530
|
+
/**
|
|
1531
|
+
* If the command at `index` of `segment` is a local executor — an interpreter
|
|
1532
|
+
* with an inline program (awk system(), perl/ruby/python/node -e/-c, …), an
|
|
1533
|
+
* ssh/scp/sftp local-exec option (ProxyCommand and friends) or `-F` config,
|
|
1534
|
+
* rsync's `-e` remote shell, git's local sshCommand, or sed's `e`/`s///e` — and
|
|
1535
|
+
* it runs UNDER a protected repo-checkout root, and a delete it could run is
|
|
1536
|
+
* visible or a program cannot be read (fail closed), refuse: the delete cannot
|
|
1537
|
+
* be redirected into trash. Outside the roots this reads nothing, so trash-guard
|
|
1538
|
+
* keeps its behaviour there, exactly as `sh -c` did. Returns null otherwise.
|
|
1539
|
+
*/
|
|
1540
|
+
function readLocalExecutor(
|
|
1541
|
+
segment: Segment,
|
|
1542
|
+
index: number,
|
|
1543
|
+
cwd: string,
|
|
1544
|
+
ctx: ScanContext,
|
|
1545
|
+
scope: { segment: Segment; index: number } = { segment, index },
|
|
1546
|
+
depth = 0,
|
|
1547
|
+
): Reading | null {
|
|
1548
|
+
const verb = commandName(segment.words[index].text);
|
|
1549
|
+
// A launcher (find -exec/-execdir/-ok/-okdir, xargs) runs a command of its
|
|
1550
|
+
// own: read it as a command in its own right, by both command-position
|
|
1551
|
+
// models, with the launcher's operands (find's start paths) and cwd deciding
|
|
1552
|
+
// whether it runs under a checkout.
|
|
1553
|
+
for (const launched of launchedCommands(segment.words.map((word) => decodeWord(word.text)), index, (at) => segment.redirects.has(at))) {
|
|
1554
|
+
if (depth >= 8) return executorTargetsHandoff(scope.segment, scope.index, cwd, ctx.home) ? { kind: "refuse", reason: LAUNCHER_DEPTH_REASON } : null;
|
|
1555
|
+
const sub: Segment = {
|
|
1556
|
+
words: segment.words.slice(launched.start, launched.end),
|
|
1557
|
+
redirects: new Set([...segment.redirects].filter((at) => at >= launched.start && at < launched.end).map((at) => at - launched.start)),
|
|
1558
|
+
};
|
|
1559
|
+
const strict = strictCommandIndex(sub);
|
|
1560
|
+
for (const inner of new Set([commandWordIndex(sub), strict ?? -1])) {
|
|
1561
|
+
if (inner < 0) continue;
|
|
1562
|
+
const reading = readLocalExecutor(sub, inner, cwd, ctx, scope, depth + 1);
|
|
1563
|
+
if (reading) return reading;
|
|
1564
|
+
}
|
|
1565
|
+
}
|
|
1566
|
+
// `sh|bash|… -c PROGRAM` / `eval WORD…` whose program is not a plain literal
|
|
1567
|
+
// (an expansion, a substitution, several eval words) cannot be read at all;
|
|
1568
|
+
// under a checkout it is refused. A literal program keeps the shell-string
|
|
1569
|
+
// reading below (OPAQUE_COMMANDS), as before.
|
|
1570
|
+
const kept = segment.words.map((word, at) => ({ word, at })).filter(({ at }) => !segment.redirects.has(at));
|
|
1571
|
+
const keptIndex = kept.findIndex(({ at }) => at === index);
|
|
1572
|
+
const shell = keptIndex < 0 ? null : shellProgram(kept.map(({ word }) => word.text), keptIndex);
|
|
1573
|
+
if (shell && !shell.literal && (executorTargetsHandoff(segment, index, cwd, ctx.home) || executorTargetsHandoff(scope.segment, scope.index, cwd, ctx.home))) {
|
|
1574
|
+
return {
|
|
1575
|
+
kind: "refuse",
|
|
1576
|
+
reason: `\`${verb}\` runs a shell program that is not a plain literal (an expansion, a substitution or several words), so this hook cannot read what it would delete under a protected repository checkout. Write the program out literally, or run it from outside the checkout.`,
|
|
1577
|
+
};
|
|
1578
|
+
}
|
|
1579
|
+
const exec = localExecutor(
|
|
1580
|
+
segment.words.map((word) => decodeWord(word.text)),
|
|
1581
|
+
index,
|
|
1582
|
+
(at) => at >= 0 && at < segment.words.length && (segment.redirects.has(at) || expandsWord(segment.words[at].text)),
|
|
1583
|
+
);
|
|
1584
|
+
if (!exec || !(executorTargetsHandoff(segment, index, cwd, ctx.home) || executorTargetsHandoff(scope.segment, scope.index, cwd, ctx.home))) return null;
|
|
1585
|
+
if (!exec.unresolved && !exec.programs.some((program) => mentionsDeleteVerb(program.text))) return null;
|
|
1586
|
+
return {
|
|
1587
|
+
kind: "refuse",
|
|
1588
|
+
reason: `\`${verb}\` runs a program locally (an interpreter's inline program or a command-running option) this hook cannot rewrite, and it could delete a file under a protected repository checkout. Run the delete directly as \`rm -- <path>\` (intercepted and redirected into trash), or run the command from outside the checkout.`,
|
|
1589
|
+
};
|
|
1590
|
+
}
|
|
1591
|
+
|
|
1592
|
+
const LAUNCHER_DEPTH_REASON =
|
|
1593
|
+
"This command nests find/xargs launchers too deeply for this hook to read what they run under a protected repository checkout. Run the inner command directly.";
|
|
1594
|
+
|
|
1595
|
+
/**
|
|
1596
|
+
* Whether a local executor's effective cwd, or one of its literal operands,
|
|
1597
|
+
* sits under a protected repo-checkout root — the only place trash-guard adds a
|
|
1598
|
+
* new refusal for this class. Operands are every word after the command word;
|
|
1599
|
+
* a word the shell would expand cannot be resolved and is ignored here (the
|
|
1600
|
+
* cwd test still applies, and workspace-repos-guard fails those closed).
|
|
1601
|
+
*/
|
|
1602
|
+
function executorTargetsHandoff(segment: Segment, index: number, cwd: string, home: string): boolean {
|
|
1603
|
+
const roots = handoffRoots(home);
|
|
1604
|
+
const underRoot = (target: string | null): boolean => target !== null && roots.some((root) => isUnder(target, root));
|
|
1605
|
+
if (roots.some((root) => isUnder(cwd, root))) return true;
|
|
1606
|
+
for (let at = index + 1; at < segment.words.length; at++) {
|
|
1607
|
+
if (segment.redirects.has(at)) continue;
|
|
1608
|
+
if (underRoot(resolveLiteralTarget(segment.words[at].text, cwd, home))) return true;
|
|
1609
|
+
}
|
|
1610
|
+
return false;
|
|
1611
|
+
}
|
|
1612
|
+
|
|
1517
1613
|
/**
|
|
1518
1614
|
* Classify a command string: which `rm` verbs are ours to rewrite, which are
|
|
1519
1615
|
* handed to the guarded-roots hook, and why anything else is refused.
|
|
@@ -82,6 +82,7 @@ import {
|
|
|
82
82
|
type CodewithHookOutput,
|
|
83
83
|
} from "../../codewith-native-common";
|
|
84
84
|
import { ASSIGNMENT, baseName, expandBraces, splitsOrMoves, unwrapCommand, type ShellWord } from "../../shell-words";
|
|
85
|
+
import { embeddedProgramIsReadOnly, launchedCommands, localExecutor, shellProgram, type ExecutorFinding } from "../../local-executors";
|
|
85
86
|
|
|
86
87
|
export { expandBraces, unwrapCommand };
|
|
87
88
|
|
|
@@ -462,6 +463,9 @@ function expandsAtRuntime(raw: string): boolean {
|
|
|
462
463
|
function segmentOperation(segment: string): Operation {
|
|
463
464
|
const trimmed = segment.trim();
|
|
464
465
|
if (!trimmed) return "read";
|
|
466
|
+
// A destructive local executor counts wherever it hides: in a substitution
|
|
467
|
+
// body, or in a command the opaque reading below would not take apart.
|
|
468
|
+
if (hidesDestructiveExecutor(trimmed)) return "delete";
|
|
465
469
|
const parsed = lexCommand(trimmed);
|
|
466
470
|
// Preserve conservative classification where the shell body is opaque.
|
|
467
471
|
if (!parsed.trustworthy || parsed.substitutions.length || /\$\{[^}]*[|;()]|[<>]\(/.test(trimmed)) return opaqueOperation(trimmed);
|
|
@@ -493,8 +497,82 @@ function partOperation(part: ReturnType<typeof lexCommand>["segments"][number]):
|
|
|
493
497
|
return strongest(shared, commandOperation(part, kept[strict.index].at));
|
|
494
498
|
}
|
|
495
499
|
|
|
500
|
+
/**
|
|
501
|
+
* Whether the simple command at `index` of `part` runs a program or option
|
|
502
|
+
* value locally that is not provably read-only — awk/perl/python/ruby/node/…
|
|
503
|
+
* inline programs, an ssh/scp/sftp local-exec option (ProxyCommand and friends)
|
|
504
|
+
* or `-F` config, rsync's `-e` remote shell, git's local sshCommand, and sed's
|
|
505
|
+
* `e`/`s///e` execute forms. A shell option value (ProxyCommand, rsync `-e`,
|
|
506
|
+
* sshCommand) is judged as the shell command it is; an interpreter program by
|
|
507
|
+
* its grammar (embeddedProgramIsReadOnly); an unreadable value fails closed.
|
|
508
|
+
* Such a command is treated as a delete so the structural guard refuses it at
|
|
509
|
+
* any depth under a protected root (a write would be allowed inside a repo).
|
|
510
|
+
*/
|
|
511
|
+
function runsDestructiveLocalProgram(part: ReturnType<typeof lexCommand>["segments"][number], index: number, depth = 0): boolean {
|
|
512
|
+
if (index < 0 || depth > 8) return depth > 8;
|
|
513
|
+
// `sh|bash|… -c PROGRAM` and `eval WORD…`: a program that is not a plain
|
|
514
|
+
// literal fails closed; a literal one keeps its existing (opaque) reading,
|
|
515
|
+
// except that a destructive local executor inside it counts here too.
|
|
516
|
+
const kept = part.words.map((word, at) => ({ word, at })).filter(({ at }) => !part.redirects.has(at));
|
|
517
|
+
const keptIndex = kept.findIndex(({ at }) => at === index);
|
|
518
|
+
const shell = keptIndex < 0 ? null : shellProgram(kept.map(({ word }) => word.text), keptIndex);
|
|
519
|
+
if (shell) {
|
|
520
|
+
if (!shell.literal) return true;
|
|
521
|
+
const inner = lexCommand(shell.program!);
|
|
522
|
+
if (inner.trustworthy && inner.segments.some((segment) => segmentRunsDestructive(segment, depth + 1))) return true;
|
|
523
|
+
}
|
|
524
|
+
const decoded = part.words.map((word) => decodeWord(word.text));
|
|
525
|
+
// A launcher (find -exec/-execdir/-ok/-okdir, xargs) runs a command of its
|
|
526
|
+
// own; that command is read like one in command position, wrappers included.
|
|
527
|
+
for (const launched of launchedCommands(decoded, index, (at) => part.redirects.has(at))) {
|
|
528
|
+
if (segmentRunsDestructive(subSegment(part, launched.start, launched.end), depth + 1)) return true;
|
|
529
|
+
}
|
|
530
|
+
const opaqueAt = (at: number) => at >= 0 && at < part.words.length && (part.redirects.has(at) || expandsAtRuntime(part.words[at].text));
|
|
531
|
+
const exec: ExecutorFinding | null = localExecutor(decoded, index, opaqueAt);
|
|
532
|
+
if (!exec) return false;
|
|
533
|
+
if (exec.unresolved) return true;
|
|
534
|
+
return exec.programs.some((program) =>
|
|
535
|
+
program.kind === "shell" ? segmentOperation(program.text) !== "read" : !embeddedProgramIsReadOnly(program));
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
/** The words `[start, end)` of a lexed simple command as a command of their own, redirections kept. */
|
|
539
|
+
function subSegment(part: ReturnType<typeof lexCommand>["segments"][number], start: number, end: number): ReturnType<typeof lexCommand>["segments"][number] {
|
|
540
|
+
const redirects = new Set([...part.redirects].filter((at) => at >= start && at < end).map((at) => at - start));
|
|
541
|
+
return { words: part.words.slice(start, end), redirects };
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
/** Whether a lexed simple command, read by both command-position models, runs a destructive local program. */
|
|
545
|
+
function segmentRunsDestructive(segment: ReturnType<typeof lexCommand>["segments"][number], depth = 0): boolean {
|
|
546
|
+
const shared = commandWordIndex(segment);
|
|
547
|
+
if (shared >= 0 && runsDestructiveLocalProgram(segment, shared, depth)) return true;
|
|
548
|
+
const kept = segment.words.map((word, at) => ({ word, at })).filter(({ at }) => !segment.redirects.has(at));
|
|
549
|
+
const strict = unwrapCommand(kept.map(({ word }) => ({ text: decodeWord(word.text), opaque: /[$\x60]/.test(word.text) })));
|
|
550
|
+
return strict !== null && strict.index >= 0 && kept[strict.index].at !== shared && runsDestructiveLocalProgram(segment, kept[strict.index].at, depth);
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
/**
|
|
554
|
+
* Whether `text` holds a destructive local executor anywhere the lexer can see
|
|
555
|
+
* one: in its own simple commands, or in a `$( )`/backtick substitution body
|
|
556
|
+
* (quoted or not), at any nesting. The rule-based reading short-circuits to the
|
|
557
|
+
* opaque reading when a command holds a substitution, and never reads a quoted
|
|
558
|
+
* substitution's body, so without this an executor whose program arrives
|
|
559
|
+
* through one (`perl -e $(cat p)`, `echo "$(awk 'BEGIN{system(…)}')"`) would
|
|
560
|
+
* escape the fail-closed rule.
|
|
561
|
+
*/
|
|
562
|
+
function hidesDestructiveExecutor(text: string, depth = 0): boolean {
|
|
563
|
+
if (depth > 4) return false;
|
|
564
|
+
const lexed = lexCommand(text);
|
|
565
|
+
if (lexed.trustworthy && lexed.segments.some((segment) => segmentRunsDestructive(segment))) return true;
|
|
566
|
+
const bodies = commandSubstitutions(text);
|
|
567
|
+
return (bodies ?? lexed.substitutions).some((body) => hidesDestructiveExecutor(body, depth + 1));
|
|
568
|
+
}
|
|
569
|
+
|
|
496
570
|
/** The operation of the command whose word is at `index` of `part`. */
|
|
497
571
|
function commandOperation(part: ReturnType<typeof lexCommand>["segments"][number], index: number): Operation {
|
|
572
|
+
// A command that runs a destructive program or option value locally (awk
|
|
573
|
+
// system(), perl -e, ssh ProxyCommand, sed e, …) is a delete here, whatever
|
|
574
|
+
// its own command word would otherwise read as.
|
|
575
|
+
if (runsDestructiveLocalProgram(part, index)) return "delete";
|
|
498
576
|
const kept = part.words.filter((_, at) => at >= index && !part.redirects.has(at));
|
|
499
577
|
const words = kept.map((word) => decodeWord(word.text));
|
|
500
578
|
const tool = words[0].split("/").pop();
|
|
@@ -1146,7 +1224,7 @@ interface ListSplitCuts {
|
|
|
1146
1224
|
* is cut, or the command cannot be lexed to its end or holds a substitution:
|
|
1147
1225
|
* every part then keeps its own reading.
|
|
1148
1226
|
*/
|
|
1149
|
-
function listSplitCuts(command: string, parts: string[]): ListSplitCuts | null {
|
|
1227
|
+
function listSplitCuts(command: string, parts: string[], allowSubstitutions = false): ListSplitCuts | null {
|
|
1150
1228
|
const offsets: number[] = [];
|
|
1151
1229
|
let at = 0;
|
|
1152
1230
|
for (const part of parts) {
|
|
@@ -1154,7 +1232,9 @@ function listSplitCuts(command: string, parts: string[]): ListSplitCuts | null {
|
|
|
1154
1232
|
at += part.length;
|
|
1155
1233
|
}
|
|
1156
1234
|
const lexed = lexCommand(command);
|
|
1157
|
-
|
|
1235
|
+
// Clearing a part to a read (cutPartReads) needs a command with no
|
|
1236
|
+
// substitution; refusing one (cutPartExecutes) only needs it lexed whole.
|
|
1237
|
+
if (!lexed.trustworthy || (!allowSubstitutions && lexed.substitutions.length > 0)) return null;
|
|
1158
1238
|
const words = lexed.segments.flatMap((segment) => segment.words).filter((word) =>
|
|
1159
1239
|
parts.some((part, k) => k % 2 === 1 && offsets[k] < word.end && offsets[k] + part.length > word.start));
|
|
1160
1240
|
return words.length > 0 ? { lexed, offsets, words } : null;
|
|
@@ -1188,6 +1268,26 @@ function onlyReads(segment: LexedSegment, text: string, skip: LexedWord[] = []):
|
|
|
1188
1268
|
&& !decoded.some((word) => FIND_ACTIONS.has(word));
|
|
1189
1269
|
}
|
|
1190
1270
|
|
|
1271
|
+
/**
|
|
1272
|
+
* Whether the part of `command` from `from` to `to` holds a piece of a quoted
|
|
1273
|
+
* word that is the inline program of a destructive local executor. The `;`,
|
|
1274
|
+
* `&&`, `||` and newline split cuts an executor's quoted program at a separator
|
|
1275
|
+
* inside it (`python3 -c 'import os; os.remove("x")'`), so no single part reads
|
|
1276
|
+
* as the delete it is. The lexed command keeps the program whole, so here the
|
|
1277
|
+
* owning lexed command is read as one: when it runs a destructive local program
|
|
1278
|
+
* (runsDestructiveLocalProgram), the part counts as a delete, whatever its
|
|
1279
|
+
* unbalanced quote makes the opaque reading say. Only a cut quoted word counts,
|
|
1280
|
+
* so a plain `a; rm` list is unaffected (its `rm` part already reads as a
|
|
1281
|
+
* delete).
|
|
1282
|
+
*/
|
|
1283
|
+
function cutPartExecutes(command: string, cuts: ListSplitCuts, from: number, to: number): boolean {
|
|
1284
|
+
const overlaps = (word: LexedWord) => word.start < to && word.end > from;
|
|
1285
|
+
if (!cuts.words.some(overlaps)) return false;
|
|
1286
|
+
return cuts.lexed.segments
|
|
1287
|
+
.filter((segment) => segment.words.some(overlaps))
|
|
1288
|
+
.some((segment) => segmentRunsDestructive(segment));
|
|
1289
|
+
}
|
|
1290
|
+
|
|
1191
1291
|
/** The text of the lexed simple commands in `text`, first word to last. */
|
|
1192
1292
|
const segmentText = (text: string, segment: LexedSegment): string =>
|
|
1193
1293
|
text.slice(segment.words[0].start, segment.words[segment.words.length - 1].end);
|
|
@@ -1253,6 +1353,7 @@ function segmentTargets(command: string, home: string, cwd: string): PathTarget[
|
|
|
1253
1353
|
const underRoot = (path: string) => roots.some((root) => path === root || path.startsWith(`${root}${sep}`));
|
|
1254
1354
|
const parts = command.split(LIST_SEPARATOR);
|
|
1255
1355
|
const cuts = listSplitCuts(command, parts);
|
|
1356
|
+
const execCuts = cuts ?? listSplitCuts(command, parts, true);
|
|
1256
1357
|
|
|
1257
1358
|
const targets: PathTarget[] = [];
|
|
1258
1359
|
// Every directory the current segment can run in, every directory any
|
|
@@ -1279,6 +1380,14 @@ function segmentTargets(command: string, home: string, cwd: string): PathTarget[
|
|
|
1279
1380
|
// words belong to (cutPartReads); when every one only reads, so does the
|
|
1280
1381
|
// part, whatever its unbalanced quote makes the opaque reading say.
|
|
1281
1382
|
const readsOnly = cuts !== null && cutPartReads(command, cuts, cuts.offsets[index], cuts.offsets[index] + rawSegment.length);
|
|
1383
|
+
// The converse: a part holding a piece of a destructive local executor's
|
|
1384
|
+
// quoted program (`python3 -c 'import os; os.remove("x")'` cut at its `;`)
|
|
1385
|
+
// is a delete of every protected directory it can run in, so a split
|
|
1386
|
+
// program is refused just as the whole command is.
|
|
1387
|
+
// The part itself is also read whole before any group unwrapping, which
|
|
1388
|
+
// strips a trailing `)` and would cut `perl -e $(cat p)` short.
|
|
1389
|
+
const executes = !readsOnly && ((execCuts !== null && cutPartExecutes(command, execCuts, execCuts.offsets[index], execCuts.offsets[index] + rawSegment.length))
|
|
1390
|
+
|| hidesDestructiveExecutor(rawSegment.trim()));
|
|
1282
1391
|
|
|
1283
1392
|
const plainCd = segment.match(/^cd(?:\s+(?:-[A-Za-z@]+|--))*(?:\s+([^\s;&|<>()\x60]+))?$/);
|
|
1284
1393
|
const embeddedCd = plainCd ? null : segment.match(/(?:^|\s)cd(?:\s+(?:-[A-Za-z]+|--))*\s+([^\s;&|<>()\x60]+)/);
|
|
@@ -1319,7 +1428,7 @@ function segmentTargets(command: string, home: string, cwd: string): PathTarget[
|
|
|
1319
1428
|
if (operand) operands.push(operand);
|
|
1320
1429
|
return " ";
|
|
1321
1430
|
});
|
|
1322
|
-
const commandOp: Operation = readsOnly ? "read" : segmentOperation(commandPart);
|
|
1431
|
+
const commandOp: Operation = readsOnly ? "read" : executes ? "delete" : segmentOperation(commandPart);
|
|
1323
1432
|
return { commandPart, operands, op: opaqueRedirect && commandOp === "read" ? ("write" as Operation) : commandOp };
|
|
1324
1433
|
});
|
|
1325
1434
|
|
|
@@ -1334,6 +1443,10 @@ function segmentTargets(command: string, home: string, cwd: string): PathTarget[
|
|
|
1334
1443
|
? "write"
|
|
1335
1444
|
: "read";
|
|
1336
1445
|
const bases = current.filter(underRoot);
|
|
1446
|
+
// A part cut out of a destructive executor's quoted program is a delete of
|
|
1447
|
+
// every protected directory it can run in; its own words no longer parse,
|
|
1448
|
+
// so it is read from the whole lexed command, not its stages.
|
|
1449
|
+
if (executes) for (const base of bases) targets.push({ path: base, op: "delete" });
|
|
1337
1450
|
for (const stage of stages) {
|
|
1338
1451
|
let foundExplicit = false;
|
|
1339
1452
|
for (const path of explicitTargets(stage.commandPart)) {
|