From 6e03d826fbf689ef275ecbd99aea5673ca0a917b Mon Sep 17 00:00:00 2001 From: Ruben Fiszel Date: Wed, 21 Oct 2026 12:12:34 +0200 Subject: [PATCH] fix: enforce token path scopes on path listing and exists endpoints Co-Authored-By: Claude Opus 5.5 --- backend/windmill-api-flows/src/flows.rs | 5 +- .../tests/scripts.rs | 53 +++++++++++++++++++ backend/windmill-api-schedule/src/lib.rs | 2 + backend/windmill-api-scripts/src/scripts.rs | 33 ++++++++++-- backend/windmill-api/src/apps.rs | 5 +- backend/windmill-store/src/resources.rs | 5 ++ backend/windmill-store/src/variables.rs | 2 + 7 files changed, 100 insertions(+), 5 deletions(-) diff --git a/backend/windmill-api-flows/src/flows.rs b/backend/windmill-api-flows/src/flows.rs index fe4d8c37d4..f1bb00c241 100644 --- a/backend/windmill-api-flows/src/flows.rs +++ b/backend/windmill-api-flows/src/flows.rs @@ -354,7 +354,8 @@ async fn list_paths( .await?; tx.commit().await?; - Ok(Json(flows)) + let allowed = build_scope_path_predicate(&authed, "flows", "read"); + Ok(Json(flows.into_iter().filter(|p| allowed(p)).collect())) } pub async fn get_hub_flow_by_id( @@ -2026,10 +2027,12 @@ async fn get_flow_by_path( } async fn exists_flow_by_path( + authed: ApiAuthed, Extension(db): Extension, Path((w_id, path)): Path<(String, StripPath)>, ) -> JsonResult { let path = path.to_path(); + check_scopes(&authed, || format!("flows:read:{}", path))?; let exists = sqlx::query_scalar!( "SELECT EXISTS(SELECT 1 FROM flow WHERE path = $1 AND workspace_id = $2)", diff --git a/backend/windmill-api-integration-tests/tests/scripts.rs b/backend/windmill-api-integration-tests/tests/scripts.rs index d6afc9f302..d7c8f5ca95 100644 --- a/backend/windmill-api-integration-tests/tests/scripts.rs +++ b/backend/windmill-api-integration-tests/tests/scripts.rs @@ -1034,3 +1034,56 @@ async fn test_list_search_scope_filtering(db: Pool) -> anyhow::Result< Ok(()) } + +/// Path-listing and existence routes return paths or probe them without going +/// through `list_search`, so they must apply the path part of a token's scope too. +#[sqlx::test(migrations = "../migrations", fixtures("base"))] +async fn test_path_listing_and_exists_scope_filtering(db: Pool) -> anyhow::Result<()> { + initialize_tracing().await; + let server = ApiServer::start(db.clone()).await?; + let port = server.addr.port(); + let api = format!("http://localhost:{port}/api/w/test-workspace"); + + for folder in ["a", "b"] { + let resp = authed(client().post(format!("{api}/folders/create"))) + .json(&json!({ "name": folder })) + .send() + .await?; + assert_eq!(resp.status(), 200, "create folder: {}", resp.text().await?); + } + for path in ["f/a/foo", "f/b/bar"] { + let resp = authed(client().post(format!("{api}/scripts/create"))) + .json(&new_script(path, "", "export async function main() {}")) + .send() + .await?; + assert_eq!(resp.status(), 201, "create {path}: {}", resp.text().await?); + } + + // A token string distinct from the other tests': the auth cache is process-wide. + sqlx::query( + "INSERT INTO token (token_hash, token_prefix, token, email, label, super_admin, scopes) VALUES + (encode(sha256('PATH_SCOPED_TOKEN'::bytea), 'hex'), 'PATH_SCOPE', 'PATH_SCOPED_TOKEN', 'test@windmill.dev', 'scoped', true, ARRAY['scripts:read:f/a/*', 'variables:read:f/a/*'])", + ) + .execute(&db) + .await?; + let get = |url: String| { + client() + .get(url) + .header("Authorization", "Bearer PATH_SCOPED_TOKEN") + .send() + }; + + let resp = get(format!("{api}/scripts/list_paths")).await?; + assert_eq!(resp.status(), 200); + assert_eq!( + resp.json::>().await?, + vec!["f/a/foo".to_string()] + ); + + let resp = get(format!("{api}/variables/exists/f/a/x")).await?; + assert_eq!(resp.status(), 200, "{}", resp.text().await?); + let resp = get(format!("{api}/variables/exists/f/b/x")).await?; + assert_eq!(resp.status(), 403, "{}", resp.text().await?); + + Ok(()) +} diff --git a/backend/windmill-api-schedule/src/lib.rs b/backend/windmill-api-schedule/src/lib.rs index fb935e0b5c..3515f82b79 100644 --- a/backend/windmill-api-schedule/src/lib.rs +++ b/backend/windmill-api-schedule/src/lib.rs @@ -1102,9 +1102,11 @@ async fn get_schedule( } async fn exists_schedule( + authed: ApiAuthed, Extension(db): Extension, Path((w_id, path)): Path<(String, StripPath)>, ) -> JsonResult { + check_scopes(&authed, || format!("schedules:read:{}", path.to_path()))?; let mut tx = db.begin().await?; let res = windmill_queue::schedule::exists_schedule(&mut tx, w_id, path).await?; tx.commit().await?; diff --git a/backend/windmill-api-scripts/src/scripts.rs b/backend/windmill-api-scripts/src/scripts.rs index 4bbe372254..a7c869f9d6 100644 --- a/backend/windmill-api-scripts/src/scripts.rs +++ b/backend/windmill-api-scripts/src/scripts.rs @@ -643,6 +643,7 @@ async fn list_paths_from_workspace_runnable( Extension(user_db): Extension, Path((w_id, path)): Path<(String, StripPath)>, ) -> JsonResult> { + check_scopes(&authed, || format!("scripts:read:{}", path.to_path()))?; let mut tx = user_db.begin(&authed).await?; let runnables = sqlx::query_scalar!( r#"SELECT importer_path FROM dependency_map @@ -3488,7 +3489,8 @@ async fn list_paths( .await?; tx.commit().await?; - Ok(Json(scripts)) + let allowed = build_scope_path_predicate(&authed, "scripts", "read"); + Ok(Json(scripts.into_iter().filter(|p| allowed(p)).collect())) } #[derive(Deserialize)] @@ -3936,10 +3938,12 @@ async fn raw_script_by_path_internal( } async fn exists_script_by_path( + authed: ApiAuthed, Extension(db): Extension, Path((w_id, path)): Path<(String, StripPath)>, ) -> JsonResult { let path = path.to_path(); + check_scopes(&authed, || format!("scripts:read:{}", path))?; let exists = sqlx::query_scalar!( "SELECT EXISTS(SELECT 1 FROM script WHERE path = $1 AND workspace_id = $2 AND archived = false ORDER BY created_at DESC LIMIT 1)", @@ -4752,8 +4756,10 @@ async fn list_dedicated_with_deps( tx.commit().await?; + let allowed = build_scope_path_predicate(&authed, "scripts", "read"); let result = rows .into_iter() + .filter(|row| allowed(&row.path)) .map(|row| { let dep_names = windmill_common::scripts::extract_workspace_dependencies_annotated_refs( @@ -4926,12 +4932,24 @@ impl From for CiTestResult { } } +/// The scope domain a CI test result's tested item is read under. +fn ci_tested_item_scope_domain(kind: &str) -> &'static str { + if kind == "flow" { + "flows" + } else { + "scripts" + } +} + async fn get_ci_test_results( - _authed: ApiAuthed, + authed: ApiAuthed, Extension(db): Extension, Path((w_id, kind, path)): Path<(String, String, StripPath)>, ) -> JsonResult> { let path = path.to_path(); + check_scopes(&authed, || { + format!("{}:read:{}", ci_tested_item_scope_domain(&kind), path) + })?; let expected_trigger = format!("{}/{}", kind, path); // Exact matches — uses the primary ci_test_reference index. @@ -5019,7 +5037,7 @@ struct CiTestBatchRequest { } async fn get_ci_test_results_batch( - _authed: ApiAuthed, + authed: ApiAuthed, Extension(db): Extension, Path(w_id): Path, Json(req): Json, @@ -5040,7 +5058,16 @@ async fn get_ci_test_results_batch( // Group paths by kind so we can do one exact query + one wildcard query per distinct kind. // Dedupe via HashSet so duplicate items in the input don't push matches multiple times. let mut paths_by_kind: HashMap> = HashMap::new(); + let scripts_allowed = build_scope_path_predicate(&authed, "scripts", "read"); + let flows_allowed = build_scope_path_predicate(&authed, "flows", "read"); for item in &req.items { + let allowed = match ci_tested_item_scope_domain(&item.kind) { + "flows" => flows_allowed(&item.path), + _ => scripts_allowed(&item.path), + }; + if !allowed { + continue; + } paths_by_kind .entry(item.kind.clone()) .or_default() diff --git a/backend/windmill-api/src/apps.rs b/backend/windmill-api/src/apps.rs index 944535dd84..9b2b67df8b 100644 --- a/backend/windmill-api/src/apps.rs +++ b/backend/windmill-api/src/apps.rs @@ -6371,12 +6371,15 @@ async fn get_on_behalf_of(policy: &Policy, w_id: &str, db: &DB) -> Result<(Strin } async fn exists_app( + authed: ApiAuthed, Extension(db): Extension, Path((w_id, path)): Path<(String, StripPath)>, ) -> JsonResult { + let path = path.to_path(); + check_scopes(&authed, || format!("apps:read:{}", path))?; let exists = sqlx::query_scalar!( "SELECT EXISTS(SELECT 1 FROM app WHERE path = $1 AND workspace_id = $2)", - path.to_path(), + path, w_id ) .fetch_one(&db) diff --git a/backend/windmill-store/src/resources.rs b/backend/windmill-store/src/resources.rs index 010eb0a90b..24453ee17d 100644 --- a/backend/windmill-store/src/resources.rs +++ b/backend/windmill-store/src/resources.rs @@ -649,10 +649,15 @@ async fn get_resource( } async fn exists_resource( + authed: ApiAuthed, Extension(db): Extension, Path((w_id, path)): Path<(String, StripPath)>, ) -> JsonResult { let path = path.to_path(); + // `resources:run` (app embed tokens) reaches this metadata route but does not + // include `read`, so it has to be accepted here on its own. + check_scopes(&authed, || format!("resources:read:{}", path)) + .or_else(|e| check_scopes(&authed, || format!("resources:run:{}", path)).map_err(|_| e))?; let exists = sqlx::query_scalar!( "SELECT EXISTS(SELECT 1 FROM resource WHERE path = $1 AND workspace_id = $2)", diff --git a/backend/windmill-store/src/variables.rs b/backend/windmill-store/src/variables.rs index fca29736e6..a8722075e0 100644 --- a/backend/windmill-store/src/variables.rs +++ b/backend/windmill-store/src/variables.rs @@ -522,10 +522,12 @@ async fn explain_variable_perm_error( } async fn exists_variable( + authed: ApiAuthed, Extension(db): Extension, Path((w_id, path)): Path<(String, StripPath)>, ) -> JsonResult { let path = path.to_path(); + check_scopes(&authed, || format!("variables:read:{}", path))?; let exists = sqlx::query_scalar!( "SELECT EXISTS(SELECT 1 FROM variable WHERE path = $1 AND workspace_id = $2)",