taler-rust

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

commit 8ce31d91f46d6e3d8613c9c18409fc2151abd59d
parent ee63c16c619036511fe755ab267180fd516ba495
Author: Antoine A <>
Date:   Thu, 19 Nov 2026 12:26:58 +0100

taler-api: keep the values of a refused body or query out of the log

Diffstat:
Mcommon/taler-api/src/error.rs | 90+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------------
Mcommon/taler-api/src/extract.rs | 3+--
Mcommon/taler-common/src/api/params.rs | 2+-
3 files changed, 74 insertions(+), 21 deletions(-)

diff --git a/common/taler-api/src/error.rs b/common/taler-api/src/error.rs @@ -23,11 +23,13 @@ use axum::{ response::{IntoResponse, Response}, }; use serde_json::value::RawValue; +use serde_path_to_error::Error as DeserializeError; use taler_common::{ api::{ErrorDetail, params::ParamsErr}, error_code::ErrorCode, types::payto::PaytoErr, }; +use tracing::debug; pub type ApiResult<T> = Result<T, ApiError>; @@ -146,32 +148,75 @@ impl From<ParamsErr> for ApiError { } } -impl From<serde_path_to_error::Error<serde_urlencoded::de::Error>> for ApiError { - fn from(value: serde_path_to_error::Error<serde_urlencoded::de::Error>) -> Self { - let fmt = value.to_string(); - if fmt.contains("missing") { - failure(ErrorCode::GENERIC_PARAMETER_MISSING, value.inner()) - .with_path(value.path().to_string()) - .with_log(fmt) +// Serde data errors can contain arbitrary input, including in custom messages. +// Only return fixed labels, never a substring of the message. +fn serde_error_kind(message: &str) -> &'static str { + [ + "invalid type", + "invalid value", + "invalid length", + "unknown variant", + "unknown field", + "missing field", + "duplicate field", + "invalid digit", + ] + .into_iter() + .find(|kind| message.starts_with(kind)) + .unwrap_or("invalid data") +} + +/// Describe a JSON deserialization error for logging without quoting the input. +fn json_error_kind(error: &serde_json::Error) -> &'static str { + use serde_json::error::Category; + + match error.classify() { + Category::Io => "I/O error", + Category::Syntax => "syntax error", + Category::Eof => "unexpected end of input", + Category::Data => serde_error_kind(&error.to_string()), + } +} + +fn deserialization_failure( + code: ErrorCode, + hint: impl Display, + path: &serde_path_to_error::Path, + kind: &'static str, +) -> ApiError { + failure(code, hint) + .with_path(path) + .with_log(format!("{path}: {kind}")) +} + +impl From<DeserializeError<serde_urlencoded::de::Error>> for ApiError { + fn from(error: DeserializeError<serde_urlencoded::de::Error>) -> Self { + let hint = error.inner().to_string(); + let kind = serde_error_kind(&hint); + let code = if kind == "missing field" { + ErrorCode::GENERIC_PARAMETER_MISSING } else { - failure(ErrorCode::GENERIC_PARAMETER_MALFORMED, value.inner()) - .with_path(value.path().to_string()) - .with_log(fmt) - } + ErrorCode::GENERIC_PARAMETER_MALFORMED + }; + deserialization_failure(code, hint, error.path(), kind) } } -impl From<serde_path_to_error::Error<serde_json::Error>> for ApiError { - fn from(value: serde_path_to_error::Error<serde_json::Error>) -> Self { - failure(ErrorCode::GENERIC_JSON_INVALID, value.inner()) - .with_path(value.path().to_string()) - .with_log(value.to_string()) +impl From<DeserializeError<serde_json::Error>> for ApiError { + fn from(error: DeserializeError<serde_json::Error>) -> Self { + let source = error.inner(); + deserialization_failure( + ErrorCode::GENERIC_JSON_INVALID, + source, + error.path(), + json_error_kind(source), + ) } } impl From<serde_json::Error> for ApiError { fn from(value: serde_json::Error) -> Self { - failure(ErrorCode::GENERIC_JSON_INVALID, &value).with_log(value.to_string()) + failure(ErrorCode::GENERIC_JSON_INVALID, &value).with_log(json_error_kind(&value)) } } @@ -211,7 +256,16 @@ impl IntoResponse for ApiError { let status_code = status.unwrap_or_else(|| { StatusCode::from_u16(code.status_code()).expect("Invalid status code") }); - let log = log.or(hint.clone()); + + let log = if let Some(log) = log { + // Hint contains sensitive data that is skipped in logs, reveal full hint if debugging + if let Some(hint) = &hint { + debug!(target: "api", "{code} {hint}"); + } + Some(log) + } else { + hint.clone() + }; let mut resp = ( status_code, diff --git a/common/taler-api/src/extract.rs b/common/taler-api/src/extract.rs @@ -181,8 +181,7 @@ impl<T: DeserializeOwned, const MAX_SIZE: usize> TryFrom<&Bytes> for Req<T, MAX_ fn try_from(value: &Bytes) -> Result<Self, Self::Error> { let mut de = serde_json::de::Deserializer::from_slice(value); let parsed = serde_path_to_error::deserialize(&mut de)?; - de.end() - .map_err(|err| failure(ErrorCode::GENERIC_JSON_INVALID, err))?; + de.end()?; Ok(Req(parsed)) } } diff --git a/common/taler-common/src/api/params.rs b/common/taler-common/src/api/params.rs @@ -56,7 +56,7 @@ impl PageParams { } else if limit.unsigned_abs() > max_page_size { return Err(param_err( "limit", - format!("must be <= {max_page_size} for {limit}"), + format!("must be <= {max_page_size} got {limit}"), )); } if let Some(offset) = self.offset