@pyrokine/mcp-ssh 3.0.0 → 3.1.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.
@@ -3,6 +3,7 @@ import * as fs from 'fs';
3
3
  import * as path from 'path';
4
4
  import { remoteJoin } from './remote-platform.js';
5
5
  import { formatFileMode } from './file-mode.js';
6
+ import { escapeShellArg } from './tools/utils.js';
6
7
  export const DIRECTORY_VERIFY_MAX_ENTRIES = 50_000;
7
8
  export const DIRECTORY_VERIFY_MAX_MISMATCHES = 20;
8
9
  export const DIRECTORY_VERIFY_DEFAULT_MAX_FILE_BYTES = 256 * 1024 * 1024;
@@ -239,7 +240,7 @@ export async function createRemoteDirectoryManifest(sftp, rootPath, request, exc
239
240
  }
240
241
  });
241
242
  });
242
- if (!batch || batch.length === 0) {
243
+ if (!batch) {
243
244
  break;
244
245
  }
245
246
  for (const item of batch) {
@@ -334,6 +335,134 @@ export async function createRemoteDirectoryManifest(sftp, rootPath, request, exc
334
335
  unsupportedSamples,
335
336
  };
336
337
  }
338
+ export async function createOpenSshDirectoryManifest(connection, rootPath, request, timeout, exclude, followSymlinks = false) {
339
+ const deadline = Date.now() + timeout;
340
+ const remaining = () => {
341
+ const value = deadline - Date.now();
342
+ if (value <= 0)
343
+ throw new Error('OpenSSH directory verification exhausted its timeout');
344
+ return value;
345
+ };
346
+ const maxEntries = Math.min(request.maxEntries ?? 10_000, DIRECTORY_VERIFY_MAX_ENTRIES);
347
+ const maxFileBytes = Math.min(request.maxFileBytes ?? DIRECTORY_VERIFY_DEFAULT_MAX_FILE_BYTES, DIRECTORY_VERIFY_MAX_FILE_BYTES);
348
+ const maxTotalBytes = Math.min(request.maxTotalBytes ?? DIRECTORY_VERIFY_DEFAULT_MAX_TOTAL_BYTES, DIRECTORY_VERIFY_MAX_TOTAL_BYTES);
349
+ const rootBytes = await connection.capture(`realpath -ze -- ${escapeShellArg(rootPath)}`, remaining(), 64 * 1024);
350
+ if (rootBytes.at(-1) !== 0)
351
+ throw new Error('OpenSSH realpath did not return a complete path');
352
+ const root = new TextDecoder('utf8', { fatal: true }).decode(rootBytes.subarray(0, -1));
353
+ if (((await connection.stat(root, remaining())).mode & 0o170000) !== 0o040000) {
354
+ throw new Error('remote directory verification root is not a directory');
355
+ }
356
+ const entries = [];
357
+ const unsupportedSamples = [];
358
+ let skippedSymlinks = 0;
359
+ let skippedUnsupported = 0;
360
+ let limitReason;
361
+ let pending = Buffer.alloc(0);
362
+ const fields = [];
363
+ let metadataBytes = 0;
364
+ const metadataLimit = Math.min(128 * 1024 * 1024, Math.max(64 * 1024, maxEntries * 2048));
365
+ const limit = new Error('OpenSSH manifest resource limit reached');
366
+ const decoder = new TextDecoder('utf8', { fatal: true, ignoreBOM: true });
367
+ const consumeEntry = () => {
368
+ const [relative, kind, sizeText, modeText, owner, fullPath] = fields;
369
+ fields.length = 0;
370
+ const parts = relative.split('/');
371
+ if (relative && (relative.startsWith('/') || parts.some((part) => !part || part === '.' || part === '..'))) {
372
+ throw new Error('OpenSSH directory verification returned an unsafe relative path');
373
+ }
374
+ if (relative &&
375
+ parts.some((_part, index) => matchesDirectoryExclude(parts.slice(0, index + 1).join('/'), exclude)))
376
+ return;
377
+ if (relative && parts.length > 64) {
378
+ limitReason = 'depth';
379
+ throw limit;
380
+ }
381
+ if (entries.length >= maxEntries) {
382
+ limitReason = 'entries';
383
+ throw limit;
384
+ }
385
+ if (kind === 'l') {
386
+ ++skippedSymlinks;
387
+ return;
388
+ }
389
+ if (kind !== 'f' && kind !== 'd') {
390
+ ++skippedUnsupported;
391
+ if (unsupportedSamples.length < 10)
392
+ unsupportedSamples.push(relative);
393
+ return;
394
+ }
395
+ const size = Number(sizeText);
396
+ const mode = Number.parseInt(modeText, 8);
397
+ if (!Number.isSafeInteger(size) ||
398
+ size < 0 ||
399
+ !/^[0-7]{1,4}$/.test(modeText) ||
400
+ !/^\d+$/.test(owner) ||
401
+ !fullPath) {
402
+ throw new Error('OpenSSH directory verification returned invalid file metadata');
403
+ }
404
+ entries.push({
405
+ path: relative || '.',
406
+ type: kind === 'd' ? 'directory' : 'file',
407
+ size: kind === 'f' ? size : 0,
408
+ mode: formatFileMode(mode),
409
+ owner,
410
+ });
411
+ };
412
+ const format = '%P\\0%y\\0%s\\0%m\\0%U\\0%p\\0';
413
+ try {
414
+ await connection.consume(`LC_ALL=C find ${followSymlinks ? '-L' : '-P'} ${escapeShellArg(root)} -maxdepth 65 -printf ${escapeShellArg(format)}`, (chunk) => {
415
+ metadataBytes += chunk.length;
416
+ if (metadataBytes > metadataLimit) {
417
+ limitReason = 'entries';
418
+ throw limit;
419
+ }
420
+ pending = Buffer.concat([pending, chunk]);
421
+ let end;
422
+ while ((end = pending.indexOf(0)) >= 0) {
423
+ fields.push(decoder.decode(pending.subarray(0, end)));
424
+ pending = pending.subarray(end + 1);
425
+ if (fields.length === 6)
426
+ consumeEntry();
427
+ }
428
+ if (pending.length > 64 * 1024)
429
+ throw new Error('OpenSSH manifest field exceeded its byte budget');
430
+ }, remaining());
431
+ if (pending.length || fields.length)
432
+ throw new Error('OpenSSH manifest ended with incomplete metadata');
433
+ }
434
+ catch (error) {
435
+ if (error !== limit)
436
+ throw error;
437
+ }
438
+ if (!limitReason && request.sha256) {
439
+ let hashedBytes = 0;
440
+ for (const entry of entries) {
441
+ if (entry.type !== 'file')
442
+ continue;
443
+ if (entry.size > maxFileBytes) {
444
+ limitReason = 'file_bytes';
445
+ break;
446
+ }
447
+ if (hashedBytes + entry.size > maxTotalBytes) {
448
+ limitReason = 'total_bytes';
449
+ break;
450
+ }
451
+ entry.sha256 = await connection.hashFile(remoteJoin('posix', root, entry.path), 'sha256', remaining(), Math.min(maxFileBytes, maxTotalBytes - hashedBytes), entry.size);
452
+ hashedBytes += entry.size;
453
+ }
454
+ }
455
+ return {
456
+ entries,
457
+ count: entries.length,
458
+ rootSha256: rootHash(entries),
459
+ limited: limitReason !== undefined,
460
+ limitReason,
461
+ skippedSymlinks,
462
+ skippedUnsupported,
463
+ unsupportedSamples,
464
+ };
465
+ }
337
466
  export function createEmptyDirectoryManifest() {
338
467
  const entries = [];
339
468
  return {
@@ -365,7 +494,7 @@ export function compareDirectoryManifests(expected, actual, request, deletionBas
365
494
  return {
366
495
  kind: 'directory',
367
496
  skipped: true,
368
- reason: 'directory verification skipped symbolic links; set followSymlinks=true to verify link targets',
497
+ reason: 'directory verification skipped symbolic links and cannot verify the complete requested tree',
369
498
  expectedSkippedSymlinks: expected.skippedSymlinks,
370
499
  actualSkippedSymlinks: actual.skippedSymlinks,
371
500
  deletionBaselineSkippedSymlinks: deletionBaseline?.skippedSymlinks,
@@ -68,7 +68,10 @@ export declare function probeLocalPath(localPath: string, followSymlinks?: boole
68
68
  matched?: boolean;
69
69
  };
70
70
  };
71
- export declare function hashRemoteFileDigest(alias: string, remotePath: string, algorithm: 'md5' | 'sha256'): Promise<string>;
71
+ export declare function hashRemoteFileDigest(alias: string, remotePath: string, algorithm: 'md5' | 'sha256', timeout?: number, limits?: {
72
+ maxBytes: number;
73
+ expectedSize: number;
74
+ }): Promise<string>;
72
75
  export declare function atomicReplaceCapability(alias: string, timeout?: number): Promise<{
73
76
  atomicReplaceSupported: boolean;
74
77
  reason?: string;
@@ -231,4 +234,8 @@ type SyncResult = {
231
234
  commandSummary?: Record<string, unknown>;
232
235
  output?: string;
233
236
  };
237
+ export declare function hashLocalFile(filePath: string, algorithm?: 'md5' | 'sha256', timeout?: number, limits?: {
238
+ maxBytes: number;
239
+ expectedSize: number;
240
+ }): Promise<string>;
234
241
  export {};