mcp-ssh-manager 4.4.2 → 4.4.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/CHANGELOG.md CHANGED
@@ -7,6 +7,28 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ## [4.4.3] - 2026-11-28
11
+
12
+ **A security release: upgrade if an agent can call the database, backup or deploy tools on a server you care about.**
13
+ A database name that starts with `-` was one correctly quoted word and still an option to the database client
14
+ ([GHSA-mwc5-p6j7-hh4q](https://github.com/bvisible/mcp-ssh-manager/security/advisories/GHSA-mwc5-p6j7-hh4q),
15
+ high, reported by @usdnr-dotcom). `--execute=…` makes `mysql` ignore its standard input, which is where the
16
+ read-only transaction of `ssh_db_query` is sent, so the statement ran with write access; `--init-command=…` runs
17
+ before anything on standard input, with the same result; `--host=…` sends the connection, and its password,
18
+ elsewhere. `ssh_db_list`, which is allowed on `readonly` and `restricted` servers, takes the same name. Releases
19
+ from 3.0.0, which added the database tools, are affected.
20
+
21
+ ### Fixed
22
+
23
+ - **A tool argument that starts with `-` is refused wherever a command would read it as an option.** Quoting keeps the shell from interpreting a value; it cannot keep the program from doing so. The database tools (`ssh_db_query`, `ssh_db_list`, `ssh_db_dump`, `ssh_db_import`, and the backup, restore and scheduled-backup tools) refuse such a database name, table, import or restore file and backup directory; the file backup refuses such a path (tar's `--checkpoint-action` runs a command); `ssh_deploy` refuses such a target; `ssh_service_status` such a service name; and `ssh_tail` such a file. A relative path can still be written `./-name`.
24
+ - **A PostgreSQL or MongoDB database name that is a connection string is refused too.** `psql`, `pg_dump` and `pg_restore` read a name containing `=` or starting with `postgresql://` as a whole connection string, and the mongo shell reads `host/name`: either could send the connection, and the password, to another host.
25
+ - **Control characters (a line break, NUL, an escape) are refused in the same values.**
26
+
27
+ ### Changed
28
+
29
+ - A `chmod` mode or `chown` owner given to `ssh_deploy` is now read as a value, never as an option (`chmod -- -w file` works). A `grep` filter given to `ssh_tail` or `ssh_process_manager` that starts with `-` is now a pattern, not an option (`grep -e`).
30
+ - What does not change: a database, table, file or directory name that does not start with `-` builds exactly the command it built before.
31
+
10
32
  ## [4.4.2] - 2026-11-28
11
33
 
12
34
  **A session starts again on CentOS 7.** 4.4.1 broke it there; found, traced and fixed by @nethinwei (#163, #180). Nothing else changes.
@@ -13,8 +13,8 @@
13
13
  class SshManager < Formula
14
14
  desc "SSH server management for AI agents, with approval and audit"
15
15
  homepage "https://github.com/bvisible/mcp-ssh-manager"
16
- url "https://registry.npmjs.org/mcp-ssh-manager/-/mcp-ssh-manager-4.4.1.tgz"
17
- sha256 "7b699c25345a95f5ef3388344c149a456fa8dd3b5b23388aa7e7b0374e496bc8"
16
+ url "https://registry.npmjs.org/mcp-ssh-manager/-/mcp-ssh-manager-4.4.2.tgz"
17
+ sha256 "942ebe3c3f3993ddee2e983eb54d779d675094ff28817f9708c4796b73a3ebf4"
18
18
  license "MIT"
19
19
 
20
20
  depends_on "node"
package/README.md CHANGED
@@ -25,7 +25,7 @@
25
25
  [![Desktop app downloads](https://img.shields.io/endpoint?url=https%3A%2F%2Fraw.githubusercontent.com%2Fbvisible%2Fmcp-ssh-manager%2Fstats%2Fdesktop.json&style=flat-square)](https://github.com/bvisible/mcp-ssh-manager/releases/latest)
26
26
  [![GitHub stars](https://img.shields.io/github/stars/bvisible/mcp-ssh-manager?style=flat-square&color=c04500)](https://github.com/bvisible/mcp-ssh-manager/stargazers)
27
27
  [![MCP Toplist](https://mcptoplist.com/badge/io.github.bvisible%2Fmcp-ssh-manager.svg)](https://mcptoplist.com/server/io.github.bvisible%2Fmcp-ssh-manager)
28
- [![Version](https://img.shields.io/badge/release-4.4.2-c04500?style=flat-square)](https://github.com/bvisible/mcp-ssh-manager/releases/tag/v4.4.2)
28
+ [![Version](https://img.shields.io/badge/release-4.4.3-c04500?style=flat-square)](https://github.com/bvisible/mcp-ssh-manager/releases/tag/v4.4.3)
29
29
  [![Claude Code](https://img.shields.io/badge/Claude_Code-Compatible-5A67D8?style=flat-square&logo=anthropic)](https://claude.ai/code)
30
30
  [![OpenAI Codex](https://img.shields.io/badge/OpenAI_Codex-Compatible-00A67E?style=flat-square&logo=openai)](https://openai.com/codex)
31
31
  [![MCP](https://img.shields.io/badge/MCP-Server-orange?style=flat-square)](https://modelcontextprotocol.io)
@@ -58,9 +58,9 @@ An MCP SSH server is the most dangerous tool you can hand an agent: a shell on m
58
58
 
59
59
  ## Choose your workspace
60
60
 
61
- **Desktop application (`4.4.2`):** one installation with its own runtime,
61
+ **Desktop application (`4.4.3`):** one installation with its own runtime,
62
62
  terminal, file browser and server settings. No separate Node.js or npm
63
- installation. [Download it](https://github.com/bvisible/mcp-ssh-manager/releases/tag/v4.4.2):
63
+ installation. [Download it](https://github.com/bvisible/mcp-ssh-manager/releases/tag/v4.4.3):
64
64
 
65
65
  | Platform | File | Signature |
66
66
  |---|---|---|
@@ -68,7 +68,7 @@ installation. [Download it](https://github.com/bvisible/mcp-ssh-manager/releases
68
68
  | Windows x64 / arm64 | `…-setup.exe` | **Not code-signed yet.** Windows shows *"Windows protected your PC"*: *More info → Run anyway*. Check the file against the SHA-256 in `release-manifest-win32.json` on the release (`ssh-manager app` does it for you). Signing comes in a later release. |
69
69
  | Linux x64 | `.deb`, `.AppImage` | Unsigned, as Linux packages usually are |
70
70
 
71
- **Already using npm / the CLI?** `npm update -g mcp-ssh-manager` brings 4.4.2,
71
+ **Already using npm / the CLI?** `npm update -g mcp-ssh-manager` brings 4.4.3,
72
72
  and the V4 engine runs without an interface, from your existing `.env`, TOML or
73
73
  process variables — no automatic import, no forced wizard, no approval prompt
74
74
  unless you enable one per server. The one behaviour that tightens is **SSH
@@ -722,6 +722,8 @@ The hero animation ships twice, light and dark, swapped by `<picture>` on `prefe
722
722
 
723
723
  ## What's new
724
724
 
725
+ **[4.4.3](https://github.com/bvisible/mcp-ssh-manager/releases/tag/v4.4.3) — a security fix: a database name that starts with `-` was read as an option by the database client.** A name such as `--execute=…` or `--init-command=…`, passed to a database tool, reached `mysql` as an option and bypassed the read-only transaction ([GHSA-mwc5-p6j7-hh4q](https://github.com/bvisible/mcp-ssh-manager/security/advisories/GHSA-mwc5-p6j7-hh4q), high). Every value a tool puts in a position where a command can read it as an option now refuses a leading `-`: database names, tables, import and backup files, tar paths, backup directories, the deploy target, the service name and the file of `ssh_tail`. Reported by @usdnr-dotcom.
726
+
725
727
  **[4.4.2](https://github.com/bvisible/mcp-ssh-manager/releases/tag/v4.4.2) — a session starts again on CentOS 7, and on a host that takes seconds to answer.** 4.4.1 sent a new session's set-up in a way bash 4.2 drops, so no session started on CentOS 7, and it gave the shell only five seconds to answer, too short behind a bastion. The set-up now goes on one line and the session waits up to thirty seconds. Found, traced and fixed by @nethinwei (#163, #180).
726
728
 
727
729
  **[4.4.1](https://github.com/bvisible/mcp-ssh-manager/releases/tag/v4.4.1) — sessions read what the shell answers, and a session command shows on the Live page.** Sending a command into a session reported it as failed on a server that echoes its input (a bastion that ignores the request to turn echo off), kept the old working directory after `cd` and `export`, and returned prompts and escape codes in the output. It now reads the shell's own answer, and the commands you send into a session appear on the Live page and in the history as `ssh_execute` ones do. Reported and traced to the line by @nethinwei (#163).
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "ssh-manager-desktop",
3
- "version": "4.4.2",
3
+ "version": "4.4.3",
4
4
  "lockfileVersion": 3,
5
5
  "requires": true,
6
6
  "packages": {
7
7
  "": {
8
8
  "name": "ssh-manager-desktop",
9
- "version": "4.4.2",
9
+ "version": "4.4.3",
10
10
  "license": "MIT",
11
11
  "dependencies": {
12
12
  "electron-updater": "^6.8.9",
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "ssh-manager-desktop",
3
3
  "productName": "SSH Manager",
4
- "version": "4.4.2",
4
+ "version": "4.4.3",
5
5
  "private": true,
6
6
  "description": "The SSH Manager control plane as a desktop application",
7
7
  "main": "main.js",
package/docs/MIGRATION.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Upgrading from 3.8 to 4.0
2
2
 
3
- **4.0 is released, on npm and as a desktop application**; the latest is 4.4.2 ([release](https://github.com/bvisible/mcp-ssh-manager/releases/tag/v4.4.2)).
3
+ **4.0 is released, on npm and as a desktop application**; the latest is 4.4.3 ([release](https://github.com/bvisible/mcp-ssh-manager/releases/tag/v4.4.3)).
4
4
  The last 3.8 release is 3.8.7, for anyone who pins the 3.8 line.
5
5
 
6
6
  The upgrade path preserves headless npm/CLI use. The application, vault import
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mcp-ssh-manager",
3
- "version": "4.4.2",
3
+ "version": "4.4.3",
4
4
  "mcpName": "io.github.bvisible/mcp-ssh-manager",
5
5
  "description": "MCP server for SSH: let AI agents (Claude Code, Codex, any MCP client) run commands, transfer files and manage servers over SSH, with human approval, an encrypted vault and a live audit trail.",
6
6
  "main": "src/index.js",
@@ -14,7 +14,7 @@
14
14
  "setup": "npm install && pip install -r tools/requirements.txt",
15
15
  "configure": "python tools/server-manager.py",
16
16
  "test-connection": "python tools/test-connection.py",
17
- "test": "npm run test:lockfile && npm run test:sudostdin && npm run test:vault && npm run test:recovery && npm run test:upgrade && npm run test:notify && npm run test:commands && npm run test:commandlog && npm run test:portability && npm run test:approval && npm run test:controlplane && npm run test:terminal && npm run test:files && npm run test:livestream && npm run test:livefailure && npm run test:sessionshell && npm run test:sessionlive && npm run test:transferpaths && npm run test:skills && npm run test:redaction && npm run test:profiles && npm run test:aliases && npm run test:hooks && npm run test:tools && npm run test:policy && npm run test:shellsegments && npm run test:policynames && npm run test:auditsuccess && npm run test:ping && npm run test:config && npm run test:fieldnames && npm run test:groups && npm run test:agentforward && npm run test:agentkey && npm run test:certpath && npm run test:agentannounce && npm run test:autherrors && npm run test:envchoice && npm run test:projectenv && npm run test:lifecycle && npm run test:tunnel && npm run test:sync && npm run test:sync-path && npm run test:dbquoting && npm run test:dbinjection && npm run test:quoteprops && npm run test:backupinjection && npm run test:dbrowcount && npm run test:dbclientcmds && npm run test:migration && npm run test:security && npm run test:hostkeys && npm run test:proxy && npm run test:working-directory && npm run test:groups-persistence && npm run test:release && npm run test:cli-experience && npm run test:desktop-install && npm run test:agent-clients && npm run test:appupdate && npm run test:preview && npm run test:desktop-smoke && npm run test:local-shell && npm run test:windowclose && npm run test:cli-edit && npm run test:timeoutwrap && npm run test:downloadstats && npm run test:remotetunnel && npm run test:connsharing && npm run test:auditcoverage && npm run test:background && npm run test:quietstderr && npm run test:sshguard && npm run test:locale && npm run test:langtool && npm run test:healthdisks && npm run test:backupintegrity && npm run test:clienvlines && npm run test:clicodex",
17
+ "test": "npm run test:lockfile && npm run test:sudostdin && npm run test:vault && npm run test:recovery && npm run test:upgrade && npm run test:notify && npm run test:commands && npm run test:commandlog && npm run test:portability && npm run test:approval && npm run test:controlplane && npm run test:terminal && npm run test:files && npm run test:livestream && npm run test:livefailure && npm run test:sessionshell && npm run test:sessionlive && npm run test:transferpaths && npm run test:skills && npm run test:redaction && npm run test:profiles && npm run test:aliases && npm run test:hooks && npm run test:tools && npm run test:policy && npm run test:shellsegments && npm run test:policynames && npm run test:auditsuccess && npm run test:ping && npm run test:config && npm run test:fieldnames && npm run test:groups && npm run test:agentforward && npm run test:agentkey && npm run test:certpath && npm run test:agentannounce && npm run test:autherrors && npm run test:envchoice && npm run test:projectenv && npm run test:lifecycle && npm run test:tunnel && npm run test:sync && npm run test:sync-path && npm run test:dbquoting && npm run test:dbinjection && npm run test:argumentinjection && npm run test:quoteprops && npm run test:backupinjection && npm run test:dbrowcount && npm run test:dbclientcmds && npm run test:migration && npm run test:security && npm run test:hostkeys && npm run test:proxy && npm run test:working-directory && npm run test:groups-persistence && npm run test:release && npm run test:cli-experience && npm run test:desktop-install && npm run test:agent-clients && npm run test:appupdate && npm run test:preview && npm run test:desktop-smoke && npm run test:local-shell && npm run test:windowclose && npm run test:cli-edit && npm run test:timeoutwrap && npm run test:downloadstats && npm run test:remotetunnel && npm run test:connsharing && npm run test:auditcoverage && npm run test:background && npm run test:quietstderr && npm run test:sshguard && npm run test:locale && npm run test:langtool && npm run test:healthdisks && npm run test:backupintegrity && npm run test:clienvlines && npm run test:clicodex",
18
18
  "test:lockfile": "node tests/test-lockfile.js",
19
19
  "test:sudostdin": "node tests/test-sudo-stdin.js",
20
20
  "test:vault": "node tests/test-secret-store.js",
@@ -51,6 +51,7 @@
51
51
  "test:sync-path": "node tests/test-rsync-path.js",
52
52
  "test:dbquoting": "node tests/test-database-query-quoting.js",
53
53
  "test:dbinjection": "node tests/test-database-injection.js",
54
+ "test:argumentinjection": "node tests/test-argument-injection.js",
54
55
  "test:quoteprops": "node tests/test-shell-quote-properties.js",
55
56
  "test:backupinjection": "node tests/test-backup-monitor-injection.js",
56
57
  "test:dbrowcount": "node tests/test-database-row-count.js",
package/server.json CHANGED
@@ -6,12 +6,12 @@
6
6
  "url": "https://github.com/bvisible/mcp-ssh-manager",
7
7
  "source": "github"
8
8
  },
9
- "version": "4.4.2",
9
+ "version": "4.4.3",
10
10
  "packages": [
11
11
  {
12
12
  "registryType": "npm",
13
13
  "identifier": "mcp-ssh-manager",
14
- "version": "4.4.2",
14
+ "version": "4.4.3",
15
15
  "transport": {
16
16
  "type": "stdio"
17
17
  },
@@ -7,7 +7,7 @@
7
7
  import path from 'path';
8
8
  import crypto from 'crypto';
9
9
  import { logger } from './logger.js';
10
- import { shellQuote, safeInteger } from './shell-quote.js';
10
+ import { shellQuote, safeInteger, positional, databaseName } from './shell-quote.js';
11
11
  import { guardedDump, guardedFeed, mongoArchivePath } from './dump-guard.js';
12
12
 
13
13
  // Backup types
@@ -35,7 +35,7 @@ export function generateBackupId(type, name) {
35
35
  * Get backup metadata file path
36
36
  */
37
37
  export function getBackupMetadataPath(backupId, backupDir = DEFAULT_BACKUP_DIR) {
38
- return path.join(backupDir, `${backupId}.meta.json`);
38
+ return path.join(positional(backupDir, 'backupDir'), `${backupId}.meta.json`);
39
39
  }
40
40
 
41
41
  /**
@@ -81,14 +81,14 @@ export function buildReadMetadataCommand(backupId, backupDir = DEFAULT_BACKUP_DI
81
81
  * @returns {string}
82
82
  */
83
83
  export function buildEnsureBackupDirCommand(backupDir = DEFAULT_BACKUP_DIR) {
84
- return `mkdir -p ${shellQuote(backupDir)}`;
84
+ return `mkdir -p ${shellQuote(positional(backupDir, 'backupDir'))}`;
85
85
  }
86
86
 
87
87
  /**
88
88
  * Get backup file path
89
89
  */
90
90
  export function getBackupFilePath(backupId, backupDir = DEFAULT_BACKUP_DIR, extension = '.gz') {
91
- return path.join(backupDir, `${backupId}${extension}`);
91
+ return path.join(positional(backupDir, 'backupDir'), `${backupId}${extension}`);
92
92
  }
93
93
 
94
94
  /**
@@ -131,7 +131,7 @@ export function buildMySQLDumpCommand(options) {
131
131
  command += ' --routines --triggers';
132
132
 
133
133
  // Database name
134
- command += ` ${shellQuote(database)}`;
134
+ command += ` ${shellQuote(databaseName(database, 'database', 'mysql'))}`;
135
135
 
136
136
  // Output handling
137
137
  // A producer that fails must fail the whole command and leave no file: see dump-guard.js.
@@ -169,7 +169,7 @@ export function buildPostgreSQLDumpCommand(options) {
169
169
  command += ' --format=custom --clean --if-exists';
170
170
 
171
171
  // Database name
172
- command += ` ${shellQuote(database)}`;
172
+ command += ` ${shellQuote(databaseName(database, 'database', 'postgresql'))}`;
173
173
 
174
174
  // Output handling
175
175
  // A producer that fails must fail the whole command and leave no file: see dump-guard.js.
@@ -201,8 +201,8 @@ export function buildMongoDBDumpCommand(options) {
201
201
  // Database selection
202
202
  if (database) command += ` --db ${shellQuote(database)}`;
203
203
 
204
- // Output directory
205
- command += ` --out ${shellQuote(outputDir)}`;
204
+ // Output directory (also read positionally by the commands that follow)
205
+ command += ` --out ${shellQuote(positional(outputDir, 'outputDir'))}`;
206
206
 
207
207
  // Compress the output directory
208
208
  if (compress) {
@@ -249,7 +249,7 @@ export function buildFilesBackupCommand(options) {
249
249
  }
250
250
 
251
251
  // Paths to backup
252
- command += ` ${paths.map(p => shellQuote(p)).join(' ')}`;
252
+ command += ` ${paths.map(p => shellQuote(positional(p, 'path'))).join(' ')}`;
253
253
 
254
254
  return command;
255
255
  }
@@ -293,11 +293,11 @@ function buildMySQLRestoreCommand(backupFile, options) {
293
293
  if (password) command += ` -p${shellQuote(password)}`;
294
294
  if (host) command += ` -h ${shellQuote(host)}`;
295
295
  if (port) command += ` -P ${safeInteger(port, 3306)}`;
296
- if (database) command += ` ${shellQuote(database)}`;
296
+ if (database) command += ` ${shellQuote(databaseName(database, 'database', 'mysql'))}`;
297
297
 
298
298
  // A gzip file is verified whole before mysql reads a byte of it (dump-guard.js).
299
299
  if (backupFile.endsWith('.gz')) return guardedFeed(backupFile, command);
300
- return `cat ${shellQuote(backupFile)} | ${command}`;
300
+ return `cat ${shellQuote(positional(backupFile, 'backupFile'))} | ${command}`;
301
301
  }
302
302
 
303
303
  /**
@@ -323,14 +323,14 @@ function buildPostgreSQLRestoreCommand(backupFile, options) {
323
323
  if (user) command += ` -U ${shellQuote(user)}`;
324
324
  if (host) command += ` -h ${shellQuote(host)}`;
325
325
  if (port) command += ` -p ${safeInteger(port, 5432)}`;
326
- if (database) command += ` -d ${shellQuote(database)}`;
326
+ if (database) command += ` -d ${shellQuote(databaseName(database, 'database', 'postgresql'))}`;
327
327
 
328
328
  // Restore options
329
329
  command += ' --clean --if-exists';
330
330
 
331
331
  // Handle compressed files
332
332
  if (backupFile.endsWith('.gz')) return guardedFeed(backupFile, command);
333
- command += ` ${shellQuote(backupFile)}`;
333
+ command += ` ${shellQuote(positional(backupFile, 'backupFile'))}`;
334
334
 
335
335
  return command;
336
336
  }
@@ -352,6 +352,7 @@ function buildMongoDBRestoreCommand(backupFile, options) {
352
352
  // Extract if compressed
353
353
  if (backupFile.endsWith('.tar.gz')) {
354
354
  const extractDir = backupFile.replace('.tar.gz', '');
355
+ positional(backupFile, 'backupFile');
355
356
  command = `tar -xzf ${shellQuote(backupFile)} -C "$(dirname ${shellQuote(backupFile)})" && `;
356
357
  command += 'mongorestore';
357
358
 
@@ -361,7 +362,7 @@ function buildMongoDBRestoreCommand(backupFile, options) {
361
362
  if (user) command += ` --username ${shellQuote(user)}`;
362
363
  if (password) command += ` --password ${shellQuote(password)}`;
363
364
 
364
- command += ` ${shellQuote(extractDir)}`;
365
+ command += ` ${shellQuote(positional(extractDir, 'backupFile'))}`;
365
366
  command += ` && rm -rf ${shellQuote(extractDir)}`;
366
367
  } else {
367
368
  command = 'mongorestore';
@@ -370,7 +371,7 @@ function buildMongoDBRestoreCommand(backupFile, options) {
370
371
  if (port) command += ` --port ${safeInteger(port, 27017)}`;
371
372
  if (user) command += ` --username ${shellQuote(user)}`;
372
373
  if (password) command += ` --password ${shellQuote(password)}`;
373
- command += ` ${shellQuote(backupFile)}`;
374
+ command += ` ${shellQuote(positional(backupFile, 'backupFile'))}`;
374
375
  }
375
376
 
376
377
  return command;
@@ -431,7 +432,7 @@ export function buildSaveMetadataCommand(metadata, metadataPath) {
431
432
  * Build command to list backups from remote server
432
433
  */
433
434
  export function buildListBackupsCommand(backupDir = DEFAULT_BACKUP_DIR, type = null) {
434
- let command = `find ${shellQuote(backupDir)} -name '*.meta.json' -type f`;
435
+ let command = `find ${shellQuote(positional(backupDir, 'backupDir'))} -name '*.meta.json' -type f`;
435
436
 
436
437
  if (type) {
437
438
  command += ` | grep ${shellQuote(`${type}_`)}`;
@@ -472,7 +473,7 @@ export function parseBackupsList(output) {
472
473
  */
473
474
  export function buildCleanupCommand(backupDir = DEFAULT_BACKUP_DIR, retentionDays = 7) {
474
475
  // Find backup files older than retention period and delete them
475
- return `find ${shellQuote(backupDir)} -name '*_*_*' -type f -mtime +${safeInteger(retentionDays, 7)} -delete`;
476
+ return `find ${shellQuote(positional(backupDir, 'backupDir'))} -name '*_*_*' -type f -mtime +${safeInteger(retentionDays, 7)} -delete`;
476
477
  }
477
478
 
478
479
  /**
@@ -12,7 +12,7 @@ export const DB_TYPES = {
12
12
 
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
- import { shellQuote } from './shell-quote.js';
15
+ import { shellQuote, positional, databaseName } from './shell-quote.js';
16
16
  import { guardedDump, guardedFeed, mongoArchivePath } from './dump-guard.js';
17
17
  export { shellQuote, mongoArchivePath };
18
18
 
@@ -39,10 +39,10 @@ export function buildMySQLDumpCommand(options) {
39
39
  if (port) command += ` -P ${shellQuote(port)}`;
40
40
 
41
41
  command += ' --single-transaction --routines --triggers';
42
- command += ` ${shellQuote(database)}`;
42
+ command += ` ${shellQuote(databaseName(database, 'database', 'mysql'))}`;
43
43
 
44
44
  if (tables && Array.isArray(tables)) {
45
- command += ` ${tables.map(shellQuote).join(' ')}`;
45
+ command += ` ${tables.map(table => shellQuote(positional(table, 'table'))).join(' ')}`;
46
46
  }
47
47
 
48
48
  // A producer that fails must fail the whole command and leave no file: see dump-guard.js.
@@ -81,7 +81,7 @@ export function buildPostgreSQLDumpCommand(options) {
81
81
  }
82
82
  }
83
83
 
84
- command += ` ${shellQuote(database)}`;
84
+ command += ` ${shellQuote(databaseName(database, 'database', 'postgresql'))}`;
85
85
 
86
86
  // A producer that fails must fail the whole command and leave no file: see dump-guard.js.
87
87
  return guardedDump(command, outputFile, compress);
@@ -143,11 +143,11 @@ export function buildMySQLImportCommand(options) {
143
143
  if (password) command += ` -p${shellQuote(password)}`;
144
144
  if (host) command += ` -h ${shellQuote(host)}`;
145
145
  if (port) command += ` -P ${shellQuote(port)}`;
146
- command += ` ${shellQuote(database)}`;
146
+ command += ` ${shellQuote(databaseName(database, 'database', 'mysql'))}`;
147
147
 
148
148
  // A gzip file is verified whole before mysql reads a byte of it (dump-guard.js).
149
149
  if (inputFile.endsWith('.gz')) return guardedFeed(inputFile, command);
150
- return `cat ${shellQuote(inputFile)} | ${command}`;
150
+ return `cat ${shellQuote(positional(inputFile, 'inputFile'))} | ${command}`;
151
151
  }
152
152
 
153
153
  /**
@@ -173,10 +173,10 @@ export function buildPostgreSQLImportCommand(options) {
173
173
  if (host) command += ` -h ${shellQuote(host)}`;
174
174
  if (port) command += ` -p ${shellQuote(port)}`;
175
175
  command += ' --clean --if-exists';
176
- command += ` -d ${shellQuote(database)}`;
176
+ command += ` -d ${shellQuote(databaseName(database, 'database', 'postgresql'))}`;
177
177
 
178
178
  if (inputFile.endsWith('.gz')) return guardedFeed(inputFile, command);
179
- command += ` ${shellQuote(inputFile)}`;
179
+ command += ` ${shellQuote(positional(inputFile, 'inputFile'))}`;
180
180
 
181
181
  return command;
182
182
  }
@@ -198,6 +198,7 @@ export function buildMongoDBRestoreCommand(options) {
198
198
 
199
199
  if (inputPath.endsWith('.tar.gz')) {
200
200
  const extractDir = inputPath.replace('.tar.gz', '');
201
+ positional(inputPath, 'inputPath');
201
202
  command = `tar -xzf ${shellQuote(inputPath)} -C "$(dirname ${shellQuote(inputPath)})" && `;
202
203
  command += 'mongorestore';
203
204
  if (drop) command += ' --drop';
@@ -205,7 +206,7 @@ export function buildMongoDBRestoreCommand(options) {
205
206
  if (port) command += ` --port ${shellQuote(port)}`;
206
207
  if (user) command += ` --username ${shellQuote(user)}`;
207
208
  if (password) command += ` --password ${shellQuote(password)}`;
208
- command += ` ${shellQuote(extractDir)}`;
209
+ command += ` ${shellQuote(positional(extractDir, 'inputPath'))}`;
209
210
  command += ` && rm -rf ${shellQuote(extractDir)}`;
210
211
  } else {
211
212
  command = 'mongorestore';
@@ -214,7 +215,7 @@ export function buildMongoDBRestoreCommand(options) {
214
215
  if (port) command += ` --port ${shellQuote(port)}`;
215
216
  if (user) command += ` --username ${shellQuote(user)}`;
216
217
  if (password) command += ` --password ${shellQuote(password)}`;
217
- command += ` ${shellQuote(inputPath)}`;
218
+ command += ` ${shellQuote(positional(inputPath, 'inputPath'))}`;
218
219
  }
219
220
 
220
221
  return command;
@@ -250,7 +251,7 @@ export function buildMySQLListTablesCommand(options) {
250
251
  // Pass the database as a shell-quoted positional argument instead of
251
252
  // interpolating it into the -e SQL, so the name can inject neither the
252
253
  // shell nor the SQL (a crafted USE clause).
253
- command += ` ${shellQuote(database)} -e "SHOW TABLES;" | tail -n +2`;
254
+ command += ` ${shellQuote(databaseName(database, 'database', 'mysql'))} -e "SHOW TABLES;" | tail -n +2`;
254
255
 
255
256
  return command;
256
257
  }
@@ -290,7 +291,7 @@ export function buildPostgreSQLListTablesCommand(options) {
290
291
  if (user) command += ` -U ${shellQuote(user)}`;
291
292
  if (host) command += ` -h ${shellQuote(host)}`;
292
293
  if (port) command += ` -p ${shellQuote(port)}`;
293
- command += ` -d ${shellQuote(database)}`;
294
+ command += ` -d ${shellQuote(databaseName(database, 'database', 'postgresql'))}`;
294
295
  command += ' -t -c "SELECT tablename FROM pg_tables WHERE schemaname = \'public\';" | sed \'/^$/d\' | sed \'s/^[ \\t]*//\'';
295
296
 
296
297
  return command;
@@ -323,7 +324,7 @@ export function buildMongoDBListCollectionsCommand(options) {
323
324
  if (port) command += ` --port ${shellQuote(port)}`;
324
325
  if (user) command += ` --username ${shellQuote(user)}`;
325
326
  if (password) command += ` --password ${shellQuote(password)}`;
326
- command += ` ${shellQuote(database)}`;
327
+ command += ` ${shellQuote(databaseName(database, 'database', 'mongodb'))}`;
327
328
  command += ' --quiet --eval "db.getCollectionNames().forEach(function(c){print(c)})"';
328
329
 
329
330
  return command;
@@ -383,7 +384,7 @@ export function buildMySQLQueryCommand(options) {
383
384
  if (password) command += ` -p${shellQuote(password)}`;
384
385
  if (host) command += ` -h ${shellQuote(host)}`;
385
386
  if (port) command += ` -P ${shellQuote(port)}`;
386
- command += ` ${shellQuote(database)}`;
387
+ command += ` ${shellQuote(databaseName(database, 'database', 'mysql'))}`;
387
388
 
388
389
  // Feed the SQL via stdin (quoted heredoc) so the remote shell never parses it —
389
390
  // backtick-quoted identifiers and `$` survive intact, and query text cannot inject
@@ -421,7 +422,7 @@ export function buildPostgreSQLQueryCommand(options) {
421
422
  if (user) command += ` -U ${shellQuote(user)}`;
422
423
  if (host) command += ` -h ${shellQuote(host)}`;
423
424
  if (port) command += ` -p ${shellQuote(port)}`;
424
- command += ` -d ${shellQuote(database)}`;
425
+ command += ` -d ${shellQuote(databaseName(database, 'database', 'postgresql'))}`;
425
426
  // Each statement is a single-quoted `-c` argument, never psql's input. psql runs
426
427
  // the backslash commands of a script it reads (`\!` runs a shell command, `\o |cmd`
427
428
  // pipes to one) wherever they appear, so a query fed on stdin could leave the
@@ -448,7 +449,7 @@ export function buildMongoDBQueryCommand(options) {
448
449
  if (port) command += ` --port ${shellQuote(port)}`;
449
450
  if (user) command += ` --username ${shellQuote(user)}`;
450
451
  if (password) command += ` --password ${shellQuote(password)}`;
451
- command += ` ${shellQuote(database)}`;
452
+ command += ` ${shellQuote(databaseName(database, 'database', 'mongodb'))}`;
452
453
  // Feed the JS script via stdin (quoted heredoc) instead of `--eval "..."` so the
453
454
  // remote shell never expands backticks/`$` in the query. See buildHeredoc and issue #44.
454
455
  //
@@ -1,6 +1,6 @@
1
1
  import path from 'path';
2
2
  import crypto from 'crypto';
3
- import { shellQuote } from './shell-quote.js';
3
+ import { shellQuote, positional } from './shell-quote.js';
4
4
 
5
5
  /**
6
6
  * Deploy helper functions for secure file deployment
@@ -39,7 +39,7 @@ export function buildDeploymentStrategy(remotePath, options = {}) {
39
39
 
40
40
  // Every value below reaches the remote shell. Up to 3.8.5 the path sat in
41
41
  // double quotes, where $(…) still runs, and owner and permissions were bare.
42
- const target = shellQuote(remotePath);
42
+ const target = shellQuote(positional(remotePath, 'remotePath'));
43
43
 
44
44
  // Step 1: Backup existing file if requested
45
45
  if (backup) {
@@ -83,7 +83,7 @@ export function buildDeploymentStrategy(remotePath, options = {}) {
83
83
  if (owner) {
84
84
  strategy.steps.push(withSudoStdin({
85
85
  type: 'chown',
86
- command: `${sudoPrefix}chown ${shellQuote(owner)} ${target}`
86
+ command: `${sudoPrefix}chown -- ${shellQuote(owner)} ${target}`
87
87
  }));
88
88
  }
89
89
 
@@ -91,7 +91,7 @@ export function buildDeploymentStrategy(remotePath, options = {}) {
91
91
  if (permissions) {
92
92
  strategy.steps.push(withSudoStdin({
93
93
  type: 'chmod',
94
- command: `${sudoPrefix}chmod ${shellQuote(permissions)} ${target}`
94
+ command: `${sudoPrefix}chmod -- ${shellQuote(permissions)} ${target}`
95
95
  }));
96
96
  }
97
97
 
@@ -4,7 +4,7 @@
4
4
  */
5
5
 
6
6
  import { logger } from './logger.js';
7
- import { shellQuote, safeInteger } from './shell-quote.js';
7
+ import { shellQuote, safeInteger, positional } from './shell-quote.js';
8
8
 
9
9
  // Health status levels
10
10
  export const HEALTH_STATUS = {
@@ -177,7 +177,7 @@ export function buildServiceStatusCommand(serviceName) {
177
177
  // serviceName is a tool argument: resolveServiceName returns non-aliases
178
178
  // unchanged, so it reaches the shell as-is. Quoting every occurrence is what
179
179
  // stops `nginx; id > /tmp/pwned` from executing (GHSA-m793-whw6-f537).
180
- const name = shellQuote(serviceName);
180
+ const name = shellQuote(positional(serviceName, 'serviceName'));
181
181
  // Try systemd first, fallback to sysv
182
182
  return `
183
183
  if command -v systemctl >/dev/null 2>&1; then
@@ -230,7 +230,7 @@ export function buildProcessListCommand(options = {}) {
230
230
  let command = `ps aux --sort=${sortFlag === '-c' ? '-pcpu' : '-pmem'} | head -n ${safeInteger(limit, 20) + 1}`;
231
231
 
232
232
  if (filter) {
233
- command += ` | grep -i ${shellQuote(filter)}`;
233
+ command += ` | grep -i -e ${shellQuote(filter)}`;
234
234
  }
235
235
 
236
236
  // Format output as JSON-like structure
package/src/index.js CHANGED
@@ -157,7 +157,7 @@ import { loadToolConfig, isToolEnabled } from './tool-config-manager.js';
157
157
  import { evaluatePolicy } from './policy.js';
158
158
  import { buildBackgroundCommand, parseBackgroundStart } from './background-job.js';
159
159
  import { needsApproval, isControlPlaneListening, requestDecision, buildRequest, isApprovalExempt, isReadOnlyAction, HANDLER_GATED_TOOLS } from './approval.js';
160
- import { shellQuote, shellPath, safeInteger, withRemoteTimeout } from './shell-quote.js';
160
+ import { shellQuote, shellPath, safeInteger, withRemoteTimeout, positional, databaseName } from './shell-quote.js';
161
161
  import { openStream } from './live-stream.js';
162
162
  import { transferPolicy, confineLocalPath } from './transfer-paths.js';
163
163
  import { auditLog, sanitize } from './audit.js';
@@ -1430,11 +1430,11 @@ registerToolConditional(
1430
1430
  if (follow) {
1431
1431
  command += ' -f';
1432
1432
  }
1433
- command += ` ${shellQuote(file)}`;
1433
+ command += ` ${shellQuote(positional(file, 'file'))}`;
1434
1434
 
1435
1435
  // Add grep filter if specified
1436
1436
  if (grep) {
1437
- command += ` | grep ${shellQuote(grep)}`;
1437
+ command += ` | grep -e ${shellQuote(grep)}`;
1438
1438
  }
1439
1439
 
1440
1440
  logger.info(`Starting tail on ${serverName}`, {
@@ -4121,17 +4121,17 @@ registerToolConditional(
4121
4121
  // a value spliced in raw here would run every night, not once.
4122
4122
  switch (type) {
4123
4123
  case BACKUP_TYPES.MYSQL:
4124
- scriptContent += `mysqldump --single-transaction --routines --triggers ${shellQuote(database)} | gzip > "$BACKUP_FILE" || fail\n`;
4124
+ scriptContent += `mysqldump --single-transaction --routines --triggers ${shellQuote(databaseName(database, 'database', 'mysql'))} | gzip > "$BACKUP_FILE" || fail\n`;
4125
4125
  break;
4126
4126
  case BACKUP_TYPES.POSTGRESQL:
4127
- scriptContent += `pg_dump --format=custom --clean --if-exists ${shellQuote(database)} | gzip > "$BACKUP_FILE" || fail\n`;
4127
+ scriptContent += `pg_dump --format=custom --clean --if-exists ${shellQuote(databaseName(database, 'database', 'postgresql'))} | gzip > "$BACKUP_FILE" || fail\n`;
4128
4128
  break;
4129
4129
  case BACKUP_TYPES.MONGODB:
4130
4130
  scriptContent += 'DUMP_DIR="/tmp/mongo_${RANDOM}"\n';
4131
4131
  scriptContent += `mongodump --db ${shellQuote(database)} --out "$DUMP_DIR" && tar -czf "$BACKUP_FILE" -C /tmp "$(basename "$DUMP_DIR")" && rm -rf "$DUMP_DIR" || fail\n`;
4132
4132
  break;
4133
4133
  case BACKUP_TYPES.FILES:
4134
- scriptContent += `tar -czf "$BACKUP_FILE" ${(paths || []).map(shellQuote).join(' ')} || fail\n`;
4134
+ scriptContent += `tar -czf "$BACKUP_FILE" ${(paths || []).map(p => shellQuote(positional(p, 'path'))).join(' ')} || fail\n`;
4135
4135
  break;
4136
4136
  }
4137
4137
 
@@ -28,6 +28,66 @@ export function shellQuote(value) {
28
28
  return SQ + String(value).replace(/'/g, SQ + '\\' + SQ + SQ) + SQ;
29
29
  }
30
30
 
31
+ // A control character (NUL, newline, tab, escape...) has no place in a name or a path.
32
+ // eslint-disable-next-line no-control-regex
33
+ const CONTROL_CHARACTERS = /[\u0000-\u001f\u007f]/;
34
+
35
+ /**
36
+ * A caller-controlled word that goes into a command as an argument, without
37
+ * letting the program read it as an option.
38
+ *
39
+ * shellQuote() keeps the SHELL from interpreting a value; it cannot keep the
40
+ * PROGRAM from doing so: `'--execute=DROP TABLE t'` is one perfectly quoted word
41
+ * and mysql still takes it for its --execute option, which makes it ignore
42
+ * stdin, where the read-only transaction travels (GHSA-mwc5-p6j7-hh4q).
43
+ * `--init-command`, `--host`, tar's `--checkpoint-action` and the like reach the
44
+ * same way. A name that starts with "-" can never be selected positionally
45
+ * anyway, so refusing it costs a legitimate caller nothing; a relative path
46
+ * written `./-name` is fine.
47
+ *
48
+ * Use it on every positional argument built from a tool argument, and quote
49
+ * the result: `shellQuote(positional(database, 'database'))`.
50
+ *
51
+ * @param {any} value - The value the caller supplied
52
+ * @param {string} [label] - What it is, for the error message
53
+ * @returns {string} The value as text, unchanged
54
+ */
55
+ export function positional(value, label = 'value') {
56
+ const text = String(value ?? '');
57
+ if (text.startsWith('-')) {
58
+ throw new Error(`${label} must not start with "-": the command it goes to would read it as an option (a relative path can be written ./name)`);
59
+ }
60
+ if (CONTROL_CHARACTERS.test(text)) {
61
+ throw new Error(`${label} must not contain control characters`);
62
+ }
63
+ return text;
64
+ }
65
+
66
+ /**
67
+ * A database name, checked like positional() and, where the client reads more
68
+ * than a name from that word, for what it must not carry.
69
+ *
70
+ * psql and pg_dump take a value with "=" or a postgres:// prefix for a whole
71
+ * connection string, and the mongo shell reads host/name and host:port/name:
72
+ * either way the word could send the connection, and the password with it,
73
+ * somewhere else.
74
+ *
75
+ * @param {any} value - The name the caller supplied
76
+ * @param {string} [label] - What it is, for the error message
77
+ * @param {'mysql'|'postgresql'|'mongodb'} [engine] - Which client reads it
78
+ * @returns {string} The name as text, unchanged
79
+ */
80
+ export function databaseName(value, label = 'database', engine = 'mysql') {
81
+ const text = positional(value, label);
82
+ if (engine === 'postgresql' && (text.includes('=') || /^postgres(?:ql)?:\/\//i.test(text))) {
83
+ throw new Error(`${label} must be a database name, not a connection string`);
84
+ }
85
+ if (engine === 'mongodb' && /[/:]/.test(text)) {
86
+ throw new Error(`${label} must be a database name, not a host or a connection string`);
87
+ }
88
+ return text;
89
+ }
90
+
31
91
  /** Quote a POSIX path while preserving the documented ~/ home shorthand. */
32
92
  export function shellPath(value) {
33
93
  const text = String(value);