mirror of
https://gitlab.com/famedly/conduit.git
synced 2025-08-06 17:40:59 +00:00
fix: Respond with HTTP code 413, when request size is too big
This commit is contained in:
parent
a8fa237fad
commit
a87f4b6171
4 changed files with 19 additions and 13 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
@ -510,6 +510,7 @@ dependencies = [
|
|||
"hickory-resolver",
|
||||
"hmac",
|
||||
"http",
|
||||
"http-body-util",
|
||||
"humantime",
|
||||
"humantime-serde",
|
||||
"hyper",
|
||||
|
|
|
@ -152,6 +152,8 @@ 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
|
||||
[dependencies.ruma]
|
||||
|
|
|
@ -1,10 +1,10 @@
|
|||
use std::{collections::BTreeMap, iter::FromIterator, str};
|
||||
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},
|
||||
|
@ -48,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()
|
||||
|
@ -59,7 +58,17 @@ where
|
|||
.unwrap_or(usize::MAX),
|
||||
)
|
||||
.await
|
||||
.map_err(|_| Error::BadRequest(ErrorKind::MissingToken, "Missing token."))?;
|
||||
.map_err(|err| {
|
||||
if err
|
||||
.source()
|
||||
.is_some_and(|err| err.is::<http_body_util::LengthLimitError>())
|
||||
{
|
||||
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")
|
||||
}
|
||||
})?;
|
||||
(parts, body)
|
||||
};
|
||||
|
||||
|
|
10
src/main.rs
10
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();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue