From 235087ae7fcd6fee64d446ca16014dff0dacb9c3 Mon Sep 17 00:00:00 2001 From: hugocasa Date: Fri, 27 Nov 2026 12:13:28 +0100 Subject: [PATCH] fix: read view-only folder grants in list_ws_specific_versions and agent test runs Co-Authored-By: Claude Opus 5.5 --- .../20261113143004_acl_read_level.down.sql | 6 ++++ .../20261113143004_acl_read_level.up.sql | 10 +++++++ backend/windmill-api/src/jobs.rs | 28 +++++++++++++++-- .../tests/ws_specific_versions.rs | 27 +++++++++++++++++ frontend/src/lib/utils.test.ts | 30 ++++++++++++++++++- 5 files changed, 98 insertions(+), 3 deletions(-) diff --git a/backend/migrations/20261113143004_acl_read_level.down.sql b/backend/migrations/20261113143004_acl_read_level.down.sql index a5da469c1a..d8cc152164 100644 --- a/backend/migrations/20261113143004_acl_read_level.down.sql +++ b/backend/migrations/20261113143004_acl_read_level.down.sql @@ -1,5 +1,11 @@ DROP FUNCTION acl_grants_run(JSONB, TEXT[]); +DO $$ +BEGIN + EXECUTE replace(pg_get_functiondef('list_ws_specific_versions'::regproc), + $r$t.value IN ('true', 'write')$r$, 't.value::boolean IS true'); +END $$; + -- Back to booleans, so the casts below hold: a view-only grant becomes a read grant, which runs. DO $$ DECLARE t TEXT; diff --git a/backend/migrations/20261113143004_acl_read_level.up.sql b/backend/migrations/20261113143004_acl_read_level.up.sql index d5e9d6765a..ba7c73d15a 100644 --- a/backend/migrations/20261113143004_acl_read_level.up.sql +++ b/backend/migrations/20261113143004_acl_read_level.up.sql @@ -35,6 +35,16 @@ BEGIN END LOOP; END $$; +-- Its folder write check is the same read of a grant: patched in place rather than restated whole. +DO $$ +DECLARE def TEXT := pg_get_functiondef('list_ws_specific_versions'::regproc); +BEGIN + IF position('t.value::boolean IS true' IN def) = 0 THEN + RAISE EXCEPTION 'list_ws_specific_versions no longer reads folder grants as expected'; + END IF; + EXECUTE replace(def, 't.value::boolean IS true', $r$t.value IN ('true', 'write')$r$); +END $$; + -- Whether any of `principals` holds a grant in `extra_perms` of a runnable that includes running. CREATE FUNCTION acl_grants_run(extra_perms JSONB, principals TEXT[]) RETURNS BOOLEAN LANGUAGE sql IMMUTABLE AS $$ diff --git a/backend/windmill-api/src/jobs.rs b/backend/windmill-api/src/jobs.rs index 050d6448e4..9f6b2ba221 100644 --- a/backend/windmill-api/src/jobs.rs +++ b/backend/windmill-api/src/jobs.rs @@ -10742,8 +10742,32 @@ async fn run_preview_flow_job( // jobs:run scope so a narrowly-scoped token cannot escape its scope. See run_preview_script. check_scopes(&authed, || format!("jobs:run"))?; require_path_read_access_for_preview(&authed, &raw_flow.path)?; - require_preview_label_runnable(&db, &authed, &w_id, RunnableAclKind::Flow, &raw_flow.path) - .await?; + // A saved agent's test run is this preview of its one-step flow, labelled `.chat`, as + // `job_runnables` recognizes it. + let agent_label = (raw_flow.value.modules.len() == 1 + && raw_flow.value.modules[0].id == "__wm_agent_root") + .then(|| { + raw_flow + .path + .as_deref() + .map(|p| p.strip_suffix(".chat").unwrap_or(p).to_string()) + }); + match agent_label { + Some(label) => { + require_preview_label_runnable(&db, &authed, &w_id, RunnableAclKind::Agent, &label) + .await? + } + None => { + require_preview_label_runnable( + &db, + &authed, + &w_id, + RunnableAclKind::Flow, + &raw_flow.path, + ) + .await? + } + } // Step restrictions apply to a preview as they do to a deployed run, so they are checked // the same way: an invalid entry or an older worker would leave a step unrestricted. if windmill_common::scopes::validate_flow_step_job_token_scopes(&raw_flow.value)? { diff --git a/backend/windmill-common/tests/ws_specific_versions.rs b/backend/windmill-common/tests/ws_specific_versions.rs index 23a5df3a92..1c20e1a7bd 100644 --- a/backend/windmill-common/tests/ws_specific_versions.rs +++ b/backend/windmill-common/tests/ws_specific_versions.rs @@ -64,3 +64,30 @@ async fn pairs_prod_and_dev_without_plain_forks(db: Pool) { assert_eq!(versions("lwd").await, vec!["lwd", "lwp"]); assert_eq!(versions("lwf").await, vec!["lwd", "lwf", "lwp"]); } + +/// A member's folder grants are read for each workspace of the chain, a view-only one included. +#[sqlx::test(migrations = "../migrations", fixtures("base"))] +async fn reads_a_view_only_folder_grant(db: Pool) { + insert_ws(&db, "lwp").await; + sqlx::query( + "INSERT INTO usr (workspace_id, email, username, is_admin, role) \ + VALUES ('lwp', 'test2@windmill.dev', 'test-user-2', false, 'User')", + ) + .execute(&db) + .await + .expect("add member"); + sqlx::query( + "INSERT INTO folder (workspace_id, name, display_name, owners, extra_perms) \ + VALUES ('lwp', 'x', 'x', '{}', '{\"u/test-user-2\": \"read\"}')", + ) + .execute(&db) + .await + .expect("add folder"); + + sqlx::query_scalar::<_, String>( + "SELECT ws FROM list_ws_specific_versions('lwp', 'test2@windmill.dev', 'resource', 'u/admin/shared')", + ) + .fetch_all(&db) + .await + .expect("call list_ws_specific_versions"); +} diff --git a/frontend/src/lib/utils.test.ts b/frontend/src/lib/utils.test.ts index 65cef7085c..194b9a5907 100644 --- a/frontend/src/lib/utils.test.ts +++ b/frontend/src/lib/utils.test.ts @@ -10,7 +10,8 @@ import { getQueryStmtCountHeuristic, isJobResolvable, parseDbInputFromAssetSyntax, - apiErrorMessage + apiErrorMessage, + canRun } from './utils' // Mirrors the backend invariant that only failed or canceled rows can carry a resolution; @@ -600,3 +601,30 @@ describe('apiErrorMessage', () => { expect(apiErrorMessage(new Error('boom'))).toBe('boom') }) }) + +// Mirrors `require_can_run`: run is the union of a caller's grants, and only `'read'` withholds it. +describe('canRun', () => { + const user = (o: object = {}) => + ({ + username: 'bob', + is_admin: false, + is_super_admin: false, + pgroups: ['g/ops'], + folders_read: ['vo'], + folders_view_only: ['vo'], + ...o + }) as any + it('withholds run when every grant is view-only', () => { + expect(canRun('f/vo/s', { 'u/bob': 'read' }, user())).toBe(false) + expect(canRun('f/vo/s', {}, user())).toBe(false) + }) + it('runs on any grant that runs, whichever folder grant there is', () => { + expect(canRun('f/vo/s', { 'g/ops': false }, user())).toBe(true) + expect(canRun('f/vo/s', { 'u/bob': 'run' }, user())).toBe(true) + expect(canRun('f/vo/s', {}, user({ folders_view_only: [] }))).toBe(true) + }) + it('runs what the user or their group owns', () => { + expect(canRun('u/bob/s', {}, user())).toBe(true) + expect(canRun('g/ops/s', {}, user())).toBe(true) + }) +})