Skip to content

Commit 0ba6947

Browse files
bryanbeverlymcastorina
authored andcommitted
Add tilde expansion for TUI args after removing shell layer
Since sh -c was removed to fix command injection, ~/foo paths entered in the TUI are no longer expanded by a shell. This adds a narrow expandTilde helper that replaces a leading ~ with os.UserHomeDir() before exec, restoring path resolution without reintroducing any shell interpretation. Guards against empty $HOME to prevent ~/foo silently resolving to /foo. Made-with: Cursor
1 parent 287c3d0 commit 0ba6947

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

main.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,28 @@ var (
280280
usingTUI = false
281281
)
282282

283+
// expandTilde replaces a leading ~ in each argument with the user's home
284+
// directory. This compensates for bypassing the shell (which would normally
285+
// perform this expansion) while still avoiding shell metacharacter injection.
286+
func expandTilde(args []string) []string {
287+
home, err := os.UserHomeDir()
288+
if err != nil || home == "" {
289+
return args
290+
}
291+
out := make([]string, len(args))
292+
for i, arg := range args {
293+
switch {
294+
case arg == "~":
295+
out[i] = home
296+
case strings.HasPrefix(arg, "~/"):
297+
out[i] = home + arg[1:]
298+
default:
299+
out[i] = arg
300+
}
301+
}
302+
return out
303+
}
304+
283305
func init() {
284306
_, _ = maxprocs.Set()
285307

@@ -308,6 +330,11 @@ func init() {
308330
os.Exit(0)
309331
}
310332

333+
// The TUI passes user input as literal strings. Since we no longer
334+
// invoke a shell, we must expand ~ ourselves so paths like ~/foo
335+
// resolve correctly.
336+
args = expandTilde(args)
337+
311338
binary, err := exec.LookPath(os.Args[0])
312339
if err == nil {
313340
// On success, this call will never return. On failure, fallthrough

0 commit comments

Comments
 (0)