fix: upload: on posix system, paths were not correctly escaped

Massimo Melina committed Jul 1, 2024 at 20:36 UTC b0d7c34902b0a94b2fced4bd11769b54b3a544be
1 file changed +6 -1
src/util-os.ts
+6 -1
@@ -15,10 +15,14 @@ export function getDiskSpaceSync(path: string) {
15 return parseLogicaldisk(execSync(makeLogicaldisk(path), { timeout: LOGICALDISK_TIMEOUT }).toString())[0]
16 while (path && !existsSync(path))
17 path = dirname(path)
18 - try { return parseDfResult(execSync(`df -k "${path}"`, { timeout: DF_TIMEOUT }).toString())[0] }
18 + try { return parseDfResult(execSync(`df -k ${bashEscape(path)}`, { timeout: DF_TIMEOUT }).toString())[0] }
19 catch(e: any) { throw parseDfResult(e) }
20 }
21
22 +function bashEscape(par: string) {
23 + return `"${par.replaceAll('"', '\\"')}"`
24 +}
25 +
26 export async function getDiskSpace(path: string) {
27 if (IS_WINDOWS)
28 return parseLogicaldisk(await runCmd(makeLogicaldisk(path), [], { timeout: LOGICALDISK_TIMEOUT }))[0]
@@ -83,6 +87,7 @@ const wmicFields = ['Size','FreeSpace','Name','Description'] as const
87
88 function makeLogicaldisk(path='') {
89 const drive = resolve(path).slice(0, 2).toUpperCase()
90 + if (!drive.match(/^(|\w:)$/)) throw 'invalid-path'
91 return `wmic logicaldisk ${prefix(`where "DeviceID = '`, drive, `'"`)} get ${wmicFields.join()} /format:list`
92 }
93