diff --git a/backend/tests/list_paths_scope.rs b/backend/tests/list_paths_scope.rs new file mode 100644 index 0000000000..3f84f1c0b0 --- /dev/null +++ b/backend/tests/list_paths_scope.rs @@ -0,0 +1,78 @@ +//! `GET /flows/list_paths` and `GET /scripts/list_paths` must honor a path-scoped token: +//! RLS knows nothing of token scopes, so the handlers filter by them. + +use sqlx::{Pool, Postgres}; +use windmill_test_utils::*; + +async fn list_paths(port: u16, kind: &str, token: &str) -> anyhow::Result> { + let resp = reqwest::Client::new() + .get(format!( + "http://localhost:{port}/api/w/test-workspace/{kind}/list_paths" + )) + .header("Authorization", format!("Bearer {token}")) + .send() + .await?; + assert_eq!( + resp.status(), + 200, + "{kind}/list_paths: {}", + resp.text().await? + ); + let mut paths: Vec = resp.json().await?; + paths.sort(); + Ok(paths) +} + +#[sqlx::test(fixtures("base"))] +async fn test_list_paths_filters_by_token_scope(db: Pool) -> anyhow::Result<()> { + initialize_tracing().await; + + for (hash, path) in [(9100001_i64, "f/allowed/s"), (9100002, "f/other/s")] { + sqlx::query( + "INSERT INTO script (workspace_id, created_by, content, schema, summary, description, path, hash, language, lock) + VALUES ('test-workspace', 'test-user', 'echo', '{}', '', '', $1, $2, 'bash', '')", + ) + .bind(path) + .bind(hash) + .execute(&db) + .await?; + } + for path in ["f/allowed/f", "f/other/f"] { + sqlx::query( + "INSERT INTO flow (workspace_id, summary, description, path, versions, schema, value, edited_by) + VALUES ('test-workspace', '', '', $1, '{}', '{}', '{\"modules\": []}', 'test-user')", + ) + .bind(path) + .execute(&db) + .await?; + } + sqlx::query( + "INSERT INTO token (token_hash, token_prefix, token, email, label, scopes) + VALUES (encode(sha256('SCOPED_TOKEN'::bytea), 'hex'), 'SCOPED_TOK', 'SCOPED_TOKEN', + 'test@windmill.dev', 'scoped', + ARRAY['flows:read:f/allowed/*', 'scripts:read:f/allowed/*'])", + ) + .execute(&db) + .await?; + + let server = ApiServer::start(db.clone()).await?; + let port = server.addr.port(); + + assert_eq!( + list_paths(port, "flows", "SECRET_TOKEN").await?, + ["f/allowed/f", "f/other/f"] + ); + assert_eq!( + list_paths(port, "scripts", "SECRET_TOKEN").await?, + ["f/allowed/s", "f/other/s"] + ); + assert_eq!( + list_paths(port, "flows", "SCOPED_TOKEN").await?, + ["f/allowed/f"] + ); + assert_eq!( + list_paths(port, "scripts", "SCOPED_TOKEN").await?, + ["f/allowed/s"] + ); + Ok(()) +} diff --git a/backend/windmill-api-flows/src/flows.rs b/backend/windmill-api-flows/src/flows.rs index 01cdfd155d..e0eabea353 100644 --- a/backend/windmill-api-flows/src/flows.rs +++ b/backend/windmill-api-flows/src/flows.rs @@ -354,12 +354,16 @@ async fn list_paths( ) -> JsonResult> { let mut tx = user_db.begin(&authed).await?; + let allowed = build_scope_path_predicate(&authed, "flows", "read"); let flows = sqlx::query_scalar!( "SELECT distinct(path) FROM flow WHERE workspace_id = $1", w_id ) .fetch_all(&mut *tx) - .await?; + .await? + .into_iter() + .filter(|p| allowed(p)) + .collect::>(); tx.commit().await?; Ok(Json(flows)) diff --git a/backend/windmill-api-scripts/src/scripts.rs b/backend/windmill-api-scripts/src/scripts.rs index 1102db5fe1..9f08faaf1b 100644 --- a/backend/windmill-api-scripts/src/scripts.rs +++ b/backend/windmill-api-scripts/src/scripts.rs @@ -3562,12 +3562,16 @@ async fn list_paths( ) -> JsonResult> { let mut tx = user_db.begin(&authed).await?; + let allowed = build_scope_path_predicate(&authed, "scripts", "read"); let scripts = sqlx::query_scalar!( "SELECT distinct(path) FROM script WHERE workspace_id = $1", w_id ) .fetch_all(&mut *tx) - .await?; + .await? + .into_iter() + .filter(|p| allowed(p)) + .collect::>(); tx.commit().await?; Ok(Json(scripts))