(dirPath: string)
| 367 | * can vet a path a project root is searched for from. |
| 368 | */ |
| 369 | export function sensitiveDirectoryRefusal(dirPath: string): string | null { |
| 370 | const resolved = path.resolve(dirPath); |
| 371 | const lexical = path.resolve(stripWin32NamespacePrefix(dirPath)); |
| 372 | const real = nativeRealPath(lexical); |
| 373 | const candidates = [lexical]; |
| 374 | if (real && real !== lexical) candidates.push(real); |
| 375 | const named = (candidate: string): string => |
| 376 | candidate === resolved ? resolved : `${resolved} (resolves to ${candidate})`; |
| 377 | |
| 378 | // Block sensitive system directories |
| 379 | const systemPaths = sensitiveSystemPaths(); |
| 380 | for (const candidate of candidates) { |
| 381 | if ( |
| 382 | SENSITIVE_PATHS.has(candidate) || |
| 383 | SENSITIVE_PATHS.has(candidate.toLowerCase()) || |
| 384 | systemPaths.has(foldPathCase(candidate)) |
| 385 | ) { |
| 386 | return `Refusing to operate on sensitive system directory: ${named(candidate)}`; |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | // Also block common sensitive home subdirectories |
| 391 | const homeTrees = sensitiveHomeTrees(); |
| 392 | for (const candidate of candidates) { |
| 393 | const folded = foldPathCase(candidate); |
| 394 | if (homeTrees.some((tree) => isSameOrUnder(folded, tree))) { |
| 395 | return `Refusing to operate on sensitive directory: ${named(candidate)}`; |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | return null; |
| 400 | } |
| 401 | |
| 402 | /** |
| 403 | * Safely parse JSON with a fallback value. |
no test coverage detected