mirror of
https://github.com/windmill-labs/windmill.git
synced 2026-12-03 16:10:37 +00:00
fix: enforce token path scopes on flows and scripts list_paths
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
86651b4fb7
commit
c253a4fffd
@@ -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<Vec<String>> {
|
||||
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<String> = resp.json().await?;
|
||||
paths.sort();
|
||||
Ok(paths)
|
||||
}
|
||||
|
||||
#[sqlx::test(fixtures("base"))]
|
||||
async fn test_list_paths_filters_by_token_scope(db: Pool<Postgres>) -> 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(())
|
||||
}
|
||||
@@ -354,12 +354,16 @@ async fn list_paths(
|
||||
) -> JsonResult<Vec<String>> {
|
||||
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::<Vec<_>>();
|
||||
tx.commit().await?;
|
||||
|
||||
Ok(Json(flows))
|
||||
|
||||
@@ -3562,12 +3562,16 @@ async fn list_paths(
|
||||
) -> JsonResult<Vec<String>> {
|
||||
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::<Vec<_>>();
|
||||
tx.commit().await?;
|
||||
|
||||
Ok(Json(scripts))
|
||||
|
||||
Reference in New Issue
Block a user