Skip to content

Commit

Permalink
fix trimming to inaccessible root path (#1678)
Browse files Browse the repository at this point in the history
  • Loading branch information
robjtede committed Sep 15, 2020
1 parent 4b4c9d1 commit 7f80732
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Changes

## Unreleased - 2020-xx-xx
### Fixed
* `NormalizePath` when used with `TrailingSlash::Trim` no longer trims the root path "/". [#1678]

[#1678]: https://github.com/actix/actix-web/pull/1678


## 3.0.1 - 2020-09-13
Expand Down
32 changes: 32 additions & 0 deletions src/middleware/normalize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ where
}
}

#[doc(hidden)]
pub struct NormalizePathNormalization<S> {
service: S,
merge_slash: Regex,
Expand Down Expand Up @@ -113,6 +114,10 @@ where
// normalize multiple /'s to one /
let path = self.merge_slash.replace_all(&path, "/");

// Ensure root paths are still resolvable. If resulting path is blank after previous step
// it means the path was one or more slashes. Reduce to single slash.
let path = if path.is_empty() { "/" } else { path.as_ref() };

// Check whether the path has been changed
//
// This check was previously implemented as string length comparison
Expand Down Expand Up @@ -158,10 +163,23 @@ mod tests {
let mut app = init_service(
App::new()
.wrap(NormalizePath::default())
.service(web::resource("/").to(HttpResponse::Ok))
.service(web::resource("/v1/something/").to(HttpResponse::Ok)),
)
.await;

let req = TestRequest::with_uri("/").to_request();
let res = call_service(&mut app, req).await;
assert!(res.status().is_success());

let req = TestRequest::with_uri("/?query=test").to_request();
let res = call_service(&mut app, req).await;
assert!(res.status().is_success());

let req = TestRequest::with_uri("///").to_request();
let res = call_service(&mut app, req).await;
assert!(res.status().is_success());

let req = TestRequest::with_uri("/v1//something////").to_request();
let res = call_service(&mut app, req).await;
assert!(res.status().is_success());
Expand All @@ -184,10 +202,24 @@ mod tests {
let mut app = init_service(
App::new()
.wrap(NormalizePath(TrailingSlash::Trim))
.service(web::resource("/").to(HttpResponse::Ok))
.service(web::resource("/v1/something").to(HttpResponse::Ok)),
)
.await;

// root paths should still work
let req = TestRequest::with_uri("/").to_request();
let res = call_service(&mut app, req).await;
assert!(res.status().is_success());

let req = TestRequest::with_uri("/?query=test").to_request();
let res = call_service(&mut app, req).await;
assert!(res.status().is_success());

let req = TestRequest::with_uri("///").to_request();
let res = call_service(&mut app, req).await;
assert!(res.status().is_success());

let req = TestRequest::with_uri("/v1/something////").to_request();
let res = call_service(&mut app, req).await;
assert!(res.status().is_success());
Expand Down

0 comments on commit 7f80732

Please sign in to comment.