[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:
@@ -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.
|
||||
|
||||
@@ -355,6 +355,20 @@ fn paths_under_the_prefix_are_within() {
|
||||
("/a", "/a/%C3%A9t%C3%A9.jpg"),
|
||||
("/a", "/a/%e2%9c%93"),
|
||||
("/a", "/a/a%20b"),
|
||||
// QC fix round 3: ordinary names with look-alike dots or spaces inside.
|
||||
("/a", "/a/Wait%E2%80%A6.pdf"),
|
||||
("/a", "/a/%E3%80%80photo"),
|
||||
("/a", "/a/my%C2%A0file.pdf"),
|
||||
("/a", "/a/dokumenty/p%C5%99ehled"),
|
||||
("/a", "/a/my%20file.pdf"),
|
||||
("/a", "/a/a+b"),
|
||||
("/a", "/a/caf%C3%A9"),
|
||||
("/a", "/a/e%CC%81"),
|
||||
("/a", "/a/%E2%80%93"),
|
||||
("/a", "/a/%E6%97%85%E8%A1%8C"),
|
||||
("/a", "/a/.well-known/x"),
|
||||
("/a", "/a/%C2%BD.txt"),
|
||||
("/a", "/a/x%3Fy"),
|
||||
] {
|
||||
assert!(path_within(prefix, path), "{prefix} should cover {path}");
|
||||
}
|
||||
@@ -431,6 +445,19 @@ fn no_path_trick_escapes_the_prefix() {
|
||||
("/a", "/a/%e3%80%80/b"),
|
||||
("/a", "/a/%zz/b"),
|
||||
("/a", "/a/%e2%9c"),
|
||||
// QC fix round 3: a segment that is only an ellipsis or a look-alike space, slash
|
||||
// look-alikes, and a broken escape that from_str_radix would take.
|
||||
("/a", "/a/%E2%80%A6/b"),
|
||||
("/a", "/a/%E2%80%A6"),
|
||||
("/a", "/a/%E3%80%80/b"),
|
||||
("/a", "/a/%C2%A0%C2%A0/b"),
|
||||
("/a", "/a/..%e2%88%95b"),
|
||||
("/a", "/a/..%e2%81%84b"),
|
||||
("/a", "/a/..%e2%a7%b8b"),
|
||||
("/a", "/a/x%e2%88%95y"),
|
||||
("/a", "/a/x%e2%81%84y"),
|
||||
("/a", "/a/%+f/b"),
|
||||
("/a", "/a/%-1/b"),
|
||||
];
|
||||
for (prefix, path) in cases {
|
||||
assert!(
|
||||
|
||||
Reference in New Issue
Block a user