ssh-mcp 2.2.2 → 2.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -150,7 +150,7 @@ sessionMaxPerConnection = 5
150
150
  sessionIdleTimeoutMs = 600000 # 10min
151
151
  sessionBackgroundMaxMs = 3600000 # 1hr
152
152
  commandTimeoutMs = 60000
153
- commandMaxChars = 5000
153
+ commandMaxChars = 5000 # 0 = unlimited, the config spelling of --maxChars=none
154
154
  commandMaxOutputBytes = 1048576 # 1MB
155
155
  connectionIdleReapMs = 900000 # 15min
156
156
  commandQuotaPerDay = 0 # 0 = unlimited; circuit breaker for runaway agents
@@ -177,6 +177,7 @@ cert = false # SSH CA cert auth — auto-detects keyRef-c
177
177
  sessionMaxPerConnection = 3 # per-profile override
178
178
  sessionIdleTimeoutMs = 300000 # stricter for prod
179
179
  commandQuotaPerDay = 200 # per-profile override
180
+ maxChars = 2000 # per-profile override; stricter for prod
180
181
 
181
182
  # Optional. Merged over the built-in role matrix; see "Policy Engine" below.
182
183
  # roleBindings is keyed by role and then by tier, so the block below changes
@@ -554,7 +555,7 @@ Secrets are **never** passed as CLI arguments.
554
555
  | `--workdir` | — | Quick start: Working directory for commands and sessions |
555
556
  | `--group` | prod | Quick start: Policy tier — `prod`, `staging` or `dev` |
556
557
  | `--timeout` | 60000 | Command timeout in ms |
557
- | `--maxChars` | 5000 | Max command length (`none` or `0` disables the limit) |
558
+ | `--maxChars` | 5000 | Max command length (`none` or `0` disables the limit; in a config file the same setting is `commandMaxChars = 0`) |
558
559
  | `--sessionMax` | 5 | Max concurrent sessions per connection |
559
560
  | `--sessionTtl` | 600000 | Session idle timeout in ms |
560
561
  | `--transport` | stdio | `stdio` or `http` |
@@ -67,6 +67,22 @@ export async function loadConfig(customPath) {
67
67
  }
68
68
  return normalizeConfig(result.data);
69
69
  }
70
+ /**
71
+ * `0` means unlimited, the convention `commandQuotaPerDay` and
72
+ * `approvalGrantTtlMs` already use in this config file.
73
+ *
74
+ * It has to be *mapped* rather than merely permitted. `sanitizeCommand` tests
75
+ * `cleaned.length > maxChars`, so a literal `0` arriving there rejects every
76
+ * non-empty command with "Command is too long (max 0 characters)" — a worse
77
+ * failure than the one this spelling exists to fix.
78
+ *
79
+ * MAX_SAFE_INTEGER is the value `parseMaxChars` produces for `--maxChars=none`,
80
+ * so the flag and the config file hand the rest of the code an identical
81
+ * Profile rather than two spellings of uncapped (#123).
82
+ */
83
+ function uncapZero(maxChars) {
84
+ return maxChars === 0 ? Number.MAX_SAFE_INTEGER : maxChars;
85
+ }
70
86
  /**
71
87
  * Resolve every profile against [defaults]. Each of these keys is documented as
72
88
  * a default that profiles inherit, so all of them must cascade — a key that is
@@ -75,13 +91,23 @@ export async function loadConfig(customPath) {
75
91
  * downgrade, not just a papercut).
76
92
  */
77
93
  function normalizeConfig(raw) {
94
+ // commandMaxChars is mapped here as well as on the profile below, so no `0`
95
+ // survives anywhere in AppConfig. Leaving the raw value on `defaults` left one
96
+ // object carrying two encodings of "unlimited" — `profile.maxChars` as
97
+ // MAX_SAFE_INTEGER, `defaults.commandMaxChars` as 0 — with nothing in the
98
+ // types to tell them apart. index.ts:161 already copies straight across
99
+ // (`maxChars: defaults.commandMaxChars`), and was correct only because that
100
+ // path's value comes from parseMaxChars, which never returns 0. Correct by
101
+ // coincidence of another surface is not a property to leave standing when the
102
+ // wrong encoding rejects every non-empty command.
78
103
  const defaults = {
79
104
  ...raw.defaults,
105
+ commandMaxChars: uncapZero(raw.defaults.commandMaxChars),
80
106
  };
81
107
  const profiles = raw.profiles.map((p) => ({
82
108
  ...p,
83
109
  timeout: p.timeout ?? defaults.commandTimeoutMs,
84
- maxChars: p.maxChars ?? defaults.commandMaxChars,
110
+ maxChars: uncapZero(p.maxChars ?? defaults.commandMaxChars),
85
111
  maxOutputBytes: p.maxOutputBytes ?? defaults.commandMaxOutputBytes,
86
112
  approvalPolicy: p.approvalPolicy ?? defaults.approvalMode,
87
113
  sessionMaxPerConnection: p.sessionMaxPerConnection ?? defaults.sessionMaxPerConnection,
@@ -18,7 +18,10 @@ export const defaultsSchema = z.object({
18
18
  sessionIdleTimeoutMs: z.number().int().positive().default(600_000),
19
19
  sessionBackgroundMaxMs: z.number().int().positive().default(3_600_000),
20
20
  commandTimeoutMs: z.number().int().positive().default(60_000),
21
- commandMaxChars: z.number().int().positive().default(5000),
21
+ // 0 = unlimited, matching `--maxChars=none` on the CLI. normalizeConfig()
22
+ // maps it to Number.MAX_SAFE_INTEGER so both surfaces hand the rest of the
23
+ // code an identical Profile.
24
+ commandMaxChars: z.number().int().nonnegative().default(5000),
22
25
  commandMaxOutputBytes: z.number().int().positive().default(1_048_576),
23
26
  connectionIdleReapMs: z.number().int().positive().default(900_000),
24
27
  // 0 = unlimited. A circuit breaker for runaway agents, not a rate limit.
@@ -50,7 +53,8 @@ export const profileSchema = z.object({
50
53
  // a schema-level .default() would make "user omitted it" indistinguishable
51
54
  // from "user set the default value", silently shadowing [defaults].
52
55
  timeout: z.number().int().positive().optional(),
53
- maxChars: z.number().int().positive().optional(),
56
+ // 0 = unlimited, as in [defaults].commandMaxChars above.
57
+ maxChars: z.number().int().nonnegative().optional(),
54
58
  maxOutputBytes: z.number().int().positive().optional(),
55
59
  approvalPolicy: approvalModeSchema.optional(),
56
60
  sessionMaxPerConnection: z.number().int().positive().optional(),
@@ -47,28 +47,38 @@ const READ_ONLY_ALLOWLIST = new Set([
47
47
  * purposes; see DESTRUCTIVE_DENYLIST below.
48
48
  */
49
49
  /*
50
- * Every `\s+` here is followed by a literal. Where a `.*` or `[^|]*` comes
51
- * next, the quantifier before it is a single `\s`: `\s+.*` lets both halves
52
- * claim the same run of spaces, so a non-matching command is retried from every
53
- * split and the match goes quadratic. sanitizeCommand caps commands at
54
- * profile.maxChars (5000 by default) long before this runs, which is what keeps
55
- * that cheap — but a policy check should not depend on a limit set three layers
56
- * away and configurable to any value.
50
+ * These are the patterns that stayed regexes, and every one of them is linear.
51
+ *
52
+ * The four that were not — `dd\s.*\bof=/dev/`, the two `curl|wget …\|\s*(sh…)`
53
+ * forms, and `chown\s+-R\s.*\s/\s*$` — are now segment checks below. An earlier
54
+ * comment here reasoned that ambiguity *within* a match was the only cost, and
55
+ * that was wrong: the engine also restarts at every offset where the cheap
56
+ * literal head matches, so a command built from `dd curl wget chown -R x `
57
+ * repeated ran at 4x per doubling. Measured on the real chain: 64 KB took
58
+ * 255 ms, 1 MB took 65 seconds of blocked event loop, and the stall lands in
59
+ * classifyCommand — before the approval gate and before the allow/deny
60
+ * decision, so no role, approval mode or readOnly flag protects against it.
61
+ *
62
+ * The old note said sanitizeCommand's maxChars cap kept this cheap, "but a
63
+ * policy check should not depend on a limit set three layers away and
64
+ * configurable to any value". Once a config file could say `commandMaxChars = 0`
65
+ * (#123) that limit went away, and the prediction came true. The rewrite below
66
+ * is what that sentence was asking for: the policy check is now safe on its own
67
+ * terms, whatever maxChars says.
68
+ *
69
+ * The existing cost test did not catch it because its seeds are runs of spaces,
70
+ * which never match the literal heads and so never trigger the restart.
57
71
  */
58
72
  const FORBIDDEN_PATTERNS = [
59
73
  /rm\s+-rf?\s+\/(\s|$)/, // rm -rf / — the filesystem root itself
60
74
  /mkfs\./,
61
- /dd\s.*\bof=\/dev\//,
62
75
  />\s*\/dev\/sd/,
63
76
  /:\(\)\s*\{\s*:\|:\&\s*\}\s*;\s*:/, // fork bomb
64
- /curl\s[^|]*\|\s*(sh|bash|zsh)/,
65
- /wget\s[^|]*\|\s*(sh|bash|zsh)/,
66
77
  />\s*\/etc\/cron/,
67
78
  />\s*\/etc\/systemd/,
68
79
  />\s*~\/.ssh\/authorized_keys/,
69
80
  /\biptables\s+-F\b/,
70
81
  /\bchmod\s+-R\s+777\s+\//,
71
- /\bchown\s+-R\s.*\s\/\s*$/,
72
82
  ];
73
83
  /**
74
84
  * Words that are forbidden when *invoked*, which the patterns above cannot
@@ -104,20 +114,12 @@ function stripPath(word) {
104
114
  const slash = word.lastIndexOf('/');
105
115
  return slash === -1 ? word : word.slice(slash + 1);
106
116
  }
107
- /**
108
- * The command words a shell would actually execute — the head of every
109
- * `;`/`&&`/`||`/`|`/newline-separated segment, past any privilege prefix, plus
110
- * the arguments of a multiplexer like `systemctl`.
111
- *
112
- * Pure string work: split, trim, set lookups. No quantifiers, so nothing here
113
- * can backtrack.
114
- */
115
- function invokedWords(command) {
116
- const invoked = [];
117
- for (const segment of command.split(/[;&|\n]/)) {
118
- const words = segment.trim().split(/\s+/).filter(Boolean).map(stripPath);
117
+ function parseSegments(command) {
118
+ const segments = [];
119
+ for (const raw of command.split(/[;&|\n]/)) {
120
+ const words = raw.trim().split(/\s+/).filter(Boolean);
119
121
  let i = 0;
120
- while (i < words.length && PRIVILEGE_PREFIXES.has(words[i])) {
122
+ while (i < words.length && PRIVILEGE_PREFIXES.has(stripPath(words[i]))) {
121
123
  i++;
122
124
  while (i < words.length && words[i].startsWith('-')) {
123
125
  const consumesValue = PREFIX_VALUE_FLAGS.has(words[i]);
@@ -126,25 +128,73 @@ function invokedWords(command) {
126
128
  i++;
127
129
  }
128
130
  }
129
- const head = words[i];
130
- if (!head)
131
- continue;
131
+ if (i < words.length) {
132
+ segments.push({ head: stripPath(words[i]), args: words.slice(i + 1) });
133
+ }
134
+ }
135
+ return segments;
136
+ }
137
+ function invokedWords(command) {
138
+ const invoked = [];
139
+ for (const { head, args } of parseSegments(command)) {
132
140
  invoked.push(head);
133
141
  // `systemctl reboot` restarts the host. Reading a unit that happens to be
134
142
  // named after a power action is rare enough that erring towards refusal
135
143
  // here costs little.
136
144
  if (ACTION_MULTIPLEXERS.has(head)) {
137
- invoked.push(...words.slice(i + 1).filter((w) => !w.startsWith('-')));
145
+ invoked.push(...args.filter((w) => !w.startsWith('-')).map(stripPath));
138
146
  }
139
147
  }
140
148
  return invoked;
141
149
  }
150
+ const SHELLS = new Set(['sh', 'bash', 'zsh']);
151
+ const DOWNLOADERS = new Set(['curl', 'wget']);
152
+ /**
153
+ * A download piped into a shell.
154
+ *
155
+ * Split on `|` rather than reading the whole string, so cost is linear in the
156
+ * command's length no matter how the two halves are spaced. `||` produces an
157
+ * empty part between them, and this deliberately still matches: `curl x || sh`
158
+ * runs a shell when the download fails, which is not meaningfully safer than
159
+ * running one when it succeeds.
160
+ */
161
+ function pipesDownloadIntoShell(command) {
162
+ const heads = command.split('|').map((part) => parseSegments(part)[0]?.head);
163
+ const firstDownload = heads.findIndex((h) => h !== undefined && DOWNLOADERS.has(h));
164
+ if (firstDownload === -1)
165
+ return false;
166
+ return heads.slice(firstDownload + 1).some((h) => h !== undefined && SHELLS.has(h));
167
+ }
168
+ /** `dd … of=/dev/sda` — writing an image straight onto a block device. */
169
+ function writesToDevice(command) {
170
+ return parseSegments(command).some(({ head, args }) => head === 'dd' && args.some((a) => a.startsWith('of=/dev/')));
171
+ }
172
+ /**
173
+ * `chown -R … /` — a recursive chown whose target is the filesystem root.
174
+ *
175
+ * The last non-flag argument is the target; `chown -R app:app /srv/app` is
176
+ * ordinary and stays allowed.
177
+ */
178
+ function chownsRoot(command) {
179
+ return parseSegments(command).some(({ head, args }) => {
180
+ if (head !== 'chown' || !args.includes('-R'))
181
+ return false;
182
+ const positional = args.filter((a) => !a.startsWith('-'));
183
+ return positional[positional.length - 1] === '/';
184
+ });
185
+ }
142
186
  const FORBIDDEN_RULES = [
143
187
  ...FORBIDDEN_PATTERNS.map((re) => ({ label: String(re), test: (c) => re.test(c) })),
144
188
  {
145
189
  label: 'invoking a power-state command (shutdown, reboot, halt, poweroff) or eval',
146
190
  test: (command) => invokedWords(command).some((w) => FORBIDDEN_INVOCATIONS.has(w)),
147
191
  },
192
+ {
193
+ label: 'piping a download into a shell (curl or wget into sh, bash or zsh)',
194
+ test: pipesDownloadIntoShell,
195
+ },
196
+ { label: 'dd writing to a block device (of=/dev/…)', test: writesToDevice },
197
+ { label: 'a recursive chown of the filesystem root', test: chownsRoot },
148
198
  ];
149
199
  /**
150
200
  * Which never-allowed rule this command trips, or null. The single entry point:
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "ssh-mcp",
3
3
  "license": "MIT",
4
- "version": "2.2.2",
4
+ "version": "2.2.3",
5
5
  "description": "MCP server exposing policy-gated, audited SSH access for Linux and Windows hosts via the Model Context Protocol.",
6
6
  "type": "module",
7
7
  "bin": {