From b0f86dcba0629e1ab402c35e1ac2602348aad0d7 Mon Sep 17 00:00:00 2001 From: Sergio Benitez Date: Sun, 29 Jul 2018 18:24:25 -0700 Subject: [PATCH] Fix URI normalization checks in 'Rocket::mount()'. --- core/lib/src/rocket.rs | 38 +++++++++++++++++++----------------- core/lib/src/router/route.rs | 1 + examples/todo/src/main.rs | 2 +- 3 files changed, 22 insertions(+), 19 deletions(-) diff --git a/core/lib/src/rocket.rs b/core/lib/src/rocket.rs index 341900fc..368d7a47 100644 --- a/core/lib/src/rocket.rs +++ b/core/lib/src/rocket.rs @@ -499,22 +499,28 @@ impl Rocket { Paint::blue(base)); if base.contains('<') || base.contains('>') { - error_!("Invalid mount point: {}", base); - panic!("Mount points cannot contain dynamic parameters"); + error_!("Mount point '{}' contains dynamic paramters.", base); + panic!("Invalid mount point."); + } + + let base_uri = Origin::parse(base) + .unwrap_or_else(|e| { + error_!("Invalid origin URI '{}' used as mount point.", base); + panic!("Error: {}", e); + }); + + if base_uri.query().is_some() { + error_!("Mount point '{}' contains query string.", base); + panic!("Invalid mount point."); + } + + if !base_uri.is_normalized() { + error_!("Mount point '{}' is not normalized.", base_uri); + info_!("Expected: '{}'.", base_uri.to_normalized()); + panic!("Invalid mount point."); } for mut route in routes { - let base_uri = Origin::parse(base) - .unwrap_or_else(|e| { - error_!("Invalid origin URI used as mount point: {}", base); - panic!("Error: {}", e); - }); - - if base_uri.query().is_some() { - error_!("Mount point cannot contain a query string: {}", base_uri); - panic!("Invalid mount point."); - } - let complete_uri = format!("{}/{}", base_uri, route.uri); let uri = Origin::parse_route(&complete_uri) .unwrap_or_else(|e| { @@ -522,11 +528,7 @@ impl Rocket { panic!("Error: {}", e) }); - if !uri.is_normalized() { - warn_!("Abnormal URI '{}' will be automatically normalized.", uri); - } - - route.set_base(base_uri); + route.set_base(base_uri.clone()); route.set_uri(uri.to_normalized()); info_!("{}", route); diff --git a/core/lib/src/router/route.rs b/core/lib/src/router/route.rs index 65cb8615..f5eb356b 100644 --- a/core/lib/src/router/route.rs +++ b/core/lib/src/router/route.rs @@ -266,6 +266,7 @@ impl fmt::Debug for Route { #[doc(hidden)] impl<'a> From<&'a StaticRouteInfo> for Route { fn from(info: &'a StaticRouteInfo) -> Route { + // This should never panic since `info.path` is statically checked. let mut route = Route::new(info.method, info.path, info.handler); route.format = info.format.clone(); route.name = Some(info.name); diff --git a/examples/todo/src/main.rs b/examples/todo/src/main.rs index d6c4474d..f2aa293d 100644 --- a/examples/todo/src/main.rs +++ b/examples/todo/src/main.rs @@ -80,7 +80,7 @@ fn rocket() -> (Rocket, Option) { let rocket = rocket::ignite() .manage(pool) .mount("/", routes![index, static_files::all]) - .mount("/todo/", routes![new, toggle, delete]) + .mount("/todo", routes![new, toggle, delete]) .attach(Template::fairing()); (rocket, conn)