Don't allow empty paths (#421)

This commit is contained in:
David Pedersen
2021-10-26 15:37:25 +00:00
committed by GitHub
parent 9b17d86b92
commit e9533c566f
2 changed files with 28 additions and 2 deletions
+14 -2
View File
@@ -178,6 +178,8 @@ where
/// # axum::Server::bind(&"".parse().unwrap()).serve(app.into_make_service()).await.unwrap(); /// # axum::Server::bind(&"".parse().unwrap()).serve(app.into_make_service()).await.unwrap();
/// # }; /// # };
/// ``` /// ```
///
/// Also panics if `path` is empty.
pub fn route<T>(mut self, path: &str, svc: T) -> Self pub fn route<T>(mut self, path: &str, svc: T) -> Self
where where
T: Service<Request<B>, Response = Response<BoxBody>, Error = Infallible> T: Service<Request<B>, Response = Response<BoxBody>, Error = Infallible>
@@ -186,6 +188,10 @@ where
+ 'static, + 'static,
T::Future: Send + 'static, T::Future: Send + 'static,
{ {
if path.is_empty() {
panic!("Invalid route: empty path");
}
let id = RouteId::next(); let id = RouteId::next();
if let Err(err) = self.node.insert(path, id) { if let Err(err) = self.node.insert(path, id) {
@@ -310,8 +316,10 @@ where
/// ///
/// # Panics /// # Panics
/// ///
/// Panics if the route overlaps with another route. See [`Router::route`] /// - If the route overlaps with another route. See [`Router::route`]
/// for more details. /// for more details.
/// - If the route contains a wildcard (`*`).
/// - If `path` is empty.
/// ///
/// [`OriginalUri`]: crate::extract::OriginalUri /// [`OriginalUri`]: crate::extract::OriginalUri
pub fn nest<T>(mut self, path: &str, svc: T) -> Self pub fn nest<T>(mut self, path: &str, svc: T) -> Self
@@ -322,12 +330,16 @@ where
+ 'static, + 'static,
T::Future: Send + 'static, T::Future: Send + 'static,
{ {
let id = RouteId::next(); if path.is_empty() {
panic!("Invalid route: empty path");
}
if path.contains('*') { if path.contains('*') {
panic!("Invalid route: nested routes cannot contain wildcards (*)"); panic!("Invalid route: nested routes cannot contain wildcards (*)");
} }
let id = RouteId::next();
let path = if path == "/" { let path = if path == "/" {
format!("/*{}", NEST_TAIL_PARAM) format!("/*{}", NEST_TAIL_PARAM)
} else { } else {
+14
View File
@@ -658,6 +658,20 @@ async fn static_and_dynamic_paths() {
assert_eq!(res.text().await, "static"); assert_eq!(res.text().await, "static");
} }
#[tokio::test]
#[should_panic(expected = "Invalid route: empty path")]
async fn empty_route() {
let app = Router::new().route("", get(|| async {}));
TestClient::new(app);
}
#[tokio::test]
#[should_panic(expected = "Invalid route: empty path")]
async fn empty_route_nested() {
let app = Router::new().nest("", get(|| async {}));
TestClient::new(app);
}
pub(crate) fn assert_send<T: Send>() {} pub(crate) fn assert_send<T: Send>() {}
pub(crate) fn assert_sync<T: Sync>() {} pub(crate) fn assert_sync<T: Sync>() {}
pub(crate) fn assert_unpin<T: Unpin>() {} pub(crate) fn assert_unpin<T: Unpin>() {}