Skip to content

Quote a list for a .bat or .cmd program so cmd.exe can't run anything else - #42

Merged
HalRiveria-Bot merged 2 commits into
mainfrom
fix/batch-quote-injection
Nov 16, 2026
Merged

HalRiveria-Bot merged 2 commits into
mainfrom
fix/batch-quote-injection

Conversation

@HalRiveria-Bot

Copy link
Copy Markdown
Collaborator

What was wrong

A .bat or .cmd program runs through cmd.exe, which reads the command line before the batch file does. cmd.exe counts every quote, and to it a \" is just a quote. So when a list in Arguments held an element containing a quote, the kit's \" escaping could leave the rest of that element unquoted to cmd.exe. An element like a" & echo x>file & " then ran a second command. With an untrusted value in the list, that's command injection. Of 7 such payloads, all 7 ran against a batch file that does nothing. Paths can't contain ", so this only affected non-path arguments.

What changed

  • When the program is a .bat or .cmd, a list is now quoted with "" for a quote instead of \". That keeps cmd.exe's quote count even, so & | < > ^ ( ) inside the element stay quoted. A batch file that passes %* on to another program still delivers the exact argument, because the C runtime reads "" inside quotes as one quote. Backslashes are still doubled before a quote, as the C runtime needs.
  • The program is detected by its extension. A bare name only ever finds an .exe, so a batch file always has one. Trailing dots and spaces are ignored, because Windows drops them and run.cmd. still runs through cmd.exe.
  • An element holding a line break is refused for these programs, with an error naming the element. cmd.exe would end the command line there and silently drop the rest, and a carriage return is silently removed. Other programs still take it.
  • Unchanged: other programs, a string Arguments (passed exactly as written), and ConvertTo-PkArgumentString's own output. The quoting moved into a private ConvertTo-PkArgumentStringCore, and the public function's help now says to give the kit the list for a batch file.
  • Not covered: cmd.exe still expands %NAME% inside quotes, as the README already says.

I chose "" over refusing quotes outright. In testing it closed every payload, and it breaks nothing that worked before: a quote in an argument already couldn't reach a batch file correctly.

Breaking

A list element with a line break, passed to a .bat or .cmd, is now an error instead of being cut off silently.

How it was tested

New checks in harness section AE:

  • None of the 7 payloads can make a .cmd run another command, through Invoke-PkProcessWithTimeout, through the batch for a .BAT in capitals, and through a .cmd. path with a trailing dot.
  • A .cmd that passes %* to an argument-printing program gets 9 tricky arguments through exactly, including say "hi", a lone ", a\\"b and a trailing backslash.
  • A line break is refused, with a clear error and a batch StartError, but still reaches an ordinary program.

All of these failed before the change. The harness passes on Windows PowerShell 5.1 and PowerShell 7 (444 assertions each).

🤖 Generated with Claude Code

HalRiveria and others added 2 commits November 16, 2026 14:37
… else

cmd.exe reads a batch file's command line before the batch file does, and counts every quote, \" included. So a list element containing a quote, like a" & del ... & ", left what followed it unquoted, and cmd.exe ran it as a second command. With an untrusted value in the list, that was command injection.

When the program is a .bat or .cmd, a list is now quoted with "" for a quote instead of \". That keeps cmd.exe's quote count even, and a batch file that passes %* on to another program still delivers it as one quote, since the C runtime reads "" inside quotes that way. An element with a line break is refused for these programs: cmd.exe would end the command line there and silently drop the rest. Other programs, and ConvertTo-PkArgumentString's own output, are unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Windows drops trailing dots and spaces from a file name, so run.cmd. still runs through cmd.exe and needs the same quoting. The injection tests now write their marker by a bare name in the working folder, so a space in the temp path can't make them pass vacuously.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@HalRiveria-Bot
HalRiveria-Bot merged commit 9a5fb48 into main Nov 16, 2026
2 checks passed
@HalRiveria-Bot
HalRiveria-Bot deleted the fix/batch-quote-injection branch November 16, 2026 19:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants