-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Fix Windows backslash paths being mangled when adding an SSH target #14554
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ec79965
Fix Windows backslash paths being mangled when adding an SSH target
sean-mcmanus f16281c
Alternative fix.
sean-mcmanus 6a30914
Merge branch 'main' into seanmcm/fixSSHBug
sean-mcmanus 7685554
Alternate fix.
sean-mcmanus a8aa3b6
Fixes. Unit tests.
sean-mcmanus fa62652
Copilot review fixes.
sean-mcmanus 28e7a97
Merge branch 'main' into seanmcm/fixSSHBug
sean-mcmanus File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| /* -------------------------------------------------------------------------------------------- | ||
| * Copyright (c) Microsoft Corporation. All Rights Reserved. | ||
| * See 'LICENSE' in the project root for license information. | ||
| * ------------------------------------------------------------------------------------------ */ | ||
|
|
||
| import { deepStrictEqual, strictEqual } from 'assert'; | ||
| import { describe, it } from 'mocha'; | ||
| import { splitArgs, sshCommandToConfig } from '../../src/SSH/sshCommandToConfig'; | ||
|
|
||
| // eslint-disable-next-line import/no-unassigned-import | ||
| require('source-map-support/register'); | ||
|
|
||
| describe('splitArgs', () => { | ||
| // [description, input, expected tokens] | ||
| const cases: [string, string, string[]][] = [ | ||
| ['empty string', '', []], | ||
| ['whitespace only', ' \t ', []], | ||
| ['simple words', 'ssh user@host', ['ssh', 'user@host']], | ||
| ['collapses runs of whitespace', 'ssh \t user@host', ['ssh', 'user@host']], | ||
| ['trims leading/trailing whitespace', ' ssh user@host ', ['ssh', 'user@host']], | ||
|
|
||
| // Windows paths: backslashes must stay literal (the original bug). | ||
| ['bare Windows path', 'ssh -i C:\\Users\\me\\key user@host', ['ssh', '-i', 'C:\\Users\\me\\key', 'user@host']], | ||
| ['double-quoted Windows path with spaces', 'ssh -i "C:\\Program Files\\me\\key" user@host', ['ssh', '-i', 'C:\\Program Files\\me\\key', 'user@host']], | ||
| ['single-quoted Windows path with spaces', "ssh -i 'C:\\Program Files\\me\\key' user@host", ['ssh', '-i', 'C:\\Program Files\\me\\key', 'user@host']], | ||
| ['single-quoted Windows path without spaces', "ssh -i 'C:\\Users\\me\\key' user@host", ['ssh', '-i', 'C:\\Users\\me\\key', 'user@host']], | ||
|
|
||
| // Quote handling. | ||
| ['strips double quotes', '"a b" c', ['a b', 'c']], | ||
| ['strips single quotes', "'a b' c", ['a b', 'c']], | ||
| ['quotes joined to adjacent text', 'a"b c"d', ['ab cd']], | ||
| ['single quotes inside double quotes are literal', '"it\'s here"', ["it's here"]], | ||
| ['double quotes inside single quotes are literal', "'say \"hi\"'", ['say "hi"']], | ||
| ['empty double-quoted token is preserved', 'a "" b', ['a', '', 'b']], | ||
| ['empty single-quoted token is preserved', "a '' b", ['a', '', 'b']], | ||
|
|
||
| // Forward-slash (POSIX-style) paths are unaffected. | ||
| ['forward-slash path', 'ssh -i /home/me/.ssh/id_rsa user@host', ['ssh', '-i', '/home/me/.ssh/id_rsa', 'user@host']], | ||
|
|
||
| // An unquoted backslash escapes a following whitespace character (POSIX behavior), so a | ||
| // Unix path with an escaped space stays a single argument. | ||
| ['backslash escapes a space', 'ssh -i /home/me/my\\ key user@host', ['ssh', '-i', '/home/me/my key', 'user@host']], | ||
| ['backslash escapes multiple spaces', 'ssh -i /home/me/key\\ with\\ spaces user@host', ['ssh', '-i', '/home/me/key with spaces', 'user@host']], | ||
| ['backslash escapes a tab', 'a\\\tb', ['a\tb']], | ||
| ['trailing backslash is literal', 'foo\\', ['foo\\']], | ||
| ['backslash before a letter stays literal (Windows path)', 'C:\\Users\\me', ['C:\\Users\\me']], | ||
| ['UNC path keeps doubled backslashes', '\\\\server\\share', ['\\\\server\\share']], | ||
| // A backslash that ends a quoted segment is literal and must not escape the following | ||
| // separator (only an unquoted backslash directly before whitespace escapes). | ||
| ['quoted path ending in backslash is not joined to the next arg', '"C:\\Program Files\\" next', ['C:\\Program Files\\', 'next']], | ||
|
|
||
| // Lenient handling of an unterminated quote: runs to end of string. | ||
| ['unterminated double quote runs to end', 'ssh -i "C:\\Users\\me', ['ssh', '-i', 'C:\\Users\\me']], | ||
| ['unterminated single quote runs to end', "ssh -i 'C:\\Users\\me", ['ssh', '-i', 'C:\\Users\\me']] | ||
| ]; | ||
|
|
||
| for (const [description, input, expected] of cases) { | ||
| it(`${description}: ${JSON.stringify(input)}`, () => { | ||
| deepStrictEqual(splitArgs(input), expected); | ||
| }); | ||
| } | ||
| }); | ||
|
|
||
| describe('sshCommandToConfig', () => { | ||
| it('preserves a bare Windows identity-file path', () => { | ||
| const config = sshCommandToConfig('ssh -i C:\\Users\\me\\.ssh\\id_rsa user@host'); | ||
| strictEqual(config.IdentityFile, 'C:\\Users\\me\\.ssh\\id_rsa'); | ||
| strictEqual(config.HostName, 'host'); | ||
| strictEqual(config.User, 'user'); | ||
| }); | ||
|
|
||
| it('preserves a single-quoted Windows identity-file path with spaces', () => { | ||
| const config = sshCommandToConfig("ssh -i 'C:\\Program Files\\me\\key' user@host"); | ||
| strictEqual(config.IdentityFile, 'C:\\Program Files\\me\\key'); | ||
| }); | ||
|
|
||
| it('preserves a double-quoted Windows identity-file path with spaces', () => { | ||
| const config = sshCommandToConfig('ssh -i "C:\\Program Files\\me\\key" user@host'); | ||
| strictEqual(config.IdentityFile, 'C:\\Program Files\\me\\key'); | ||
| }); | ||
|
|
||
| it('preserves a Unix identity-file path with a backslash-escaped space', () => { | ||
| const config = sshCommandToConfig('ssh -i /home/me/my\\ key user@host'); | ||
| strictEqual(config.IdentityFile, '/home/me/my key'); | ||
| strictEqual(config.HostName, 'host'); | ||
| strictEqual(config.User, 'user'); | ||
| }); | ||
|
|
||
| it('parses host, user, and port from the connection string', () => { | ||
| const config = sshCommandToConfig('ssh -p 2222 user@host'); | ||
| strictEqual(config.HostName, 'host'); | ||
| strictEqual(config.User, 'user'); | ||
| strictEqual(config.Port, '2222'); | ||
| }); | ||
| }); |
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.