diff --git a/backend/windmill-api-workspaces/src/remote_deploy.rs b/backend/windmill-api-workspaces/src/remote_deploy.rs index 070e138154..095436f91b 100644 --- a/backend/windmill-api-workspaces/src/remote_deploy.rs +++ b/backend/windmill-api-workspaces/src/remote_deploy.rs @@ -64,24 +64,6 @@ pub fn workspaced_service(proxy_body_limit: usize) -> Router { ) } -/// A request URI as logs should record it: with the proxy key masked, since that key is what keeps -/// a link from spending a stored remote token. -pub struct RedactedUri<'a>(pub &'a axum::http::Uri); - -impl std::fmt::Display for RedactedUri<'_> { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - let Some((head, keyed)) = self.0.path().split_once(PROXY_PREFIX) else { - return self.0.fmt(f); - }; - let rest = keyed.find('/').map_or("", |i| &keyed[i..]); - write!(f, "{head}{PROXY_PREFIX}***{rest}")?; - match self.0.query() { - Some(query) => write!(f, "?{query}"), - None => Ok(()), - } - } -} - #[derive(Serialize, Deserialize, Clone, Debug)] pub struct RemoteDeployTarget { /// Root URL of the remote instance, without `/api`. @@ -747,19 +729,6 @@ mod tests { } } - #[test] - fn redacted_uri_masks_the_proxy_key() { - let proxied: axum::http::Uri = "/api/w/dev/remote_deploy/proxy/secretKey/flows/get/f/a?x=1" - .parse() - .unwrap(); - assert_eq!( - RedactedUri(&proxied).to_string(), - "/api/w/dev/remote_deploy/proxy/***/flows/get/f/a?x=1" - ); - let other: axum::http::Uri = "/api/w/dev/flows/list?x=1".parse().unwrap(); - assert_eq!(RedactedUri(&other).to_string(), "/api/w/dev/flows/list?x=1"); - } - #[test] fn target_normalization() { let target = RemoteDeployTarget { diff --git a/backend/windmill-api/src/s3_log_batching.rs b/backend/windmill-api/src/s3_log_batching.rs index 4c1d0e8e44..bf8fb60168 100644 --- a/backend/windmill-api/src/s3_log_batching.rs +++ b/backend/windmill-api/src/s3_log_batching.rs @@ -116,7 +116,7 @@ pub fn flush_s3_batches() { pub async fn s3_proxy_log_middleware(request: Request, next: Next) -> Response { let s3_info = if request.uri().path().contains("/s3_proxy/") { Some(S3ProxyRequest { - uri: request.uri().to_string(), + uri: crate::tracing_init::RedactedUri(request.uri()).to_string(), method: request.method().to_string(), }) } else { diff --git a/backend/windmill-api/src/tracing_init.rs b/backend/windmill-api/src/tracing_init.rs index 2fb668a39c..675ac339ed 100644 --- a/backend/windmill-api/src/tracing_init.rs +++ b/backend/windmill-api/src/tracing_init.rs @@ -14,9 +14,74 @@ use axum::response::Response as AxumResponse; use hyper::Response; use tower_http::trace::{MakeSpan, OnFailure, OnResponse}; use uuid::Uuid; -use windmill_api_workspaces::remote_deploy::RedactedUri; use windmill_common::log_context::{with_log_context, LogContext}; +/// Routes that take a credential as a path segment, each given as the segments that precede it +/// (`*` matches any one). A route that takes a secret in its path belongs here, or the secret is +/// written to the request logs. +const SECRET_PATH_SEGMENTS: &[&[&str]] = &[ + &["scripts_u", "tokened_raw", "*"], + &["jobs_u", "resume", "*", "*"], + &["jobs_u", "cancel", "*", "*"], + &["jobs_u", "get_flow", "*", "*"], + &["apps_u", "public_app"], + &["apps_u", "guest_entry"], + &["apps_u", "embed_token"], + &["auth", "login_link"], + &["remote_deploy", "proxy"], +]; + +fn is_secret_query_key(key: &str) -> bool { + let key = key.to_ascii_lowercase(); + key == "code" + || [ + "token", + "secret", + "password", + "signature", + "api_key", + "apikey", + ] + .iter() + .any(|s| key.contains(s)) +} + +/// A request URI as logs record it: with the credentials it may carry masked. +pub(crate) struct RedactedUri<'a>(pub &'a axum::http::Uri); + +impl std::fmt::Display for RedactedUri<'_> { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let mut segments: Vec<&str> = self.0.path().split('/').collect(); + for i in 0..segments.len() { + for prefix in SECRET_PATH_SEGMENTS { + let secret = i + prefix.len(); + if secret < segments.len() + && prefix + .iter() + .zip(&segments[i..]) + .all(|(p, s)| *p == "*" || p == s) + { + segments[secret] = "***"; + } + } + } + f.write_str(&segments.join("/"))?; + if let Some(query) = self.0.query() { + f.write_str("?")?; + for (i, pair) in query.split('&').enumerate() { + if i > 0 { + f.write_str("&")?; + } + match pair.split_once('=') { + Some((key, _)) if is_secret_query_key(key) => write!(f, "{key}=***")?, + _ => f.write_str(pair)?, + } + } + } + Ok(()) + } +} + lazy_static::lazy_static! { static ref LOG_REQUESTS: bool = std::env::var("LOG_REQUESTS") .ok() @@ -123,3 +188,44 @@ pub async fn log_context_middleware(request: Request, next: Next) -> AxumRespons with_log_context(ctx, next.run(request)).await } + +#[cfg(test)] +mod tests { + use super::RedactedUri; + + fn redacted(uri: &str) -> String { + RedactedUri(&uri.parse().unwrap()).to_string() + } + + #[test] + fn redacted_uri_masks_credentials() { + for (uri, expected) in [ + ( + "/api/scripts_u/tokened_raw/ws/abc123/u/admin/foo.ts", + "/api/scripts_u/tokened_raw/ws/***/u/admin/foo.ts", + ), + ( + "/api/w/ws/jobs_u/resume/0191/42/deadbeef?approver=bob", + "/api/w/ws/jobs_u/resume/0191/42/***?approver=bob", + ), + ( + "/api/w/ws/jobs_u/get_flow/0191/42/deadbeef", + "/api/w/ws/jobs_u/get_flow/0191/42/***", + ), + ("/api/w/ws/apps_u/public_app/s3cr3t", "/api/w/ws/apps_u/public_app/***"), + ("/api/auth/login_link/tok", "/api/auth/login_link/***"), + ( + "/api/w/dev/remote_deploy/proxy/key/flows/get/f/a?x=1", + "/api/w/dev/remote_deploy/proxy/***/flows/get/f/a?x=1", + ), + ( + "/api/w/ws/jobs/run_wait_result/p/f/a?token=abc&x=1&view_token=v", + "/api/w/ws/jobs/run_wait_result/p/f/a?token=***&x=1&view_token=***", + ), + ("/api/w/ws/flows/list?x=1", "/api/w/ws/flows/list?x=1"), + ("/api/w/ws/jobs_u/resume", "/api/w/ws/jobs_u/resume"), + ] { + assert_eq!(redacted(uri), expected, "{uri}"); + } + } +}