mcp-ssh-manager 4.0.3 → 4.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. package/CHANGELOG.md +199 -0
  2. package/CLAUDE.md +17 -5
  3. package/Formula/ssh-manager.rb +2 -2
  4. package/INSTALLATION.md +4 -0
  5. package/README.md +41 -5
  6. package/brand/README.md +1 -0
  7. package/cli/codex.js +67 -0
  8. package/cli/guard.js +69 -0
  9. package/cli/lib/config.sh +102 -37
  10. package/cli/ssh-manager +13 -0
  11. package/cli/ssh-manager.js +1 -1
  12. package/desktop/electron/electron-builder.yml +3 -0
  13. package/desktop/electron/locale.mjs +34 -0
  14. package/desktop/electron/main.js +52 -65
  15. package/desktop/electron/native-menu.mjs +44 -0
  16. package/desktop/electron/native.zh-CN.json +58 -0
  17. package/desktop/electron/package-lock.json +15 -40
  18. package/desktop/electron/package.json +4 -1
  19. package/desktop/electron/smoke-test.mjs +11 -1
  20. package/dist/ui/app.css +1 -1
  21. package/dist/ui/app.js +123 -115
  22. package/docs/MIGRATION.md +1 -1
  23. package/docs/SECURITY_MODES.md +15 -8
  24. package/docs/SEO.md +1 -1
  25. package/llms-install.md +97 -0
  26. package/package.json +17 -3
  27. package/scripts/check-action-pins.mjs +57 -0
  28. package/scripts/test-upgrade-from-published.mjs +24 -2
  29. package/server.json +2 -2
  30. package/src/audit.js +6 -15
  31. package/src/background-job.js +44 -0
  32. package/src/backup-manager.js +24 -29
  33. package/src/config-loader.js +10 -1
  34. package/src/database-manager.js +13 -31
  35. package/src/dump-guard.js +79 -0
  36. package/src/filtered-agent.js +53 -0
  37. package/src/health-monitor.js +8 -4
  38. package/src/index.js +216 -21
  39. package/src/logger.js +3 -1
  40. package/src/quiet-stderr.js +15 -0
  41. package/src/ssh-guard.js +344 -0
  42. package/src/ssh-manager.js +71 -2
  43. package/src/tunnel-manager.js +14 -4
@@ -13,7 +13,8 @@ export const DB_TYPES = {
13
13
  // A single quote character as a constant, so shellQuote()'s escaping stays
14
14
  // readable under the repo's single-quote lint style.
15
15
  import { shellQuote } from './shell-quote.js';
16
- export { shellQuote };
16
+ import { guardedDump, guardedFeed, mongoArchivePath } from './dump-guard.js';
17
+ export { shellQuote, mongoArchivePath };
17
18
 
18
19
  /**
19
20
  * Build MySQL dump command
@@ -44,13 +45,8 @@ export function buildMySQLDumpCommand(options) {
44
45
  command += ` ${tables.map(shellQuote).join(' ')}`;
45
46
  }
46
47
 
47
- if (compress) {
48
- command += ` | gzip > ${shellQuote(outputFile)}`;
49
- } else {
50
- command += ` > ${shellQuote(outputFile)}`;
51
- }
52
-
53
- return command;
48
+ // A producer that fails must fail the whole command and leave no file: see dump-guard.js.
49
+ return guardedDump(command, outputFile, compress);
54
50
  }
55
51
 
56
52
  /**
@@ -87,13 +83,8 @@ export function buildPostgreSQLDumpCommand(options) {
87
83
 
88
84
  command += ` ${shellQuote(database)}`;
89
85
 
90
- if (compress) {
91
- command += ` | gzip > ${shellQuote(outputFile)}`;
92
- } else {
93
- command += ` > ${shellQuote(outputFile)}`;
94
- }
95
-
96
- return command;
86
+ // A producer that fails must fail the whole command and leave no file: see dump-guard.js.
87
+ return guardedDump(command, outputFile, compress);
97
88
  }
98
89
 
99
90
  /**
@@ -127,7 +118,7 @@ export function buildMongoDBDumpCommand(options) {
127
118
  command += ` --out ${shellQuote(outputDir)}`;
128
119
 
129
120
  if (compress) {
130
- command += ` && tar -czf ${shellQuote(outputDir + '.tar.gz')} -C "$(dirname ${shellQuote(outputDir)})" "$(basename ${shellQuote(outputDir)})"`;
121
+ command += ` && tar -czf ${shellQuote(mongoArchivePath(outputDir))} -C "$(dirname ${shellQuote(outputDir)})" "$(basename ${shellQuote(outputDir)})"`;
131
122
  command += ` && rm -rf ${shellQuote(outputDir)}`;
132
123
  }
133
124
 
@@ -147,22 +138,16 @@ export function buildMySQLImportCommand(options) {
147
138
  inputFile
148
139
  } = options;
149
140
 
150
- let command = '';
151
-
152
- if (inputFile.endsWith('.gz')) {
153
- command = `gunzip -c ${shellQuote(inputFile)} | `;
154
- } else {
155
- command = `cat ${shellQuote(inputFile)} | `;
156
- }
157
-
158
- command += 'mysql';
141
+ let command = 'mysql';
159
142
  if (user) command += ` -u${shellQuote(user)}`;
160
143
  if (password) command += ` -p${shellQuote(password)}`;
161
144
  if (host) command += ` -h ${shellQuote(host)}`;
162
145
  if (port) command += ` -P ${shellQuote(port)}`;
163
146
  command += ` ${shellQuote(database)}`;
164
147
 
165
- return command;
148
+ // A gzip file is verified whole before mysql reads a byte of it (dump-guard.js).
149
+ if (inputFile.endsWith('.gz')) return guardedFeed(inputFile, command);
150
+ return `cat ${shellQuote(inputFile)} | ${command}`;
166
151
  }
167
152
 
168
153
  /**
@@ -190,11 +175,8 @@ export function buildPostgreSQLImportCommand(options) {
190
175
  command += ' --clean --if-exists';
191
176
  command += ` -d ${shellQuote(database)}`;
192
177
 
193
- if (inputFile.endsWith('.gz')) {
194
- command = `gunzip -c ${shellQuote(inputFile)} | ${command}`;
195
- } else {
196
- command += ` ${shellQuote(inputFile)}`;
197
- }
178
+ if (inputFile.endsWith('.gz')) return guardedFeed(inputFile, command);
179
+ command += ` ${shellQuote(inputFile)}`;
198
180
 
199
181
  return command;
200
182
  }
@@ -0,0 +1,79 @@
1
+ // Backups and restores whose exit code means what it says.
2
+ //
3
+ // `mysqldump ... | gzip > out` reports the exit code of the LAST command of the
4
+ // pipeline: when the dump failed (wrong password, missing database, full disk),
5
+ // gzip still succeeded, the file was an empty archive of 20 bytes, and ssh_db_dump
6
+ // and ssh_backup_create answered success with it. The mirror image on the way in:
7
+ // `gunzip -c dump.gz | mysql` runs mysql on whatever gunzip managed to read, so a
8
+ // truncated archive was restored in part, without an error.
9
+ //
10
+ // Found by reading jaredshuai's fork, which hit the same two defects and fixed them
11
+ // with a temporary file; this keeps the dump streaming (a database can be bigger than
12
+ // the free space on its own disk) by recording the producer's exit status on the side.
13
+ //
14
+ // Both commands are one simple command of single-quoted words (sh -c '<script>' sh
15
+ // <args>), like withRemoteTimeout: the SSH server hands them to the account's login
16
+ // shell, which may be csh or tcsh and would reject `{ }` and `$?` at the top level.
17
+ // The producer and the file names are positional parameters, never spliced into the
18
+ // script, and the producer is the same quoted text that used to run as it was.
19
+ import { shellQuote } from './shell-quote.js';
20
+
21
+ // $1 = file to write, $2 = the producing command, $3 = 1 to compress.
22
+ // The producer's status goes to a file next to the output (no mktemp: it is not on
23
+ // every system, and the folder is known to be writable since the output goes there).
24
+ // A failed run leaves no output file at all, so nothing can be mistaken for a backup.
25
+ const GUARDED_DUMP_SCRIPT = [
26
+ 'out=$1; st="$1.status.$$"; trap \'rm -f "$st"\' EXIT;',
27
+ 'if [ "$3" = 1 ]; then',
28
+ '{ eval "$2"; echo $? > "$st"; } | gzip -c > "$out"; gz=$?;',
29
+ 'read status < "$st" || status=1;',
30
+ 'if [ "$status" != 0 ] || [ "$gz" != 0 ]; then rm -f "$out"; exit 1; fi;',
31
+ 'else eval "$2" > "$out" || { rm -f "$out"; exit 1; }; fi',
32
+ ].join(' ');
33
+
34
+ // $1 = the gzip file, $2 = the command that reads the decompressed stream.
35
+ // A real gzip file is tested whole before a byte reaches the consumer. A file that
36
+ // is not gzip at all (a plain dump a backup left under a .gz name when it was made
37
+ // without compression) is fed as it is, so it is not refused for its name.
38
+ const GUARDED_FEED_SCRIPT = [
39
+ 'f=$1;',
40
+ 'if [ "$(head -c 2 "$f" | od -An -tx1 | tr -d " \\n")" = 1f8b ];',
41
+ 'then gzip -t "$f" || exit 1; gunzip -c "$f" | eval "$2";',
42
+ 'else eval "$2" < "$f"; fi',
43
+ ].join(' ');
44
+
45
+ /**
46
+ * The command that runs `producer` and writes its output to `outputFile`
47
+ * (gzipped when `compress`), and fails, leaving no file, if the producer does.
48
+ * @param {string} producer - A command line already built from quoted words
49
+ * @param {string} outputFile
50
+ * @param {boolean} [compress]
51
+ * @returns {string}
52
+ */
53
+ export function guardedDump(producer, outputFile, compress = true) {
54
+ return `sh -c ${shellQuote(GUARDED_DUMP_SCRIPT)} sh ${shellQuote(outputFile)} ${shellQuote(producer)} ${compress ? '1' : '0'}`;
55
+ }
56
+
57
+ /**
58
+ * The command that feeds a gzip file to `consumer` once the whole file has been
59
+ * verified, and fails without running the consumer if it is corrupt.
60
+ * @param {string} gzipFile
61
+ * @param {string} consumer - A command line already built from quoted words
62
+ * @returns {string}
63
+ */
64
+ export function guardedFeed(gzipFile, consumer) {
65
+ return `sh -c ${shellQuote(GUARDED_FEED_SCRIPT)} sh ${shellQuote(gzipFile)} ${shellQuote(consumer)}`;
66
+ }
67
+
68
+ /**
69
+ * Where mongodump's output ends up when it is compressed: one archive beside the
70
+ * dump directory. The one place that knows it: the dump builders write it, and
71
+ * ssh_backup_create, ssh_backup_restore and ssh_db_dump measure, record and read the
72
+ * same path. They used to disagree (written as <id>.tar.gz, measured and restored as
73
+ * <id>.gz), so a MongoDB backup reported a size of 0 and could not be restored.
74
+ * @param {string} outputDir
75
+ * @returns {string}
76
+ */
77
+ export function mongoArchivePath(outputDir) {
78
+ return `${outputDir}.tar.gz`;
79
+ }
@@ -0,0 +1,53 @@
1
+ // Select one public identity while the original agent retains the private key
2
+ // and handles signing and any user authorization prompts.
3
+ import ssh2 from 'ssh2';
4
+ const { BaseAgent, utils } = ssh2;
5
+
6
+ /** @param {any} value @returns {Buffer} */
7
+ function publicBytes(value) {
8
+ const key = value && typeof value.getPublicSSH === 'function'
9
+ ? value : utils.parseKey(value);
10
+ if (key instanceof Error) throw key;
11
+ if (!key || Array.isArray(key)) throw new Error('Expected one SSH public key');
12
+ return key.getPublicSSH();
13
+ }
14
+
15
+ export class FilteredAgent extends BaseAgent {
16
+ /** @param {import('ssh2').BaseAgent} delegate @param {Buffer} publicKey */
17
+ constructor(delegate, publicKey) {
18
+ super();
19
+ this.delegate = delegate;
20
+ this.allowedKey = publicBytes(publicKey);
21
+ }
22
+
23
+ /** @param {function} callback */
24
+ getIdentities(callback) {
25
+ this.delegate.getIdentities((error, keys) => {
26
+ if (error) return callback(error);
27
+ let selected;
28
+ try {
29
+ selected = keys.find(key => publicBytes(key).equals(this.allowedKey));
30
+ if (!selected) throw new Error('Configured public key is not available in SSH Agent');
31
+ } catch (error) {
32
+ return callback(error);
33
+ }
34
+ callback(null, [selected]);
35
+ });
36
+ }
37
+
38
+ /** @param {any} publicKey @param {Buffer} data @param {any} options @param {function} [callback] */
39
+ sign(publicKey, data, options, callback) {
40
+ if (typeof options === 'function') {
41
+ callback = options;
42
+ options = undefined;
43
+ }
44
+ try {
45
+ if (!publicBytes(publicKey).equals(this.allowedKey)) {
46
+ throw new Error('SSH Agent signing identity does not match configured public key');
47
+ }
48
+ } catch (error) {
49
+ return callback(error);
50
+ }
51
+ this.delegate.sign(publicKey, data, options, callback);
52
+ }
53
+ }
@@ -48,7 +48,7 @@ function buildMemoryCheckCommand() {
48
48
  function buildDiskCheckCommand(mountPoint = '/') {
49
49
  // Returns JSON with disk usage for specific mount point or all
50
50
  if (mountPoint === 'all') {
51
- return 'df -h | awk \'NR>1 {gsub(/%/,"",$5); printf "{\\"mount\\":\\"%s\\",\\"size\\":\\"%s\\",\\"used\\":\\"%s\\",\\"avail\\":\\"%s\\",\\"percent\\":%s}\\n", $6,$2,$3,$4,$5}\'';
51
+ return 'LC_ALL=C df -PTh | awk \'NR>1 {gsub(/%/,"",$6); printf "{\\"filesystem\\":\\"%s\\",\\"mount\\":\\"%s\\",\\"size\\":\\"%s\\",\\"used\\":\\"%s\\",\\"avail\\":\\"%s\\",\\"percent\\":%s}\\n", $2,$7,$3,$4,$5,$6}\'';
52
52
  }
53
53
  return `df -h "${mountPoint}" | awk 'NR>1 {gsub(/%/,"",$5); printf "{\\"mount\\":\\"%s\\",\\"size\\":\\"%s\\",\\"used\\":\\"%s\\",\\"avail\\":\\"%s\\",\\"percent\\":%s}", $6,$2,$3,$4,$5}'`;
54
54
  }
@@ -116,6 +116,10 @@ function parseDiskUsage(output) {
116
116
  for (const line of lines) {
117
117
  try {
118
118
  const disk = JSON.parse(line);
119
+ // Container layers share the backing disk's free space. RAM-backed and
120
+ // read-only image mounts are not independent persistent disks either.
121
+ // Keep / even when the monitored host itself runs inside a container.
122
+ if (disk.mount !== '/' && ['overlay', 'aufs', 'tmpfs', 'devtmpfs', 'squashfs'].includes(disk.filesystem)) continue;
119
123
  disk.status = disk.percent > 90 ? HEALTH_STATUS.CRITICAL : disk.percent > 80 ? HEALTH_STATUS.WARNING : HEALTH_STATUS.HEALTHY;
120
124
  disks.push(disk);
121
125
  } catch (error) {
@@ -322,7 +326,7 @@ export function buildLoadAlertConfigCommand(configPath = '/etc/ssh-manager-alert
322
326
  export function checkAlertThresholds(metrics, thresholds) {
323
327
  const alerts = [];
324
328
 
325
- if (thresholds.cpu && metrics.cpu && metrics.cpu.percent > thresholds.cpu) {
329
+ if (thresholds.cpu && metrics.cpu && metrics.cpu.percent >= thresholds.cpu) {
326
330
  alerts.push({
327
331
  type: 'cpu',
328
332
  severity: 'warning',
@@ -332,7 +336,7 @@ export function checkAlertThresholds(metrics, thresholds) {
332
336
  });
333
337
  }
334
338
 
335
- if (thresholds.memory && metrics.memory && metrics.memory.percent > thresholds.memory) {
339
+ if (thresholds.memory && metrics.memory && metrics.memory.percent >= thresholds.memory) {
336
340
  alerts.push({
337
341
  type: 'memory',
338
342
  severity: 'warning',
@@ -344,7 +348,7 @@ export function checkAlertThresholds(metrics, thresholds) {
344
348
 
345
349
  if (thresholds.disk && metrics.disks) {
346
350
  for (const disk of metrics.disks) {
347
- if (disk.percent > thresholds.disk) {
351
+ if (disk.percent >= thresholds.disk) {
348
352
  alerts.push({
349
353
  type: 'disk',
350
354
  severity: 'warning',