fix(gitIgnore): prevent crash/error when processing malformed file paths in GitIgnoreParser (#7553)

Co-authored-by: Srinath Padmanabhan <17151014+srithreepo@users.noreply.github.com>
This commit is contained in:
fuyou
2025-09-03 14:19:20 +08:00
committed by GitHub
parent b5dd6f9ea6
commit 5c2bb990d8
2 changed files with 38 additions and 7 deletions
+28 -7
View File
@@ -57,16 +57,37 @@ export class GitIgnoreParser implements GitIgnoreFilter {
}
isIgnored(filePath: string): boolean {
const resolved = path.resolve(this.projectRoot, filePath);
const relativePath = path.relative(this.projectRoot, resolved);
if (relativePath === '' || relativePath.startsWith('..')) {
if (!filePath || typeof filePath !== 'string') {
return false;
}
// Even in windows, Ignore expects forward slashes.
const normalizedPath = relativePath.replace(/\\/g, '/');
return this.ig.ignores(normalizedPath);
if (
filePath.startsWith('\\') ||
filePath === '/' ||
filePath.includes('\0')
) {
return false;
}
try {
const resolved = path.resolve(this.projectRoot, filePath);
const relativePath = path.relative(this.projectRoot, resolved);
if (relativePath === '' || relativePath.startsWith('..')) {
return false;
}
// Even in windows, Ignore expects forward slashes.
const normalizedPath = relativePath.replace(/\\/g, '/');
if (normalizedPath.startsWith('/') || normalizedPath === '') {
return false;
}
return this.ig.ignores(normalizedPath);
} catch (_error) {
return false;
}
}
getPatterns(): string[] {