diff --git a/crates/horizon-port-pack/src/net.rs b/crates/horizon-port-pack/src/net.rs index d829d1357..a441fb13a 100644 --- a/crates/horizon-port-pack/src/net.rs +++ b/crates/horizon-port-pack/src/net.rs @@ -91,8 +91,9 @@ pub fn keep( pub fn parse(spec: &str) -> Option { if socks(spec) { say(&format!( - "{spec} is a SOCKS proxy; downloads go without one. Write the http:// address your \ - proxy app listens on as well." + "{} is a SOCKS proxy; downloads go without one. Write the http:// address your \ + proxy app listens on as well.", + redacted(spec) )); return None; } @@ -100,13 +101,29 @@ pub fn parse(spec: &str) -> Option { Ok(proxy) => Some(proxy), Err(e) => { say(&format!( - "The proxy address {spec} can't be used ({e}); downloads go without one." + "The proxy address {} can't be used ({e}); downloads go without one.", + redacted(spec) )); None } } } +/// `spec` as it may be shown: a user name and password in it become `***`. An address that can't +/// be used is printed to stderr and shown in the launcher, and a proxy's credentials don't belong in +/// either. Everything up to the last `@` is the credentials, so a password with an unescaped `/` or +/// `@` is hidden too. +pub fn redacted(spec: &str) -> String { + let (scheme, rest) = match spec.split_once("://") { + Some((scheme, rest)) => (format!("{scheme}://"), rest), + None => (String::new(), spec), + }; + match rest.rfind('@') { + Some(at) => format!("{scheme}***@{}", &rest[at + 1..]), + None => spec.to_owned(), + } +} + /// Whether `spec` names a SOCKS proxy, which these downloads don't go through: `ureq` reads a /// SOCKS address happily and only fails when it connects through it. pub fn socks(spec: &str) -> bool { @@ -227,6 +244,26 @@ mod tests { lock(&TURN) } + #[test] + fn credentials_are_not_shown() { + assert_eq!(redacted("http://127.0.0.1:7890"), "http://127.0.0.1:7890"); + assert_eq!(redacted("proxy.local:3128"), "proxy.local:3128"); + assert_eq!( + redacted("http://user:pass@proxy.local:3128"), + "http://***@proxy.local:3128" + ); + assert_eq!( + redacted("socks5://u:p@10.0.0.1:1080"), + "socks5://***@10.0.0.1:1080" + ); + assert_eq!( + redacted("user:pass@proxy.local:3128"), + "***@proxy.local:3128" + ); + assert_eq!(redacted("http://u:p/a@ss@proxy:1"), "http://***@proxy:1"); + assert_eq!(redacted("http://user:pass@"), "http://***@"); + } + #[test] fn a_proxy_the_launcher_sets_is_trimmed_and_kept() { let _turn = alone(); diff --git a/launcher/src/browser/horizon.rs b/launcher/src/browser/horizon.rs index a34ab52fb..741fe336a 100644 --- a/launcher/src/browser/horizon.rs +++ b/launcher/src/browser/horizon.rs @@ -160,7 +160,7 @@ fn refresh_ratings() { } let at = given.unwrap_or_else(|| RATINGS_URL.to_owned()); let text = if at.starts_with("https://") { - download::agent().get(&at).call().ok().and_then(|r| { + download::agent(&at).get(&at).call().ok().and_then(|r| { use std::io::Read; let mut text = String::new(); r.into_reader() diff --git a/launcher/src/proxy.rs b/launcher/src/proxy.rs index 8d6493456..d08b932b9 100644 --- a/launcher/src/proxy.rs +++ b/launcher/src/proxy.rs @@ -53,8 +53,9 @@ pub fn refuse(spec: &str) -> Option { } if horizon_port_pack::net::socks(spec) { return Some(format!( - "{spec} is a SOCKS proxy; Horizon downloads through an HTTP proxy, such as the \ - http:// address your proxy app also listens on." + "{} is a SOCKS proxy; Horizon downloads through an HTTP proxy, such as the \ + http:// address your proxy app also listens on.", + horizon_port_pack::net::redacted(spec) )); } // `ureq` takes `http://` without a host, which would leave a download failing with nothing @@ -63,7 +64,8 @@ pub fn refuse(spec: &str) -> Option { || horizon_port_pack::net::host_of(spec).is_none_or(str::is_empty) { return Some(format!( - "{spec} isn't a proxy address. Write it as http://host:port." + "{} isn't a proxy address. Write it as http://host:port.", + horizon_port_pack::net::redacted(spec) )); } None @@ -137,4 +139,13 @@ mod tests { assert!(refuse("http://").is_some()); assert!(refuse("socks5://127.0.0.1:7891").is_some()); } + + /// The reason is shown under the field and printed by `horizon-launcher settings set`. + #[test] + fn a_refused_address_keeps_its_credentials_to_itself() { + let why = refuse("socks5://user:secret@127.0.0.1:7891").unwrap(); + assert!(why.starts_with("socks5://***@127.0.0.1:7891 "), "{why}"); + let why = refuse("http://user:secret@").unwrap(); + assert!(!why.contains("secret"), "{why}"); + } }