@hasna/hooks 0.12.17 → 0.12.19
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/bin/native-safety-entry.js +37 -27
- package/bin/native-safety-worker.cjs +37 -27
- package/hooks/codewith-native-common.ts +47 -8
- package/hooks/hook-trash-guard/src/hook.ts +98 -94
- package/hooks/hook-workspace-repos-guard/src/hook.ts +98 -16
- package/hooks/local-executors.ts +268 -96
- package/hooks/native-safety-composite.ts +5 -1
- package/package.json +1 -1
package/hooks/local-executors.ts
CHANGED
|
@@ -48,6 +48,30 @@ export function mentionsDeleteVerb(text: string): boolean {
|
|
|
48
48
|
return /(^|[^A-Za-z0-9_.-])(rm|rmdir|unlink|shred|trash)(?![A-Za-z0-9_.-])/.test(text);
|
|
49
49
|
}
|
|
50
50
|
|
|
51
|
+
/**
|
|
52
|
+
* Bash keeps a backslash inside double quotes unless it comes before `$`, a
|
|
53
|
+
* backtick, `"`, `\` or a newline, and the guards' word decoders read it so.
|
|
54
|
+
* Their earlier reading dropped it before every character. `on` is set while
|
|
55
|
+
* a guard reads a command a second time with that earlier reading
|
|
56
|
+
* (readWithLegacyQuotes), so that whatever it refused stays refused.
|
|
57
|
+
*/
|
|
58
|
+
export const legacyQuotes = { on: false };
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* `read()` with the earlier double-quote backslash reading, or undefined for
|
|
62
|
+
* a command both readings read alike (it holds no backslash or no `"`).
|
|
63
|
+
*/
|
|
64
|
+
export function readWithLegacyQuotes<T>(command: string, read: () => T): T | undefined {
|
|
65
|
+
if (!command.includes("\\") || !command.includes('"')) return undefined;
|
|
66
|
+
const was = legacyQuotes.on;
|
|
67
|
+
legacyQuotes.on = true;
|
|
68
|
+
try {
|
|
69
|
+
return read();
|
|
70
|
+
} finally {
|
|
71
|
+
legacyQuotes.on = was;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
51
75
|
/** The grammar an embedded program is written in, which decides how it reads. */
|
|
52
76
|
export type ProgramKind = "c-interp" | "awk" | "shell";
|
|
53
77
|
|
|
@@ -65,6 +89,12 @@ export interface ExecutorFinding {
|
|
|
65
89
|
* read). The caller fails closed: under a protected root it refuses.
|
|
66
90
|
*/
|
|
67
91
|
unresolved: boolean;
|
|
92
|
+
/**
|
|
93
|
+
* Files the command writes because its own program says so (sed's `w`, `W`
|
|
94
|
+
* and `s///w` targets), as written. The caller judges each as a write target,
|
|
95
|
+
* exactly as it judges a `>` redirect target.
|
|
96
|
+
*/
|
|
97
|
+
writes?: string[];
|
|
68
98
|
}
|
|
69
99
|
|
|
70
100
|
const baseName = (word: string): string => word.split("/").pop() ?? "";
|
|
@@ -119,7 +149,7 @@ const SSH_LOCAL_EXEC_OPTS = new Set(["proxycommand", "localcommand", "knownhosts
|
|
|
119
149
|
* `>`/`|` are NOT dangerous here — the shell already took the real redirections
|
|
120
150
|
* and pipes, so inside the program they are comparison/bit operators.
|
|
121
151
|
*/
|
|
122
|
-
const C_INTERP_DANGER = /(?:`|\$\()|\b(?:system|exec|execve|execvp|popen|spawn|posix_spawn|fork|subprocess|Popen|check_output|check_call|shell_exec|passthru|proc_open|pcntl_exec|qx|open|fopen|sysopen|unlink|remove|delete|rmtree|rmdir|mkdir|rename|truncate|ftruncate|chmod|chown|symlink|writeFile|createWriteStream|appendFile|child_process|FileUtils|shutil|Kernel|require|import|load|eval|os|sys|io|fs|File|IO|Dir|Path|pathlib)/;
|
|
152
|
+
const C_INTERP_DANGER = /(?:`|\$\()|\b(?:system|exec|execve|execvp|popen|spawn|posix_spawn|fork|subprocess|Popen|check_output|check_call|shell_exec|passthru|proc_open|pcntl_exec|qx|open|fopen|sysopen|unlink|remove|delete|rmtree|rmdir|mkdir|rename|truncate|ftruncate|chmod|chown|symlink|writeFile|createWriteStream|appendFile|child_process|FileUtils|shutil|Kernel|require|import|load|eval|os|sys|io|fs|File|IO|Dir|Path|pathlib|Deno|Bun)|%x\W/;
|
|
123
153
|
|
|
124
154
|
/**
|
|
125
155
|
* awk danger: `system()` or a pipe/redirect (`| "cmd"`, `print > "file"`), a
|
|
@@ -182,6 +212,25 @@ function interpPrograms(name: string, words: string[], index: number, opaque: (a
|
|
|
182
212
|
return { programs, unresolved };
|
|
183
213
|
}
|
|
184
214
|
|
|
215
|
+
/**
|
|
216
|
+
* `deno eval [OPTIONS] CODE [ARGS…]`: deno runs CODE inline. The subcommand is
|
|
217
|
+
* the first word after deno that is not an option (`-L`/`--log-level` take a
|
|
218
|
+
* value). Every later word that is not an option is judged as the program, so
|
|
219
|
+
* an option value cannot hide it; an expanded one is unresolved.
|
|
220
|
+
*/
|
|
221
|
+
function denoEvalPrograms(words: string[], index: number, opaque: (at: number) => boolean): ExecutorFinding | null {
|
|
222
|
+
let at = index + 1;
|
|
223
|
+
for (; at < words.length && words[at].startsWith("-"); at++) if (words[at] === "-L" || words[at] === "--log-level") at++;
|
|
224
|
+
if (words[at] !== "eval" || opaque(at)) return null;
|
|
225
|
+
const programs: EmbeddedProgram[] = [];
|
|
226
|
+
let unresolved = false;
|
|
227
|
+
for (at++; at < words.length; at++) {
|
|
228
|
+
if (opaque(at)) { unresolved = true; continue; }
|
|
229
|
+
if (!words[at].startsWith("-")) programs.push({ text: words[at], kind: "c-interp" });
|
|
230
|
+
}
|
|
231
|
+
return programs.length === 0 && !unresolved ? null : { programs, unresolved };
|
|
232
|
+
}
|
|
233
|
+
|
|
185
234
|
/** The awk program: every `-e` value, or the first bare operand; `-f` file is unresolved. */
|
|
186
235
|
function awkProgram(words: string[], index: number, opaque: (at: number) => boolean): ExecutorFinding {
|
|
187
236
|
const programs: EmbeddedProgram[] = [];
|
|
@@ -499,11 +548,10 @@ function gitPrograms(words: string[], index: number, opaque: (at: number) => boo
|
|
|
499
548
|
/**
|
|
500
549
|
* The text a shell word stands for when it is a plain literal, or null when the
|
|
501
550
|
* shell can change it: a `$` expansion or substitution (unquoted or in double
|
|
502
|
-
* quotes), a backtick, an unquoted glob, brace or leading tilde, `$"…"`, or
|
|
503
|
-
* `$'…'
|
|
551
|
+
* quotes), a backtick, an unquoted glob, brace or leading tilde, `$"…"`, or
|
|
552
|
+
* ANSI-C quoting `$'…'`, whose escapes differ between shells.
|
|
504
553
|
*/
|
|
505
554
|
export function literalWord(raw: string): string | null {
|
|
506
|
-
const ANSI: Record<string, string> = { n: "\n", t: "\t", r: "\r", a: "\x07", b: "\b", e: "\x1b", E: "\x1b", f: "\f", v: "\v", "\\": "\\", "'": "'", '"': '"', "?": "?" };
|
|
507
555
|
if (/^~/.test(raw)) return null;
|
|
508
556
|
let out = "";
|
|
509
557
|
for (let i = 0; i < raw.length; i++) {
|
|
@@ -513,19 +561,6 @@ export function literalWord(raw: string): string | null {
|
|
|
513
561
|
if (end < 0) return null;
|
|
514
562
|
out += raw.slice(i + 1, end);
|
|
515
563
|
i = end;
|
|
516
|
-
} else if (ch === "$" && raw[i + 1] === "'") {
|
|
517
|
-
let j = i + 2;
|
|
518
|
-
for (; j < raw.length && raw[j] !== "'"; j++) {
|
|
519
|
-
if (raw[j] !== "\\") { out += raw[j]; continue; }
|
|
520
|
-
const c = raw[j + 1];
|
|
521
|
-
let m: RegExpMatchArray | null;
|
|
522
|
-
if (c !== undefined && Object.hasOwn(ANSI, c)) { out += ANSI[c]; j++; }
|
|
523
|
-
else if ((m = raw.slice(j + 1).match(/^[0-7]{1,3}/))) { out += String.fromCharCode(parseInt(m[0], 8) & 0xff); j += m[0].length; }
|
|
524
|
-
else if ((m = raw.slice(j + 1).match(/^x([0-9A-Fa-f]{1,2})/))) { out += String.fromCharCode(parseInt(m[1], 16)); j += m[0].length; }
|
|
525
|
-
else return null;
|
|
526
|
-
}
|
|
527
|
-
if (j >= raw.length) return null;
|
|
528
|
-
i = j;
|
|
529
564
|
} else if (ch === "$" || ch === "\x60") {
|
|
530
565
|
return null;
|
|
531
566
|
} else if (ch === '"') {
|
|
@@ -533,7 +568,8 @@ export function literalWord(raw: string): string | null {
|
|
|
533
568
|
for (; j < raw.length && raw[j] !== '"'; j++) {
|
|
534
569
|
const c = raw[j];
|
|
535
570
|
if (c === "$" || c === "\x60") return null;
|
|
536
|
-
|
|
571
|
+
// A line continuation removes the backslash and the newline.
|
|
572
|
+
if (c === "\\" && j + 1 < raw.length && /[$\x60"\\\n]/.test(raw[j + 1])) { if (raw[++j] !== "\n" || legacyQuotes.on) out += raw[j]; continue; }
|
|
537
573
|
out += c;
|
|
538
574
|
}
|
|
539
575
|
if (j >= raw.length) return null;
|
|
@@ -585,15 +621,22 @@ export interface ShellProgram {
|
|
|
585
621
|
* cluster, `-lc`, `-ec`) or `eval WORD…`, from the RAW words of one simple
|
|
586
622
|
* command (redirections removed) with the command word at `index`. A literal
|
|
587
623
|
* program is returned for the caller to classify as it does today; a program
|
|
588
|
-
* that is not a plain literal (an expansion, a substitution,
|
|
624
|
+
* that is not a plain literal (an expansion, a substitution, ANSI-C quoting
|
|
589
625
|
* `$'…'`, an unquoted expansion where `-c` could hide, or an eval of several
|
|
590
626
|
* words) is `literal: false` and the caller fails closed. Null when the command
|
|
591
627
|
* runs no inline program (a script file, stdin).
|
|
592
628
|
*/
|
|
593
|
-
|
|
629
|
+
/**
|
|
630
|
+
* `text` with every `\'` inside an ANSI-C `$'…'` word written `\x27`, which
|
|
631
|
+
* the shell decodes to the same quote. The shared lexer does not follow a
|
|
632
|
+
* `\'` there (it stops trusting the command), but reads this spelling.
|
|
633
|
+
*/
|
|
634
|
+
export const ansiPlain = (text: string): string =>
|
|
635
|
+
text.replace(/\$'(?:[^'\\]|\\[\s\S])*'/g, (word) => word.replace(/\\[\s\S]/g, (pair) => (pair === "\\'" ? "\\x27" : pair)));
|
|
636
|
+
|
|
637
|
+
export function shellProgram(raw: string[], index: number, decodeName: (raw: string) => string | null = literalWord): ShellProgram | null {
|
|
594
638
|
if (index < 0 || index >= raw.length) return null;
|
|
595
|
-
const
|
|
596
|
-
const name = baseName(nameText ?? "");
|
|
639
|
+
const name = baseName(decodeName(raw[index]) ?? "");
|
|
597
640
|
if (name === "eval") {
|
|
598
641
|
let at = index + 1;
|
|
599
642
|
if (literalWord(raw[at] ?? "") === "--") at++;
|
|
@@ -627,92 +670,217 @@ export function shellProgram(raw: string[], index: number): ShellProgram | null
|
|
|
627
670
|
}
|
|
628
671
|
|
|
629
672
|
/**
|
|
630
|
-
* sed
|
|
631
|
-
*
|
|
632
|
-
*
|
|
633
|
-
*
|
|
634
|
-
*
|
|
635
|
-
*
|
|
673
|
+
* sed (GNU sed 4.9 grammar; BSD's `-i ''`/`-i .bak` suffix word is read too).
|
|
674
|
+
* Its script can execute (`e`, `s///e`) and write files (`w`, `W`, `s///w`,
|
|
675
|
+
* which sed opens, truncating them, before it reads any input). Returns:
|
|
676
|
+
* - `unresolved` when a script cannot be read exactly: an expanded script or
|
|
677
|
+
* option word, a `-f` script file, an option sed does not know, or a script
|
|
678
|
+
* this reader cannot parse (fail closed);
|
|
679
|
+
* - the scripts as shell programs, unresolved, when one executes (the command
|
|
680
|
+
* it runs comes from the input);
|
|
681
|
+
* - otherwise the write targets in `writes` (none for a filter such as
|
|
682
|
+
* `sed -n 1p`), for the caller to judge as write targets.
|
|
683
|
+
* Null when sed neither executes nor writes a file through its script.
|
|
636
684
|
*/
|
|
637
685
|
function sedPrograms(words: string[], index: number, opaque: (at: number) => boolean): ExecutorFinding | null {
|
|
638
|
-
|
|
686
|
+
const readings = sedReadings(words, index, opaque);
|
|
687
|
+
if (readings === null) return { programs: [], unresolved: true };
|
|
688
|
+
const writes = new Set<string>();
|
|
689
|
+
for (const { scripts, mayFail } of readings) {
|
|
690
|
+
const script = sedParseScript(scripts.join("\n"));
|
|
691
|
+
if (script === null) {
|
|
692
|
+
// sed opens each `w` file while it compiles, so a script that fails part
|
|
693
|
+
// way may already have truncated one: fail closed. The one exception is
|
|
694
|
+
// GNU's reading of a BSD `-i` suffix word as the script, which fails at
|
|
695
|
+
// its first character, before anything is opened.
|
|
696
|
+
if (mayFail) continue;
|
|
697
|
+
return { programs: [], unresolved: true };
|
|
698
|
+
}
|
|
699
|
+
if (script.executes) return { programs: scripts.map((text) => ({ text, kind: "shell" as const })), unresolved: true };
|
|
700
|
+
for (const file of script.writes) writes.add(file);
|
|
701
|
+
}
|
|
702
|
+
if (writes.size === 0) return null;
|
|
703
|
+
return { programs: [], unresolved: false, writes: [...writes] };
|
|
704
|
+
}
|
|
705
|
+
|
|
706
|
+
/**
|
|
707
|
+
* GNU sed long options (without `--`), matched exactly or by a unique prefix
|
|
708
|
+
* as getopt_long does. The last two take a value (attached with `=` or the
|
|
709
|
+
* next word); `in-place` takes one only attached; `file` names a script file.
|
|
710
|
+
*/
|
|
711
|
+
const SED_LONG = "quiet silent debug follow-symlinks posix regexp-extended separate sandbox unbuffered null-data zero-terminated binary help version in-place file expression line-length".split(" ");
|
|
712
|
+
|
|
713
|
+
/**
|
|
714
|
+
* The script fragments of a sed command, per reading. GNU getopt permutes, so
|
|
715
|
+
* every `-e`/`--expression` before `--` counts, and without one the first
|
|
716
|
+
* operand is the script. BSD sed reads the word after a lone `-i` as the
|
|
717
|
+
* backup suffix when it is empty or starts with `.` (`-i ''`, `-i .bak`), and
|
|
718
|
+
* after `-I` (`-Ibak` attached); that reading is added (and, for `-I`, which
|
|
719
|
+
* GNU rejects, it is the only one). Null when a script cannot be read: an
|
|
720
|
+
* expanded script or option word, a `-f`/`--file` script file, or an option
|
|
721
|
+
* sed does not know.
|
|
722
|
+
*/
|
|
723
|
+
function sedReadings(words: string[], index: number, opaque: (at: number) => boolean): Array<{ scripts: string[]; mayFail: boolean }> | null {
|
|
639
724
|
const scripts: string[] = [];
|
|
640
|
-
|
|
641
|
-
let
|
|
642
|
-
let
|
|
643
|
-
let
|
|
644
|
-
|
|
725
|
+
const operands: number[] = [];
|
|
726
|
+
let bsd = false;
|
|
727
|
+
let suffixAt = -1;
|
|
728
|
+
let gnuRejects = false;
|
|
729
|
+
let endOfOptions = false;
|
|
730
|
+
for (let at = index + 1; at < words.length; at++) {
|
|
645
731
|
const word = words[at];
|
|
646
|
-
if (
|
|
647
|
-
if (
|
|
648
|
-
if (word === "
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
732
|
+
if (endOfOptions || !/^-./.test(word)) { operands.push(at); continue; }
|
|
733
|
+
if (opaque(at)) return null;
|
|
734
|
+
if (word === "--") { endOfOptions = true; continue; }
|
|
735
|
+
let option: string;
|
|
736
|
+
let value: string | undefined;
|
|
737
|
+
if (word[1] === "-") {
|
|
738
|
+
const eq = word.indexOf("=");
|
|
739
|
+
const flag = (eq < 0 ? word : word.slice(0, eq)).slice(2);
|
|
740
|
+
const names = SED_LONG.filter((name) => name.startsWith(flag));
|
|
741
|
+
const name = SED_LONG.includes(flag) ? flag : names.length === 1 ? names[0] : "";
|
|
742
|
+
if (SED_LONG.indexOf(name) < 15) { if (!name) return null; continue; }
|
|
743
|
+
option = name[0];
|
|
744
|
+
if (eq >= 0) value = word.slice(eq + 1);
|
|
745
|
+
} else {
|
|
746
|
+
let i = 1;
|
|
747
|
+
while ("nrEsuzb".includes(word[i] ?? "_")) i++;
|
|
748
|
+
option = word[i] ?? "";
|
|
749
|
+
if (option === "i" || option === "I") {
|
|
750
|
+
// GNU `-i[SUFFIX]` takes the rest of the word; BSD `-i SUFFIX` and
|
|
751
|
+
// `-I SUFFIX` may take the next word.
|
|
752
|
+
const next = i === word.length - 1 && at + 1 < words.length && !opaque(at + 1) ? words[at + 1] : undefined;
|
|
753
|
+
if (option === "I") {
|
|
754
|
+
gnuRejects = bsd = true;
|
|
755
|
+
if (i === word.length - 1) { if (next === undefined) return null; suffixAt = at + 1; }
|
|
756
|
+
} else if (next === "" || next?.startsWith(".")) {
|
|
757
|
+
bsd = true;
|
|
758
|
+
suffixAt = at + 1;
|
|
759
|
+
}
|
|
760
|
+
continue;
|
|
761
|
+
}
|
|
762
|
+
if (option === "") continue;
|
|
763
|
+
if (!"efl".includes(option)) return null;
|
|
764
|
+
value = word.slice(i + 1) || undefined;
|
|
765
|
+
}
|
|
766
|
+
if (option === "f") return null;
|
|
767
|
+
if (value === undefined) {
|
|
768
|
+
value = words[++at];
|
|
769
|
+
if (value === undefined || opaque(at)) return null;
|
|
770
|
+
}
|
|
771
|
+
if (option === "e") scripts.push(value);
|
|
664
772
|
}
|
|
665
|
-
const
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
return {
|
|
773
|
+
const reading = (skip: number): string[] | null => {
|
|
774
|
+
if (scripts.length > 0) return scripts;
|
|
775
|
+
const first = operands.find((at) => at !== skip);
|
|
776
|
+
return first === undefined ? [] : opaque(first) ? null : [words[first]];
|
|
777
|
+
};
|
|
778
|
+
const readings: Array<{ scripts: string[] | null; mayFail: boolean }> = [];
|
|
779
|
+
// GNU reads a BSD suffix word itself as the script, and fails at once.
|
|
780
|
+
if (!gnuRejects) readings.push({ scripts: reading(-1), mayFail: suffixAt >= 0 && scripts.length === 0 && operands[0] === suffixAt });
|
|
781
|
+
if (bsd) readings.push({ scripts: reading(suffixAt), mayFail: false });
|
|
782
|
+
return readings.some((r) => r.scripts === null) ? null : (readings as Array<{ scripts: string[]; mayFail: boolean }>);
|
|
675
783
|
}
|
|
676
784
|
|
|
677
|
-
/**
|
|
678
|
-
|
|
785
|
+
/**
|
|
786
|
+
* Read a sed script as GNU sed 4.9 tokenizes it (compile.c), for the files it
|
|
787
|
+
* writes and whether it executes. It follows every construct that takes an
|
|
788
|
+
* argument, exactly: regex addresses (`/re/`, `\cREc`, bracket expressions
|
|
789
|
+
* read whole), labels (`: b t T v`, to whitespace or `;`), the text commands
|
|
790
|
+
* (`a i c`, to the end of the line, a backslash escaping the next character),
|
|
791
|
+
* `r R w W e` (to the end of the line), comments, and `s`/`y` with any
|
|
792
|
+
* delimiter, including the flags of `s` (`w FILE` to the end of the line).
|
|
793
|
+
* Every other character is stepped over, so a script sed rejects still yields
|
|
794
|
+
* every `w` file it names: sed opens those while compiling, before any error
|
|
795
|
+
* stops it. `/dev/stdout` and `/dev/stderr` are not files. Null when a
|
|
796
|
+
* delimited argument is not terminated: the script cannot be read.
|
|
797
|
+
*/
|
|
798
|
+
export function sedParseScript(script: string): { writes: string[]; executes: boolean } | null {
|
|
799
|
+
const writes: string[] = [];
|
|
800
|
+
let executes = false;
|
|
801
|
+
let i = 0;
|
|
802
|
+
const n = script.length;
|
|
803
|
+
const c = () => script[i] ?? "";
|
|
804
|
+
const rest = () => {
|
|
805
|
+
while (c() === " " || c() === "\t") i++;
|
|
806
|
+
const start = i;
|
|
807
|
+
const end = script.indexOf("\n", i);
|
|
808
|
+
i = end < 0 ? n : end;
|
|
809
|
+
return script.slice(start, i);
|
|
810
|
+
};
|
|
811
|
+
const file = () => { const name = rest(); if (name && !/^\/dev\/std(?:out|err)$/.test(name)) writes.push(name); };
|
|
812
|
+
// match_slash: up to an unescaped delimiter; a raw newline ends it
|
|
813
|
+
// unterminated. In a regex (an address, the pattern of `s`) a bracket
|
|
814
|
+
// expression is read whole, so the delimiter inside it (`s/[/]/x/`) is
|
|
815
|
+
// literal, as GNU sed 4.9 reads it; a backslash inside a bracket is
|
|
816
|
+
// literal. The replacement of `s` and both parts of `y` are not regexes.
|
|
817
|
+
const delimited = (delim: string, regex: boolean): boolean => {
|
|
818
|
+
for (; i < n; i++) {
|
|
819
|
+
const ch = script[i];
|
|
820
|
+
if (ch === "\n") return false;
|
|
821
|
+
if (ch === "\\") { if (++i >= n) return false; }
|
|
822
|
+
else if (ch === delim) { i++; return true; }
|
|
823
|
+
else if (regex && ch === "[") {
|
|
824
|
+
let j = i + 1;
|
|
825
|
+
if (script[j] === "^") j++;
|
|
826
|
+
if (script[j] === "]") j++;
|
|
827
|
+
for (; script[j] !== "]"; j++) {
|
|
828
|
+
if (j >= n || script[j] === "\n") return false;
|
|
829
|
+
if (script[j] === "[" && ":.=".includes(script[j + 1] || "\n")) {
|
|
830
|
+
const close = script.indexOf(script[j + 1] + "]", j + 2);
|
|
831
|
+
if (close < 0 || script.slice(j, close).includes("\n")) return false;
|
|
832
|
+
j = close + 1;
|
|
833
|
+
}
|
|
834
|
+
}
|
|
835
|
+
i = j;
|
|
836
|
+
}
|
|
837
|
+
}
|
|
838
|
+
return false;
|
|
839
|
+
};
|
|
840
|
+
while (i < n) {
|
|
841
|
+
const cmd = script[i++];
|
|
842
|
+
if (cmd === "/" || cmd === "\\") {
|
|
843
|
+
// A regex address, then its I/M flags (stepped over below).
|
|
844
|
+
const delim = cmd === "\\" ? script[i++] ?? "" : "/";
|
|
845
|
+
if (!delim || delim === "\n" || !delimited(delim, true)) return null;
|
|
846
|
+
} else if (cmd === "#") rest();
|
|
847
|
+
else if (":btTv".includes(cmd)) { while (c() === " " || c() === "\t") i++; while (i < n && !/[\s;]/.test(c())) i++; }
|
|
848
|
+
else if ("aic".includes(cmd)) {
|
|
849
|
+
// GNU sed 4.9 read_text: after `a\` the next character is the text's
|
|
850
|
+
// first character taken literally (a newline there starts the text on
|
|
851
|
+
// the next line); GNU's one-liner `a text` starts at the first non-blank.
|
|
852
|
+
// A backslash escapes the next character, so only an unescaped newline
|
|
853
|
+
// ends the text: `a\\<newline>e cmd` ends at the newline and `e cmd` is
|
|
854
|
+
// a command.
|
|
855
|
+
while (c() === " " || c() === "\t") i++;
|
|
856
|
+
if (c() === "\\") i += 2;
|
|
857
|
+
for (; i < n && script[i] !== "\n"; i++) if (script[i] === "\\") i++;
|
|
858
|
+
} else if (cmd === "e") { executes = true; rest(); }
|
|
859
|
+
else if (cmd === "r" || cmd === "R") rest();
|
|
860
|
+
else if (cmd === "w" || cmd === "W") file();
|
|
861
|
+
else if (cmd === "s" || cmd === "y") {
|
|
862
|
+
const delim = script[i++] ?? "";
|
|
863
|
+
if (!delim || delim === "\n" || delim === "\\" || !delimited(delim, cmd === "s") || !delimited(delim, false)) return null;
|
|
864
|
+
// The flags of `s`; `w` takes the rest of the line.
|
|
865
|
+
for (; cmd === "s" && " \tgpiImMe0123456789w".includes(c() || "_"); i++) {
|
|
866
|
+
if (c() === "e") executes = true;
|
|
867
|
+
if (c() === "w") { i++; file(); break; }
|
|
868
|
+
}
|
|
869
|
+
}
|
|
870
|
+
}
|
|
871
|
+
return { writes, executes };
|
|
872
|
+
}
|
|
679
873
|
|
|
680
|
-
/** Whether a sed script executes: an `e` command, or an `s///` with the `e` flag. */
|
|
874
|
+
/** Whether a sed script executes: an `e` command, or an `s///` with the `e` flag (unparseable: true). */
|
|
681
875
|
export function sedScriptExecutes(script: string): boolean {
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
// `s`, handled next.
|
|
685
|
-
if (new RegExp(`(?:^|[;{\\n])${SED_ADDRESS}e(?:\\s|$|[^A-Za-z])`).test(script)) return true;
|
|
686
|
-
// `s<delim>…<delim>…<delim>` with a flag run containing `e`.
|
|
687
|
-
return sedSubstFlags(script).some((flags) => flags.includes("e"));
|
|
876
|
+
const parsed = sedParseScript(script);
|
|
877
|
+
return parsed === null || parsed.executes;
|
|
688
878
|
}
|
|
689
879
|
|
|
690
|
-
/** Whether a sed script writes a file: a `w`/`W` command, or an `s///` with the `w` flag. */
|
|
880
|
+
/** Whether a sed script writes a file: a `w`/`W` command, or an `s///` with the `w` flag (unparseable: true). */
|
|
691
881
|
export function sedScriptWrites(script: string): boolean {
|
|
692
|
-
|
|
693
|
-
return
|
|
694
|
-
}
|
|
695
|
-
|
|
696
|
-
/** The flag runs of every `s<delim>pat<delim>rep<delim>FLAGS` in a sed script (best effort). */
|
|
697
|
-
function sedSubstFlags(script: string): string[] {
|
|
698
|
-
const flags: string[] = [];
|
|
699
|
-
for (let i = 0; i < script.length; i++) {
|
|
700
|
-
if (script[i] !== "s") continue;
|
|
701
|
-
if (i > 0 && /[A-Za-z0-9_]/.test(script[i - 1])) continue;
|
|
702
|
-
const delim = script[i + 1];
|
|
703
|
-
if (!delim || /[\sA-Za-z0-9\\]/.test(delim)) continue;
|
|
704
|
-
let seen = 0;
|
|
705
|
-
let j = i + 2;
|
|
706
|
-
for (; j < script.length && seen < 2; j++) {
|
|
707
|
-
if (script[j] === "\\") { j++; continue; }
|
|
708
|
-
if (script[j] === delim) seen++;
|
|
709
|
-
}
|
|
710
|
-
if (seen < 2) continue;
|
|
711
|
-
const run = script.slice(j).match(/^[a-zA-Z0-9]*/);
|
|
712
|
-
flags.push(run ? run[0] : "");
|
|
713
|
-
i = j;
|
|
714
|
-
}
|
|
715
|
-
return flags;
|
|
882
|
+
const parsed = sedParseScript(script);
|
|
883
|
+
return parsed === null || parsed.writes.length > 0;
|
|
716
884
|
}
|
|
717
885
|
|
|
718
886
|
/** A command a launcher runs: its words are `words[start..end)` of the launcher's segment. */
|
|
@@ -831,6 +999,10 @@ export function launchedCommands(words: string[], index: number, skip: (at: numb
|
|
|
831
999
|
export function localExecutor(words: string[], index: number, opaque: (at: number) => boolean): ExecutorFinding | null {
|
|
832
1000
|
if (index < 0 || index >= words.length) return null;
|
|
833
1001
|
const name = baseName(words[index]);
|
|
1002
|
+
if (name === "deno") {
|
|
1003
|
+
const evaluated = denoEvalPrograms(words, index, opaque);
|
|
1004
|
+
if (evaluated) return evaluated;
|
|
1005
|
+
}
|
|
834
1006
|
if (Object.hasOwn(INTERP_PROGRAM_FLAGS, name) || VERSIONED_INTERP.test(name)) return interpPrograms(name, words, index, opaque);
|
|
835
1007
|
if (AWK_NAMES.has(name)) return awkProgram(words, index, opaque);
|
|
836
1008
|
if (SSH_NAMES.has(name)) return sshPrograms(words, index, opaque);
|
|
@@ -2,6 +2,7 @@ import { homedir } from "node:os";
|
|
|
2
2
|
import { isAbsolute } from "node:path";
|
|
3
3
|
import { evaluate as evaluateTrash, findTrashBinary, inspectTrashBinary, scanCommand } from "./hook-trash-guard/src/hook";
|
|
4
4
|
import { evaluate as evaluateRepos } from "./hook-workspace-repos-guard/src/hook";
|
|
5
|
+
import { readWithLegacyQuotes } from "./local-executors";
|
|
5
6
|
import { evaluate as evaluateSignedLinks } from "./hook-signed-link-guard/src/hook";
|
|
6
7
|
import type { CodewithHookInput, CodewithHookOutput } from "./codewith-native-common";
|
|
7
8
|
|
|
@@ -58,7 +59,10 @@ export function evaluateComposite(guard: string, inputText: string): string {
|
|
|
58
59
|
// registration of this entry. Its refusal outranks a Trash rewrite, because an
|
|
59
60
|
// allowed rewrite would also run the composite gh read.
|
|
60
61
|
const signedLinks = evaluateSignedLinks(evaluated);
|
|
61
|
-
const
|
|
62
|
+
const command = evaluated.tool_input!.command as string;
|
|
63
|
+
const handsOff = () => scanCommand(command, { home: homedir(), cwd: input.cwd }).handoffHits.length > 0;
|
|
64
|
+
// Read with the earlier double-quote backslash reading too (legacyQuotes).
|
|
65
|
+
const handoff = shellCommand && (handsOff() || !!readWithLegacyQuotes(command, handsOff));
|
|
62
66
|
let trash: CodewithHookOutput = name === "trash-guard" && (!monitor || shellCommand)
|
|
63
67
|
? evaluateTrash(evaluated, { home: homedir(), cwd: input.cwd, findTrash: findTrashBinary, inspectTrash: inspectTrashBinary, execution }) : { continue: true };
|
|
64
68
|
// A Trash rewrite is proven only for Bash. Under Monitor the deletion is
|
package/package.json
CHANGED