Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 17 additions & 67 deletions src-tauri/src/session_manager/terminal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,19 @@ end tell"#
}

fn launch_ghostty(command: &str, cwd: Option<&str>) -> Result<(), String> {
let args = build_ghostty_args(command, cwd);
let full_command = build_shell_command(command, cwd);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Avoid shell-expanding cwd in Ghostty launch

launch_ghostty now feeds cwd through build_shell_command and runs it via -c, which turns the path into shell-parsed text (cd "..." && ...). This regresses the previous --working-directory=<dir> behavior by allowing expansions/substitutions in directory names (for example $VAR or $(...)), so a crafted or unusual project path can execute unintended commands or resolve to the wrong directory when resuming a session. Pass the working directory as a dedicated Ghostty argument (or apply shell-safe literal escaping) instead of embedding it in a shell command string.

Useful? React with 👍 / 👎.

let shell = std::env::var("SHELL").unwrap_or_else(|_| "/bin/zsh".to_string());

let status = Command::new("open")
.args(args.iter().map(String::as_str))
.arg("-na")
.arg("Ghostty")
.arg("--args")
.arg("--quit-after-last-window-closed=true")
.arg("-e")
.arg(&shell)
.arg("-l")
.arg("-c")
.arg(&full_command)
.status()
.map_err(|e| format!("Failed to launch Ghostty: {e}"))?;

Expand All @@ -91,40 +100,6 @@ fn launch_ghostty(command: &str, cwd: Option<&str>) -> Result<(), String> {
}
}

fn build_ghostty_args(command: &str, cwd: Option<&str>) -> Vec<String> {
let input = ghostty_raw_input(command);

let mut args = vec![
"-na".to_string(),
"Ghostty".to_string(),
"--args".to_string(),
"--quit-after-last-window-closed=true".to_string(),
];

if let Some(dir) = cwd {
if !dir.trim().is_empty() {
args.push(format!("--working-directory={dir}"));
}
}

args.push(format!("--input={input}"));
args
}

fn ghostty_raw_input(command: &str) -> String {
let mut escaped = String::from("raw:");
for ch in command.chars() {
match ch {
'\\' => escaped.push_str("\\\\"),
'\n' => escaped.push_str("\\n"),
'\r' => escaped.push_str("\\r"),
_ => escaped.push(ch),
}
}
escaped.push_str("\\n");
escaped
}

fn launch_kitty(command: &str, cwd: Option<&str>) -> Result<(), String> {
let full_command = build_shell_command(command, cwd);

Expand Down Expand Up @@ -266,43 +241,18 @@ mod tests {
use super::*;

#[test]
fn ghostty_uses_shell_mode_for_resume_commands() {
let args = build_ghostty_args("claude --resume abc-123", Some("/tmp/project dir"));

assert_eq!(
args,
vec![
"-na",
"Ghostty",
"--args",
"--quit-after-last-window-closed=true",
"--working-directory=/tmp/project dir",
"--input=raw:claude --resume abc-123\\n",
]
);
}

#[test]
fn ghostty_keeps_command_without_cwd_prefix_when_not_provided() {
let args = build_ghostty_args("claude --resume abc-123", None);

fn build_shell_command_prefixes_cwd_for_ghostty_shell_execution() {
assert_eq!(
args,
vec![
"-na",
"Ghostty",
"--args",
"--quit-after-last-window-closed=true",
"--input=raw:claude --resume abc-123\\n",
]
build_shell_command("claude --resume abc-123", Some("/tmp/project dir")),
"cd \"/tmp/project dir\" && claude --resume abc-123"
);
}

#[test]
fn ghostty_escapes_newlines_and_backslashes_in_input() {
fn build_shell_command_keeps_command_without_cwd_prefix_when_not_provided() {
assert_eq!(
ghostty_raw_input("echo foo\\\\bar\npwd"),
"raw:echo foo\\\\\\\\bar\\npwd\\n"
build_shell_command("claude --resume abc-123", None),
"claude --resume abc-123"
);
}
}
Loading