0008-api-keep-the-values-of-a-refused-body-or-query-out-o.patch (6,988 bytes)
From 94fc2be776cd5f02f1d4fdaa6b814dffa9cbbf4b Mon Sep 17 00:00:00 2001
From: Christian Grothoff <christian@grothoff.org>
Date: Wed, 18 Nov 2026 14:55:05 +0100
Subject: [PATCH 8/9] api: keep the values of a refused body or query out of
the log
When a JSON body or a query string does not deserialize, the error's hint
is serde's message, which quotes the value that failed ("invalid type:
string "hunter2", expected u32"). The request logger wrote that hint at
WARN, so passwords and personal data sent with the wrong type ended up in
the log. The logged line now names the error code, the path and the kind
of error, such as "pin: invalid type"; the client still gets the whole
message as the hint. json_error_kind() gives the kind to callers that
parse JSON themselves.
Assisted by LLM
---
common/taler-api/src/api.rs | 66 +++++++++++++++++++++++++++++++++++
common/taler-api/src/error.rs | 58 ++++++++++++++++++++++++------
2 files changed, 113 insertions(+), 11 deletions(-)
diff --git a/common/taler-api/src/api.rs b/common/taler-api/src/api.rs
index 789841b..e7ba351 100644
--- a/common/taler-api/src/api.rs
+++ b/common/taler-api/src/api.rs
@@ -528,4 +528,70 @@ mod tests {
assert!(log.contains(line), "no {line} in the log:\n{log}");
}
}
+
+ /// The log line of a refused body or query names the code, the path and
+ /// the kind of error, but not the value, which only the client sees.
+ #[tokio::test]
+ async fn logger_writes_no_values_of_a_bad_request() {
+ #[derive(serde::Deserialize)]
+ struct Login {
+ #[allow(dead_code)]
+ pin: u32,
+ }
+ #[derive(serde::Deserialize)]
+ #[serde(rename_all = "lowercase")]
+ enum Kind {
+ Plain,
+ }
+ #[derive(serde::Deserialize)]
+ struct Search {
+ #[allow(dead_code)]
+ kind: Kind,
+ }
+ let (captured, _guard) = capture();
+ let router = Router::new()
+ .route(
+ "/login",
+ axum::routing::post(
+ async |crate::extract::Req(_): crate::extract::Req<Login>| "ok",
+ ),
+ )
+ .route(
+ "/search",
+ get(async |crate::extract::Query(_): crate::extract::Query<Search>| "ok"),
+ )
+ .finalize();
+
+ let login = Request::builder()
+ .method(Method::POST)
+ .uri("/login")
+ .header("content-type", "application/json")
+ .body(Body::from(r#"{"pin": "hunter2-secret"}"#))
+ .unwrap();
+ let r = router.clone().oneshot(login).await.unwrap();
+ assert_eq!(r.status(), StatusCode::BAD_REQUEST);
+ let body = axum::body::to_bytes(r.into_body(), 4096).await.unwrap();
+ let body = String::from_utf8(body.to_vec()).unwrap();
+ assert!(body.contains("hunter2-secret"), "the client's hint: {body}");
+ let r = request(&router, Method::GET, "/search?kind=search-secret", &[]).await;
+ assert_eq!(r.status(), StatusCode::BAD_REQUEST);
+
+ // The request body is logged at TRACE only.
+ let log = captured.text();
+ let warnings: Vec<&str> = log.lines().filter(|l| l.contains("WARN")).collect();
+ assert_eq!(warnings.len(), 2, "{log}");
+ for line in &warnings {
+ assert!(!line.contains("secret"), "a value in the log:\n{line}");
+ }
+ assert!(
+ warnings[0].contains("POST /login") && warnings[0].contains("pin: invalid type"),
+ "{}",
+ warnings[0]
+ );
+ assert!(
+ warnings[1].contains("GET /search") && warnings[1].contains("kind: unknown variant"),
+ "{}",
+ warnings[1]
+ );
+ }
}
diff --git a/common/taler-api/src/error.rs b/common/taler-api/src/error.rs
index 50bed41..191e476 100644
--- a/common/taler-api/src/error.rs
+++ b/common/taler-api/src/error.rs
@@ -265,26 +265,62 @@ impl From<ParamsErr> for ApiError {
}
}
+/// The kind of error at the start of serde's own messages, such as
+/// `invalid type: string "hunter2", expected u32`.
+fn serde_error_kind(message: &str) -> Option<&'static str> {
+ const KINDS: [&str; 7] = [
+ "invalid type",
+ "invalid value",
+ "invalid length",
+ "unknown variant",
+ "unknown field",
+ "missing field",
+ "duplicate field",
+ ];
+ KINDS.into_iter().find(|kind| message.starts_with(kind))
+}
+
+/// The kind of error that serde_json found, for the log. The message itself
+/// goes to the client only: it may quote the value that failed, and request
+/// bodies hold passwords and personal data.
+pub fn json_error_kind(e: &serde_json::Error) -> &'static str {
+ use serde_json::error::Category;
+ match e.classify() {
+ Category::Io => "unreadable input",
+ Category::Syntax => "syntax error",
+ Category::Eof => "unexpected end of input",
+ Category::Data => serde_error_kind(&e.to_string()).unwrap_or("invalid data"),
+ }
+}
+
+/// The log names the parameter and the kind of error, not the value: a
+/// query can hold personal data, such as a search term.
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)
+ let path = value.path().to_string();
+ let message = value.inner().to_string();
+ let log = format!(
+ "{path}: {}",
+ serde_error_kind(&message).unwrap_or("invalid parameter")
+ );
+ let code = if message.contains("missing") {
+ 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
+ };
+ failure(code, message).with_path(path).with_log(log)
}
}
+/// The log names the path and the kind of error, not serde_json's message
+/// (see [`json_error_kind`]); the client gets the message as the hint.
impl From<serde_path_to_error::Error<serde_json::Error>> for ApiError {
fn from(value: serde_path_to_error::Error<serde_json::Error>) -> Self {
+ let path = value.path().to_string();
+ let log = format!("{path}: {}", json_error_kind(value.inner()));
failure(ErrorCode::GENERIC_JSON_INVALID, value.inner())
- .with_path(value.path().to_string())
- .with_log(value.to_string())
+ .with_path(path)
+ .with_log(log)
}
}
--
2.47.3