diff --git a/backend/.sqlx/query-4985970f9ea9e0e7644e9fe3f81fc2fe29682ec0673bf29d8ee21a313bef6bd6.json b/backend/.sqlx/query-4985970f9ea9e0e7644e9fe3f81fc2fe29682ec0673bf29d8ee21a313bef6bd6.json new file mode 100644 index 0000000000..0449c10bf4 --- /dev/null +++ b/backend/.sqlx/query-4985970f9ea9e0e7644e9fe3f81fc2fe29682ec0673bf29d8ee21a313bef6bd6.json @@ -0,0 +1,23 @@ +{ + "db_name": "PostgreSQL", + "query": "SELECT path FROM variable WHERE workspace_id = $1 AND path = ANY($2) AND is_secret", + "describe": { + "columns": [ + { + "ordinal": 0, + "name": "path", + "type_info": "Varchar" + } + ], + "parameters": { + "Left": [ + "Text", + "TextArray" + ] + }, + "nullable": [ + false + ] + }, + "hash": "4985970f9ea9e0e7644e9fe3f81fc2fe29682ec0673bf29d8ee21a313bef6bd6" +} diff --git a/backend/windmill-api-integration-tests/tests/flow_env_secret_refs.rs b/backend/windmill-api-integration-tests/tests/flow_env_secret_refs.rs new file mode 100644 index 0000000000..0cefff6342 --- /dev/null +++ b/backend/windmill-api-integration-tests/tests/flow_env_secret_refs.rs @@ -0,0 +1,104 @@ +//! A secret reached through `flow_env` is stored on the step job as a reference (exact +//! `flow_env.X` read) or encrypted (computed from it), and still resolves when the step runs. +//! A subflow step, which resolves nothing in its args, gets a secret entry as a reference or +//! encrypted, and the resolved value of a non-secret one. +//! +//! cargo test -p windmill-api-integration-tests --features deno_core --test flow_env_secret_refs +#![cfg(feature = "deno_core")] + +use serde_json::json; +use sqlx::{Pool, Postgres}; +use windmill_common::{flows::FlowValue, jobs::JobPayload}; +use windmill_test_utils::*; + +async fn api_create(port: u16, kind: &str, body: serde_json::Value) { + let resp = reqwest::Client::new() + .post(format!( + "http://localhost:{port}/api/w/test-workspace/{kind}/create" + )) + .header("Authorization", "Bearer SECRET_TOKEN") + .json(&body) + .send() + .await + .unwrap(); + assert_eq!( + resp.status(), + 201, + "{}", + resp.text().await.unwrap_or_default() + ); +} + +#[sqlx::test(migrations = "../migrations", fixtures("base"))] +async fn test_flow_env_secret_kept_out_of_step_args(db: Pool) -> anyhow::Result<()> { + initialize_tracing().await; + let server = ApiServer::start(db.clone()).await?; + let port = server.addr.port(); + let secret = "flow_env_secret_value_8k2q"; + for (path, value, is_secret) in [ + ("u/test-user/tok", secret, true), + ("u/test-user/host", "example.com", false), + ] { + let body = json!({"path": path, "value": value, "is_secret": is_secret, "description": ""}); + api_create(port, "variables", body).await; + } + // A flow without steps returns its own inputs. + let sub = json!({"path": "u/test-user/sub", "summary": "", "description": "", + "schema": {"type": "object", "properties": {}}, "value": {"modules": []}}); + api_create(port, "flows", sub).await; + + let flow: FlowValue = serde_json::from_value(json!({ + "flow_env": {"TOKEN": "$var:u/test-user/tok", "HOST": "$var:u/test-user/host"}, + "modules": [ + {"id": "a", "value": { + "type": "rawscript", + "language": "bash", + "content": "tok=\"$1\"\nhdr=\"$2\"\nurl=\"$3\"\necho \"{\\\"tok\\\":\\\"$tok\\\",\\\"hdr\\\":\\\"$hdr\\\",\\\"url\\\":\\\"$url\\\"}\" > result.json", + "input_transforms": { + "tok": {"type": "javascript", "expr": "flow_env.TOKEN"}, + "hdr": {"type": "javascript", "expr": "\"Bearer \" + flow_env.TOKEN"}, + "url": {"type": "javascript", "expr": "\"https://\" + flow_env.HOST"} + } + }}, + {"id": "s", "value": { + "type": "flow", + "path": "u/test-user/sub", + "input_transforms": { + "tok": {"type": "javascript", "expr": "flow_env.TOKEN"}, + "hdr": {"type": "javascript", "expr": "\"Bearer \" + flow_env.TOKEN"}, + "host": {"type": "javascript", "expr": "flow_env.HOST"} + } + }} + ] + }))?; + + let flow_job = + RunJob::from(JobPayload::RawFlow { value: flow, path: None, restarted_from: None }) + .run_until_complete(&db, false, port) + .await; + let sub_args = flow_job.json_result().unwrap(); + assert_eq!(sub_args["tok"], "$var:u/test-user/tok"); + assert!(sub_args["hdr"].as_str().unwrap().starts_with("$encrypted:")); + assert_eq!(sub_args["host"], "example.com"); + + let (step_args, step_result): (serde_json::Value, serde_json::Value) = sqlx::query_as( + "SELECT j.args, c.result FROM v2_job j JOIN v2_job_completed c ON c.id = j.id + WHERE j.parent_job = $1 AND j.kind <> 'flow'", + ) + .bind(flow_job.id) + .fetch_one(&db) + .await?; + assert_eq!( + step_result, + json!({"tok": secret, "hdr": format!("Bearer {secret}"), "url": "https://example.com"}) + ); + assert_eq!(step_args["tok"], "$var:u/test-user/tok"); + assert!(step_args["hdr"] + .as_str() + .unwrap() + .starts_with("$encrypted:")); + assert_eq!(step_args["url"], "https://example.com"); + assert!(!step_args.to_string().contains(secret)); + + Ok(()) +} diff --git a/backend/windmill-worker/src/common.rs b/backend/windmill-worker/src/common.rs index c0ae372e38..f0697283aa 100644 --- a/backend/windmill-worker/src/common.rs +++ b/backend/windmill-worker/src/common.rs @@ -369,9 +369,11 @@ pub async fn transform_json_value( }) }) } - Connection::Http(_) => { - Err(Error::NotFound("Http connection not supported".to_string())) - } + Connection::Http(_) => Err(Error::ExecutionErr(format!( + "`{name}` is an `$encrypted:` value, which an agent worker cannot decrypt: \ + pass the secret as a `$var:` reference (in a flow, a static input or an \ + exact `flow_env.` read) and build the value inside the script" + ))), } // let path = y.strip_prefix("$res:").unwrap(); diff --git a/backend/windmill-worker/src/worker_flow.rs b/backend/windmill-worker/src/worker_flow.rs index ff2a9db436..23f8fb69e9 100644 --- a/backend/windmill-worker/src/worker_flow.rs +++ b/backend/windmill-worker/src/worker_flow.rs @@ -18,6 +18,7 @@ use crate::{ JobCompletedSender, PreviousResult, SameWorkerSender, SendResultPayload, StepFailureKind, UpdateFlow, KEEP_JOB_DIR, }; +use windmill_common::variables::{build_crypt_with_key_suffix, encrypt, WorkspaceCrypt}; use anyhow::Context; use async_once_cell::Lazy; @@ -2888,11 +2889,132 @@ where } } +/// The unresolved `flow_env` and its secret entries, so that the args stored on a step job +/// never hold a resolved secret: an exact `flow_env.X` read stores the entry's reference, +/// resolved (audited and masked) when the step runs like a static input, and any other +/// expression reading a secret entry stores its result encrypted. +/// +/// A subflow step resolves nothing in its args, so it gets the resolved value of a +/// non-secret entry, while a secret one reaches it as a reference or encrypted, which its own +/// steps resolve or decrypt (they share its root job id). +pub(crate) struct FlowEnvRefs<'a> { + raw: &'a HashMap>, + db: &'a DB, + workspace_id: String, + root_job_id: Uuid, + // Loaded by the first input whose expression mentions `flow_env`, so that the steps + // reading none of it cost no query. + secrets: tokio::sync::OnceCell, +} + +struct FlowEnvSecrets { + entries: HashMap>, + // Keyed on the root job id, which is what the step's worker decrypts `$encrypted:` + // args with. Present only when `entries` is non-empty. + crypt: Option, +} + +impl<'a> FlowEnvRefs<'a> { + fn new(db: &'a DB, flow_job: &MiniPulledJob, raw: &'a HashMap>) -> Self { + Self { + raw, + db, + workspace_id: flow_job.workspace_id.clone(), + root_job_id: get_root_job_id(flow_job), + secrets: tokio::sync::OnceCell::new(), + } + } + + async fn secrets(&self) -> error::Result<&FlowEnvSecrets> { + self.secrets.get_or_try_init(|| self.load_secrets()).await + } + + async fn load_secrets(&self) -> error::Result { + let raw = self.raw; + let mut secret_keys = vec![]; + let mut var_keys: Vec<(String, String)> = vec![]; + for (k, v) in raw { + if !v.get().starts_with("\"$") { + continue; + } + let Ok(s) = serde_json::from_str::(v.get()) else { + continue; + }; + if s.starts_with("$encrypted:") { + secret_keys.push(k.clone()); + } else if let Some(path) = s + .strip_prefix("$var:") + .or_else(|| s.strip_prefix("$jsonvar:")) + { + var_keys.push((k.clone(), path.to_string())); + } + } + if !var_keys.is_empty() { + let paths: Vec = var_keys.iter().map(|(_, p)| p.clone()).collect(); + let secret_paths = sqlx::query_scalar!( + "SELECT path FROM variable WHERE workspace_id = $1 AND path = ANY($2) AND is_secret", + &self.workspace_id, + &paths + ) + .fetch_all(self.db) + .await?; + secret_keys.extend( + var_keys + .into_iter() + .filter(|(_, p)| secret_paths.contains(p)) + .map(|(k, _)| k), + ); + } + let entries: HashMap> = secret_keys + .into_iter() + .map(|k| (k.clone(), raw[&k].clone())) + .collect(); + let crypt = if entries.is_empty() { + None + } else { + Some( + build_crypt_with_key_suffix( + self.db, + &self.workspace_id, + &self.root_job_id.to_string(), + ) + .await?, + ) + }; + Ok(FlowEnvSecrets { entries, crypt }) + } +} + +impl FlowEnvSecrets { + // Decided from the expression's text, by a literal `flow_env.KEY` / `flow_env["KEY"]` + // access: a dynamic key (`flow_env[k]`) or a destructured `flow_env` is not detected. + fn reads_secret(&self, expr: &str) -> bool { + let ident = |c: char| c.is_alphanumeric() || c == '_' || c == '$'; + self.entries.keys().any(|k| { + let dotted = [format!("flow_env.{k}"), format!("flow_env?.{k}")]; + let bracketed = ["\"", "'", "`"].map(|q| format!("[{q}{k}{q}]")); + dotted.iter().any(|p| { + expr.match_indices(p.as_str()).any(|(i, _)| { + !expr[..i].chars().next_back().is_some_and(ident) + && !expr[i + p.len()..].chars().next().is_some_and(ident) + }) + }) || bracketed.iter().any(|b| { + ["flow_env", "flow_env?."].iter().any(|o| { + expr.match_indices(&format!("{o}{b}")) + .any(|(i, _)| !expr[..i].chars().next_back().is_some_and(ident)) + }) + }) + }) + } +} + /// resumes should be in order of timestamp ascending, so that more recent are at the end #[instrument(level = "trace", skip_all)] async fn transform_input( flow_args: Marc>>, flow_env: Option<&HashMap>>, + env_refs: Option<&FlowEnvRefs<'_>>, + into_subflow: bool, last_result: Arc>, input_transforms: &HashMap, resumes: Arc>, @@ -2930,6 +3052,22 @@ async fn transform_input( mapped.insert(key.to_string(), to_raw_value(&value)); } InputTransform::Javascript { expr } => { + let env_refs = env_refs.filter(|_| expr.contains("flow_env")); + if let Some(r) = env_refs { + let exact_env = if into_subflow { + &r.secrets().await?.entries + } else { + r.raw + }; + if let Some(raw) = windmill_jseval::try_exact_property_access( + expr.trim(), + None, + Some(exact_env), + ) { + mapped.insert(key.to_string(), raw); + continue; + } + } let v = eval_timeout( expr.to_string(), env.clone(), @@ -2945,6 +3083,16 @@ async fn transform_input( "Error during isolated evaluation of expression `{expr}`:\n{e:#}" )) })?; + let secrets = match env_refs { + Some(r) => Some(r.secrets().await?), + None => None, + }; + let v = match secrets { + Some(s @ FlowEnvSecrets { crypt: Some(crypt), .. }) if s.reads_secret(expr) => { + to_raw_value(&format!("$encrypted:{}", encrypt(crypt, v.get()))) + } + _ => v, + }; mapped.insert(key.to_string(), v); } InputTransform::Ai => (), @@ -3043,6 +3191,8 @@ pub async fn handle_flow( } } + let env_refs = env_source.map(|raw| FlowEnvRefs::new(db, &flow_job, raw)); + let schedule_path = flow_job.schedule_path(); if !flow_job.is_flow_step() && status.retry.fail_count == 0 @@ -3186,6 +3336,7 @@ pub async fn handle_flow( status, flow, flow_env, + env_refs.as_ref(), no_flow_env, db, client, @@ -3475,6 +3626,7 @@ async fn push_next_flow_job( mut status: FlowStatus, flow: &FlowValue, flow_env: Option<&HashMap>>, + env_refs: Option<&FlowEnvRefs<'_>>, // Neither this flow nor any ancestor defines `flow_env`. no_flow_env: bool, db: &sqlx::Pool, @@ -4296,6 +4448,8 @@ async fn push_next_flow_job( transform_input( arc_flow_job_args.clone(), flow_env, + env_refs, + module.is_flow(), arc_last_job_result.clone(), input_transforms, resumes.clone(), @@ -4539,6 +4693,9 @@ async fn push_next_flow_job( let ti = transform_input( args, flow_env, + env_refs, + // A `flow` module is never a simple loop body. + false, arc_last_job_result.clone(), input_transforms, resumes.clone(), @@ -4594,6 +4751,8 @@ async fn push_next_flow_job( let ti = transform_input( Marc::new(hm), flow_env, + env_refs, + false, arc_last_job_result.clone(), input_transforms, resumes.clone(), @@ -6713,7 +6872,40 @@ pub async fn get_previous_job_result( mod tests { use super::{ effective_flow_step_timeout, extract_chat_message_from_flow_result, is_simple_modules, + FlowEnvSecrets, }; + + // An input computed from a secret is stored encrypted, which an agent worker cannot + // decrypt, so a key that merely shares a secret entry's name must not count as reading it. + #[test] + fn reads_secret_only_through_a_flow_env_access() { + let refs = FlowEnvSecrets { + entries: [( + "token".to_string(), + to_raw_value(&json!("$var:u/x/t")).unwrap(), + )] + .into(), + crypt: None, + }; + for expr in [ + "flow_env.token", + "\"Bearer \" + flow_env.token", + "`${flow_env?.token}`", + "flow_env[\"token\"] + 'x'", + "flow_env['token']", + ] { + assert!(refs.reads_secret(expr), "{expr}"); + } + for expr in [ + "flow_env.BASE + results.login.token", + "flow_env.tokens", + "flow_input.token", + "my_flow_env.token", + "flow_env[\"tokens\"]", + ] { + assert!(!refs.reads_secret(expr), "{expr}"); + } + } use serde_json::{json, value::to_raw_value}; use windmill_common::flows::FlowModule;