Skip to content

Commit cf21df1

Browse files
authored
Path: fix unsafe malformed string (#359)
1 parent a1bf866 commit cf21df1

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

actix-router/CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
## Unreleased - 2021-xx-xx
44
* When matching URL parameters, `%25` is kept in the percent-encoded form - no longer decoded to `%`. [#357]
5+
* Fixed a bug where the `Path` extractor returns unsafe malformed string due to malformed URL. [#359]
56

67
[#357]: https://github.com/actix/actix-net/pull/357
8+
[#359]: https://github.com/actix/actix-net/pull/359
79

810

911
## 0.2.7 - 2021-02-06

actix-router/src/url.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -170,11 +170,7 @@ impl Quoter {
170170
idx += 1;
171171
}
172172

173-
cloned.map(|data| {
174-
// SAFETY: we get data from http::Uri, which does UTF-8 checks already
175-
// this code only decodes valid pct encoded values
176-
unsafe { String::from_utf8_unchecked(data) }
177-
})
173+
cloned.map(|data| String::from_utf8_lossy(&data).into_owned())
178174
}
179175
}
180176

@@ -259,6 +255,16 @@ mod tests {
259255
assert_eq!(path.get("id").unwrap(), &test);
260256
}
261257

258+
#[test]
259+
fn test_invalid_utf8() {
260+
let invalid_utf8 = percent_encode((0x80..=0xff).collect::<Vec<_>>().as_slice());
261+
let uri = Uri::try_from(format!("/{}", invalid_utf8)).unwrap();
262+
let path = Path::new(Url::new(uri));
263+
264+
// We should always get a valid utf8 string
265+
assert!(String::from_utf8(path.path().as_bytes().to_owned()).is_ok());
266+
}
267+
262268
#[test]
263269
fn test_from_hex() {
264270
let hex = b"0123456789abcdefABCDEF";

0 commit comments

Comments
 (0)