@pyrokine/mcp-ssh 3.3.0 → 3.3.1
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 +7 -4
- package/README_zh.md +5 -2
- package/dist/file-ops.js +2 -1
- package/dist/known-hosts.d.ts +2 -2
- package/dist/known-hosts.js +102 -61
- package/dist/session-manager.js +1 -1
- package/dist/windows-acl.d.ts +3 -2
- package/dist/windows-acl.js +17 -4
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -10,7 +10,7 @@ SSH MCP Server for AI assistants (Claude, Cursor, Windsurf, etc.)
|
|
|
10
10
|
|
|
11
11
|

|
|
12
12
|

|
|
13
|
-
](TESTING.md)
|
|
14
14
|
|
|
15
15
|
## Migrating to 3.0
|
|
16
16
|
|
|
@@ -36,7 +36,9 @@ the returned prefix, including whether its last line ends with a newline.
|
|
|
36
36
|
|
|
37
37
|
Windows connections require `remotePlatform="windows"`; local Windows does not change the remote default. Set
|
|
38
38
|
`commandShell` and `remotePathStyle` for the host, and an explicit `rsyncProfile` when using rsync with Windows. The
|
|
39
|
-
Windows behavior and examples below remain available.
|
|
39
|
+
Windows behavior and examples below remain available. Local permission, boot-identity, and filename checks ignore
|
|
40
|
+
inherited `PSModulePath`, allowing MCP to start under PowerShell 7 while using Windows PowerShell 5.1. Remote commands
|
|
41
|
+
and user scripts retain their configured environment.
|
|
40
42
|
|
|
41
43
|
## Host key trust
|
|
42
44
|
|
|
@@ -66,8 +68,9 @@ the shared bastion and unrelated aliases. Explicit connect or reconnect verifies
|
|
|
66
68
|
retains automatic reconnect. Hashed hostnames, host/port pairs, IPv6, multiple names, wildcard patterns, and negated
|
|
67
69
|
patterns are supported. Matching CA or host-certificate entries require the OpenSSH backend. Concurrent first-use
|
|
68
70
|
writes
|
|
69
|
-
use an exclusive owner-directory lock with a bounded, cancellable wait.
|
|
70
|
-
|
|
71
|
+
use an exclusive owner-directory lock with a bounded, cancellable wait. Preparation and permission checks share the
|
|
72
|
+
connection deadline; the five-second lock-contention budget starts after preparation and stays within that deadline.
|
|
73
|
+
A complete owner record is published before writing trust. A confirmed exited owner is recovered automatically within the same verified local process domain:
|
|
71
74
|
Linux uses the `/proc` boot ID and PID namespace, macOS uses `kern.bootsessionuuid`, and Windows queries the kernel's
|
|
72
75
|
`SystemBootEnvironmentInformation.BootIdentifier` through the built-in PowerShell. Live or reused PIDs, denied
|
|
73
76
|
liveness
|
package/README_zh.md
CHANGED
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
|
|
11
11
|

|
|
12
12
|

|
|
13
|
-
](TESTING.md)
|
|
14
14
|
|
|
15
15
|
## 迁移到 3.0
|
|
16
16
|
|
|
@@ -30,6 +30,7 @@ PTY 的 `bufferSize` 参数与列表结果中的 `bufferSize`,以及 read/list
|
|
|
30
30
|
|
|
31
31
|
连接 Windows 主机须设置 `remotePlatform="windows"`,本机为 Windows 不会改变远端默认值;按主机设置 `commandShell` 和
|
|
32
32
|
`remotePathStyle`,使用 Windows rsync 时显式设置 `rsyncProfile`;下文 Windows 功能和示例继续适用
|
|
33
|
+
本机权限、启动身份与文件名检查忽略继承的 `PSModulePath`,从 PowerShell 7 启动 MCP 时仍可使用 Windows PowerShell 5.1,远端命令和用户脚本沿用各自配置的环境
|
|
33
34
|
|
|
34
35
|
## 主机密钥信任
|
|
35
36
|
|
|
@@ -48,7 +49,9 @@ SHA2 签名
|
|
|
48
49
|
目标失败保留共享跳板和无关 alias;显式 connect/reconnect 重新验证信任,普通网络断线仍自动重连
|
|
49
50
|
支持哈希主机名、host/port、IPv6、多名称、通配和否定模式,匹配
|
|
50
51
|
CA 或主机证书记录时使用 OpenSSH
|
|
51
|
-
|
|
52
|
+
后端;首次信任写入使用持有者目录排他锁,等待有期限且可取消
|
|
53
|
+
准备与权限检查共用连接总期限,五秒锁竞争预算从准备完成后开始,并受该总期限约束
|
|
54
|
+
完整持有者记录发布后才开始写入信任;在同一可核实的本地进程域内,自动恢复已核实退出的持有者
|
|
52
55
|
Linux 使用 `/proc` 启动标识与 PID 命名空间,macOS 使用 `kern.bootsessionuuid`,Windows 通过系统自带 PowerShell 查询内核的
|
|
53
56
|
`SystemBootEnvironmentInformation.BootIdentifier`
|
|
54
57
|
存活或已复用的 PID、存活探测被拒绝、启动标识或进程域无法取得或不匹配时,保留锁并等待至期限或取消
|
package/dist/file-ops.js
CHANGED
|
@@ -18,7 +18,7 @@ import { sessionManager } from './session-manager.js';
|
|
|
18
18
|
import { sftpOperationOutcome, SftpOperationTimeoutError, SftpWriteState, withSftpOperationTimeout, } from './sftp-timeout.js';
|
|
19
19
|
import { escapeShellArg, expandTilde } from './tools/utils.js';
|
|
20
20
|
import { ensureUtf8Boundary, isUtf8ContinuationByte } from './utf8.js';
|
|
21
|
-
import { checkWindowsAcl, windowsAclScript } from './windows-acl.js';
|
|
21
|
+
import { checkWindowsAcl, getWindowsPowerShellEnv, windowsAclScript } from './windows-acl.js';
|
|
22
22
|
// 文件类型 mode 常量
|
|
23
23
|
const S_IFMT = 0o170000; // 文件类型位掩码
|
|
24
24
|
const S_IFDIR = 0o40000;
|
|
@@ -1697,6 +1697,7 @@ function verifyWindowsDownloadNames(localPath, remoteNames, timeout) {
|
|
|
1697
1697
|
const script = encodedPowershell(windowsCaseSemanticsScript(parent)).split(' ').at(-1);
|
|
1698
1698
|
const result = spawnSync('powershell.exe', ['-NoProfile', '-NonInteractive', '-EncodedCommand', script], {
|
|
1699
1699
|
input: Buffer.from(JSON.stringify(names)).toString('base64'),
|
|
1700
|
+
env: getWindowsPowerShellEnv(),
|
|
1700
1701
|
encoding: 'utf8',
|
|
1701
1702
|
windowsHide: true,
|
|
1702
1703
|
timeout: Math.min(timeout ?? 10_000, 10_000),
|
package/dist/known-hosts.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { type ServerHostKeyAlgorithm } from 'ssh2';
|
|
2
|
-
import type
|
|
2
|
+
import { type ConnectionFailureDetails } from './types.js';
|
|
3
3
|
export interface HostKeyTrust {
|
|
4
4
|
status: 'known' | 'added';
|
|
5
5
|
fingerprint: string;
|
|
@@ -14,7 +14,7 @@ export declare class KnownHosts {
|
|
|
14
14
|
private readonly userFile;
|
|
15
15
|
private readonly systemFiles;
|
|
16
16
|
constructor(userFile?: string, systemFiles?: string[]);
|
|
17
|
-
getHostKeyAlgorithms(host: string, port: number): ServerHostKeyAlgorithm[];
|
|
17
|
+
getHostKeyAlgorithms(host: string, port: number, signal?: AbortSignal, deadline?: number): ServerHostKeyAlgorithm[];
|
|
18
18
|
verify(host: string, port: number, rawKey: Buffer, signal?: AbortSignal, deadline?: number): Promise<HostKeyTrust>;
|
|
19
19
|
private read;
|
|
20
20
|
private matchingKeys;
|
package/dist/known-hosts.js
CHANGED
|
@@ -6,7 +6,8 @@ import * as path from 'node:path';
|
|
|
6
6
|
import { setTimeout as delay } from 'node:timers/promises';
|
|
7
7
|
import ssh2 from 'ssh2';
|
|
8
8
|
import { validateKnownHostsPath } from './security.js';
|
|
9
|
-
import {
|
|
9
|
+
import { DEFAULT_EXEC_TIMEOUT_MS } from './types.js';
|
|
10
|
+
import { checkWindowsAcl, checkWindowsKnownHostsIntegrity, getWindowsPowerShellEnv } from './windows-acl.js';
|
|
10
11
|
export class HostKeyError extends Error {
|
|
11
12
|
hostKey;
|
|
12
13
|
constructor(message, options) {
|
|
@@ -15,24 +16,36 @@ export class HostKeyError extends Error {
|
|
|
15
16
|
this.hostKey = options?.hostKey;
|
|
16
17
|
}
|
|
17
18
|
}
|
|
18
|
-
function
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
19
|
+
function assertVerificationActive(signal, deadline) {
|
|
20
|
+
signal?.throwIfAborted();
|
|
21
|
+
if (deadline !== undefined && Date.now() >= deadline) {
|
|
22
|
+
throw new HostKeyError('known_hosts verification deadline exhausted');
|
|
22
23
|
}
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
throw new HostKeyError(`known_hosts has an untrusted owner or writer: ${current}`);
|
|
30
|
-
}
|
|
31
|
-
const parent = path.dirname(current);
|
|
32
|
-
if (parent === current) {
|
|
24
|
+
}
|
|
25
|
+
function assertIntegrity(filePath, signal, deadline) {
|
|
26
|
+
assertVerificationActive(signal, deadline);
|
|
27
|
+
try {
|
|
28
|
+
if (process.platform === 'win32') {
|
|
29
|
+
checkWindowsKnownHostsIntegrity(filePath, deadline);
|
|
33
30
|
return;
|
|
34
31
|
}
|
|
35
|
-
current =
|
|
32
|
+
let current = filePath;
|
|
33
|
+
for (;;) {
|
|
34
|
+
const stats = fs.statSync(current);
|
|
35
|
+
const trustedOwner = stats.uid === 0 || stats.uid === process.getuid?.();
|
|
36
|
+
const stickyDirectory = stats.isDirectory() && (stats.mode & 0o1000) !== 0;
|
|
37
|
+
if (!trustedOwner || ((stats.mode & 0o022) !== 0 && !stickyDirectory)) {
|
|
38
|
+
throw new HostKeyError(`known_hosts has an untrusted owner or writer: ${current}`);
|
|
39
|
+
}
|
|
40
|
+
const parent = path.dirname(current);
|
|
41
|
+
if (parent === current) {
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
current = parent;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
finally {
|
|
48
|
+
assertVerificationActive(signal, deadline);
|
|
36
49
|
}
|
|
37
50
|
}
|
|
38
51
|
function localProcessDomain(deadline) {
|
|
@@ -91,6 +104,7 @@ function localProcessDomain(deadline) {
|
|
|
91
104
|
]
|
|
92
105
|
: ['-n', 'kern.bootsessionuuid'], {
|
|
93
106
|
timeout: Math.max(1, Math.min(5000, deadline - Date.now())),
|
|
107
|
+
...(windows ? { env: getWindowsPowerShellEnv() } : {}),
|
|
94
108
|
encoding: 'utf8',
|
|
95
109
|
windowsHide: true,
|
|
96
110
|
maxBuffer: 16_384,
|
|
@@ -143,8 +157,9 @@ async function removeLockOwner(directory, owner) {
|
|
|
143
157
|
// A new owner's unique filename keeps this non-recursive removal from deleting its lock.
|
|
144
158
|
await removeEmptyLock(directory);
|
|
145
159
|
}
|
|
146
|
-
async function inspectLock(directory, domain, preparation = false) {
|
|
160
|
+
async function inspectLock(directory, domain, preparation = false, signal, deadline) {
|
|
147
161
|
try {
|
|
162
|
+
assertVerificationActive(signal, deadline);
|
|
148
163
|
const stats = await fs.promises.lstat(directory);
|
|
149
164
|
if (stats.isFile()) {
|
|
150
165
|
return 'legacy lock file has no verifiable owner; it was retained';
|
|
@@ -152,8 +167,9 @@ async function inspectLock(directory, domain, preparation = false) {
|
|
|
152
167
|
if (!stats.isDirectory()) {
|
|
153
168
|
return 'lock owner identity could not be verified; it was retained';
|
|
154
169
|
}
|
|
155
|
-
assertIntegrity(directory);
|
|
170
|
+
assertIntegrity(directory, signal, deadline);
|
|
156
171
|
const entries = await fs.promises.readdir(directory);
|
|
172
|
+
assertVerificationActive(signal, deadline);
|
|
157
173
|
if (entries.length === 0 && !preparation) {
|
|
158
174
|
await removeEmptyLock(directory);
|
|
159
175
|
return;
|
|
@@ -164,13 +180,15 @@ async function inspectLock(directory, domain, preparation = false) {
|
|
|
164
180
|
}
|
|
165
181
|
const ownerPath = path.join(directory, entries[0]);
|
|
166
182
|
const ownerStats = await fs.promises.lstat(ownerPath);
|
|
183
|
+
assertVerificationActive(signal, deadline);
|
|
167
184
|
if (!ownerStats.isFile() || ownerStats.size > 4096) {
|
|
168
185
|
return 'lock owner identity could not be verified; it was retained';
|
|
169
186
|
}
|
|
170
|
-
assertIntegrity(ownerPath);
|
|
187
|
+
assertIntegrity(ownerPath, signal, deadline);
|
|
171
188
|
let owner;
|
|
172
189
|
try {
|
|
173
190
|
owner = JSON.parse(await fs.promises.readFile(ownerPath, 'utf8'));
|
|
191
|
+
assertVerificationActive(signal, deadline);
|
|
174
192
|
}
|
|
175
193
|
catch (error) {
|
|
176
194
|
if (error instanceof SyntaxError) {
|
|
@@ -193,6 +211,7 @@ async function inspectLock(directory, domain, preparation = false) {
|
|
|
193
211
|
}
|
|
194
212
|
catch (error) {
|
|
195
213
|
if (error.code === 'ESRCH') {
|
|
214
|
+
assertVerificationActive(signal, deadline);
|
|
196
215
|
await removeLockOwner(directory, entries[0]);
|
|
197
216
|
return;
|
|
198
217
|
}
|
|
@@ -205,6 +224,7 @@ async function inspectLock(directory, domain, preparation = false) {
|
|
|
205
224
|
return 'lock is held by a live process';
|
|
206
225
|
}
|
|
207
226
|
catch (error) {
|
|
227
|
+
assertVerificationActive(signal, deadline);
|
|
208
228
|
if (error.code === 'ENOENT') {
|
|
209
229
|
return;
|
|
210
230
|
}
|
|
@@ -270,9 +290,9 @@ export class KnownHosts {
|
|
|
270
290
|
this.userFile = userFile;
|
|
271
291
|
this.systemFiles = systemFiles;
|
|
272
292
|
}
|
|
273
|
-
getHostKeyAlgorithms(host, port) {
|
|
293
|
+
getHostKeyAlgorithms(host, port, signal, deadline) {
|
|
274
294
|
const types = new Set();
|
|
275
|
-
for (const { type, revoked } of this.matchingKeys(knownHostName(host, port))) {
|
|
295
|
+
for (const { type, revoked } of this.matchingKeys(knownHostName(host, port), signal, deadline)) {
|
|
276
296
|
if (!revoked) {
|
|
277
297
|
types.add(type);
|
|
278
298
|
}
|
|
@@ -288,30 +308,28 @@ export class KnownHosts {
|
|
|
288
308
|
];
|
|
289
309
|
return algorithms.filter((algorithm) => types.has(algorithm.startsWith('rsa-sha2-') ? 'ssh-rsa' : algorithm));
|
|
290
310
|
}
|
|
291
|
-
async verify(host, port, rawKey, signal, deadline = Date.now() +
|
|
311
|
+
async verify(host, port, rawKey, signal, deadline = Date.now() + DEFAULT_EXEC_TIMEOUT_MS) {
|
|
292
312
|
const name = knownHostName(host, port);
|
|
293
313
|
const parsed = ssh2.utils.parseKey(rawKey);
|
|
294
314
|
if (parsed instanceof Error || Array.isArray(parsed) || parsed.type.includes('-cert-')) {
|
|
295
315
|
throw new HostKeyError('Unsupported or damaged server host key');
|
|
296
316
|
}
|
|
297
317
|
const fingerprint = `SHA256:${createHash('sha256').update(rawKey).digest('base64').replace(/=+$/, '')}`;
|
|
298
|
-
const check = () =>
|
|
299
|
-
signal?.throwIfAborted();
|
|
300
|
-
if (Date.now() >= deadline) {
|
|
301
|
-
throw new HostKeyError('known_hosts verification deadline exhausted');
|
|
302
|
-
}
|
|
303
|
-
};
|
|
318
|
+
const check = () => assertVerificationActive(signal, deadline);
|
|
304
319
|
check();
|
|
305
|
-
|
|
320
|
+
const known = this.lookup(name, rawKey, signal, deadline);
|
|
321
|
+
check();
|
|
322
|
+
if (known) {
|
|
306
323
|
return { status: 'known', fingerprint };
|
|
307
324
|
}
|
|
308
325
|
const real = validateKnownHostsPath(this.userFile);
|
|
309
326
|
const parent = path.dirname(real);
|
|
327
|
+
check();
|
|
310
328
|
fs.mkdirSync(parent, { recursive: true, mode: 0o700 });
|
|
311
|
-
assertIntegrity(parent);
|
|
329
|
+
assertIntegrity(parent, signal, deadline);
|
|
312
330
|
const lockPath = `${real}.mcp-lock`;
|
|
313
|
-
const
|
|
314
|
-
|
|
331
|
+
const domain = localProcessDomain(deadline);
|
|
332
|
+
check();
|
|
315
333
|
const identity = `${process.pid}-${randomBytes(16).toString('hex')}`;
|
|
316
334
|
const preparation = `${lockPath}.prepare-${identity}`;
|
|
317
335
|
const owner = `owner-${identity}.json`;
|
|
@@ -326,32 +344,48 @@ export class KnownHosts {
|
|
|
326
344
|
for (const entry of await fs.promises.readdir(parent)) {
|
|
327
345
|
if (entry.startsWith(prefix) && /^\d+-[\da-f]{32}$/.test(entry.slice(prefix.length))) {
|
|
328
346
|
check();
|
|
329
|
-
await inspectLock(path.join(parent, entry), domain, true);
|
|
347
|
+
await inspectLock(path.join(parent, entry), domain, true, signal, deadline);
|
|
330
348
|
}
|
|
331
349
|
}
|
|
332
350
|
check();
|
|
333
351
|
await fs.promises.mkdir(preparation, { mode: 0o700 });
|
|
334
352
|
prepared = true;
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
353
|
+
check();
|
|
354
|
+
if (process.platform === 'win32') {
|
|
355
|
+
const acl = checkWindowsAcl(preparation, true, deadline);
|
|
356
|
+
check();
|
|
357
|
+
if (acl !== 'key_path_usable') {
|
|
358
|
+
// noinspection ExceptionCaughtLocallyJS — 保留锁归属及清理失败信息
|
|
359
|
+
throw new HostKeyError('known_hosts lock preparation permissions could not be verified');
|
|
360
|
+
}
|
|
338
361
|
}
|
|
339
362
|
await fs.promises.writeFile(path.join(preparation, owner), JSON.stringify({ version: 1, pid: process.pid, domain }), {
|
|
340
363
|
flag: 'wx',
|
|
341
364
|
mode: 0o600,
|
|
342
365
|
});
|
|
343
|
-
assertIntegrity(path.join(preparation, owner));
|
|
366
|
+
assertIntegrity(path.join(preparation, owner), signal, deadline);
|
|
367
|
+
check();
|
|
368
|
+
const lockDeadline = Math.min(deadline, Date.now() + 5000);
|
|
344
369
|
let waitingReason = 'another writer';
|
|
345
|
-
|
|
370
|
+
const checkLockBudget = () => {
|
|
346
371
|
signal?.throwIfAborted();
|
|
347
372
|
if (Date.now() >= lockDeadline) {
|
|
348
|
-
// noinspection ExceptionCaughtLocallyJS — 保留锁归属及清理失败信息
|
|
349
373
|
throw new HostKeyError(`known_hosts is locked: ${waitingReason}`);
|
|
350
374
|
}
|
|
351
|
-
|
|
375
|
+
};
|
|
376
|
+
while (!owned) {
|
|
377
|
+
checkLockBudget();
|
|
378
|
+
let waiting;
|
|
379
|
+
try {
|
|
380
|
+
waiting = await inspectLock(lockPath, domain, false, signal, lockDeadline);
|
|
381
|
+
}
|
|
382
|
+
catch (error) {
|
|
383
|
+
checkLockBudget();
|
|
384
|
+
throw error;
|
|
385
|
+
}
|
|
352
386
|
waitingReason = waiting ?? waitingReason;
|
|
387
|
+
checkLockBudget();
|
|
353
388
|
if (!waiting) {
|
|
354
|
-
check();
|
|
355
389
|
try {
|
|
356
390
|
// Await publication even if canceled, so ownership is recorded before cleanup.
|
|
357
391
|
await fs.promises.rename(preparation, lockPath);
|
|
@@ -369,6 +403,7 @@ export class KnownHosts {
|
|
|
369
403
|
throw cause;
|
|
370
404
|
}
|
|
371
405
|
});
|
|
406
|
+
checkLockBudget();
|
|
372
407
|
if (target?.isDirectory()) {
|
|
373
408
|
await removeEmptyLock(lockPath);
|
|
374
409
|
}
|
|
@@ -380,25 +415,24 @@ export class KnownHosts {
|
|
|
380
415
|
}
|
|
381
416
|
}
|
|
382
417
|
if (!owned) {
|
|
383
|
-
|
|
384
|
-
// noinspection ExceptionCaughtLocallyJS — 保留锁归属及清理失败信息
|
|
385
|
-
throw new HostKeyError(`known_hosts is locked: ${waiting}`);
|
|
386
|
-
}
|
|
418
|
+
checkLockBudget();
|
|
387
419
|
await delay(Math.min(25, lockDeadline - Date.now()), undefined, { signal });
|
|
388
420
|
}
|
|
389
421
|
}
|
|
390
422
|
check();
|
|
391
|
-
if (this.lookup(name, rawKey)) {
|
|
423
|
+
if (this.lookup(name, rawKey, signal, deadline)) {
|
|
392
424
|
result = { status: 'known', fingerprint };
|
|
393
425
|
}
|
|
394
426
|
else {
|
|
395
|
-
const original = this.read(this.userFile, false);
|
|
427
|
+
const original = this.read(this.userFile, false, signal, deadline);
|
|
396
428
|
const existed = fs.existsSync(real);
|
|
397
429
|
const before = existed ? fs.statSync(real) : undefined;
|
|
430
|
+
check();
|
|
398
431
|
file = await fs.promises.open(real, existed ? fs.constants.O_WRONLY | fs.constants.O_APPEND | (fs.constants.O_NOFOLLOW ?? 0) : 'ax', 0o600);
|
|
399
432
|
check();
|
|
400
|
-
assertIntegrity(real);
|
|
433
|
+
assertIntegrity(real, signal, deadline);
|
|
401
434
|
const opened = await file.stat();
|
|
435
|
+
check();
|
|
402
436
|
if (before && (opened.ino !== before.ino || opened.dev !== before.dev || opened.size !== before.size)) {
|
|
403
437
|
// noinspection ExceptionCaughtLocallyJS — 保留锁归属及清理失败信息
|
|
404
438
|
throw new HostKeyError('known_hosts changed while opening it');
|
|
@@ -476,27 +510,34 @@ export class KnownHosts {
|
|
|
476
510
|
}
|
|
477
511
|
return result;
|
|
478
512
|
}
|
|
479
|
-
read(filePath, system) {
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
513
|
+
read(filePath, system, signal, deadline) {
|
|
514
|
+
assertVerificationActive(signal, deadline);
|
|
515
|
+
try {
|
|
516
|
+
const real = validateKnownHostsPath(filePath, system);
|
|
517
|
+
if (!fs.existsSync(real)) {
|
|
518
|
+
return '';
|
|
519
|
+
}
|
|
520
|
+
assertIntegrity(real, signal, deadline);
|
|
521
|
+
const stats = fs.statSync(real);
|
|
522
|
+
if (!stats.isFile() || stats.size > 8 * 1024 * 1024) {
|
|
523
|
+
throw new HostKeyError('known_hosts must be a regular file no larger than 8 MiB');
|
|
524
|
+
}
|
|
525
|
+
return fs.readFileSync(real, 'utf8');
|
|
483
526
|
}
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
if (!stats.isFile() || stats.size > 8 * 1024 * 1024) {
|
|
487
|
-
throw new HostKeyError('known_hosts must be a regular file no larger than 8 MiB');
|
|
527
|
+
finally {
|
|
528
|
+
assertVerificationActive(signal, deadline);
|
|
488
529
|
}
|
|
489
|
-
return fs.readFileSync(real, 'utf8');
|
|
490
530
|
}
|
|
491
|
-
*matchingKeys(hostname) {
|
|
531
|
+
*matchingKeys(hostname, signal, deadline) {
|
|
492
532
|
const files = [
|
|
493
533
|
[this.userFile, false],
|
|
494
534
|
[`${this.userFile}2`, false],
|
|
495
535
|
...this.systemFiles.map((name) => [name, true]),
|
|
496
536
|
];
|
|
497
537
|
for (const [filePath, system] of files) {
|
|
498
|
-
const lines = this.read(filePath, system).split(/\r?\n/);
|
|
538
|
+
const lines = this.read(filePath, system, signal, deadline).split(/\r?\n/);
|
|
499
539
|
for (let index = 0; index < lines.length; ++index) {
|
|
540
|
+
assertVerificationActive(signal, deadline);
|
|
500
541
|
const line = lines[index].trim();
|
|
501
542
|
if (!line || line.startsWith('#')) {
|
|
502
543
|
continue;
|
|
@@ -528,14 +569,14 @@ export class KnownHosts {
|
|
|
528
569
|
}
|
|
529
570
|
}
|
|
530
571
|
}
|
|
531
|
-
lookup(hostname, rawKey) {
|
|
572
|
+
lookup(hostname, rawKey, signal, deadline) {
|
|
532
573
|
let accepted = false;
|
|
533
574
|
const hostKey = {
|
|
534
575
|
keyType: ssh2.utils.parseKey(rawKey).type,
|
|
535
576
|
fingerprint: `SHA256:${createHash('sha256').update(rawKey).digest('base64').replace(/=+$/, '')}`,
|
|
536
577
|
knownKeys: [],
|
|
537
578
|
};
|
|
538
|
-
for (const { key, type, revoked, file, line } of this.matchingKeys(hostname)) {
|
|
579
|
+
for (const { key, type, revoked, file, line } of this.matchingKeys(hostname, signal, deadline)) {
|
|
539
580
|
hostKey.knownKeys.push({
|
|
540
581
|
keyType: type,
|
|
541
582
|
fingerprint: `SHA256:${createHash('sha256').update(key).digest('base64').replace(/=+$/, '')}`,
|
package/dist/session-manager.js
CHANGED
|
@@ -1568,7 +1568,7 @@ export class SessionManager {
|
|
|
1568
1568
|
});
|
|
1569
1569
|
try {
|
|
1570
1570
|
verificationDeadline = Date.now() + readyTimeout;
|
|
1571
|
-
const algorithms = this.knownHosts.getHostKeyAlgorithms(config.host, config.port ?? 22);
|
|
1571
|
+
const algorithms = this.knownHosts.getHostKeyAlgorithms(config.host, config.port ?? 22, verificationSignal, verificationDeadline);
|
|
1572
1572
|
if (algorithms.length > 0) {
|
|
1573
1573
|
// ssh2 的 prepend 跳过已有项,先 remove 才能调整默认顺序
|
|
1574
1574
|
connectConfig.algorithms = {
|
package/dist/windows-acl.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
export type AclStatus = 'key_path_usable' | 'key_acl_insecure' | 'key_acl_unverified';
|
|
2
|
+
export declare function getWindowsPowerShellEnv(): NodeJS.ProcessEnv;
|
|
2
3
|
export declare function windowsAclScript(filePath: string, apply: boolean): string;
|
|
3
4
|
export declare function windowsTemporaryPathScript(name: string): string;
|
|
4
|
-
export declare function checkWindowsKnownHostsIntegrity(filePath: string): void;
|
|
5
|
-
export declare function checkWindowsAcl(filePath: string, apply?: boolean): AclStatus;
|
|
5
|
+
export declare function checkWindowsKnownHostsIntegrity(filePath: string, deadline?: number): void;
|
|
6
|
+
export declare function checkWindowsAcl(filePath: string, apply?: boolean, deadline?: number): AclStatus;
|
package/dist/windows-acl.js
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
import { spawnSync } from 'node:child_process';
|
|
2
2
|
import { encodedPowershell, quotePowershell } from './remote-platform.js';
|
|
3
|
+
export function getWindowsPowerShellEnv() {
|
|
4
|
+
return Object.fromEntries(Object.entries(process.env).filter(([name]) => name.toLowerCase() !== 'psmodulepath'));
|
|
5
|
+
}
|
|
3
6
|
export function windowsAclScript(filePath, apply) {
|
|
4
7
|
const target = quotePowershell(filePath);
|
|
5
8
|
return ('\n' +
|
|
@@ -117,10 +120,14 @@ export function windowsTemporaryPathScript(name) {
|
|
|
117
120
|
`${quotePowershell(name)}` +
|
|
118
121
|
'))\n');
|
|
119
122
|
}
|
|
120
|
-
export function checkWindowsKnownHostsIntegrity(filePath) {
|
|
123
|
+
export function checkWindowsKnownHostsIntegrity(filePath, deadline) {
|
|
121
124
|
if (process.platform !== 'win32') {
|
|
122
125
|
throw new Error('Windows ACL verification requires a native Windows process');
|
|
123
126
|
}
|
|
127
|
+
const timeout = deadline === undefined ? 10_000 : Math.min(10_000, deadline - Date.now());
|
|
128
|
+
if (timeout <= 0) {
|
|
129
|
+
throw new Error('known_hosts verification deadline exhausted');
|
|
130
|
+
}
|
|
124
131
|
const script = "$ErrorActionPreference = 'Stop'\n" +
|
|
125
132
|
'$identity = [Security.Principal.WindowsIdentity]::GetCurrent().User.Valu' +
|
|
126
133
|
'e\n' +
|
|
@@ -165,7 +172,8 @@ export function checkWindowsKnownHostsIntegrity(filePath) {
|
|
|
165
172
|
"[Console]::Write('verified')";
|
|
166
173
|
const encoded = encodedPowershell(script).split(' ').at(-1);
|
|
167
174
|
const result = spawnSync('powershell.exe', ['-NoProfile', '-NonInteractive', '-EncodedCommand', encoded], {
|
|
168
|
-
timeout
|
|
175
|
+
timeout,
|
|
176
|
+
env: getWindowsPowerShellEnv(),
|
|
169
177
|
encoding: 'utf8',
|
|
170
178
|
windowsHide: true,
|
|
171
179
|
maxBuffer: 16_384,
|
|
@@ -174,14 +182,19 @@ export function checkWindowsKnownHostsIntegrity(filePath) {
|
|
|
174
182
|
throw new Error('known_hosts write integrity could not be verified on Windows');
|
|
175
183
|
}
|
|
176
184
|
}
|
|
177
|
-
export function checkWindowsAcl(filePath, apply = false) {
|
|
185
|
+
export function checkWindowsAcl(filePath, apply = false, deadline) {
|
|
178
186
|
if (process.platform !== 'win32') {
|
|
179
187
|
throw new Error('Windows ACL verification requires a native Windows process');
|
|
180
188
|
}
|
|
189
|
+
const timeout = deadline === undefined ? 10_000 : Math.min(10_000, deadline - Date.now());
|
|
190
|
+
if (timeout <= 0) {
|
|
191
|
+
return 'key_acl_unverified';
|
|
192
|
+
}
|
|
181
193
|
const command = encodedPowershell(windowsAclScript(filePath, apply));
|
|
182
194
|
const encoded = command.split(' ').at(-1);
|
|
183
195
|
const result = spawnSync('powershell.exe', ['-NoProfile', '-NonInteractive', '-EncodedCommand', encoded], {
|
|
184
|
-
timeout
|
|
196
|
+
timeout,
|
|
197
|
+
env: getWindowsPowerShellEnv(),
|
|
185
198
|
encoding: 'utf8',
|
|
186
199
|
windowsHide: true,
|
|
187
200
|
maxBuffer: 16_384,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pyrokine/mcp-ssh",
|
|
3
|
-
"version": "3.3.
|
|
3
|
+
"version": "3.3.1",
|
|
4
4
|
"description": "A comprehensive SSH MCP Server for Claude Code",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -57,5 +57,5 @@
|
|
|
57
57
|
"README_zh.md",
|
|
58
58
|
"LICENSE"
|
|
59
59
|
],
|
|
60
|
-
"gitHead": "
|
|
60
|
+
"gitHead": "3cdfd27ab6a62e45882e64ef05b9c49f82694604"
|
|
61
61
|
}
|