Open History to see the last commit
1type CommandWrapper = "env" | "sudo";
2
3const SHELL_PROGRAMS = new Set(["sh", "bash", "zsh", "dash", "ash", "ksh", "fish"]);
4const SHELL_OPTIONS_WITH_VALUE = new Set(["-o", "-O", "--rcfile", "--init-file"]);
5
6function shellCommandArgumentIndex(tokens: ReadonlyArray<string>, start: number): number | null {
7 for (let index = start; index < tokens.length; index += 1) {
8 const option = tokens[index]!;
9 if (option === "--" || !option.startsWith("-")) return null;
10 if (SHELL_OPTIONS_WITH_VALUE.has(option)) {
11 index += 1;
12 continue;
13 }
14 if (option === "--command" || /^-[a-zA-Z]*c[a-zA-Z]*$/.test(option)) return index + 1;
15 }
16 return null;
17}
18
19const COMMAND_WRAPPER_OPTIONS_WITH_VALUE: Record<CommandWrapper, ReadonlySet<string>> = {
20 env: new Set(["-C", "--chdir", "-S", "--split-string", "-u", "--unset"]),
21 sudo: new Set(["-C", "--close-from", "-D", "--chdir", "-g", "--group", "-u", "--user"]),
22};
23
24const COMMAND_WRAPPER_FLAGS: Record<CommandWrapper, ReadonlySet<string>> = {
25 env: new Set(["-0", "--null", "-i", "--ignore-environment", "--debug", "-v"]),
26 sudo: new Set(["-A", "--askpass", "-b", "--background", "-E", "-H", "-i", "-n", "-S"]),
27};
28
29function tokenizeShellCommand(command: string): string[] | null {
30 const input = command.trim();
31 const tokens: string[] = [];
32 let current = "";
33 let quote: '"' | "'" | null = null;
34 let escaping = false;
35 let substitutionDepth = 0;
36 let tokenStarted = false;
37
38 for (let index = 0; index < input.length; index += 1) {
39 const character = input[index]!;
40 if (escaping) {
41 current += character;
42 escaping = false;
43 tokenStarted = true;
44 continue;
45 }
46 if (character === "\\" && quote !== "'") {
47 const nextCharacter = input[index + 1];
48 const isWindowsDrivePath = quote === null && /^[A-Za-z]:/.test(current);
49 if (
50 (quote === '"' || isWindowsDrivePath) &&
51 nextCharacter !== undefined &&
52 nextCharacter !== '"' &&
53 nextCharacter !== "\\" &&
54 nextCharacter !== "$" &&
55 nextCharacter !== "`" &&
56 nextCharacter !== "\n"
57 ) {
58 current += character;
59 tokenStarted = true;
60 continue;
61 }
62 escaping = true;
63 tokenStarted = true;
64 continue;
65 }
66 if (quote !== null) {
67 if (character === quote) {
68 quote = null;
69 } else {
70 current += character;
71 }
72 tokenStarted = true;
73 continue;
74 }
75 if (character === "$" && input[index + 1] === "(") {
76 current += "$(";
77 substitutionDepth += 1;
78 tokenStarted = true;
79 index += 1;
80 continue;
81 }
82 if (character === ")" && substitutionDepth > 0) {
83 current += character;
84 substitutionDepth -= 1;
85 tokenStarted = true;
86 continue;
87 }
88 if (character === '"' || character === "'") {
89 quote = character;
90 tokenStarted = true;
91 continue;
92 }
93 if (/\s/u.test(character)) {
94 if (substitutionDepth > 0) {
95 current += character;
96 tokenStarted = true;
97 continue;
98 }
99 if (tokenStarted) {
100 tokens.push(current);
101 current = "";
102 tokenStarted = false;
103 }
104 continue;
105 }
106 current += character;
107 tokenStarted = true;
108 }
109
110 if (quote !== null || escaping || substitutionDepth > 0) return null;
111 if (tokenStarted) tokens.push(current);
112 return tokens;
113}
114
115export function commandProgramName(command: string, depth = 0): string | null {
116 if (depth >= 8) return null;
117 const tokens = tokenizeShellCommand(command);
118 if (tokens === null) return null;
119 let index = 0;
120 let wrapper: CommandWrapper | null = null;
121
122 while (index < tokens.length) {
123 const token = tokens[index];
124 if (!token) return null;
125 if (/^[A-Za-z_][A-Za-z0-9_]*=/.test(token)) {
126 index += 1;
127 continue;
128 }
129 const tokenProgram = token.split(/[\\/]/).at(-1);
130 if (tokenProgram === "env" || tokenProgram === "sudo") {
131 wrapper = tokenProgram;
132 index += 1;
133 continue;
134 }
135 if (wrapper !== null && token === "--") {
136 wrapper = null;
137 index += 1;
138 continue;
139 }
140 if (wrapper !== null && token.startsWith("-")) {
141 if (wrapper === "env" && (token === "-S" || token === "--split-string")) {
142 const splitCommand = tokens[index + 1];
143 return splitCommand ? commandProgramName(splitCommand, depth + 1) : null;
144 }
145 if (wrapper === "env" && token.startsWith("--split-string=")) {
146 return commandProgramName(token.slice("--split-string=".length), depth + 1);
147 }
148 if (COMMAND_WRAPPER_OPTIONS_WITH_VALUE[wrapper].has(token)) {
149 if (tokens[index + 1] === undefined) return null;
150 index += 2;
151 continue;
152 }
153 if (COMMAND_WRAPPER_FLAGS[wrapper].has(token)) {
154 index += 1;
155 continue;
156 }
157 const equalsIndex = token.indexOf("=");
158 if (token.startsWith("--") && equalsIndex > 2) {
159 if (!COMMAND_WRAPPER_OPTIONS_WITH_VALUE[wrapper].has(token.slice(0, equalsIndex))) {
160 return null;
161 }
162 index += 1;
163 continue;
164 }
165 if (/^-[A-Za-z].+/.test(token) && !token.startsWith("--")) {
166 let consumesNextToken = false;
167 for (const [optionIndex, option] of token.slice(1).split("").entries()) {
168 const shortOption = `-${option}`;
169 if (COMMAND_WRAPPER_OPTIONS_WITH_VALUE[wrapper].has(shortOption)) {
170 consumesNextToken = optionIndex === token.length - 2;
171 break;
172 }
173 if (!COMMAND_WRAPPER_FLAGS[wrapper].has(shortOption)) return null;
174 }
175 if (consumesNextToken && tokens[index + 1] === undefined) return null;
176 index += consumesNextToken ? 2 : 1;
177 continue;
178 }
179 return null;
180 }
181 if (tokenProgram && SHELL_PROGRAMS.has(tokenProgram.replace(/\.exe$/i, "").toLowerCase())) {
182 const scriptIndex = shellCommandArgumentIndex(tokens, index + 1);
183 if (scriptIndex !== null) {
184 const script = tokens[scriptIndex];
185 return script ? commandProgramName(script, depth + 1) : null;
186 }
187 }
188 return tokenProgram || null;
189 }
190
191 return null;
192}