@wonderwhy-er/desktop-commander 0.3.2-alpha.1 → 0.3.2-alpha.3
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/dist/bootstrap.js +7 -7
- package/dist/command-manager.d.ts +98 -1
- package/dist/command-manager.js +200 -16
- package/dist/remote-device/desktop-commander-integration.js +3 -1
- package/dist/remote-device/diagnostics/redact.js +30 -8
- package/dist/remote-device/diagnostics/report.js +30 -13
- package/dist/remote-device/mqtt/mqtt-enrollment.js +1 -1
- package/dist/remote-device/mqtt/mqtt-transport.js +2 -2
- package/dist/remote-device/remote-channel.d.ts +1 -1
- package/dist/remote-device/remote-channel.js +1 -1
- package/dist/remote-device/transport-telemetry.js +5 -4
- package/dist/server.js +2 -0
- package/dist/terminal-manager.js +4 -6
- package/dist/tools/improved-process-tools.d.ts +2 -0
- package/dist/tools/improved-process-tools.js +68 -40
- package/dist/tools/pdf/markdown.js +7 -3
- package/dist/tools/process.js +10 -2
- package/dist/track-installation.js +2 -2
- package/dist/ui/file-preview/preview-runtime.js +145 -138
- package/dist/utils/brace-expansion.d.ts +25 -0
- package/dist/utils/brace-expansion.js +171 -0
- package/dist/utils/capture.d.ts +8 -2
- package/dist/utils/capture.js +1 -1
- package/dist/utils/cmd-commands.d.ts +3 -0
- package/dist/utils/cmd-commands.js +322 -0
- package/dist/utils/command-reading.d.ts +78 -0
- package/dist/utils/command-reading.js +7 -0
- package/dist/utils/command-wrappers.d.ts +45 -0
- package/dist/utils/command-wrappers.js +941 -0
- package/dist/utils/inline-shells.d.ts +64 -0
- package/dist/utils/inline-shells.js +290 -0
- package/dist/utils/posix-commands.d.ts +3 -0
- package/dist/utils/posix-commands.js +201 -0
- package/dist/utils/powershell-commands.d.ts +60 -0
- package/dist/utils/powershell-commands.js +420 -0
- package/dist/utils/process-tree.js +4 -4
- package/dist/utils/shell-script-files.d.ts +4 -0
- package/dist/utils/shell-script-files.js +121 -0
- package/dist/utils/shell.d.ts +36 -0
- package/dist/utils/shell.js +69 -0
- package/dist/utils/system-info.d.ts +2 -0
- package/dist/utils/system-info.js +5 -1
- package/dist/utils/threadpool-size.d.ts +36 -0
- package/dist/utils/threadpool-size.js +143 -0
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +11 -9
- package/node_modules/@desktop-commander/telemetry-contract/README.md +0 -56
- package/node_modules/@desktop-commander/telemetry-contract/package.json +0 -32
- package/node_modules/@desktop-commander/telemetry-contract/transport.d.ts +0 -39
- package/node_modules/@desktop-commander/telemetry-contract/transport.js +0 -167
- package/vendor/telemetry-contract/README.md +0 -56
- package/vendor/telemetry-contract/package.json +0 -32
- package/vendor/telemetry-contract/transport.d.ts +0 -38
- package/vendor/telemetry-contract/transport.js +0 -164
- package/vendor/telemetry-contract/transport.test.js +0 -39
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { type ShellType } from './shell.js';
|
|
2
|
+
import type { FoundCommand } from './command-reading.js';
|
|
3
|
+
/**
|
|
4
|
+
* What a shell started by a command runs, by the shell's own rules for its
|
|
5
|
+
* arguments: a command text (bash -c 'sudo ls', pwsh -Command 'net user',
|
|
6
|
+
* powershell -EncodedCommand <base64>, cmd /c net user), a script file
|
|
7
|
+
* (bash deploy.sh, pwsh -File deploy.ps1), or its input (bash -s). The
|
|
8
|
+
* shell around it sees a command text only as an argument, so
|
|
9
|
+
* checkCommand() checks the text like any command, read for the shell that
|
|
10
|
+
* runs it. A script file's commands aren't in the text, so the check notes it.
|
|
11
|
+
* eval and Invoke-Expression run a command text too, in the same shell
|
|
12
|
+
* (eval 'sudo ls', Invoke-Expression 'net user'), and their texts follow the
|
|
13
|
+
* same rules: see sameShellTexts(). So do the programs that run one command
|
|
14
|
+
* text in a shell (su -c 'sudo ls', watch 'sudo ls': see TEXT_PROGRAMS in
|
|
15
|
+
* command-wrappers.ts).
|
|
16
|
+
*
|
|
17
|
+
* The rules are which option gives the command text or the file, and which
|
|
18
|
+
* options take a value. They work on the words a reader found, so they read
|
|
19
|
+
* no shell grammar of their own. A POSIX shell's options are read by the
|
|
20
|
+
* wrappers' reader, from its rules there (see command-wrappers.ts), in every
|
|
21
|
+
* way they can be read. A word known only at run time is used as its
|
|
22
|
+
* text ("cd $HOME && sudo ls"), so the programs written in it are still seen.
|
|
23
|
+
* The shell that starts the shell fills that word in before the started shell
|
|
24
|
+
* reads it, so it may hold any command (pwsh -Command $cmd, bash -c "ls $X"):
|
|
25
|
+
* such a command text notes the shell too (file null), as a script file named
|
|
26
|
+
* only at run time does.
|
|
27
|
+
*/
|
|
28
|
+
/** What a shell runs, or eval or Invoke-Expression in the shell they are in */
|
|
29
|
+
export interface ShellRun {
|
|
30
|
+
/** The command texts it is given, each to be read by the shell that runs it */
|
|
31
|
+
texts: string[];
|
|
32
|
+
/**
|
|
33
|
+
* The script file it is given, undefined when it is given none; null when the file, or a part of a command
|
|
34
|
+
* text it is given, is known only at run time, or when its options can be read as naming different files
|
|
35
|
+
*/
|
|
36
|
+
file?: string | null;
|
|
37
|
+
/** The shell that reads its texts, when it isn't the program itself: sh for su -c's, cmd for runas's command line */
|
|
38
|
+
shell?: string;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* What eval (in a POSIX shell) or Invoke-Expression (in PowerShell, also
|
|
42
|
+
* written iex) runs in the shell it is in: its command texts. eval joins its
|
|
43
|
+
* words with spaces. Invoke-Expression runs one text, its Command; PowerShell
|
|
44
|
+
* binds one of the words that aren't parameter names to it, and the others
|
|
45
|
+
* may be values of other parameters (iex -ea 0 'net user'), so each of them
|
|
46
|
+
* is a text. Written with a colon, the parameter and its value are one word
|
|
47
|
+
* (iex -Command:'net user'), which runs what iex given that value runs. A
|
|
48
|
+
* text with a part known only at run time is read as written,
|
|
49
|
+
* as the text of bash -c is, so the programs written in it are checked
|
|
50
|
+
* (eval "cd $HOME && sudo ls"). That part may hold any command, so file is
|
|
51
|
+
* null then, and the command is noted; so is a text that comes from the
|
|
52
|
+
* pipeline ('net user' | iex). gsudo, in any shell, runs its command in the
|
|
53
|
+
* shell it was started from too (gsudo 'net user'), and gsudo !! runs the
|
|
54
|
+
* last command again, which the check can't see, so it is noted. No texts
|
|
55
|
+
* for any other command.
|
|
56
|
+
*/
|
|
57
|
+
export declare function sameShellTexts(found: FoundCommand, shellType: ShellType): ShellRun;
|
|
58
|
+
/**
|
|
59
|
+
* What the shell `found` starts runs, or the program that runs one command
|
|
60
|
+
* text in a shell (su -c 'sudo ls'): nothing for any other command. Its kind
|
|
61
|
+
* of shell is taken from its name as found, as checkCommand() takes the
|
|
62
|
+
* reader for a text from the shell it is given.
|
|
63
|
+
*/
|
|
64
|
+
export declare function shellRuns(found: FoundCommand): ShellRun;
|
|
@@ -0,0 +1,290 @@
|
|
|
1
|
+
import { getShellType, POSIX_SHELLS, programName } from './shell.js';
|
|
2
|
+
import { sameShellProgramReadings, shellOptionReadings, textProgramReadings } from './command-wrappers.js';
|
|
3
|
+
/**
|
|
4
|
+
* PowerShell's parameters that take the next word as their value, whatever
|
|
5
|
+
* it is (-wd /home/me). A parameter may be shortened to any start of its
|
|
6
|
+
* full name (-ex for -ExecutionPolicy); ep, wd, if, of and ea are short names
|
|
7
|
+
* of their own.
|
|
8
|
+
*/
|
|
9
|
+
const POWERSHELL_VALUE_PARAMETERS = [
|
|
10
|
+
'executionpolicy', 'ep', 'windowstyle', 'workingdirectory', 'wd', 'configurationname', 'configurationfile',
|
|
11
|
+
'custompipename', 'inputformat', 'if', 'outputformat', 'of', 'settingsfile', 'version', 'psconsolefile',
|
|
12
|
+
'encodedarguments', 'ea',
|
|
13
|
+
];
|
|
14
|
+
/** PowerShell's switches, in pwsh and in Windows PowerShell */
|
|
15
|
+
const POWERSHELL_SWITCHES = [
|
|
16
|
+
'help', 'login', 'noexit', 'nologo', 'noninteractive', 'noprofile', 'noprofileloadtime', 'interactive', 'sta', 'mta',
|
|
17
|
+
'servermode', 'socketservermode', 'v2socketservermode', 'namedpipeservermode', 'sshservermode',
|
|
18
|
+
'removeworkingdirectorytrailingcharacter',
|
|
19
|
+
];
|
|
20
|
+
/** Every PowerShell parameter: those above, and those that give the command text or the script file */
|
|
21
|
+
const POWERSHELL_PARAMETERS = [
|
|
22
|
+
...POWERSHELL_VALUE_PARAMETERS, ...POWERSHELL_SWITCHES, 'command', 'commandwithargs', 'cwa', 'encodedcommand', 'ec', 'file',
|
|
23
|
+
];
|
|
24
|
+
/**
|
|
25
|
+
* The name a word gives a PowerShell parameter, written -Name, --Name or /Name, in lowercase. PowerShell takes an
|
|
26
|
+
* en dash, an em dash or a horizontal bar (–, —, ―) for - too, and a second dash only if it is the same
|
|
27
|
+
*/
|
|
28
|
+
const parameterName = (word) => /^(?:([-\u2013\u2014\u2015])\1?|\/)([a-z]+)$/i.exec(word)?.[2].toLowerCase();
|
|
29
|
+
/** Whether a word is written as a PowerShell parameter is, with the start of a parameter's name (/c, not /tmp) */
|
|
30
|
+
function isPowerShellParameter(word) {
|
|
31
|
+
const name = word === null ? undefined : parameterName(word);
|
|
32
|
+
return name !== undefined && POWERSHELL_PARAMETERS.some((known) => known.startsWith(name));
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* What a POSIX shell runs, by its options as the wrappers' reader reads them
|
|
36
|
+
* (shellOptionReadings()): the command text, which is the first word after
|
|
37
|
+
* bash's options with -c (bash -lc make, bash -c -e 'sudo ls') or the value
|
|
38
|
+
* of fish's -c, --command or -C (fish -C : -c 'sudo ls'), or else the script
|
|
39
|
+
* file, the first word after the options (bash -o pipefail -x deploy.sh).
|
|
40
|
+
* With bash -s or fish -c, the words after the options are the command's
|
|
41
|
+
* arguments. Where the options can be read more than one way, every reading
|
|
42
|
+
* counts. bash's --rcfile or --init-file names a file it runs too.
|
|
43
|
+
*/
|
|
44
|
+
function posixShellRuns(found, program) {
|
|
45
|
+
return readingsRun(shellOptionReadings(program, found.words), found, { script: true });
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* What a program that runs one command text in a shell runs (su -c 'sudo
|
|
49
|
+
* ls', watch 'sudo ls': see TEXT_PROGRAMS in command-wrappers.ts), and the
|
|
50
|
+
* shell that reads it. The words after its options are no script file: a
|
|
51
|
+
* user, a file or a group.
|
|
52
|
+
*/
|
|
53
|
+
function textProgramRuns(found, program) {
|
|
54
|
+
const text = textProgramReadings(program, found.words);
|
|
55
|
+
if (!text)
|
|
56
|
+
return { texts: [] };
|
|
57
|
+
const run = { ...readingsRun(text.readings, found, { script: false }), shell: text.shell };
|
|
58
|
+
// An explicit command text consumes the shell's later words as arguments, not another script.
|
|
59
|
+
if (run.texts.length > 0 || !text.arguments?.length)
|
|
60
|
+
return run;
|
|
61
|
+
const at = text.arguments;
|
|
62
|
+
const shellWords = {
|
|
63
|
+
...found, name: text.shell,
|
|
64
|
+
words: [text.shell, ...at.map(i => found.words[i])],
|
|
65
|
+
written: [text.shell, ...at.map(i => found.written[i])],
|
|
66
|
+
wordTexts: [text.shell, ...at.map(i => found.wordTexts[i])],
|
|
67
|
+
};
|
|
68
|
+
const inner = posixShellRuns(shellWords, text.shell);
|
|
69
|
+
return {
|
|
70
|
+
...run, texts: [...new Set([...run.texts, ...inner.texts])],
|
|
71
|
+
...(run.file === null || inner.file !== undefined ? { file: null } : {}),
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* What the readings of a command's options give it to run: its command texts,
|
|
76
|
+
* each once also when more than one reading finds it, and one known only at
|
|
77
|
+
* run time as written; and for a shell (`script`), its script file. A part
|
|
78
|
+
* known only at run time, in a text or the word after the options, may hold
|
|
79
|
+
* any command, and so may a script file the readings don't agree on, and
|
|
80
|
+
* texts that aren't among its words at all (parallel :::: jobs.txt): the
|
|
81
|
+
* command is noted then (file null).
|
|
82
|
+
*/
|
|
83
|
+
function readingsRun({ operands, texts, hidden }, { words, wordTexts }, { script }) {
|
|
84
|
+
const textOf = ({ text, at, to }) => {
|
|
85
|
+
if (at === undefined)
|
|
86
|
+
return text ?? '';
|
|
87
|
+
if (to !== undefined)
|
|
88
|
+
return wordTexts.slice(at, to).join(' ');
|
|
89
|
+
return wordTexts[at];
|
|
90
|
+
};
|
|
91
|
+
const after = [...new Set(operands.filter((at) => at < words.length).map((at) => words[at]))];
|
|
92
|
+
const atRunTime = hidden || texts.some(({ text, at }) => at !== undefined && text === null) || after.includes(null)
|
|
93
|
+
|| (script && after.length > 1);
|
|
94
|
+
const file = script && after.length === 1 ? { file: after[0] } : {};
|
|
95
|
+
return { texts: [...new Set(texts.map(textOf))], ...(atRunTime ? { file: null } : file) };
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* What a PowerShell runs: the words after -Command (-c) joined with spaces,
|
|
99
|
+
* the base64 UTF-16LE text after -EncodedCommand (-e, -enc, -ec), or the
|
|
100
|
+
* script file after -File (-f). Without these, its first word that isn't a
|
|
101
|
+
* parameter is a script file in PowerShell 7; Windows PowerShell runs it, and
|
|
102
|
+
* the words after it, as a command, or as a script file when it is a .ps1.
|
|
103
|
+
* Parameters start with -, -- or / (pwsh takes –, — and ― for -, and Windows
|
|
104
|
+
* PowerShell, whose parser isn't open source, may too, so there such a word
|
|
105
|
+
* is read as no parameter as well), and a parameter's value is the next word,
|
|
106
|
+
* whatever it is (-wd /home/me). A word known only at run time where a
|
|
107
|
+
* parameter can be may be any of them: a switch, a parameter with a value,
|
|
108
|
+
* -Command or -File. Where the check can't tell which word is a value,
|
|
109
|
+
* every reading counts.
|
|
110
|
+
*/
|
|
111
|
+
function powerShellRuns({ words, wordTexts }, program) {
|
|
112
|
+
const texts = [];
|
|
113
|
+
// The script files the readings name, and null for a part known only at run time, which may hold any command
|
|
114
|
+
const files = new Set();
|
|
115
|
+
const read = new Set();
|
|
116
|
+
// Adds the command texts made of the words `parts`
|
|
117
|
+
const addTexts = (found, parts) => {
|
|
118
|
+
texts.push(...found);
|
|
119
|
+
if (parts.includes(null))
|
|
120
|
+
files.add(null);
|
|
121
|
+
};
|
|
122
|
+
// Reads the words from the `from`th on, as PowerShell does
|
|
123
|
+
const readFrom = (from) => {
|
|
124
|
+
if (read.has(from))
|
|
125
|
+
return;
|
|
126
|
+
read.add(from);
|
|
127
|
+
for (let i = from; i < words.length; i++) {
|
|
128
|
+
const word = words[i];
|
|
129
|
+
if (word === null) {
|
|
130
|
+
// Such a word may be any parameter, the command or the script file, so the shell is noted. As -Command, the
|
|
131
|
+
// words after it are the command text (pwsh $mode net user); as a parameter with a value, the next word is that
|
|
132
|
+
files.add(null);
|
|
133
|
+
if (i + 1 < words.length)
|
|
134
|
+
texts.push(wordTexts.slice(i + 1).join(' '));
|
|
135
|
+
readFrom(i + 2);
|
|
136
|
+
continue;
|
|
137
|
+
}
|
|
138
|
+
const parameter = parameterName(word);
|
|
139
|
+
if (parameter === undefined || (program !== 'pwsh' && /^[\u2013\u2014\u2015]/.test(word))) {
|
|
140
|
+
if (program === 'pwsh' || /\.ps1$/i.test(word))
|
|
141
|
+
files.add(word);
|
|
142
|
+
else
|
|
143
|
+
addTexts([wordTexts.slice(i).join(' ')], words.slice(i));
|
|
144
|
+
if (parameter === undefined)
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
if (parameter === 'ec' || 'encodedcommand'.startsWith(parameter)) {
|
|
148
|
+
const encoded = words[i + 1];
|
|
149
|
+
return addTexts(encoded ? [Buffer.from(encoded, 'base64').toString('utf16le')] : [], [encoded]);
|
|
150
|
+
}
|
|
151
|
+
if ('command'.startsWith(parameter)) {
|
|
152
|
+
// -Command - reads the command from standard input
|
|
153
|
+
const text = words[i + 1] === '-' ? '' : wordTexts.slice(i + 1).join(' ');
|
|
154
|
+
return addTexts(text ? [text] : [], words.slice(i + 1));
|
|
155
|
+
}
|
|
156
|
+
// -CommandWithArgs: the next word is the command, and the words after it are its $args
|
|
157
|
+
if (parameter === 'cwa' || parameter === 'commandwithargs')
|
|
158
|
+
return addTexts(i + 1 < words.length ? [wordTexts[i + 1]] : [], [words[i + 1]]);
|
|
159
|
+
if ('file'.startsWith(parameter)) {
|
|
160
|
+
if (i + 1 < words.length && words[i + 1] !== '-')
|
|
161
|
+
files.add(words[i + 1]);
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
// A parameter's value, also one known only at run time. When the next word is written as a parameter is (-wd /c:
|
|
165
|
+
// Git Bash's C: drive, and -Command's short form), or the parameter may be a switch (pwsh's -i is a switch,
|
|
166
|
+
// Windows PowerShell's -i takes a value), it is read as the next parameter too
|
|
167
|
+
if (POWERSHELL_VALUE_PARAMETERS.some((name) => name.startsWith(parameter)) && i + 1 < words.length) {
|
|
168
|
+
if (isPowerShellParameter(words[i + 1]) || POWERSHELL_SWITCHES.some((name) => name.startsWith(parameter)))
|
|
169
|
+
readFrom(i + 1);
|
|
170
|
+
i++;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
};
|
|
174
|
+
readFrom(1);
|
|
175
|
+
// A script file the readings don't agree on may hold any command too
|
|
176
|
+
const file = [...files];
|
|
177
|
+
return { texts: [...new Set(texts)], ...(file.length === 0 ? {} : { file: file.length === 1 ? file[0] : null }) };
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* cmd's rule for quotes around the command after /c: when it starts with ",
|
|
181
|
+
* cmd drops that quote and the last one. It keeps both when there are only
|
|
182
|
+
* those two, with whitespace and no special characters between them, and
|
|
183
|
+
* they enclose a program's path (cmd /c "C:\Program Files\x\tool.exe" -v).
|
|
184
|
+
* Whether that file exists depends on where the command runs, so both ways
|
|
185
|
+
* of reading the command are returned then.
|
|
186
|
+
*/
|
|
187
|
+
function cmdQuoteReadings(line, alwaysDropsQuotes) {
|
|
188
|
+
if (!line.startsWith('"'))
|
|
189
|
+
return [line];
|
|
190
|
+
const last = line.lastIndexOf('"');
|
|
191
|
+
const dropped = line.slice(1, last) + line.slice(last + 1);
|
|
192
|
+
const keeps = !alwaysDropsQuotes && (line.match(/"/g) ?? []).length === 2 && /^"[^"&<>()@^|]*\s[^"&<>()@^|]*"/.test(line);
|
|
193
|
+
return keeps ? [line, dropped] : [dropped];
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* What cmd runs: what follows /c, /k or /r, after other switches such as /q,
|
|
197
|
+
* /d or /v:on. Switches can be written together (/q/c), and the command can
|
|
198
|
+
* start in the same word (/cnet). cmd gets the words as one command line,
|
|
199
|
+
* where a word with whitespace in it is in quotes, as PowerShell and Git Bash
|
|
200
|
+
* pass it.
|
|
201
|
+
*/
|
|
202
|
+
function cmdRuns({ words, wordTexts }) {
|
|
203
|
+
// Each word is a switch or a part of the command, so one known only at run time may hold any command (cmd $opts)
|
|
204
|
+
const noted = words.slice(1).includes(null) ? { file: null } : {};
|
|
205
|
+
let alwaysDropsQuotes = false;
|
|
206
|
+
for (let i = 1; i < wordTexts.length; i++) {
|
|
207
|
+
const word = wordTexts[i];
|
|
208
|
+
if (!word.startsWith('/'))
|
|
209
|
+
break;
|
|
210
|
+
const run = /^((?:\/(?![ckr])[^/]*)*)\/[ckr](.*)$/is.exec(word);
|
|
211
|
+
// /s: cmd drops the quotes around the command in every case
|
|
212
|
+
if (/\/s(?:\/|$)/i.test(run ? run[1] : word))
|
|
213
|
+
alwaysDropsQuotes = true;
|
|
214
|
+
if (!run)
|
|
215
|
+
continue;
|
|
216
|
+
const lineWords = run[2] ? [run[2], ...wordTexts.slice(i + 1)] : wordTexts.slice(i + 1);
|
|
217
|
+
if (lineWords.length === 0)
|
|
218
|
+
break;
|
|
219
|
+
const line = lineWords.map((lineWord) => (lineWord === '' || /\s/.test(lineWord) ? `"${lineWord}"` : lineWord)).join(' ');
|
|
220
|
+
return { texts: cmdQuoteReadings(line, alwaysDropsQuotes), ...noted };
|
|
221
|
+
}
|
|
222
|
+
return { texts: [], ...noted };
|
|
223
|
+
}
|
|
224
|
+
/**
|
|
225
|
+
* What eval (in a POSIX shell) or Invoke-Expression (in PowerShell, also
|
|
226
|
+
* written iex) runs in the shell it is in: its command texts. eval joins its
|
|
227
|
+
* words with spaces. Invoke-Expression runs one text, its Command; PowerShell
|
|
228
|
+
* binds one of the words that aren't parameter names to it, and the others
|
|
229
|
+
* may be values of other parameters (iex -ea 0 'net user'), so each of them
|
|
230
|
+
* is a text. Written with a colon, the parameter and its value are one word
|
|
231
|
+
* (iex -Command:'net user'), which runs what iex given that value runs. A
|
|
232
|
+
* text with a part known only at run time is read as written,
|
|
233
|
+
* as the text of bash -c is, so the programs written in it are checked
|
|
234
|
+
* (eval "cd $HOME && sudo ls"). That part may hold any command, so file is
|
|
235
|
+
* null then, and the command is noted; so is a text that comes from the
|
|
236
|
+
* pipeline ('net user' | iex). gsudo, in any shell, runs its command in the
|
|
237
|
+
* shell it was started from too (gsudo 'net user'), and gsudo !! runs the
|
|
238
|
+
* last command again, which the check can't see, so it is noted. No texts
|
|
239
|
+
* for any other command.
|
|
240
|
+
*/
|
|
241
|
+
export function sameShellTexts(found, shellType) {
|
|
242
|
+
const { name, words, wordTexts } = found;
|
|
243
|
+
const program = programName(name);
|
|
244
|
+
const sameShell = sameShellProgramReadings(program, words);
|
|
245
|
+
if (sameShell) {
|
|
246
|
+
const run = readingsRun(sameShell, found, { script: false });
|
|
247
|
+
return run.texts.includes('!!') ? { texts: [], file: null } : run;
|
|
248
|
+
}
|
|
249
|
+
if (shellType === 'posix' && program === 'eval') {
|
|
250
|
+
const from = words[1] === '--' ? 2 : 1;
|
|
251
|
+
return {
|
|
252
|
+
texts: from < words.length ? [wordTexts.slice(from).join(' ')] : [],
|
|
253
|
+
...(words.slice(from).includes(null) ? { file: null } : {}),
|
|
254
|
+
};
|
|
255
|
+
}
|
|
256
|
+
if (shellType === 'powershell' && program === 'invoke-expression') {
|
|
257
|
+
// Where the words that aren't parameter names are; with none, the text comes from the pipeline
|
|
258
|
+
const at = words.flatMap((word, i) => (i > 0 && (word === null || !/^[-\u2013\u2014\u2015]/.test(word)) ? [i] : []));
|
|
259
|
+
// -Command:'net user' (-c: 'net user'): the command without the parameter's name runs the same, and is shorter
|
|
260
|
+
const withColon = words.flatMap((word) => {
|
|
261
|
+
const colon = word === null ? null : /^[-\u2013\u2014\u2015]([a-z]+):(.*)$/is.exec(word);
|
|
262
|
+
return colon && colon[2] !== '' && 'command'.startsWith(colon[1].toLowerCase())
|
|
263
|
+
? [`${wordTexts[0]} ${colon[2]}`]
|
|
264
|
+
: [];
|
|
265
|
+
});
|
|
266
|
+
return {
|
|
267
|
+
texts: [...at.map((i) => wordTexts[i]), ...withColon],
|
|
268
|
+
...((at.length === 0 && withColon.length === 0) || at.some((i) => words[i] === null) ? { file: null } : {}),
|
|
269
|
+
};
|
|
270
|
+
}
|
|
271
|
+
return { texts: [] };
|
|
272
|
+
}
|
|
273
|
+
/**
|
|
274
|
+
* What the shell `found` starts runs, or the program that runs one command
|
|
275
|
+
* text in a shell (su -c 'sudo ls'): nothing for any other command. Its kind
|
|
276
|
+
* of shell is taken from its name as found, as checkCommand() takes the
|
|
277
|
+
* reader for a text from the shell it is given.
|
|
278
|
+
*/
|
|
279
|
+
export function shellRuns(found) {
|
|
280
|
+
const program = programName(found.name);
|
|
281
|
+
switch (getShellType(found.name)) {
|
|
282
|
+
case 'powershell':
|
|
283
|
+
return powerShellRuns(found, program);
|
|
284
|
+
case 'cmd':
|
|
285
|
+
return cmdRuns(found);
|
|
286
|
+
default:
|
|
287
|
+
// getShellType() takes every other program for a POSIX shell
|
|
288
|
+
return POSIX_SHELLS.has(program) ? posixShellRuns(found, program) : textProgramRuns(found, program);
|
|
289
|
+
}
|
|
290
|
+
}
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
import { parse, forEachChild } from 'unbash';
|
|
2
|
+
import { braceWords, quotedChars, unquotedChars, MAX_BRACE_WORDS } from './brace-expansion.js';
|
|
3
|
+
/**
|
|
4
|
+
* The command check's reader for POSIX shells (bash, zsh, sh, dash, ksh,
|
|
5
|
+
* fish), with unbash, a bash parser. It finds every command the shell would
|
|
6
|
+
* run, also on later lines and inside $( ), backticks, <( ), { } groups,
|
|
7
|
+
* subshells, functions and if, while, for and case bodies. Words inside
|
|
8
|
+
* quotes and heredoc bodies are text. A command name with a {a,b} brace
|
|
9
|
+
* expansion is the first word it expands to, so {sudo,ls} is sudo, and so is
|
|
10
|
+
* {$X,sudo} when $X is empty. zsh and
|
|
11
|
+
* the other shells are read as bash, which covers the commands they have in
|
|
12
|
+
* common.
|
|
13
|
+
*
|
|
14
|
+
* unbash doesn't stop at a syntax error: it returns what it could read and
|
|
15
|
+
* lists the errors, each on the script it found it in ($( ) and backticks
|
|
16
|
+
* are scripts of their own). Any error anywhere is a parse error here, so the
|
|
17
|
+
* check never decides on a command it read only in part.
|
|
18
|
+
*/
|
|
19
|
+
/** Word parts whose text is taken as written (a glob as its own text, not the files it matches) */
|
|
20
|
+
const STATIC_PARTS = new Set(['Literal', 'SingleQuoted', 'AnsiCQuoted', 'Glob']);
|
|
21
|
+
/** Word parts that bash drops when they are empty and unquoted ($X, ${X}, $(cmd)) */
|
|
22
|
+
const VANISHING_PARTS = new Set(['SimpleExpansion', 'ParameterExpansion', 'CommandExpansion']);
|
|
23
|
+
/** The commands that remove a function: unset -f, and zsh's unfunction, unhash -f and disable -f */
|
|
24
|
+
const FUNCTION_REMOVERS = new Set(['unset', 'unfunction', 'unhash', 'disable']);
|
|
25
|
+
/**
|
|
26
|
+
* Stand-ins for a piece known only at run time in a brace word's characters,
|
|
27
|
+
* so that braceWords keeps the words next to it ({$X,sudo} makes $X and sudo):
|
|
28
|
+
* one for a piece in VANISHING_PARTS, one for any other ("$X"). They start
|
|
29
|
+
* with a NUL, which no command text holds: Node starts no command that has one.
|
|
30
|
+
*/
|
|
31
|
+
const DROPPED_IF_EMPTY = '\0$';
|
|
32
|
+
const KEPT_IF_EMPTY = '\0"';
|
|
33
|
+
/**
|
|
34
|
+
* A word that brace expansion made and bash may drop: only pieces it drops
|
|
35
|
+
* when they are empty, and letters, digits or _ after one, which bash may read
|
|
36
|
+
* into a $X's name ({$X,su}do makes $Xdo)
|
|
37
|
+
*/
|
|
38
|
+
const MAY_VANISH = /^(?:\0\$\w*)+$/;
|
|
39
|
+
/**
|
|
40
|
+
* The commands that run, in this shell, code the reader doesn't read: eval and
|
|
41
|
+
* zsh's emulate -c (their words), trap and mapfile or readarray -C (later), .
|
|
42
|
+
* and source (a file), and alias (its value, where the name is used: an alias
|
|
43
|
+
* comes before a function)
|
|
44
|
+
*/
|
|
45
|
+
const CODE_RUNNERS = new Set(['eval', 'emulate', 'trap', 'mapfile', 'readarray', '.', 'source', 'alias']);
|
|
46
|
+
/** The words that run the next word as a command in this shell: builtin, command, and zsh's noglob and nocorrect */
|
|
47
|
+
const COMMAND_PREFIXES = new Set(['builtin', 'command', 'noglob', 'nocorrect']);
|
|
48
|
+
/**
|
|
49
|
+
* A word's value as bash reads it (quotes and escapes removed, $'…' decoded),
|
|
50
|
+
* or null when part of it is known only at run time: an expansion, a command's
|
|
51
|
+
* output, or a {a,b} brace expansion, which makes several words (the reader
|
|
52
|
+
* expands a command name's braces with braceWords).
|
|
53
|
+
*/
|
|
54
|
+
function wordValue(word) {
|
|
55
|
+
return (word.parts ?? []).every((part) => partValue(part) !== null) ? word.value : null;
|
|
56
|
+
}
|
|
57
|
+
/** One part's value as bash reads it, or null when it is known only at run time */
|
|
58
|
+
function partValue(part) {
|
|
59
|
+
if (part.type === 'DoubleQuoted' || part.type === 'LocaleString') {
|
|
60
|
+
if (!part.parts.every((inner) => inner.type === 'Literal'))
|
|
61
|
+
return null;
|
|
62
|
+
return part.parts.map((inner) => (inner.type === 'Literal' ? inner.value : '')).join('');
|
|
63
|
+
}
|
|
64
|
+
return STATIC_PARTS.has(part.type) && 'value' in part ? part.value : null;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* A word's characters for brace expansion. Only the braces unbash reads as an
|
|
68
|
+
* expansion are syntax. The rest of the word is text, and so are the quoted
|
|
69
|
+
* pieces inside the braces: {"a,b",c} gives a,b and c.
|
|
70
|
+
*/
|
|
71
|
+
function braceChars(word) {
|
|
72
|
+
const chars = [];
|
|
73
|
+
for (const part of word.parts ?? []) {
|
|
74
|
+
if (part.type !== 'BraceExpansion') {
|
|
75
|
+
chars.push(...partChars(part));
|
|
76
|
+
continue;
|
|
77
|
+
}
|
|
78
|
+
// The braces' text as written, except the quoted or expanded pieces inside, which are parts of their own
|
|
79
|
+
let at = part.pos;
|
|
80
|
+
for (const inner of part.parts ?? []) {
|
|
81
|
+
chars.push(...unquotedChars(part.text.slice(at - part.pos, inner.pos - part.pos)));
|
|
82
|
+
chars.push(...(inner.type === 'Literal' ? unquotedChars(inner.text) : partChars(inner)));
|
|
83
|
+
at = inner.end;
|
|
84
|
+
}
|
|
85
|
+
chars.push(...unquotedChars(part.text.slice(at - part.pos)));
|
|
86
|
+
}
|
|
87
|
+
return chars;
|
|
88
|
+
}
|
|
89
|
+
/** A part's characters, all text: its value, or a stand-in when it is known only at run time */
|
|
90
|
+
function partChars(part) {
|
|
91
|
+
const value = partValue(part);
|
|
92
|
+
if (value !== null)
|
|
93
|
+
return quotedChars(value);
|
|
94
|
+
return quotedChars(VANISHING_PARTS.has(part.type) ? DROPPED_IF_EMPTY : KEPT_IF_EMPTY);
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* The words bash makes from a word, each with its value (null when it is known
|
|
98
|
+
* only at run time) and whether bash may drop it, or null past braceWords' limits.
|
|
99
|
+
* A brace expansion makes several and bash drops the empty ones: {,sudo} ls
|
|
100
|
+
* runs sudo, {,} sudo runs sudo, and {$X,sudo} id runs sudo when $X is empty.
|
|
101
|
+
*/
|
|
102
|
+
function wordsMade(word) {
|
|
103
|
+
if (!word.parts?.some((part) => part.type === 'BraceExpansion')) {
|
|
104
|
+
return [{ value: wordValue(word), mayVanish: mayVanish(word) }];
|
|
105
|
+
}
|
|
106
|
+
const made = braceWords(braceChars(word));
|
|
107
|
+
return made && made.map((text) => ({ value: text.includes('\0') ? null : text, mayVanish: MAY_VANISH.test(text) }));
|
|
108
|
+
}
|
|
109
|
+
/** Whether bash may drop the word entirely, so that the next word is the command's name */
|
|
110
|
+
function mayVanish(word) {
|
|
111
|
+
const parts = word.parts ?? [];
|
|
112
|
+
return parts.length > 0 && parts.every((part) => VANISHING_PARTS.has(part.type));
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Whether a command may remove the function `name`: one of FUNCTION_REMOVERS
|
|
116
|
+
* anywhere among the command's words (so also after builtin, command or
|
|
117
|
+
* eval), followed by the name or by a word known only at run time
|
|
118
|
+
*/
|
|
119
|
+
function mayRemoveFunction(command, name) {
|
|
120
|
+
const at = command.words.findIndex((word) => word !== null && FUNCTION_REMOVERS.has(word));
|
|
121
|
+
return at >= 0 && command.words.slice(at + 1).some((word) => word === null || word === name);
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Whether a command may run code the reader doesn't read, which may remove any
|
|
125
|
+
* function: one of CODE_RUNNERS, also after COMMAND_PREFIXES (command -p eval),
|
|
126
|
+
* or a name known only at run time ($cmd may be unset or eval)
|
|
127
|
+
*/
|
|
128
|
+
function mayRunUnreadCode(command) {
|
|
129
|
+
const { words } = command;
|
|
130
|
+
let at = 0;
|
|
131
|
+
while (COMMAND_PREFIXES.has(words[at] ?? '')) {
|
|
132
|
+
at++;
|
|
133
|
+
while (words[at]?.startsWith('-'))
|
|
134
|
+
at++;
|
|
135
|
+
}
|
|
136
|
+
const name = words[at];
|
|
137
|
+
return name === null || (name !== undefined && CODE_RUNNERS.has(name));
|
|
138
|
+
}
|
|
139
|
+
/** Reads a command for a POSIX shell */
|
|
140
|
+
export async function readPosixCommands(command) {
|
|
141
|
+
const script = parse(command);
|
|
142
|
+
let parseError;
|
|
143
|
+
const commands = [];
|
|
144
|
+
// Positions inside backticks count from the backticks' own text, so a command
|
|
145
|
+
// there is given the start of the backticks around it
|
|
146
|
+
const visit = (node, backticksStart) => {
|
|
147
|
+
if (node.type === 'Script')
|
|
148
|
+
parseError ?? (parseError = node.errors?.[0]?.message);
|
|
149
|
+
if (node.type === 'Command' && node.name) {
|
|
150
|
+
const words = [node.name, ...node.args.filter((arg) => arg.type === 'Word')];
|
|
151
|
+
const values = words.map(wordValue);
|
|
152
|
+
// The name, and the next word too while the words before it may vanish ($EMPTY sudo ls runs sudo)
|
|
153
|
+
for (let i = 0; i < words.length; i++) {
|
|
154
|
+
const made = wordsMade(words[i]);
|
|
155
|
+
if (made === null) {
|
|
156
|
+
parseError ?? (parseError = `brace expansion makes more than ${MAX_BRACE_WORDS} words, or takes too long to read`);
|
|
157
|
+
break;
|
|
158
|
+
}
|
|
159
|
+
// Each word made is the name while the ones before it may vanish; those known only at run time all
|
|
160
|
+
// give the word's text as the name, so it is given once
|
|
161
|
+
const kept = made.findIndex((word) => !word.mayVanish);
|
|
162
|
+
const names = kept < 0 ? made : made.slice(0, kept + 1);
|
|
163
|
+
names.forEach((name, k) => {
|
|
164
|
+
if (name.value === null && names.findIndex((other) => other.value === null) < k)
|
|
165
|
+
return;
|
|
166
|
+
commands.push({
|
|
167
|
+
name: name.value ?? words[i].text,
|
|
168
|
+
words: [...made.slice(k).map((word) => word.value), ...values.slice(i + 1)],
|
|
169
|
+
written: [...made.slice(k).map(() => words[i].text), ...words.slice(i + 1).map((word) => word.text)],
|
|
170
|
+
// unbash's value of a word keeps the parts known only at run time as written
|
|
171
|
+
wordTexts: [...made.slice(k).map((word) => word.value ?? words[i].value), ...words.slice(i + 1).map((word) => word.value)],
|
|
172
|
+
start: backticksStart ?? (i === 0 ? node.pos : words[i].pos),
|
|
173
|
+
nameAtRunTime: name.value === null,
|
|
174
|
+
});
|
|
175
|
+
});
|
|
176
|
+
if (kept >= 0)
|
|
177
|
+
break;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
const inBackticks = node.type === 'CommandExpansion' && node.text.startsWith('`');
|
|
181
|
+
const childBackticksStart = inBackticks ? backticksStart ?? node.pos : backticksStart;
|
|
182
|
+
forEachChild(node, (child) => visit(child, childBackticksStart));
|
|
183
|
+
};
|
|
184
|
+
visit(script, undefined);
|
|
185
|
+
if (parseError !== undefined)
|
|
186
|
+
return { commands: [], functions: [], parseError };
|
|
187
|
+
// Only the script's own statements (see CommandReading.functions), and none the
|
|
188
|
+
// text may remove: wherever the removal is written, it may run before a call,
|
|
189
|
+
// from a function's body. Code the reader doesn't read may remove any of them.
|
|
190
|
+
const functions = [];
|
|
191
|
+
const statements = commands.some(mayRunUnreadCode) ? [] : script.commands;
|
|
192
|
+
for (const statement of statements) {
|
|
193
|
+
if (statement.command.type !== 'Function' || statement.background)
|
|
194
|
+
continue;
|
|
195
|
+
const name = statement.command.name.value;
|
|
196
|
+
if (!commands.some((command) => mayRemoveFunction(command, name))) {
|
|
197
|
+
functions.push({ name, end: statement.command.end });
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
return { commands: commands.sort((a, b) => a.start - b.start), functions };
|
|
201
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import type { CommandReading } from './command-reading.js';
|
|
2
|
+
/**
|
|
3
|
+
* The command check's reader for PowerShell. No JavaScript parser reads
|
|
4
|
+
* PowerShell correctly (here-strings, @{ } tables, & $var), so a helper
|
|
5
|
+
* PowerShell process reads each command with PowerShell's own parser. It
|
|
6
|
+
* starts at the first PowerShell check, so nothing runs for users who never
|
|
7
|
+
* run PowerShell commands, and it is kept running: a check then takes a few
|
|
8
|
+
* milliseconds instead of PowerShell's start (about a second for Windows
|
|
9
|
+
* PowerShell). It is not a terminal session, so list_sessions never shows it.
|
|
10
|
+
* It starts as the PowerShell a command runs in starts: with the user's
|
|
11
|
+
* profile (start_process starts it without -NoProfile), without
|
|
12
|
+
* -NonInteractive, with -Login for PowerShell 7, and with the commands'
|
|
13
|
+
* environment. So a profile sets the same aliases for both. Only its working
|
|
14
|
+
* folder is its own (see start()).
|
|
15
|
+
*/
|
|
16
|
+
/** How long a check waits for a running helper's answer. A helper that doesn't answer is stopped, and the check fails. */
|
|
17
|
+
export declare const HELPER_TIMEOUT_MS = 5000;
|
|
18
|
+
/**
|
|
19
|
+
* How long the first check waits after a helper starts, PowerShell's start
|
|
20
|
+
* and the user's profile included: about a second for Windows PowerShell,
|
|
21
|
+
* longer on a busy machine or with a slow profile
|
|
22
|
+
*/
|
|
23
|
+
export declare const HELPER_START_TIMEOUT_MS = 15000;
|
|
24
|
+
/** One helper process for one PowerShell program, started at its first read and again after it ended */
|
|
25
|
+
export declare class PowerShellHelper {
|
|
26
|
+
private readonly program;
|
|
27
|
+
private readonly args;
|
|
28
|
+
private child;
|
|
29
|
+
private output;
|
|
30
|
+
private nextId;
|
|
31
|
+
private readonly pending;
|
|
32
|
+
private timer;
|
|
33
|
+
/** Whether the running helper process has answered yet: until then its start counts in the time limit */
|
|
34
|
+
private answered;
|
|
35
|
+
/** Made at each start and sent with every request; an answer has it, a line the profile printed doesn't */
|
|
36
|
+
private token;
|
|
37
|
+
/** args replace the helper's arguments, for a stand-in in tests */
|
|
38
|
+
constructor(program: string, args?: string[]);
|
|
39
|
+
/** The running helper's process id: undefined before the first read and after the process ended */
|
|
40
|
+
get pid(): number | undefined;
|
|
41
|
+
/** Reads one command. It rejects when the helper doesn't answer in time, can't start, or ends first. */
|
|
42
|
+
read(command: string): Promise<CommandReading>;
|
|
43
|
+
/** The time limit for the oldest waiting request: longer while the helper hasn't answered since it started */
|
|
44
|
+
private startTimeLimit;
|
|
45
|
+
/** Ends the helper process. The next read starts a new one. */
|
|
46
|
+
stop(): void;
|
|
47
|
+
private start;
|
|
48
|
+
private onOutput;
|
|
49
|
+
private onAnswer;
|
|
50
|
+
/** The helper process ended or failed to start: every waiting read fails, and the next read starts a new one */
|
|
51
|
+
private ended;
|
|
52
|
+
private fail;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Reads a command for a PowerShell shell ("powershell.exe", "pwsh" or a path),
|
|
56
|
+
* with the same program as the helper. checkCommand() has made sure it's there.
|
|
57
|
+
*/
|
|
58
|
+
export declare function readPowerShellCommands(command: string, shell: string): Promise<CommandReading>;
|
|
59
|
+
/** The process ids of the helpers running now, for tests */
|
|
60
|
+
export declare function runningPowerShellHelpers(): number[];
|