axum-macros: report invalid TypedPath capture names without panicking (#3918)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Shuvro Roy · committed by GitHub · parent f8b02f2 · Browse files · 4 files·+56 added−4 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+56 added−4 removed
- tests:+45 added−0 removed
- other:+11 added−4 removed
Provenance
- Commit
f660aa9- Tree
e2c1df1- Author
Shuvro Roy <shuvro.nsu.cse@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
f8b02f2
4 files changed+56 added−4 removed
modified:axum-macros/src/typed_path.rs+9 added-4 removed
402 unmodified lines403 if capture.is_empty() || capture.contains('{') {404 return Err(syn::Error::new_spanned(path, "invalid capture in path"));405 }406 407- segments.push(Segment::Capture(408- capture.strip_prefix('*').unwrap_or(capture).to_owned(),409- path.span(),410- ));407+ let capture = capture.strip_prefix('*').unwrap_or(capture);408+ if syn::parse_str::<syn::Ident>(capture).map_or(true, |ident| ident != capture) {409+ return Err(syn::Error::new_spanned(410+ path,411+ format!("Invalid capture name `{capture}`: expected a Rust identifier"),412+ ));413+ }414 415+ segments.push(Segment::Capture(capture.to_owned(), path.span()));416+417 rest = &rest[end + 1..];418 } else {419 segments.push(Segment::Static(format!("{{{rest}")));420 rest = "";105 unmodified linesadded:axum-macros/tests/typed_path/fail/invalid_capture_name.rs+22 added
1+use axum_macros::TypedPath;2+use serde::Deserialize;3+4+#[derive(TypedPath, Deserialize)]5+#[typed_path("/users/{user-id}")]6+struct Named {7+ user_id: String,8+}9+10+#[derive(TypedPath, Deserialize)]11+#[typed_path("/users/{123}")]12+struct Tuple(String);13+14+#[derive(TypedPath, Deserialize)]15+#[typed_path("/files/{*file-name}")]16+struct Wildcard(String);17+18+#[derive(TypedPath, Deserialize)]19+#[typed_path("/users/{user_id }")]20+struct Whitespace(String);21+22+fn main() {}added:axum-macros/tests/typed_path/fail/invalid_capture_name.stderr+23 added
1+error: Invalid capture name `user-id`: expected a Rust identifier2+ --> tests/typed_path/fail/invalid_capture_name.rs:5:143+ |4+5 | #[typed_path("/users/{user-id}")]5+ | ^^^^^^^^^^^^^^^^^^6+7+error: Invalid capture name `123`: expected a Rust identifier8+ --> tests/typed_path/fail/invalid_capture_name.rs:11:149+ |10+11 | #[typed_path("/users/{123}")]11+ | ^^^^^^^^^^^^^^12+13+error: Invalid capture name `file-name`: expected a Rust identifier14+ --> tests/typed_path/fail/invalid_capture_name.rs:15:1415+ |16+15 | #[typed_path("/files/{*file-name}")]17+ | ^^^^^^^^^^^^^^^^^^^^^18+19+error: Invalid capture name `user_id `: expected a Rust identifier20+ --> tests/typed_path/fail/invalid_capture_name.rs:19:1421+ |22+19 | #[typed_path("/users/{user_id }")]23+ | ^^^^^^^^^^^^^^^^^^^modified:axum-macros/CHANGELOG.md+2 added
5 unmodified lines6 and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).7 8 # Unreleased9 10+- **fixed:** Report invalid `TypedPath` capture names without panicking ([#3918])11 - **breaking:** `#[from_request(via(Extractor))]` now uses the extractor's12 rejection type instead of `axum::response::Response` ([#3261])13 - **breaking:** `Option<T>` fields in `#[derive(FromRequest)]` and14 `#[derive(FromRequestParts)]` now use `OptionalFromRequest` /2 unmodified lines17 silently converting all rejections to `None`. Extractors used with18 `Option<T>` must implement `OptionalFromRequest` or19 `OptionalFromRequestParts`. ([#3623])20 21+[#3918]: https://github.com/tokio-rs/axum/pull/391822 [#3261]: https://github.com/tokio-rs/axum/pull/326123 [#3623]: https://github.com/tokio-rs/axum/issues/362324 25 # 0.5.1203 unmodified lines