From 617ea3614e30540b0ca24536cf84057a20cd6658 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Pie=C5=84kowski?= <4557247-Jakski@users.noreply.gitlab.com> Date: Thu, 3 Jul 2025 19:23:03 +0000 Subject: [PATCH 1/5] fix: Respond with HTTP code 413, when request size is too big --- Cargo.lock | 1 + Cargo.toml | 1 + src/api/ruma_wrapper/axum.rs | 10 +++++++++- src/utils/error.rs | 1 + 4 files changed, 12 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index 6bb2699d..2a48d656 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -510,6 +510,7 @@ dependencies = [ "hickory-resolver", "hmac", "http", + "http-body-util", "humantime", "humantime-serde", "hyper", diff --git a/Cargo.toml b/Cargo.toml index 557b155b..cba97538 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -152,6 +152,7 @@ tikv-jemallocator = { version = "0.6", features = [ ], optional = true } sd-notify = { version = "0.4", optional = true } +http-body-util = "0.1.3" # Used for matrix spec type definitions and helpers [dependencies.ruma] diff --git a/src/api/ruma_wrapper/axum.rs b/src/api/ruma_wrapper/axum.rs index f933796e..5fa7cd29 100644 --- a/src/api/ruma_wrapper/axum.rs +++ b/src/api/ruma_wrapper/axum.rs @@ -59,7 +59,15 @@ where .unwrap_or(usize::MAX), ) .await - .map_err(|_| Error::BadRequest(ErrorKind::MissingToken, "Missing token."))?; + .map_err(|err| { + if std::error::Error::source(&err).as_ref().and_then(|err| std::error::Error::source(err)).is_some() { + Error::BadRequest( + ErrorKind::ResourceLimitExceeded{ admin_contact: String::default() }, + "Reached maximum request size") + } else { + Error::BadRequest(ErrorKind::Unknown, "An unknown error has occurred") + } + })?; (parts, body) }; diff --git a/src/utils/error.rs b/src/utils/error.rs index 1b1a26db..943df398 100644 --- a/src/utils/error.rs +++ b/src/utils/error.rs @@ -123,6 +123,7 @@ impl Error { Unauthorized | UnknownToken { .. } | MissingToken => StatusCode::UNAUTHORIZED, NotFound | Unrecognized => StatusCode::NOT_FOUND, LimitExceeded { .. } => StatusCode::TOO_MANY_REQUESTS, + ResourceLimitExceeded { .. } => StatusCode::PAYLOAD_TOO_LARGE, UserDeactivated => StatusCode::FORBIDDEN, TooLarge => StatusCode::PAYLOAD_TOO_LARGE, ConnectionTimeout => StatusCode::GATEWAY_TIMEOUT, From 8f3959b3f302719b179ec50ea3ccb558e8c066b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Pie=C5=84kowski?= <4557247-Jakski@users.noreply.gitlab.com> Date: Thu, 3 Jul 2025 22:55:22 +0000 Subject: [PATCH 2/5] fix: Respond with HTTP code 413, when request size is too big --- src/api/ruma_wrapper/axum.rs | 4 +--- src/utils/error.rs | 1 - 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/src/api/ruma_wrapper/axum.rs b/src/api/ruma_wrapper/axum.rs index 5fa7cd29..f061856f 100644 --- a/src/api/ruma_wrapper/axum.rs +++ b/src/api/ruma_wrapper/axum.rs @@ -61,9 +61,7 @@ where .await .map_err(|err| { if std::error::Error::source(&err).as_ref().and_then(|err| std::error::Error::source(err)).is_some() { - Error::BadRequest( - ErrorKind::ResourceLimitExceeded{ admin_contact: String::default() }, - "Reached maximum request size") + Error::BadRequest(ErrorKind::TooLarge, "Reached maximum request size") } else { Error::BadRequest(ErrorKind::Unknown, "An unknown error has occurred") } diff --git a/src/utils/error.rs b/src/utils/error.rs index 943df398..1b1a26db 100644 --- a/src/utils/error.rs +++ b/src/utils/error.rs @@ -123,7 +123,6 @@ impl Error { Unauthorized | UnknownToken { .. } | MissingToken => StatusCode::UNAUTHORIZED, NotFound | Unrecognized => StatusCode::NOT_FOUND, LimitExceeded { .. } => StatusCode::TOO_MANY_REQUESTS, - ResourceLimitExceeded { .. } => StatusCode::PAYLOAD_TOO_LARGE, UserDeactivated => StatusCode::FORBIDDEN, TooLarge => StatusCode::PAYLOAD_TOO_LARGE, ConnectionTimeout => StatusCode::GATEWAY_TIMEOUT, From c23c15e5126bd942c4aba6a35d99e4b705b53b83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Pie=C5=84kowski?= <4557247-Jakski@users.noreply.gitlab.com> Date: Thu, 3 Jul 2025 23:07:57 +0000 Subject: [PATCH 3/5] fix: Respond with HTTP code 413, when request size is too big --- src/api/ruma_wrapper/axum.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/api/ruma_wrapper/axum.rs b/src/api/ruma_wrapper/axum.rs index f061856f..73a75b3d 100644 --- a/src/api/ruma_wrapper/axum.rs +++ b/src/api/ruma_wrapper/axum.rs @@ -60,7 +60,11 @@ where ) .await .map_err(|err| { - if std::error::Error::source(&err).as_ref().and_then(|err| std::error::Error::source(err)).is_some() { + if std::error::Error::source(&err) + .as_ref() + .and_then(|err| std::error::Error::source(err)) + .is_some_and(|err| err.is::()) + { Error::BadRequest(ErrorKind::TooLarge, "Reached maximum request size") } else { Error::BadRequest(ErrorKind::Unknown, "An unknown error has occurred") From 1b32cfe955b4675d884663d0795a7178985b37d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Pie=C5=84kowski?= <4557247-Jakski@users.noreply.gitlab.com> Date: Fri, 4 Jul 2025 08:51:15 +0000 Subject: [PATCH 4/5] fix: Respond with HTTP code 413, when request size is too big --- Cargo.toml | 1 + src/api/ruma_wrapper/axum.rs | 5 +++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index cba97538..5455ad79 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -152,6 +152,7 @@ tikv-jemallocator = { version = "0.6", features = [ ], optional = true } sd-notify = { version = "0.4", optional = true } +# Used for inspecting request errors http-body-util = "0.1.3" # Used for matrix spec type definitions and helpers diff --git a/src/api/ruma_wrapper/axum.rs b/src/api/ruma_wrapper/axum.rs index 73a75b3d..2eb2c7f8 100644 --- a/src/api/ruma_wrapper/axum.rs +++ b/src/api/ruma_wrapper/axum.rs @@ -1,4 +1,5 @@ use std::{collections::BTreeMap, iter::FromIterator, str}; +use std::error::Error as _; use axum::{ body::Body, @@ -60,9 +61,9 @@ where ) .await .map_err(|err| { - if std::error::Error::source(&err) + if err.source() .as_ref() - .and_then(|err| std::error::Error::source(err)) + .and_then(|err| err.source()) .is_some_and(|err| err.is::()) { Error::BadRequest(ErrorKind::TooLarge, "Reached maximum request size") From 0772a7c15182a90a77f9693fc99cf4940abbdfa7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Pie=C5=84kowski?= <4557247-Jakski@users.noreply.gitlab.com> Date: Fri, 4 Jul 2025 14:10:34 +0000 Subject: [PATCH 5/5] fix: Respond with HTTP code 413, when request size is too big --- src/api/ruma_wrapper/axum.rs | 14 ++++++-------- src/main.rs | 10 ++-------- 2 files changed, 8 insertions(+), 16 deletions(-) diff --git a/src/api/ruma_wrapper/axum.rs b/src/api/ruma_wrapper/axum.rs index 2eb2c7f8..04456543 100644 --- a/src/api/ruma_wrapper/axum.rs +++ b/src/api/ruma_wrapper/axum.rs @@ -1,11 +1,10 @@ -use std::{collections::BTreeMap, iter::FromIterator, str}; -use std::error::Error as _; +use std::{collections::BTreeMap, error::Error as _, iter::FromIterator, str}; use axum::{ body::Body, extract::{FromRequest, Path}, response::{IntoResponse, Response}, - RequestExt, RequestPartsExt, + RequestPartsExt, }; use axum_extra::{ headers::{authorization::Bearer, Authorization}, @@ -49,8 +48,7 @@ where } let (mut parts, mut body) = { - let limited_req = req.with_limited_body(); - let (parts, body) = limited_req.into_parts(); + let (parts, body) = req.into_parts(); let body = axum::body::to_bytes( body, services() @@ -61,13 +59,13 @@ where ) .await .map_err(|err| { - if err.source() - .as_ref() - .and_then(|err| err.source()) + if err + .source() .is_some_and(|err| err.is::()) { Error::BadRequest(ErrorKind::TooLarge, "Reached maximum request size") } else { + error!("An unknown error has occurred: {err}"); Error::BadRequest(ErrorKind::Unknown, "An unknown error has occurred") } })?; diff --git a/src/main.rs b/src/main.rs index 4af1162c..b9e34765 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,7 +2,7 @@ use std::{future::Future, io, net::SocketAddr, sync::atomic, time::Duration}; use axum::{ body::Body, - extract::{DefaultBodyLimit, FromRequestParts, MatchedPath}, + extract::{FromRequestParts, MatchedPath}, middleware::map_response, response::{IntoResponse, Response}, routing::{any, get, on, MethodFilter}, @@ -240,13 +240,7 @@ async fn run_server() -> io::Result<()> { ]) .max_age(Duration::from_secs(86400)), ) - .layer(map_response(set_csp_header)) - .layer(DefaultBodyLimit::max( - config - .max_request_size - .try_into() - .expect("failed to convert max request size"), - )); + .layer(map_response(set_csp_header)); let app = routes(config).layer(middlewares).into_make_service(); let handle = ServerHandle::new();