diff --git a/backend/tests/apps_runnable_paths_scope.rs b/backend/tests/apps_runnable_paths_scope.rs new file mode 100644 index 0000000000..103ce679d2 --- /dev/null +++ b/backend/tests/apps_runnable_paths_scope.rs @@ -0,0 +1,62 @@ +//! `GET /apps/list_paths_from_workspace_runnable` must honor a path-scoped token. The +//! route layer only checks the scope domain and RLS knows nothing of token scopes, so +//! the per-path filter lives in the handler. + +use sqlx::{Pool, Postgres}; +use windmill_test_utils::*; + +async fn list_paths(port: u16, token: &str) -> anyhow::Result> { + let resp = reqwest::Client::new() + .get(format!( + "http://localhost:{port}/api/w/test-workspace/apps/list_paths_from_workspace_runnable/script/u/test-user/s" + )) + .header("Authorization", format!("Bearer {token}")) + .send() + .await?; + assert_eq!(resp.status(), 200, "list: {}", resp.text().await?); + let mut paths: Vec = resp.json().await?; + paths.sort(); + Ok(paths) +} + +#[sqlx::test(fixtures("base"))] +async fn test_runnable_app_paths_filtered_by_token_scope(db: Pool) -> anyhow::Result<()> { + initialize_tracing().await; + + for (id, path) in [(999801i64, "u/test-user/a"), (999802, "u/test-user-2/b")] { + sqlx::query( + "INSERT INTO app (id, workspace_id, path, summary, policy, versions, extra_perms) + VALUES ($1, 'test-workspace', $2, '', '{}'::jsonb, '{}', + '{\"u/test-user\": true}'::jsonb)", + ) + .bind(id) + .bind(path) + .execute(&db) + .await?; + sqlx::query( + "INSERT INTO workspace_runnable_dependencies + (runnable_path, runnable_is_flow, workspace_id, app_path) + VALUES ('u/test-user/s', false, 'test-workspace', $1)", + ) + .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['apps:read:u/test-user/*'])", + ) + .execute(&db) + .await?; + + let server = ApiServer::start(db.clone()).await?; + let port = server.addr.port(); + + assert_eq!( + list_paths(port, "SECRET_TOKEN").await?, + ["u/test-user-2/b", "u/test-user/a"] + ); + assert_eq!(list_paths(port, "SCOPED_TOKEN").await?, ["u/test-user/a"]); + Ok(()) +} diff --git a/backend/windmill-api/src/apps.rs b/backend/windmill-api/src/apps.rs index cbf8552495..9b3851fbc0 100644 --- a/backend/windmill-api/src/apps.rs +++ b/backend/windmill-api/src/apps.rs @@ -2896,9 +2896,9 @@ async fn list_paths_from_workspace_runnable( Path((w_id, runnable_kind, path)): Path<(String, RunnableKind, StripPath)>, ) -> JsonResult> { let mut tx = user_db.begin(&authed).await?; - let runnables = sqlx::query_scalar!( + let mut runnables = sqlx::query_scalar!( r#"SELECT a.path - FROM workspace_runnable_dependencies wru + FROM workspace_runnable_dependencies wru JOIN app a ON wru.app_path = a.path AND wru.workspace_id = a.workspace_id WHERE wru.runnable_path = $1 AND wru.runnable_is_flow = $2 AND wru.workspace_id = $3"#, @@ -2909,6 +2909,8 @@ async fn list_paths_from_workspace_runnable( .fetch_all(&mut *tx) .await?; tx.commit().await?; + let allowed = build_scope_path_predicate(&authed, "apps", "read"); + runnables.retain(|p| allowed(p)); Ok(Json(runnables)) }