diff --git a/config/src/config.rs b/config/src/config.rs index bf4925823..aaf44bf8a 100644 --- a/config/src/config.rs +++ b/config/src/config.rs @@ -1935,7 +1935,9 @@ impl DroppedFileQuoting { Self::None => s.to_string(), Self::SpacesOnly => s.replace(" ", "\\ "), // https://docs.rs/shlex/latest/shlex/fn.quote.html - Self::Posix => shlex::quote(s).into_owned(), + Self::Posix => shlex::try_quote(s) + .unwrap_or_else(|_| "".into()) + .into_owned(), Self::Windows => { let chars_need_quoting = [' ', '\t', '\n', '\x0b', '\"']; if s.chars().any(|c| chars_need_quoting.contains(&c)) { diff --git a/config/src/keyassignment.rs b/config/src/keyassignment.rs index 3479f1c8f..ed284a5ec 100644 --- a/config/src/keyassignment.rs +++ b/config/src/keyassignment.rs @@ -229,7 +229,7 @@ impl SpawnCommand { if let Some(label) = &self.label { Some(label.to_string()) } else if let Some(args) = &self.args { - Some(shlex::join(args.iter().map(|s| s.as_str()))) + Some(shlex::try_join(args.iter().map(|s| s.as_str())).ok()?) } else { None } diff --git a/config/src/lua.rs b/config/src/lua.rs index ecc22432a..b36e50683 100644 --- a/config/src/lua.rs +++ b/config/src/lua.rs @@ -412,11 +412,13 @@ fn shell_split<'lua>(_: &'lua Lua, line: String) -> mlua::Result> { } fn shell_join_args<'lua>(_: &'lua Lua, args: Vec) -> mlua::Result { - Ok(shlex::join(args.iter().map(|arg| arg.as_ref()))) + Ok(shlex::try_join(args.iter().map(|arg| arg.as_ref())).map_err(mlua::Error::external)?) } fn shell_quote_arg<'lua>(_: &'lua Lua, arg: String) -> mlua::Result { - Ok(shlex::quote(&arg).into_owned()) + Ok(shlex::try_quote(&arg) + .map_err(mlua::Error::external)? + .into_owned()) } /// Returns the system hostname. diff --git a/wezterm-gui/src/frontend.rs b/wezterm-gui/src/frontend.rs index 6c1c5ff07..bfbe1a7c9 100644 --- a/wezterm-gui/src/frontend.rs +++ b/wezterm-gui/src/frontend.rs @@ -219,6 +219,16 @@ impl GuiFrontEnd { log::trace!("Got app event {event:?}"); match event { ApplicationEvent::OpenCommandScript(file_name) => { + let quoted_file_name = match shlex::try_quote(&file_name) { + Ok(name) => name.to_owned().to_string(), + Err(_) => { + log::error!( + "OpenCommandScript: {file_name} has embedded NUL bytes and + cannot be launched via the shell" + ); + return; + } + }; promise::spawn::spawn(async move { use config::keyassignment::SpawnTabDomain; use wezterm_term::TerminalSize; @@ -252,7 +262,7 @@ impl GuiFrontEnd { Ok((_tab, pane, _window_id)) => { log::trace!("Spawned {file_name} as pane_id {}", pane.pane_id()); let mut writer = pane.writer(); - write!(writer, "{} ; exit\n", shlex::quote(&file_name)).ok(); + write!(writer, "{quoted_file_name} ; exit\n").ok(); } Err(err) => { log::error!("Failed to spawn {file_name}: {err:#?}");