0003-api-make-the-request-body-limit-configurable-per-rou.patch (6,997 bytes)
From 480b6104f9ab3bf4ff5a1fbe2f27e69c5c41c7a5 Mon Sep 17 00:00:00 2001
From: Christian Grothoff <christian@grothoff.org>
Date: Tue, 29 Sep 2026 13:49:29 +0200
Subject: [PATCH 3/9] api: make the request body limit configurable per router
The fixed 4 KiB suits the wire gateway, but a service that takes larger JSON documents (form definitions of up to 1 MiB) could not use the extractors; the decompression buffer also moves from the stack to the heap.
---
common/taler-api/src/extract.rs | 102 +++++++++++++++++++++++++++++---
1 file changed, 94 insertions(+), 8 deletions(-)
diff --git a/common/taler-api/src/extract.rs b/common/taler-api/src/extract.rs
index 6811672..5a6f22d 100644
--- a/common/taler-api/src/extract.rs
+++ b/common/taler-api/src/extract.rs
@@ -35,7 +35,28 @@ use crate::{
error::{ApiError, ApiResult, failure, failure_status},
};
+/// The largest request body, before and after decompression, that [`Req`] and
+/// [`OptReq`] accept; [`MAX_BODY_LENGTH`] unless set for a router with
+/// `.route_layer(axum::Extension(BodyLimit(n)))`.
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+pub struct BodyLimit(pub usize);
+
+fn body_limit(parts: &Parts) -> usize {
+ parts
+ .extensions
+ .get::<BodyLimit>()
+ .map_or(MAX_BODY_LENGTH, |l| l.0)
+}
+
pub async fn decompressed_strict_body(headers: &HeaderMap, body: Body) -> ApiResult<Bytes> {
+ decompressed_strict_body_limited(headers, body, MAX_BODY_LENGTH).await
+}
+
+pub async fn decompressed_strict_body_limited(
+ headers: &HeaderMap,
+ body: Body,
+ limit: usize,
+) -> ApiResult<Bytes> {
// Check content type
match headers.get(header::CONTENT_TYPE) {
Some(header) => {
@@ -61,11 +82,11 @@ pub async fn decompressed_strict_body(headers: &HeaderMap, body: Body) -> ApiRes
.get(header::CONTENT_LENGTH)
.and_then(|it| it.to_str().ok())
.and_then(|it| it.parse::<usize>().ok())
- && length > MAX_BODY_LENGTH
+ && length > limit
{
return Err(failure(
ErrorCode::GENERIC_UPLOAD_EXCEEDS_LIMIT,
- format!("Body is suspiciously big > {MAX_BODY_LENGTH}B"),
+ format!("Body is suspiciously big > {limit}B"),
));
}
@@ -88,14 +109,14 @@ pub async fn decompressed_strict_body(headers: &HeaderMap, body: Body) -> ApiRes
};
// Buffer body
- let body = http_body_util::Limited::new(body, MAX_BODY_LENGTH);
+ let body = http_body_util::Limited::new(body, limit);
let bytes = match body.collect().await {
Ok(chunks) => chunks.to_bytes(),
Err(it) => match it.downcast::<http_body_util::LengthLimitError>() {
Ok(_) => {
return Err(failure(
ErrorCode::GENERIC_UPLOAD_EXCEEDS_LIMIT,
- format!("Body is suspiciously big > {MAX_BODY_LENGTH}B"),
+ format!("Body is suspiciously big > {limit}B"),
));
}
Err(err) => {
@@ -108,7 +129,7 @@ pub async fn decompressed_strict_body(headers: &HeaderMap, body: Body) -> ApiRes
};
let bytes = if compressed {
- let mut buf = [0; MAX_BODY_LENGTH];
+ let mut buf = vec![0; limit];
let (decompressed, code) =
zlib_rs::decompress_slice(&mut buf, &bytes, InflateConfig::default());
match code {
@@ -116,7 +137,7 @@ pub async fn decompressed_strict_body(headers: &HeaderMap, body: Body) -> ApiRes
ReturnCode::BufError => {
return Err(failure(
ErrorCode::GENERIC_UPLOAD_EXCEEDS_LIMIT,
- format!("Decompressed body is suspiciously big > {MAX_BODY_LENGTH}B"),
+ format!("Decompressed body is suspiciously big > {limit}B"),
));
}
_ => {
@@ -146,7 +167,8 @@ where
async fn from_request(req: Request, _state: &S) -> Result<Self, Self::Rejection> {
let (parts, body) = req.into_parts();
- let bytes = decompressed_strict_body(&parts.headers, body).await?;
+ let limit = body_limit(&parts);
+ let bytes = decompressed_strict_body_limited(&parts.headers, body, limit).await?;
Self::try_from(&bytes)
}
}
@@ -177,7 +199,8 @@ where
if !parts.headers.contains_key(CONTENT_TYPE) && body.size_hint().exact() == Some(0) {
Ok(Self(None))
} else {
- let bytes = decompressed_strict_body(&parts.headers, body).await?;
+ let limit = body_limit(&parts);
+ let bytes = decompressed_strict_body_limited(&parts.headers, body, limit).await?;
let req = Req::try_from(&bytes)?;
Ok(Self(Some(req.0)))
}
@@ -211,3 +234,66 @@ impl<T: serde::de::DeserializeOwned, S: Sync + Send> FromRequestParts<S> for Que
Ok(Query(params))
}
}
+
+#[cfg(test)]
+mod tests {
+ use axum::http::Request;
+
+ use super::*;
+
+ fn request(len: usize, limit: Option<usize>) -> Request<Body> {
+ let json = format!("\"{}\"", "x".repeat(len - 2));
+ let mut req = Request::builder()
+ .header(CONTENT_TYPE, "application/json")
+ .body(Body::from(json))
+ .unwrap();
+ if let Some(limit) = limit {
+ req.extensions_mut().insert(BodyLimit(limit));
+ }
+ req
+ }
+
+ #[tokio::test]
+ async fn body_limit() {
+ let big = MAX_BODY_LENGTH + 1;
+ assert!(
+ Req::<String>::from_request(request(big, None), &())
+ .await
+ .is_err()
+ );
+ let Req(s) = Req::<String>::from_request(request(big, Some(big)), &())
+ .await
+ .unwrap();
+ assert_eq!(s.len(), big - 2);
+ assert!(
+ Req::<String>::from_request(request(big + 1, Some(big)), &())
+ .await
+ .is_err()
+ );
+
+ // The limit also applies after decompression.
+ let json = format!("\"{}\"", "x".repeat(big));
+ let compressed = {
+ let mut out = vec![0; big * 2];
+ let (c, code) = zlib_rs::compress_slice(
+ &mut out,
+ json.as_bytes(),
+ zlib_rs::DeflateConfig::default(),
+ );
+ assert_eq!(code, ReturnCode::Ok);
+ c.to_vec()
+ };
+ assert!(compressed.len() < MAX_BODY_LENGTH);
+ let req = |limit: usize| {
+ let mut req = Request::builder()
+ .header(CONTENT_TYPE, "application/json")
+ .header(header::CONTENT_ENCODING, "deflate")
+ .body(Body::from(compressed.clone()))
+ .unwrap();
+ req.extensions_mut().insert(BodyLimit(limit));
+ req
+ };
+ assert!(Req::<String>::from_request(req(big), &()).await.is_err());
+ assert!(Req::<String>::from_request(req(big + 2), &()).await.is_ok());
+ }
+}
--
2.47.3