* Remove shell escape backslashes from a path (for macOS/Linux/WSL) * On Windows systems, this function returns the path unchanged * @param path Path that might contain shell-escaped characters * @returns Path with escape backslashes removed (on macOS/Linux/WSL only)
(path: string)
| 291 | * @returns Path with escape backslashes removed (on macOS/Linux/WSL only) |
| 292 | */ |
| 293 | function stripBackslashEscapes(path: string): string { |
| 294 | const platform = process.platform as SupportedPlatform |
| 295 | |
| 296 | // On Windows, don't remove backslashes as they're part of the path |
| 297 | if (platform === 'win32') { |
| 298 | return path |
| 299 | } |
| 300 | |
| 301 | // On macOS/Linux/WSL, handle shell-escaped paths |
| 302 | // Double-backslashes (\\) represent actual backslashes in the filename |
| 303 | // Single backslashes followed by special chars are shell escapes |
| 304 | |
| 305 | // First, temporarily replace double backslashes with a placeholder |
| 306 | // Use random salt to prevent injection attacks where path contains literal placeholder |
| 307 | const salt = randomBytes(8).toString('hex') |
| 308 | const placeholder = `__DOUBLE_BACKSLASH_${salt}__` |
| 309 | const withPlaceholder = path.replace(/\\\\/g, placeholder) |
| 310 | |
| 311 | // Remove single backslashes that are shell escapes |
| 312 | // This handles cases like "name\ \(15\).png" -> "name (15).png" |
| 313 | const withoutEscapes = withPlaceholder.replace(/\\(.)/g, '$1') |
| 314 | |
| 315 | // Replace placeholders back to single backslashes |
| 316 | return withoutEscapes.replace(new RegExp(placeholder, 'g'), '\\') |
| 317 | } |
| 318 | |
| 319 | /** |
| 320 | * Check if a given text represents an image file path |
no test coverage detected