-
Notifications
You must be signed in to change notification settings - Fork 905
fix: handle paths with spaces in Match exec clause of SSH config #18266
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
5 commits
Select commit
Hold shift + click to select a range
0862dd0
fix: fix handling coder path with spaces in config-ssh
spikecurtis c363edd
add support for macOS & Linux
spikecurtis 970b115
add test, support backslashes
spikecurtis caee378
fixup test on Windows
spikecurtis 0969202
no quotes on non-Windows either
spikecurtis 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 |
---|---|---|
|
@@ -2,5 +2,58 @@ | |
|
||
package cli | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"golang.org/x/xerrors" | ||
) | ||
|
||
// Must be a var for unit tests to conform behavior | ||
var hideForceUnixSlashes = false | ||
|
||
// sshConfigMatchExecEscape prepares the path for use in `Match exec` statement. | ||
// | ||
// OpenSSH parses the Match line with a very simple tokenizer that accepts "-enclosed strings for the exec command, and | ||
// has no supported escape sequences for ". This means we cannot include " within the command to execute. | ||
// | ||
// To make matters worse, on Windows, OpenSSH passes the string directly to cmd.exe for execution, and as far as I can | ||
// tell, the only supported way to call a path that has spaces in it is to surround it with ". | ||
// | ||
// So, we can't actually include " directly, but here is a horrible workaround: | ||
// | ||
// "for /f %%a in ('powershell.exe -Command [char]34') do @cmd.exe /c %%aC:\Program Files\Coder\bin\coder.exe%%a connect exists %h" | ||
// | ||
// The key insight here is to store the character " in a variable (%a in this case, but the % itself needs to be | ||
// escaped, so it becomes %%a), and then use that variable to construct the double-quoted path: | ||
// | ||
// %%aC:\Program Files\Coder\bin\coder.exe%%a. | ||
// | ||
// How do we generate a single " character without actually using that character? I couldn't find any command in cmd.exe | ||
// to do it, but powershell.exe can convert ASCII to characters like this: `[char]34` (where 34 is the code point for "). | ||
// | ||
// Other notes: | ||
// - @ in `@cmd.exe` suppresses echoing it, so you don't get this command printed | ||
// - we need another invocation of cmd.exe (e.g. `do @cmd.exe /c %%aC:\Program Files\Coder\bin\coder.exe%%a`). Without | ||
// it the double-quote gets interpreted as part of the path, and you get: '"C:\Program' is not recognized. | ||
// Constructing the string and then passing it to another instance of cmd.exe does this trick here. | ||
// - OpenSSH passes the `Match exec` command to cmd.exe regardless of whether the user has a unix-like shell like | ||
// git bash, so we don't have a `forceUnixPath` option like for the ProxyCommand which does respect the user's | ||
// configured shell on Windows. | ||
func sshConfigMatchExecEscape(path string) (string, error) { | ||
// This is unlikely to ever happen, but newlines are allowed on | ||
// certain filesystems, but cannot be used inside ssh config. | ||
if strings.ContainsAny(path, "\n") { | ||
return "", xerrors.Errorf("invalid path: %s", path) | ||
} | ||
// Windows does not allow double-quotes or tabs in paths. If we get one it is an error. | ||
if strings.ContainsAny(path, "\"\t") { | ||
return "", xerrors.Errorf("path must not contain quotes or tabs: %q", path) | ||
} | ||
|
||
if strings.ContainsAny(path, " ") { | ||
// c.f. function comment for how this works. | ||
path = fmt.Sprintf("for /f %%%%a in ('powershell.exe -Command [char]34') do @cmd.exe /c %%%%a%s%%%%a", path) //nolint:gocritic // We don't want %q here. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💫😵💫 %%%%%%%%%%%%%%%%%%%%%%%% 💫😵💫 |
||
} | ||
return path, nil | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the great write-up! ❤️
QQ: Is there a chance the "default shell" that OpenSSH uses can be configured to be powershell instead of cmd.exe? And if yes, would the for loop break?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've looked into this, and OpenSSH on Windows calls
system
, which seems to always becmd.exe
.https://github.com/PowerShell/openssh-portable/blob/bdacf9868fe9e8024a5312861b7aaa6faba1821f/readconf.c#L544
https://learn.microsoft.com/en-us/cpp/c-language/system-function?view=msvc-170