fix: apply token path scope to apps that depend on a runnable

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Ruben Fiszel
2026-12-03 00:09:06 +01:00
co-authored by Claude Opus 5.5
parent 86651b4fb7
commit ee9d0d2473
2 changed files with 66 additions and 2 deletions
@@ -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<Vec<String>> {
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<String> = resp.json().await?;
paths.sort();
Ok(paths)
}
#[sqlx::test(fixtures("base"))]
async fn test_runnable_app_paths_filtered_by_token_scope(db: Pool<Postgres>) -> 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(())
}
+4 -2
View File
@@ -2896,9 +2896,9 @@ async fn list_paths_from_workspace_runnable(
Path((w_id, runnable_kind, path)): Path<(String, RunnableKind, StripPath)>,
) -> JsonResult<Vec<String>> {
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))
}