mcp-ssh-manager 4.3.2 → 4.3.4
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 +75 -0
- package/CLAUDE.md +2 -0
- package/Formula/ssh-manager.rb +2 -2
- package/README.md +8 -4
- package/cli/lib/config.sh +1 -1
- package/desktop/electron/package-lock.json +2 -2
- package/desktop/electron/package.json +1 -1
- package/docs/MIGRATION.md +1 -1
- package/docs/SECURITY_MODES.md +51 -6
- package/package.json +4 -2
- package/server.json +2 -2
- package/src/config-loader.js +5 -0
- package/src/index.js +38 -12
- package/src/shell-segments.js +97 -3
- package/src/transfer-paths.js +134 -0
package/CHANGELOG.md
CHANGED
|
@@ -7,6 +7,81 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
7
7
|
|
|
8
8
|
## [Unreleased]
|
|
9
9
|
|
|
10
|
+
## [4.3.4] - 2026-11-23
|
|
11
|
+
|
|
12
|
+
**A security release: upgrade.** `ssh_download` wrote any local path it was given,
|
|
13
|
+
including for servers in `readonly` or `restricted` mode
|
|
14
|
+
([GHSA-2v9g-gm8g-mxgj](https://github.com/bvisible/mcp-ssh-manager/security/advisories/GHSA-2v9g-gm8g-mxgj),
|
|
15
|
+
high, found by the maintainer). Every release from 3.0.0 is affected. **One thing
|
|
16
|
+
behaves differently on purpose:** a download from a `readonly` or `restricted`
|
|
17
|
+
server is now refused outside `~/.ssh-manager/transfers`; if you download to a
|
|
18
|
+
folder of your own, set `TRANSFER_ROOT` for that server (see below). Nothing else
|
|
19
|
+
changes (the release pipeline upgrades 3.8.5, 3.8.6, 3.8.7, every 4.0.x, 4.1.0,
|
|
20
|
+
4.2.0, 4.3.0, 4.3.1, 4.3.2 and 4.3.3 from the registry to this version and back).
|
|
21
|
+
|
|
22
|
+
The Windows installer is still **not Authenticode-signed**, for the same reason
|
|
23
|
+
as 4.0.0: Windows shows *"Windows protected your PC"*, *More info → Run anyway*,
|
|
24
|
+
and `ssh-manager app` checks the installer against the SHA-256 in
|
|
25
|
+
`release-manifest-win32.json`. macOS builds are signed and notarized.
|
|
26
|
+
|
|
27
|
+
### Security
|
|
28
|
+
|
|
29
|
+
- **A server in `readonly` or `restricted` mode can no longer have a download written
|
|
30
|
+
anywhere on this machine.** `ssh_download` writes a local file and never looked at
|
|
31
|
+
the path, in any mode, and is not blocked in `readonly` or `restricted` because it
|
|
32
|
+
changes nothing on the server. An agent that cannot run a command on such a server
|
|
33
|
+
could still create a file there (a redirect to `/tmp` is allowed in `readonly`) and
|
|
34
|
+
download it over `~/.zshrc`, or into `~/.ssh/authorized_keys`, which a later shell
|
|
35
|
+
or login then ran. The local side of a transfer now has a **transfer root**: a
|
|
36
|
+
`readonly` or `restricted` server downloads into `~/.ssh-manager/transfers` (made
|
|
37
|
+
the first time, private to you; a relative local path is read from it), and any
|
|
38
|
+
server can name its own folder with `SSH_SERVER_<NAME>_TRANSFER_ROOT` (TOML:
|
|
39
|
+
`transfer_root`), which then also holds `ssh_upload` and `ssh_sync` to it. A path is
|
|
40
|
+
judged by where it really is after following links, a link as the file is refused,
|
|
41
|
+
and a download is received beside its place and moved there, so a failed one leaves
|
|
42
|
+
no half file. Servers in `unrestricted` mode with no root are not filtered, as
|
|
43
|
+
before. **If you download from a `readonly` or `restricted` server to a folder of
|
|
44
|
+
your own, set `TRANSFER_ROOT` to it**: a download elsewhere is refused with a message
|
|
45
|
+
that names the folder. Found while comparing this project with another SSH server
|
|
46
|
+
for MCP that confines its transfers the same way.
|
|
47
|
+
|
|
48
|
+
## [4.3.3] - 2026-11-21
|
|
49
|
+
|
|
50
|
+
**A security release: upgrade if you use `restricted` mode.** On a server in
|
|
51
|
+
`restricted` mode, a command could be hidden from the allowlist and still run
|
|
52
|
+
([GHSA-86xm-rv3h-r4f2](https://github.com/bvisible/mcp-ssh-manager/security/advisories/GHSA-86xm-rv3h-r4f2),
|
|
53
|
+
high, reported by @Fastyyy123). Every release from 3.8.6 is affected. Servers in
|
|
54
|
+
`readonly` mode and servers without a mode are not (nothing else changes; the
|
|
55
|
+
release pipeline upgrades 3.8.5, 3.8.6, 3.8.7, every 4.0.x, 4.1.0, 4.2.0, 4.3.0,
|
|
56
|
+
4.3.1 and 4.3.2 from the registry to this version and back).
|
|
57
|
+
|
|
58
|
+
The Windows installer is still **not Authenticode-signed**, for the same reason
|
|
59
|
+
as 4.0.0: Windows shows *"Windows protected your PC"*, *More info → Run anyway*,
|
|
60
|
+
and `ssh-manager app` checks the installer against the SHA-256 in
|
|
61
|
+
`release-manifest-win32.json`. macOS builds are signed and notarized.
|
|
62
|
+
|
|
63
|
+
### Security
|
|
64
|
+
|
|
65
|
+
- **`restricted` mode no longer lets a command hide from the allowlist**
|
|
66
|
+
(GHSA-86xm-rv3h-r4f2, reported by @Fastyyy123). The allowlist is checked against
|
|
67
|
+
each command the line runs, found by reading the line, and the reader did not
|
|
68
|
+
recognise comments: a quote or a trailing backslash in one made it see fewer
|
|
69
|
+
commands than the shell ran, so `echo x # it's` followed by a line with `touch`
|
|
70
|
+
and a second apostrophe ran `touch` on a server that allowed only `^echo`. Looking
|
|
71
|
+
for the same thing in other places found four more ways, fixed together: a
|
|
72
|
+
here-document body that opens a quote; an ANSI-C quote (`$'a\'b'`, which ends
|
|
73
|
+
at a different place in dash than in bash, ksh and zsh); a `${…}` that is more than
|
|
74
|
+
a plain name, which includes `${ command; }` of ksh and bash 5.3; and parentheses,
|
|
75
|
+
which zsh and fish read as commands (`echo =(touch x)`), reachable when the login
|
|
76
|
+
shell reads the line, that is with `timeout: 300000` or in a session. Comments are
|
|
77
|
+
now read as comments and left out of the command they follow (so an apostrophe or
|
|
78
|
+
a `;` in a comment is no longer a refusal), and the other four are refused with a
|
|
79
|
+
reason. Unquoted parentheses, here-documents, `$'…'` and `${x:-word}` in a command
|
|
80
|
+
that `restricted` mode allows were never reliable, and are now refused. `readonly`
|
|
81
|
+
mode is not affected: it also tests the whole line. A new test runs random lines
|
|
82
|
+
through the policy and the shells of the machine (sh, dash, bash, zsh) and fails if
|
|
83
|
+
one the policy allows runs a command it never checked.
|
|
84
|
+
|
|
10
85
|
## [4.3.2] - 2026-11-21
|
|
11
86
|
|
|
12
87
|
**A fix to the Live page.** A command that did not finish, because its call timed
|
package/CLAUDE.md
CHANGED
|
@@ -193,6 +193,7 @@ SSH_SERVER_[NAME]_PLATFORM=windows # Optional: "linux" (default) or "win
|
|
|
193
193
|
SSH_SERVER_[NAME]_PROXYJUMP=bastion # Optional: name of another server to use as jump host
|
|
194
194
|
SSH_SERVER_[NAME]_PROXYCOMMAND=command # Optional: custom proxy command (ncat, ssh -W, etc.)
|
|
195
195
|
SSH_SERVER_[NAME]_FORWARD_AGENT=true # Optional: forward local ssh-agent to remote (needs SSH_AUTH_SOCK; security risk)
|
|
196
|
+
SSH_SERVER_[NAME]_TRANSFER_ROOT=~/transfers # Optional: the only local folder the file tools may use (readonly/restricted servers default to ~/.ssh-manager/transfers for downloads)
|
|
196
197
|
SSH_SERVER_[NAME]_ANNOUNCE_AGENT=true # Optional: send AI_AGENT=mcp-ssh-manager to this host (off by default; SSH_MANAGER_ANNOUNCE_AGENT=true for all)
|
|
197
198
|
```
|
|
198
199
|
|
|
@@ -213,6 +214,7 @@ proxy_jump = "bastion" # Optional: name of another server to
|
|
|
213
214
|
proxy_command = "command" # Optional: custom proxy command (ncat, ssh -W, etc.)
|
|
214
215
|
forward_agent = true # Optional: forward local ssh-agent to remote (needs SSH_AUTH_SOCK; security risk)
|
|
215
216
|
announce_agent = true # Optional: send AI_AGENT=mcp-ssh-manager to this host (off by default; false keeps it silent under the global switch)
|
|
217
|
+
transfer_root = "~/transfers" # Optional: the only local folder the file tools may use (see docs/SECURITY_MODES.md)
|
|
216
218
|
```
|
|
217
219
|
|
|
218
220
|
### Selecting an SSH agent key
|
package/Formula/ssh-manager.rb
CHANGED
|
@@ -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.3.
|
|
17
|
-
sha256 "
|
|
16
|
+
url "https://registry.npmjs.org/mcp-ssh-manager/-/mcp-ssh-manager-4.3.3.tgz"
|
|
17
|
+
sha256 "d8ee1b01cdc8238edee9c9bf0a6f83f7be98750ccd23976d273e7ad7f65df3cf"
|
|
18
18
|
license "MIT"
|
|
19
19
|
|
|
20
20
|
depends_on "node"
|
package/README.md
CHANGED
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
[](https://github.com/bvisible/mcp-ssh-manager/releases/latest)
|
|
26
26
|
[](https://github.com/bvisible/mcp-ssh-manager/stargazers)
|
|
27
27
|
[](https://mcptoplist.com/server/io.github.bvisible%2Fmcp-ssh-manager)
|
|
28
|
-
[](https://github.com/bvisible/mcp-ssh-manager/releases/tag/v4.3.4)
|
|
29
29
|
[](https://claude.ai/code)
|
|
30
30
|
[](https://openai.com/codex)
|
|
31
31
|
[](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.3.
|
|
61
|
+
**Desktop application (`4.3.4`):** 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.3.
|
|
63
|
+
installation. [Download it](https://github.com/bvisible/mcp-ssh-manager/releases/tag/v4.3.4):
|
|
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.3.
|
|
71
|
+
**Already using npm / the CLI?** `npm update -g mcp-ssh-manager` brings 4.3.4,
|
|
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
|
|
@@ -720,6 +720,10 @@ The hero animation ships twice, light and dark, swapped by `<picture>` on `prefe
|
|
|
720
720
|
|
|
721
721
|
## What's new
|
|
722
722
|
|
|
723
|
+
**[4.3.4](https://github.com/bvisible/mcp-ssh-manager/releases/tag/v4.3.4) — a security fix: `ssh_download` could write any file on your machine, even from a `readonly` server.** An agent that could not run a command on a `readonly` or `restricted` server could still have a file it had put on the server downloaded over `~/.zshrc` or into `~/.ssh/authorized_keys`. The local side of a transfer now has a transfer root: `readonly` and `restricted` servers download into `~/.ssh-manager/transfers`, and any server can name its own folder with `TRANSFER_ROOT`. If you download from such a server to a folder of your own, set `TRANSFER_ROOT` to it. (GHSA-2v9g-gm8g-mxgj) See the [CHANGELOG](CHANGELOG.md#434---2026-11-23).
|
|
724
|
+
|
|
725
|
+
**[4.3.3](https://github.com/bvisible/mcp-ssh-manager/releases/tag/v4.3.3) — a security fix: `restricted` mode could be made to run a command its allowlist never saw.** A comment, a here-document, an ANSI-C quote (`$'…'`) or a parenthesis could hide a command from the check while the shell ran it. The allowlist now reads comments as comments and refuses those constructs, with a reason. Servers in `readonly` mode and servers without a mode are unaffected; if you use `restricted` mode, upgrade. Reported by @Fastyyy123 (GHSA-86xm-rv3h-r4f2). See the [CHANGELOG](CHANGELOG.md#433---2026-11-21).
|
|
726
|
+
|
|
723
727
|
**[4.3.2](https://github.com/bvisible/mcp-ssh-manager/releases/tag/v4.3.2) — a command that fails no longer stays "running" on the Live page.** When a call threw instead of returning (a timeout, a dropped connection), or when the engine was closed mid-command, the Live entry read "running" until the control plane restarted. It now ends, as `exit -1`. Display only: nothing about the commands themselves changes. Found and traced by @TillNano (#159). See the [CHANGELOG](CHANGELOG.md#432---2026-11-21).
|
|
724
728
|
|
|
725
729
|
**[4.3.1](https://github.com/bvisible/mcp-ssh-manager/releases/tag/v4.3.1) — a security fix: the approval card shows what a deploy or an upload will do.** A request without a command line used to read "(no command)", so a deploy could be approved without seeing its files, their target paths or its `restart` command. The card now shows them. Nothing else changes; upgrade. Reported by @Fastyyy123 (GHSA-vx67-5jmh-4xq2). See the [CHANGELOG](CHANGELOG.md#431---2026-11-20).
|
package/cli/lib/config.sh
CHANGED
|
@@ -123,7 +123,7 @@ load_servers() {
|
|
|
123
123
|
# The settings of a server, as the engine reads them (src/config-loader.js; a test
|
|
124
124
|
# keeps the two lists in step). A server's lines are found by these names and never
|
|
125
125
|
# by the bare prefix: removing "prod" used to remove the lines of "prod_eu" too.
|
|
126
|
-
SERVER_ENV_FIELDS="HOST USER PASSWORD KEYPATH CERTPATH AGENT_PUBLIC_KEY_PATH PASSPHRASE PORT DEFAULT_DIR SUDO_PASSWORD DESCRIPTION GROUP PLATFORM PROXYJUMP PROXYCOMMAND FORWARD_AGENT ANNOUNCE_AGENT MODE ALLOW_PATTERNS DENY_PATTERNS AUDIT_LOG APPROVAL"
|
|
126
|
+
SERVER_ENV_FIELDS="HOST USER PASSWORD KEYPATH CERTPATH AGENT_PUBLIC_KEY_PATH PASSPHRASE PORT DEFAULT_DIR SUDO_PASSWORD DESCRIPTION GROUP PLATFORM PROXYJUMP PROXYCOMMAND FORWARD_AGENT ANNOUNCE_AGENT TRANSFER_ROOT MODE ALLOW_PATTERNS DENY_PATTERNS AUDIT_LOG APPROVAL"
|
|
127
127
|
|
|
128
128
|
# Copy stdin to stdout without the lines of server $1 (upper case) whose setting is
|
|
129
129
|
# in $2 (space separated). With a file as $3, its lines take the place of the first
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ssh-manager-desktop",
|
|
3
|
-
"version": "4.3.
|
|
3
|
+
"version": "4.3.4",
|
|
4
4
|
"lockfileVersion": 3,
|
|
5
5
|
"requires": true,
|
|
6
6
|
"packages": {
|
|
7
7
|
"": {
|
|
8
8
|
"name": "ssh-manager-desktop",
|
|
9
|
-
"version": "4.3.
|
|
9
|
+
"version": "4.3.4",
|
|
10
10
|
"license": "MIT",
|
|
11
11
|
"dependencies": {
|
|
12
12
|
"electron-updater": "^6.8.9",
|
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.3.
|
|
3
|
+
**4.0 is released, on npm and as a desktop application**; the latest is 4.3.4 ([release](https://github.com/bvisible/mcp-ssh-manager/releases/tag/v4.3.4)).
|
|
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/docs/SECURITY_MODES.md
CHANGED
|
@@ -40,11 +40,22 @@ Also refused, because they run or change things the patterns never see:
|
|
|
40
40
|
- command substitution: `$(…)`, backticks, `<(…)`, `>(…)`, even inside double quotes;
|
|
41
41
|
- redirections that write a file: `>`, `>>`, `>|`, `&>`, `<>` — except to
|
|
42
42
|
`/dev/null`, `/dev/stdout`, `/dev/stderr` and descriptor copies such as `2>&1`;
|
|
43
|
-
- a line that cannot be read with confidence (an unterminated quote, for example)
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
43
|
+
- a line that cannot be read with confidence (an unterminated quote, for example):
|
|
44
|
+
the constructs that shells read in their own way, so that a command could be
|
|
45
|
+
hidden from the allowlist in one of them. These are here-documents (`<<`;
|
|
46
|
+
`<<<` is fine), ANSI-C quotes (`$'…'`), a `${…}` that is more than a plain name
|
|
47
|
+
(`${HOME}` and `${#HOME}` are fine, `${x:-word}` and `${ command; }` are not),
|
|
48
|
+
and parentheses that are not quoted or escaped (a subshell to `sh`, a command
|
|
49
|
+
to zsh and fish; `\(` and `'('` are fine).
|
|
50
|
+
|
|
51
|
+
A comment is a `#` that starts a word, up to the end of its line. It is not part of
|
|
52
|
+
the command it follows, so `ls -l # list; all` is one command, `ls -l`, and an
|
|
53
|
+
apostrophe or a backslash in a comment changes nothing. Up to 4.3.2 comments were
|
|
54
|
+
not recognised, and a quote or a trailing backslash in one made the reader see
|
|
55
|
+
fewer commands than the shell ran
|
|
56
|
+
([GHSA-86xm-rv3h-r4f2](https://github.com/bvisible/mcp-ssh-manager/security/advisories/GHSA-86xm-rv3h-r4f2)).
|
|
57
|
+
`readonly` mode also checks the commands inside `$(…)` and backticks against its
|
|
58
|
+
built-in list.
|
|
48
59
|
|
|
49
60
|
## Configuration
|
|
50
61
|
|
|
@@ -84,6 +95,39 @@ audit_log = "/var/log/mcp-ssh-prod.jsonl"
|
|
|
84
95
|
TOML arrays are preferred. A `;`-separated string is also accepted for
|
|
85
96
|
parity with the `.env` form.
|
|
86
97
|
|
|
98
|
+
### Where the file tools may write on this machine: the transfer root
|
|
99
|
+
|
|
100
|
+
`ssh_download` writes a file on the machine that runs the engine, and `ssh_upload`
|
|
101
|
+
and `ssh_sync` read from it. `readonly` and `restricted` are about what runs on the
|
|
102
|
+
*server*, and an agent that cannot run a command there could still put a file of its
|
|
103
|
+
choosing on the server (a redirect to `/tmp` is allowed in `readonly`) and have it
|
|
104
|
+
downloaded over `~/.zshrc` or into `~/.ssh/authorized_keys` here. Up to 4.3.3 nothing
|
|
105
|
+
looked at the local path. A **transfer root** is the one folder the local side of a
|
|
106
|
+
transfer may use:
|
|
107
|
+
|
|
108
|
+
```bash
|
|
109
|
+
SSH_SERVER_PROD_TRANSFER_ROOT=~/transfers/prod # TOML: transfer_root = "~/transfers/prod"
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
- A server with a `TRANSFER_ROOT` is held to it in every mode, by all three tools.
|
|
113
|
+
- A server in `readonly` or `restricted` mode with none is held to
|
|
114
|
+
`~/.ssh-manager/transfers` for `ssh_download` (`ssh_upload` and `ssh_sync` are refused
|
|
115
|
+
there already). The folder is made, private to you, the first time a download needs it.
|
|
116
|
+
- A server in `unrestricted` mode with none is not filtered, as before.
|
|
117
|
+
|
|
118
|
+
A relative local path is read from the root, so `ssh_download` with the local path
|
|
119
|
+
`logs/app.log` writes `~/.ssh-manager/transfers/logs/app.log`. A path is accepted when
|
|
120
|
+
its real location, after following every link, is inside the root; `..`, a sibling
|
|
121
|
+
folder that starts like the root, and a folder link that leads out are refused, and so
|
|
122
|
+
is a link as the file itself. A download is received beside its place and moved there,
|
|
123
|
+
so a transfer that fails leaves no half file, and a link put there meanwhile is replaced,
|
|
124
|
+
not followed. The check runs just before the transfer: it does not stop someone who can
|
|
125
|
+
already change files inside the root while it runs, and for `ssh_sync` it covers the
|
|
126
|
+
folder named, not what rsync does inside it.
|
|
127
|
+
|
|
128
|
+
If you download to a folder of your own from a `readonly` or `restricted` server, set
|
|
129
|
+
`TRANSFER_ROOT` to that folder.
|
|
130
|
+
|
|
87
131
|
## Audit log
|
|
88
132
|
|
|
89
133
|
When `AUDIT_LOG` (`.env`) or `audit_log` (TOML) is set to a file path, every
|
|
@@ -175,7 +219,8 @@ SSH_SERVER_CLIENT_PROD_AUDIT_LOG=~/.ssh-manager/audit/client-prod.jsonl
|
|
|
175
219
|
`ssh_key_manage` (only `accept` / `remove`), `ssh_alert_setup` (only `set`),
|
|
176
220
|
`ssh_process_manager` (only `kill`).
|
|
177
221
|
|
|
178
|
-
**Not gated** (pure reads or local-only state — no remote effect to block
|
|
222
|
+
**Not gated** (pure reads or local-only state — no remote effect to block; the
|
|
223
|
+
local side of `ssh_download` is held to the transfer root, see above):
|
|
179
224
|
`ssh_list_servers`, `ssh_download`, `ssh_tail`, `ssh_monitor`, `ssh_history`,
|
|
180
225
|
`ssh_health_check`, `ssh_service_status`, `ssh_db_list`, `ssh_db_query`
|
|
181
226
|
(one SELECT run in a read-only transaction; for MongoDB a filter document,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mcp-ssh-manager",
|
|
3
|
-
"version": "4.3.
|
|
3
|
+
"version": "4.3.4",
|
|
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: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: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: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: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: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: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: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",
|
|
@@ -22,6 +22,7 @@
|
|
|
22
22
|
"test:controlplane": "node tests/test-control-plane.js",
|
|
23
23
|
"test:livestream": "node tests/test-live-stream.js",
|
|
24
24
|
"test:livefailure": "node tests/test-live-stream-failure.js",
|
|
25
|
+
"test:transferpaths": "node tests/test-transfer-paths.js",
|
|
25
26
|
"test:skills": "node tests/test-skills.js",
|
|
26
27
|
"test:redaction": "node tests/test-logger-redaction.js",
|
|
27
28
|
"test:profiles": "node tests/test-profiles.js",
|
|
@@ -29,6 +30,7 @@
|
|
|
29
30
|
"test:hooks": "node tests/test-hooks.js",
|
|
30
31
|
"test:tools": "node tests/test-tool-registry.js",
|
|
31
32
|
"test:policy": "node tests/test-policy.js",
|
|
33
|
+
"test:shellsegments": "node tests/test-shell-segments.js",
|
|
32
34
|
"test:auditsuccess": "node tests/test-audit-success.js",
|
|
33
35
|
"test:ping": "node tests/test-ssh-ping.js",
|
|
34
36
|
"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.3.
|
|
9
|
+
"version": "4.3.4",
|
|
10
10
|
"packages": [
|
|
11
11
|
{
|
|
12
12
|
"registryType": "npm",
|
|
13
13
|
"identifier": "mcp-ssh-manager",
|
|
14
|
-
"version": "4.3.
|
|
14
|
+
"version": "4.3.4",
|
|
15
15
|
"transport": {
|
|
16
16
|
"type": "stdio"
|
|
17
17
|
},
|
package/src/config-loader.js
CHANGED
|
@@ -38,6 +38,7 @@ import { resolveConfigOptions } from './config-paths.js';
|
|
|
38
38
|
* @property {string} [proxyJump] Name of another configured server to jump through.
|
|
39
39
|
* @property {string} [proxyCommand] Custom proxy command (`%h` / `%p` placeholders).
|
|
40
40
|
* @property {boolean} [forwardAgent] Forward the local ssh-agent to this server.
|
|
41
|
+
* @property {string} [transferRoot] The only folder the local side of ssh_download, ssh_upload and ssh_sync may use. Unset: no limit in unrestricted mode, and ~/.ssh-manager/transfers for ssh_download in readonly and restricted mode.
|
|
41
42
|
* @property {boolean} [announceAgent] Send AI_AGENT=mcp-ssh-manager on each command channel. Unset defers to SSH_MANAGER_ANNOUNCE_AGENT (off by default); false keeps a host silent even when that is on.
|
|
42
43
|
* @property {string} [mode] Security mode: `unrestricted`, `readonly` or `restricted`.
|
|
43
44
|
* @property {string} [approval] Human approval mode: `never`, `destructive` or `always`.
|
|
@@ -280,6 +281,7 @@ export class ConfigLoader {
|
|
|
280
281
|
proxyCommand: serverConfig.proxy_command || serverConfig.proxycommand,
|
|
281
282
|
forwardAgent: parseBool(serverConfig.forward_agent),
|
|
282
283
|
announceAgent: parseOptionalBool(serverConfig.announce_agent),
|
|
284
|
+
transferRoot: serverConfig.transfer_root,
|
|
283
285
|
mode,
|
|
284
286
|
// approval is deliberately NOT read here — see loadEnvConfig below.
|
|
285
287
|
|
|
@@ -359,6 +361,7 @@ export class ConfigLoader {
|
|
|
359
361
|
proxyCommand: env[`SSH_SERVER_${match[1]}_PROXYCOMMAND`],
|
|
360
362
|
forwardAgent: parseBool(env[`SSH_SERVER_${match[1]}_FORWARD_AGENT`]),
|
|
361
363
|
announceAgent: parseOptionalBool(env[`SSH_SERVER_${match[1]}_ANNOUNCE_AGENT`]),
|
|
364
|
+
transferRoot: env[`SSH_SERVER_${match[1]}_TRANSFER_ROOT`],
|
|
362
365
|
mode,
|
|
363
366
|
// approval is deliberately NOT read from files. It is the switch
|
|
364
367
|
// that makes an agent stop and wait for a human, and a switch that
|
|
@@ -447,6 +450,7 @@ export class ConfigLoader {
|
|
|
447
450
|
// Either explicit value is a decision worth keeping; unset is not written,
|
|
448
451
|
// so the global switch still applies after a round trip.
|
|
449
452
|
if (typeof server.announceAgent === 'boolean') serverConfig.announce_agent = server.announceAgent;
|
|
453
|
+
if (server.transferRoot) serverConfig.transfer_root = server.transferRoot;
|
|
450
454
|
// Only emit security fields if they diverge from defaults — keeps generated
|
|
451
455
|
// TOML files clean for users who never opted in.
|
|
452
456
|
if (server.mode && server.mode !== 'unrestricted') serverConfig.mode = server.mode;
|
|
@@ -493,6 +497,7 @@ export class ConfigLoader {
|
|
|
493
497
|
// Written whenever it was set, true or false: an explicit false has to
|
|
494
498
|
// survive a round trip, or a global switch would re-enable that host.
|
|
495
499
|
if (typeof server.announceAgent === 'boolean') lines.push(`SSH_SERVER_${upperName}_ANNOUNCE_AGENT=${server.announceAgent}`);
|
|
500
|
+
if (server.transferRoot) lines.push(`SSH_SERVER_${upperName}_TRANSFER_ROOT=${server.transferRoot}`);
|
|
496
501
|
if (server.proxyJump) lines.push(`SSH_SERVER_${upperName}_PROXYJUMP=${server.proxyJump}`);
|
|
497
502
|
if (server.proxyCommand) lines.push(`SSH_SERVER_${upperName}_PROXYCOMMAND=${server.proxyCommand}`);
|
|
498
503
|
// Security fields — only emit when non-default to avoid clutter in
|
package/src/index.js
CHANGED
|
@@ -10,6 +10,7 @@ import { connectSSH, connectServer } from './ssh-connection.js';
|
|
|
10
10
|
import * as dotenv from 'dotenv';
|
|
11
11
|
import fs from 'fs';
|
|
12
12
|
import path from 'path';
|
|
13
|
+
import crypto from 'node:crypto';
|
|
13
14
|
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
14
15
|
import { isDeepStrictEqual } from 'node:util';
|
|
15
16
|
import { envFileChoice, envFileProblem, expandHomePath } from './config-paths.js';
|
|
@@ -158,6 +159,7 @@ import { buildBackgroundCommand, parseBackgroundStart } from './background-job.j
|
|
|
158
159
|
import { needsApproval, isControlPlaneListening, requestDecision, buildRequest, isApprovalExempt, isReadOnlyAction, HANDLER_GATED_TOOLS } from './approval.js';
|
|
159
160
|
import { shellQuote, shellPath, safeInteger, withRemoteTimeout } from './shell-quote.js';
|
|
160
161
|
import { openStream } from './live-stream.js';
|
|
162
|
+
import { transferPolicy, confineLocalPath } from './transfer-paths.js';
|
|
161
163
|
import { auditLog, sanitize } from './audit.js';
|
|
162
164
|
|
|
163
165
|
const __filename = fileURLToPath(import.meta.url);
|
|
@@ -918,7 +920,7 @@ registerToolConditional(
|
|
|
918
920
|
registerToolConditional(
|
|
919
921
|
'ssh_upload',
|
|
920
922
|
{
|
|
921
|
-
description: 'Uploads one local file to a remote destination path over SFTP on the named server, overwriting any existing remote file at that path. Mutates remote state and is not idempotent beyond replacing the target. Creates no backup. Requires the local file to exist. Does not use sudo, so the remote path must be writable by the configured SSH user. This tool is blocked entirely on servers set to readonly or restricted security mode. For directory trees use ssh_sync instead.',
|
|
923
|
+
description: 'Uploads one local file to a remote destination path over SFTP on the named server, overwriting any existing remote file at that path. Mutates remote state and is not idempotent beyond replacing the target. Creates no backup. Requires the local file to exist. Does not use sudo, so the remote path must be writable by the configured SSH user. This tool is blocked entirely on servers set to readonly or restricted security mode. On a server with a transferRoot the local file must be inside that folder. For directory trees use ssh_sync instead.',
|
|
922
924
|
inputSchema: {
|
|
923
925
|
server: z.string().describe('Server name'),
|
|
924
926
|
localPath: z.string().describe('Local file path'),
|
|
@@ -928,14 +930,18 @@ registerToolConditional(
|
|
|
928
930
|
async ({ server: serverName, localPath, remotePath }) => {
|
|
929
931
|
|
|
930
932
|
try {
|
|
933
|
+
// With a transferRoot on the server, the local file must be inside it (readonly
|
|
934
|
+
// and restricted servers never get here: the tool is refused for them).
|
|
935
|
+
const policy = transferPolicy(await getServerConfig(serverName), 'ssh_upload');
|
|
936
|
+
const source = policy ? confineLocalPath(policy, localPath, { direction: 'read' }) : localPath;
|
|
931
937
|
const ssh = await getConnection(serverName);
|
|
932
938
|
|
|
933
939
|
logger.logTransfer('upload', serverName, localPath, remotePath);
|
|
934
940
|
const startTime = Date.now();
|
|
935
941
|
|
|
936
|
-
await ssh.putFile(
|
|
942
|
+
await ssh.putFile(source, remotePath);
|
|
937
943
|
|
|
938
|
-
const fileStats = fs.statSync(
|
|
944
|
+
const fileStats = fs.statSync(source);
|
|
939
945
|
logger.logTransfer('upload', serverName, localPath, remotePath, {
|
|
940
946
|
success: true,
|
|
941
947
|
size: fileStats.size,
|
|
@@ -977,7 +983,7 @@ registerToolConditional(
|
|
|
977
983
|
registerToolConditional(
|
|
978
984
|
'ssh_download',
|
|
979
985
|
{
|
|
980
|
-
description: 'Downloads one remote file from the named server to a local destination path over SFTP, overwriting any existing local file at that path. Affects only the local filesystem and is read-only on the remote side, so it stays allowed even on servers in readonly or restricted security mode. Reads the remote file using the configured SSH user, which must have permission to read it. Handles single files only; use ssh_sync for directories.',
|
|
986
|
+
description: 'Downloads one remote file from the named server to a local destination path over SFTP, overwriting any existing local file at that path. Affects only the local filesystem and is read-only on the remote side, so it stays allowed even on servers in readonly or restricted security mode, where the local path must be inside the transfer folder (~/.ssh-manager/transfers, or the transferRoot set on the server): a relative path is read from it, and anything outside it, or reached through a link, is refused. On other servers the local path is unrestricted unless a transferRoot is set. Reads the remote file using the configured SSH user, which must have permission to read it. Handles single files only; use ssh_sync for directories.',
|
|
981
987
|
inputSchema: {
|
|
982
988
|
server: z.string().describe('Server name'),
|
|
983
989
|
remotePath: z.string().describe('Remote file path'),
|
|
@@ -986,15 +992,32 @@ registerToolConditional(
|
|
|
986
992
|
},
|
|
987
993
|
async ({ server: serverName, remotePath, localPath }) => {
|
|
988
994
|
try {
|
|
995
|
+
// A server in readonly or restricted mode, or with a transferRoot, writes only
|
|
996
|
+
// inside that folder: the file is received beside its place and moved there, so a
|
|
997
|
+
// failed transfer never leaves half a file, and a link put there meanwhile is
|
|
998
|
+
// replaced, not followed.
|
|
999
|
+
const policy = transferPolicy(await getServerConfig(serverName), 'ssh_download');
|
|
1000
|
+
const target = policy ? confineLocalPath(policy, localPath, { direction: 'write' }) : localPath;
|
|
989
1001
|
const ssh = await getConnection(serverName);
|
|
990
1002
|
|
|
991
|
-
logger.logTransfer('download', serverName, remotePath,
|
|
1003
|
+
logger.logTransfer('download', serverName, remotePath, target);
|
|
992
1004
|
const startTime = Date.now();
|
|
993
1005
|
|
|
994
|
-
|
|
1006
|
+
if (policy) {
|
|
1007
|
+
const partial = `${target}.part-${crypto.randomBytes(4).toString('hex')}`;
|
|
1008
|
+
try {
|
|
1009
|
+
await ssh.getFile(partial, remotePath);
|
|
1010
|
+
fs.renameSync(partial, target);
|
|
1011
|
+
} catch (error) {
|
|
1012
|
+
fs.rmSync(partial, { force: true });
|
|
1013
|
+
throw error;
|
|
1014
|
+
}
|
|
1015
|
+
} else {
|
|
1016
|
+
await ssh.getFile(target, remotePath);
|
|
1017
|
+
}
|
|
995
1018
|
|
|
996
|
-
const fileStats = fs.statSync(
|
|
997
|
-
logger.logTransfer('download', serverName, remotePath,
|
|
1019
|
+
const fileStats = fs.statSync(target);
|
|
1020
|
+
logger.logTransfer('download', serverName, remotePath, target, {
|
|
998
1021
|
success: true,
|
|
999
1022
|
size: fileStats.size,
|
|
1000
1023
|
duration: `${Date.now() - startTime}ms`
|
|
@@ -1006,7 +1029,7 @@ registerToolConditional(
|
|
|
1006
1029
|
content: [
|
|
1007
1030
|
{
|
|
1008
1031
|
type: 'text',
|
|
1009
|
-
text: `✅ File downloaded successfully\nServer: ${serverName}\nRemote: ${remotePath}\nLocal: ${
|
|
1032
|
+
text: `✅ File downloaded successfully\nServer: ${serverName}\nRemote: ${remotePath}\nLocal: ${target}`,
|
|
1010
1033
|
},
|
|
1011
1034
|
],
|
|
1012
1035
|
};
|
|
@@ -1035,7 +1058,7 @@ registerToolConditional(
|
|
|
1035
1058
|
registerToolConditional(
|
|
1036
1059
|
'ssh_sync',
|
|
1037
1060
|
{
|
|
1038
|
-
description: 'Synchronizes files or directories between local and remote using rsync over SSH on the named server. Each of source and destination must carry a local: or remote: prefix and one side must be local and the other remote; with no prefix it assumes a push from local to remote. On Windows MCP hosts, provide native Windows local paths such as C:\\project or .\\project; the tool converts drive-letter and UNC paths to MSYS2 format before spawning rsync. Do not pre-convert a local path to /c/project because Node performs local filesystem checks using Windows path semantics. Mutates the destination. Setting delete true removes destination files absent from source, which is destructive; dryRun true previews without changing anything. Compression is on by default. Password authentication requires sshpass installed locally. Blocked on readonly or restricted servers. Timeout defaults to 30000 ms.',
|
|
1061
|
+
description: 'Synchronizes files or directories between local and remote using rsync over SSH on the named server. Each of source and destination must carry a local: or remote: prefix and one side must be local and the other remote; with no prefix it assumes a push from local to remote. On Windows MCP hosts, provide native Windows local paths such as C:\\project or .\\project; the tool converts drive-letter and UNC paths to MSYS2 format before spawning rsync. Do not pre-convert a local path to /c/project because Node performs local filesystem checks using Windows path semantics. Mutates the destination. Setting delete true removes destination files absent from source, which is destructive; dryRun true previews without changing anything. Compression is on by default. Password authentication requires sshpass installed locally. Blocked on readonly or restricted servers. On a server with a transferRoot the local side must be inside that folder. Timeout defaults to 30000 ms.',
|
|
1039
1062
|
inputSchema: {
|
|
1040
1063
|
server: z.string().describe('Server name from configuration'),
|
|
1041
1064
|
source: z.string().describe('Source path with a "local:" or "remote:" prefix. On Windows, use a native local path such as "local:C:\\project" or "local:.\\project"; do not pre-convert it to MSYS2 /c/... syntax.'),
|
|
@@ -1123,8 +1146,11 @@ registerToolConditional(
|
|
|
1123
1146
|
let localPath;
|
|
1124
1147
|
let remotePath;
|
|
1125
1148
|
|
|
1149
|
+
// With a transferRoot on the server, the local side stays inside it. (readonly
|
|
1150
|
+
// and restricted servers never get here: the tool is refused for them.)
|
|
1151
|
+
const syncPolicy = transferPolicy(serverConfig, 'ssh_sync');
|
|
1126
1152
|
if (direction === 'push') {
|
|
1127
|
-
localPath = cleanSource;
|
|
1153
|
+
localPath = syncPolicy ? confineLocalPath(syncPolicy, cleanSource, { direction: 'read', allowRoot: true }) : cleanSource;
|
|
1128
1154
|
remotePath = cleanDest;
|
|
1129
1155
|
|
|
1130
1156
|
// Check if local path exists
|
|
@@ -1132,7 +1158,7 @@ registerToolConditional(
|
|
|
1132
1158
|
throw new Error(`Local path does not exist: ${localPath}`);
|
|
1133
1159
|
}
|
|
1134
1160
|
} else {
|
|
1135
|
-
localPath = cleanDest;
|
|
1161
|
+
localPath = syncPolicy ? confineLocalPath(syncPolicy, cleanDest, { direction: 'write', allowRoot: true }) : cleanDest;
|
|
1136
1162
|
remotePath = cleanSource;
|
|
1137
1163
|
}
|
|
1138
1164
|
|
package/src/shell-segments.js
CHANGED
|
@@ -10,13 +10,36 @@
|
|
|
10
10
|
*
|
|
11
11
|
* This is a conservative reader, not a shell. Whatever it cannot place with
|
|
12
12
|
* confidence is reported so the caller can refuse it: command substitution
|
|
13
|
-
* (`$(…)`, backticks), process substitution,
|
|
14
|
-
* file
|
|
15
|
-
*
|
|
13
|
+
* (`$(…)`, backticks), process substitution, redirections that write to a
|
|
14
|
+
* file, and the constructs below.
|
|
15
|
+
*
|
|
16
|
+
* What makes a reader like this unsafe is not what it refuses but where it
|
|
17
|
+
* and the shell disagree about which text is quoted or a comment: whatever the
|
|
18
|
+
* reader thinks is hidden, it does not check, and whatever it thinks is live
|
|
19
|
+
* but the shell has hidden, it can swallow a real command into a quote
|
|
20
|
+
* (GHSA-86xm-rv3h-r4f2). So:
|
|
21
|
+
*
|
|
22
|
+
* - a `#` at the start of a word is a comment, to the end of its line, and
|
|
23
|
+
* the text of it is dropped: an apostrophe or a backslash in a comment must
|
|
24
|
+
* not open a quote or join two lines. `\<newline>` is a line continuation, which
|
|
25
|
+
* the shell removes before it looks for a comment, so it changes nothing here;
|
|
26
|
+
* - a `#` the shells read differently (next to `<>(){}`) is refused;
|
|
27
|
+
* - an unquoted `(` or `)` is refused: it is a subshell to sh, and a command to
|
|
28
|
+
* zsh (`=(cmd)`, `*(e:cmd:)`) and fish (`(cmd)`);
|
|
29
|
+
* - what hides text from this reader and that shells read in their own way is
|
|
30
|
+
* refused rather than guessed: here-documents (`<<`), ANSI-C quotes
|
|
31
|
+
* (`$'…'`, where `\'` does not end the string in bash, ksh and zsh but does
|
|
32
|
+
* in dash), and a `${…}` that is more than a plain name (`${x:-word}`,
|
|
33
|
+
* `${x#pattern}`, ksh's and bash's `${ command; }`).
|
|
34
|
+
*
|
|
35
|
+
* Each of those sets `problem` and the reading goes on as before, so a caller
|
|
36
|
+
* that only wants the segments (readonly mode) still gets them.
|
|
16
37
|
*/
|
|
17
38
|
|
|
18
39
|
const CONTROL_OPERATORS = /^(;;|;&|&&|\|\||\|&)/;
|
|
19
40
|
const HARMLESS_WRITE_TARGETS = new Set(['/dev/null', '/dev/stdout', '/dev/stderr']);
|
|
41
|
+
/** `${name}`, `${#name}`, `${1}`, `${@}`: a parameter, and nothing the shell reads further. */
|
|
42
|
+
const PLAIN_EXPANSION = /^\$\{(?:#?[A-Za-z_][A-Za-z0-9_]*|[0-9]+|[@*#?$!-])\}/;
|
|
20
43
|
|
|
21
44
|
/**
|
|
22
45
|
* Index of the `)` closing the `(` at `open`, skipping quoted text and nested
|
|
@@ -80,6 +103,10 @@ export function splitShellCommand(command) {
|
|
|
80
103
|
const result = { segments: [], substitutions: [], writes: [], problem: null };
|
|
81
104
|
const text = String(command);
|
|
82
105
|
let current = '';
|
|
106
|
+
// True where a word could start: the start of the line, after unescaped blanks
|
|
107
|
+
// and after ; & | or a newline. A `#` there is a comment; anywhere else it is
|
|
108
|
+
// part of a word (`a#b`, `$#`, `16#ff`).
|
|
109
|
+
let wordStart = true;
|
|
83
110
|
|
|
84
111
|
const endSegment = () => {
|
|
85
112
|
if (current.trim()) result.segments.push(current.trim());
|
|
@@ -98,19 +125,40 @@ export function splitShellCommand(command) {
|
|
|
98
125
|
result.problem = reason;
|
|
99
126
|
return result;
|
|
100
127
|
};
|
|
128
|
+
/**
|
|
129
|
+
* Something this reader will not guess at. The line is not fit to be allowed,
|
|
130
|
+
* but reading goes on, so the segments found are still reported.
|
|
131
|
+
* @param {string} reason
|
|
132
|
+
*/
|
|
133
|
+
const refuse = reason => {
|
|
134
|
+
result.problem ||= reason;
|
|
135
|
+
};
|
|
101
136
|
|
|
102
137
|
for (let i = 0; i < text.length;) {
|
|
103
138
|
const ch = text[i];
|
|
104
139
|
|
|
105
140
|
if (ch === '\\') {
|
|
141
|
+
// A backslash and a newline are removed by the shell before it reads
|
|
142
|
+
// anything else: they leave the state of the word as it was.
|
|
106
143
|
current += text.slice(i, i + 2);
|
|
144
|
+
if (text[i + 1] !== '\n') wordStart = false;
|
|
107
145
|
i += 2;
|
|
108
146
|
continue;
|
|
109
147
|
}
|
|
148
|
+
if (ch === '#') {
|
|
149
|
+
if (wordStart) {
|
|
150
|
+
// A comment: to the end of the line, and nothing in it is read.
|
|
151
|
+
const newline = text.indexOf('\n', i);
|
|
152
|
+
i = newline < 0 ? text.length : newline;
|
|
153
|
+
continue;
|
|
154
|
+
}
|
|
155
|
+
if ('<>(){}'.includes(text[i - 1] ?? '')) refuse('a # next to a bracket or a redirection, which shells read differently');
|
|
156
|
+
}
|
|
110
157
|
if (ch === '\'') {
|
|
111
158
|
const end = text.indexOf('\'', i + 1);
|
|
112
159
|
if (end < 0) return fail('an unterminated single quote');
|
|
113
160
|
current += text.slice(i, end + 1);
|
|
161
|
+
wordStart = false;
|
|
114
162
|
i = end + 1;
|
|
115
163
|
continue;
|
|
116
164
|
}
|
|
@@ -119,6 +167,11 @@ export function splitShellCommand(command) {
|
|
|
119
167
|
let j = i + 1;
|
|
120
168
|
while (j < text.length && text[j] !== '"') {
|
|
121
169
|
if (text[j] === '\\') { j += 2; continue; }
|
|
170
|
+
if (text[j] === '$' && text[j + 1] === '{') {
|
|
171
|
+
const plain = PLAIN_EXPANSION.exec(text.slice(j));
|
|
172
|
+
if (plain) { j += plain[0].length; continue; }
|
|
173
|
+
refuse('a ${…} with an operator or a word in it, which shells read in their own way');
|
|
174
|
+
}
|
|
122
175
|
if (text[j] === '$' && text[j + 1] === '(') {
|
|
123
176
|
const end = closingParen(text, j + 1);
|
|
124
177
|
if (end < 0) return fail('an unterminated $(');
|
|
@@ -137,15 +190,40 @@ export function splitShellCommand(command) {
|
|
|
137
190
|
}
|
|
138
191
|
if (j >= text.length) return fail('an unterminated double quote');
|
|
139
192
|
current += text.slice(i, j + 1);
|
|
193
|
+
wordStart = false;
|
|
140
194
|
i = j + 1;
|
|
141
195
|
continue;
|
|
142
196
|
}
|
|
197
|
+
if (ch === '$' && text[i + 1] === '\'') {
|
|
198
|
+
refuse('ANSI-C quoting ($\'…\'), where shells disagree on where the string ends');
|
|
199
|
+
}
|
|
200
|
+
if (ch === '$' && text[i + 1] === '{') {
|
|
201
|
+
const plain = PLAIN_EXPANSION.exec(text.slice(i));
|
|
202
|
+
if (plain) {
|
|
203
|
+
current += plain[0];
|
|
204
|
+
wordStart = false;
|
|
205
|
+
i += plain[0].length;
|
|
206
|
+
continue;
|
|
207
|
+
}
|
|
208
|
+
refuse('a ${…} with an operator or a word in it, which shells read in their own way');
|
|
209
|
+
}
|
|
210
|
+
if (ch === '<' && text[i + 1] === '<') {
|
|
211
|
+
if (text[i + 2] === '<') {
|
|
212
|
+
// <<< is a here-string: one word, no body.
|
|
213
|
+
current += '<<<';
|
|
214
|
+
wordStart = false;
|
|
215
|
+
i += 3;
|
|
216
|
+
continue;
|
|
217
|
+
}
|
|
218
|
+
refuse('a here-document (<<), whose body is not a list of commands');
|
|
219
|
+
}
|
|
143
220
|
if ((ch === '$' || ch === '<' || ch === '>') && text[i + 1] === '(') {
|
|
144
221
|
// $(…) command substitution, <(…) and >(…) process substitution.
|
|
145
222
|
const end = closingParen(text, i + 1);
|
|
146
223
|
if (end < 0) return fail(`an unterminated ${ch}(`);
|
|
147
224
|
addSubstitution(text.slice(i + 2, end));
|
|
148
225
|
current += text.slice(i, end + 1);
|
|
226
|
+
wordStart = false;
|
|
149
227
|
i = end + 1;
|
|
150
228
|
continue;
|
|
151
229
|
}
|
|
@@ -154,6 +232,7 @@ export function splitShellCommand(command) {
|
|
|
154
232
|
if (end < 0) return fail('an unterminated backtick');
|
|
155
233
|
addSubstitution(text.slice(i + 1, end));
|
|
156
234
|
current += text.slice(i, end + 1);
|
|
235
|
+
wordStart = false;
|
|
157
236
|
i = end + 1;
|
|
158
237
|
continue;
|
|
159
238
|
}
|
|
@@ -171,16 +250,31 @@ export function splitShellCommand(command) {
|
|
|
171
250
|
|| (ch !== '<' && HARMLESS_WRITE_TARGETS.has(target));
|
|
172
251
|
if (!harmless) result.writes.push(target || '(no target)');
|
|
173
252
|
current += text.slice(i, k);
|
|
253
|
+
wordStart = false;
|
|
174
254
|
i = k;
|
|
175
255
|
continue;
|
|
176
256
|
}
|
|
257
|
+
if (ch === '(' || ch === ')') {
|
|
258
|
+
// sh reads these as a subshell or a function body, never inside an argument; other shells
|
|
259
|
+
// run commands from them: zsh's =(cmd) and glob qualifiers *(e:cmd:), fish's (cmd).
|
|
260
|
+
// The login shell reads the line when it is not wrapped in `sh -c` (a timeout of
|
|
261
|
+
// 300000, a session), so it is not enough to be right about sh.
|
|
262
|
+
refuse('an unquoted parenthesis, which zsh and fish read as a command');
|
|
263
|
+
current += ch;
|
|
264
|
+
wordStart = false;
|
|
265
|
+
i++;
|
|
266
|
+
continue;
|
|
267
|
+
}
|
|
177
268
|
if (ch === ';' || ch === '\n' || ch === '|' || ch === '&') {
|
|
178
269
|
endSegment();
|
|
270
|
+
wordStart = true;
|
|
179
271
|
const operator = CONTROL_OPERATORS.exec(text.slice(i));
|
|
180
272
|
i += operator ? operator[0].length : 1;
|
|
181
273
|
continue;
|
|
182
274
|
}
|
|
183
275
|
current += ch;
|
|
276
|
+
// Only a space or a tab is a blank to the shells; a form feed or a carriage return is part of a word.
|
|
277
|
+
wordStart = ch === ' ' || ch === '\t';
|
|
184
278
|
i++;
|
|
185
279
|
}
|
|
186
280
|
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Where the file tools may touch the local disk.
|
|
3
|
+
*
|
|
4
|
+
* `ssh_download` writes a local file, `ssh_upload` reads one, `ssh_sync` does
|
|
5
|
+
* either. None of them looked at the local path, so an agent that could not run
|
|
6
|
+
* a single command on a `readonly` server could still have `ssh_download` write
|
|
7
|
+
* `~/.zshrc` or `~/.ssh/authorized_keys` on this machine, with content it had
|
|
8
|
+
* just put on the server (`printf … > /tmp/x` is allowed in `readonly`).
|
|
9
|
+
*
|
|
10
|
+
* A transfer root is the one directory the local side of a transfer may use:
|
|
11
|
+
*
|
|
12
|
+
* - `transferRoot` on a server (`SSH_SERVER_<NAME>_TRANSFER_ROOT`,
|
|
13
|
+
* `transfer_root` in TOML) turns it on for that server, in every mode, for
|
|
14
|
+
* all three tools;
|
|
15
|
+
* - a server in `readonly` or `restricted` mode has one even when none is set,
|
|
16
|
+
* for `ssh_download` (the other two are refused there already): the
|
|
17
|
+
* `transfers` folder of the manager home (`~/.ssh-manager/transfers`). It is
|
|
18
|
+
* created, private to the user, the first time a download needs it;
|
|
19
|
+
* - a server in `unrestricted` mode with no `transferRoot` is exactly what it
|
|
20
|
+
* was: no filter at all, by design.
|
|
21
|
+
*
|
|
22
|
+
* A path is accepted when its real location, after following every link, is
|
|
23
|
+
* inside the root. A relative path is read from the root. A link as the file
|
|
24
|
+
* itself is refused, in either direction, and so is a link directory that leads
|
|
25
|
+
* out. The checks run just before the transfer; they do not stop somebody who
|
|
26
|
+
* can already change files in the root while it runs.
|
|
27
|
+
*/
|
|
28
|
+
|
|
29
|
+
import fs from 'fs';
|
|
30
|
+
import path from 'path';
|
|
31
|
+
import { expandHomePath, managerHome } from './config-paths.js';
|
|
32
|
+
|
|
33
|
+
const CONFINED_MODES = new Set(['readonly', 'restricted']);
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* @typedef {object} TransferPolicy
|
|
37
|
+
* @property {string} root - Where the local side of a transfer must stay
|
|
38
|
+
* @property {boolean} explicit - The server names its own root
|
|
39
|
+
*/
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* The transfer policy of a server for a tool, or null when its local paths are
|
|
43
|
+
* not filtered.
|
|
44
|
+
*
|
|
45
|
+
* @param {{mode?: string, transferRoot?: string}|null|undefined} serverConfig
|
|
46
|
+
* @param {string} toolName - ssh_download, ssh_upload or ssh_sync
|
|
47
|
+
* @returns {TransferPolicy|null}
|
|
48
|
+
*/
|
|
49
|
+
export function transferPolicy(serverConfig, toolName) {
|
|
50
|
+
const configured = typeof serverConfig?.transferRoot === 'string' ? serverConfig.transferRoot.trim() : '';
|
|
51
|
+
if (configured) return { root: path.resolve(expandHomePath(configured)), explicit: true };
|
|
52
|
+
// Without a named root only the download is held to the default one: the
|
|
53
|
+
// upload and the sync cannot run at all in these two modes.
|
|
54
|
+
if (toolName === 'ssh_download' && CONFINED_MODES.has(String(serverConfig?.mode ?? '').toLowerCase())) {
|
|
55
|
+
return { root: path.join(managerHome(), 'transfers'), explicit: false };
|
|
56
|
+
}
|
|
57
|
+
return null;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* True when `candidate` is `root` or below it.
|
|
62
|
+
* @param {string} root
|
|
63
|
+
* @param {string} candidate
|
|
64
|
+
* @param {boolean} allowRoot
|
|
65
|
+
*/
|
|
66
|
+
function within(root, candidate, allowRoot) {
|
|
67
|
+
const relative = path.relative(root, candidate);
|
|
68
|
+
if (relative === '') return allowRoot;
|
|
69
|
+
return relative !== '..' && !relative.startsWith(`..${path.sep}`) && !path.isAbsolute(relative);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** @param {string} target */
|
|
73
|
+
function lstatOrNull(target) {
|
|
74
|
+
try { return fs.lstatSync(target); } catch { return null; }
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Where `target` really is: the part that exists with every link followed, then
|
|
79
|
+
* the part that does not exist yet as written.
|
|
80
|
+
* @param {string} target - Absolute path
|
|
81
|
+
* @returns {string}
|
|
82
|
+
*/
|
|
83
|
+
function realLocation(target) {
|
|
84
|
+
const missing = [];
|
|
85
|
+
let current = target;
|
|
86
|
+
while (!lstatOrNull(current)) {
|
|
87
|
+
const parent = path.dirname(current);
|
|
88
|
+
if (parent === current) break;
|
|
89
|
+
missing.unshift(path.basename(current));
|
|
90
|
+
current = parent;
|
|
91
|
+
}
|
|
92
|
+
return path.join(fs.realpathSync.native(current), ...missing);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Resolve the local side of a transfer to the path the tool must use, or throw
|
|
97
|
+
* with the reason it may not.
|
|
98
|
+
*
|
|
99
|
+
* @param {TransferPolicy} policy
|
|
100
|
+
* @param {string} localPath - As the caller gave it
|
|
101
|
+
* @param {{direction: 'read'|'write', allowRoot?: boolean}} options
|
|
102
|
+
* `read`: the path must exist. `write`: its folder is made inside the root when missing.
|
|
103
|
+
* `allowRoot`: the root itself is acceptable (a folder for a sync).
|
|
104
|
+
* @returns {string} An absolute path under the real root; a trailing separator is kept
|
|
105
|
+
*/
|
|
106
|
+
export function confineLocalPath(policy, localPath, { direction, allowRoot = false }) {
|
|
107
|
+
const given = String(localPath);
|
|
108
|
+
if (!given || given.includes('\0')) throw new Error('The local path is empty or not a valid path');
|
|
109
|
+
const hint = policy.explicit
|
|
110
|
+
? 'the transfer root set for this server (transferRoot)'
|
|
111
|
+
: 'the transfer folder of a server in readonly or restricted mode (~/.ssh-manager/transfers, or set transferRoot on the server)';
|
|
112
|
+
const outside = () => new Error(`Local path "${given}" is outside ${hint}: ${policy.root}`);
|
|
113
|
+
|
|
114
|
+
fs.mkdirSync(policy.root, { recursive: true, mode: 0o700 });
|
|
115
|
+
const realRoot = fs.realpathSync.native(policy.root);
|
|
116
|
+
const trailing = /[\\/]$/.test(given) ? path.sep : '';
|
|
117
|
+
|
|
118
|
+
// A relative path is read from the root; an absolute one is taken as written. Either
|
|
119
|
+
// way what counts is where it really is: its folder with every link followed (so
|
|
120
|
+
// /tmp/x and /private/tmp/x, or the root by another name, are the same place, and a
|
|
121
|
+
// link folder that leads out is out). The file itself is looked at below.
|
|
122
|
+
const absolute = path.isAbsolute(given) ? path.resolve(given) : path.resolve(realRoot, given);
|
|
123
|
+
const target = path.join(realLocation(path.dirname(absolute)), path.basename(absolute));
|
|
124
|
+
if (!within(realRoot, target, allowRoot)) throw outside();
|
|
125
|
+
|
|
126
|
+
const existing = lstatOrNull(target);
|
|
127
|
+
if (existing?.isSymbolicLink()) throw new Error(`Local path "${given}" is a link; a transfer does not follow links`);
|
|
128
|
+
if (direction === 'read' && !existing) throw new Error(`Local path does not exist: ${given}`);
|
|
129
|
+
if (direction === 'write' && existing?.isDirectory() && !allowRoot) throw new Error(`Local path "${given}" is a folder`);
|
|
130
|
+
if (direction === 'write' && !existing) {
|
|
131
|
+
fs.mkdirSync(trailing ? target : path.dirname(target), { recursive: true, mode: 0o700 });
|
|
132
|
+
}
|
|
133
|
+
return trailing && !target.endsWith(path.sep) ? target + trailing : target;
|
|
134
|
+
}
|