Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bad request 400 #2

Open
pronebird opened this issue Jan 16, 2025 · 9 comments · May be fixed by #3
Open

Bad request 400 #2

pronebird opened this issue Jan 16, 2025 · 9 comments · May be fixed by #3

Comments

@pronebird
Copy link

pronebird commented Jan 16, 2025

Adding the RateLimitLayer configuration makes axum always return http 400.

    let app = Router::new()
        .route("/new-post", post(new_post))
+        .route_layer(
+            RateLimitLayer::<RealIp>::builder()
+                .with_default_quota(Quota::simple(Duration::from_secs(5)))
+                .with_global_fallback(true)
+                .with_extension(true)
+                .default_handle_error(),
+       )
        .layer(Extension(state));
@novacrazy
Copy link
Member

Are you using app.into_make_service_with_connect_info::<SocketAddr>()?

@pronebird
Copy link
Author

Are you using app.into_make_service_with_connect_info::<SocketAddr>()?

Oh of course I don’t. I followed the example from readme. That’s probably the missing bit!

@pronebird
Copy link
Author

pronebird commented Jan 16, 2025

Well that didn't work. That's my setup:

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let state = Arc::new(Database::new().await?);

    let app = Router::new()
        .route("/new-post", post(new_post))
        .layer(Extension(state))
        .route_layer(
            RateLimitLayer::<RealIp>::builder()
                .with_default_quota(Quota::simple(Duration::from_secs(5)))
                .with_global_fallback(true)
                .with_extension(true)
                .default_handle_error(),
        );
    let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await?;

    tracing::debug!("Listening on {}", listener.local_addr()?);

    axum::serve(
        listener,
        app.into_make_service_with_connect_info::<SocketAddr>(),
    )
    .await?;

    Ok(())
}

@novacrazy
Copy link
Member

novacrazy commented Jan 16, 2025

Oh, is the "tokio" feature enabled for axum_gcra? It should be for it to fallback to the local SocketAddr. I should document that better.

@pronebird
Copy link
Author

tokio is seemingly enabled by default when I run cargo add

axum_gcra v0.1.1 to dependencies
             Features:
             + ahash
             + real_ip
             + tokio

@pronebird
Copy link
Author

pronebird commented Jan 16, 2025

Just ran a debugger, it errors when extracting the "user_key":

Image

If I step inside, the following snippet fails to extract the IP:

if let Some(ip) = parts.extensions.get::<RealIp>() {
  return Ok(*ip);
}

@novacrazy
Copy link
Member

Oh. You're using axum 0.8, aren't you?

I created a new project with nearly your exact code using axum 0.7 and it worked fine, but if I use axum 0.8 it somehow still compiles fine but throws that 400 error.

@novacrazy
Copy link
Member

I'll find some time tonight/tomorrow to go through and upgrade to axum 0.8 for this crate, which may or may not be easy.

@pronebird
Copy link
Author

pronebird commented Jan 16, 2025

Yeah I use axum v0.8. Seems like something changed and axum_gcra cannot fetch the client ip from it anymore. Thank you!

@pronebird pronebird linked a pull request Jan 16, 2025 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants