From 45e8a3d803094c3fc6c3ec9dbb0df90751bfe092 Mon Sep 17 00:00:00 2001 From: hugocasa Date: Thu, 3 Dec 2026 16:21:02 +0100 Subject: [PATCH] fix: resolve job argument references only with the caller's own access --- backend/windmill-api-jobs/src/execution.rs | 83 +++++++++++++++++++++- backend/windmill-api/src/agent_runs.rs | 21 +++--- backend/windmill-api/src/jobs.rs | 4 ++ backend/windmill-worker/src/common.rs | 4 +- 4 files changed, 100 insertions(+), 12 deletions(-) diff --git a/backend/windmill-api-jobs/src/execution.rs b/backend/windmill-api-jobs/src/execution.rs index d362c91d78..a919365195 100644 --- a/backend/windmill-api-jobs/src/execution.rs +++ b/backend/windmill-api-jobs/src/execution.rs @@ -22,7 +22,7 @@ use std::str::FromStr; use std::sync::Arc; use tokio::sync::RwLock; use uuid::Uuid; -use windmill_api_auth::{check_scopes, get_scope_tags, ApiAuthed}; +use windmill_api_auth::{check_scopes, get_scope_tags, is_effectively_unscoped, ApiAuthed}; use windmill_audit::{ audit_oss::{audit_log_many, AuditAuthorable}, ActionKind, @@ -37,7 +37,7 @@ use windmill_common::{ get_latest_flow_version_info_for_path, jobs::{ check_tag_available_for_workspace_internal, format_result, script_path_to_payload, - ChosenBy, JobPayload, + ChosenBy, JobPayload, OnBehalfOf, }, runnable_ref::RunnableRef, triggers::TriggerMetadata, @@ -245,6 +245,35 @@ pub async fn unclaimable_run_lineage( Ok(referenced) } +/// The args of a run started through a `/jobs/run*` route, with the references the worker would +/// resolve omitted when it would resolve them with more than the caller holds: the run executes +/// as an `on_behalf_of` identity other than the caller's, or the caller's token is restricted by +/// scopes the job's token does not carry. A job token passes its own cap on to the job it starts +/// (`caller_scope_ceiling`), so only its identity is compared. An `mcp:all` token already reaches +/// everything its owner can read, so its references resolve as the owner's. +pub fn caller_run_args( + authed: &ApiAuthed, + run_as: Option<&OnBehalfOf>, + args: PushArgsOwned, +) -> PushArgsOwned { + let as_other = + run_as.is_some_and(|o| o.permissioned_as != username_to_permissioned_as(&authed.username)); + if as_other || restricted_caller(authed) { + args.omit_untrusted_interpolation() + } else { + args + } +} + +/// Whether `authed` is a token restricted by scopes the job it starts does not inherit, so the +/// references in its args must not resolve with its owner's full rights. +pub fn restricted_caller(authed: &ApiAuthed) -> bool { + let scopes = authed.scopes.as_deref(); + authed.job_id.is_none() + && !is_effectively_unscoped(scopes) + && !scopes.is_some_and(|s| s.iter().any(|s| s == "mcp:all")) +} + /// `WM_END_USER_EMAIL` of a run started through a `/jobs/run*` route. `WM_EMAIL` names the /// runnable's `on_behalf_of` identity when it has one, so this is the only place the caller's /// address reaches the job. A job's own `WM_TOKEN` passes on the end user of that job, and a run @@ -1441,6 +1470,11 @@ pub async fn run_flow<'c>( drop_unclaimable_run_lineage(db, w_id, &mut run_query, authed).await?; take_over_memory_turn(db, authed, w_id, flow_path, &run_query).await?; let on_behalf_of = flow_version_info.on_behalf_of(w_id, &db).await?; + let args = caller_run_args( + authed, + on_behalf_of.as_ref().filter(|_| tx_o.is_none()), + args, + ); let FlowVersionInfo { version, tag, @@ -1704,6 +1738,11 @@ pub async fn push_script_job_by_path_into_queue<'c>( let resolved_delete_secs = resolve_delete_after_secs(delete_after_use, delete_after_secs); let scheduled_for = run_query.get_scheduled_for(&db).await?; + let args = caller_run_args( + &authed, + on_behalf_of.as_ref().filter(|_| tx_o.is_none()), + args, + ); let tag = run_query.tag.clone().or(tag); let push_args = PushArgs { args: &args.args, extra: args.extra }; check_tag_available_for_workspace(&db, &w_id, &tag, &push_args, &authed).await?; @@ -2013,3 +2052,43 @@ mod result_to_response_tests { ); } } + +#[cfg(test)] +mod caller_run_args_tests { + use super::*; + + fn resolves(authed: &ApiAuthed, run_as: Option<&str>) -> bool { + let run_as = run_as.map(|p| OnBehalfOf { email: String::new(), permissioned_as: p.into() }); + let args = PushArgsOwned { + args: HashMap::from([("x".into(), serde_json::from_str(r#""$var:f/a/b""#).unwrap())]), + extra: None, + }; + caller_run_args(authed, run_as.as_ref(), args).args["x"].get() == r#""$var:f/a/b""# + } + + #[test] + fn omits_only_for_another_identity_or_a_scoped_token() { + let alice = ApiAuthed { username: "alice".into(), ..Default::default() }; + assert!(resolves(&alice, None)); + assert!(resolves(&alice, Some("u/alice"))); + assert!(!resolves(&alice, Some("u/bob"))); + + let scoped = ApiAuthed { + username: "alice".into(), + scopes: Some(vec!["jobs:run:scripts:u/alice/s".into()]), + ..Default::default() + }; + assert!(!resolves(&scoped, None)); + let tag_filter_only = + ApiAuthed { scopes: Some(vec!["if_jobs:filter_tags:t".into()]), ..scoped.clone() }; + assert!(resolves(&tag_filter_only, None)); + let mcp_all = ApiAuthed { scopes: Some(vec!["mcp:all".into()]), ..scoped.clone() }; + assert!(resolves(&mcp_all, None)); + assert!(!resolves(&mcp_all, Some("u/bob"))); + let mcp_selection = + ApiAuthed { scopes: Some(vec!["mcp:scripts:u/alice/s".into()]), ..scoped.clone() }; + assert!(!resolves(&mcp_selection, None)); + let job = ApiAuthed { job_id: Some(Uuid::nil()), ..scoped }; + assert!(resolves(&job, None)); + } +} diff --git a/backend/windmill-api/src/agent_runs.rs b/backend/windmill-api/src/agent_runs.rs index eedc726f9d..0ddd35129c 100644 --- a/backend/windmill-api/src/agent_runs.rs +++ b/backend/windmill-api/src/agent_runs.rs @@ -94,15 +94,18 @@ pub(crate) async fn run_agent_as( PushIsolationLevel::Isolated(user_db.clone(), authed.clone().into()), ) }; - if let Some(obo) = on_behalf_of.as_ref() { - // The worker would interpolate such an input with the job's token, which is the - // identity's. Checked as a whole so an argument name serde_json unwraps is refused too. - if untrusted_arg_interpolates(&serde_json::to_string(args)?) { - return Err(Error::BadRequest(format!( - "Agent {path} runs on behalf of {}, so its inputs cannot reference variables or resources", - obo.permissioned_as - ))); - } + // The worker would interpolate such an input with the job's token, which is the identity's, + // or carries none of a scoped caller's restrictions. Checked as a whole so an argument name + // serde_json unwraps is refused too. + if (on_behalf_of.is_some() || crate::jobs::restricted_caller(authed)) + && untrusted_arg_interpolates(&serde_json::to_string(args)?) + { + return Err(Error::BadRequest(format!( + "Agent {path} runs on behalf of another identity or is run with a scoped token, so \ + its inputs cannot reference variables or resources" + ))); + } + if on_behalf_of.is_some() { #[cfg(all(feature = "parquet", feature = "private"))] check_attachments_readable(db, authed, w_id, args).await?; } diff --git a/backend/windmill-api/src/jobs.rs b/backend/windmill-api/src/jobs.rs index 4446597635..5be1d4c87c 100644 --- a/backend/windmill-api/src/jobs.rs +++ b/backend/windmill-api/src/jobs.rs @@ -9099,6 +9099,7 @@ pub async fn run_wait_result_job_by_path_get( ) .await?; + let args = caller_run_args(&authed, on_behalf_authed.as_ref(), args); let tag = run_query.tag.clone().or(tag); let push_args = PushArgs { args: &args.args, extra: args.extra }; check_tag_available_for_workspace(&db, &w_id, &tag, &push_args, &authed).await?; @@ -9271,6 +9272,7 @@ pub async fn run_wait_result_script_by_path_internal( ) .await?; + let args = caller_run_args(&authed, on_behalf_of.as_ref(), args); let tag = run_query.tag.clone().or(tag); let push_args = PushArgs { args: &args.args, extra: args.extra }; check_tag_available_for_workspace(&db, &w_id, &tag, &push_args, &authed).await?; @@ -9392,6 +9394,7 @@ pub async fn run_wait_result_script_by_hash( } check_scopes(&authed, || format!("jobs:run:scripts:{path}"))?; + let args = caller_run_args(&authed, on_behalf_of.as_ref(), args); let tag = run_query.tag.clone().or(tag); let push_args = PushArgs { args: &args.args, extra: args.extra }; check_tag_available_for_workspace(&db, &w_id, &tag, &push_args, &authed).await?; @@ -11786,6 +11789,7 @@ pub async fn run_job_by_hash_inner( cache_ignore_s3_path = run_query.cache_ignore_s3_path; } let scheduled_for = run_query.get_scheduled_for(&db).await?; + let args = caller_run_args(&authed, on_behalf_of.as_ref(), args); let tag = run_query.tag.clone().or(tag); let push_args = PushArgs { args: &args.args, extra: args.extra }; diff --git a/backend/windmill-worker/src/common.rs b/backend/windmill-worker/src/common.rs index 381b004ca1..13b0054913 100644 --- a/backend/windmill-worker/src/common.rs +++ b/backend/windmill-worker/src/common.rs @@ -400,9 +400,11 @@ pub async fn transform_json_value( let name = y.strip_prefix("$").unwrap(); + // The job's token never becomes an argument: an argument is caller-controlled and the + // result echoes it back to the caller, who would hold the run-as identity's token. let value = variables .iter() - .find(|x| x.0 == name) + .find(|x| x.0 == name && name != "WM_TOKEN") .map(|x| x.1.clone()) .unwrap_or_else(|| y); Ok(json!(value))