taler-rust

GNU Taler code in Rust. Largely core banking integrations.
Log | Files | Refs | Submodules | README | LICENSE

commit 6327747249839969be3d420a6fddee35a005eaa9
parent ed4278906bc774f4899a497a2ee97ea15d468781
Author: Antoine A <>
Date:   Thu, 19 Nov 2026 13:01:44 +0100

taler-api: fix JSON content type parsing

Diffstat:
Mcommon/taler-api/src/extract.rs | 18++++++++++++++----
Mcommon/taler-api/src/test.rs | 33++++++++++++++++++++++++---------
2 files changed, 38 insertions(+), 13 deletions(-)

diff --git a/common/taler-api/src/extract.rs b/common/taler-api/src/extract.rs @@ -54,10 +54,20 @@ pub async fn decompressed_strict_body<const MAX_SIZE: usize>( ) }; - match headers.get(header::CONTENT_TYPE) { - Some(value) if value.as_bytes() == b"application/json" => {} - Some(_) => return Err(unsupported_media_type("Bad Content-Type header")), - None => return Err(unsupported_media_type("Missing Content-Type header")), + let Some(content_type) = headers.get(CONTENT_TYPE) else { + return Err(unsupported_media_type("Missing Content-Type header")); + }; + // JSON has no media type parameters that affect how we decode it. + let media_type = content_type + .as_bytes() + .split(|&byte| byte == b';') + .next() + .unwrap(); + if !media_type + .trim_ascii() + .eq_ignore_ascii_case(b"application/json") + { + return Err(unsupported_media_type("Bad Content-Type header")); } // Check content length if present and well formed diff --git a/common/taler-api/src/test.rs b/common/taler-api/src/test.rs @@ -118,6 +118,19 @@ async fn body_parsing(db: PgPool) { .await .assert_ok_json::<TransferResponse>(); + for content_type in [ + "application/json; charset=utf-8", + "Application/JSON", + "Application/JSON \t; charset=\"UTF-8\"", + ] { + server + .post("/taler-wire-gateway/transfer") + .json(&normal_body) + .header(header::CONTENT_TYPE, content_type) + .await + .assert_ok_json::<TransferResponse>(); + } + // Headers check server .post("/taler-wire-gateway/transfer") @@ -135,15 +148,17 @@ async fn body_parsing(db: PgPool) { .remove(header::CONTENT_ENCODING) .await .assert_error(ErrorCode::GENERIC_JSON_INVALID); - server - .post("/taler-wire-gateway/transfer") - .json(&normal_body) - .header(header::CONTENT_TYPE, "invalid") - .await - .assert_error_status( - ErrorCode::GENERIC_HTTP_HEADERS_MALFORMED, - StatusCode::UNSUPPORTED_MEDIA_TYPE, - ); + for content_type in ["invalid", "application/json-seq; charset=utf-8"] { + server + .post("/taler-wire-gateway/transfer") + .json(&normal_body) + .header(header::CONTENT_TYPE, content_type) + .await + .assert_error_status( + ErrorCode::GENERIC_HTTP_HEADERS_MALFORMED, + StatusCode::UNSUPPORTED_MEDIA_TYPE, + ); + } server .post("/taler-wire-gateway/transfer") .json(&normal_body)