[core] Share paths: judge dot segments on the NFKC form, so look-alike dots and spaces inside names pass; refuse slash look-alikes and broken escapes like %+f

This commit is contained in:
2026-11-07 19:40:49 +01:00
parent 17f26f94a3
commit cbb3e41ca7
2 changed files with 65 additions and 7 deletions
+38 -7
View File
@@ -402,7 +402,15 @@ pub fn check_prefix(prefix: &str) -> Result<String, &'static str> {
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] = &['.', '/', '\\', ';', '?', '#', '%', ':'];
/// ASCII characters a non-ASCII character must not turn into under NFKC.
const STRUCTURAL: &[char] = &['/', '\\', ';', '?', '#', '%'];
/// Slash and backslash look-alikes NFKC leaves alone (some upstreams still treat them as
/// separators): division slash, fraction slash, big solidus, big reverse solidus, set minus,
/// reverse solidus operator, box-drawing diagonals.
const SLASH_LOOKALIKES: &[char] = &[
'\u{2215}', '\u{2044}', '\u{29F8}', '\u{29F9}', '\u{2216}', '\u{29F5}', '\u{2571}', '\u{2572}',
];
/// Decode `%xx` escapes once. `None` for a broken escape.
fn percent_decode(raw: &str) -> Option<Vec<u8>> {
@@ -412,6 +420,10 @@ fn percent_decode(raw: &str) -> Option<Vec<u8>> {
while i < b.len() {
if b[i] == b'%' {
let hex = raw.get(i + 1..i + 3)?;
// from_str_radix would also take "+f".
if !hex.bytes().all(|c| c.is_ascii_hexdigit()) {
return None;
}
out.push(u8::from_str_radix(hex, 16).ok()?);
i += 3;
} else {
@@ -429,9 +441,12 @@ fn percent_decode(raw: &str) -> Option<Vec<u8>> {
/// - 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;
/// - any encoded character that NFKC normalisation turns into something containing `/`, `\`,
/// `;`, `?`, `#`, `%` or a control character (fullwidth and small forms), and the slash
/// look-alikes NFKC leaves alone ([`SLASH_LOOKALIKES`]);
/// - a segment that NFKC turns into only dots or nothing, after trimming whitespace (two-dot
/// leaders, ellipses, fullwidth dots, look-alike spaces); look-alike dots and spaces inside a
/// name with other characters (`Wait….pdf`, an ideographic space) are fine;
/// - 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.
@@ -461,13 +476,29 @@ pub fn path_within(prefix: &str, raw_path: &str) -> bool {
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())
SLASH_LOOKALIKES.contains(&c)
|| std::iter::once(c)
.nfkc()
.any(|n| STRUCTURAL.contains(&n) || n.is_control())
});
if looks_structural {
return false;
}
// Dot segments as an upstream that normalises (NFKC) and trims whitespace would see them.
let segments: Vec<&str> = decoded[1..].split('/').collect();
for (i, seg) in segments.iter().enumerate() {
if seg.is_empty() && i + 1 == segments.len() {
continue; // trailing slash
}
if seg.is_ascii() {
continue; // the raw checks below cover ASCII segments
}
let n: String = seg.nfkc().collect();
let name = n.split(';').next().unwrap_or("").trim();
if name.chars().all(|c| c == '.') {
return false;
}
}
for seg in lower[1..].split('/') {
// `..;x` is `..` to Tomcat and friends; `..%3b` is `..;` to servers that decode
// before splitting.