diff --git a/crates/lib/page/src/server.rs b/crates/lib/page/src/server.rs index 26174e5..5e73dbb 100644 --- a/crates/lib/page/src/server.rs +++ b/crates/lib/page/src/server.rs @@ -130,6 +130,38 @@ impl PageServer { .and_then(|x| x.to_str().ok()) .unwrap_or(""); + // Normalize url with redirect + if route.ends_with('/') || route.contains("//") || route.starts_with('/') { + let mut new_route = route.clone(); + while new_route.contains("//") { + new_route = new_route.replace("//", "/"); + } + let new_route = new_route.trim_matches('/'); + + trace!( + message = "Redirecting route", + route, + new_route, + addr = ?addr.addr, + user_agent = ua, + device_type = ?client_info.device_type + ); + + let mut headers = HeaderMap::with_capacity(2); + + let new_route = match HeaderValue::from_str(&format!("/{new_route}")) { + Ok(x) => x, + Err(_) => { + // Be extra careful, this is user-provided data + return StatusCode::BAD_REQUEST.into_response(); + } + }; + + headers.append(header::LOCATION, new_route); + headers.append("Accept-CH", HeaderValue::from_static("Sec-CH-UA-Mobile")); + return (StatusCode::PERMANENT_REDIRECT, headers).into_response(); + } + trace!( message = "Serving route", route,