(folderPath: string | undefined | null)
| 64 | * and does not point into the `.git` directory. |
| 65 | */ |
| 66 | export function isSafeRepoFolderPath(folderPath: string | undefined | null): boolean { |
| 67 | if (!folderPath) { |
| 68 | return true; |
| 69 | } |
| 70 | |
| 71 | if (folderPath.startsWith('/') || folderPath.startsWith('\\')) { |
| 72 | return false; |
| 73 | } |
| 74 | |
| 75 | // A `<letter>:` prefix is a drive reference only on Windows; elsewhere `v: specs` is a legal folder name. |
| 76 | if (isWindows && /^[a-zA-Z]:/.test(folderPath)) { |
| 77 | return false; |
| 78 | } |
| 79 | |
| 80 | return folderPath.split(/[\\/]/).every(segment => { |
| 81 | // On Windows, `:` addresses an NTFS alternate data stream (e.g. `.git::$INDEX_ALLOCATION` resolves to `.git`), |
| 82 | // but it is a legal character on macOS/Linux, so only compare the part before the stream suffix. |
| 83 | const name = segment.split(':')[0]; |
| 84 | // Windows ignores trailing dots and spaces in path segments, so `.git.` or `.git ` resolve to `.git`, |
| 85 | // and a segment made only of dots and spaces (e.g. `.. ` or `.. .`) can resolve to `..`. |
| 86 | const windowsName = name.replace(/[. ]+$/, ''); |
| 87 | if (name === '..' || (windowsName === '' && name !== '')) { |
| 88 | return false; |
| 89 | } |
| 90 | |
| 91 | return windowsName.toLowerCase() !== GIT_DIR_NAME; |
| 92 | }); |
| 93 | } |
no test coverage detected