Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

files: percent-decode url path #2398

Merged
merged 14 commits into from
Jan 4, 2022
1 change: 1 addition & 0 deletions actix-files/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,4 @@ tokio-uring = { version = "0.1", optional = true }
actix-rt = "2.2"
actix-web = "4.0.0-beta.11"
actix-test = "0.1.0-beta.7"
tempfile = "3.2"
3 changes: 3 additions & 0 deletions actix-files/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ pub enum UriSegmentError {
/// The segment ended with the wrapped invalid character.
#[display(fmt = "The segment ended with the wrapped invalid character")]
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`
Expand Down
23 changes: 23 additions & 0 deletions actix-files/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -804,6 +804,29 @@ mod tests {
assert_eq!(res.status(), StatusCode::OK);
}

#[actix_rt::test]
async fn test_percent_encoding_2() {
let tmpdir = tempfile::tempdir().unwrap();
let filename = match cfg!(unix) {
true => "ض:?#[]{}<>()@!$&'`|*+,;= %20.test",
false => "ض#[]{}()@!$&'`+,;= %20.test",
};
let filename_encoded = filename
.as_bytes()
.iter()
.map(|c| format!("%{:02X}", c))
.collect::<String>();
std::fs::File::create(tmpdir.path().join(filename)).unwrap();

let srv = test::init_service(App::new().service(Files::new("", tmpdir.path()))).await;

let req = TestRequest::get()
.uri(&format!("/{}", filename_encoded))
.to_request();
let res = test::call_service(&srv, req).await;
assert_eq!(res.status(), StatusCode::OK);
}

#[actix_rt::test]
async fn test_serve_named_file() {
let factory = NamedFile::open_async("Cargo.toml").await.unwrap();
Expand Down
4 changes: 4 additions & 0 deletions actix-files/src/path_buf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ impl PathBufWrap {
pub fn parse_path(path: &str, hidden_files: bool) -> Result<Self, UriSegmentError> {
let mut buf = PathBuf::new();

let path = percent_encoding::percent_decode_str(path)
robjtede marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Member

@robjtede robjtede Jan 4, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

needs a comment in here about why the "full" decode is acceptable

.decode_utf8()
.map_err(|_| UriSegmentError::NotValidUtf8)?;

for segment in path.split('/') {
if segment == ".." {
buf.pop();
Expand Down