mcp-ssh-manager 3.6.6 → 3.6.7

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
@@ -5,6 +5,21 @@ All notable changes to MCP SSH Manager will be documented in this file.
5
5
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
6
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
7
 
8
+ ## [3.6.7] - 2026-07-11
9
+
10
+ ### Security
11
+
12
+ - **Command injection in the database command builders** ([#51](https://github.com/bvisible/mcp-ssh-manager/pull/51) — responsibly disclosed by **Ugur Ozer / AI Risk Management**, see [#48](https://github.com/bvisible/mcp-ssh-manager/issues/48))
13
+ - Every `ssh_db_*` tool argument — `database`, `table`, `collection`, output/input file paths, and the `user` / `host` / `port` / `password` connection fields — arrives from tool-call parameters and was interpolated straight into a **shell-evaluated** command string in `src/database-manager.js`. A crafted value (`$(…)`, backticks, `; cmd`, `| cmd`, `&& cmd`, bare `> file`) executed arbitrary commands on the configured SSH target, under the account the MCP server uses.
14
+ - The strongest path is **`ssh_db_list`**: it is classified read-only, so it stayed **allowed in the `readonly` and `restricted`** per-server security modes while still reaching an injectable builder — bypassing the boundary those modes promise.
15
+ - The v3.6.5 heredoc fix (#44) only hardened `ssh_db_query`'s query **text**; the `list` / `dump` / `import` / `restore` builders and the query builders' **connection flags** were untouched.
16
+ - **Fix:** a centralized `shellQuote()` (single-quote wrap + `'\''` escaping) now wraps **every** interpolated caller value across all 15 builders. The MySQL list-tables `database` became a shell-quoted positional argument instead of an interpolated `USE <db>` SQL clause, closing the SQL path too. Shell-quoting is transparent for legitimate values.
17
+
18
+ ### Added
19
+
20
+ - Security regression test `tests/test-database-injection.js` (`npm run test:dbinjection`, part of `npm test`): drives every builder × every caller-controlled parameter × an injection-payload battery (648 combinations) through a real `/bin/sh` with fake DB client binaries and asserts no payload ever executes; unit-tests `shellQuote()`; verifies benign values stay correctly quoted.
21
+ - `SECURITY.md` documenting the private vulnerability disclosure process (GitHub private vulnerability reporting + security email; resolves [#48](https://github.com/bvisible/mcp-ssh-manager/issues/48)).
22
+
8
23
  ## [3.6.6] - 2026-07-11
9
24
 
10
25
  ### Fixed
package/README.md CHANGED
@@ -6,7 +6,7 @@ A Model Context Protocol (MCP) server that enables **Claude Code** and **OpenAI
6
6
 
7
7
  [![npm version](https://img.shields.io/npm/v/mcp-ssh-manager.svg?style=for-the-badge&logo=npm)](https://www.npmjs.com/package/mcp-ssh-manager)
8
8
  [![npm downloads](https://img.shields.io/npm/dt/mcp-ssh-manager.svg?style=for-the-badge&logo=npm)](https://www.npmjs.com/package/mcp-ssh-manager)
9
- [![Version](https://img.shields.io/badge/Version-3.6.6-brightgreen?style=for-the-badge)](https://github.com/bvisible/mcp-ssh-manager/releases/tag/v3.6.6)
9
+ [![Version](https://img.shields.io/badge/Version-3.6.7-brightgreen?style=for-the-badge)](https://github.com/bvisible/mcp-ssh-manager/releases/tag/v3.6.7)
10
10
  [![Claude Code](https://img.shields.io/badge/Claude_Code-Compatible-5A67D8?style=for-the-badge&logo=anthropic)](https://claude.ai/code)
11
11
  [![OpenAI Codex](https://img.shields.io/badge/OpenAI_Codex-Compatible-00A67E?style=for-the-badge&logo=openai)](https://openai.com/codex)
12
12
  [![MCP](https://img.shields.io/badge/MCP-Server-orange?style=for-the-badge)](https://modelcontextprotocol.io)
@@ -20,21 +20,27 @@ A Model Context Protocol (MCP) server that enables **Claude Code** and **OpenAI
20
20
 
21
21
  ---
22
22
 
23
- ## 🎉 What's New in v3.6.6
23
+ ## 🎉 What's New in v3.6.7
24
24
 
25
- **Bugfix: per-server `SUDO_PASSWORD`, `DEFAULT_DIR` and `ssh_sync` key auth work again** (Released: July 11, 2026)
25
+ **🔒 Security fix: command injection in the database helpers (`ssh_db_list` / `ssh_db_dump` / `ssh_db_import`)** (Released: July 11, 2026)
26
26
 
27
- - **🔑 `ssh_execute_sudo` respects the configured `SSH_SERVER_<NAME>_SUDO_PASSWORD` again** ([#49](https://github.com/bvisible/mcp-ssh-manager/issues/49) — thanks [@egoan82](https://github.com/egoan82) for the excellent root-cause report) — since the v3.0.0 ConfigLoader refactor, resolved server configs expose **camelCase** fields (`sudoPassword`), but the handler still read the pre-3.0 snake_case name (`sudo_password`), which is always `undefined`. Non-interactive sudo therefore failed with `sudo: a terminal is required to authenticate` unless the password was passed explicitly on every call.
28
- - **📁 Same mismatch, same fix for `DEFAULT_DIR` and `ssh_sync` key auth** ([#50](https://github.com/bvisible/mcp-ssh-manager/pull/50)) — `ssh_execute`, `ssh_group_execute` and `ssh_execute_sudo` silently ignored the configured default directory (commands ran in `$HOME`), `ssh_list_servers` always reported an empty `defaultDir`, and `ssh_sync` never passed the configured SSH key to rsync (`-i` / `BatchMode=yes`).
29
- - **🧪 Regression-proofed** — a new test locks the ConfigLoader's output field names (from both `.env` and TOML) and statically forbids stale snake_case field access in `src/`.
30
- - **No configuration change required** — your `.env`/TOML keys are unchanged; existing configs simply start working again.
27
+ - **Every `ssh_db_*` argument is now shell-quoted** ([#51](https://github.com/bvisible/mcp-ssh-manager/pull/51) — responsibly disclosed by **Ugur Ozer / AI Risk Management**, see [#48](https://github.com/bvisible/mcp-ssh-manager/issues/48)) — the database, table, collection, file-path and connection values passed to the database command builders were interpolated straight into a shell-evaluated string, so a crafted value (`$(…)`, backticks, `; cmd`) executed arbitrary commands on the SSH target. The strongest path was **`ssh_db_list`**, which is read-only-classified and therefore stayed allowed in the `readonly` and `restricted` security modes. The v3.6.5 heredoc fix (#44) only covered `ssh_db_query`'s query *text*; this hardens the list/dump/import/restore builders and the query builders' connection flags too.
28
+ - **🧪 Regression-proofed** — a new test drives every builder × every caller-controlled parameter × an injection-payload battery (648 combinations) through a real shell with fake DB binaries and asserts nothing executes.
29
+ - **🔐 `SECURITY.md` added** — documents private vulnerability reporting (GitHub Security tab + security email).
30
+ - **No configuration or API change** — shell-quoting is transparent for normal database/table/file names.
31
31
 
32
- [Read full changelog →](CHANGELOG.md#366---2026-07-11)
32
+ ⚠️ **Upgrade recommended for anyone exposing the database tools**, especially operators relying on `readonly` / `restricted` modes as a security boundary.
33
+
34
+ [Read full changelog →](CHANGELOG.md#367---2026-07-11)
33
35
 
34
36
  ---
35
37
 
36
38
  ## Previous Releases
37
39
 
40
+ ### v3.6.6 - `SUDO_PASSWORD` / `DEFAULT_DIR` / `ssh_sync` key auth work again (July 11, 2026)
41
+
42
+ - **🔑 camelCase config field reads** ([#50](https://github.com/bvisible/mcp-ssh-manager/pull/50) — thanks [@egoan82](https://github.com/egoan82)) — since the v3.0.0 ConfigLoader refactor, `ssh_execute_sudo` ignored `SUDO_PASSWORD`, `DEFAULT_DIR` was ignored by `ssh_execute`/`ssh_group_execute`/`ssh_list_servers`, and `ssh_sync` never passed the configured SSH key to rsync. All aligned with the loader's camelCase fields, with a regression test locking the loader output shape. [Full changelog →](CHANGELOG.md#366---2026-07-11)
43
+
38
44
  ### v3.6.5 - `ssh_db_query` shell-injection security fix + real row_count (June 30, 2026)
39
45
 
40
46
  - **🔒 Queries are delivered on stdin via a single-quoted heredoc** ([#44](https://github.com/bvisible/mcp-ssh-manager/pull/44), [#45](https://github.com/bvisible/mcp-ssh-manager/pull/45) — thanks [@technophile77](https://github.com/technophile77)) — the remote shell no longer parses backticks/`$(…)` inside queries (which corrupted backtick identifiers **and** let the "SELECT-only" tool run arbitrary shell commands), and `row_count` now reflects each engine's real output instead of counting wrapper lines. [Full changelog →](CHANGELOG.md#365---2026-06-30)
package/SECURITY.md ADDED
@@ -0,0 +1,55 @@
1
+ # Security Policy
2
+
3
+ ## Supported Versions
4
+
5
+ Only the latest published `mcp-ssh-manager` release on npm receives security fixes.
6
+ Please upgrade to the newest version before reporting an issue.
7
+
8
+ | Version | Supported |
9
+ | ------- | --------- |
10
+ | latest | ✅ |
11
+ | older | ❌ |
12
+
13
+ ## Reporting a Vulnerability
14
+
15
+ **Please do not report security vulnerabilities through public GitHub issues,
16
+ discussions, or pull requests.**
17
+
18
+ Report privately through either channel:
19
+
20
+ - **GitHub Private Vulnerability Reporting** (preferred) — open the
21
+ [**Security** tab](https://github.com/bvisible/mcp-ssh-manager/security/advisories)
22
+ of this repository and click **Report a vulnerability**. This keeps the report,
23
+ discussion, and any resulting advisory private until a fix is released.
24
+ - **Email** — <security@bvisible.ch> (or <jeremy@bvisible.ch>).
25
+
26
+ Please include, as far as you can:
27
+
28
+ - the affected version and the affected tool(s) or source area,
29
+ - a description of the vulnerability and its impact,
30
+ - a minimal proof of concept or reproduction steps, and
31
+ - any suggested remediation.
32
+
33
+ To keep proof-of-concept work safe, validate against **local package source and
34
+ your own owned test boundary** — do not exercise third-party hosts or
35
+ maintainer infrastructure.
36
+
37
+ ## Disclosure Process
38
+
39
+ - We aim to acknowledge a report within **72 hours**.
40
+ - We will confirm the issue, determine affected versions, and prepare a fix,
41
+ keeping you informed along the way.
42
+ - Fixes are released as a new npm version accompanied by a
43
+ [GitHub Security Advisory](https://github.com/bvisible/mcp-ssh-manager/security/advisories)
44
+ (with a CVE where warranted).
45
+ - We are happy to **credit reporters** in the advisory and release notes.
46
+ Let us know the name/handle you would like used, or if you prefer to remain anonymous.
47
+
48
+ ## Scope Notes
49
+
50
+ `mcp-ssh-manager` executes commands on remote hosts on behalf of an AI assistant.
51
+ Reports are especially valuable when a tool documented as read-only or safe
52
+ (for example `ssh_db_list`, `ssh_db_query`, or any tool permitted under the
53
+ `readonly` / `restricted` per-server security modes) can be made to execute
54
+ arbitrary commands, escalate privileges, or exfiltrate data on the configured
55
+ SSH target.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mcp-ssh-manager",
3
- "version": "3.6.6",
3
+ "version": "3.6.7",
4
4
  "description": "MCP SSH Manager: Model Context Protocol server for SSH remote server management. Control SSH connections from Claude Code and OpenAI Codex - execute commands, transfer files, database operations, backups, health monitoring, and DevOps automation. NEW: Tool activation system reduces context usage by 92%",
5
5
  "main": "src/index.js",
6
6
  "bin": {
@@ -13,7 +13,7 @@
13
13
  "setup": "npm install && pip install -r tools/requirements.txt",
14
14
  "configure": "python tools/server-manager.py",
15
15
  "test-connection": "python tools/test-connection.py",
16
- "test": "npm run test:profiles && npm run test:aliases && npm run test:hooks && npm run test:tools && npm run test:policy && npm run test:ping && npm run test:config && npm run test:fieldnames && npm run test:lifecycle && npm run test:sync && npm run test:dbquoting && npm run test:dbrowcount",
16
+ "test": "npm run test:profiles && npm run test:aliases && npm run test:hooks && npm run test:tools && npm run test:policy && npm run test:ping && npm run test:config && npm run test:fieldnames && npm run test:lifecycle && npm run test:sync && npm run test:dbquoting && npm run test:dbinjection && npm run test:dbrowcount",
17
17
  "test:profiles": "node tests/test-profiles.js",
18
18
  "test:aliases": "node tests/test-command-aliases.js",
19
19
  "test:hooks": "node tests/test-hooks.js",
@@ -25,6 +25,7 @@
25
25
  "test:lifecycle": "node tests/test-lifecycle.js",
26
26
  "test:sync": "node tests/test-sync-stats.js",
27
27
  "test:dbquoting": "node tests/test-database-query-quoting.js",
28
+ "test:dbinjection": "node tests/test-database-injection.js",
28
29
  "test:dbrowcount": "node tests/test-database-row-count.js",
29
30
  "test:all": "npm test && ./scripts/validate.sh",
30
31
  "validate": "./scripts/validate.sh",
@@ -10,6 +10,28 @@ export const DB_TYPES = {
10
10
  MONGODB: 'mongodb'
11
11
  };
12
12
 
13
+ // A single quote character as a constant, so shellQuote()'s escaping stays
14
+ // readable under the repo's single-quote lint style.
15
+ const SQ = '\'';
16
+
17
+ /**
18
+ * Quote a value for safe inclusion as a single POSIX shell word.
19
+ *
20
+ * Wraps the value in single quotes and escapes any embedded single quote as
21
+ * '\'' so the remote shell treats it literally — it never interprets $(...),
22
+ * backticks, $VAR, ;, |, &, redirects, spaces, or newlines inside the value.
23
+ * EVERY caller-controlled value interpolated into a database command string
24
+ * MUST go through this: the database/table/collection names, file paths, and
25
+ * connection fields all arrive from tool arguments (issue #48 — command
26
+ * injection via ssh_db_list / ssh_db_dump / ssh_db_import builder arguments).
27
+ * Numbers are coerced to string; null/undefined become an empty quoted word.
28
+ */
29
+ export function shellQuote(value) {
30
+ if (value === null || value === undefined) return SQ + SQ;
31
+ // Replace every ' with '\'' (close quote, escaped quote, reopen), then wrap.
32
+ return SQ + String(value).replace(/'/g, SQ + '\\' + SQ + SQ) + SQ;
33
+ }
34
+
13
35
  /**
14
36
  * Build MySQL dump command
15
37
  */
@@ -27,22 +49,22 @@ export function buildMySQLDumpCommand(options) {
27
49
 
28
50
  let command = 'mysqldump';
29
51
 
30
- if (user) command += ` -u${user}`;
31
- if (password) command += ` -p'${password}'`;
32
- if (host) command += ` -h ${host}`;
33
- if (port) command += ` -P ${port}`;
52
+ if (user) command += ` -u${shellQuote(user)}`;
53
+ if (password) command += ` -p${shellQuote(password)}`;
54
+ if (host) command += ` -h ${shellQuote(host)}`;
55
+ if (port) command += ` -P ${shellQuote(port)}`;
34
56
 
35
57
  command += ' --single-transaction --routines --triggers';
36
- command += ` ${database}`;
58
+ command += ` ${shellQuote(database)}`;
37
59
 
38
60
  if (tables && Array.isArray(tables)) {
39
- command += ` ${tables.join(' ')}`;
61
+ command += ` ${tables.map(shellQuote).join(' ')}`;
40
62
  }
41
63
 
42
64
  if (compress) {
43
- command += ` | gzip > "${outputFile}"`;
65
+ command += ` | gzip > ${shellQuote(outputFile)}`;
44
66
  } else {
45
- command += ` > "${outputFile}"`;
67
+ command += ` > ${shellQuote(outputFile)}`;
46
68
  }
47
69
 
48
70
  return command;
@@ -65,27 +87,27 @@ export function buildPostgreSQLDumpCommand(options) {
65
87
 
66
88
  let command = '';
67
89
  if (password) {
68
- command = `PGPASSWORD='${password}' `;
90
+ command = `PGPASSWORD=${shellQuote(password)} `;
69
91
  }
70
92
 
71
93
  command += 'pg_dump';
72
- if (user) command += ` -U ${user}`;
73
- if (host) command += ` -h ${host}`;
74
- if (port) command += ` -p ${port}`;
94
+ if (user) command += ` -U ${shellQuote(user)}`;
95
+ if (host) command += ` -h ${shellQuote(host)}`;
96
+ if (port) command += ` -p ${shellQuote(port)}`;
75
97
  command += ' --format=custom --clean --if-exists';
76
98
 
77
99
  if (tables && Array.isArray(tables)) {
78
100
  for (const table of tables) {
79
- command += ` -t ${table}`;
101
+ command += ` -t ${shellQuote(table)}`;
80
102
  }
81
103
  }
82
104
 
83
- command += ` ${database}`;
105
+ command += ` ${shellQuote(database)}`;
84
106
 
85
107
  if (compress) {
86
- command += ` | gzip > "${outputFile}"`;
108
+ command += ` | gzip > ${shellQuote(outputFile)}`;
87
109
  } else {
88
- command += ` > "${outputFile}"`;
110
+ command += ` > ${shellQuote(outputFile)}`;
89
111
  }
90
112
 
91
113
  return command;
@@ -107,23 +129,23 @@ export function buildMongoDBDumpCommand(options) {
107
129
  } = options;
108
130
 
109
131
  let command = 'mongodump';
110
- if (host) command += ` --host ${host}`;
111
- if (port) command += ` --port ${port}`;
112
- if (user) command += ` --username ${user}`;
113
- if (password) command += ` --password '${password}'`;
114
- if (database) command += ` --db ${database}`;
132
+ if (host) command += ` --host ${shellQuote(host)}`;
133
+ if (port) command += ` --port ${shellQuote(port)}`;
134
+ if (user) command += ` --username ${shellQuote(user)}`;
135
+ if (password) command += ` --password ${shellQuote(password)}`;
136
+ if (database) command += ` --db ${shellQuote(database)}`;
115
137
 
116
138
  if (collections && Array.isArray(collections)) {
117
139
  for (const collection of collections) {
118
- command += ` --collection ${collection}`;
140
+ command += ` --collection ${shellQuote(collection)}`;
119
141
  }
120
142
  }
121
143
 
122
- command += ` --out "${outputDir}"`;
144
+ command += ` --out ${shellQuote(outputDir)}`;
123
145
 
124
146
  if (compress) {
125
- command += ` && tar -czf "${outputDir}.tar.gz" -C "$(dirname ${outputDir})" "$(basename ${outputDir})"`;
126
- command += ` && rm -rf "${outputDir}"`;
147
+ command += ` && tar -czf ${shellQuote(outputDir + '.tar.gz')} -C "$(dirname ${shellQuote(outputDir)})" "$(basename ${shellQuote(outputDir)})"`;
148
+ command += ` && rm -rf ${shellQuote(outputDir)}`;
127
149
  }
128
150
 
129
151
  return command;
@@ -145,17 +167,17 @@ export function buildMySQLImportCommand(options) {
145
167
  let command = '';
146
168
 
147
169
  if (inputFile.endsWith('.gz')) {
148
- command = `gunzip -c "${inputFile}" | `;
170
+ command = `gunzip -c ${shellQuote(inputFile)} | `;
149
171
  } else {
150
- command = `cat "${inputFile}" | `;
172
+ command = `cat ${shellQuote(inputFile)} | `;
151
173
  }
152
174
 
153
175
  command += 'mysql';
154
- if (user) command += ` -u${user}`;
155
- if (password) command += ` -p'${password}'`;
156
- if (host) command += ` -h ${host}`;
157
- if (port) command += ` -P ${port}`;
158
- command += ` ${database}`;
176
+ if (user) command += ` -u${shellQuote(user)}`;
177
+ if (password) command += ` -p${shellQuote(password)}`;
178
+ if (host) command += ` -h ${shellQuote(host)}`;
179
+ if (port) command += ` -P ${shellQuote(port)}`;
180
+ command += ` ${shellQuote(database)}`;
159
181
 
160
182
  return command;
161
183
  }
@@ -175,20 +197,20 @@ export function buildPostgreSQLImportCommand(options) {
175
197
 
176
198
  let command = '';
177
199
  if (password) {
178
- command = `PGPASSWORD='${password}' `;
200
+ command = `PGPASSWORD=${shellQuote(password)} `;
179
201
  }
180
202
 
181
203
  command += 'pg_restore';
182
- if (user) command += ` -U ${user}`;
183
- if (host) command += ` -h ${host}`;
184
- if (port) command += ` -p ${port}`;
204
+ if (user) command += ` -U ${shellQuote(user)}`;
205
+ if (host) command += ` -h ${shellQuote(host)}`;
206
+ if (port) command += ` -p ${shellQuote(port)}`;
185
207
  command += ' --clean --if-exists';
186
- command += ` -d ${database}`;
208
+ command += ` -d ${shellQuote(database)}`;
187
209
 
188
210
  if (inputFile.endsWith('.gz')) {
189
- command = `gunzip -c "${inputFile}" | ${command}`;
211
+ command = `gunzip -c ${shellQuote(inputFile)} | ${command}`;
190
212
  } else {
191
- command += ` "${inputFile}"`;
213
+ command += ` ${shellQuote(inputFile)}`;
192
214
  }
193
215
 
194
216
  return command;
@@ -211,23 +233,23 @@ export function buildMongoDBRestoreCommand(options) {
211
233
 
212
234
  if (inputPath.endsWith('.tar.gz')) {
213
235
  const extractDir = inputPath.replace('.tar.gz', '');
214
- command = `tar -xzf "${inputPath}" -C "$(dirname ${inputPath})" && `;
236
+ command = `tar -xzf ${shellQuote(inputPath)} -C "$(dirname ${shellQuote(inputPath)})" && `;
215
237
  command += 'mongorestore';
216
238
  if (drop) command += ' --drop';
217
- if (host) command += ` --host ${host}`;
218
- if (port) command += ` --port ${port}`;
219
- if (user) command += ` --username ${user}`;
220
- if (password) command += ` --password '${password}'`;
221
- command += ` "${extractDir}"`;
222
- command += ` && rm -rf "${extractDir}"`;
239
+ if (host) command += ` --host ${shellQuote(host)}`;
240
+ if (port) command += ` --port ${shellQuote(port)}`;
241
+ if (user) command += ` --username ${shellQuote(user)}`;
242
+ if (password) command += ` --password ${shellQuote(password)}`;
243
+ command += ` ${shellQuote(extractDir)}`;
244
+ command += ` && rm -rf ${shellQuote(extractDir)}`;
223
245
  } else {
224
246
  command = 'mongorestore';
225
247
  if (drop) command += ' --drop';
226
- if (host) command += ` --host ${host}`;
227
- if (port) command += ` --port ${port}`;
228
- if (user) command += ` --username ${user}`;
229
- if (password) command += ` --password '${password}'`;
230
- command += ` "${inputPath}"`;
248
+ if (host) command += ` --host ${shellQuote(host)}`;
249
+ if (port) command += ` --port ${shellQuote(port)}`;
250
+ if (user) command += ` --username ${shellQuote(user)}`;
251
+ if (password) command += ` --password ${shellQuote(password)}`;
252
+ command += ` ${shellQuote(inputPath)}`;
231
253
  }
232
254
 
233
255
  return command;
@@ -240,10 +262,10 @@ export function buildMySQLListDatabasesCommand(options) {
240
262
  const { user, password, host = 'localhost', port = 3306 } = options;
241
263
 
242
264
  let command = 'mysql';
243
- if (user) command += ` -u${user}`;
244
- if (password) command += ` -p'${password}'`;
245
- if (host) command += ` -h ${host}`;
246
- if (port) command += ` -P ${port}`;
265
+ if (user) command += ` -u${shellQuote(user)}`;
266
+ if (password) command += ` -p${shellQuote(password)}`;
267
+ if (host) command += ` -h ${shellQuote(host)}`;
268
+ if (port) command += ` -P ${shellQuote(port)}`;
247
269
  command += ' -e "SHOW DATABASES;" | tail -n +2';
248
270
 
249
271
  return command;
@@ -256,11 +278,14 @@ export function buildMySQLListTablesCommand(options) {
256
278
  const { database, user, password, host = 'localhost', port = 3306 } = options;
257
279
 
258
280
  let command = 'mysql';
259
- if (user) command += ` -u${user}`;
260
- if (password) command += ` -p'${password}'`;
261
- if (host) command += ` -h ${host}`;
262
- if (port) command += ` -P ${port}`;
263
- command += ` -e "USE ${database}; SHOW TABLES;" | tail -n +2`;
281
+ if (user) command += ` -u${shellQuote(user)}`;
282
+ if (password) command += ` -p${shellQuote(password)}`;
283
+ if (host) command += ` -h ${shellQuote(host)}`;
284
+ if (port) command += ` -P ${shellQuote(port)}`;
285
+ // Pass the database as a shell-quoted positional argument instead of
286
+ // interpolating it into the -e SQL, so the name can inject neither the
287
+ // shell nor the SQL (a crafted USE clause).
288
+ command += ` ${shellQuote(database)} -e "SHOW TABLES;" | tail -n +2`;
264
289
 
265
290
  return command;
266
291
  }
@@ -273,13 +298,13 @@ export function buildPostgreSQLListDatabasesCommand(options) {
273
298
 
274
299
  let command = '';
275
300
  if (password) {
276
- command = `PGPASSWORD='${password}' `;
301
+ command = `PGPASSWORD=${shellQuote(password)} `;
277
302
  }
278
303
 
279
304
  command += 'psql';
280
- if (user) command += ` -U ${user}`;
281
- if (host) command += ` -h ${host}`;
282
- if (port) command += ` -p ${port}`;
305
+ if (user) command += ` -U ${shellQuote(user)}`;
306
+ if (host) command += ` -h ${shellQuote(host)}`;
307
+ if (port) command += ` -p ${shellQuote(port)}`;
283
308
  command += ' -t -c "SELECT datname FROM pg_database WHERE datistemplate = false;" | sed \'/^$/d\' | sed \'s/^[ \\t]*//\'';
284
309
 
285
310
  return command;
@@ -293,14 +318,14 @@ export function buildPostgreSQLListTablesCommand(options) {
293
318
 
294
319
  let command = '';
295
320
  if (password) {
296
- command = `PGPASSWORD='${password}' `;
321
+ command = `PGPASSWORD=${shellQuote(password)} `;
297
322
  }
298
323
 
299
324
  command += 'psql';
300
- if (user) command += ` -U ${user}`;
301
- if (host) command += ` -h ${host}`;
302
- if (port) command += ` -p ${port}`;
303
- command += ` -d ${database}`;
325
+ if (user) command += ` -U ${shellQuote(user)}`;
326
+ if (host) command += ` -h ${shellQuote(host)}`;
327
+ if (port) command += ` -p ${shellQuote(port)}`;
328
+ command += ` -d ${shellQuote(database)}`;
304
329
  command += ' -t -c "SELECT tablename FROM pg_tables WHERE schemaname = \'public\';" | sed \'/^$/d\' | sed \'s/^[ \\t]*//\'';
305
330
 
306
331
  return command;
@@ -313,10 +338,10 @@ export function buildMongoDBListDatabasesCommand(options) {
313
338
  const { user, password, host = 'localhost', port = 27017 } = options;
314
339
 
315
340
  let command = 'mongo';
316
- if (host) command += ` --host ${host}`;
317
- if (port) command += ` --port ${port}`;
318
- if (user) command += ` --username ${user}`;
319
- if (password) command += ` --password '${password}'`;
341
+ if (host) command += ` --host ${shellQuote(host)}`;
342
+ if (port) command += ` --port ${shellQuote(port)}`;
343
+ if (user) command += ` --username ${shellQuote(user)}`;
344
+ if (password) command += ` --password ${shellQuote(password)}`;
320
345
  command += ' --quiet --eval "db.adminCommand(\'listDatabases\').databases.forEach(function(d){print(d.name)})"';
321
346
 
322
347
  return command;
@@ -329,11 +354,11 @@ export function buildMongoDBListCollectionsCommand(options) {
329
354
  const { database, user, password, host = 'localhost', port = 27017 } = options;
330
355
 
331
356
  let command = 'mongo';
332
- if (host) command += ` --host ${host}`;
333
- if (port) command += ` --port ${port}`;
334
- if (user) command += ` --username ${user}`;
335
- if (password) command += ` --password '${password}'`;
336
- command += ` ${database}`;
357
+ if (host) command += ` --host ${shellQuote(host)}`;
358
+ if (port) command += ` --port ${shellQuote(port)}`;
359
+ if (user) command += ` --username ${shellQuote(user)}`;
360
+ if (password) command += ` --password ${shellQuote(password)}`;
361
+ command += ` ${shellQuote(database)}`;
337
362
  command += ' --quiet --eval "db.getCollectionNames().forEach(function(c){print(c)})"';
338
363
 
339
364
  return command;
@@ -386,11 +411,11 @@ export function buildMySQLQueryCommand(options) {
386
411
  }
387
412
 
388
413
  let command = 'mysql';
389
- if (user) command += ` -u${user}`;
390
- if (password) command += ` -p'${password}'`;
391
- if (host) command += ` -h ${host}`;
392
- if (port) command += ` -P ${port}`;
393
- command += ` ${database}`;
414
+ if (user) command += ` -u${shellQuote(user)}`;
415
+ if (password) command += ` -p${shellQuote(password)}`;
416
+ if (host) command += ` -h ${shellQuote(host)}`;
417
+ if (port) command += ` -P ${shellQuote(port)}`;
418
+ command += ` ${shellQuote(database)}`;
394
419
 
395
420
  // Feed the SQL via stdin (quoted heredoc) so the remote shell never parses it —
396
421
  // backtick-quoted identifiers and `$` survive intact, and query text cannot inject
@@ -419,14 +444,14 @@ export function buildPostgreSQLQueryCommand(options) {
419
444
 
420
445
  let command = '';
421
446
  if (password) {
422
- command = `PGPASSWORD='${password}' `;
447
+ command = `PGPASSWORD=${shellQuote(password)} `;
423
448
  }
424
449
 
425
450
  command += 'psql';
426
- if (user) command += ` -U ${user}`;
427
- if (host) command += ` -h ${host}`;
428
- if (port) command += ` -p ${port}`;
429
- command += ` -d ${database}`;
451
+ if (user) command += ` -U ${shellQuote(user)}`;
452
+ if (host) command += ` -h ${shellQuote(host)}`;
453
+ if (port) command += ` -p ${shellQuote(port)}`;
454
+ command += ` -d ${shellQuote(database)}`;
430
455
  // Feed SQL via stdin (quoted heredoc) instead of `-c "${query}"` so the remote shell
431
456
  // never parses it. See buildHeredoc and issue #44.
432
457
  command += buildHeredoc(query);
@@ -441,11 +466,11 @@ export function buildMongoDBQueryCommand(options) {
441
466
  const { database, collection, query, user, password, host = 'localhost', port = 27017 } = options;
442
467
 
443
468
  let command = 'mongo';
444
- if (host) command += ` --host ${host}`;
445
- if (port) command += ` --port ${port}`;
446
- if (user) command += ` --username ${user}`;
447
- if (password) command += ` --password '${password}'`;
448
- command += ` ${database}`;
469
+ if (host) command += ` --host ${shellQuote(host)}`;
470
+ if (port) command += ` --port ${shellQuote(port)}`;
471
+ if (user) command += ` --username ${shellQuote(user)}`;
472
+ if (password) command += ` --password ${shellQuote(password)}`;
473
+ command += ` ${shellQuote(database)}`;
449
474
  // Feed the JS script via stdin (quoted heredoc) instead of `--eval "..."` so the
450
475
  // remote shell never expands backticks/`$` in the query. mongo still evaluates the
451
476
  // script as JavaScript (expected). See buildHeredoc and issue #44.