Repository navigation
Quote a list for a .bat or .cmd program so cmd.exe can't run anything else - #42
Merged
Merged
Conversation
… 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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What was wrong
A
.bator.cmdprogram runs throughcmd.exe, which reads the command line before the batch file does.cmd.execounts every quote, and to it a\"is just a quote. So when a list inArgumentsheld an element containing a quote, the kit's\"escaping could leave the rest of that element unquoted tocmd.exe. An element likea" & 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
.bator.cmd, a list is now quoted with""for a quote instead of\". That keepscmd.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..exe, so a batch file always has one. Trailing dots and spaces are ignored, because Windows drops them andrun.cmd.still runs throughcmd.exe.cmd.exewould end the command line there and silently drop the rest, and a carriage return is silently removed. Other programs still take it.Arguments(passed exactly as written), andConvertTo-PkArgumentString's own output. The quoting moved into a privateConvertTo-PkArgumentStringCore, and the public function's help now says to give the kit the list for a batch file.cmd.exestill 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
.bator.cmd, is now an error instead of being cut off silently.How it was tested
New checks in harness section AE:
.cmdrun another command, throughInvoke-PkProcessWithTimeout, through the batch for a.BATin capitals, and through a.cmd.path with a trailing dot..cmdthat passes%*to an argument-printing program gets 9 tricky arguments through exactly, includingsay "hi", a lone",a\\"band a trailing backslash.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