From 3bdfd68f9d28def968b6493bcd5f4da086cd4acd Mon Sep 17 00:00:00 2001 From: Quentin Gliech Date: Fri, 13 Aug 2021 16:19:45 +0200 Subject: [PATCH] Rework warp top-filters to get proper 404 errors Before, some had `warp::get().and(warp::path!("foo"))`, which resulted to a `405 Method not allowed` instead of a 404. It also uses the `wrap::path!` macro instead of the function to ensure we're not setting a prefix --- matrix-authentication-service/src/handlers/health.rs | 4 ++-- .../src/handlers/oauth2/authorization.rs | 8 ++++---- .../src/handlers/oauth2/discovery.rs | 4 ++-- matrix-authentication-service/src/handlers/views/index.rs | 4 ++-- matrix-authentication-service/src/handlers/views/login.rs | 2 +- .../src/handlers/views/logout.rs | 4 ++-- .../src/handlers/views/reauth.rs | 2 +- 7 files changed, 14 insertions(+), 14 deletions(-) diff --git a/matrix-authentication-service/src/handlers/health.rs b/matrix-authentication-service/src/handlers/health.rs index 6b37d158d..e07225d11 100644 --- a/matrix-authentication-service/src/handlers/health.rs +++ b/matrix-authentication-service/src/handlers/health.rs @@ -23,8 +23,8 @@ use crate::{errors::WrapError, filters::database::with_connection}; pub fn filter( pool: &PgPool, ) -> impl Filter + Clone + Send + Sync + 'static { - warp::get() - .and(warp::path("health")) + warp::path!("health") + .and(warp::get()) .and(with_connection(pool)) .and_then(get) } diff --git a/matrix-authentication-service/src/handlers/oauth2/authorization.rs b/matrix-authentication-service/src/handlers/oauth2/authorization.rs index b265bfc44..64f1984d3 100644 --- a/matrix-authentication-service/src/handlers/oauth2/authorization.rs +++ b/matrix-authentication-service/src/handlers/oauth2/authorization.rs @@ -163,8 +163,8 @@ pub fn filter( cookies_config: &CookiesConfig, ) -> impl Filter + Clone + Send + Sync + 'static { let clients = oauth2_config.clients.clone(); - let authorize = warp::get() - .and(warp::path!("oauth2" / "authorize")) + let authorize = warp::path!("oauth2" / "authorize") + .and(warp::get()) .map(move || clients.clone()) .and(warp::query()) .and(with_optional_session(pool, cookies_config)) @@ -172,8 +172,8 @@ pub fn filter( .and(with_templates(templates)) .and_then(get); - let step = warp::get() - .and(warp::path!("oauth2" / "authorize" / "step")) + let step = warp::path!("oauth2" / "authorize" / "step") + .and(warp::get()) .and(warp::query().map(|s: StepRequest| s.id)) .and(with_session(pool, cookies_config)) .and(with_transaction(pool)) diff --git a/matrix-authentication-service/src/handlers/oauth2/discovery.rs b/matrix-authentication-service/src/handlers/oauth2/discovery.rs index e3c1305d1..0733cdac4 100644 --- a/matrix-authentication-service/src/handlers/oauth2/discovery.rs +++ b/matrix-authentication-service/src/handlers/oauth2/discovery.rs @@ -60,8 +60,8 @@ pub(super) fn filter( let cors = warp::cors().allow_any_origin(); - warp::get() - .and(warp::path!(".well-known" / "openid-configuration")) + warp::path!(".well-known" / "openid-configuration") + .and(warp::get()) .map(move || warp::reply::json(&metadata)) .with(cors) } diff --git a/matrix-authentication-service/src/handlers/views/index.rs b/matrix-authentication-service/src/handlers/views/index.rs index 4dfd72fd4..0d050f164 100644 --- a/matrix-authentication-service/src/handlers/views/index.rs +++ b/matrix-authentication-service/src/handlers/views/index.rs @@ -32,8 +32,8 @@ pub(super) fn filter( csrf_config: &CsrfConfig, cookies_config: &CookiesConfig, ) -> impl Filter + Clone + Send + Sync + 'static { - warp::get() - .and(warp::path::end()) + warp::path::end() + .and(warp::get()) .and(with_templates(templates)) .and(updated_csrf_token(cookies_config, csrf_config)) .and(with_optional_session(pool, cookies_config)) diff --git a/matrix-authentication-service/src/handlers/views/login.rs b/matrix-authentication-service/src/handlers/views/login.rs index 5c3c63e82..989ed846f 100644 --- a/matrix-authentication-service/src/handlers/views/login.rs +++ b/matrix-authentication-service/src/handlers/views/login.rs @@ -99,7 +99,7 @@ pub(super) fn filter( .untuple_one() .with(wrap_fn(save_session(cookies_config))); - warp::path("login").and(get.or(post)) + warp::path!("login").and(get.or(post)) } async fn get( diff --git a/matrix-authentication-service/src/handlers/views/logout.rs b/matrix-authentication-service/src/handlers/views/logout.rs index 76f419966..99be03189 100644 --- a/matrix-authentication-service/src/handlers/views/logout.rs +++ b/matrix-authentication-service/src/handlers/views/logout.rs @@ -26,8 +26,8 @@ pub(super) fn filter( pool: &PgPool, cookies_config: &CookiesConfig, ) -> impl Filter + Clone + Send + Sync + 'static { - warp::post() - .and(warp::path("logout")) + warp::path!("logout") + .and(warp::post()) .and(with_session(pool, cookies_config)) .and(with_connection(pool)) .and(protected_form(cookies_config)) diff --git a/matrix-authentication-service/src/handlers/views/reauth.rs b/matrix-authentication-service/src/handlers/views/reauth.rs index bd1555d69..d6a91e0ef 100644 --- a/matrix-authentication-service/src/handlers/views/reauth.rs +++ b/matrix-authentication-service/src/handlers/views/reauth.rs @@ -54,7 +54,7 @@ pub(super) fn filter( .and(protected_form(cookies_config)) .and_then(post); - warp::path("reauth").and(get.or(post)) + warp::path!("reauth").and(get.or(post)) } async fn get(