Merge branch 'unrecognizedmethods' into 'next'

fix: send unrecognized error on wrong http methods

See merge request famedly/conduit!397
This commit is contained in:
Timo Kösters 2022-10-15 08:35:39 +00:00
commit aca6218c0a
7 changed files with 40 additions and 10 deletions

View file

@ -55,7 +55,9 @@ where
Err(e) => { Err(e) => {
warn!( warn!(
"Could not send request to appservice {:?} at {}: {}", "Could not send request to appservice {:?} at {}: {}",
registration.get("id"), destination, e registration.get("id"),
destination,
e
); );
return Err(e.into()); return Err(e.into());
} }

View file

@ -443,7 +443,7 @@ pub(crate) async fn claim_keys_helper(
let mut futures: FuturesUnordered<_> = get_over_federation let mut futures: FuturesUnordered<_> = get_over_federation
.into_iter() .into_iter()
.map(|(server, vec)| async move { .map(|(server, vec)| async move {
let mut one_time_keys_input_fed = BTreeMap::new(); let mut one_time_keys_input_fed = BTreeMap::new();
for (user_id, keys) in vec { for (user_id, keys) in vec {
one_time_keys_input_fed.insert(user_id.clone(), keys.clone()); one_time_keys_input_fed.insert(user_id.clone(), keys.clone());
} }
@ -459,7 +459,8 @@ pub(crate) async fn claim_keys_helper(
) )
.await, .await,
) )
}).collect(); })
.collect();
while let Some((server, response)) = futures.next().await { while let Some((server, response)) = futures.next().await {
match response { match response {

View file

@ -1,7 +1,10 @@
use crate::{services, Error, Result, Ruma}; use crate::{services, Error, Result, Ruma};
use ruma::{ use ruma::{
api::client::{error::ErrorKind, read_marker::set_read_marker, receipt::create_receipt}, api::client::{error::ErrorKind, read_marker::set_read_marker, receipt::create_receipt},
events::{receipt::{ReceiptType, ReceiptThread}, RoomAccountDataEventType}, events::{
receipt::{ReceiptThread, ReceiptType},
RoomAccountDataEventType,
},
MilliSecondsSinceUnixEpoch, MilliSecondsSinceUnixEpoch,
}; };
use std::collections::BTreeMap; use std::collections::BTreeMap;

View file

@ -305,9 +305,12 @@ where
} }
} }
Err(e) => { Err(e) => {
warn!("Could not send request to {} at {}: {}", destination, actual_destination_str, e); warn!(
"Could not send request to {} at {}: {}",
destination, actual_destination_str, e
);
Err(e.into()) Err(e.into())
}, }
} }
} }

View file

@ -6,7 +6,7 @@ use crate::{
self, self,
sending::{OutgoingKind, SendingEventType}, sending::{OutgoingKind, SendingEventType},
}, },
utils, Error, Result, services, services, utils, Error, Result,
}; };
impl service::sending::Data for KeyValueDatabase { impl service::sending::Data for KeyValueDatabase {

View file

@ -145,6 +145,7 @@ async fn run_server() -> io::Result<()> {
}), }),
) )
.compression() .compression()
.layer(axum::middleware::from_fn(unrecognized_method))
.layer( .layer(
CorsLayer::new() CorsLayer::new()
.allow_origin(cors::Any) .allow_origin(cors::Any)
@ -187,6 +188,22 @@ async fn run_server() -> io::Result<()> {
Ok(()) Ok(())
} }
async fn unrecognized_method<B>(
req: axum::http::Request<B>,
next: axum::middleware::Next<B>,
) -> std::result::Result<axum::response::Response, axum::http::StatusCode> {
let method = req.method().clone();
let uri = req.uri().clone();
let inner = next.run(req).await;
if inner.status() == axum::http::StatusCode::METHOD_NOT_ALLOWED {
warn!("Method not allowed: {method} {uri}");
return Ok(
Error::BadRequest(ErrorKind::Unrecognized, "Unrecognized request").into_response(),
);
}
Ok(inner)
}
fn routes() -> Router { fn routes() -> Router {
Router::new() Router::new()
.ruma_route(client_server::get_supported_versions_route) .ruma_route(client_server::get_supported_versions_route)
@ -386,12 +403,16 @@ async fn shutdown_signal(handle: ServerHandle) {
handle.graceful_shutdown(Some(Duration::from_secs(30))); handle.graceful_shutdown(Some(Duration::from_secs(30)));
} }
async fn not_found(_uri: Uri) -> impl IntoResponse { async fn not_found(uri: Uri) -> impl IntoResponse {
warn!("Not found: {uri}");
Error::BadRequest(ErrorKind::Unrecognized, "Unrecognized request") Error::BadRequest(ErrorKind::Unrecognized, "Unrecognized request")
} }
async fn initial_sync(_uri: Uri) -> impl IntoResponse { async fn initial_sync(_uri: Uri) -> impl IntoResponse {
Error::BadRequest(ErrorKind::GuestAccessForbidden, "Guest access not implemented") Error::BadRequest(
ErrorKind::GuestAccessForbidden,
"Guest access not implemented",
)
} }
trait RouterExt { trait RouterExt {

View file

@ -131,7 +131,7 @@ impl Service {
Err(e) => { Err(e) => {
warn!("Could not send request to pusher {}: {}", destination, e); warn!("Could not send request to pusher {}: {}", destination, e);
Err(e.into()) Err(e.into())
}, }
} }
} }