@pyrokine/mcp-ssh 2.4.3 → 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.
@@ -1,6 +1,8 @@
1
1
  import { createHash } from 'crypto';
2
2
  import * as fs from 'fs';
3
3
  import * as path from 'path';
4
+ import { remoteJoin } from './remote-platform.js';
5
+ import { formatFileMode } from './file-mode.js';
4
6
  import { escapeShellArg } from './tools/utils.js';
5
7
  export const DIRECTORY_VERIFY_MAX_ENTRIES = 50_000;
6
8
  export const DIRECTORY_VERIFY_MAX_MISMATCHES = 20;
@@ -21,48 +23,53 @@ function rootHash(entries) {
21
23
  }
22
24
  return hash.digest('hex');
23
25
  }
24
- async function hashFile(filePath) {
26
+ async function hashFile(filePath, size, maxBytes) {
25
27
  const hash = createHash('sha256');
26
- await new Promise((resolve, reject) => {
27
- const stream = fs.createReadStream(filePath);
28
- stream.on('data', (chunk) => hash.update(chunk));
29
- stream.on('error', reject);
30
- stream.on('end', resolve);
31
- });
32
- return hash.digest('hex');
33
- }
34
- function globPatternToRegExp(pattern) {
35
- let expression = '';
36
- for (let index = 0; index < pattern.length; ++index) {
37
- const character = pattern[index];
38
- if (character === '*' && pattern[index + 1] === '*') {
39
- expression += '.*';
40
- ++index;
41
- }
42
- else if (character === '*') {
43
- expression += '[^/]*';
44
- }
45
- else if (character === '?') {
46
- expression += '[^/]';
47
- }
48
- else {
49
- expression += character.replace(/[.+^${}()|[\]\\]/g, '\\$&');
28
+ let readBytes = 0;
29
+ for await (const chunk of fs.createReadStream(filePath)) {
30
+ readBytes += Buffer.byteLength(chunk);
31
+ if (readBytes > maxBytes) {
32
+ throw new Error('local file grew beyond directory manifest hash budget');
50
33
  }
34
+ hash.update(chunk);
35
+ }
36
+ if (readBytes !== size) {
37
+ throw new Error('local file changed during manifest hashing');
51
38
  }
52
- return new RegExp(`^${expression}$`);
39
+ return hash.digest('hex');
53
40
  }
54
- const excludePatternCache = new Map();
55
41
  export function matchesDirectoryExclude(relativePath, patterns) {
56
42
  const normalizedPath = relativePath.replace(/^\.\//, '').replace(/^\//, '');
57
43
  const basename = path.posix.basename(normalizedPath);
58
44
  return Boolean(patterns?.some((pattern) => {
59
45
  const normalizedPattern = pattern.replace(/^\//, '');
60
- let regex = excludePatternCache.get(normalizedPattern);
61
- if (!regex) {
62
- regex = globPatternToRegExp(normalizedPattern);
63
- excludePatternCache.set(normalizedPattern, regex);
46
+ const candidate = normalizedPattern.includes('/') ? normalizedPath : basename;
47
+ const matches = new Uint8Array(candidate.length + 1);
48
+ matches[0] = 1;
49
+ for (let index = 0; index < normalizedPattern.length; ++index) {
50
+ const character = normalizedPattern[index];
51
+ const globstar = character === '*' && normalizedPattern[index + 1] === '*';
52
+ if (globstar) {
53
+ ++index;
54
+ }
55
+ let previous = matches[0];
56
+ if (character !== '*') {
57
+ matches[0] = 0;
58
+ }
59
+ for (let offset = 1; offset <= candidate.length; ++offset) {
60
+ const before = matches[offset];
61
+ const value = candidate[offset - 1];
62
+ if (character === '*') {
63
+ const canConsume = globstar ? /./.test(value) : value !== '/';
64
+ matches[offset] = before || (matches[offset - 1] && canConsume) ? 1 : 0;
65
+ }
66
+ else {
67
+ matches[offset] = previous && (character === '?' ? value !== '/' : character === value) ? 1 : 0;
68
+ }
69
+ previous = before;
70
+ }
64
71
  }
65
- return regex.test(normalizedPattern.includes('/') ? normalizedPath : basename);
72
+ return matches[candidate.length] === 1;
66
73
  }));
67
74
  }
68
75
  export async function createLocalDirectoryManifest(rootPath, request, exclude, followSymlinks = false) {
@@ -78,7 +85,7 @@ export async function createLocalDirectoryManifest(rootPath, request, exclude, f
78
85
  path: '.',
79
86
  type: 'directory',
80
87
  size: 0,
81
- mode: (rootStats.mode & 0o777).toString(8).padStart(4, '0'),
88
+ mode: formatFileMode(rootStats.mode),
82
89
  owner: String(rootStats.uid),
83
90
  },
84
91
  ];
@@ -117,7 +124,7 @@ export async function createLocalDirectoryManifest(rootPath, request, exclude, f
117
124
  path: relativePath,
118
125
  type: 'directory',
119
126
  size: 0,
120
- mode: (stats.mode & 0o777).toString(8).padStart(4, '0'),
127
+ mode: formatFileMode(stats.mode),
121
128
  owner: String(stats.uid),
122
129
  });
123
130
  await visit(fullPath, relativePath);
@@ -136,14 +143,14 @@ export async function createLocalDirectoryManifest(rootPath, request, exclude, f
136
143
  limitReason = 'total_bytes';
137
144
  return;
138
145
  }
146
+ sha256 = await hashFile(fullPath, stats.size, Math.min(maxFileBytes, maxTotalBytes - hashedBytes));
139
147
  hashedBytes += stats.size;
140
- sha256 = await hashFile(fullPath);
141
148
  }
142
149
  entries.push({
143
150
  path: relativePath,
144
151
  type: 'file',
145
152
  size: stats.size,
146
- mode: (stats.mode & 0o777).toString(8).padStart(4, '0'),
153
+ mode: formatFileMode(stats.mode),
147
154
  owner: String(stats.uid),
148
155
  sha256,
149
156
  });
@@ -172,203 +179,278 @@ export async function createLocalDirectoryManifest(rootPath, request, exclude, f
172
179
  unsupportedSamples,
173
180
  };
174
181
  }
175
- export function createEmptyDirectoryManifest() {
176
- const entries = [];
182
+ export async function createRemoteDirectoryManifest(sftp, rootPath, request, exclude, followSymlinks = false, style = 'posix') {
183
+ const maxEntries = Math.min(request.maxEntries ?? 10_000, DIRECTORY_VERIFY_MAX_ENTRIES);
184
+ const maxFileBytes = Math.min(request.maxFileBytes ?? DIRECTORY_VERIFY_DEFAULT_MAX_FILE_BYTES, DIRECTORY_VERIFY_MAX_FILE_BYTES);
185
+ const maxTotalBytes = Math.min(request.maxTotalBytes ?? DIRECTORY_VERIFY_DEFAULT_MAX_TOTAL_BYTES, DIRECTORY_VERIFY_MAX_TOTAL_BYTES);
186
+ const join = (parent, child) => remoteJoin(style, parent, child);
187
+ const stat = (remotePath) => new Promise((resolve, reject) => {
188
+ sftp.stat(remotePath, (error, stats) => (error ? reject(error) : resolve(stats)));
189
+ });
190
+ const lstat = (remotePath) => new Promise((resolve, reject) => {
191
+ sftp.lstat(remotePath, (error, stats) => (error ? reject(error) : resolve(stats)));
192
+ });
193
+ const root = await stat(rootPath);
194
+ if ((root.mode & 0o170000) !== 0o040000) {
195
+ throw new Error('remote directory verification root is not a directory');
196
+ }
197
+ const entry = (name, kind, stats, sha256) => ({
198
+ path: name,
199
+ type: kind,
200
+ size: kind === 'file' ? stats.size : 0,
201
+ mode: formatFileMode(stats.mode),
202
+ owner: String(stats.uid),
203
+ sha256,
204
+ });
205
+ const entries = [entry('.', 'directory', root)];
206
+ let hashedBytes = 0;
207
+ let skippedSymlinks = 0;
208
+ let skippedUnsupported = 0;
209
+ const unsupportedSamples = [];
210
+ let limitReason;
211
+ const active = new Set();
212
+ const visit = async (directory, relative, depth) => {
213
+ if (depth > 64) {
214
+ limitReason = 'depth';
215
+ return;
216
+ }
217
+ const resolved = await new Promise((resolve, reject) => {
218
+ sftp.realpath(directory, (error, result) => (error ? reject(error) : resolve(result)));
219
+ });
220
+ if (active.has(resolved)) {
221
+ ++skippedSymlinks;
222
+ return;
223
+ }
224
+ active.add(resolved);
225
+ const handle = await new Promise((resolve, reject) => {
226
+ sftp.opendir(directory, (error, result) => (error ? reject(error) : resolve(result)));
227
+ });
228
+ try {
229
+ while (!limitReason) {
230
+ const batch = await new Promise((resolve, reject) => {
231
+ sftp.readdir(handle, (error, list) => {
232
+ if (error && error.code === 1) {
233
+ resolve(null);
234
+ }
235
+ else if (error) {
236
+ reject(error);
237
+ }
238
+ else {
239
+ resolve(list);
240
+ }
241
+ });
242
+ });
243
+ if (!batch) {
244
+ break;
245
+ }
246
+ for (const item of batch) {
247
+ const name = item.filename;
248
+ if (!name || name.length > 1024 || name === '.' || name === '..' || /[\\/\0]/.test(name)) {
249
+ throw new Error('remote directory verification returned unsafe entry name');
250
+ }
251
+ const relativePath = relative ? `${relative}/${name}` : name;
252
+ if (matchesDirectoryExclude(relativePath, exclude)) {
253
+ continue;
254
+ }
255
+ if (entries.length >= maxEntries) {
256
+ limitReason = 'entries';
257
+ break;
258
+ }
259
+ const fullPath = join(directory, name);
260
+ const link = await lstat(fullPath);
261
+ if ((link.mode & 0o170000) === 0o120000 && !followSymlinks) {
262
+ ++skippedSymlinks;
263
+ continue;
264
+ }
265
+ const stats = (link.mode & 0o170000) === 0o120000 ? await stat(fullPath) : link;
266
+ if ((stats.mode & 0o170000) === 0o040000) {
267
+ entries.push(entry(relativePath, 'directory', stats));
268
+ await visit(fullPath, relativePath, depth + 1);
269
+ }
270
+ else if ((stats.mode & 0o170000) === 0o100000) {
271
+ if (!Number.isSafeInteger(stats.size) || stats.size < 0) {
272
+ throw new Error('remote manifest returned an invalid file size');
273
+ }
274
+ let sha256;
275
+ if (request.sha256) {
276
+ if (stats.size > maxFileBytes) {
277
+ limitReason = 'file_bytes';
278
+ break;
279
+ }
280
+ if (hashedBytes + stats.size > maxTotalBytes) {
281
+ limitReason = 'total_bytes';
282
+ break;
283
+ }
284
+ const digest = createHash('sha256');
285
+ let actualSize = 0;
286
+ for await (const chunk of sftp.createReadStream(fullPath)) {
287
+ actualSize += Buffer.byteLength(chunk);
288
+ if (actualSize > maxFileBytes) {
289
+ limitReason = 'file_bytes';
290
+ break;
291
+ }
292
+ if (hashedBytes + actualSize > maxTotalBytes) {
293
+ limitReason = 'total_bytes';
294
+ break;
295
+ }
296
+ digest.update(chunk);
297
+ }
298
+ if (limitReason) {
299
+ break;
300
+ }
301
+ if (actualSize !== stats.size) {
302
+ throw new Error('remote file changed during manifest hashing');
303
+ }
304
+ hashedBytes += actualSize;
305
+ sha256 = digest.digest('hex');
306
+ }
307
+ entries.push(entry(relativePath, 'file', stats, sha256));
308
+ }
309
+ else {
310
+ ++skippedUnsupported;
311
+ if (unsupportedSamples.length < 10) {
312
+ unsupportedSamples.push(relativePath);
313
+ }
314
+ }
315
+ if (limitReason) {
316
+ break;
317
+ }
318
+ }
319
+ }
320
+ }
321
+ finally {
322
+ active.delete(resolved);
323
+ await new Promise((resolve, reject) => sftp.close(handle, (error) => (error ? reject(error) : resolve())));
324
+ }
325
+ };
326
+ await visit(rootPath, '', 0);
177
327
  return {
178
328
  entries,
179
- count: 0,
329
+ count: entries.length,
180
330
  rootSha256: rootHash(entries),
181
- limited: false,
182
- skippedSymlinks: 0,
183
- skippedUnsupported: 0,
184
- unsupportedSamples: [],
331
+ limited: limitReason !== undefined,
332
+ limitReason,
333
+ skippedSymlinks,
334
+ skippedUnsupported,
335
+ unsupportedSamples,
185
336
  };
186
337
  }
187
- function buildExcludeRegexes(exclude) {
188
- const pathPatterns = [];
189
- const basenamePatterns = [];
190
- for (const pattern of exclude ?? []) {
191
- const normalized = pattern.replace(/^\//, '');
192
- const expression = globPatternToRegExp(normalized).source.replace(/^\^|\$$/g, '');
193
- if (normalized.includes('/')) {
194
- pathPatterns.push(expression);
195
- }
196
- else {
197
- basenamePatterns.push(expression);
198
- }
199
- }
200
- return {
201
- path: pathPatterns.length > 0 ? `^(${pathPatterns.join('|')})$` : undefined,
202
- basename: basenamePatterns.length > 0 ? `^(${basenamePatterns.join('|')})$` : undefined,
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;
203
345
  };
204
- }
205
- export function buildRemoteDirectoryManifestCommand(rootPath, request, exclude, followSymlinks = false) {
206
346
  const maxEntries = Math.min(request.maxEntries ?? 10_000, DIRECTORY_VERIFY_MAX_ENTRIES);
207
347
  const maxFileBytes = Math.min(request.maxFileBytes ?? DIRECTORY_VERIFY_DEFAULT_MAX_FILE_BYTES, DIRECTORY_VERIFY_MAX_FILE_BYTES);
208
348
  const maxTotalBytes = Math.min(request.maxTotalBytes ?? DIRECTORY_VERIFY_DEFAULT_MAX_TOTAL_BYTES, DIRECTORY_VERIFY_MAX_TOTAL_BYTES);
209
- const normalizedRoot = rootPath.replace(/\/+$/, '') || '/';
210
- const root = escapeShellArg(normalizedRoot);
211
- const includeHash = request.sha256 === true;
212
- const excludeRegexes = buildExcludeRegexes(exclude);
213
- const pathExcludeRegex = excludeRegexes.path ? escapeShellArg(excludeRegexes.path) : undefined;
214
- const basenameExcludeRegex = excludeRegexes.basename ? escapeShellArg(excludeRegexes.basename) : undefined;
215
- const findFollow = followSymlinks ? '-L' : '-H';
216
- const script = [
217
- 'set -euo pipefail',
218
- 'export LC_ALL=C',
219
- `root=${root}`,
220
- '[ -d "$root" ] || { printf "remote directory does not exist\\n" >&2; exit 3; }',
221
- 'count=0',
222
- 'hashed_bytes=0',
223
- 'skipped_symlinks=0',
224
- 'skipped_unsupported=0',
225
- 'unsupported_samples=()',
226
- 'excluded_dirs=()',
227
- 'while IFS= read -r -d "" item; do',
228
- ' if [ "$item" = "__MCP_FIND_ERROR__" ]; then exit 4; fi',
229
- ` if [ "$item" = "$root" ]; then
230
- rel=.
231
- elif [ "$root" = / ]; then
232
- rel=\${item#/}
233
- else
234
- rel=\${item#"$root"/}
235
- fi`,
236
- ' for excluded_dir in "${excluded_dirs[@]}"; do case "$rel" in "$excluded_dir"/*) continue 2;; esac; done',
237
- pathExcludeRegex
238
- ? ` if [ "$rel" != . ] && printf %s "$rel" | grep -Eq -- ${pathExcludeRegex}; then
239
- [ -d "$item" ] && excluded_dirs+=("$rel")
240
- continue
241
- fi`
242
- : ' :',
243
- basenameExcludeRegex
244
- ? ` base=\${rel##*/}
245
- if [ "$rel" != . ] && printf %s "$base" | grep -Eq -- ${basenameExcludeRegex}; then
246
- [ -d "$item" ] && excluded_dirs+=("$rel")
247
- continue
248
- fi`
249
- : ' :',
250
- followSymlinks
251
- ? ' :'
252
- : ' if [ "$rel" != . ] && [ -L "$item" ]; then skipped_symlinks=$((skipped_symlinks + 1)); continue; fi',
253
- ` if [ -d "$item" ]; then
254
- type=directory
255
- size=0
256
- elif [ -f "$item" ]; then
257
- type=file
258
- size=$(stat -c %s -- "$item")
259
- else
260
- skipped_unsupported=$((skipped_unsupported + 1))
261
- if [ "\${#unsupported_samples[@]}" -lt 10 ]; then
262
- unsupported_samples+=("$(printf %s "$rel" | base64 | tr -d "\\n")")
263
- fi
264
- continue
265
- fi`,
266
- ' count=$((count + 1))',
267
- ` if [ "$count" -gt ${maxEntries} ]; then printf '__MCP_LIMIT__\\tentries\\n'; break; fi`,
268
- ' raw_mode=$(stat -c %a -- "$item")',
269
- ' mode=$(printf \'%04o\' "$((8#$raw_mode & 0777))")',
270
- ' owner=$(stat -c %u -- "$item")',
271
- includeHash
272
- ? ` if [ "$type" = file ]; then
273
- if [ "$size" -gt ${maxFileBytes} ]; then printf '__MCP_LIMIT__\\tfile_bytes\\n'; break; fi
274
- next_hashed_bytes=$((hashed_bytes + size))
275
- if [ "$next_hashed_bytes" -gt ${maxTotalBytes} ]; then printf '__MCP_LIMIT__\\ttotal_bytes\\n'; break; fi
276
- hashed_bytes=$next_hashed_bytes
277
- digest=$(sha256sum -- "$item" | cut -d " " -f 1)
278
- else digest=; fi`
279
- : ' digest=',
280
- ' encoded=$(printf %s "$rel" | base64 | tr -d "\\n")',
281
- ' printf "%s\\t%s\\t%s\\t%s\\t%s\\t%s\\n" "$encoded" "$type" "$size" "$mode" "$owner" "$digest"',
282
- `done < <({ find ${findFollow} "$root" -print0 || printf '%s\\0' '__MCP_FIND_ERROR__'; })`,
283
- 'printf "__MCP_SKIPPED_SYMLINKS__\\t%s\\n" "$skipped_symlinks"',
284
- 'printf "__MCP_SKIPPED_UNSUPPORTED__\\t%s\\n" "$skipped_unsupported"',
285
- 'for sample in "${unsupported_samples[@]}"; do printf "__MCP_UNSUPPORTED_SAMPLE__\\t%s\\n" "$sample"; done',
286
- ].join('\n');
287
- return `bash -c ${escapeShellArg(script)}`;
288
- }
289
- function decodeManifestPath(encodedPath) {
290
- const decodedPath = Buffer.from(encodedPath, 'base64').toString('utf-8');
291
- if (!encodedPath ||
292
- !/^[A-Za-z0-9+/]+={0,2}$/.test(encodedPath) ||
293
- encodedPath.length % 4 !== 0 ||
294
- !decodedPath ||
295
- decodedPath.length > 4096 ||
296
- decodedPath.includes('\0') ||
297
- path.posix.isAbsolute(decodedPath) ||
298
- decodedPath.split('/').includes('..')) {
299
- throw new Error('remote directory verification returned malformed manifest path');
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');
300
355
  }
301
- return decodedPath;
302
- }
303
- export function parseRemoteDirectoryManifest(output) {
304
356
  const entries = [];
305
- const paths = new Set();
357
+ const unsupportedSamples = [];
306
358
  let skippedSymlinks = 0;
307
359
  let skippedUnsupported = 0;
308
- const unsupportedSamples = [];
309
360
  let limitReason;
310
- for (const line of output.split('\n')) {
311
- if (!line) {
312
- continue;
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');
313
373
  }
314
- if (line.startsWith('__MCP_LIMIT__\t')) {
315
- const reason = line.slice('__MCP_LIMIT__\t'.length);
316
- if (reason === 'entries' || reason === 'file_bytes' || reason === 'total_bytes') {
317
- limitReason = reason;
318
- continue;
319
- }
320
- throw new Error('remote directory verification returned an unknown limit reason');
321
- }
322
- if (line.startsWith('__MCP_SKIPPED_SYMLINKS__\t')) {
323
- const count = Number(line.slice('__MCP_SKIPPED_SYMLINKS__\t'.length));
324
- if (!Number.isSafeInteger(count) || count < 0) {
325
- throw new Error('remote directory verification returned an invalid symlink count');
326
- }
327
- skippedSymlinks = count;
328
- continue;
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;
329
380
  }
330
- if (line.startsWith('__MCP_SKIPPED_UNSUPPORTED__\t')) {
331
- const count = Number(line.slice('__MCP_SKIPPED_UNSUPPORTED__\t'.length));
332
- if (!Number.isSafeInteger(count) || count < 0) {
333
- throw new Error('remote directory verification returned an invalid unsupported entry count');
334
- }
335
- skippedUnsupported = count;
336
- continue;
381
+ if (entries.length >= maxEntries) {
382
+ limitReason = 'entries';
383
+ throw limit;
337
384
  }
338
- if (line.startsWith('__MCP_UNSUPPORTED_SAMPLE__\t')) {
339
- const encodedPath = line.slice('__MCP_UNSUPPORTED_SAMPLE__\t'.length);
340
- const samplePath = decodeManifestPath(encodedPath);
341
- if (unsupportedSamples.length >= 10) {
342
- throw new Error('remote directory verification returned too many unsupported entry samples');
343
- }
344
- unsupportedSamples.push(samplePath);
345
- continue;
385
+ if (kind === 'l') {
386
+ ++skippedSymlinks;
387
+ return;
346
388
  }
347
- const fields = line.split('\t');
348
- if (fields.length !== 6) {
349
- throw new Error('remote directory verification returned malformed manifest data');
389
+ if (kind !== 'f' && kind !== 'd') {
390
+ ++skippedUnsupported;
391
+ if (unsupportedSamples.length < 10)
392
+ unsupportedSamples.push(relative);
393
+ return;
350
394
  }
351
- const [encodedPath, type, sizeText, mode, owner, sha256] = fields;
352
395
  const size = Number(sizeText);
353
- const entryPath = decodeManifestPath(encodedPath);
354
- if (paths.has(entryPath) ||
355
- (type !== 'file' && type !== 'directory') ||
356
- !Number.isSafeInteger(size) ||
396
+ const mode = Number.parseInt(modeText, 8);
397
+ if (!Number.isSafeInteger(size) ||
357
398
  size < 0 ||
358
- !/^0[0-7]{3}$/.test(mode) ||
399
+ !/^[0-7]{1,4}$/.test(modeText) ||
359
400
  !/^\d+$/.test(owner) ||
360
- (sha256 !== undefined && sha256 !== '' && !/^[a-f0-9]{64}$/.test(sha256))) {
361
- throw new Error('remote directory verification returned malformed manifest data');
401
+ !fullPath) {
402
+ throw new Error('OpenSSH directory verification returned invalid file metadata');
362
403
  }
363
- paths.add(entryPath);
364
404
  entries.push({
365
- path: entryPath,
366
- type,
367
- size,
368
- mode,
405
+ path: relative || '.',
406
+ type: kind === 'd' ? 'directory' : 'file',
407
+ size: kind === 'f' ? size : 0,
408
+ mode: formatFileMode(mode),
369
409
  owner,
370
- sha256: sha256 || undefined,
371
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
+ }
372
454
  }
373
455
  return {
374
456
  entries,
@@ -381,6 +463,18 @@ export function parseRemoteDirectoryManifest(output) {
381
463
  unsupportedSamples,
382
464
  };
383
465
  }
466
+ export function createEmptyDirectoryManifest() {
467
+ const entries = [];
468
+ return {
469
+ entries,
470
+ count: 0,
471
+ rootSha256: rootHash(entries),
472
+ limited: false,
473
+ skippedSymlinks: 0,
474
+ skippedUnsupported: 0,
475
+ unsupportedSamples: [],
476
+ };
477
+ }
384
478
  export function compareDirectoryManifests(expected, actual, request, deletionBaseline) {
385
479
  if (expected.limited || actual.limited || (request.deletions && deletionBaseline?.limited)) {
386
480
  return {
@@ -400,7 +494,7 @@ export function compareDirectoryManifests(expected, actual, request, deletionBas
400
494
  return {
401
495
  kind: 'directory',
402
496
  skipped: true,
403
- 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',
404
498
  expectedSkippedSymlinks: expected.skippedSymlinks,
405
499
  actualSkippedSymlinks: actual.skippedSymlinks,
406
500
  deletionBaselineSkippedSymlinks: deletionBaseline?.skippedSymlinks,
@@ -0,0 +1,6 @@
1
+ export declare const FILE_CREATE_MODE_MASK = 511;
2
+ export declare const FILE_MODE_MASK = 4095;
3
+ export declare function extractFileMode(mode: number): number;
4
+ export declare function formatFileMode(mode: number): string;
5
+ export declare function formatFilePermissions(mode: number): string;
6
+ export declare function parseExpectedFileMode(value: string): number | undefined;