Fix Windows local file download paths (#810)

This commit is contained in:
ekko
2026-05-17 11:09:28 +08:00
committed by GitHub
parent bcfc5053c4
commit fa035f348e
4 changed files with 76 additions and 47 deletions
@@ -65,9 +65,17 @@ export interface TerminalConfig {
/**
* Validate a file path: must be absolute and not contain '..' traversal.
*/
export function normalizePlatformPath(filePath: string, platform = process.platform): string {
if (platform !== 'win32') return filePath
const msysDrivePath = filePath.match(/^\/([a-zA-Z])(?:\/(.*))?$/)
if (!msysDrivePath) return filePath
const [, drive, rest = ''] = msysDrivePath
return `${drive.toUpperCase()}:\\${rest.replace(/\//g, '\\')}`
}
export function validatePath(filePath: string): string {
if (!filePath) throw Object.assign(new Error('Missing file path'), { code: 'missing_path' })
const resolved = resolve(filePath)
const resolved = resolve(normalizePlatformPath(filePath))
const normalized = normalize(resolved)
if (normalized.includes('..')) {
throw Object.assign(new Error('Invalid file path'), { code: 'invalid_path' })