[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:
+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");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user