Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions src/filesystem/__tests__/lib.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,40 @@ describe('Lib Functions', () => {
expect(result).toEqual([expectedResult]);
});

it('skips excluded entries before validating their real paths', async () => {
const mockEntries = [
{ name: 'GoogleDrive-lazy', isDirectory: () => true },
{ name: 'match.txt', isDirectory: () => false }
];

mockFs.readdir.mockResolvedValueOnce(mockEntries as any);

const testDir = process.platform === 'win32' ? 'C:\\allowed\\dir' : '/allowed/dir';
const allowedDirs = process.platform === 'win32' ? ['C:\\allowed'] : ['/allowed'];

mockFs.realpath.mockImplementation(async (inputPath: any) => {
const pathStr = inputPath.toString();
if (pathStr.includes('GoogleDrive-lazy')) {
throw new Error('excluded entries should not be realpathed');
}
return pathStr;
});

const result = await searchFilesWithValidation(
testDir,
'*',
allowedDirs,
{ excludePatterns: ['GoogleDrive-*'] }
);

const expectedResult = process.platform === 'win32' ? 'C:\\allowed\\dir\\match.txt' : '/allowed/dir/match.txt';
expect(result).toEqual([expectedResult]);
const validatedExcludedEntry = mockFs.realpath.mock.calls.some(([inputPath]: any[]) =>
inputPath.toString().includes('GoogleDrive-lazy')
);
expect(validatedExcludedEntry).toBe(false);
});

it('handles validation errors during search', async () => {
const mockEntries = [
{ name: 'test.txt', isDirectory: () => false },
Expand Down
13 changes: 6 additions & 7 deletions src/filesystem/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -362,17 +362,16 @@ export async function searchFilesWithValidation(

for (const entry of entries) {
const fullPath = path.join(currentPath, entry.name);
const relativePath = path.relative(rootPath, fullPath);
const shouldExclude = excludePatterns.some(excludePattern =>
minimatch(relativePath, excludePattern, { dot: true })
);

if (shouldExclude) continue;

try {
await validatePath(fullPath);

const relativePath = path.relative(rootPath, fullPath);
const shouldExclude = excludePatterns.some(excludePattern =>
minimatch(relativePath, excludePattern, { dot: true })
);

if (shouldExclude) continue;

// Use glob matching for the search pattern
if (minimatch(relativePath, pattern, { dot: true })) {
results.push(fullPath);
Expand Down
Loading