refactor: scope the minimum to the worker that runs a tag's jobs

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Ruben Fiszel
2026-10-18 06:14:49 +02:00
co-authored by Claude Opus 5.5
parent e2f4583ded
commit d92f0434df
4 changed files with 31 additions and 32 deletions
@@ -198,20 +198,17 @@ pub fn worker_allowed_tags(wc: &WorkerConfig, tags: &[String]) -> Vec<String> {
/// Why this worker must not run a job with `tag`, if it must not. Pulls already leave refused
/// tags out; this catches the jobs handed to a worker directly, like a same-worker flow's steps,
/// which cannot be left for another worker and so fail. A job that runs `in_process` (a native
/// language, a `//native` Bun script) gets no sandbox on any worker, so it counts as `None`.
pub fn worker_refusal(wc: &WorkerConfig, tag: &str, in_process: bool) -> Option<String> {
let mins = MIN_JOB_ISOLATION_BY_TAG.load();
let min = min_for_tag(tag, &mins)?;
if in_process {
return (min > IsolationLevel::None).then(|| {
format!(
"Tag {tag} requires job isolation {} but this job runs inside the worker process, without isolation",
min.as_str()
)
});
}
let own = worker_job_isolation(wc);
/// which cannot be left for another worker and so fail.
pub fn worker_refusal(wc: &WorkerConfig, tag: &str) -> Option<String> {
refusal(
tag,
worker_job_isolation(wc),
&MIN_JOB_ISOLATION_BY_TAG.load(),
)
}
fn refusal(tag: &str, own: IsolationLevel, mins: &MinJobIsolationByTag) -> Option<String> {
let min = min_for_tag(tag, mins)?;
(min > own).then(|| {
format!(
"Tag {tag} requires job isolation {} but this worker's isolation is {}",
@@ -303,6 +300,14 @@ mod tests {
assert_eq!(min("bashx"), None);
assert_eq!(min("python3"), None);
assert!(parse_min_job_isolation_by_tag(Some(serde_json::json!(["bash"]))).is_empty());
assert_eq!(refusal("bash-dev", IsolationLevel::Unshare, &mins), None);
assert_eq!(
refusal("bash-ws1", IsolationLevel::Unshare, &mins).as_deref(),
Some(
"Tag bash-ws1 requires job isolation nsjail but this worker's isolation is unshare"
)
);
assert_eq!(refusal("python3", IsolationLevel::None, &mins), None);
}
#[test]
@@ -2318,16 +2318,6 @@ pub async fn handle_bun_job(
None
};
if annotation.native {
if let Some(refusal) = windmill_common::min_job_isolation::worker_refusal(
&windmill_common::worker::WORKER_CONFIG.load(),
&job.tag,
true,
) {
return Err(error::Error::ExecutionErr(refusal));
}
}
if annotation.sandbox && NSJAIL_AVAILABLE.is_none() {
return Err(error::Error::ExecutionErr(
"Script has //sandbox annotation but nsjail is not available on this worker. \
+11 -7
View File
@@ -3846,8 +3846,14 @@ pub async fn run_worker(
// A job carrying a pre-run error never runs its code: it only has to be
// pulled so `handle_queued_job` can fail it. Both hand-off paths below
// dispatch by path and return before that check, so a job sent down them
// would run with whatever arguments survived the failure.
let fails_before_running = job.pre_run_error.is_some();
// would run with whatever arguments survived the failure. The same holds for
// a job whose tag this worker refuses, handed to it without a pull.
let fails_before_running = job.pre_run_error.is_some()
|| windmill_common::min_job_isolation::worker_refusal(
&WORKER_CONFIG.load(),
&job.tag,
)
.is_some();
// A dedicated worker or flow runner runs every job it gets with its own
// unscoped worker token, so a job with a restricted token runs here, with the
// token minted for it, whichever tag brought it.
@@ -4864,11 +4870,9 @@ pub async fn handle_queued_job(
}
}
if let Some(refusal) = windmill_common::min_job_isolation::worker_refusal(
&WORKER_CONFIG.load(),
&job.tag,
job.script_lang.is_some_and(|lang| lang.is_native()),
) {
if let Some(refusal) =
windmill_common::min_job_isolation::worker_refusal(&WORKER_CONFIG.load(), &job.tag)
{
return Err(Error::ExecutionErr(refusal));
}
@@ -441,7 +441,7 @@ export const settings: Record<string, Setting[]> = {
key: 'min_job_isolation_by_tag',
fieldType: 'min_job_isolation_by_tag',
description:
'Require a minimum isolation for the jobs of a tag (none &lt; unshare &lt; nsjail). A worker whose isolation is below a tag\'s minimum pulls no jobs for that tag: they stay queued until a sufficiently isolated worker picks them up. A minimum also covers the tags derived from it (per-workspace, fork and dev workspace tags): any tag starting with the tag followed by <code>-</code>. Native-mode and dedicated workers count as none, and jobs that run inside the worker process (native languages, <code>//native</code> Bun scripts) fail on a tag with a minimum.This guards against a misconfigured worker group, not against a tampered worker. <a href="https://www.windmill.dev/docs/advanced/security_isolation">Learn more</a>',
'Require a minimum isolation for the jobs of a tag (none &lt; unshare &lt; nsjail). A worker whose isolation is below a tag\'s minimum pulls no jobs for that tag: they stay queued until a sufficiently isolated worker picks them up. A minimum also covers the tags derived from it (per-workspace, fork and dev workspace tags): any tag starting with the tag followed by <code>-</code>. Native-mode and dedicated workers count as none. The minimum decides which workers may run a tag\'s jobs, not how a job runs: native languages, <code>//native</code> Bun scripts and squashed loops run outside the sandbox on any worker. This guards against a misconfigured worker group, not against a tampered worker. <a href="https://www.windmill.dev/docs/advanced/security_isolation">Learn more</a>',
storage: 'setting'
},
{