Skip to content

Commit 313022f

Browse files
fix: errors when using untrimmed urls
1 parent 9f823d7 commit 313022f

File tree

2 files changed

+77
-4
lines changed

2 files changed

+77
-4
lines changed

crates/bws/src/cli.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::path::PathBuf;
22

33
use bitwarden_cli::Color;
4-
use clap::{ArgGroup, Parser, Subcommand, ValueEnum};
4+
use clap::{builder::ValueParser, ArgGroup, Parser, Subcommand, ValueEnum};
55
use clap_complete::Shell;
66
use uuid::Uuid;
77

@@ -63,10 +63,18 @@ pub(crate) struct Cli {
6363
#[arg(short = 'p', long, global = true, env = PROFILE_KEY_VAR_NAME, help="Profile to use from the config file")]
6464
pub(crate) profile: Option<String>,
6565

66-
#[arg(short = 'u', long, global = true, env = SERVER_URL_KEY_VAR_NAME, help="Override the server URL from the config file")]
66+
#[arg(short = 'u', long, global = true, env = SERVER_URL_KEY_VAR_NAME, help="Override the server URL from the config file", value_parser = ValueParser::new(url_parser) )]
6767
pub(crate) server_url: Option<String>,
6868
}
6969

70+
fn url_parser(value: &str) -> Result<String, String> {
71+
if value.starts_with("http://") || value.starts_with("https://") {
72+
Ok(value.trim_end_matches('/').into())
73+
} else {
74+
Err(format!("'{value}' is not a valid URL"))
75+
}
76+
}
77+
7078
#[derive(Subcommand, Debug)]
7179
pub(crate) enum Commands {
7280
#[command(long_about = "Configure the CLI", arg_required_else_help(true))]

crates/bws/src/config.rs

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,12 @@ pub(crate) fn update_profile(
9090
let mut config = load_config(config_file, false)?;
9191

9292
let p = config.profiles.entry(profile).or_default();
93-
name.update_profile_value(p, value);
93+
94+
if value.starts_with("http://") || value.starts_with("https://") {
95+
name.update_profile_value(p, value.trim_end_matches('/').to_string());
96+
} else {
97+
name.update_profile_value(p, value);
98+
}
9499

95100
write_config(config, config_file)?;
96101
Ok(())
@@ -172,7 +177,7 @@ impl Config {
172177

173178
#[cfg(test)]
174179
mod tests {
175-
use std::io::Write;
180+
use std::{io::Write, process::Command};
176181

177182
use tempfile::NamedTempFile;
178183

@@ -214,4 +219,64 @@ mod tests {
214219
c.unwrap().profiles["default"].server_base.as_ref().unwrap()
215220
);
216221
}
222+
223+
#[test]
224+
fn config_trims_trailing_forward_slashes_in_urls() {
225+
let tmpfile = NamedTempFile::new().unwrap();
226+
write!(tmpfile.as_file(), "[profiles.default]").unwrap();
227+
228+
let _ = update_profile(
229+
Some(tmpfile.as_ref()),
230+
"default".to_owned(),
231+
ProfileKey::server_base,
232+
"https://vault.bitwarden.com//////".to_owned(),
233+
);
234+
235+
let _ = update_profile(
236+
Some(tmpfile.as_ref()),
237+
"default".to_owned(),
238+
ProfileKey::server_api,
239+
"https://api.bitwarden.com/".to_owned(),
240+
);
241+
242+
let _ = update_profile(
243+
Some(tmpfile.as_ref()),
244+
"default".to_owned(),
245+
ProfileKey::server_identity,
246+
"https://identity.bitwarden.com/".to_owned(),
247+
);
248+
249+
let c = load_config(Some(Path::new(tmpfile.as_ref())), true).unwrap();
250+
assert_eq!(
251+
"https://vault.bitwarden.com",
252+
c.profiles["default"].server_base.as_ref().unwrap()
253+
);
254+
assert_eq!(
255+
"https://api.bitwarden.com",
256+
c.profiles["default"].server_api.as_ref().unwrap()
257+
);
258+
assert_eq!(
259+
"https://identity.bitwarden.com",
260+
c.profiles["default"].server_identity.as_ref().unwrap()
261+
);
262+
}
263+
264+
#[test]
265+
fn config_does_not_trim_forward_slashes_in_non_url_values() {
266+
let tmpfile = NamedTempFile::new().unwrap();
267+
write!(tmpfile.as_file(), "[profiles.default]").unwrap();
268+
269+
let _ = update_profile(
270+
Some(tmpfile.as_ref()),
271+
"default".to_owned(),
272+
ProfileKey::state_dir,
273+
"/dev/null/".to_owned(),
274+
);
275+
276+
let c = load_config(Some(Path::new(tmpfile.as_ref())), true).unwrap();
277+
assert_eq!(
278+
"/dev/null/",
279+
c.profiles["default"].state_dir.as_ref().unwrap()
280+
);
281+
}
217282
}

0 commit comments

Comments
 (0)