@sagentlab/navarch-runtime 0.1.151 → 0.1.153
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/worktree-guard-hook.cjs +340 -24
- package/package.json +1 -1
|
@@ -269,19 +269,23 @@ function quotedSpanAt(command, index) {
|
|
|
269
269
|
* plain-quoted word in a shell segment whose command word is `jq`, or is `gh`
|
|
270
270
|
* with the word given to `--jq`/`-q`. Unquoted tokens (`find // -x`),
|
|
271
271
|
* quoted bare roots (`ls "//"`) and `//` in other quoted text stay screened.
|
|
272
|
+
* As in isJqProgramText, only a gh command that evaluates jq itself
|
|
273
|
+
* (GH_JQ_COMMAND_RE) qualifies, and a command that could redefine `jq` or
|
|
274
|
+
* `gh` (mayRedefineCommands) or that the scanner cannot follow gets no
|
|
275
|
+
* exemption.
|
|
272
276
|
*/
|
|
273
|
-
function isJqAlternativeOperator(command, start, token) {
|
|
277
|
+
function isJqAlternativeOperator(command, start, token, heredocs) {
|
|
274
278
|
if (token !== "//" && token !== "//=") return false;
|
|
275
279
|
const next = command[start + token.length];
|
|
276
280
|
if (!/\s/.test(command[start - 1] ?? "") || next === undefined || !/\s/.test(next)) return false;
|
|
277
281
|
const span = quotedSpanAt(command, start);
|
|
278
282
|
if (!span) return false;
|
|
279
283
|
const segment = command.slice(span[2], span[0]);
|
|
280
|
-
const
|
|
281
|
-
|
|
282
|
-
return
|
|
283
|
-
|
|
284
|
-
);
|
|
284
|
+
const rest = segment.replace(/^\s*\(?\s*(?:[A-Za-z_]\w*=\S*\s+)*/, "");
|
|
285
|
+
const isJq = /^jq\s/.test(rest) || (GH_JQ_COMMAND_RE.test(rest) && /\s(?:--jq|-q)(?:=|\s+)$/.test(segment));
|
|
286
|
+
if (!isJq) return false;
|
|
287
|
+
const { ok, bodies } = heredocs();
|
|
288
|
+
return ok && !mayRedefineCommands(command, bodies);
|
|
285
289
|
}
|
|
286
290
|
|
|
287
291
|
/**
|
|
@@ -296,11 +300,11 @@ function isJqAlternativeOperator(command, start, token) {
|
|
|
296
300
|
* program is exempt under the separate, narrower rule in
|
|
297
301
|
* isJqAlternativeOperator.
|
|
298
302
|
*/
|
|
299
|
-
function isCommentOrMarkupToken(command, start, token) {
|
|
303
|
+
function isCommentOrMarkupToken(command, start, token, heredocs) {
|
|
300
304
|
const end = start + token.length;
|
|
301
305
|
const next = command[end];
|
|
302
306
|
|
|
303
|
-
if (isJqAlternativeOperator(command, start, token)) return true;
|
|
307
|
+
if (isJqAlternativeOperator(command, start, token, heredocs)) return true;
|
|
304
308
|
|
|
305
309
|
if (command[start - 1] === "<" && next === ">" && /^\/[A-Za-z][\w:.-]*$/.test(token)) {
|
|
306
310
|
return !fs.existsSync(token);
|
|
@@ -629,10 +633,11 @@ function scanHeredocs(command) {
|
|
|
629
633
|
while (command[j] === " " || command[j] === "\t") j++;
|
|
630
634
|
const word = /^[^\s;|&<>()]+/.exec(command.slice(j));
|
|
631
635
|
if (!word) return failed;
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
if (
|
|
636
|
+
// Accept only complete identifier words with at most one matching
|
|
637
|
+
// outer quote pair. Other shell quote forms can change body boundaries.
|
|
638
|
+
const identifier = /^(?:([A-Za-z0-9_]+)|'([A-Za-z0-9_]+)'|"([A-Za-z0-9_]+)")$/.exec(word[0]);
|
|
639
|
+
if (!identifier) return failed;
|
|
640
|
+
const delimiter = identifier[1] ?? identifier[2] ?? identifier[3];
|
|
636
641
|
const lineEnd = command.indexOf("\n", i);
|
|
637
642
|
pending.push({
|
|
638
643
|
delimiter,
|
|
@@ -656,6 +661,9 @@ function scanHeredocs(command) {
|
|
|
656
661
|
const lineEnd = newline === -1 ? n : newline;
|
|
657
662
|
let line = command.slice(i, lineEnd);
|
|
658
663
|
if (doc.stripTabs) line = line.replace(/^\t+/, "");
|
|
664
|
+
// In an unquoted body the shell joins a line that ends in `\` with
|
|
665
|
+
// the next line before it compares the delimiter.
|
|
666
|
+
if (!doc.quoted && /(?:^|[^\\])(?:\\\\)*\\$/.test(command.slice(i, lineEnd))) return failed;
|
|
659
667
|
const lineStart = i;
|
|
660
668
|
i = newline === -1 ? n + 1 : newline + 1;
|
|
661
669
|
if (line === doc.delimiter) {
|
|
@@ -692,6 +700,8 @@ function scanHeredocs(command) {
|
|
|
692
700
|
*/
|
|
693
701
|
function readsAsText(segment, text, isMessage) {
|
|
694
702
|
const word = commandWordOf(segment);
|
|
703
|
+
// A jq module directive reads a file from its `search` path (isJqProgramText).
|
|
704
|
+
if (word === "jq" && /\b(?:import|include)\b/.test(text)) return false;
|
|
695
705
|
if (TEXT_COMMANDS.has(word)) return true;
|
|
696
706
|
if (PROGRAM_COMMANDS.has(word)) return !PROGRAM_SHELL_RE.test(text);
|
|
697
707
|
return MESSAGE_SUBCOMMAND_RE.test(segment.replace(ASSIGNMENTS_RE, "")) && isMessage;
|
|
@@ -700,24 +710,327 @@ function readsAsText(segment, text, isMessage) {
|
|
|
700
710
|
/** Reserved words that may precede a command word (`do jq ...`, `then gh ...`). */
|
|
701
711
|
const SHELL_KEYWORDS_RE = /^\s*(?:(?:do|then|else|elif|if|while|until|time|!|\{)\s+)*/;
|
|
702
712
|
|
|
713
|
+
/**
|
|
714
|
+
* gh commands whose `-q`/`--jq` value is a jq filter that gh evaluates
|
|
715
|
+
* itself. Any other gh command, such as an extension or a `--shell` alias,
|
|
716
|
+
* gets its arguments raw and can run them. gh global flags (`-R`/`--repo`)
|
|
717
|
+
* may come before the command.
|
|
718
|
+
*/
|
|
719
|
+
const GH_JQ_COMMAND_RE = new RegExp(
|
|
720
|
+
"^gh(?:\\s+(?:-R|--repo)(?:=|\\s+)[^\\s;|&<>()'\"`$]+)*\\s+(?:api|" +
|
|
721
|
+
"(?:pr|issue)\\s+(?:list|view|status|checks)|" +
|
|
722
|
+
"(?:run|release|repo|workflow|cache|label|variable|secret|ruleset)\\s+(?:list|view)|" +
|
|
723
|
+
"search\\s+(?:repos|issues|prs|commits|code))\\s",
|
|
724
|
+
);
|
|
725
|
+
|
|
726
|
+
/**
|
|
727
|
+
* Command words that a reader exemption may share a command with. Each one
|
|
728
|
+
* is an external program, or a builtin that cannot define a function or
|
|
729
|
+
* alias, read definitions, or set a variable (`printf` only with a literal
|
|
730
|
+
* format that is not an option and uses only `%s`, `%b` and `%%`, so
|
|
731
|
+
* `printf -v` and zsh math arguments such as `printf %d x=1` refuse). `cd`
|
|
732
|
+
* changes which program runs only when the inherited PATH holds a relative
|
|
733
|
+
* entry, which is outside this check like any other shell snapshot state.
|
|
734
|
+
*/
|
|
735
|
+
// `$name`, `${name}` or a special parameter, without a zsh subscript.
|
|
736
|
+
const PLAIN_PARAMETER_RE = /^(?:[A-Za-z_]\w*|\{[A-Za-z_]\w*\}|[0-9?#@*!$-])(?!\[)/;
|
|
737
|
+
// printf directives that never read an argument as a number.
|
|
738
|
+
const PLAIN_PRINTF_DIRECTIVES_RE = /%(?:%|[-+ #0]*\d*(?:\.\d*)?[sb])/g;
|
|
739
|
+
const MAX_PLAIN_DEPTH = 32;
|
|
740
|
+
|
|
741
|
+
const PLAIN_COMMANDS = new Set([...LITERAL_PATTERN_COMMANDS, ...PROGRAM_COMMANDS, "false", "gh", "git", "rm"]);
|
|
742
|
+
|
|
743
|
+
// Variables and zsh special arrays that change command lookup, aliases or
|
|
744
|
+
// functions (`PATH=.:$PATH`, `path=(. $path)`, `functions[jq]=...`,
|
|
745
|
+
// `BASH_CMDS[jq]=./x`), and loader variables that inject code into any
|
|
746
|
+
// program (`LD_PRELOAD`, `DYLD_INSERT_LIBRARIES`). These count anywhere, so
|
|
747
|
+
// `export PATH` and `echo $PATH` do too. zsh math allows `path = 5`.
|
|
748
|
+
const LOOKUP_VARIABLE_RE =
|
|
749
|
+
/\b(?:PATH|BASH_ALIASES|BASH_CMDS|FPATH|(?:LD|DYLD)_\w+)\b|\b(?:path|fpath|functions|aliases|galiases|saliases|commands|builtins|dis_\w+)(?:\[[^\]]*\])?\s*\+?=/;
|
|
750
|
+
|
|
751
|
+
/**
|
|
752
|
+
* True unless every simple command outside the heredoc bodies is positively
|
|
753
|
+
* recognised as plain words with a PLAIN_COMMANDS command word. Every rule
|
|
754
|
+
* that trusts a reader's command word (`jq`, `cat`, `gh`, `node`) needs this,
|
|
755
|
+
* because an earlier command in the same shell could redefine that word.
|
|
756
|
+
*
|
|
757
|
+
* The parser accepts only this grammar; anything else refuses the exemption:
|
|
758
|
+
* - Commands separated by `;`, `&`, `&&`, `|`, `||`, `|&` or a newline.
|
|
759
|
+
* - A command word of plain characters (`[A-Za-z0-9_.-]`) in PLAIN_COMMANDS.
|
|
760
|
+
* Standalone decimal scalar assignments, numeric `for` lists, the exact
|
|
761
|
+
* `export GH_TOKEN=$(gh auth token)` form and `npm test` also qualify.
|
|
762
|
+
* Leading assignments attached to a command, leading redirects, here-strings,
|
|
763
|
+
* other keywords and compound syntax (`if`, `case`, `{`, `(`, `!`, `time`),
|
|
764
|
+
* precommands, function definitions, unknown command words and quoted,
|
|
765
|
+
* escaped or expanded command words all refuse.
|
|
766
|
+
* - Argument words built from plain characters, `\` escapes, single quotes,
|
|
767
|
+
* double quotes, `$name`, `${name}`, special parameters, and `$(...)` whose
|
|
768
|
+
* content passes the same parser. Backticks, `$'...'`, `${...}` operators,
|
|
769
|
+
* `$[...]`, arithmetic and an unquoted `(` refuse.
|
|
770
|
+
* - Redirects after the command word (`>f`, `2>&1`, `<<'EOF'`), except a
|
|
771
|
+
* here-string or a word directly before `<` or `>` (`{fd}>f`, `a>b`).
|
|
772
|
+
* - Comments without separators, quotes, escapes or expansions.
|
|
773
|
+
* Expansions refuse when a subscript follows (`$a[x]`), and `$(` nesting
|
|
774
|
+
* deeper than MAX_PLAIN_DEPTH refuses. An unquoted heredoc body expands in
|
|
775
|
+
* this shell, so it may hold only `$name`, `${name}` and special
|
|
776
|
+
* parameters. LOOKUP_VARIABLE_RE refuses even a read.
|
|
777
|
+
*
|
|
778
|
+
* This check is syntactic. A function, alias or PATH entry that the user's
|
|
779
|
+
* shell snapshot already defines before the command runs is out of its
|
|
780
|
+
* reach.
|
|
781
|
+
*/
|
|
782
|
+
function mayRedefineCommands(command, bodies) {
|
|
783
|
+
let text = command;
|
|
784
|
+
for (const { start, end, quoted } of bodies) {
|
|
785
|
+
if (!quoted && !bodyExpansionsArePlain(command.slice(start, end))) return true;
|
|
786
|
+
// Blank the closing delimiter line too, so it does not read as a command.
|
|
787
|
+
const lineEnd = command.indexOf("\n", end);
|
|
788
|
+
const stop = lineEnd === -1 ? command.length : lineEnd;
|
|
789
|
+
text = text.slice(0, start) + " ".repeat(stop - start) + text.slice(stop);
|
|
790
|
+
}
|
|
791
|
+
if (LOOKUP_VARIABLE_RE.test(text.replace(/['"\\]/g, ""))) return true;
|
|
792
|
+
const parser = { text, i: 0, depth: 0 };
|
|
793
|
+
return !(parsePlainList(parser, false) && parser.i === text.length);
|
|
794
|
+
}
|
|
795
|
+
|
|
796
|
+
/** True when an unquoted heredoc body expands only plain parameters. */
|
|
797
|
+
function bodyExpansionsArePlain(body) {
|
|
798
|
+
for (let i = 0; i < body.length; i++) {
|
|
799
|
+
const ch = body[i];
|
|
800
|
+
if (ch === "`") return false;
|
|
801
|
+
if (ch === "\\") {
|
|
802
|
+
i++;
|
|
803
|
+
continue;
|
|
804
|
+
}
|
|
805
|
+
if (ch !== "$") continue;
|
|
806
|
+
const plain = PLAIN_PARAMETER_RE.exec(body.slice(i + 1));
|
|
807
|
+
if (plain) i += plain[0].length;
|
|
808
|
+
else if (!/^(?:\s|"|$)/.test(body.slice(i + 1))) return false;
|
|
809
|
+
}
|
|
810
|
+
return true;
|
|
811
|
+
}
|
|
812
|
+
|
|
813
|
+
/** Skips blanks and `\` line continuations. */
|
|
814
|
+
function skipPlainBlanks(p) {
|
|
815
|
+
for (;;) {
|
|
816
|
+
const ch = p.text[p.i];
|
|
817
|
+
if (ch === " " || ch === "\t") p.i++;
|
|
818
|
+
else if (ch === "\\" && p.text[p.i + 1] === "\n") p.i += 2;
|
|
819
|
+
else return;
|
|
820
|
+
}
|
|
821
|
+
}
|
|
822
|
+
|
|
823
|
+
/**
|
|
824
|
+
* Skips a comment that starts at a word boundary, up to its newline. A shell
|
|
825
|
+
* without comment support (zsh without INTERACTIVE_COMMENTS) reads it as
|
|
826
|
+
* words, so it may hold no separator, quote, escape or expansion.
|
|
827
|
+
*/
|
|
828
|
+
function skipPlainComment(p) {
|
|
829
|
+
const comment = /^#[^\n;&|<>()`$\\'"]*/.exec(p.text.slice(p.i))[0];
|
|
830
|
+
p.i += comment.length;
|
|
831
|
+
return p.i === p.text.length || p.text[p.i] === "\n";
|
|
832
|
+
}
|
|
833
|
+
|
|
834
|
+
/**
|
|
835
|
+
* Parses commands up to the end of the text or, in a `$(` substitution, up to
|
|
836
|
+
* and including its `)`, or through `done` in a numeric loop body.
|
|
837
|
+
* Returns false on anything outside the grammar.
|
|
838
|
+
*/
|
|
839
|
+
function parsePlainList(p, inSubstitution, inLoop = false) {
|
|
840
|
+
for (;;) {
|
|
841
|
+
skipPlainBlanks(p);
|
|
842
|
+
const ch = p.text[p.i];
|
|
843
|
+
if (ch === undefined) return !inSubstitution && !inLoop;
|
|
844
|
+
if (inLoop && /^done(?=[\s;&|)]|$)/.test(p.text.slice(p.i))) {
|
|
845
|
+
p.i += 4;
|
|
846
|
+
return true;
|
|
847
|
+
}
|
|
848
|
+
if (ch === ")") {
|
|
849
|
+
if (!inSubstitution || inLoop) return false;
|
|
850
|
+
p.i++;
|
|
851
|
+
return true;
|
|
852
|
+
}
|
|
853
|
+
if (ch === "&" && p.text[p.i + 1] === ">") return false;
|
|
854
|
+
if (/[;&|\n]/.test(ch)) {
|
|
855
|
+
p.i++;
|
|
856
|
+
continue;
|
|
857
|
+
}
|
|
858
|
+
if (!parsePlainCommand(p)) return false;
|
|
859
|
+
}
|
|
860
|
+
}
|
|
861
|
+
|
|
862
|
+
/** Parses a recognised command, a standalone assignment or a numeric loop. */
|
|
863
|
+
function parsePlainCommand(p) {
|
|
864
|
+
if (p.text[p.i] === "#") return skipPlainComment(p);
|
|
865
|
+
const rest = p.text.slice(p.i);
|
|
866
|
+
// These are whole commands, never prefixes to a command whose lookup could
|
|
867
|
+
// change. No indexed, append, expanded or quoted assignment is recognised.
|
|
868
|
+
const assignment = /^[A-Za-z_]\w*=\d+(?=[ \t]*(?:[;\n]|$))/.exec(rest);
|
|
869
|
+
const tokenExport = /^export[ \t]+GH_TOKEN=\$\(gh[ \t]+auth[ \t]+token\)(?=[ \t]*(?:[;\n]|$))/.exec(rest);
|
|
870
|
+
if (assignment || tokenExport) {
|
|
871
|
+
p.i += (assignment || tokenExport)[0].length;
|
|
872
|
+
skipPlainBlanks(p);
|
|
873
|
+
return true;
|
|
874
|
+
}
|
|
875
|
+
// A finite list of decimal literals cannot evaluate shell code. Parse the
|
|
876
|
+
// entire body with the same reader rules; unknown syntax still refuses.
|
|
877
|
+
const loop = /^for[ \t]+([A-Za-z_]\w*)[ \t]+in[ \t]+\d+(?:[ \t]+\d+)*[ \t]*;[ \t]*do[ \t\n]+/.exec(rest);
|
|
878
|
+
if (loop) {
|
|
879
|
+
// A loop binds its variable too. Apply the assignment screen, including
|
|
880
|
+
// zsh special arrays such as `path`, which is tied to PATH.
|
|
881
|
+
if (LOOKUP_VARIABLE_RE.test(`${loop[1]}=`)) return false;
|
|
882
|
+
if (p.depth >= MAX_PLAIN_DEPTH) return false;
|
|
883
|
+
p.i += loop[0].length;
|
|
884
|
+
p.depth++;
|
|
885
|
+
const ok = parsePlainList(p, false, true);
|
|
886
|
+
p.depth--;
|
|
887
|
+
return ok;
|
|
888
|
+
}
|
|
889
|
+
const word = readPlainWord(p);
|
|
890
|
+
if (!word || !word.plain || (!PLAIN_COMMANDS.has(word.value) && word.value !== "npm")) return false;
|
|
891
|
+
const args = [];
|
|
892
|
+
for (;;) {
|
|
893
|
+
skipPlainBlanks(p);
|
|
894
|
+
const ch = p.text[p.i];
|
|
895
|
+
if (ch === undefined || /[;|\n)]/.test(ch) || (ch === "&" && p.text[p.i + 1] !== ">")) break;
|
|
896
|
+
if (ch === "#") {
|
|
897
|
+
if (!skipPlainComment(p)) return false;
|
|
898
|
+
break;
|
|
899
|
+
}
|
|
900
|
+
const rest = p.text.slice(p.i);
|
|
901
|
+
const redirect = /^(?:\d*(?:<<-|<<|<>|<&|>&|>>|>\||<|>)|&>>|&>)/.exec(rest);
|
|
902
|
+
if (redirect) {
|
|
903
|
+
if (rest.startsWith("<<<")) return false;
|
|
904
|
+
p.i += redirect[0].length;
|
|
905
|
+
skipPlainBlanks(p);
|
|
906
|
+
const duplicate = /&$/.test(redirect[0]) && /^(?:\d+|-)(?=[\s;&|)]|$)/.exec(p.text.slice(p.i));
|
|
907
|
+
if (duplicate) p.i += duplicate[0].length;
|
|
908
|
+
else if (!readPlainWord(p)) return false;
|
|
909
|
+
continue;
|
|
910
|
+
}
|
|
911
|
+
const arg = readPlainWord(p);
|
|
912
|
+
if (!arg) return false;
|
|
913
|
+
// `{fd}>file` assigns a variable; refuse every word glued to a redirect.
|
|
914
|
+
if (p.text[p.i] === "<" || p.text[p.i] === ">") return false;
|
|
915
|
+
args.push(arg);
|
|
916
|
+
}
|
|
917
|
+
if (word.value === "npm") return args.length === 1 && args[0].plain && args[0].value === "test";
|
|
918
|
+
if (word.value !== "printf") return true;
|
|
919
|
+
// `printf -v name` assigns a variable, and zsh evaluates numeric arguments
|
|
920
|
+
// as math, which can assign too. Allow only a literal text-only format.
|
|
921
|
+
const format = args[0];
|
|
922
|
+
if (!format || !format.literal || format.value.startsWith("-")) return false;
|
|
923
|
+
return !format.value.replace(PLAIN_PRINTF_DIRECTIVES_RE, "").includes("%");
|
|
924
|
+
return true;
|
|
925
|
+
}
|
|
926
|
+
|
|
927
|
+
/**
|
|
928
|
+
* Reads one shell word. Returns {value, plain, literal}, or null when the word
|
|
929
|
+
* is empty or uses syntax outside the grammar. `plain` means only
|
|
930
|
+
* `[A-Za-z0-9_.-]` characters; `literal` means no expansion, so `value` is the
|
|
931
|
+
* word after quote removal.
|
|
932
|
+
*/
|
|
933
|
+
function readPlainWord(p) {
|
|
934
|
+
const start = p.i;
|
|
935
|
+
let value = "";
|
|
936
|
+
let plain = true;
|
|
937
|
+
let literal = true;
|
|
938
|
+
for (;;) {
|
|
939
|
+
const ch = p.text[p.i];
|
|
940
|
+
if (ch === undefined || /[\s;&|<>()]/.test(ch)) break;
|
|
941
|
+
if (ch === "`") return null;
|
|
942
|
+
plain = plain && /[A-Za-z0-9_.-]/.test(ch);
|
|
943
|
+
if (ch === "\\") {
|
|
944
|
+
const next = p.text[p.i + 1];
|
|
945
|
+
if (next === undefined) return null;
|
|
946
|
+
if (next !== "\n") value += next;
|
|
947
|
+
p.i += 2;
|
|
948
|
+
} else if (ch === "'") {
|
|
949
|
+
const close = p.text.indexOf("'", p.i + 1);
|
|
950
|
+
if (close === -1) return null;
|
|
951
|
+
value += p.text.slice(p.i + 1, close);
|
|
952
|
+
p.i = close + 1;
|
|
953
|
+
} else if (ch === '"') {
|
|
954
|
+
p.i++;
|
|
955
|
+
for (;;) {
|
|
956
|
+
const inner = p.text[p.i];
|
|
957
|
+
if (inner === undefined || inner === "`") return null;
|
|
958
|
+
if (inner === '"') {
|
|
959
|
+
p.i++;
|
|
960
|
+
break;
|
|
961
|
+
}
|
|
962
|
+
if (inner === "\\") {
|
|
963
|
+
value += p.text[p.i + 1] ?? "";
|
|
964
|
+
p.i += 2;
|
|
965
|
+
} else if (inner === "$") {
|
|
966
|
+
if (!readPlainExpansion(p)) return null;
|
|
967
|
+
literal = false;
|
|
968
|
+
} else {
|
|
969
|
+
value += inner;
|
|
970
|
+
p.i++;
|
|
971
|
+
}
|
|
972
|
+
}
|
|
973
|
+
} else if (ch === "$") {
|
|
974
|
+
if (!readPlainExpansion(p)) return null;
|
|
975
|
+
literal = false;
|
|
976
|
+
} else {
|
|
977
|
+
value += ch;
|
|
978
|
+
p.i++;
|
|
979
|
+
}
|
|
980
|
+
}
|
|
981
|
+
return p.i === start ? null : { value, plain, literal };
|
|
982
|
+
}
|
|
983
|
+
|
|
984
|
+
/**
|
|
985
|
+
* Reads a `$` expansion: `$name`, `${name}`, a special parameter, or `$(...)`
|
|
986
|
+
* whose content parses as a plain list. A `$` before a blank, a closing
|
|
987
|
+
* double quote or the end stays literal. Anything else returns false.
|
|
988
|
+
*/
|
|
989
|
+
function readPlainExpansion(p) {
|
|
990
|
+
const rest = p.text.slice(p.i + 1);
|
|
991
|
+
if (rest.startsWith("(")) {
|
|
992
|
+
if (rest.startsWith("((") || p.depth >= MAX_PLAIN_DEPTH) return false;
|
|
993
|
+
p.i += 2;
|
|
994
|
+
p.depth++;
|
|
995
|
+
const ok = parsePlainList(p, true);
|
|
996
|
+
p.depth--;
|
|
997
|
+
return ok;
|
|
998
|
+
}
|
|
999
|
+
// zsh evaluates `$a[expr]` as a subscript, which can assign, so refuse `[`.
|
|
1000
|
+
const plain = PLAIN_PARAMETER_RE.exec(rest);
|
|
1001
|
+
if (plain) {
|
|
1002
|
+
p.i += 1 + plain[0].length;
|
|
1003
|
+
return true;
|
|
1004
|
+
}
|
|
1005
|
+
if (!/^(?:\s|"|$)/.test(rest)) return false;
|
|
1006
|
+
p.i++;
|
|
1007
|
+
return true;
|
|
1008
|
+
}
|
|
1009
|
+
|
|
703
1010
|
/**
|
|
704
1011
|
* True when bare root text sits inside a single-quoted jq program: a filter
|
|
705
|
-
* argument of `jq`, or the `--jq`/`-q` value of
|
|
706
|
-
*
|
|
707
|
-
*
|
|
708
|
-
*
|
|
709
|
-
*
|
|
710
|
-
*
|
|
711
|
-
*
|
|
712
|
-
*
|
|
1012
|
+
* argument of `jq`, or the `--jq`/`-q` value of a gh command that evaluates
|
|
1013
|
+
* jq itself (GH_JQ_COMMAND_RE: `gh api x --jq '.a // "none"'`,
|
|
1014
|
+
* `gh pr view 1 -q '.url|split("/")|last'`). The shell passes single-quoted
|
|
1015
|
+
* text through unchanged, and a jq filter cannot start a process, so `/`,
|
|
1016
|
+
* `//` or `/*` there is jq syntax. A filter can read files through
|
|
1017
|
+
* `import`/`include` (with a `search` path or `-L`), so a program that names
|
|
1018
|
+
* either gets no exemption. The command word must be a bare `jq` or `gh` (not
|
|
1019
|
+
* `./jq`), and the word must hold more than the token, so `jq . '/'` stays
|
|
1020
|
+
* screened. Only bare root text qualifies: every unquoted path argument stays
|
|
1021
|
+
* a candidate. The caller refuses this exemption when the command could
|
|
1022
|
+
* redefine `jq` or `gh` (mayRedefineCommands).
|
|
713
1023
|
*/
|
|
714
1024
|
function isJqProgramText(command, start, token, singleQuoted, segments) {
|
|
715
1025
|
const span = singleQuoted.find(({ open, close }) => start > open && start < close);
|
|
716
1026
|
if (!span) return false;
|
|
717
|
-
|
|
1027
|
+
const program = command.slice(span.open + 1, span.close);
|
|
1028
|
+
if (program.trim() === token || /\b(?:import|include)\b/.test(program)) return false;
|
|
718
1029
|
const prefix = command.slice(span.segmentStart, span.open).replace(SHELL_KEYWORDS_RE, "");
|
|
719
1030
|
const rest = prefix.replace(ASSIGNMENTS_RE, "");
|
|
720
|
-
const isJq =
|
|
1031
|
+
const isJq =
|
|
1032
|
+
rest === prefix.trimStart() &&
|
|
1033
|
+
(/^jq\s/.test(rest) || (GH_JQ_COMMAND_RE.test(rest) && /\s(?:--jq|-q)(?:=|\s+)$/.test(prefix)));
|
|
721
1034
|
return isJq && outputStaysText(command, span.segmentStart, segments, singleQuoted);
|
|
722
1035
|
}
|
|
723
1036
|
|
|
@@ -880,7 +1193,9 @@ function isProgramFileBody(command, body, segments) {
|
|
|
880
1193
|
* command cannot run that text (exemptionContextIsSafe), except for a
|
|
881
1194
|
* single-quoted jq program whose output stays text (isJqProgramText), and a
|
|
882
1195
|
* quoted heredoc body that reaches only its reader (isSealedBody) or runs only
|
|
883
|
-
* as a program file (isProgramFileBody).
|
|
1196
|
+
* as a program file (isProgramFileBody). Every rule trusts the reader's
|
|
1197
|
+
* command word (`jq`, `cat`, `gh`, `node`), so none applies when the command
|
|
1198
|
+
* could redefine it (mayRedefineCommands). The two heredoc rules look for hook
|
|
884
1199
|
* locations (HOOK_PATH_RE) only outside the bodies, so body prose that
|
|
885
1200
|
* mentions `hooks` stays exempt. The jq rule needs no hook check, because jq
|
|
886
1201
|
* output never reaches a hook file when it reaches no other command.
|
|
@@ -889,6 +1204,7 @@ function isProgramTextRootToken(command, start, token, heredocs) {
|
|
|
889
1204
|
if (!isBareRootText(token)) return false;
|
|
890
1205
|
const { ok, bodies, segments, singleQuoted } = heredocs();
|
|
891
1206
|
if (!ok) return false;
|
|
1207
|
+
if (mayRedefineCommands(command, bodies)) return false;
|
|
892
1208
|
if (isJqProgramText(command, start, token, singleQuoted, segments)) return true;
|
|
893
1209
|
const body = bodies.find((doc) => start >= doc.start && start < doc.end);
|
|
894
1210
|
const outside = body && body.quoted ? textOutsideBodies(command, bodies) : "";
|
|
@@ -940,7 +1256,7 @@ function extractBashPathCandidates(command) {
|
|
|
940
1256
|
let match;
|
|
941
1257
|
while ((match = absoluteRe.exec(command))) {
|
|
942
1258
|
const start = match.index + match[0].length - match[1].length;
|
|
943
|
-
if (isCommentOrMarkupToken(command, start, match[1])) continue;
|
|
1259
|
+
if (isCommentOrMarkupToken(command, start, match[1], getHeredocs)) continue;
|
|
944
1260
|
const token = clean(match[1]);
|
|
945
1261
|
// Quotes keep `$`, `(` and `{` literal only for a command that does not
|
|
946
1262
|
// hand the word to a shell, such as `sed '/^$/d'`; `sh -c '... /$D'` expands.
|
package/package.json
CHANGED