Requires paths start with a / (#823)

* Requires routes to start with `/`

* Also check routes in `TypedPath`

* changelog

* changelog links
This commit is contained in:
David Pedersen 2022-03-04 00:24:27 +01:00 committed by GitHub
parent ab486198e6
commit 90e74f12c4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 52 additions and 2 deletions

View file

@ -8,6 +8,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
# Unreleased
- **added:** Add `#[derive(TypedPath)]` for use with axum-extra's new "type safe" routing API ([#756])
- **breaking:** Routes are now required to start with `/`. Previously empty routes or routes such
as `:foo` would be accepted but most likely result in bugs ([#823])
[#823]: https://github.com/tokio-rs/axum/pull/823
# 0.1.0 (31. January, 2022)

View file

@ -284,6 +284,16 @@ fn captures_from_path(segments: &[Segment]) -> Vec<syn::Ident> {
}
fn parse_path(path: &LitStr) -> syn::Result<Vec<Segment>> {
let value = path.value();
if value.is_empty() {
return Err(syn::Error::new_spanned(
path,
"paths must start with a `/`. Use \"/\" for root routes",
));
} else if !path.value().starts_with('/') {
return Err(syn::Error::new_spanned(path, "paths must start with a `/`"));
}
path.value()
.split('/')
.map(|segment| {

View file

@ -0,0 +1,7 @@
use axum_extra::routing::TypedPath;
#[derive(TypedPath)]
#[typed_path("")]
struct MyPath;
fn main() {}

View file

@ -0,0 +1,5 @@
error: paths must start with a `/`. Use "/" for root routes
--> tests/typed_path/fail/route_not_starting_with_slash.rs:4:14
|
4 | #[typed_path("")]
| ^^

View file

@ -0,0 +1,7 @@
use axum_extra::routing::TypedPath;
#[derive(TypedPath)]
#[typed_path(":foo")]
struct MyPath;
fn main() {}

View file

@ -0,0 +1,5 @@
error: paths must start with a `/`
--> tests/typed_path/fail/route_not_starting_with_slash_non_empty.rs:4:14
|
4 | #[typed_path(":foo")]
| ^^^^^^

View file

@ -60,6 +60,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- **breaking:** `AddExtensionLayer` has been removed. Use `Extension` instead. It now implements
`tower::Layer` ([#807])
- **breaking:** `AddExtension` has been moved from the root module to `middleware`
- **breaking:** Routes are now required to start with `/`. Previously routes such as `:foo` would
be accepted but most likely result in bugs ([#823])
- **fixed:** Set `Allow` header when responding with `405 Method Not Allowed` ([#733])
- **fixed:** Correctly set the `Content-Length` header for response to `HEAD`
requests ([#734])
@ -82,6 +84,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
[#801]: https://github.com/tokio-rs/axum/pull/801
[#807]: https://github.com/tokio-rs/axum/pull/807
[#819]: https://github.com/tokio-rs/axum/pull/819
[#823]: https://github.com/tokio-rs/axum/pull/823
# 0.4.4 (13. January, 2022)

View file

@ -125,7 +125,9 @@ where
T::Future: Send + 'static,
{
if path.is_empty() {
panic!("Invalid route: empty path");
panic!("Paths must start with a `/`. Use \"/\" for root routes");
} else if !path.starts_with('/') {
panic!("Paths must start with a `/`");
}
let service = match try_downcast::<Router<B>, _>(service) {

View file

@ -441,7 +441,7 @@ async fn static_and_dynamic_paths() {
}
#[tokio::test]
#[should_panic(expected = "Invalid route: empty path")]
#[should_panic(expected = "Paths must start with a `/`. Use \"/\" for root routes")]
async fn empty_route() {
let app = Router::new().route("", get(|| async {}));
TestClient::new(app);
@ -678,3 +678,10 @@ async fn head_with_middleware_applied() {
// is compressed
assert!(!res.headers().contains_key("content-length"));
}
#[tokio::test]
#[should_panic(expected = "Paths must start with a `/`")]
async fn routes_must_start_with_slash() {
let app = Router::new().route(":foo", get(|| async {}));
TestClient::new(app);
}