mirror of
https://github.com/windmill-labs/windmill.git
synced 2026-11-17 00:20:51 +00:00
fix: keep secret values out of stored step inputs from flow_env (#12130)
* fix: keep secret values out of stored step inputs reached through flow_env Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix: pass secret flow_env entries to subflow steps as references Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * test: cover a subflow input computed from a secret flow_env entry Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix: detect a secret flow_env read only through a flow_env access Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix: pass a subflow input computed from a secret flow_env entry encrypted Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * perf: load flow_env secrets only for an input that reads flow_env Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
b5d40c3e31
commit
37b1ea23f9
+23
@@ -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"
|
||||
}
|
||||
@@ -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<Postgres>) -> 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(())
|
||||
}
|
||||
@@ -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.<key>` read) and build the value inside the script"
|
||||
))),
|
||||
}
|
||||
|
||||
// let path = y.strip_prefix("$res:").unwrap();
|
||||
|
||||
@@ -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<String, Box<RawValue>>,
|
||||
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<FlowEnvSecrets>,
|
||||
}
|
||||
|
||||
struct FlowEnvSecrets {
|
||||
entries: HashMap<String, Box<RawValue>>,
|
||||
// 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<WorkspaceCrypt>,
|
||||
}
|
||||
|
||||
impl<'a> FlowEnvRefs<'a> {
|
||||
fn new(db: &'a DB, flow_job: &MiniPulledJob, raw: &'a HashMap<String, Box<RawValue>>) -> 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<FlowEnvSecrets> {
|
||||
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::<String>(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<String> = 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<String, Box<RawValue>> = 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<HashMap<String, Box<RawValue>>>,
|
||||
flow_env: Option<&HashMap<String, Box<RawValue>>>,
|
||||
env_refs: Option<&FlowEnvRefs<'_>>,
|
||||
into_subflow: bool,
|
||||
last_result: Arc<Box<RawValue>>,
|
||||
input_transforms: &HashMap<String, InputTransform>,
|
||||
resumes: Arc<Box<RawValue>>,
|
||||
@@ -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<String, Box<RawValue>>>,
|
||||
env_refs: Option<&FlowEnvRefs<'_>>,
|
||||
// Neither this flow nor any ancestor defines `flow_env`.
|
||||
no_flow_env: bool,
|
||||
db: &sqlx::Pool<sqlx::Postgres>,
|
||||
@@ -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;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user