mirror of
https://github.com/coder/coder.git
synced 2025-07-12 00:14:10 +00:00
fixes #18199 Corrects handling of paths with spaces in the `Match !exec` clause we use to determine whether Coder Connect is running. This is handled differently than the ProxyCommand, so we have a different escape routine, which also varies by OS. On Windows, we resort to a pretty gnarly hack, but it does work and I feel the only other option would be to reduce functionality such that we could not detect the Coder Connect state.
37 lines
1.4 KiB
Go
37 lines
1.4 KiB
Go
//go:build !windows
|
|
|
|
package cli
|
|
|
|
import (
|
|
"strings"
|
|
|
|
"golang.org/x/xerrors"
|
|
)
|
|
|
|
var hideForceUnixSlashes = true
|
|
|
|
// 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.
|
|
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)
|
|
}
|
|
// Quotes are allowed in path names on unix-like file systems, but OpenSSH's parsing of `Match exec` doesn't allow
|
|
// them.
|
|
if strings.Contains(path, `"`) {
|
|
return "", xerrors.Errorf("path must not contain quotes: %q", path)
|
|
}
|
|
|
|
// OpenSSH passes the match exec string directly to the user's shell. sh, bash and zsh accept spaces, tabs and
|
|
// backslashes simply escaped by a `\`. It's hard to predict exactly what more exotic shells might do, but this
|
|
// should work for macOS and most Linux distros in their default configuration.
|
|
path = strings.ReplaceAll(path, `\`, `\\`) // must be first, since later replacements add backslashes.
|
|
path = strings.ReplaceAll(path, " ", "\\ ")
|
|
path = strings.ReplaceAll(path, "\t", "\\\t")
|
|
return path, nil
|
|
}
|