@pyrokine/mcp-ssh 3.1.0 → 3.2.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.
package/dist/security.js CHANGED
@@ -20,9 +20,12 @@ function getAllowedDirs() {
20
20
  function isUnderAllowedDir(resolved) {
21
21
  const allowed = getAllowedDirs();
22
22
  const ok = allowed.some((dir) => {
23
- const relative = path.relative(dir, resolved);
24
- return (relative === '' ||
25
- (relative !== '..' && !relative.startsWith(`..${path.sep}`) && !path.isAbsolute(relative)));
23
+ const roots = fs.existsSync(dir) ? [dir, fs.realpathSync(dir)] : [dir];
24
+ return roots.some((root) => {
25
+ const relative = path.relative(root, resolved);
26
+ return (relative === '' ||
27
+ (relative !== '..' && !relative.startsWith(`..${path.sep}`) && !path.isAbsolute(relative)));
28
+ });
26
29
  });
27
30
  return { ok, allowed };
28
31
  }
@@ -75,6 +78,39 @@ export function validateConfigFile(p) {
75
78
  throw new Error(`SSH config file too large: ${stats.size} bytes (max ${MAX_CONFIG_FILE_SIZE})`);
76
79
  }
77
80
  }
81
+ /** known_hosts protects write integrity; public read access is permitted. */
82
+ export function validateKnownHostsPath(filePath, system = false) {
83
+ const resolveRealPath = (candidate) => {
84
+ let ancestor = candidate;
85
+ const suffix = [];
86
+ while (!fs.existsSync(ancestor)) {
87
+ const parent = path.dirname(ancestor);
88
+ if (parent === ancestor) {
89
+ throw new Error('known_hosts path has no existing ancestor');
90
+ }
91
+ suffix.unshift(path.basename(ancestor));
92
+ ancestor = parent;
93
+ }
94
+ return path.join(fs.realpathSync(ancestor), ...suffix);
95
+ };
96
+ const allowed = getAllowedDirs();
97
+ if (system && process.platform === 'win32') {
98
+ allowed.push(path.resolve(process.env.ProgramData ?? 'C:\\ProgramData', 'ssh'));
99
+ }
100
+ // Resolve trusted roots as well as files: system ancestors such as /etc can be aliases.
101
+ const realAllowed = allowed.map(resolveRealPath);
102
+ const under = (candidate, directories) => directories.some((dir) => {
103
+ const relative = path.relative(dir, candidate);
104
+ return (relative === '' ||
105
+ (relative !== '..' && !relative.startsWith(`..${path.sep}`) && !path.isAbsolute(relative)));
106
+ });
107
+ const resolved = path.resolve(filePath);
108
+ const real = resolveRealPath(resolved);
109
+ if (!under(resolved, [...allowed, ...realAllowed]) || !under(real, realAllowed)) {
110
+ throw new Error('known_hosts path is outside the allowed SSH directories');
111
+ }
112
+ return real;
113
+ }
78
114
  /**
79
115
  * 脱敏 ssh2 keyParser 错误,避免 PEM 文件 cipher/kdf 字段被泄露
80
116
  */
@@ -8,8 +8,9 @@
8
8
  * - 会话持久化
9
9
  */
10
10
  import { Client, SFTPWrapper } from 'ssh2';
11
+ import { type HostKeyTrust } from './known-hosts.js';
11
12
  import { OpenSshConnection } from './openssh.js';
12
- import { ExecOptions, ExecResult, ExternalTransferCapability, ForwardCloseOptions, ForwardCloseResult, OperationCancelResult, OperationInfo, OperationReadResult, OperationStartOptions, OpenSshMasterInfo, PortForwardInfo, PtyCloseResult, PtyOptions, PtyReadResult, PtySessionInfo, PtyWriteResult, SSHConnectionConfig, SSHSessionBrief, SSHSessionDetail } from './types.js';
13
+ import { ConnectionFailureDetails, ExecOptions, ExecResult, ExternalTransferCapability, ForwardCloseOptions, ForwardCloseResult, OpenSshMasterInfo, OperationCancelResult, OperationInfo, OperationReadResult, OperationStartOptions, PortForwardInfo, PtyCloseResult, PtyOptions, PtyReadResult, PtySessionInfo, PtyWriteResult, SSHConnectionConfig, SSHSessionBrief, SSHSessionDetail } from './types.js';
13
14
  interface SessionState {
14
15
  config: SSHConnectionConfig;
15
16
  connectedAt: number;
@@ -17,6 +18,12 @@ interface SessionState {
17
18
  reconnectAttempts: number;
18
19
  connected: boolean;
19
20
  authAgent?: string;
21
+ hostKey?: HostKeyTrust;
22
+ hostKeys?: (HostKeyTrust & {
23
+ host: string;
24
+ port: number;
25
+ })[];
26
+ connectionError?: ConnectionError;
20
27
  manualClose: boolean;
21
28
  /** 当前正在等待的重连定时器,disconnect 时需要清除以防 zombie 重连 */
22
29
  reconnectTimer?: NodeJS.Timeout;
@@ -24,6 +31,7 @@ interface SessionState {
24
31
  interface Ssh2Session extends SessionState {
25
32
  backend?: 'ssh2';
26
33
  client: Client;
34
+ jumpClient?: Client;
27
35
  }
28
36
  type SSHSession = Ssh2Session | (SessionState & {
29
37
  backend: 'openssh';
@@ -48,6 +56,15 @@ interface SessionConnectionInfo {
48
56
  commandShell: 'posix' | 'cmd' | 'powershell';
49
57
  remotePathStyle: 'posix' | 'win32' | 'sftp-posix';
50
58
  keyAclStatus?: 'key_path_usable' | 'key_acl_insecure' | 'key_acl_unverified';
59
+ hostKey?: HostKeyTrust;
60
+ hostKeys?: (HostKeyTrust & {
61
+ host: string;
62
+ port: number;
63
+ })[];
64
+ }
65
+ declare class ConnectionError extends Error {
66
+ readonly details: ConnectionFailureDetails;
67
+ constructor(message: string, details: ConnectionFailureDetails);
51
68
  }
52
69
  export declare class SessionManager {
53
70
  private sessions;
@@ -55,6 +72,7 @@ export declare class SessionManager {
55
72
  private connectInFlight;
56
73
  private pendingConnections;
57
74
  private connectionGenerations;
75
+ private readonly knownHosts;
58
76
  private readonly ptyManager;
59
77
  private readonly forwardManager;
60
78
  private readonly operationManager;
@@ -133,7 +151,7 @@ export declare class SessionManager {
133
151
  ptyRead(ptyId: string, options?: {
134
152
  mode?: 'screen' | 'raw';
135
153
  clear?: boolean;
136
- }): PtyReadResult;
154
+ }): Promise<PtyReadResult>;
137
155
  ptyResize(ptyId: string, rows: number, cols: number): boolean;
138
156
  ptyClose(ptyId: string): PtyCloseResult;
139
157
  ptyList(): PtySessionInfo[];
@@ -157,6 +175,7 @@ export declare class SessionManager {
157
175
  private appendWithinByteLimit;
158
176
  private appendOutputTail;
159
177
  private recommendedReadCommand;
178
+ private commandExitMetadata;
160
179
  private emptyOutputFailureMetadata;
161
180
  private outputMetadata;
162
181
  private normalizeMaxOutputSize;
@@ -182,6 +201,7 @@ export declare class SessionManager {
182
201
  private isCurrentPendingConnection;
183
202
  private cancelPendingConnection;
184
203
  private invalidateAliasResources;
204
+ private stopUntrustedConnection;
185
205
  /**
186
206
  * 检查连接是否存活
187
207
  */