From 6ec0296435aeff61c62c5d08d8bb793520b91622 Mon Sep 17 00:00:00 2001 From: James Garbutt <43081j@users.noreply.github.com> Date: Tue, 1 Dec 2026 10:19:37 +0000 Subject: [PATCH] fix: support paths with spaces on windows Adds support for spaces in Windows paths, e.g. `Program Files/foo/bar.cmd`. --- src/normalize.ts | 6 ++++-- src/test/main_test.ts | 26 ++++++++++++++++++++++---- src/test/normalize_test.ts | 22 +++++++++++++++++----- 3 files changed, 43 insertions(+), 11 deletions(-) diff --git a/src/normalize.ts b/src/normalize.ts index 4533393..65c30a0 100644 --- a/src/normalize.ts +++ b/src/normalize.ts @@ -97,8 +97,10 @@ export function normalizeSpawnCommand( // This is necessary otherwise it will always fail with ENOENT in those cases command = normalizePath(command); - // Escape command & arguments - command = command.replace(metaCharsRegExp, '^$1'); + // Quote the command for windows so it doesn't bother expanding meta chars + command = `"${command.replace(/%/g, '%%cd:~,%')}"`; + + // Escape arguments args = args.map((arg) => { // Algorithm below is based on https://qntm.org/cmd // It's slightly altered to disable JS backtracking to avoid hanging on specially crafted input diff --git a/src/test/main_test.ts b/src/test/main_test.ts index 38cd7b8..895d35e 100644 --- a/src/test/main_test.ts +++ b/src/test/main_test.ts @@ -403,7 +403,7 @@ if (isWindows) { test('does not throw spawn errors', async () => { const result = await x('definitelyNonExistent'); expect(result.stderr).toBe( - "'definitelyNonExistent' is not recognized as an internal" + + '\'"definitelyNonExistent"\' is not recognized as an internal' + ' or external command,\r\noperable program or batch file.\r\n' ); expect(result.stdout).toBe(''); @@ -419,6 +419,24 @@ if (isWindows) { expect(result.stdout).toContain('&echo INJECTED'); expect(result.stdout.split(/\r?\n/)).not.toContain('INJECTED'); }); + + test.for([ + ['spaces', 'tinyexec program files (x86)-'], + ['variables', 'tinyexec-%CD%-'] + ])('runs batch files in directories with %s', async ([, prefix]) => { + const dir = fs.mkdtempSync(path.join(os.tmpdir(), prefix)); + try { + const scriptPath = path.join(dir, 'tool.cmd'); + fs.writeFileSync(scriptPath, '@echo ran %*\r\n'); + + const result = await x(scriptPath, ['hello']); + + expect(result.exitCode).toBe(0); + expect(result.stdout).toBe('ran "hello"\r\n'); + } finally { + fs.rmSync(dir, {recursive: true, force: true}); + } + }); }); describe('exec (windows) (async)', () => { @@ -440,7 +458,7 @@ if (isWindows) { } catch (err) { expect.assert(err instanceof NonZeroExitError); expect(err.output?.stderr).toBe( - "'definitelyNonExistent' is not recognized as an internal" + + '\'"definitelyNonExistent"\' is not recognized as an internal' + ' or external command,\r\noperable program or batch file.\r\n' ); expect(err.output?.stdout).toBe(''); @@ -490,7 +508,7 @@ if (isWindows) { } expect(lines).toEqual([ - "'nonexistentforsure' is not recognized as an internal or " + + '\'"nonexistentforsure"\' is not recognized as an internal or ' + 'external command,', 'operable program or batch file.' ]); @@ -547,7 +565,7 @@ if (isWindows) { } expect(lines).toEqual([ - "'nonexistentforsure' is not recognized as an internal or " + + '\'"nonexistentforsure"\' is not recognized as an internal or ' + 'external command,', 'operable program or batch file.' ]); diff --git a/src/test/normalize_test.ts b/src/test/normalize_test.ts index 6506583..8b37ff7 100644 --- a/src/test/normalize_test.ts +++ b/src/test/normalize_test.ts @@ -43,7 +43,7 @@ describe('normalizeSpawnCommand', () => { expect(normalized).toEqual({ command: 'cmd.exe', - args: ['/d', '/s', '/v:off', '/c', '"nonexistent ^"hi^""'], + args: ['/d', '/s', '/v:off', '/c', '""nonexistent" ^"hi^""'], options: {windowsVerbatimArguments: true} }); }); @@ -56,7 +56,7 @@ describe('normalizeSpawnCommand', () => { const normalized = normalizeSpawnCommand(relativePath, []); expect(normalized).toEqual({ command: 'cmd.exe', - args: ['/d', '/s', '/v:off', '/c', `"${relativePath}"`], + args: ['/d', '/s', '/v:off', '/c', `""${relativePath}""`], options: {windowsVerbatimArguments: true} }); }); @@ -99,14 +99,26 @@ describe('normalizeSpawnCommand', () => { // the batch interpreter consumes a second layer of `^` escapes when it // expands `%*`, so `&` must still be escaped at that point expect(normalized.args[4]).toBe( - `"${cmdPath} ^^^"\\^^^"^^^&injected^^^""` + `""${cmdPath}" ^^^"\\^^^"^^^&injected^^^""` ); }); test('single escapes meta chars in args to a non-batch command', () => { const normalized = normalizeSpawnCommand('nonexistent', ['"&injected']); - expect(normalized.args[4]).toBe('"nonexistent ^"\\^"^&injected^""'); + expect(normalized.args[4]).toBe('""nonexistent" ^"\\^"^&injected^""'); + }); + + test('passes the command to cmd.exe as a single quoted token', () => { + const normalized = normalizeSpawnCommand('a b&c(d)', ['arg']); + + expect(normalized.args[4]).toBe('""a b&c(d)" ^"arg^""'); + }); + + test('prevents variable expansion in the command', () => { + const normalized = normalizeSpawnCommand('a%CD%b', []); + + expect(normalized.args[4]).toBe('""a%%cd:~,%CD%%cd:~,%b""'); }); test('handles relative commands without extension', () => { @@ -117,7 +129,7 @@ describe('normalizeSpawnCommand', () => { const normalized = normalizeSpawnCommand(relativePath, []); expect(normalized).toEqual({ command: 'cmd.exe', - args: ['/d', '/s', '/v:off', '/c', `"${relativePath}"`], + args: ['/d', '/s', '/v:off', '/c', `""${relativePath}""`], options: {windowsVerbatimArguments: true} }); });