[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
+27 -9
View File
@@ -397,11 +397,25 @@ 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",
];
/// 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`), and the percent-encoded
/// forms of `.`, `/`, `\`, `%` and NUL. Other paths match on whole segments, case-sensitively:
/// 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`.
pub fn path_within(prefix: &str, raw_path: &str) -> bool {
if !raw_path.starts_with('/')
@@ -411,19 +425,23 @@ pub fn path_within(prefix: &str, raw_path: &str) -> bool {
return false;
}
let lower = raw_path.to_ascii_lowercase();
if ["%2e", "%2f", "%5c", "%25", "%00"]
.iter()
.any(|bad| lower.contains(bad))
{
if REFUSED_ENCODINGS.iter().any(|bad| lower.contains(bad)) {
return false;
}
// An empty segment anywhere but at the end (a trailing slash) is `//`.
if raw_path.contains("//") {
return false;
}
for seg in raw_path[1..].split('/') {
// `..;x` is `..` to Tomcat and friends.
let name = seg.split(';').next().unwrap_or(seg);
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'.') {
return false;
}