test more cases of path validation

This commit is contained in:
Rob Ede 2021-08-30 21:20:26 +01:00
parent a1a9db15ac
commit 329148465d
No known key found for this signature in database
GPG Key ID: 97C636207D3EF933
4 changed files with 73 additions and 10 deletions

View File

@ -967,7 +967,7 @@ impl ResourceDef {
_ => false, _ => false,
}) })
.unwrap_or_else(|| { .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); let (mut param, mut unprocessed) = pattern.split_at(close_idx + 1);

View File

@ -102,6 +102,7 @@ impl Args {
match arg { match arg {
NestedMeta::Lit(syn::Lit::Str(lit)) => match path { NestedMeta::Lit(syn::Lit::Str(lit)) => match path {
None => { None => {
let _ = ResourceDef::new(lit.value());
path = Some(lit); path = Some(lit);
} }
_ => { _ => {
@ -114,7 +115,6 @@ impl Args {
NestedMeta::Meta(syn::Meta::NameValue(nv)) => { NestedMeta::Meta(syn::Meta::NameValue(nv)) => {
if nv.path.is_ident("name") { if nv.path.is_ident("name") {
if let syn::Lit::Str(lit) = nv.lit { if let syn::Lit::Str(lit) = nv.lit {
let _ = ResourceDef::new(lit.value());
resource_name = Some(lit); resource_name = Some(lit);
} else { } else {
return Err(syn::Error::new_spanned( return Err(syn::Error::new_spanned(

View File

@ -1,7 +1,33 @@
use actix_web_codegen::*; use actix_web_codegen::get;
#[get("/one/{", other)] #[get("/{")]
async fn one() -> String { async fn zero() -> &'static str {
"Hello World!".to_owned() "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() {} fn main() {}

View File

@ -1,5 +1,42 @@
error: Unknown attribute. error: custom attribute panicked
--> $DIR/route-malformed-path-fail.rs:3:17 --> $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