fix(cli): prevent OOM crash by limiting file search traversal and adding timeout (#16696)

This commit is contained in:
Gal Zahavi
2026-01-15 12:04:22 -08:00
committed by GitHub
parent 48fdb9872f
commit e77d7b2e1e
7 changed files with 126 additions and 3 deletions
+20 -1
View File
@@ -16,6 +16,8 @@ export interface CrawlOptions {
cwd: string;
// The fdir maxDepth option.
maxDepth?: number;
// Maximum number of files to return.
maxFiles?: number;
// A pre-configured Ignore instance.
ignore: Ignore;
// Caching options.
@@ -43,6 +45,9 @@ export async function crawl(options: CrawlOptions): Promise<string[]> {
const posixCwd = toPosixPath(options.cwd);
const posixCrawlDirectory = toPosixPath(options.crawlDirectory);
const maxFiles = options.maxFiles ?? Infinity;
let fileCount = 0;
let truncated = false;
let results: string[];
try {
@@ -51,7 +56,21 @@ export async function crawl(options: CrawlOptions): Promise<string[]> {
.withRelativePaths()
.withDirs()
.withPathSeparator('/') // Always use unix style paths
.filter((path, isDirectory) => {
if (!isDirectory) {
fileCount++;
if (fileCount > maxFiles) {
truncated = true;
return false;
}
}
return true;
})
.exclude((_, dirPath) => {
if (fileCount > maxFiles) {
truncated = true;
return true;
}
const relativePath = path.posix.relative(posixCrawlDirectory, dirPath);
return dirFilter(`${relativePath}/`);
});
@@ -72,7 +91,7 @@ export async function crawl(options: CrawlOptions): Promise<string[]> {
path.posix.join(relativeToCrawlDir, p),
);
if (options.cache) {
if (options.cache && !truncated) {
const cacheKey = cache.getCacheKey(
options.crawlDirectory,
options.ignore.getFingerprint(),