mcp-ssh-manager 4.4.4 → 4.4.5

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,24 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ## [4.4.5] - 2026-12-02
11
+
12
+ **A security release: upgrade if you use `readonly` mode.** The built-in list of `readonly` mode only knew a command
13
+ after a blank, `;`, `&` or `|`, so a command inside a subshell with no blank before it ran: `(rm -rf /srv/data)`
14
+ deleted the folder on a `readonly` server ([GHSA-c577-5jq9-pqq3](https://github.com/bvisible/mcp-ssh-manager/security/advisories/GHSA-c577-5jq9-pqq3), medium, reported by @InertFluid). The same
15
+ was true of a brace group in zsh (`{rm x}`), of a quoted name after `(`, `if`, `while`, `!` or a case pattern
16
+ (`if "rm" x; then :; fi`, in every shell), of a quoted name after a word that expands to nothing (`$u "rm" x`), and
17
+ of zsh's `=rm x` and glob qualifiers (`ls *(e:rm:)`). `restricted` mode refused all of these lines already. Every
18
+ release with `readonly` mode, from 3.5.0, is affected.
19
+
20
+ ### Fixed
21
+
22
+ - **`readonly` reads a command inside a group as a command.** Each command is also read with every unquoted `(`, `)`, `{` and `}` read as a blank, and a quoted word is read as the command when it can be one: after `(`, `{`, `!`, `if`, `then`, `while`, `until`, `do`, a case pattern, zsh's `noglob`, and a word that can expand to nothing. zsh's `=rm` is read as `rm` wherever a command can stand, and a parenthesis right after a word (a zsh glob qualifier, which can run a command) is refused. Quoted text is untouched: `grep -E "(error|kill)" /var/log/syslog`, `(cd /var/log && ls)`, `f() { ls; }` and `diff <(ls a) <(ls b)` still run. A seeded test puts a stand-in for a denied command in 1,500 such lines and runs them in sh, dash, bash and zsh: none that `readonly` allows runs it.
23
+
24
+ ### Changed
25
+
26
+ - **Desktop build tools: minimatch 11.0.10** ([GHSA-cf2h-pxjq-68xw](https://github.com/advisories/GHSA-cf2h-pxjq-68xw), [GHSA-c859-jr93-m5j4](https://github.com/advisories/GHSA-c859-jr93-m5j4), [GHSA-hqmv-qgp3-jx75](https://github.com/advisories/GHSA-hqmv-qgp3-jx75)) and tar 7.5.24. The node-tar advisories of 2026-11-28 are fixed from tar 8.0.2, which no release of electron-builder accepts yet; tar and minimatch build the desktop app and are not shipped in it or in the npm package.
27
+
10
28
  ## [4.4.4] - 2026-12-01
11
29
 
12
30
  **A security release: upgrade if an agent can call `ssh_sync`.** `ssh_sync` handed its local path to rsync as a
@@ -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.3.tgz"
17
- sha256 "4f4ebe829cd6be96cbd4b9797791c00e934cf28aad876dd7b2b6a8a4fa4b874c"
16
+ url "https://registry.npmjs.org/mcp-ssh-manager/-/mcp-ssh-manager-4.4.4.tgz"
17
+ sha256 "f546847a430d9a7b3b0c692041d167be04d7ec05917c352c42fe127d33c3be4f"
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.4-c04500?style=flat-square)](https://github.com/bvisible/mcp-ssh-manager/releases/tag/v4.4.4)
28
+ [![Version](https://img.shields.io/badge/release-4.4.5-c04500?style=flat-square)](https://github.com/bvisible/mcp-ssh-manager/releases/tag/v4.4.5)
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.4`):** one installation with its own runtime,
61
+ **Desktop application (`4.4.5`):** 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.4):
63
+ installation. [Download it](https://github.com/bvisible/mcp-ssh-manager/releases/tag/v4.4.5):
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.4,
71
+ **Already using npm / the CLI?** `npm update -g mcp-ssh-manager` brings 4.4.5,
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.5](https://github.com/bvisible/mcp-ssh-manager/releases/tag/v4.4.5) — a security fix: `readonly` mode let a command inside a subshell run.** `(rm -rf /srv/data)` has a `(` before its `rm`, not a blank, and the built-in list of destructive commands missed it ([GHSA-c577-5jq9-pqq3](https://github.com/bvisible/mcp-ssh-manager/security/advisories/GHSA-c577-5jq9-pqq3), medium). A command is now also read inside `( )` and `{ }`, after `if`, `while`, `!` and a case pattern, behind a word that expands to nothing, and as zsh reads `=rm` and glob qualifiers. `restricted` mode was not affected. Reported by @InertFluid.
726
+
725
727
  **[4.4.4](https://github.com/bvisible/mcp-ssh-manager/releases/tag/v4.4.4) — a security fix: a path given to `ssh_sync` that starts with `-` was read as an option by rsync.** A pull to `local:--rsh=<command>` ran that command on the machine that runs the server ([GHSA-m727-8qhq-9f7f](https://github.com/bvisible/mcp-ssh-manager/security/advisories/GHSA-m727-8qhq-9f7f), high). `ssh_sync` now refuses a local or remote path that starts with `-` and always puts the two paths behind `--`. Also cross-spawn 7.1.2 (a critical advisory for Windows batch files, in the SDK's stdio client and the desktop build tools) and the pending dependency updates. Reported by @manus-use.
726
728
 
727
729
  **[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.
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "ssh-manager-desktop",
3
- "version": "4.4.4",
3
+ "version": "4.4.5",
4
4
  "lockfileVersion": 3,
5
5
  "requires": true,
6
6
  "packages": {
7
7
  "": {
8
8
  "name": "ssh-manager-desktop",
9
- "version": "4.4.4",
9
+ "version": "4.4.5",
10
10
  "license": "MIT",
11
11
  "dependencies": {
12
12
  "electron-updater": "^6.8.9",
@@ -304,9 +304,9 @@
304
304
  }
305
305
  },
306
306
  "node_modules/@isaacs/nonrecursive-glob-match": {
307
- "version": "1.0.3",
308
- "resolved": "https://registry.npmjs.org/@isaacs/nonrecursive-glob-match/-/nonrecursive-glob-match-1.0.3.tgz",
309
- "integrity": "sha512-SI7kyKHq4XXDRozw+r9GlRZkia1ZkFrpLW98r2IC2Bpr8YkFGE3fvjzmxCG0x78A++69HmDNrDvSMYBZmHGpKA==",
307
+ "version": "1.1.0",
308
+ "resolved": "https://registry.npmjs.org/@isaacs/nonrecursive-glob-match/-/nonrecursive-glob-match-1.1.0.tgz",
309
+ "integrity": "sha512-GPm/Dbvk5RLmhRnxXIa7nTpK0xtl4E0wj0MslsuNiJ1FIROU+2+9lgUC/dgF7Ey4QCWhfEsgxilMRMCdHPfDfw==",
310
310
  "dev": true,
311
311
  "license": "BlueOak-1.0.0"
312
312
  },
@@ -2455,13 +2455,13 @@
2455
2455
  }
2456
2456
  },
2457
2457
  "node_modules/minimatch": {
2458
- "version": "11.0.4",
2459
- "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-11.0.4.tgz",
2460
- "integrity": "sha512-/UJ1dEoVQTjPNDh6SGlUo+ueLMmoHTas4q+8yaAR/5t6nLAIiBDJtoYUh20IYleix7ehiQkgczb84PE3uzHkxg==",
2458
+ "version": "11.0.10",
2459
+ "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-11.0.10.tgz",
2460
+ "integrity": "sha512-L3/cjhQ5E6lft0VCp/2mO3Ic74RnutKy3rz7iGwp/6w1hKcO262g2rbfacFyw6eBig5OdI0nu5PpuA0/WQs9Tw==",
2461
2461
  "dev": true,
2462
2462
  "license": "BlueOak-1.0.0",
2463
2463
  "dependencies": {
2464
- "@isaacs/nonrecursive-glob-match": "^1.0.3",
2464
+ "@isaacs/nonrecursive-glob-match": "^1.1.0",
2465
2465
  "brace-expansion": "^5.0.12"
2466
2466
  },
2467
2467
  "engines": {
@@ -3287,9 +3287,9 @@
3287
3287
  }
3288
3288
  },
3289
3289
  "node_modules/tar": {
3290
- "version": "7.5.22",
3291
- "resolved": "https://registry.npmjs.org/tar/-/tar-7.5.22.tgz",
3292
- "integrity": "sha512-MFO/QzvtAOmJbkhOaCTvbGcFN9L9b+JunIsDwaKljSOdcLMea3NJ1k9Usz/rjdfSXTq4dfzfeS7W4p4YOAAHeA==",
3290
+ "version": "7.5.24",
3291
+ "resolved": "https://registry.npmjs.org/tar/-/tar-7.5.24.tgz",
3292
+ "integrity": "sha512-to/OxuCk5DeVONcjjODT7aeoRq5NIPPlfPc0tNML+wLlnf6u5BcJz97Uukv3pT3jzorkFhUevylkAiBr+ZmV2w==",
3293
3293
  "dev": true,
3294
3294
  "license": "BlueOak-1.0.0",
3295
3295
  "dependencies": {
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "ssh-manager-desktop",
3
3
  "productName": "SSH Manager",
4
- "version": "4.4.4",
4
+ "version": "4.4.5",
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.4 ([release](https://github.com/bvisible/mcp-ssh-manager/releases/tag/v4.4.4)).
3
+ **4.0 is released, on npm and as a desktop application**; the latest is 4.4.5 ([release](https://github.com/bvisible/mcp-ssh-manager/releases/tag/v4.4.5)).
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
@@ -86,6 +86,19 @@ so `ls -l /usr/bin/su` is refused there. A word that is only a quoted string is
86
86
  read as typed unless it can be the command (the first word, or the one after
87
87
  `-exec`, `xargs`, `env` and the like), so `grep "kill -9" /var/log/syslog` still runs.
88
88
 
89
+ A command can also stand inside a group, and `readonly` reads it there since 4.4.5.
90
+ Up to 4.4.4 the built-in list only knew a command after a blank, `;`, `&` or `|`, so
91
+ `(rm -rf /srv/data)` ran on a `readonly` server
92
+ ([GHSA-c577-5jq9-pqq3](https://github.com/bvisible/mcp-ssh-manager/security/advisories/GHSA-c577-5jq9-pqq3)).
93
+ Each command is now also read with every unquoted `(`, `)`, `{` and `}` read as a
94
+ blank (`(rm x)`, `{ rm x; }`, zsh's `{rm x}` and `cat =(rm x)`, fish's `echo (rm x)`),
95
+ and a quoted word is read as the command when it can be one: after `(`, `{`, `!`,
96
+ `if`, `then`, `while`, `until`, `do`, a case pattern, zsh's `noglob`, and a word that
97
+ can expand to nothing (`$u "rm" x`). zsh's `=rm x` is read as `rm x`, and a
98
+ parenthesis right after a word (`ls *(e:rm:)`, a zsh glob qualifier that runs a
99
+ command) is refused. Quoted text is not touched: `grep -E "(error|kill)" /var/log/syslog`
100
+ and `(cd /var/log && ls)` still run.
101
+
89
102
  ## Configuration
90
103
 
91
104
  ### Environment variables / `.env`
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mcp-ssh-manager",
3
- "version": "4.4.4",
3
+ "version": "4.4.5",
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: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",
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:readonlygroups && 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",
@@ -33,6 +33,7 @@
33
33
  "test:tools": "node tests/test-tool-registry.js",
34
34
  "test:policy": "node tests/test-policy.js",
35
35
  "test:shellsegments": "node tests/test-shell-segments.js",
36
+ "test:readonlygroups": "node tests/test-readonly-groups.js",
36
37
  "test:auditsuccess": "node tests/test-audit-success.js",
37
38
  "test:ping": "node tests/test-ssh-ping.js",
38
39
  "test:config": "node tests/test-server-config-manager.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.4",
9
+ "version": "4.4.5",
10
10
  "packages": [
11
11
  {
12
12
  "registryType": "npm",
13
13
  "identifier": "mcp-ssh-manager",
14
- "version": "4.4.4",
14
+ "version": "4.4.5",
15
15
  "transport": {
16
16
  "type": "stdio"
17
17
  },
package/src/policy.js CHANGED
@@ -183,6 +183,7 @@ export function evaluatePolicy(serverConfig, toolName, command) {
183
183
  // The whole line, then each command in it, including those inside $(…)
184
184
  // and backticks: `echo $(rm -rf x)` has no space before its `rm`.
185
185
  // Each of them as the shell reads it too: `r\<newline>m`, `r""m` and `r${x}m` are `rm`.
186
+ // And as the command it is inside a group: `(rm x)`, `{rm x}` and `if "rm" x` run `rm` (GHSA-c577-5jq9-pqq3).
186
187
  const { segments, substitutions } = splitShellCommand(command);
187
188
  const { texts, unreadable } = readSegments([...segments, ...substitutions]);
188
189
  if (unreadable) {
@@ -294,6 +294,15 @@ export function splitShellCommand(command) {
294
294
  // allowed command that starts another one (`find -exec`, `xargs`, `env`, `sed`,
295
295
  // `awk`, `tar`, `git`, `make`, an interpreter) lets the rest through.
296
296
  //
297
+ // A command can also stand inside a group: `(rm x)` and `{ rm x; }` run `rm` in
298
+ // every shell, `{rm x}` and `cat =(rm x)` in zsh, `echo (rm x)` in fish, and the
299
+ // word after `(`, `if`, `while`, `!` or a case pattern is a command
300
+ // (GHSA-c577-5jq9-pqq3). Each reading is therefore also made with every unquoted
301
+ // `(`, `)`, `{` and `}` read as a blank, and a quoted word is read as the command
302
+ // it can be. zsh reads `=rm` as the path of `rm`, and runs the code of a glob
303
+ // qualifier (`*(e:rm x:)`, `*(+rm)`): the first is read without its `=` where a
304
+ // command can stand, the second, which no reading follows, is reported.
305
+ //
297
306
  // Everything above is a fixed rewrite, so it is read the same way the shell reads
298
307
  // it and the pattern is tested on the result as well. What stands for text the
299
308
  // reader cannot know (`$_`, `${x:-word}`, `{a,b}`: a line can make any of them
@@ -306,6 +315,14 @@ const BRACE_GROUP = /^(?:\{[^{}\s,]*(?:,[^{}\s,]*)+\}|\{[^{}\s,]*\.\.[^{}\s,]*\}
306
315
  /** A word before the command: `NAME=value`, or a redirection. */
307
316
  const ASSIGNMENT_OR_REDIRECTION = /^(?:[A-Za-z_][A-Za-z0-9_]*=|\d*[<>]|&>)/;
308
317
  const NAME_EXPANSION = /^\$([A-Za-z_][A-Za-z0-9_]*)/;
318
+ /**
319
+ * Reserved words and zsh's precommand modifiers: the word after one of them is a
320
+ * command (`( "rm" x)`, `if "rm" x`, `! "rm" x`, zsh's `noglob "rm" x`).
321
+ */
322
+ const BEFORE_A_COMMAND = new Set(['!', '(', '((', '{', '}', ')', 'if', 'then', 'else', 'elif', 'while', 'until', 'do',
323
+ 'coproc', 'noglob', 'nocorrect']);
324
+ /** The four readings of a word, in the order they are tried. */
325
+ const READINGS = /** @type {const} */ (['unquoted', 'bare', 'spaced', 'defaulted']);
309
326
  /** Combinations a brace expansion is read to, so `{a,b}{c,d}{e,f}…` cannot grow without end. */
310
327
  const BRACE_READINGS = 64;
311
328
 
@@ -376,32 +393,44 @@ function joinLines(text) {
376
393
  }
377
394
 
378
395
  /**
379
- * Read one simple command the way the shell does before running it.
396
+ * @typedef {object} Reading
397
+ * @property {string} unquoted - Quotes, backslashes and line continuations removed, names left in
398
+ * @property {string} bare - Also with every name removed, as when it is unset
399
+ * @property {string} spaced - With every name replaced by a blank, as when it holds blanks
400
+ * @property {string} defaulted - With `${name:-word}` replaced by its word, as when the name is unset
401
+ */
402
+
403
+ /**
404
+ * Read one word of a simple command the way the shell does before running it.
380
405
  *
381
- * @param {string} original - One segment, as `splitShellCommand` returned it
382
- * @returns {{unquoted: string, bare: string, spaced: string, defaulted: string, unreadable: string|null, guessed: string|null}}
383
- * `unquoted`: quotes, backslashes and line continuations removed, names left in;
384
- * `bare`: also with every name removed, as when it is unset;
385
- * `spaced`: with every name replaced by a blank, as when it holds blanks;
386
- * `defaulted`: with `${name:-word}` replaced by its word, as when the name is unset;
387
- * `unreadable`: the first thing found that no reading follows (`$_`);
406
+ * @param {string} original - One word of a segment, as `splitShellCommand` returned it
407
+ * @returns {{read: Reading, opened: Reading, grouped: boolean, unreadable: string|null, guessed: string|null}}
408
+ * `read`: the readings of the word (see Reading);
409
+ * `opened`: the same, with every unquoted `(`, `)`, `{` and `}` read as a blank;
410
+ * `grouped`: whether the word holds one of them, after which a command can start;
411
+ * `unreadable`: the first thing found that no reading follows (`$_`, a glob qualifier);
388
412
  * `guessed`: the first thing found that is read by guesses (`${x:-word}`, `{a,b}`)
389
413
  */
390
414
  function readOne(original) {
391
415
  const text = joinLines(original);
392
- let unquoted = '';
393
- let bare = '';
394
- let spaced = '';
395
- let defaulted = '';
416
+ /** @type {Reading} */
417
+ const read = { unquoted: '', bare: '', spaced: '', defaulted: '' };
418
+ /** @type {Reading} */
419
+ const opened = { unquoted: '', bare: '', spaced: '', defaulted: '' };
420
+ let grouped = false;
396
421
  /** @type {string|null} */
397
422
  let unreadable = null;
398
423
  /** @type {string|null} */
399
424
  let guessed = null;
400
425
  let inDouble = false;
401
426
  /** @param {string} s */
402
- const plain = s => { unquoted += s; bare += s; spaced += s; defaulted += s; };
427
+ const plain = s => {
428
+ for (const reading of [read, opened]) for (const kind of READINGS) reading[kind] += s;
429
+ };
403
430
  /** @param {string} s */
404
- const expansion = s => { unquoted += s; spaced += ' '; };
431
+ const expansion = s => {
432
+ for (const reading of [read, opened]) { reading.unquoted += s; reading.spaced += ' '; }
433
+ };
405
434
  /**
406
435
  * `$_` is the last argument of the command before it: a line can set it
407
436
  * (`echo m; r$_`), so it is the one name the reader cannot treat as empty.
@@ -454,7 +483,11 @@ function readOne(original) {
454
483
  const length = bracedLength(rest);
455
484
  // ${name:-word}, ${name-word}, ${name:=word}, ${name=word}: the word, when the name is unset.
456
485
  const fallback = /^\$\{[A-Za-z_][A-Za-z0-9_]*:?[-=]([\s\S]*)\}$/.exec(rest.slice(0, length));
457
- if (fallback) defaulted += readOne(fallback[1]).unquoted;
486
+ if (fallback) {
487
+ const word = readOne(fallback[1]);
488
+ read.defaulted += word.read.unquoted;
489
+ opened.defaulted += word.opened.unquoted;
490
+ }
458
491
  expansion(rest.slice(0, length));
459
492
  i += length;
460
493
  continue;
@@ -495,10 +528,29 @@ function readOne(original) {
495
528
  if (ch === '{' && !inDouble && BRACE_GROUP.test(text.slice(i))) {
496
529
  guessed ||= 'a brace expansion ({a,b}, {1..3}), which sh does not expand and bash, ksh and zsh do';
497
530
  }
531
+ if (ch === '=' && !inDouble && (i === 0 || '(){}'.includes(text[i - 1])) && /[A-Za-z_]/.test(text[i + 1] ?? '')) {
532
+ // zsh runs `=rm x` as the path of `rm` (its EQUALS option, on by default), wherever a command
533
+ // can stand: `case a in a) =rm x`, `cat =(=rm x)`. Opened, the word is read without its `=`.
534
+ for (const kind of READINGS) read[kind] += ch;
535
+ i++;
536
+ continue;
537
+ }
538
+ if (!inDouble && '(){}'.includes(ch)) {
539
+ // `(` right after a word is a glob qualifier or a pattern to zsh, bash and ksh,
540
+ // and zsh runs the code of `*(e:rm x:)` and `*(+rm)`; sh refuses the line.
541
+ // `name()`, `a=(…)` and `<(…)` are left out.
542
+ if (ch === '(' && ((i > 0 && !'(<>={'.includes(text[i - 1]) && text[i + 1] !== ')') || text[i + 1] === '#')) {
543
+ unreadable ||= 'a parenthesis right after a word or before a #, which zsh reads as a glob qualifier that can run a command';
544
+ }
545
+ grouped = true;
546
+ for (const kind of READINGS) { read[kind] += ch; opened[kind] += ' '; }
547
+ i++;
548
+ continue;
549
+ }
498
550
  plain(ch);
499
551
  i++;
500
552
  }
501
- return { unquoted, bare, spaced, defaulted, unreadable, guessed };
553
+ return { read, opened, grouped, unreadable, guessed };
502
554
  }
503
555
 
504
556
  /**
@@ -528,12 +580,13 @@ function expandBraces(text) {
528
580
 
529
581
  /**
530
582
  * `/bin/rm -rf x` runs `rm`: the same command with the directory of its name left out.
583
+ * The name is the first word that is not an assignment, a redirection or a reserved word (`if /bin/rm x`).
531
584
  * @param {string} text
532
585
  */
533
586
  function withoutDirectory(text) {
534
587
  const words = text.split(/([ \t]+)/);
535
588
  let index = 0;
536
- while (index < words.length && ASSIGNMENT_OR_REDIRECTION.test(words[index])) index += 2;
589
+ while (index < words.length && (ASSIGNMENT_OR_REDIRECTION.test(words[index]) || BEFORE_A_COMMAND.has(words[index]))) index += 2;
537
590
  if (index >= words.length || !words[index].includes('/')) return null;
538
591
  words[index] = words[index].slice(words[index].lastIndexOf('/') + 1);
539
592
  return words.join('');
@@ -621,20 +674,28 @@ export function readSegments(segments) {
621
674
  }
622
675
  // A word that is only a quoted string (`"kill -9"`) is an argument: it is read
623
676
  // as typed, or `grep "service x restart" log` would be a `service x restart`.
624
- // It is read when it can be the command: the first word, or the one after a launcher.
677
+ // It is read when it can be the command: until a word that names one has been
678
+ // seen (not a reserved word such as `(` or `if`, nor a word that can expand to
679
+ // nothing, such as `$u` in `$u "rm" x`), after a launcher, and after a word that
680
+ // holds `(`, `)`, `{` or `}` (`case a in a) "rm" x`).
625
681
  let commandSeen = false;
626
682
  const kept = words.map((word, index) => {
627
- const isCommand = !commandSeen && !ASSIGNMENT_OR_REDIRECTION.test(word.text);
628
- if (isCommand) commandSeen = true;
629
- return !isCommand && !LAUNCHERS.has(words[index - 1]?.text ?? '') && QUOTED_STRING.test(word.text);
683
+ const keep = commandSeen && !LAUNCHERS.has(words[index - 1]?.text ?? '') && QUOTED_STRING.test(word.text);
684
+ if (!ASSIGNMENT_OR_REDIRECTION.test(word.text) && !BEFORE_A_COMMAND.has(word.text) && readings[index].read.bare.trim()) {
685
+ commandSeen = true;
686
+ }
687
+ if (readings[index].grouped) commandSeen = false;
688
+ return keep;
630
689
  });
631
- for (const kind of /** @type {const} */ (['unquoted', 'bare', 'spaced', 'defaulted'])) {
632
- const text = words.map((word, index) => (kept[index] ? word.text : readings[index][kind]) + word.gap).join('');
633
- for (const variant of expandBraces(text)) {
634
- const trimmed = variant.trim();
635
- texts.add(trimmed);
636
- const bare = withoutDirectory(trimmed);
637
- if (bare) texts.add(bare);
690
+ for (const side of /** @type {const} */ (['read', 'opened'])) {
691
+ for (const kind of READINGS) {
692
+ const text = words.map((word, index) => (kept[index] ? word.text : readings[index][side][kind]) + word.gap).join('');
693
+ for (const variant of expandBraces(text)) {
694
+ const trimmed = variant.trim();
695
+ texts.add(trimmed);
696
+ const bare = withoutDirectory(trimmed);
697
+ if (bare) texts.add(bare);
698
+ }
638
699
  }
639
700
  }
640
701
  }