mirror of
https://github.com/windmill-labs/windmill.git
synced 2026-11-29 08:06:46 +00:00
fix: enforce token path scopes on path listing and exists endpoints
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
a3a4fa5038
commit
6e03d826fb
@@ -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<DB>,
|
||||
Path((w_id, path)): Path<(String, StripPath)>,
|
||||
) -> JsonResult<bool> {
|
||||
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)",
|
||||
|
||||
@@ -1034,3 +1034,56 @@ async fn test_list_search_scope_filtering(db: Pool<Postgres>) -> 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<Postgres>) -> 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::<Vec<String>>().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(())
|
||||
}
|
||||
|
||||
@@ -1102,9 +1102,11 @@ async fn get_schedule(
|
||||
}
|
||||
|
||||
async fn exists_schedule(
|
||||
authed: ApiAuthed,
|
||||
Extension(db): Extension<DB>,
|
||||
Path((w_id, path)): Path<(String, StripPath)>,
|
||||
) -> JsonResult<bool> {
|
||||
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?;
|
||||
|
||||
@@ -643,6 +643,7 @@ async fn list_paths_from_workspace_runnable(
|
||||
Extension(user_db): Extension<UserDB>,
|
||||
Path((w_id, path)): Path<(String, StripPath)>,
|
||||
) -> JsonResult<Vec<String>> {
|
||||
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<DB>,
|
||||
Path((w_id, path)): Path<(String, StripPath)>,
|
||||
) -> JsonResult<bool> {
|
||||
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<CiTestResultWithPattern> 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<DB>,
|
||||
Path((w_id, kind, path)): Path<(String, String, StripPath)>,
|
||||
) -> JsonResult<Vec<CiTestResult>> {
|
||||
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<DB>,
|
||||
Path(w_id): Path<String>,
|
||||
Json(req): Json<CiTestBatchRequest>,
|
||||
@@ -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<String, std::collections::HashSet<String>> = 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()
|
||||
|
||||
@@ -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<DB>,
|
||||
Path((w_id, path)): Path<(String, StripPath)>,
|
||||
) -> JsonResult<bool> {
|
||||
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)
|
||||
|
||||
@@ -649,10 +649,15 @@ async fn get_resource(
|
||||
}
|
||||
|
||||
async fn exists_resource(
|
||||
authed: ApiAuthed,
|
||||
Extension(db): Extension<DB>,
|
||||
Path((w_id, path)): Path<(String, StripPath)>,
|
||||
) -> JsonResult<bool> {
|
||||
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)",
|
||||
|
||||
@@ -522,10 +522,12 @@ async fn explain_variable_perm_error(
|
||||
}
|
||||
|
||||
async fn exists_variable(
|
||||
authed: ApiAuthed,
|
||||
Extension(db): Extension<DB>,
|
||||
Path((w_id, path)): Path<(String, StripPath)>,
|
||||
) -> JsonResult<bool> {
|
||||
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)",
|
||||
|
||||
Reference in New Issue
Block a user