Fix URI normalization checks in 'Rocket::mount()'.

This commit is contained in:
Sergio Benitez 2018-07-29 18:24:25 -07:00
parent 56c6a96f6a
commit b0f86dcba0
3 changed files with 22 additions and 19 deletions

View File

@ -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.");
}
for mut route in routes {
let base_uri = Origin::parse(base)
.unwrap_or_else(|e| {
error_!("Invalid origin URI used as mount point: {}", base);
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);
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 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);

View File

@ -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);

View File

@ -80,7 +80,7 @@ fn rocket() -> (Rocket, Option<db::Conn>) {
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)