[core] Share paths and errors: decode once and refuse invalid UTF-8 and any NFKC look-alike of path structure, dots before any escape; hide everything before the last @ of a URL

This commit is contained in:
2026-11-07 19:18:17 +01:00
parent dd10557b87
commit 43918943da
6 changed files with 113 additions and 61 deletions
+9 -7
View File
@@ -774,17 +774,16 @@ 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.
/// A URL as an error may show it: everything before the last `@` (after the scheme) is
/// replaced by `…`, so a user name or password, even one containing `/`, `?` or `#`, never
/// reaches a message, a log or the API. A URL with no `@` is shown as it is.
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..]),
match rest.rfind('@') {
Some(at) => format!("{scheme}\u{2026}@{}", &rest[at + 1..]),
None => raw.to_owned(),
}
}
@@ -801,6 +800,9 @@ mod shown_tests {
);
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");
assert_eq!(shown_url("http://h/a@b"), "http://\u{2026}@b");
assert_eq!(shown_url("http://admin:hunter/2@10.0.0.1:80"), "http://\u{2026}@10.0.0.1:80");
assert_eq!(shown_url("http://admin:hunter?2@10.0.0.1:80"), "http://\u{2026}@10.0.0.1:80");
assert_eq!(shown_url("http://admin:hunter#2@10.0.0.1:80"), "http://\u{2026}@10.0.0.1:80");
}
}