fix: log request paths only, never query strings
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -434,12 +434,18 @@ pub fn router(state: HubState) -> Router {
|
||||
Ok("1" | "true")
|
||||
);
|
||||
api.fallback(spa_fallback)
|
||||
.layer(TraceLayer::new_for_http())
|
||||
.layer(TraceLayer::new_for_http().make_span_with(request_span))
|
||||
.layer(axum::middleware::from_fn(move |request, next| {
|
||||
security_headers(hsts, request, next)
|
||||
}))
|
||||
}
|
||||
|
||||
/// The log span for a request: method and path only. The query string is left out because it carries
|
||||
/// OIDC `code` and `state` values (and may carry other secrets).
|
||||
fn request_span(request: &axum::http::Request<axum::body::Body>) -> tracing::Span {
|
||||
tracing::info_span!("request", method = %request.method(), path = %request.uri().path())
|
||||
}
|
||||
|
||||
/// Policy for the bundled web app: everything from this origin. The stylesheet pulls the
|
||||
/// Google Fonts CSS and font files, hence those two allowances.
|
||||
const CONTENT_SECURITY_POLICY: &str = "default-src 'self'; script-src 'self'; \
|
||||
@@ -1694,6 +1700,39 @@ mod tests {
|
||||
assert_eq!(r.status(), StatusCode::CONFLICT);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn request_logs_name_the_path_and_never_the_query() {
|
||||
use std::sync::{Arc, Mutex};
|
||||
#[derive(Clone, Default)]
|
||||
struct Sink(Arc<Mutex<Vec<u8>>>);
|
||||
impl std::io::Write for Sink {
|
||||
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<'a> tracing_subscriber::fmt::MakeWriter<'a> for Sink {
|
||||
type Writer = Sink;
|
||||
fn make_writer(&'a self) -> Sink {
|
||||
self.clone()
|
||||
}
|
||||
}
|
||||
let sink = Sink::default();
|
||||
let subscriber = tracing_subscriber::fmt().with_writer(sink.clone()).finish();
|
||||
tracing::subscriber::with_default(subscriber, || {
|
||||
let request = Request::get("/api/v1/auth/oidc/callback?code=secret-code&state=secret-state")
|
||||
.body(Body::empty())
|
||||
.unwrap();
|
||||
request_span(&request).in_scope(|| tracing::info!("handled"));
|
||||
});
|
||||
let logged = String::from_utf8(sink.0.lock().unwrap().clone()).unwrap();
|
||||
assert!(logged.contains("/api/v1/auth/oidc/callback"), "{logged}");
|
||||
assert!(!logged.contains("secret-code") && !logged.contains("secret-state"), "{logged}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn content_types_are_right_and_unknown_is_never_html() {
|
||||
assert_eq!(content_type("assets/x.png"), "image/png");
|
||||
|
||||
Reference in New Issue
Block a user