|
@@ -118,8 +118,11 @@ fn percent_decode(s: &str) -> String {
|
|
|
let mut out = Vec::with_capacity(b.len());
|
|
let mut out = Vec::with_capacity(b.len());
|
|
|
let mut i = 0;
|
|
let mut i = 0;
|
|
|
while i < b.len() {
|
|
while i < b.len() {
|
|
|
|
|
+ // `from_str_radix` accepts a leading sign, so `%+1` would decode as
|
|
|
|
|
+ // a control byte. A percent escape is exactly two hex digits.
|
|
|
let hex = b
|
|
let hex = b
|
|
|
.get(i + 1..i + 3)
|
|
.get(i + 1..i + 3)
|
|
|
|
|
+ .filter(|h| h.iter().all(|c| c.is_ascii_hexdigit()))
|
|
|
.and_then(|h| std::str::from_utf8(h).ok());
|
|
.and_then(|h| std::str::from_utf8(h).ok());
|
|
|
match (b[i], hex.and_then(|h| u8::from_str_radix(h, 16).ok())) {
|
|
match (b[i], hex.and_then(|h| u8::from_str_radix(h, 16).ok())) {
|
|
|
(b'%', Some(v)) => {
|
|
(b'%', Some(v)) => {
|
|
@@ -493,6 +496,15 @@ mod tests {
|
|
|
String::from_utf16_lossy(v.strip_suffix(&[0]).unwrap_or(v))
|
|
String::from_utf16_lossy(v.strip_suffix(&[0]).unwrap_or(v))
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ #[test]
|
|
|
|
|
+ fn percent_decode_requires_two_hex_digits() {
|
|
|
|
|
+ assert_eq!(percent_decode("%20"), " ");
|
|
|
|
|
+ assert_eq!(percent_decode("%+1"), "%+1");
|
|
|
|
|
+ assert_eq!(percent_decode("%2"), "%2");
|
|
|
|
|
+ assert_eq!(percent_decode("%%20"), "% ");
|
|
|
|
|
+ assert_eq!(percent_decode("a%2Gb"), "a%2Gb");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
#[test]
|
|
#[test]
|
|
|
fn only_local_files_reach_the_host() {
|
|
fn only_local_files_reach_the_host() {
|
|
|
// Network schemes and shares are refused, naming scheme and host.
|
|
// Network schemes and shares are refused, naming scheme and host.
|