- Notifications
You must be signed in to change notification settings - Fork907
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
0862dd0
c363edd
970b115
caee378
0969202
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
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. | ||
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. 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 commentThe reason will be displayed to describe this comment to others.Learn more. I've looked into this, and OpenSSH on Windows calls https://learn.microsoft.com/en-us/cpp/c-language/system-function?view=msvc-170 | ||
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 | ||
} |
Uh oh!
There was an error while loading.Please reload this page.