-1

I am using actix-web as my web server, now I want to limit the request parameter length, what I am tried to do like this:

use actix_web::{get, App, HttpResponse, HttpServer, Responder, web};
use validator::Validate;

#[derive(serde::Deserialize, Validate)]
pub struct AppParams {
    #[validate(length(max = 2))]
    tag: String,
}

#[get("/")]
async fn hello(params: web::Query<AppParams>) -> impl Responder {
    HttpResponse::Ok().body("Hello,world!")
}

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    HttpServer::new(|| {
        App::new()
            .service(hello)
    })
    .bind("127.0.0.1:8080")?
    .run()
    .await
}

but when I using this command to check the parameter length in macOS terminal, seems did not work as expect:

> curl http://localhost:8080\?tag\=allallall
Hello,world!%

Am I missing something? what should I do to fixed this issue? This is the cargo dependecies:

[package]
name = "rust-learn"
version = "0.1.0"
edition = "2018"

[dependencies]
tokio = { version = "1.17.0", features = ["full"] }
serde = { version = "1.0.64", features = ["derive"] }
serde_json = "1.0.64"
actix-web = "4"
validator = { version = "0.16.1", features = ["derive"] }

I also tried to add actix-web-validator = "5.0.1" followed by the https://crates.io/crates/actix-web-validator. still did not work.

Dolphin
  • 29,069
  • 61
  • 260
  • 539
  • You need to call `params.validate` to check the conditions. – Jmb Aug 24 '23 at 06:32
  • You can have `validate` called automatically if you use [`actix-web-validator`](https://crates.io/crates/actix-web-validator). – Jmb Aug 24 '23 at 06:42
  • I added the actix-web-validator dependencies but still could not work. – Dolphin Aug 24 '23 at 06:48

1 Answers1

2

You're just using the wrong Query as your parameter type -- you're using actix_web::web::Query<_> but you should use actix_web_validator::Query<_> if you want validation.

cdhowie
  • 158,093
  • 24
  • 286
  • 300