neiam
/recourse
public · Issues · Pulls · Labels · Forks · Compare · Actions success · Badges · Packages
⭐
Log in to mark this repository.
Never log Cookie/Authorization/Set-Cookie; INFO by default
dba9c4f · gmorell · 2026-12-03 04:38
Message
{commit_body(@commit)}
Files changed
modified
Cargo.toml
+1
−1
@@ -25,7 +25,7 @@ path = "src/main.rs"
| 25 | 25 | # Web stack |
| 26 | 26 | axum = { version = "0.8.5", features = ["multipart"] } |
| 27 | 27 | tokio = { version = "1.47.1", features = ["full"] } |
| 28 | −tower-http = { version = "0.6.6", features = ["fs", "trace"] } | |
| 28 | +tower-http = { version = "0.6.6", features = ["fs", "trace", "sensitive-headers"] } | |
| 29 | 29 | tracing = "0.1.41" |
| 30 | 30 | tracing-subscriber = { version = "0.3.19", features = ["env-filter"] } |
| 31 | 31 | log = "0.4.28" |
modified
src/main.rs
+8
−1
@@ -24,7 +24,10 @@ async fn main() -> Result<(), anyhow::Error> {
| 24 | 24 | tracing_subscriber::registry() |
| 25 | 25 | .with( |
| 26 | 26 | tracing_subscriber::EnvFilter::try_from_default_env() |
| 27 | − .unwrap_or_else(|_| "recourse=debug,tower_http=debug".into()), | |
| 27 | + // INFO by default: tower_http's DEBUG request spans list every | |
| 28 | + // header. Cookie/Authorization/Set-Cookie are marked sensitive | |
| 29 | + // below either way, so even RUST_LOG=debug prints "Sensitive". | |
| 30 | + .unwrap_or_else(|_| "recourse=info,tower_http=info".into()), | |
| 28 | 31 | ) |
| 29 | 32 | .with(tracing_subscriber::fmt::layer()) |
| 30 | 33 | .init(); |
@@ -139,6 +142,10 @@ async fn main() -> Result<(), anyhow::Error> {
| 139 | 142 | }, |
| 140 | 143 | ), |
| 141 | 144 | ) |
| 145 | + // Outside the TraceLayer, so the span sees them already marked: | |
| 146 | + // session cookies and bearer tokens never reach the logs. | |
| 147 | + .layer(recourse::web::sensitive_request_headers()) | |
| 148 | + .layer(recourse::web::sensitive_response_headers()) | |
| 142 | 149 | .layer(prometheus_layer) |
| 143 | 150 | // Outermost: the client address, for everything inside. |
| 144 | 151 | .layer(axum::middleware::from_fn_with_state(trusted_proxies, recourse::client_ip::layer)); |
modified
src/web.rs
+58
−0
@@ -44,6 +44,25 @@ pub fn add_helpers(jinja: &mut Environment<'static>, require_email_verification:
| 44 | 44 | } |
| 45 | 45 | |
| 46 | 46 | /// Every page and form route, with its authentication. |
| 47 | +/// Request headers that must never be logged: the session cookie and API | |
| 48 | +/// bearer tokens. Marked sensitive, a header's `Debug` prints `Sensitive`, so | |
| 49 | +/// tower_http's request spans (which list headers) leak nothing at any level. | |
| 50 | +const SENSITIVE: [axum::http::HeaderName; 3] = [ | |
| 51 | + axum::http::header::COOKIE, | |
| 52 | + axum::http::header::AUTHORIZATION, | |
| 53 | + axum::http::header::PROXY_AUTHORIZATION, | |
| 54 | +]; | |
| 55 | + | |
| 56 | +/// Wrap the trace layer in this (apply it after `TraceLayer`). | |
| 57 | +pub fn sensitive_request_headers() -> tower_http::sensitive_headers::SetSensitiveRequestHeadersLayer { | |
| 58 | + tower_http::sensitive_headers::SetSensitiveRequestHeadersLayer::new(SENSITIVE) | |
| 59 | +} | |
| 60 | + | |
| 61 | +/// `Set-Cookie` on the way out (a fresh session token at sign-in). | |
| 62 | +pub fn sensitive_response_headers() -> tower_http::sensitive_headers::SetSensitiveResponseHeadersLayer { | |
| 63 | + tower_http::sensitive_headers::SetSensitiveResponseHeadersLayer::new([axum::http::header::SET_COOKIE]) | |
| 64 | +} | |
| 65 | + | |
| 47 | 66 | pub fn router(db: Arc<DatabaseConnection>) -> Router<AppState> { |
| 48 | 67 | // Browse routes read `Option<UserContext>` so they work for both anonymous |
| 49 | 68 | // visitors and signed-in users. |
@@ -195,3 +214,42 @@ mod attr_tests {
| 195 | 214 | assert_eq!(escaped.as_str().unwrap(), "https://x/y?a=1&b=2 "q" <s> it's"); |
| 196 | 215 | } |
| 197 | 216 | } |
| 217 | + | |
| 218 | +#[cfg(test)] | |
| 219 | +mod sensitive_tests { | |
| 220 | + use axum::body::Body; | |
| 221 | + use axum::http::Request; | |
| 222 | + use tower::{ServiceBuilder, ServiceExt}; | |
| 223 | + | |
| 224 | + #[tokio::test] | |
| 225 | + async fn session_cookies_and_tokens_are_never_printed() { | |
| 226 | + let seen = std::sync::Arc::new(std::sync::Mutex::new(String::new())); | |
| 227 | + let s2 = seen.clone(); | |
| 228 | + let svc = ServiceBuilder::new() | |
| 229 | + .layer(super::sensitive_response_headers()) | |
| 230 | + .layer(super::sensitive_request_headers()) | |
| 231 | + .service_fn(move |req: Request<Body>| { | |
| 232 | + let s2 = s2.clone(); | |
| 233 | + async move { | |
| 234 | + // What a DEBUG span with include_headers(true) prints. | |
| 235 | + *s2.lock().unwrap() = format!("{:?}", req.headers()); | |
| 236 | + let mut res = axum::http::Response::new(Body::empty()); | |
| 237 | + res.headers_mut().insert("set-cookie", "session_token=fresh; HttpOnly".parse().unwrap()); | |
| 238 | + Ok::<_, std::convert::Infallible>(res) | |
| 239 | + } | |
| 240 | + }); | |
| 241 | + let req = Request::builder() | |
| 242 | + .header("cookie", "session_token=secret-session") | |
| 243 | + .header("authorization", "Bearer secret-token") | |
| 244 | + .header("user-agent", "test") | |
| 245 | + .body(Body::empty()) | |
| 246 | + .unwrap(); | |
| 247 | + let res = svc.oneshot(req).await.unwrap(); | |
| 248 | + let printed = seen.lock().unwrap().clone(); | |
| 249 | + assert!(!printed.contains("secret-session") && !printed.contains("secret-token"), "{printed}"); | |
| 250 | + assert!(printed.contains("Sensitive") && printed.contains("test")); | |
| 251 | + assert!(!format!("{:?}", res.headers()).contains("fresh")); | |
| 252 | + // Still delivered to handlers and clients unchanged. | |
| 253 | + assert_eq!(res.headers()["set-cookie"], "session_token=fresh; HttpOnly"); | |
| 254 | + } | |
| 255 | +} |
Parents: 60fbb92