Skip to content

process: fix backslash escaping in win32 git_process__cmdline - #7494

Open
insaf021 wants to merge 1 commit into
libgit2:mainfrom
insaf021:win32-cmdline-backslash
Open

insaf021 wants to merge 1 commit into
libgit2:mainfrom
insaf021:win32-cmdline-backslash

Conversation

@insaf021

Copy link
Copy Markdown

git_process__cmdline output fed back through CommandLineToArgvW, with the remote command ssh_exec builds for the path x'; echo INJECTED; ':

argv in : git-upload-pack 'x'\''; echo INJECTED; '\'''
argv out: git-upload-pack 'x'\\''; echo INJECTED; '\\'''

$ sh -c <argv out, with git-upload-pack replaced by echo>
x\
INJECTED

Windows only gives backslashes a meaning when they precede a double quote; anywhere else they are literal. The encoder escapes every backslash, so the child sees each one doubled. For ssh_exec that turns the '\'' used to quote a single quote in the repository path into '\\'', which ends the quoting in the remote shell, so an ssh URL path containing ' (a submodule URL, for instance) runs commands on the remote side. It affects Windows builds with USE_SSH=exec. The same function also left a quote open after an argument ending in whitespace, joining it with the next argument.

The fix emits backslashes literally, doubles them only before a quote (literal or one that toggles quoting), and closes any open quote at the end of each argument. Round-tripping 20000 random argument vectors through CommandLineToArgvW gave 13639 mismatches before and 0 after. The existing cmdline_escapes_backslash expectations encoded the doubling, so they are corrected; two tests are added for backslashes before a quote and for the unterminated quote. util_tests passes (VS 2022, x64, USE_SSH=exec).

Windows only treats backslashes specially when they precede a double
quote, so escaping every backslash handed the child process doubled
backslashes.  A quote opened for trailing whitespace was also left
open, joining that argument with the next one.

With the exec-based ssh transport the doubled backslash breaks the
'\'' quoting of the repository path for the remote shell, so a path
containing a single quote could end the quoting and run commands.

Emit backslashes literally, doubling them only before a quote, and
close any open quote at the end of each argument.

This branch has not been deployed

No deployments
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.

1 participant