Skip to content

Find, verify, and analyze leaked credentials

Expand tilde manually in TUI (#4827)

Miccah6c64db94d5b2
diff --git a/main.go b/main.go
index bbabd0130..d01a5fb50 100644
--- a/main.go
+++ b/main.go
@@ -280,6 +280,28 @@ var (
usingTUI = false
)
+// expandTilde replaces a leading ~ in each argument with the user's home
+// directory. This compensates for bypassing the shell (which would normally
+// perform this expansion) while still avoiding shell metacharacter injection.
+func expandTilde(args []string) []string {
+ home, err := os.UserHomeDir()
+ if err != nil || home == "" {
+ return args
+ }
+ out := make([]string, len(args))
+ for i, arg := range args {
+ switch {
+ case arg == "~":
+ out[i] = home
+ case strings.HasPrefix(arg, "~/"):
+ out[i] = home + arg[1:]
+ default:
+ out[i] = arg
+ }
+ }
+ return out
+}
+
func init() {
_, _ = maxprocs.Set()
@@ -308,12 +330,17 @@ func init() {
os.Exit(0)
}
- binary, err := exec.LookPath("sh")
+ // The TUI passes user input as literal strings. Since we no longer
+ // invoke a shell, we must expand ~ ourselves so paths like ~/foo
+ // resolve correctly.
+ args = expandTilde(args)
+
+ binary, err := exec.LookPath(os.Args[0])
if err == nil {
// On success, this call will never return. On failure, fallthrough
// to overwriting os.Args.
- cmd := strings.Join(append(os.Args[:1], args...), " ")
- _ = syscall.Exec(binary, []string{"sh", "-c", cmd}, append(os.Environ(), "TUI_PARENT=true"))
+ execArgs := append([]string{binary}, args...)
+ _ = syscall.Exec(binary, execArgs, append(os.Environ(), "TUI_PARENT=true"))
}
// Overwrite the Args slice so overseer works properly.