| 1 |
package desync |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"strings" |
| 6 |
) |
| 7 |
|
| 8 |
// validateSSHHost rejects an ssh destination that begins with a dash. Such a |
| 9 |
// value (built from the store URL's user and host) would otherwise be parsed by |
| 10 |
// ssh as a command-line option rather than a destination, allowing option |
| 11 |
// injection such as -oProxyCommand=... and arbitrary local command execution |
| 12 |
// (the same class of bug as Git CVE-2017-1000117 and Mercurial CVE-2017-1000116). |
| 13 |
// Callers must reject the destination before passing it to ssh; the "--" |
| 14 |
// argument added to the ssh invocation is a defense-in-depth measure that only |
| 15 |
// some ssh implementations honor. |
| 16 |
func validateSSHHost(host string) error { |
| 17 |
if strings.HasPrefix(host, "-") { |
| 18 |
return fmt.Errorf("invalid ssh destination %q: must not begin with '-'", host) |
| 19 |
} |
| 20 |
return nil |
| 21 |
} |
| 22 |
|
| 23 |
// shellQuote wraps s in single quotes for safe inclusion in a POSIX shell |
| 24 |
// command line, escaping any embedded single quotes. This prevents shell |
| 25 |
// injection when interpolating untrusted values (such as a store URL path) into |
| 26 |
// the remote command string passed to ssh. |
| 27 |
func shellQuote(s string) string { |
| 28 |
return "'" + strings.ReplaceAll(s, "'", `'\''`) + "'" |
| 29 |
} |