@pyrokine/mcp-ssh 2.3.2 → 2.4.0

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.
@@ -5,6 +5,7 @@
5
5
  * ssh_exec_batch, ssh_quick_exec, ssh_exec_parallel
6
6
  */
7
7
  import { z } from 'zod';
8
+ import { classifyCommandRisk } from '../command-risk.js';
8
9
  import * as fileOps from '../file-ops.js';
9
10
  import { sessionManager } from '../session-manager.js';
10
11
  import { HARD_EXEC_MAX_OUTPUT_SIZE } from '../types.js';
@@ -95,67 +96,7 @@ const execScriptSchema = z.object({
95
96
  keepScript: z.boolean().optional().describe('是否保留远端临时脚本,默认 false'),
96
97
  maxOutputSize: maxOutputSizeSchema,
97
98
  });
98
- function pushRisk(categories, signals, category, signal) {
99
- categories.add(category);
100
- signals.push(signal);
101
- }
102
- function classifyCommandRisk(command) {
103
- const signals = [];
104
- const categories = new Set();
105
- const pipeCount = (command.match(/\|/g) ?? []).length;
106
- const lowerCommand = command.toLowerCase();
107
- if (command.length > 500) {
108
- pushRisk(categories, signals, 'long-running', 'long_command');
109
- }
110
- if (/\bgrep\s+(?:-\S*R|--recursive)\b/.test(command)) {
111
- pushRisk(categories, signals, 'long-running', 'recursive_grep');
112
- }
113
- if (/\bfind\b/.test(command) && !/\s-maxdepth\s+\d+/.test(command)) {
114
- pushRisk(categories, signals, 'long-running', 'unbounded_find');
115
- }
116
- if (/\b(?:python|python3|node|perl|ruby)\b/.test(command)) {
117
- pushRisk(categories, signals, 'script-execution', 'interpreter_script');
118
- }
119
- if (/\bsleep\s+\d{2,}\b/.test(command) || /\b(?:tail\s+-f|journalctl\s+-f|watch|top|htop)\b/.test(command)) {
120
- pushRisk(categories, signals, 'long-running', 'long_running');
121
- }
122
- if (pipeCount >= 3) {
123
- pushRisk(categories, signals, 'long-running', 'long_pipeline');
124
- }
125
- if (/(?:^|[^&])&(?!&)/.test(command)) {
126
- pushRisk(categories, signals, 'process-control', 'background_task');
127
- }
128
- if (/\b(?:rm\s+-[a-zA-Z]*r|dd\s+if=|mkfs|fdisk|parted|shutdown|reboot)\b/.test(lowerCommand)) {
129
- pushRisk(categories, signals, 'destructive', 'destructive_command');
130
- }
131
- if (/\b(?:kill|pkill|killall)\b/.test(lowerCommand)) {
132
- pushRisk(categories, signals, 'process-control', 'process_control');
133
- }
134
- if (/\b(?:systemctl|service|docker|kubectl)\s+(?:restart|stop|kill|delete|rm|down)\b/.test(lowerCommand)) {
135
- pushRisk(categories, signals, 'service-control', 'service_control');
136
- }
137
- if (/\b(?:password|passwd|token|authorization|cookie|secret|private[_-]?key)\b/i.test(command)) {
138
- pushRisk(categories, signals, 'credential-bearing', 'credential_bearing');
139
- }
140
- if (/\bsu\s+-?\s*[a-zA-Z_][a-zA-Z0-9_-]*\s+-c\b/.test(command)) {
141
- pushRisk(categories, signals, 'user-switch', 'direct_su_command');
142
- }
143
- if (signals.length === 0) {
144
- return undefined;
145
- }
146
- const highCategories = new Set(['destructive', 'process-control', 'service-control', 'long-running']);
147
- const level = Array.from(categories).some((category) => highCategories.has(category)) ? 'high' : 'medium';
148
- return {
149
- level,
150
- categories: Array.from(categories),
151
- signals,
152
- suggestion: signals.includes('direct_su_command')
153
- ? '建议使用 ssh_exec 的 runAs 参数或 ssh_exec_as_user,避免手写 su 命令造成引用和环境加载差异'
154
- : level === 'high'
155
- ? '考虑用 ssh_exec_script、ssh_pty_start,或把输出重定向到远端文件后用 ssh_read_file 分块读取'
156
- : '如输出较大,请设置 maxOutputSize 或重定向到远端文件后分块读取',
157
- };
158
- }
99
+ // ========== Handlers ==========
159
100
  async function handleExec(args) {
160
101
  try {
161
102
  const result = await sessionManager.exec(args.alias, args.command, {
@@ -168,7 +109,7 @@ async function handleExec(args) {
168
109
  useLoginUser: args.useLoginUser,
169
110
  loadProfile: args.loadProfile,
170
111
  });
171
- return formatResult({ ...result, commandRisk: classifyCommandRisk(args.command) });
112
+ return formatResult({ ...result, commandRisk: classifyCommandRisk(args.command, args.timeout) });
172
113
  }
173
114
  catch (error) {
174
115
  return formatError(error);
@@ -184,7 +125,7 @@ async function handleExecAsUser(args) {
184
125
  maxOutputSize: args.maxOutputSize,
185
126
  loadProfile: args.loadProfile,
186
127
  });
187
- return formatResult({ ...result, commandRisk: classifyCommandRisk(args.command) });
128
+ return formatResult({ ...result, commandRisk: classifyCommandRisk(args.command, args.timeout) });
188
129
  }
189
130
  const result = await sessionManager.exec(args.alias, args.command, {
190
131
  timeout: args.timeout,
@@ -193,7 +134,7 @@ async function handleExecAsUser(args) {
193
134
  maxOutputSize: args.maxOutputSize,
194
135
  loadProfile: args.loadProfile,
195
136
  });
196
- return formatResult({ ...result, commandRisk: classifyCommandRisk(args.command) });
137
+ return formatResult({ ...result, commandRisk: classifyCommandRisk(args.command, args.timeout) });
197
138
  }
198
139
  catch (error) {
199
140
  return formatError(error);
@@ -216,7 +157,7 @@ async function handleExecBatch(args) {
216
157
  const stopOnError = args.stopOnError !== false;
217
158
  const results = [];
218
159
  for (let i = 0; i < args.commands.length; i++) {
219
- const commandRisk = classifyCommandRisk(args.commands[i]);
160
+ const commandRisk = classifyCommandRisk(args.commands[i], args.timeout);
220
161
  try {
221
162
  const execResult = await sessionManager.exec(args.alias, args.commands[i], { timeout: args.timeout });
222
163
  results.push({
@@ -341,7 +282,7 @@ function validateScriptUser(username) {
341
282
  }
342
283
  }
343
284
  function resolveScriptUsers(args) {
344
- const session = sessionManager.listSessions().find((item) => item.alias === args.alias);
285
+ const session = sessionManager.listSessionDetails().find((item) => item.alias === args.alias);
345
286
  return {
346
287
  effectiveUser: args.targetUser ?? args.runAs ?? session?.runAs,
347
288
  loginUser: session?.username,