0005-api-log-the-route-not-the-path-and-query.patch (7,302 bytes)
From 1df49fce6c793dd33cfddedeb61256288974252a Mon Sep 17 00:00:00 2001
From: Christian Grothoff <christian@grothoff.org>
Date: Wed, 18 Nov 2026 10:44:34 +0100
Subject: [PATCH 5/9] api: log the route, not the path and query
The request logger wrote the whole path and query at every level, so bearer
secrets in a path (an OAuth2 nonce) and personal data in a query (a search
for an e-mail address) ended up in the journal.
Assisted by LLM
---
common/taler-api/Cargo.toml | 2 +
common/taler-api/src/api.rs | 130 +++++++++++++++++++++++++++++++++---
2 files changed, 124 insertions(+), 8 deletions(-)
diff --git a/common/taler-api/Cargo.toml b/common/taler-api/Cargo.toml
index 10538a0..2ea77e4 100644
--- a/common/taler-api/Cargo.toml
+++ b/common/taler-api/Cargo.toml
@@ -39,6 +39,8 @@ prometheus-client.workspace = true
taler-test-utils.workspace = true
criterion.workspace = true
rand.workspace = true
+tower = { version = "0.5", features = ["util"] }
+tracing-subscriber.workspace = true
[[bench]]
name = "subject"
diff --git a/common/taler-api/src/api.rs b/common/taler-api/src/api.rs
index 1f59e04..38617aa 100644
--- a/common/taler-api/src/api.rs
+++ b/common/taler-api/src/api.rs
@@ -23,7 +23,7 @@ use std::{
};
use axum::{
- extract::{Request, State},
+ extract::{MatchedPath, Request, State},
middleware::{self, Next},
response::Response,
};
@@ -234,6 +234,9 @@ macro_rules! dyn_event {
};
}
+/// What the logger writes in place of the route when no route matched.
+const UNMATCHED_ROUTE: &str = "-";
+
/** Taler API logger */
async fn logger_middleware(request: Request, next: Next) -> Response {
let now = Instant::now();
@@ -249,14 +252,17 @@ async fn logger_middleware(request: Request, next: Next) -> Response {
unsafe { CompactString::from_utf8_unchecked(ansi) }
};
let method = request.method().clone();
- let path_and_query = request.uri().path_and_query().cloned();
- let path_and_query = path_and_query
+ // The route's template, never the path and query themselves: those can
+ // hold bearer secrets (a nonce in the path, a token in the query) and
+ // personal data (a search term), which do not belong in a log.
+ let route = request.extensions().get::<MatchedPath>().cloned();
+ let route = route
.as_ref()
- .map(|it| it.as_str())
- .unwrap_or_default();
+ .map(MatchedPath::as_str)
+ .unwrap_or(UNMATCHED_ROUTE);
LOG_TASK_ID
.scope(request_id, async {
- debug!(target: "api", "{method} {path_and_query}");
+ debug!(target: "api", "{method} {route}");
let response = next.run(request).await;
let elapsed = now.elapsed();
let status = response.status();
@@ -269,7 +275,7 @@ async fn logger_middleware(request: Request, next: Next) -> Response {
if let Some(log) = response.extensions().get::<LoggedError>() {
let LoggedError { code, info } = log;
dyn_event!(level, target: "api",
- "{} {method} {path_and_query} {}ms: {code}{}",
+ "{} {method} {route} {}ms: {code}{}",
response.status(),
elapsed.as_millis(),
std::fmt::from_fn(|f|{
@@ -281,7 +287,7 @@ async fn logger_middleware(request: Request, next: Next) -> Response {
);
} else {
dyn_event!(level, target: "api",
- "{} {method} {path_and_query} {}ms",
+ "{} {method} {route} {}ms",
response.status(),
elapsed.as_millis()
);
@@ -290,3 +296,111 @@ async fn logger_middleware(request: Request, next: Next) -> Response {
})
.await
}
+
+#[cfg(test)]
+mod tests {
+ use std::sync::{Arc, Mutex};
+
+ use axum::{
+ body::Body,
+ http::{Method, Request, StatusCode},
+ routing::get,
+ };
+ use tower::ServiceExt as _;
+
+ use super::*;
+
+ /// Collects what the subscriber writes.
+ #[derive(Clone, Default)]
+ struct Captured(Arc<Mutex<Vec<u8>>>);
+
+ impl Captured {
+ fn text(&self) -> String {
+ String::from_utf8(self.0.lock().unwrap().clone()).unwrap()
+ }
+ }
+
+ impl std::io::Write for Captured {
+ fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
+ self.0.lock().unwrap().extend_from_slice(buf);
+ Ok(buf.len())
+ }
+
+ fn flush(&mut self) -> std::io::Result<()> {
+ Ok(())
+ }
+ }
+
+ impl tracing_subscriber::fmt::MakeWriter<'_> for Captured {
+ type Writer = Self;
+
+ fn make_writer(&self) -> Self {
+ self.clone()
+ }
+ }
+
+ /// Sends this thread's log messages, of every level, to a buffer. A test
+ /// that sends requests installs one before the first: tracing caches
+ /// whether a log statement is of interest, and a statement first reached
+ /// on a thread without a subscriber may stay silent for the others.
+ fn capture() -> (Captured, tracing::subscriber::DefaultGuard) {
+ let captured = Captured::default();
+ let subscriber = tracing_subscriber::fmt()
+ .with_max_level(Level::TRACE)
+ .with_ansi(false)
+ .with_writer(captured.clone())
+ .finish();
+ // The test runs on one thread, so the subscriber sees every request.
+ let guard = tracing::subscriber::set_default(subscriber);
+ tracing::callsite::rebuild_interest_cache();
+ (captured, guard)
+ }
+
+ async fn send(router: &Router, method: Method, uri: &str) -> StatusCode {
+ let request = Request::builder()
+ .method(method)
+ .uri(uri)
+ .body(Body::empty())
+ .unwrap();
+ router.clone().oneshot(request).await.unwrap().status()
+ }
+
+ #[tokio::test]
+ async fn logger_writes_routes_not_paths() {
+ let (captured, _guard) = capture();
+ let router = Router::new()
+ .route("/authorize/{nonce}", get(async || "ok"))
+ .finalize();
+
+ let status = send(
+ &router,
+ Method::GET,
+ "/authorize/nonce-secret?q=bert@example.org",
+ )
+ .await;
+ assert_eq!(status, StatusCode::OK);
+ let status = send(&router, Method::POST, "/authorize/nonce-secret").await;
+ assert_eq!(status, StatusCode::METHOD_NOT_ALLOWED);
+ let status = send(
+ &router,
+ Method::GET,
+ "/unknown/path-secret?q=bert@example.org",
+ )
+ .await;
+ assert_eq!(status, StatusCode::NOT_FOUND);
+
+ let log = captured.text();
+ for secret in ["secret", "bert", "?"] {
+ assert!(!log.contains(secret), "{secret} in the log:\n{log}");
+ }
+ for line in [
+ "GET /authorize/{nonce}",
+ "200 OK GET /authorize/{nonce} ",
+ "405 Method Not Allowed POST /authorize/{nonce} ",
+ "GET -",
+ "404 Not Found GET - ",
+ ] {
+ assert!(log.contains(line), "no {line} in the log:\n{log}");
+ }
+ }
+}
--
2.47.3