[core] Share paths and errors: refuse overlong UTF-8, %u, look-alike dots and slashes and trimmed dot segments; never repeat URL credentials in an error

This commit is contained in:
2026-11-07 18:53:42 +01:00
parent 4448527b16
commit 2b66729f1b
5 changed files with 105 additions and 13 deletions
+36 -3
View File
@@ -406,14 +406,14 @@ fn check_host(file: &str, raw: RawHost, site: Option<&Site>, errors: &mut Vec<E>
} else {
errors.push(E::DuplicateUpstream {
file: f(),
url: u.clone(),
url: shown_url(u),
index,
});
}
}
Err(reason) => errors.push(E::BadUpstreamUrl {
file: f(),
url: u.clone(),
url: shown_url(u),
reason,
index: Some(index),
}),
@@ -423,7 +423,12 @@ fn check_host(file: &str, raw: RawHost, site: Option<&Site>, errors: &mut Vec<E>
if upstreams.windows(2).any(|w| w[0].scheme != w[1].scheme) {
errors.push(E::BadUpstreamUrl {
file: f(),
url: raw.upstreams.join(", "),
url: raw
.upstreams
.iter()
.map(|u| shown_url(u))
.collect::<Vec<_>>()
.join(", "),
reason: r::URL_MIXED,
index: None,
});
@@ -768,3 +773,31 @@ fn parse_url(raw: &str, allow_path: bool) -> Result<(Upstream, String), &'static
}
Ok((Upstream { scheme, host, port }, path))
}
/// A URL as an error may show it: a user name and password (`user:pass@`) are replaced by
/// `…@`, so a credential typed into an upstream never reaches a message, a log or the API.
pub(crate) fn shown_url(raw: &str) -> String {
let (scheme, rest) = match raw.find("://") {
Some(i) => (&raw[..i + 3], &raw[i + 3..]),
None => ("", raw),
};
let end = rest.find(['/', '?', '#']).unwrap_or(rest.len());
let (authority, tail) = rest.split_at(end);
match authority.rfind('@') {
Some(at) => format!("{scheme}\u{2026}@{}{tail}", &authority[at + 1..]),
None => raw.to_owned(),
}
}
#[cfg(test)]
mod shown_tests {
use super::shown_url;
#[test]
fn userinfo_is_hidden() {
assert_eq!(shown_url("http://user:hunter2@10.0.0.1:80/x"), "http://\u{2026}@10.0.0.1:80/x");
assert_eq!(shown_url("user:hunter2@host:80"), "\u{2026}@host:80");
assert_eq!(shown_url("http://10.0.0.1:80"), "http://10.0.0.1:80");
assert_eq!(shown_url("http://h/a@b"), "http://h/a@b");
}
}