Fix Windows backslash paths being mangled when adding an SSH target#14554
Open
sean-mcmanus wants to merge 1 commit into
Open
Fix Windows backslash paths being mangled when adding an SSH target#14554sean-mcmanus wants to merge 1 commit into
sean-mcmanus wants to merge 1 commit into
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes a Windows-specific issue where shell-quote.parse() mangles backslash-based paths (e.g., C:\Users\...) when converting a user-entered SSH command into an SSH config entry, which can break IdentityFile and other path-bearing arguments.
Changes:
- Adds Windows OS detection via
isWindows. - On Windows, pre-processes the SSH command by doubling backslashes before passing it to
shell-quote.parse()to preserve Windows paths during tokenization.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+128
to
+131
| // shell-quote's parse() treats '\' as a POSIX escape character and strips it, which mangles | ||
| // Windows paths (e.g. '-i C:\Users\me\key' becomes 'C:Usersmekey'). On Windows, double the | ||
| // backslashes first so parse()'s unescaping restores the original single backslashes. | ||
| const parts: string[] = parse(isWindows ? command.replace(/\\/g, '\\\\') : command) as string[]; |
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.
Found and fixed using Copilot with Claude Opus 4.8 in VS Code.
Problem
When adding an SSH target via "C/C++: Add SSH Target" (or anywhere
sshCommandToConfigruns), the user-entered SSH connection command is tokenized withshell-quote'sparse(). On Windows,parse()treats\as a POSIX escape character and strips it, so any backslash path in the command is corrupted:The resulting SSH config entry gets a broken
IdentityFile(and the same corruption applies to any other path-bearing argument, e.g. a config file or control path). The connection then fails because the key file path no longer exists.Root cause
shell-quote'sparse()follows POSIX shell rules, where\escapes the next character. Windows paths use\as the directory separator, soC:\Users\meis read as the escape sequences\U,\m, etc., and the backslashes are consumed.This is long-standing behavior of
parse()and is independent of the recentshell-quote1.8.2 -> 1.8.4 bump (verified:parse()produces the identical mangled result in both versions). It is a pre-existing bug, surfaced while reviewing the extension's use ofshell-quote.Fix
On Windows only, double the backslashes in the command before passing it to
parse().parse()'s own unescaping then collapses each\\back to a single\, preserving the original Windows path:Non-Windows behavior is unchanged (the command is passed straight through), so POSIX paths and POSIX shell escaping are unaffected.
Validation
Verified the tokenizer output with the installed
shell-quotefor several inputs:ssh -i C:\Users\me\key.pem user@host-> path preserved asC:\Users\me\key.pemssh -i "C:\Program Files\me\key.pem" user@host-> quoted path with a space preserved asC:\Program Files\me\key.pemssh hello@microsoft.com -A-> unchanged (no paths)ssh -i /home/me/.ssh/id_rsa user@host-> unchanged (forward slashes)