From 6ad88b828c49d691ea4323d3d284dcc0d2c560bb Mon Sep 17 00:00:00 2001 From: tangowithfoxtrot <5676771+tangowithfoxtrot@users.noreply.github.com> Date: Mon, 18 Mar 2024 07:12:26 -0700 Subject: [PATCH] fix: panic on invalid shell --- Cargo.lock | 14 ++++++++++++++ crates/bws/Cargo.toml | 1 + crates/bws/src/main.rs | 22 ++++++++++++++-------- 3 files changed, 29 insertions(+), 8 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 05a3f1d9b..38410e920 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -626,6 +626,7 @@ dependencies = [ "tokio", "toml 0.8.10", "uuid", + "which", ] [[package]] @@ -3982,6 +3983,19 @@ dependencies = [ "nom", ] +[[package]] +name = "which" +version = "6.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7fa5e0c10bf77f44aac573e498d1a82d5fbd5e91f6fc0a99e7be4b38e85e101c" +dependencies = [ + "either", + "home", + "once_cell", + "rustix", + "windows-sys 0.52.0", +] + [[package]] name = "winapi" version = "0.3.9" diff --git a/crates/bws/Cargo.toml b/crates/bws/Cargo.toml index cde0a4903..91904c2d4 100644 --- a/crates/bws/Cargo.toml +++ b/crates/bws/Cargo.toml @@ -44,6 +44,7 @@ toml = "0.8.10" uuid = { version = "^1.7.0", features = ["serde"] } bitwarden = { workspace = true, features = ["secrets"] } +which = "6.0.0" [dev-dependencies] tempfile = "3.10.0" diff --git a/crates/bws/src/main.rs b/crates/bws/src/main.rs index 003789cfc..86c36f3ae 100644 --- a/crates/bws/src/main.rs +++ b/crates/bws/src/main.rs @@ -27,6 +27,7 @@ mod state; use config::ProfileKey; use render::{serialize_response, Color, Output}; use uuid::Uuid; +use which::which; #[derive(Parser, Debug)] #[command(name = "bws", version, about = "Bitwarden Secrets CLI", long_about = None)] @@ -639,6 +640,19 @@ async fn process_commands() -> Result<()> { no_inherit_env, project_id, } => { + let shell = match std::env::consts::OS { + os if os == "linux" || os == "macos" || os.contains("bsd") => { + shell.unwrap_or_else(|| "sh".to_string()) + } + "windows" => shell.unwrap_or_else(|| "powershell".to_string()), + _ => unreachable!(), + }; + + if !which(&shell).is_ok() { + eprintln!("Error: shell '{}' not found", shell); + std::process::exit(1); + } + let user_command = if command.is_empty() { if atty::is(Stream::Stdin) { eprintln!("{}", Cli::command().render_help().ansi()); @@ -693,14 +707,6 @@ async fn process_commands() -> Result<()> { } } - let shell = match std::env::consts::OS { - os if os == "linux" || os == "macos" || os.contains("bsd") => { - shell.unwrap_or_else(|| "sh".to_string()) - } - "windows" => shell.unwrap_or_else(|| "powershell".to_string()), - _ => unreachable!(), - }; - let mut command = process::Command::new(shell); command .arg("-c")