Skip to content

Commit a7c7eec

Browse files
committed
fix(filesystem): let a search_files read failure reach the caller
The recursive walk sat inside the try whose catch discarded the error, so a directory that could not be read (EACCES on a locked subdirectory, EPERM, ELOOP) was turned into a skipped entry. The tool then reported success with a result set that was quietly short, and the caller had no way to tell the matches from a complete search were all of them. Only the path validation needs the guard: it is what rejects a symlink resolving outside the allowed roots, and that stays a silent skip. Everything after it is the search, so a failure there propagates, which is what buildTree and listDirectory in the same server already do.
1 parent f46d957 commit a7c7eec

2 files changed

Lines changed: 48 additions & 14 deletions

File tree

‎src/filesystem/__tests__/lib.test.ts‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,33 @@ describe('Lib Functions', () => {
441441
expect(result).toEqual([expectedResult]);
442442
});
443443

444+
it('propagates a read failure on a subdirectory instead of returning a short result', async () => {
445+
// A directory that cannot be read is a real failure: reporting the
446+
// matches found so far as the complete result is how a search silently
447+
// misses files. The validation skip above is the only case that stays
448+
// quiet.
449+
const eacces: NodeJS.ErrnoException = new Error(
450+
"EACCES: permission denied, scandir '/allowed/dir/locked'"
451+
);
452+
eacces.code = 'EACCES';
453+
454+
mockFs.realpath.mockImplementation(async (path: any) => path.toString());
455+
mockFs.readdir.mockImplementation(async (dir: any) => {
456+
if (dir.toString().endsWith('locked')) throw eacces;
457+
return [
458+
{ name: 'locked', isDirectory: () => true },
459+
{ name: 'visible.txt', isDirectory: () => false },
460+
] as any;
461+
});
462+
463+
const testDir = process.platform === 'win32' ? 'C:\\allowed\\dir' : '/allowed/dir';
464+
const allowedDirs = process.platform === 'win32' ? ['C:\\allowed'] : ['/allowed'];
465+
466+
await expect(
467+
searchFilesWithValidation(testDir, '*.txt', allowedDirs, {})
468+
).rejects.toThrow('EACCES');
469+
});
470+
444471
it('handles validation errors during search', async () => {
445472
const mockEntries = [
446473
{ name: 'test.txt', isDirectory: () => false },

‎src/filesystem/lib.ts‎

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -454,26 +454,33 @@ export async function searchFilesWithValidation(
454454
for (const entry of entries) {
455455
const fullPath = path.join(currentPath, entry.name);
456456

457+
// Only the validation is guarded: a path that resolves outside the
458+
// allowed roots is skipped, as before. Everything after it is the search
459+
// itself, and a failure there (a directory that cannot be read, for
460+
// instance) is a real error that has to reach the caller -- otherwise the
461+
// result is silently short and the caller cannot tell. `directory_tree`
462+
// and `list_directory` in this server already let their `readdir` fail
463+
// through to the tool result.
457464
try {
458465
await validatePath(fullPath);
466+
} catch {
467+
continue;
468+
}
459469

460-
const relativePath = path.relative(rootPath, fullPath);
461-
const shouldExclude = excludePatterns.some(excludePattern =>
462-
minimatch(relativePath, excludePattern, { dot: true })
463-
);
470+
const relativePath = path.relative(rootPath, fullPath);
471+
const shouldExclude = excludePatterns.some(excludePattern =>
472+
minimatch(relativePath, excludePattern, { dot: true })
473+
);
464474

465-
if (shouldExclude) continue;
475+
if (shouldExclude) continue;
466476

467-
// Use glob matching for the search pattern
468-
if (minimatch(relativePath, pattern, { dot: true })) {
469-
results.push(fullPath);
470-
}
477+
// Use glob matching for the search pattern
478+
if (minimatch(relativePath, pattern, { dot: true })) {
479+
results.push(fullPath);
480+
}
471481

472-
if (entry.isDirectory()) {
473-
await search(fullPath);
474-
}
475-
} catch {
476-
continue;
482+
if (entry.isDirectory()) {
483+
await search(fullPath);
477484
}
478485
}
479486
}

0 commit comments

Comments
 (0)