From c3adae14017cb81cf15549c3f2000eba65e424c0 Mon Sep 17 00:00:00 2001 From: hugocasa Date: Tue, 10 Nov 2026 22:55:23 +0100 Subject: [PATCH] fix: tighten forwarded http trigger headers and route resolution (#12298) Co-authored-by: Claude Opus 5.5 --- backend/windmill-api/src/args.rs | 111 ++++++++++++++++-- backend/windmill-api/src/capture.rs | 4 +- backend/windmill-api/src/mcp/utils.rs | 8 +- .../windmill-api/src/triggers/http/handler.rs | 4 + .../src/triggers/http/http_trigger_args.rs | 10 +- backend/windmill-trigger-http/src/lib.rs | 90 ++++++++++++-- 6 files changed, 197 insertions(+), 30 deletions(-) diff --git a/backend/windmill-api/src/args.rs b/backend/windmill-api/src/args.rs index 6c1c9824bd..569df56ece 100644 --- a/backend/windmill-api/src/args.rs +++ b/backend/windmill-api/src/args.rs @@ -7,14 +7,16 @@ use axum::{ }; use base64::{engine::general_purpose::STANDARD as BASE64_STANDARD, Engine as _}; use bytes::Bytes; -use http::header::CONTENT_TYPE; +use http::header::{AUTHORIZATION, CONTENT_TYPE, COOKIE}; use serde::{Deserialize, Serialize}; use serde_json::value::RawValue; use sqlx::types::JsonRawValue; +use tower_cookies::Cookie; use windmill_common::{ error::Error, jobs::WM_TRACEPARENT, triggers::{RunnableFormat, RunnableFormatVersion, TriggerKind}, + users::COOKIE_NAME, worker::to_raw_value, DB, OTEL_TRACING_ENABLED, }; @@ -363,16 +365,19 @@ impl WebhookArgs { // don't get a stray `_wm_traceparent` arg key. let trace_context = inbound_traceparent(&self.metadata.headers); + // Webhooks are authenticated by Windmill (a webhook capture stands in for one). let headers = build_headers( &self.metadata.headers, self.metadata.query_include_header, runnable_format.has_preprocessor, + true, ); let query = build_query( self.metadata.query.as_deref(), self.metadata.query_include_query, runnable_format.has_preprocessor, + true, ); let mut push_args = match runnable_format { @@ -614,19 +619,50 @@ fn inbound_traceparent(headers: &HeaderMap) -> Option { .map(|s| s.to_string()) } +/// A header's value as a runnable or a capture sees it, `None` to withhold it. +/// +/// Windmill's session cookie is never forwarded: browsers attach it on their own, so it would hand +/// the visitor's session to whoever wrote the runnable. Other cookies are kept. When the request +/// was authenticated by Windmill (`windmill_authed`), its bearer token is withheld as well. +fn forwarded_header_value( + name: &str, + value: &http::HeaderValue, + windmill_authed: bool, +) -> Option { + let value = value.to_str().unwrap_or(""); + if name.eq_ignore_ascii_case(COOKIE.as_str()) { + // Parsed the way `extract_token` reads it, so an encoded name is caught too. + let other_cookies = value + .split(';') + .filter(|c| !Cookie::parse_encoded(*c).is_ok_and(|c| c.name() == COOKIE_NAME)) + .map(str::trim) + .collect::>(); + return (!other_cookies.is_empty()).then(|| other_cookies.join("; ")); + } + if windmill_authed + && name.eq_ignore_ascii_case(AUTHORIZATION.as_str()) + && value.starts_with("Bearer ") + { + return None; + } + Some(value.to_string()) +} + +/// `windmill_authed`: the caller was authenticated by Windmill, so `Authorization: Bearer` carries +/// their Windmill token. See `forwarded_header_value`. pub fn build_headers( headers: &HeaderMap, include_header: Option, include_all_headers: bool, + windmill_authed: bool, ) -> HashMap> { let mut selected_headers = HashMap::new(); if include_all_headers { for (k, v) in headers.iter() { - selected_headers.insert( - k.to_string(), - to_raw_value(&v.to_str().unwrap_or("").to_string()), - ); + if let Some(v) = forwarded_header_value(k.as_str(), v, windmill_authed) { + selected_headers.insert(k.to_string(), to_raw_value(&v)); + } } } else { let whitelist = include_header @@ -636,10 +672,13 @@ pub fn build_headers( .iter() .chain(INCLUDE_HEADERS.iter()) .for_each(|h| { - if let Some(v) = headers.get(h) { + if let Some(v) = headers + .get(h) + .and_then(|v| forwarded_header_value(h, v, windmill_authed)) + { selected_headers.insert( h.to_string().to_lowercase().replace('-', "_"), - to_raw_value(&v.to_str().unwrap_or("").to_string()), + to_raw_value(&v), ); } }); @@ -651,16 +690,20 @@ pub fn build_query( query: Option<&str>, include_query: Option, include_all_query: bool, + windmill_authed: bool, ) -> HashMap> { let Some(query) = query else { return HashMap::new(); }; + // `?token=` is a Windmill credential when Windmill authenticated the request. + let withheld = |k: &str| windmill_authed && k == "token"; if include_all_query { let queries = serde_urlencoded::from_str::>(&query).unwrap_or_default(); queries .into_iter() + .filter(|(k, _)| !withheld(k)) .map(|(k, v)| (k, to_raw_value(&v))) .collect() } else { @@ -671,11 +714,14 @@ pub fn build_query( if !parse_query_args.is_empty() { let queries = serde_urlencoded::from_str::>(&query).unwrap_or_default(); - parse_query_args.iter().for_each(|h| { - if let Some(v) = queries.get(h) { - args.insert(h.to_string(), to_raw_value(v)); - } - }); + parse_query_args + .iter() + .filter(|h| !withheld(h)) + .for_each(|h| { + if let Some(v) = queries.get(h) { + args.insert(h.to_string(), to_raw_value(v)); + } + }); } args } @@ -755,6 +801,47 @@ mod tests { use super::*; + #[test] + fn test_build_headers_withholds_windmill_credentials() { + let headers = HeaderMap::from_iter([ + (COOKIE, "a=1; token=s; %74oken=s; b=2".parse().unwrap()), + (AUTHORIZATION, "Bearer s".parse().unwrap()), + ]); + let get = + |h: &HashMap>, k: &str| h.get(k).map(|v| v.get().to_string()); + + for windmill_authed in [false, true] { + let auth = (!windmill_authed).then(|| r#""Bearer s""#.to_string()); + let all = build_headers(&headers, None, true, windmill_authed); + assert_eq!(get(&all, "cookie").as_deref(), Some(r#""a=1; b=2""#)); + assert_eq!(get(&all, "authorization"), auth); + + let listed = build_headers( + &headers, + Some("cookie,authorization".into()), + false, + windmill_authed, + ); + assert_eq!(get(&listed, "cookie").as_deref(), Some(r#""a=1; b=2""#)); + assert_eq!(get(&listed, "authorization"), auth); + + let query = build_query(Some("token=s&a=1"), None, true, windmill_authed); + assert_eq!(query.contains_key("token"), !windmill_authed); + assert!(query.contains_key("a")); + let listed = build_query( + Some("token=s&a=1"), + Some("token,a".into()), + false, + windmill_authed, + ); + assert_eq!(listed.contains_key("token"), !windmill_authed); + assert!(listed.contains_key("a")); + } + + let only_session = HeaderMap::from_iter([(COOKIE, "token=s".parse().unwrap())]); + assert!(build_headers(&only_session, None, true, false).is_empty()); + } + #[tokio::test] async fn test_bodyless_request_with_json_content_type() { let request = Request::builder() diff --git a/backend/windmill-api/src/capture.rs b/backend/windmill-api/src/capture.rs index 8e9fb4a823..7dbb464a57 100644 --- a/backend/windmill-api/src/capture.rs +++ b/backend/windmill-api/src/capture.rs @@ -1250,8 +1250,8 @@ async fn http_payload( .map(|(k, v)| (k.to_string(), v.to_string())) .collect(); - let headers = build_headers(&args.0.metadata.headers, None, true); - let query = build_query(args.0.metadata.query.as_deref(), None, true); + let headers = build_headers(&args.0.metadata.headers, None, true, false); + let query = build_query(args.0.metadata.query.as_deref(), None, true, false); let preprocessor_args = args .clone() diff --git a/backend/windmill-api/src/mcp/utils.rs b/backend/windmill-api/src/mcp/utils.rs index e4245c32aa..72ed0ca03e 100644 --- a/backend/windmill-api/src/mcp/utils.rs +++ b/backend/windmill-api/src/mcp/utils.rs @@ -768,16 +768,16 @@ struct McpPreprocessorEvent<'a> { /// Headers withheld from a preprocessor because they authenticate the connection. /// -/// Not a security boundary: a webhook preprocessor receives all three. Withheld -/// because nothing needs them yet, and releasing one later is additive while -/// withdrawing one after runnables read it is not. +/// `build_headers` already withholds Windmill's own credentials. The rest is +/// withheld because nothing needs it yet, and releasing one later is additive +/// while withdrawing one after runnables read it is not. const WITHHELD_FROM_PREPROCESSOR: &[&str] = &["authorization", "cookie", "proxy-authorization"]; /// Every header a preprocessor may see. fn preprocessor_headers( headers: &http::HeaderMap, ) -> HashMap> { - let mut selected = build_headers(headers, None, true); + let mut selected = build_headers(headers, None, true, true); selected.retain(|name, _| { !WITHHELD_FROM_PREPROCESSOR .iter() diff --git a/backend/windmill-api/src/triggers/http/handler.rs b/backend/windmill-api/src/triggers/http/handler.rs index daff81249f..ed7a61d986 100644 --- a/backend/windmill-api/src/triggers/http/handler.rs +++ b/backend/windmill-api/src/triggers/http/handler.rs @@ -759,6 +759,10 @@ async fn route_job( ¶ms, runnable_format, trigger.wrap_body, + matches!( + trigger.authentication_method, + AuthenticationMethod::Windmill + ), ) .map(windmill_queue::PushArgsOwned::omit_untrusted_interpolation) .map_err(|e| e.into_response())?; diff --git a/backend/windmill-api/src/triggers/http/http_trigger_args.rs b/backend/windmill-api/src/triggers/http/http_trigger_args.rs index f8460f42e2..2dacbdb50d 100644 --- a/backend/windmill-api/src/triggers/http/http_trigger_args.rs +++ b/backend/windmill-api/src/triggers/http/http_trigger_args.rs @@ -121,9 +121,15 @@ impl HttpTriggerArgs { params: &HashMap, format: RunnableFormat, wrap_body: bool, + windmill_authed: bool, ) -> Result { - let headers = build_headers(&self.0.metadata.headers, None, true); - let query = build_query(self.0.metadata.query.as_deref(), None, true); + let headers = build_headers(&self.0.metadata.headers, None, true, windmill_authed); + let query = build_query( + self.0.metadata.query.as_deref(), + None, + true, + windmill_authed, + ); match format { RunnableFormat { has_preprocessor: true, version: RunnableFormatVersion::V2 } => { // we don't care about wrap_body in v2 diff --git a/backend/windmill-trigger-http/src/lib.rs b/backend/windmill-trigger-http/src/lib.rs index e9dfbd405a..e39828dfd8 100644 --- a/backend/windmill-trigger-http/src/lib.rs +++ b/backend/windmill-trigger-http/src/lib.rs @@ -58,8 +58,47 @@ pub struct TriggerRoute { pub mode: TriggerMode, } +/// The routes of one HTTP method. Workspaced routes are looked up first, so a route a workspace +/// serves under its own `/{workspace_id}/` prefix is never answered by an instance-wide route, +/// whatever their patterns, and a conflict between the two cannot drop either. +pub struct MethodRouter { + workspaced: matchit::Router, + instance_wide: matchit::Router, +} + +impl Default for MethodRouter { + fn default() -> Self { + Self { workspaced: matchit::Router::new(), instance_wide: matchit::Router::new() } + } +} + +impl MethodRouter { + pub fn insert( + &mut self, + workspaced: bool, + path: String, + value: T, + ) -> std::result::Result<(), matchit::InsertError> { + if workspaced { + &mut self.workspaced + } else { + &mut self.instance_wide + } + .insert(path, value) + } + + pub fn at<'m, 'p>( + &'m self, + path: &'p str, + ) -> std::result::Result, matchit::MatchError> { + self.workspaced + .at(path) + .or_else(|_| self.instance_wide.at(path)) + } +} + pub struct RoutersCache { - pub routers: HashMap>, + pub routers: HashMap>, pub version: i64, /// `HTTP_ROUTERS_INVALIDATIONS` as of the moment these rows were read. A rebuild that /// started before an invalidation publishes a count behind the current one, which is what @@ -343,21 +382,25 @@ pub async fn refresh_routers( .fetch_all(db) .await?; - let mut router = matchit::Router::new(); + let mut router = MethodRouter::default(); let http_route_workspaced = HTTP_ROUTE_WORKSPACED_ROUTE.load(std::sync::atomic::Ordering::Relaxed); for trigger in triggers { - let full_path = - if trigger.workspaced_route || *CLOUD_HOSTED || http_route_workspaced { - format!("/{}/{}", trigger.workspace_id, trigger.route_path) - } else { - format!("/{}", trigger.route_path) - }; + let workspaced = trigger.workspaced_route || *CLOUD_HOSTED || http_route_workspaced; + let full_path = if workspaced { + format!("/{}/{}", trigger.workspace_id, trigger.route_path) + } else { + format!("/{}", trigger.route_path) + }; if trigger.is_static_website { router - .insert(format!("{}/*wm_subpath", full_path), trigger.clone()) + .insert( + workspaced, + format!("{}/*wm_subpath", full_path), + trigger.clone(), + ) .unwrap_or_else(|e| { tracing::warn!( "Failed to consider HTTP route {}/*wm_subpath: {:?}", @@ -367,7 +410,7 @@ pub async fn refresh_routers( }); } router - .insert(full_path.clone(), trigger.clone()) + .insert(workspaced, full_path.clone(), trigger.clone()) .unwrap_or_else(|e| { tracing::warn!("Failed to consider HTTP route {}: {:?}", full_path, e,); }); @@ -492,6 +535,33 @@ mod tests { validate_allowed_origins, MAX_ALLOWED_ORIGINS, MAX_ALLOWED_ORIGIN_LEN, }; + #[test] + fn test_workspaced_route_takes_precedence_over_instance_wide() { + let mut router = MethodRouter::default(); + router + .insert(false, "/ws/gh/myrepo".to_string(), "instance") + .unwrap(); + router + .insert(false, "/ws/hooks/*rest".to_string(), "instance") + .unwrap(); + router + .insert(true, "/ws/gh/:repo".to_string(), "ws") + .unwrap(); + router + .insert(true, "/ws/hooks/:id".to_string(), "ws") + .unwrap(); + router + .insert(false, "/other/:id".to_string(), "instance") + .unwrap(); + + let at = |path| router.at(path).map(|m| *m.value).ok(); + assert_eq!(at("/ws/gh/myrepo"), Some("ws")); + assert_eq!(at("/ws/hooks/abc"), Some("ws")); + assert_eq!(at("/ws/hooks/abc/def"), Some("instance")); + assert_eq!(at("/other/1"), Some("instance")); + assert_eq!(at("/nothing"), None); + } + #[test] fn test_request_type_backward_compatibility() { // Test with new request_type field