From 329148465d2ddfa6e20156f62f9b19e649abcefd Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Mon, 30 Aug 2021 21:20:26 +0100 Subject: [PATCH] test more cases of path validation --- actix-router/src/resource.rs | 2 +- actix-web-codegen/src/route.rs | 2 +- .../trybuild/route-malformed-path-fail.rs | 34 ++++++++++++-- .../trybuild/route-malformed-path-fail.stderr | 45 +++++++++++++++++-- 4 files changed, 73 insertions(+), 10 deletions(-) diff --git a/actix-router/src/resource.rs b/actix-router/src/resource.rs index fbf29cc7a..e6e4b03bf 100644 --- a/actix-router/src/resource.rs +++ b/actix-router/src/resource.rs @@ -967,7 +967,7 @@ impl ResourceDef { _ => false, }) .unwrap_or_else(|| { - panic!(r#"path "{}" contains malformed dynamic segment"#, pattern) + panic!(r#"pattern "{}" contains malformed dynamic segment"#, pattern) }); let (mut param, mut unprocessed) = pattern.split_at(close_idx + 1); diff --git a/actix-web-codegen/src/route.rs b/actix-web-codegen/src/route.rs index df7fefa7c..c2f851a0e 100644 --- a/actix-web-codegen/src/route.rs +++ b/actix-web-codegen/src/route.rs @@ -102,6 +102,7 @@ impl Args { match arg { NestedMeta::Lit(syn::Lit::Str(lit)) => match path { None => { + let _ = ResourceDef::new(lit.value()); path = Some(lit); } _ => { @@ -114,7 +115,6 @@ impl Args { NestedMeta::Meta(syn::Meta::NameValue(nv)) => { if nv.path.is_ident("name") { if let syn::Lit::Str(lit) = nv.lit { - let _ = ResourceDef::new(lit.value()); resource_name = Some(lit); } else { return Err(syn::Error::new_spanned( diff --git a/actix-web-codegen/tests/trybuild/route-malformed-path-fail.rs b/actix-web-codegen/tests/trybuild/route-malformed-path-fail.rs index 978d12380..1258a6f2f 100644 --- a/actix-web-codegen/tests/trybuild/route-malformed-path-fail.rs +++ b/actix-web-codegen/tests/trybuild/route-malformed-path-fail.rs @@ -1,7 +1,33 @@ -use actix_web_codegen::*; +use actix_web_codegen::get; -#[get("/one/{", other)] -async fn one() -> String { - "Hello World!".to_owned() +#[get("/{")] +async fn zero() -> &'static str { + "malformed resource def" } + +#[get("/{foo")] +async fn one() -> &'static str { + "malformed resource def" +} + +#[get("/{}")] +async fn two() -> &'static str { + "malformed resource def" +} + +#[get("/*")] +async fn three() -> &'static str { + "malformed resource def" +} + +#[get("/{tail:\\d+}*")] +async fn four() -> &'static str { + "malformed resource def" +} + +#[get("/{a}/{b}/{c}/{d}/{e}/{f}/{g}/{h}/{i}/{j}/{k}/{l}/{m}/{n}/{o}/{p}/{q}")] +async fn five() -> &'static str { + "malformed resource def" +} + fn main() {} diff --git a/actix-web-codegen/tests/trybuild/route-malformed-path-fail.stderr b/actix-web-codegen/tests/trybuild/route-malformed-path-fail.stderr index 5341e0891..93c510109 100644 --- a/actix-web-codegen/tests/trybuild/route-malformed-path-fail.stderr +++ b/actix-web-codegen/tests/trybuild/route-malformed-path-fail.stderr @@ -1,5 +1,42 @@ -error: Unknown attribute. - --> $DIR/route-malformed-path-fail.rs:3:17 +error: custom attribute panicked + --> $DIR/route-malformed-path-fail.rs:3:1 | -3 | #[get("/one/{", other)] - | ^^^^^ +3 | #[get("/{")] + | ^^^^^^^^^^^^ + | + = help: message: pattern "{" contains malformed dynamic segment + +error: custom attribute panicked + --> $DIR/route-malformed-path-fail.rs:8:1 + | +8 | #[get("/{foo")] + | ^^^^^^^^^^^^^^^ + | + = help: message: pattern "{foo" contains malformed dynamic segment + +error: custom attribute panicked + --> $DIR/route-malformed-path-fail.rs:13:1 + | +13 | #[get("/{}")] + | ^^^^^^^^^^^^^ + | + = help: message: Wrong path pattern: "/{}" regex parse error: + ((?s-m)^/(?P<>[^/]+))$ + ^ + error: empty capture group name + +error: custom attribute panicked + --> $DIR/route-malformed-path-fail.rs:23:1 + | +23 | #[get("/{tail:\\d+}*")] + | ^^^^^^^^^^^^^^^^^^^^^^^ + | + = help: message: custom regex is not supported for tail match + +error: custom attribute panicked + --> $DIR/route-malformed-path-fail.rs:28:1 + | +28 | #[get("/{a}/{b}/{c}/{d}/{e}/{f}/{g}/{h}/{i}/{j}/{k}/{l}/{m}/{n}/{o}/{p}/{q}")] + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = help: message: Only 16 dynamic segments are allowed, provided: 17