Launcher proxy: keep a proxy's credentials out of messages; build again

An address the launcher can't use as a proxy is printed to stderr by
horizon-port-pack and shown under the Settings field or by `settings set`:
a user name and password in it now read `***`.

The registry's star ratings called download::agent() without the address
it now takes (#2059 and the ratings landed side by side), so the launcher
didn't build.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Ludovic Marie
2026-10-26 16:25:16 +01:00
parent 1851dc87a7
commit 600b2a1dda
3 changed files with 55 additions and 7 deletions
+40 -3
View File
@@ -91,8 +91,9 @@ pub fn keep(
pub fn parse(spec: &str) -> Option<ureq::Proxy> {
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<ureq::Proxy> {
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();
+1 -1
View File
@@ -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()
+14 -3
View File
@@ -53,8 +53,9 @@ pub fn refuse(spec: &str) -> Option<String> {
}
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<String> {
|| 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}");
}
}