mirror of https://github.com/fafhrd91/actix-web
dontuse lossy decoding
This commit is contained in:
parent
cc3cc216d5
commit
0c664cd79a
|
@ -33,6 +33,9 @@ pub enum UriSegmentError {
|
||||||
/// The segment ended with the wrapped invalid character.
|
/// The segment ended with the wrapped invalid character.
|
||||||
#[display(fmt = "The segment ended with the wrapped invalid character")]
|
#[display(fmt = "The segment ended with the wrapped invalid character")]
|
||||||
BadEnd(char),
|
BadEnd(char),
|
||||||
|
/// The path is not a valid UTF-8 string after doing percent decoding.
|
||||||
|
#[display(fmt = "The path is not a valif UTF-8 string after percent-decoding")]
|
||||||
|
NotValidUtf8,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Return `BadRequest` for `UriSegmentError`
|
/// Return `BadRequest` for `UriSegmentError`
|
||||||
|
|
|
@ -24,6 +24,10 @@ impl PathBufWrap {
|
||||||
pub fn parse_path(path: &str, hidden_files: bool) -> Result<Self, UriSegmentError> {
|
pub fn parse_path(path: &str, hidden_files: bool) -> Result<Self, UriSegmentError> {
|
||||||
let mut buf = PathBuf::new();
|
let mut buf = PathBuf::new();
|
||||||
|
|
||||||
|
let path = percent_encoding::percent_decode_str(path)
|
||||||
|
.decode_utf8()
|
||||||
|
.map_err(|_| UriSegmentError::NotValidUtf8)?;
|
||||||
|
|
||||||
for segment in path.split('/') {
|
for segment in path.split('/') {
|
||||||
if segment == ".." {
|
if segment == ".." {
|
||||||
buf.pop();
|
buf.pop();
|
||||||
|
|
|
@ -77,13 +77,11 @@ impl Service<ServiceRequest> for FilesService {
|
||||||
)));
|
)));
|
||||||
}
|
}
|
||||||
|
|
||||||
let path_decoded =
|
let real_path =
|
||||||
percent_encoding::percent_decode_str(req.match_info().path()).decode_utf8_lossy();
|
match PathBufWrap::parse_path(req.match_info().path(), self.hidden_files) {
|
||||||
|
Ok(item) => item,
|
||||||
let real_path = match PathBufWrap::parse_path(&path_decoded, self.hidden_files) {
|
Err(e) => return Box::pin(ok(req.error_response(e))),
|
||||||
Ok(item) => item,
|
};
|
||||||
Err(e) => return Box::pin(ok(req.error_response(e))),
|
|
||||||
};
|
|
||||||
|
|
||||||
if let Some(filter) = &self.path_filter {
|
if let Some(filter) = &self.path_filter {
|
||||||
if !filter(real_path.as_ref(), req.head()) {
|
if !filter(real_path.as_ref(), req.head()) {
|
||||||
|
|
Loading…
Reference in New Issue