@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
package/dist/bootstrap.js
CHANGED
|
@@ -11,12 +11,12 @@
|
|
|
11
11
|
*
|
|
12
12
|
* Raising the pool size gives enough headroom that a burst of slow reads no
|
|
13
13
|
* longer starves the rest. libuv reads UV_THREADPOOL_SIZE only when the pool is
|
|
14
|
-
* first initialized (on first submitted work), so this
|
|
15
|
-
*
|
|
16
|
-
*
|
|
14
|
+
* first initialized (on first submitted work), so this has to happen before
|
|
15
|
+
* ANY threadpool work — hence "first import". On Windows with Node 22, libuv
|
|
16
|
+
* doesn't see a value set inside the process at all, so setThreadpoolSize()
|
|
17
|
+
* starts the server again with it (src/utils/threadpool-size.ts). A
|
|
18
|
+
* user-provided value is always respected.
|
|
17
19
|
*/
|
|
20
|
+
import { setThreadpoolSize } from './utils/threadpool-size.js';
|
|
18
21
|
const DEFAULT_THREADPOOL_SIZE = 16;
|
|
19
|
-
|
|
20
|
-
process.env.UV_THREADPOOL_SIZE = String(DEFAULT_THREADPOOL_SIZE);
|
|
21
|
-
}
|
|
22
|
-
export {};
|
|
22
|
+
setThreadpoolSize(process.env.UV_THREADPOOL_SIZE || String(DEFAULT_THREADPOOL_SIZE));
|
|
@@ -1,3 +1,33 @@
|
|
|
1
|
+
import type { CommandReading } from './utils/command-reading.js';
|
|
2
|
+
/**
|
|
3
|
+
* The check's answer for one command.
|
|
4
|
+
* - allowed: no blocked program runs.
|
|
5
|
+
* - blocked: `name` is the blockedCommands entry that one of the programs matched.
|
|
6
|
+
* - unseen: allowed, but some program is chosen only when the command runs
|
|
7
|
+
* (eval "$CMD", Invoke-Expression $cmd, & $tool) or is in a script file
|
|
8
|
+
* (bash deploy.sh). Each entry of `unseen` says what hides one of them.
|
|
9
|
+
* - failed: the check couldn't read the command or the settings, and
|
|
10
|
+
* `reason` holds the error's message.
|
|
11
|
+
*/
|
|
12
|
+
export type CommandCheck = {
|
|
13
|
+
verdict: 'allowed';
|
|
14
|
+
} | {
|
|
15
|
+
verdict: 'blocked';
|
|
16
|
+
name: string;
|
|
17
|
+
} | {
|
|
18
|
+
verdict: 'unseen';
|
|
19
|
+
unseen: UnseenProgram[];
|
|
20
|
+
} | {
|
|
21
|
+
verdict: 'failed';
|
|
22
|
+
reason: string;
|
|
23
|
+
};
|
|
24
|
+
/** Something in a command that hides the program it runs from the check */
|
|
25
|
+
export interface UnseenProgram {
|
|
26
|
+
/** What hides it, as the shell reads it (in PowerShell, an alias's target): "eval", "Invoke-Expression", "$tool" or "deploy.sh" */
|
|
27
|
+
what: string;
|
|
28
|
+
/** True for a script/startup file given to a shell; false for a program chosen at runtime. */
|
|
29
|
+
inFile: boolean;
|
|
30
|
+
}
|
|
1
31
|
declare class CommandManager {
|
|
2
32
|
getBaseCommand(command: string): string;
|
|
3
33
|
/**
|
|
@@ -7,7 +37,74 @@ declare class CommandManager {
|
|
|
7
37
|
*/
|
|
8
38
|
extractCommands(commandString: string, asTyped?: boolean): string[];
|
|
9
39
|
extractBaseCommand(commandStr: string, asTyped?: boolean): string | null;
|
|
10
|
-
|
|
40
|
+
/**
|
|
41
|
+
* Reads a command with the grammar of the shell that will run it: POSIX
|
|
42
|
+
* shells (bash, zsh, sh, …), PowerShell, or cmd. fish is read with the
|
|
43
|
+
* bash reader, and where that can't parse fish's own syntax (for … end,
|
|
44
|
+
* echo (date)), as main reads it: by the names extractCommands() finds.
|
|
45
|
+
*/
|
|
46
|
+
readCommands(command: string, shell: string): Promise<CommandReading>;
|
|
47
|
+
/**
|
|
48
|
+
* The commands of a reading that run a program: the calls to the script's
|
|
49
|
+
* own functions are left out. A script can define a function with a
|
|
50
|
+
* program's name, such as an error helper named halt, and the shell then
|
|
51
|
+
* runs the function, not the program. A call is the function's when it
|
|
52
|
+
* comes after the definition, its name as written has no folder
|
|
53
|
+
* (/sbin/halt is the program), and the reader lists the function, so not
|
|
54
|
+
* one the text may remove. PowerShell finds a function whatever the case
|
|
55
|
+
* of its name, POSIX shells only by its exact name. The function's body is
|
|
56
|
+
* read as commands of its own, so a blocked program inside it is still
|
|
57
|
+
* found.
|
|
58
|
+
*/
|
|
59
|
+
private withoutOwnFunctionCalls;
|
|
60
|
+
/**
|
|
61
|
+
* What hides a program from the check among the commands that run
|
|
62
|
+
* (CommandCheck's `unseen`), in the order the commands start:
|
|
63
|
+
* - a program chosen only when the command runs: a name that is a variable
|
|
64
|
+
* or another command's output (& $tool), or eval and Invoke-Expression,
|
|
65
|
+
* which run text the command builds (eval "$CMD");
|
|
66
|
+
* - a script file given to a shell (bash deploy.sh): the commands in it
|
|
67
|
+
* aren't in the text. A file named only when the command runs ("$SCRIPT")
|
|
68
|
+
* counts as a program chosen at run time, named by the shell.
|
|
69
|
+
*/
|
|
70
|
+
private findUnseen;
|
|
71
|
+
/**
|
|
72
|
+
* What hides the program when a command gives a shell a script or startup file to run
|
|
73
|
+
* (bash deploy.sh, pwsh -File deploy.ps1, source deploy.sh), or when
|
|
74
|
+
* PowerShell runs one named as the command (./deploy.ps1): the file, or the
|
|
75
|
+
* shell when the file is named only when the command runs ("$SCRIPT").
|
|
76
|
+
* A shell's command text or standard input adds no file. An explicit Bash
|
|
77
|
+
* startup file can still run beside command text in interactive mode.
|
|
78
|
+
*/
|
|
79
|
+
private scriptFiles;
|
|
80
|
+
/**
|
|
81
|
+
* The check's answer for `command` run by `shell`:
|
|
82
|
+
* - allowed: no program it runs is blocked.
|
|
83
|
+
* - blocked: a program it runs is in blockedCommands, both compared in the
|
|
84
|
+
* form programName() gives; the answer names the entry as written (a call
|
|
85
|
+
* to the script's own function is not the program: see withoutOwnFunctionCalls).
|
|
86
|
+
* - unseen: allowed, but a program is chosen only when the command runs or
|
|
87
|
+
* is in a script file (see findUnseen), so start_process can say the
|
|
88
|
+
* blocklist couldn't check it.
|
|
89
|
+
* - failed: the command or the settings can't be read. In PowerShell a parse
|
|
90
|
+
* error or a missing PowerShell is allowed instead: nothing runs, and
|
|
91
|
+
* PowerShell or start_process answers.
|
|
92
|
+
*/
|
|
93
|
+
checkCommand(command: string, shell: string): Promise<CommandCheck>;
|
|
94
|
+
/**
|
|
95
|
+
* checkCommand() for a command, or for a command text in it: each text is
|
|
96
|
+
* checked once per command in each shell, however many readings give it.
|
|
97
|
+
* In pwsh $a pwsh $a … each text gives every later one again, so without
|
|
98
|
+
* this the work doubled with each pair.
|
|
99
|
+
*/
|
|
100
|
+
private checkText;
|
|
101
|
+
private checkOnce;
|
|
102
|
+
/** Whether a check's answer refuses the command */
|
|
103
|
+
isRefused(check: CommandCheck): check is Extract<CommandCheck, {
|
|
104
|
+
verdict: 'blocked' | 'failed';
|
|
105
|
+
}>;
|
|
106
|
+
/** Whether the command may run in `shell`: the check doesn't refuse it */
|
|
107
|
+
validateCommand(command: string, shell: string): Promise<boolean>;
|
|
11
108
|
}
|
|
12
109
|
export declare const commandManager: CommandManager;
|
|
13
110
|
export {};
|
package/dist/command-manager.js
CHANGED
|
@@ -1,6 +1,13 @@
|
|
|
1
1
|
import path from 'path';
|
|
2
2
|
import { configManager } from './config-manager.js';
|
|
3
3
|
import { capture } from "./utils/capture.js";
|
|
4
|
+
import { getShellType, isShellAvailable, programName } from './utils/shell.js';
|
|
5
|
+
import { sameShellTexts, shellRuns } from './utils/inline-shells.js';
|
|
6
|
+
import { shellScriptFiles } from './utils/shell-script-files.js';
|
|
7
|
+
import { readPosixCommands } from './utils/posix-commands.js';
|
|
8
|
+
import { readPowerShellCommands } from './utils/powershell-commands.js';
|
|
9
|
+
import { readCmdCommands } from './utils/cmd-commands.js';
|
|
10
|
+
import { withStartedPrograms } from './utils/command-wrappers.js';
|
|
4
11
|
class CommandManager {
|
|
5
12
|
getBaseCommand(command) {
|
|
6
13
|
return command.split(' ')[0].toLowerCase().trim();
|
|
@@ -211,36 +218,213 @@ class CommandManager {
|
|
|
211
218
|
return null;
|
|
212
219
|
}
|
|
213
220
|
}
|
|
214
|
-
|
|
221
|
+
/**
|
|
222
|
+
* Reads a command with the grammar of the shell that will run it: POSIX
|
|
223
|
+
* shells (bash, zsh, sh, …), PowerShell, or cmd. fish is read with the
|
|
224
|
+
* bash reader, and where that can't parse fish's own syntax (for … end,
|
|
225
|
+
* echo (date)), as main reads it: by the names extractCommands() finds.
|
|
226
|
+
*/
|
|
227
|
+
async readCommands(command, shell) {
|
|
228
|
+
switch (getShellType(shell)) {
|
|
229
|
+
case 'powershell':
|
|
230
|
+
return readPowerShellCommands(command, shell);
|
|
231
|
+
case 'cmd':
|
|
232
|
+
return readCmdCommands(command);
|
|
233
|
+
}
|
|
234
|
+
const reading = await readPosixCommands(command);
|
|
235
|
+
// fish's name, as getShellSpawnArgs() finds it
|
|
236
|
+
if (reading.parseError === undefined || !path.basename(shell).toLowerCase().includes('fish'))
|
|
237
|
+
return reading;
|
|
238
|
+
const { parseError, ...read } = reading;
|
|
239
|
+
const names = this.extractCommands(command);
|
|
240
|
+
return {
|
|
241
|
+
...read,
|
|
242
|
+
commands: (names.length > 0 ? names : [this.getBaseCommand(command)])
|
|
243
|
+
.map((name, start) => ({
|
|
244
|
+
name, words: [name], written: [name], wordTexts: [name], start, nameAtRunTime: false
|
|
245
|
+
})),
|
|
246
|
+
};
|
|
247
|
+
}
|
|
248
|
+
/**
|
|
249
|
+
* The commands of a reading that run a program: the calls to the script's
|
|
250
|
+
* own functions are left out. A script can define a function with a
|
|
251
|
+
* program's name, such as an error helper named halt, and the shell then
|
|
252
|
+
* runs the function, not the program. A call is the function's when it
|
|
253
|
+
* comes after the definition, its name as written has no folder
|
|
254
|
+
* (/sbin/halt is the program), and the reader lists the function, so not
|
|
255
|
+
* one the text may remove. PowerShell finds a function whatever the case
|
|
256
|
+
* of its name, POSIX shells only by its exact name. The function's body is
|
|
257
|
+
* read as commands of its own, so a blocked program inside it is still
|
|
258
|
+
* found.
|
|
259
|
+
*/
|
|
260
|
+
withoutOwnFunctionCalls(reading, shellType) {
|
|
261
|
+
const sameName = (a, b) => shellType === 'powershell' ? a.toLowerCase() === b.toLowerCase() : a === b;
|
|
262
|
+
return reading.commands.filter((cmd) => {
|
|
263
|
+
const written = cmd.words[0];
|
|
264
|
+
if (written === null || written.includes('/') || written.includes('\\'))
|
|
265
|
+
return true;
|
|
266
|
+
return !reading.functions.some((fn) => sameName(fn.name, written) && cmd.start >= fn.end);
|
|
267
|
+
});
|
|
268
|
+
}
|
|
269
|
+
/**
|
|
270
|
+
* What hides a program from the check among the commands that run
|
|
271
|
+
* (CommandCheck's `unseen`), in the order the commands start:
|
|
272
|
+
* - a program chosen only when the command runs: a name that is a variable
|
|
273
|
+
* or another command's output (& $tool), or eval and Invoke-Expression,
|
|
274
|
+
* which run text the command builds (eval "$CMD");
|
|
275
|
+
* - a script file given to a shell (bash deploy.sh): the commands in it
|
|
276
|
+
* aren't in the text. A file named only when the command runs ("$SCRIPT")
|
|
277
|
+
* counts as a program chosen at run time, named by the shell.
|
|
278
|
+
*/
|
|
279
|
+
findUnseen(commands, shellType) {
|
|
280
|
+
const unseen = [];
|
|
281
|
+
for (const cmd of commands) {
|
|
282
|
+
if (cmd.nameAtRunTime) {
|
|
283
|
+
unseen.push({ what: cmd.name, inFile: false });
|
|
284
|
+
continue;
|
|
285
|
+
}
|
|
286
|
+
unseen.push(...this.scriptFiles(cmd, shellType));
|
|
287
|
+
const unknown = sameShellTexts(cmd, shellType).file === null || shellRuns(cmd).file === null;
|
|
288
|
+
if (unknown && !unseen.some((item) => item.what === cmd.name && !item.inFile)) {
|
|
289
|
+
unseen.push({ what: cmd.name, inFile: false });
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
return unseen;
|
|
293
|
+
}
|
|
294
|
+
/**
|
|
295
|
+
* What hides the program when a command gives a shell a script or startup file to run
|
|
296
|
+
* (bash deploy.sh, pwsh -File deploy.ps1, source deploy.sh), or when
|
|
297
|
+
* PowerShell runs one named as the command (./deploy.ps1): the file, or the
|
|
298
|
+
* shell when the file is named only when the command runs ("$SCRIPT").
|
|
299
|
+
* A shell's command text or standard input adds no file. An explicit Bash
|
|
300
|
+
* startup file can still run beside command text in interactive mode.
|
|
301
|
+
*/
|
|
302
|
+
scriptFiles(cmd, shellType) {
|
|
303
|
+
const program = programName(cmd.name, shellType);
|
|
304
|
+
const args = cmd.words.slice(1);
|
|
305
|
+
const hidden = (file) => {
|
|
306
|
+
return file === null ? { what: cmd.name, inFile: false } : { what: file, inFile: true };
|
|
307
|
+
};
|
|
308
|
+
if (shellType === 'posix' && (program === 'source' || program === '.')) {
|
|
309
|
+
return args[0] === undefined ? [] : [hidden(args[0])];
|
|
310
|
+
}
|
|
311
|
+
// PowerShell runs a .ps1 named as the command (./deploy.ps1, & ./deploy.ps1) as a script file
|
|
312
|
+
if (shellType === 'powershell' && /\.ps1$/i.test(cmd.name)) {
|
|
313
|
+
return [hidden(cmd.name)];
|
|
314
|
+
}
|
|
315
|
+
// PowerShell text and file modes use the same host-option readings.
|
|
316
|
+
if (getShellType(cmd.name) === 'powershell') {
|
|
317
|
+
const file = shellRuns(cmd).file;
|
|
318
|
+
return file === undefined ? [] : [hidden(file)];
|
|
319
|
+
}
|
|
320
|
+
return shellScriptFiles(program, args).map(hidden);
|
|
321
|
+
}
|
|
322
|
+
/**
|
|
323
|
+
* The check's answer for `command` run by `shell`:
|
|
324
|
+
* - allowed: no program it runs is blocked.
|
|
325
|
+
* - blocked: a program it runs is in blockedCommands, both compared in the
|
|
326
|
+
* form programName() gives; the answer names the entry as written (a call
|
|
327
|
+
* to the script's own function is not the program: see withoutOwnFunctionCalls).
|
|
328
|
+
* - unseen: allowed, but a program is chosen only when the command runs or
|
|
329
|
+
* is in a script file (see findUnseen), so start_process can say the
|
|
330
|
+
* blocklist couldn't check it.
|
|
331
|
+
* - failed: the command or the settings can't be read. In PowerShell a parse
|
|
332
|
+
* error or a missing PowerShell is allowed instead: nothing runs, and
|
|
333
|
+
* PowerShell or start_process answers.
|
|
334
|
+
*/
|
|
335
|
+
checkCommand(command, shell) {
|
|
336
|
+
return this.checkText(command, shell, new Map());
|
|
337
|
+
}
|
|
338
|
+
/**
|
|
339
|
+
* checkCommand() for a command, or for a command text in it: each text is
|
|
340
|
+
* checked once per command in each shell, however many readings give it.
|
|
341
|
+
* In pwsh $a pwsh $a … each text gives every later one again, so without
|
|
342
|
+
* this the work doubled with each pair.
|
|
343
|
+
*/
|
|
344
|
+
checkText(command, shell, checked) {
|
|
345
|
+
const key = `${shell}\0${command}`;
|
|
346
|
+
let check = checked.get(key);
|
|
347
|
+
if (check === undefined) {
|
|
348
|
+
check = this.checkOnce(command, shell, checked);
|
|
349
|
+
checked.set(key, check);
|
|
350
|
+
}
|
|
351
|
+
return check;
|
|
352
|
+
}
|
|
353
|
+
async checkOnce(command, shell, checked) {
|
|
215
354
|
try {
|
|
216
|
-
// Get blocked commands from config
|
|
217
355
|
const config = await configManager.getConfig();
|
|
218
356
|
const blockedCommands = config.blockedCommands || [];
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
357
|
+
const shellType = getShellType(shell);
|
|
358
|
+
// PowerShell runs nothing when it isn't there or can't parse the command: it, or start_process, answers
|
|
359
|
+
if (shellType === 'powershell' && !isShellAvailable(shell))
|
|
360
|
+
return { verdict: 'allowed' };
|
|
361
|
+
const reading = await this.readCommands(command, shell);
|
|
362
|
+
// A parse error is the command's fault, not the server's, so it isn't logged or sent
|
|
363
|
+
if (reading.parseError !== undefined) {
|
|
364
|
+
return shellType === 'powershell'
|
|
365
|
+
? { verdict: 'allowed' }
|
|
366
|
+
: { verdict: 'failed', reason: reading.parseError };
|
|
225
367
|
}
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
368
|
+
const running = withStartedPrograms(this.withoutOwnFunctionCalls(reading, shellType), shell);
|
|
369
|
+
// set_config_value checks only that the list is an array
|
|
370
|
+
const entries = blockedCommands.filter((entry) => typeof entry === 'string');
|
|
371
|
+
for (const found of running) {
|
|
372
|
+
// A name known only at run time ($tool) names no program the list could match
|
|
373
|
+
if (found.nameAtRunTime)
|
|
374
|
+
continue;
|
|
375
|
+
const program = programName(found.name, shellType);
|
|
376
|
+
const entry = entries.find((candidate) => programName(candidate, shellType) === program);
|
|
377
|
+
if (entry !== undefined)
|
|
378
|
+
return { verdict: 'blocked', name: entry };
|
|
379
|
+
}
|
|
380
|
+
const unseen = [];
|
|
381
|
+
let unreadable;
|
|
382
|
+
for (const found of running) {
|
|
383
|
+
unseen.push(...this.findUnseen([found], shellType));
|
|
384
|
+
const run = shellRuns(found);
|
|
385
|
+
const texts = [
|
|
386
|
+
...sameShellTexts(found, shellType).texts.map((text) => ({ text, runs: shell })),
|
|
387
|
+
// su -c's text runs in sh, runas's command line as cmd reads it
|
|
388
|
+
...run.texts.map((text) => ({ text, runs: run.shell ?? found.name })),
|
|
389
|
+
];
|
|
390
|
+
for (const { text, runs } of texts) {
|
|
391
|
+
// The shell that runs the command looks a PowerShell's name up itself, in its working folder
|
|
392
|
+
// too: one the check can't find is read by the PowerShell running the command, when that is one
|
|
393
|
+
const reader = getShellType(runs) !== 'powershell' ? runs
|
|
394
|
+
: [runs, shell].find((candidate) => getShellType(candidate) === 'powershell' && isShellAvailable(candidate));
|
|
395
|
+
const inner = reader === undefined
|
|
396
|
+
? { verdict: 'failed', reason: `${runs} isn't installed here, so the command given to it can't be read` }
|
|
397
|
+
: await this.checkText(text, reader, checked);
|
|
398
|
+
if (inner.verdict === 'blocked')
|
|
399
|
+
return inner;
|
|
400
|
+
if (inner.verdict === 'failed')
|
|
401
|
+
unreadable ?? (unreadable = inner);
|
|
402
|
+
if (inner.verdict === 'unseen')
|
|
403
|
+
unseen.push(...inner.unseen);
|
|
230
404
|
}
|
|
231
405
|
}
|
|
232
|
-
|
|
233
|
-
|
|
406
|
+
if (unreadable)
|
|
407
|
+
return unreadable;
|
|
408
|
+
return unseen.length > 0 ? { verdict: 'unseen', unseen } : { verdict: 'allowed' };
|
|
234
409
|
}
|
|
235
410
|
catch (error) {
|
|
411
|
+
const reason = error instanceof Error ? error.message : String(error);
|
|
236
412
|
console.error('Error validating command:', error);
|
|
237
413
|
capture('server_validate_command_error', {
|
|
238
|
-
error:
|
|
414
|
+
error: reason
|
|
239
415
|
});
|
|
240
416
|
// Fail closed: deny the command if validation encounters an error.
|
|
241
417
|
// This prevents a config read failure from bypassing all command filtering.
|
|
242
|
-
return
|
|
418
|
+
return { verdict: 'failed', reason };
|
|
243
419
|
}
|
|
244
420
|
}
|
|
421
|
+
/** Whether a check's answer refuses the command */
|
|
422
|
+
isRefused(check) {
|
|
423
|
+
return check.verdict === 'blocked' || check.verdict === 'failed';
|
|
424
|
+
}
|
|
425
|
+
/** Whether the command may run in `shell`: the check doesn't refuse it */
|
|
426
|
+
async validateCommand(command, shell) {
|
|
427
|
+
return !this.isRefused(await this.checkCommand(command, shell));
|
|
428
|
+
}
|
|
245
429
|
}
|
|
246
430
|
export const commandManager = new CommandManager();
|
|
@@ -23,6 +23,8 @@ const RESTART_BACKOFF_CAP_MS = 5000;
|
|
|
23
23
|
const restartBackoffMs = (attempt) =>
|
|
24
24
|
// Cap AFTER jitter, so the cap is the wait this comment claims it is.
|
|
25
25
|
Math.min(RESTART_BACKOFF_CAP_MS, 250 * 2 ** Math.min(attempt, 5) * (0.5 + Math.random()));
|
|
26
|
+
// As long as the server waits: it fails a call still running in the background 30 minutes after the device claims it, at its next 30-second sweep
|
|
27
|
+
const TOOL_CALL_TIMEOUT_MS = 30 * 60000 + 30000;
|
|
26
28
|
const __filename = fileURLToPath(import.meta.url);
|
|
27
29
|
const __dirname = path.dirname(__filename);
|
|
28
30
|
export class DesktopCommanderIntegration {
|
|
@@ -274,7 +276,7 @@ export class DesktopCommanderIntegration {
|
|
|
274
276
|
name: toolName,
|
|
275
277
|
arguments: args,
|
|
276
278
|
_meta: { remote: true, ...metadata || {} }
|
|
277
|
-
});
|
|
279
|
+
}, undefined, { timeout: TOOL_CALL_TIMEOUT_MS });
|
|
278
280
|
console.debug('[DEBUG] Tool call successful:', toolName);
|
|
279
281
|
return result;
|
|
280
282
|
}
|
|
@@ -52,6 +52,35 @@ function safeUserName() {
|
|
|
52
52
|
return '';
|
|
53
53
|
}
|
|
54
54
|
}
|
|
55
|
+
/** Mask overlapping private values together so one replacement cannot expose another's fragments. */
|
|
56
|
+
function maskHostnamesAndUuids(text) {
|
|
57
|
+
const hostname = os.hostname();
|
|
58
|
+
const hosts = [...new Set([hostname, hostname.split('.')[0]])]
|
|
59
|
+
.map(wordPattern).filter((pattern) => pattern !== null);
|
|
60
|
+
const patterns = [
|
|
61
|
+
{ pattern: UUID, placeholder: '<id>' },
|
|
62
|
+
...hosts.map((pattern) => ({ pattern, placeholder: '<host>' })),
|
|
63
|
+
];
|
|
64
|
+
const matches = patterns.flatMap(({ pattern, placeholder }) => Array.from(text.matchAll(pattern), (match) => ({
|
|
65
|
+
start: match.index, end: match.index + match[0].length, placeholder,
|
|
66
|
+
}))).sort((a, b) => a.start - b.start || b.end - a.end);
|
|
67
|
+
const ranges = [];
|
|
68
|
+
for (const match of matches) {
|
|
69
|
+
const previous = ranges.at(-1);
|
|
70
|
+
if (previous && match.start < previous.end)
|
|
71
|
+
previous.end = Math.max(previous.end, match.end);
|
|
72
|
+
else
|
|
73
|
+
ranges.push({ ...match });
|
|
74
|
+
}
|
|
75
|
+
const pieces = [];
|
|
76
|
+
let cursor = 0;
|
|
77
|
+
for (const range of ranges) {
|
|
78
|
+
pieces.push(text.slice(cursor, range.start), range.placeholder);
|
|
79
|
+
cursor = range.end;
|
|
80
|
+
}
|
|
81
|
+
pieces.push(text.slice(cursor));
|
|
82
|
+
return pieces.join('');
|
|
83
|
+
}
|
|
55
84
|
export function redact(text) {
|
|
56
85
|
let out = text;
|
|
57
86
|
const home = homePattern(os.homedir());
|
|
@@ -61,14 +90,7 @@ export function redact(text) {
|
|
|
61
90
|
out = out.replace(BEARER, '$1<redacted>');
|
|
62
91
|
out = out.replace(JWT, '<jwt>');
|
|
63
92
|
out = out.replace(EMAIL, '<email>');
|
|
64
|
-
out = out
|
|
65
|
-
// "name.local" and the bare "name" a Mac also goes by
|
|
66
|
-
const hostname = os.hostname();
|
|
67
|
-
for (const name of new Set([hostname, hostname.split('.')[0]])) {
|
|
68
|
-
const host = wordPattern(name);
|
|
69
|
-
if (host)
|
|
70
|
-
out = out.replace(host, '<host>');
|
|
71
|
-
}
|
|
93
|
+
out = maskHostnamesAndUuids(out);
|
|
72
94
|
const user = wordPattern(safeUserName());
|
|
73
95
|
if (user)
|
|
74
96
|
out = out.replace(user, '<user>');
|
|
@@ -10,6 +10,7 @@ import { fileURLToPath } from 'url';
|
|
|
10
10
|
import PizZip from 'pizzip';
|
|
11
11
|
import { parseConfig } from '../../config-manager.js';
|
|
12
12
|
import { VERSION } from '../../version.js';
|
|
13
|
+
import { LAUNCHER_PID_ARGUMENT } from '../../utils/threadpool-size.js';
|
|
13
14
|
import { deviceLogNames, getDeviceLogDir, cleanLine } from './device-log.js';
|
|
14
15
|
import { redact } from './redact.js';
|
|
15
16
|
import { savedUserId, uploadReport } from './upload.js';
|
|
@@ -317,7 +318,7 @@ function elapsedSeconds(etime) {
|
|
|
317
318
|
return Number(days) * 86400 + parts[0] * 3600 + parts[1] * 60 + parts[2];
|
|
318
319
|
}
|
|
319
320
|
/**
|
|
320
|
-
* Every process with its command line and start time. process.ts's list uses
|
|
321
|
+
* Every process with its parent, command line and start time. process.ts's list uses
|
|
321
322
|
* tasklist on Windows, which has no command lines, hence Win32_Process here.
|
|
322
323
|
*/
|
|
323
324
|
function listProcesses() {
|
|
@@ -325,24 +326,24 @@ function listProcesses() {
|
|
|
325
326
|
return new Promise((resolve, reject) => {
|
|
326
327
|
if (process.platform === 'win32') {
|
|
327
328
|
const script = '[Console]::OutputEncoding = [Text.Encoding]::UTF8; Get-CimInstance Win32_Process | ForEach-Object { ' +
|
|
328
|
-
'"$($_.ProcessId)`t$(if ($_.CreationDate) { $_.CreationDate.ToUniversalTime().ToString(\'o\') })`t$($_.CommandLine)" }';
|
|
329
|
+
'"$($_.ProcessId)`t$($_.ParentProcessId)`t$(if ($_.CreationDate) { $_.CreationDate.ToUniversalTime().ToString(\'o\') })`t$($_.CommandLine)" }';
|
|
329
330
|
execFile('powershell.exe', ['-NoProfile', '-NonInteractive', '-Command', script], options, (error, stdout) => {
|
|
330
331
|
if (error)
|
|
331
332
|
return reject(error);
|
|
332
333
|
resolve(String(stdout).split(/\r?\n/).map((line) => {
|
|
333
|
-
const [pid, created, ...command] = line.split('\t');
|
|
334
|
-
return { pid: Number(pid), startedAt: Date.parse(created), command: command.join('\t') };
|
|
334
|
+
const [pid, parentPid, created, ...command] = line.split('\t');
|
|
335
|
+
return { pid: Number(pid), parentPid: Number(parentPid), startedAt: Date.parse(created), command: command.join('\t') };
|
|
335
336
|
}).filter((p) => p.pid > 0 && p.command));
|
|
336
337
|
});
|
|
337
338
|
return;
|
|
338
339
|
}
|
|
339
|
-
execFile('ps', ['-A', '-ww', '-o', 'pid=,etime=,command='], options, (error, stdout) => {
|
|
340
|
+
execFile('ps', ['-A', '-ww', '-o', 'pid=,ppid=,etime=,command='], options, (error, stdout) => {
|
|
340
341
|
if (error)
|
|
341
342
|
return reject(error);
|
|
342
343
|
const now = Date.now();
|
|
343
344
|
resolve(String(stdout).split('\n').flatMap((line) => {
|
|
344
|
-
const match = line.match(/^\s*(\d+)\s+(\S+)\s+(.*)$/);
|
|
345
|
-
return match ? [{ pid: Number(match[1]), startedAt: now - elapsedSeconds(match[
|
|
345
|
+
const match = line.match(/^\s*(\d+)\s+(\d+)\s+(\S+)\s+(.*)$/);
|
|
346
|
+
return match ? [{ pid: Number(match[1]), parentPid: Number(match[2]), startedAt: now - elapsedSeconds(match[3]) * 1000, command: match[4] }] : [];
|
|
346
347
|
}));
|
|
347
348
|
});
|
|
348
349
|
});
|
|
@@ -374,7 +375,10 @@ function isDesktopCommanderEntry(file, packageNames) {
|
|
|
374
375
|
* dist/index.js (started by an MCP client, npx, or `remote`'s local MCP), not
|
|
375
376
|
* followed by `remote` / `setup` / `remove`, and not this report. A shell
|
|
376
377
|
* wrapping it names only a bin, which doesn't resolve, so it isn't counted
|
|
377
|
-
* twice.
|
|
378
|
+
* twice. The thread-pool bootstrap marks its child with the launcher's PID,
|
|
379
|
+
* so only that recognized pair counts once. An unmarked nested server counts
|
|
380
|
+
* separately; older unmarked bootstrap pairs cannot be distinguished from it.
|
|
381
|
+
* Only the count and the earliest start leave this function.
|
|
378
382
|
*/
|
|
379
383
|
async function desktopCommanderMcp() {
|
|
380
384
|
let processes;
|
|
@@ -385,18 +389,31 @@ async function desktopCommanderMcp() {
|
|
|
385
389
|
return { running: null, since: null, error: `could not list processes (${errorText(error)})` };
|
|
386
390
|
}
|
|
387
391
|
const packageNames = new Map();
|
|
388
|
-
const servers = processes.
|
|
392
|
+
const servers = processes.flatMap(({ pid, parentPid, startedAt, command }) => {
|
|
389
393
|
if (pid === process.pid)
|
|
390
|
-
return
|
|
394
|
+
return [];
|
|
391
395
|
const tokens = (command.match(/"[^"]*"|\S+/g) ?? []).map((token) => token.replace(/^"|"$/g, ''));
|
|
392
396
|
// The first token is the executable; the entry is any later one
|
|
393
397
|
const at = tokens.findIndex((token, i) => i > 0 && /(\.js|desktop-commander(\.cmd)?)$/i.test(token) &&
|
|
394
398
|
isDesktopCommanderEntry(token, packageNames));
|
|
395
|
-
|
|
399
|
+
if (at < 0 || NOT_THE_SERVER.has(tokens[at + 1] ?? ''))
|
|
400
|
+
return [];
|
|
401
|
+
const launcherPid = tokens.at(-1) === `${LAUNCHER_PID_ARGUMENT}${parentPid}` ? parentPid : null;
|
|
402
|
+
return [{ pid, parentPid, startedAt, launcherPid }];
|
|
403
|
+
});
|
|
404
|
+
const serversByPid = new Map(servers.map((p) => [p.pid, p]));
|
|
405
|
+
const counted = servers.filter((p) => {
|
|
406
|
+
const launcher = p.launcherPid === null ? undefined : serversByPid.get(p.launcherPid);
|
|
407
|
+
if (!launcher)
|
|
408
|
+
return true;
|
|
409
|
+
// A process that started later cannot have launched this child; its PID was reused.
|
|
410
|
+
return Number.isFinite(launcher.startedAt) && Number.isFinite(p.startedAt) &&
|
|
411
|
+
launcher.startedAt > p.startedAt;
|
|
396
412
|
});
|
|
397
|
-
|
|
413
|
+
// A grouped child may have the only known start time when its launcher has no timestamp.
|
|
414
|
+
const starts = servers.map((p) => p.startedAt).filter(Number.isFinite);
|
|
398
415
|
return {
|
|
399
|
-
running:
|
|
416
|
+
running: counted.length,
|
|
400
417
|
since: starts.length ? new Date(Math.min(...starts)).toISOString() : null,
|
|
401
418
|
};
|
|
402
419
|
}
|
|
@@ -183,7 +183,7 @@ export async function enrollMqttDevice(options) {
|
|
|
183
183
|
}
|
|
184
184
|
}
|
|
185
185
|
else {
|
|
186
|
-
console.
|
|
186
|
+
console.debug("[MQTT] Previous credentials cannot be identified on this backend; continuing with fresh enrollment. The old certificate may remain active.");
|
|
187
187
|
}
|
|
188
188
|
assertCurrent();
|
|
189
189
|
try {
|
|
@@ -214,7 +214,7 @@ export class MqttTransport {
|
|
|
214
214
|
}
|
|
215
215
|
const topic = `dc/mqtt/v1/users/${this.userId}/devices/${this.deviceId}/result-${originInstance}`;
|
|
216
216
|
// Buffer identity keeps concurrent publishes of the same call separate locally.
|
|
217
|
-
// need to verify that payload is correct and this is what expected on
|
|
217
|
+
// need to verify that payload is correct and this is what expected on other side.
|
|
218
218
|
const payload = Buffer.from(JSON.stringify({ call_id: callId }));
|
|
219
219
|
try {
|
|
220
220
|
await new Promise((resolve, reject) => {
|
|
@@ -245,7 +245,7 @@ export class MqttTransport {
|
|
|
245
245
|
messageId = packet.messageId;
|
|
246
246
|
};
|
|
247
247
|
//TODO will it stop whole mqtt transport or just this message?
|
|
248
|
-
// we should stop
|
|
248
|
+
// we should stop only this message if timeout, not the other messages.
|
|
249
249
|
const timer = setTimeout(() => finish(new Error("MQTT result acknowledgement timed out")), 5000);
|
|
250
250
|
client.once("close", onClose);
|
|
251
251
|
client.on("packetsend", onPacket);
|
|
@@ -154,7 +154,7 @@ export declare class RemoteChannel {
|
|
|
154
154
|
} | null>;
|
|
155
155
|
updateDevice(deviceId: string, updates: any): Promise<{
|
|
156
156
|
data: any[] | null;
|
|
157
|
-
error: import("@supabase/
|
|
157
|
+
error: import("@supabase/postgrest-js").PostgrestError | null;
|
|
158
158
|
}>;
|
|
159
159
|
createDevice(deviceData: DeviceData): Promise<{
|
|
160
160
|
data: any;
|
|
@@ -588,7 +588,7 @@ export class RemoteChannel {
|
|
|
588
588
|
throw new Error('Device session changed during MQTT startup; restart the connector');
|
|
589
589
|
}
|
|
590
590
|
};
|
|
591
|
-
void this.startMqtt(assertSessionStillActive).catch(() => console.
|
|
591
|
+
void this.startMqtt(assertSessionStillActive).catch(() => console.debug('[MQTT] Transport unavailable; Broadcast continues'));
|
|
592
592
|
await this.createChannel();
|
|
593
593
|
}
|
|
594
594
|
else {
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { randomUUID } from 'node:crypto';
|
|
2
|
-
import { sanitizeTransportEvent } from '@desktop-commander/telemetry-contract/transport';
|
|
3
2
|
import { captureTransport } from '../utils/capture.js';
|
|
4
3
|
const ID = /^[A-Za-z0-9_-]{1,128}$/;
|
|
5
4
|
const PROCESS_ID = randomUUID();
|
|
@@ -11,6 +10,8 @@ export function observeTransport(input) {
|
|
|
11
10
|
const callId = receipt ? input.payload?.call_id : input.callId;
|
|
12
11
|
const deviceId = input.deviceId;
|
|
13
12
|
const userId = input.userId;
|
|
13
|
+
if (typeof input.stage !== 'string' || !input.stage)
|
|
14
|
+
return null;
|
|
14
15
|
if (typeof callId !== 'string' || !ID.test(callId) ||
|
|
15
16
|
typeof deviceId !== 'string' || !ID.test(deviceId) ||
|
|
16
17
|
typeof userId !== 'string' || !ID.test(userId))
|
|
@@ -33,14 +34,14 @@ export function observeTransport(input) {
|
|
|
33
34
|
} : {}),
|
|
34
35
|
...(!receipt && typeof input.toolName === 'string' && input.toolName.length > 0 &&
|
|
35
36
|
input.toolName.length <= 128 ? { tool_name: input.toolName } : {}),
|
|
36
|
-
...(!receipt && input.operation ? { operation: input.operation } : {}),
|
|
37
|
-
...(operationId ? { operation_id: operationId } : {}),
|
|
37
|
+
...(!receipt && typeof input.operation === 'string' && input.operation ? { operation: input.operation } : {}),
|
|
38
|
+
...(typeof operationId === 'string' && operationId ? { operation_id: operationId } : {}),
|
|
38
39
|
observation_id: observationId,
|
|
39
40
|
source_process_id: PROCESS_ID,
|
|
40
41
|
schema_version: 1,
|
|
41
42
|
timestamp_utc,
|
|
42
43
|
};
|
|
43
|
-
void captureTransport(input.stage,
|
|
44
|
+
void captureTransport(input.stage, event, userId);
|
|
44
45
|
return event;
|
|
45
46
|
}
|
|
46
47
|
catch {
|
package/dist/server.js
CHANGED
|
@@ -979,6 +979,8 @@ server.setRequestHandler(ListToolsRequestSchema, async () => {
|
|
|
979
979
|
description: `
|
|
980
980
|
Terminate a running process by PID.
|
|
981
981
|
|
|
982
|
+
For a PID that start_process returned, it ends the whole command: the shell and every process the command started, as force_terminate does. Any other PID ends that process only. Sessions started with node:local have a negative PID; stop them with force_terminate.
|
|
983
|
+
|
|
982
984
|
Use with caution as this will forcefully terminate the specified process.
|
|
983
985
|
|
|
984
986
|
${CMD_PREFIX_DESCRIPTION}`,
|