[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:
Generated
+16
@@ -1085,6 +1085,7 @@ dependencies = [
|
||||
"serde_json",
|
||||
"thiserror 2.0.21",
|
||||
"toml",
|
||||
"unicode-normalization",
|
||||
"url",
|
||||
]
|
||||
|
||||
@@ -2728,6 +2729,12 @@ dependencies = [
|
||||
"zerovec",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tinyvec"
|
||||
version = "1.13.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "fd3ca314f692efd6c868f8408f53fe444634a845f96c028b97d35f6a1f79f0ee"
|
||||
|
||||
[[package]]
|
||||
name = "tokio"
|
||||
version = "1.54.1"
|
||||
@@ -2939,6 +2946,15 @@ version = "1.0.27"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a2c754d6c33795a1c324727428e5a7dedb5b06195f9890bdbcba760d3e246563"
|
||||
|
||||
[[package]]
|
||||
name = "unicode-normalization"
|
||||
version = "0.1.26"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "75cd3fb03d5288b3ba9a1ee103ac7e9cbb59f0b03d64f8ddfcc04dd42758a85d"
|
||||
dependencies = [
|
||||
"tinyvec",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "unsafe-libyaml"
|
||||
version = "0.2.11"
|
||||
|
||||
@@ -15,6 +15,7 @@ thiserror.workspace = true
|
||||
url.workspace = true
|
||||
ring.workspace = true
|
||||
base64.workspace = true
|
||||
unicode-normalization = "0.1.24"
|
||||
|
||||
[lints]
|
||||
workspace = true
|
||||
|
||||
+59
-54
@@ -397,54 +397,49 @@ pub fn check_prefix(prefix: &str) -> Result<String, &'static str> {
|
||||
Ok(trimmed.to_owned())
|
||||
}
|
||||
|
||||
/// Percent-encodings no plain path needs, which some upstream reads as `.`, `/` or `\`
|
||||
/// (lowercased before matching):
|
||||
/// - `.`, `/`, `\`, `%` itself (double encoding) and NUL;
|
||||
/// - IIS `%u` escapes;
|
||||
/// - overlong UTF-8 (`%c0`, `%c1`, `%e0%80`–`%e0%9f`, `%f0%80`–`%f0%8f`) and bytes that never
|
||||
/// start UTF-8 (`%f5`–`%ff`);
|
||||
/// - fullwidth and look-alike dots and slashes that NFKC turns into `.` and `/`: U+FF0E,
|
||||
/// U+FF0F, U+FF3C, U+FE52, U+2024, U+2215, U+2044, U+29F8.
|
||||
pub const REFUSED_ENCODINGS: &[&str] = &[
|
||||
"%2e",
|
||||
"%2f",
|
||||
"%5c",
|
||||
"%25",
|
||||
"%00",
|
||||
"%u",
|
||||
"%c0",
|
||||
"%c1",
|
||||
"%e0%8",
|
||||
"%e0%9",
|
||||
"%f0%8",
|
||||
"%f5",
|
||||
"%f6",
|
||||
"%f7",
|
||||
"%f8",
|
||||
"%f9",
|
||||
"%fa",
|
||||
"%fb",
|
||||
"%fc",
|
||||
"%fd",
|
||||
"%fe",
|
||||
"%ff",
|
||||
"%ef%bc%8e",
|
||||
"%ef%bc%8f",
|
||||
"%ef%bc%bc",
|
||||
"%ef%b9%92",
|
||||
"%e2%80%a4",
|
||||
"%e2%88%95",
|
||||
"%e2%81%84",
|
||||
"%e2%a7%b8",
|
||||
];
|
||||
/// Percent-encodings no plain path needs (lowercased before matching): `.`, `/`, `\`, `%`
|
||||
/// itself (double encoding), NUL, and IIS `%u` escapes.
|
||||
pub const REFUSED_ENCODINGS: &[&str] = &["%2e", "%2f", "%5c", "%25", "%00", "%u"];
|
||||
|
||||
/// ASCII characters a look-alike must not stand for after NFKC normalisation.
|
||||
const STRUCTURAL: &[char] = &['.', '/', '\\', ';', '?', '#', '%', ':'];
|
||||
|
||||
/// Decode `%xx` escapes once. `None` for a broken escape.
|
||||
fn percent_decode(raw: &str) -> Option<Vec<u8>> {
|
||||
let b = raw.as_bytes();
|
||||
let mut out = Vec::with_capacity(b.len());
|
||||
let mut i = 0;
|
||||
while i < b.len() {
|
||||
if b[i] == b'%' {
|
||||
let hex = raw.get(i + 1..i + 3)?;
|
||||
out.push(u8::from_str_radix(hex, 16).ok()?);
|
||||
i += 3;
|
||||
} else {
|
||||
out.push(b[i]);
|
||||
i += 1;
|
||||
}
|
||||
}
|
||||
Some(out)
|
||||
}
|
||||
|
||||
/// Is `raw_path` (the request path exactly as sent, still percent-encoded) under `prefix`?
|
||||
///
|
||||
/// Refused outright, whatever the prefix: anything but visible ASCII, a backslash, an empty
|
||||
/// segment (`//`), a dot segment (`.`, `..`, also with `;params`, `%3b`, or trailing `%20` and
|
||||
/// `%09`), and every encoding in [`REFUSED_ENCODINGS`]. Other paths match on whole segments, case-sensitively:
|
||||
/// prefix `/a` covers `/a` and `/a/x`, not `/ab` or `/A`.
|
||||
/// Refused outright, whatever the prefix:
|
||||
/// - anything but visible ASCII, a backslash, an empty segment (`//`);
|
||||
/// - the encodings in [`REFUSED_ENCODINGS`] and broken `%` escapes;
|
||||
/// - escapes that do not decode to valid UTF-8 (this refuses every overlong form, such as
|
||||
/// `%c0%ae`);
|
||||
/// - any encoded character that NFKC normalisation turns into something containing `.`, `/`,
|
||||
/// `\`, `;`, `?`, `#`, `%` or `:` (fullwidth and small forms, two-dot leaders, division
|
||||
/// and fraction slashes, ...): an upstream that normalises would read structure there;
|
||||
/// - a segment of only dots, also with `;params` (`..;x`, `..%3b`), and any segment that is
|
||||
/// one or more dots followed by a percent-encoded byte (`..%20`, `..%3f`, `..%23`), since
|
||||
/// some servers trim or decode that into a dot segment.
|
||||
///
|
||||
/// Other paths match on whole segments, case-sensitively: prefix `/a` covers `/a` and `/a/x`,
|
||||
/// not `/ab` or `/A`.
|
||||
pub fn path_within(prefix: &str, raw_path: &str) -> bool {
|
||||
use unicode_normalization::UnicodeNormalization;
|
||||
if !raw_path.starts_with('/')
|
||||
|| !raw_path.bytes().all(|c| c.is_ascii_graphic())
|
||||
|| raw_path.contains('\\')
|
||||
@@ -459,17 +454,27 @@ pub fn path_within(prefix: &str, raw_path: &str) -> bool {
|
||||
if raw_path.contains("//") {
|
||||
return false;
|
||||
}
|
||||
let Some(decoded) = percent_decode(raw_path) else {
|
||||
return false;
|
||||
};
|
||||
let Ok(decoded) = String::from_utf8(decoded) else {
|
||||
return false;
|
||||
};
|
||||
let looks_structural = decoded.chars().filter(|c| !c.is_ascii()).any(|c| {
|
||||
std::iter::once(c)
|
||||
.nfkc()
|
||||
.any(|n| STRUCTURAL.contains(&n) || n.is_whitespace() || n.is_control())
|
||||
});
|
||||
if looks_structural {
|
||||
return false;
|
||||
}
|
||||
for seg in lower[1..].split('/') {
|
||||
// `..;x` is `..` to Tomcat and friends; `.. ` is `..` to Windows, which trims
|
||||
// trailing spaces; `..%3b` is `..;` to servers that decode before splitting.
|
||||
let name = seg
|
||||
.replace("%3b", ";")
|
||||
.split(';')
|
||||
.next()
|
||||
.unwrap_or("")
|
||||
.replace("%20", "")
|
||||
.replace("%09", "");
|
||||
if !name.is_empty() && name.bytes().all(|c| c == b'.') {
|
||||
// `..;x` is `..` to Tomcat and friends; `..%3b` is `..;` to servers that decode
|
||||
// before splitting.
|
||||
let seg = seg.replace("%3b", ";");
|
||||
let name = seg.split(';').next().unwrap_or("");
|
||||
let after_dots = name.trim_start_matches('.');
|
||||
if name.starts_with('.') && (after_dots.is_empty() || after_dots.starts_with('%')) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -255,3 +255,20 @@ fn a_password_in_an_upstream_url_is_never_repeated() {
|
||||
.unwrap();
|
||||
assert_eq!(e.params()["url"], "http://\u{2026}@10.0.0.2:3000");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn credentials_with_slash_query_or_hash_are_never_repeated() {
|
||||
for url in [
|
||||
"http://admin:hunter/2@10.0.0.1:80",
|
||||
"http://admin:hunter?2@10.0.0.1:80",
|
||||
"http://admin:hunter#2@10.0.0.1:80",
|
||||
"ftp://admin:hunter2@10.0.0.1",
|
||||
] {
|
||||
let errs = errors_for(&format!("hostname = \"app.test\"\nupstreams = [\"{url}\"]\n"));
|
||||
assert!(!errs.is_empty(), "{url} accepted");
|
||||
for e in &errs {
|
||||
let all = format!("{e} {:?}", e.params());
|
||||
assert!(!all.contains("hunter"), "{url}: {all}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -420,6 +420,17 @@ fn no_path_trick_escapes_the_prefix() {
|
||||
("/a", "/a/.;x/b"),
|
||||
("/a", "/a;/../b"),
|
||||
("/a", "/a/%2E./b"),
|
||||
// QC fix round 2: NFKC look-alikes and dots before an encoded byte.
|
||||
("/a", "/a/%e2%80%a5/b"),
|
||||
("/a", "/a/%E2%80%A5/b"),
|
||||
("/a", "/a/%ef%b8%b0/b"),
|
||||
("/a", "/a/..%ef%b9%a8secret"),
|
||||
("/a", "/a/..%3f/b"),
|
||||
("/a", "/a/..%3F"),
|
||||
("/a", "/a/..%23/b"),
|
||||
("/a", "/a/%e3%80%80/b"),
|
||||
("/a", "/a/%zz/b"),
|
||||
("/a", "/a/%e2%9c"),
|
||||
];
|
||||
for (prefix, path) in cases {
|
||||
assert!(
|
||||
|
||||
Reference in New Issue
Block a user