From a0818b60d31fcdf046a3a597756d29c2311dcb37 Mon Sep 17 00:00:00 2001 From: Ruben Fiszel Date: Sun, 8 Nov 2026 21:57:55 +0100 Subject: [PATCH] fix: enforce scripts:read path scope on raw script-by-hash endpoint GET /w/{workspace}/scripts/raw/h/{hash} returned a script's source without checking the token's path scope, so a token scoped to scripts:read:f/a/* could read any script by hash. Check the script's path like get/h does. Co-Authored-By: Claude Opus 5.5 --- .../tests/scripts.rs | 24 +++++++++++++++++++ backend/windmill-api-scripts/src/scripts.rs | 7 ++++-- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/backend/windmill-api-integration-tests/tests/scripts.rs b/backend/windmill-api-integration-tests/tests/scripts.rs index d6afc9f302..7786bee903 100644 --- a/backend/windmill-api-integration-tests/tests/scripts.rs +++ b/backend/windmill-api-integration-tests/tests/scripts.rs @@ -1032,5 +1032,29 @@ async fn test_list_search_scope_filtering(db: Pool) -> anyhow::Result< assert!(unscoped.contains(&"f/allowed/foo".to_string())); assert!(unscoped.contains(&"f/private/bar".to_string())); + // `raw/h` reads content by hash alone, so it must check the script's path against the + // token's scope like its JSON twin `get/h` does. + for (path, expected) in [("f/allowed/foo", 200), ("f/private/bar", 403)] { + let hash = authed_get(port, "get/p", path) + .await + .json::() + .await?["hash"] + .as_str() + .unwrap() + .to_string(); + let resp = client() + .get(script_url(port, "raw/h", &format!("{hash}.ts"))) + .header("Authorization", "Bearer SCOPED_TOKEN") + .send() + .await + .unwrap(); + assert_eq!( + resp.status(), + expected, + "raw/h for {path}: {}", + resp.text().await? + ); + } + Ok(()) } diff --git a/backend/windmill-api-scripts/src/scripts.rs b/backend/windmill-api-scripts/src/scripts.rs index 61d83e76fe..15f286e226 100644 --- a/backend/windmill-api-scripts/src/scripts.rs +++ b/backend/windmill-api-scripts/src/scripts.rs @@ -1714,8 +1714,8 @@ async fn create_script_internal<'c>( if let Some(clashing_hash) = clashing_hash_o { // Named only when the caller could already read it. The probe above has to be // unscoped to be correct, but a hash alone reads a script's content back - // through `raw/h/{hash}`, which authorizes nothing per script — so echoing one - // the caller cannot see hands them a way to fetch it. + // through `raw/h/{hash}`, which checks the token's scope but no per-script ACL, + // so echoing one the caller cannot see hands them a way to fetch it. let visible_to_caller = sqlx::query_scalar!( "SELECT 1 FROM script WHERE hash = $1 AND workspace_id = $2", clashing_hash, @@ -4048,6 +4048,7 @@ async fn get_script_by_hash( async fn raw_script_by_hash( Extension(db): Extension, Path((w_id, hash_str)): Path<(String, String)>, + Extension(authed): Extension, ) -> Result { let mut tx = db.begin().await?; let hash = ScriptHash(to_i64(hash_str.strip_suffix(".ts").ok_or_else(|| { @@ -4056,6 +4057,8 @@ async fn raw_script_by_hash( let r = get_script_by_hash_internal(&mut tx, &w_id, &hash, None).await?; tx.commit().await?; + check_scopes(&authed, || format!("scripts:read:{}", &r.script.path))?; + Ok(r.script.content) }