diff --git a/backend/.sqlx/query-513ed713afdbafb587026d1536c47a9bbaa6967e36777454746b8817d68219a5.json b/backend/.sqlx/query-3a85abf71e6b3008f92528bd43b9bb8113b163a635b5480911b7aab625d892b9.json similarity index 53% rename from backend/.sqlx/query-513ed713afdbafb587026d1536c47a9bbaa6967e36777454746b8817d68219a5.json rename to backend/.sqlx/query-3a85abf71e6b3008f92528bd43b9bb8113b163a635b5480911b7aab625d892b9.json index 31187af03a..590e610cc6 100644 --- a/backend/.sqlx/query-513ed713afdbafb587026d1536c47a9bbaa6967e36777454746b8817d68219a5.json +++ b/backend/.sqlx/query-3a85abf71e6b3008f92528bd43b9bb8113b163a635b5480911b7aab625d892b9.json @@ -1,20 +1,25 @@ { "db_name": "PostgreSQL", - "query": "SELECT s.lock, s.lock_error_logs, dm.job_id\n FROM script s\n LEFT JOIN deployment_metadata dm ON s.hash = dm.script_hash AND s.workspace_id = dm.workspace_id\n WHERE s.hash = $1 AND s.workspace_id = $2", + "query": "SELECT s.path, s.lock, s.lock_error_logs, dm.job_id\n FROM script s\n LEFT JOIN deployment_metadata dm ON s.hash = dm.script_hash AND s.workspace_id = dm.workspace_id\n WHERE s.hash = $1 AND s.workspace_id = $2", "describe": { "columns": [ { "ordinal": 0, + "name": "path", + "type_info": "Varchar" + }, + { + "ordinal": 1, "name": "lock", "type_info": "Text" }, { - "ordinal": 1, + "ordinal": 2, "name": "lock_error_logs", "type_info": "Text" }, { - "ordinal": 2, + "ordinal": 3, "name": "job_id", "type_info": "Uuid" } @@ -26,10 +31,11 @@ ] }, "nullable": [ + false, true, true, true ] }, - "hash": "513ed713afdbafb587026d1536c47a9bbaa6967e36777454746b8817d68219a5" + "hash": "3a85abf71e6b3008f92528bd43b9bb8113b163a635b5480911b7aab625d892b9" } diff --git a/backend/tests/scripts_by_hash_scope.rs b/backend/tests/scripts_by_hash_scope.rs new file mode 100644 index 0000000000..8a9c91369d --- /dev/null +++ b/backend/tests/scripts_by_hash_scope.rs @@ -0,0 +1,65 @@ +//! The by-hash script reads must honor a path-scoped token: the route layer only checks +//! the scope domain, so each handler checks `scripts:read:` once it knows the path. + +use sqlx::{Pool, Postgres}; +use windmill_test_utils::*; + +#[sqlx::test(fixtures("base"))] +async fn test_by_hash_reads_enforce_token_path_scope(db: Pool) -> anyhow::Result<()> { + initialize_tracing().await; + + for (path, hash) in [ + ("f/a/in_scope", 7_200_001_i64), + ("f/b/out_of_scope", 7_200_002_i64), + ] { + sqlx::query( + "INSERT INTO script(workspace_id, created_by, content, schema, summary, description, path, hash, language) + VALUES ('test-workspace', 'test-user', 'export function main() {}', '{}', '', '', $1, $2, 'bun')", + ) + .bind(path) + .bind(hash) + .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['scripts:read:f/a/*'])", + ) + .execute(&db) + .await?; + + let server = ApiServer::start(db.clone()).await?; + let base = format!( + "http://localhost:{}/api/w/test-workspace/scripts", + server.addr.port() + ); + let client = reqwest::Client::new(); + let status = |url: String| { + let client = client.clone(); + async move { + anyhow::Ok( + client + .get(url) + .bearer_auth("SCOPED_TOKEN") + .send() + .await? + .status() + .as_u16(), + ) + } + }; + + let in_hash = format!("{:016x}", 7_200_001_i64); + let out_hash = format!("{:016x}", 7_200_002_i64); + for endpoint in ["raw/h/{}.ts", "deployment_status/h/{}"] { + let url = |h: &str| format!("{base}/{}", endpoint.replace("{}", h)); + assert_eq!(status(url(&in_hash)).await?, 200, "{endpoint} in scope"); + assert_eq!( + status(url(&out_hash)).await?, + 403, + "{endpoint} out of scope" + ); + } + Ok(()) +} diff --git a/backend/windmill-api-scripts/src/scripts.rs b/backend/windmill-api-scripts/src/scripts.rs index ce45c05bf6..b3dc7cd1f2 100644 --- a/backend/windmill-api-scripts/src/scripts.rs +++ b/backend/windmill-api-scripts/src/scripts.rs @@ -124,9 +124,10 @@ pub fn workspaced_service() -> Router { .route("/delete/p/{*path}", post(delete_script_by_path)) .route("/delete_bulk", delete(delete_scripts_bulk)) // By hash, a script is read without the folder/path ACL that `/get/p` and `/raw/p` - // apply (`get/h` only does with `?authed=true`), and `raw/h` also skips the token-scope - // check. Ideally these would be authorized too, but the hash acts as the secret: a member - // reaching one (e.g. via a flow they can read) is by design, not a vulnerability. + // apply (`get/h` only does with `?authed=true`); only a token's `scripts:read:` + // scope is enforced. Ideally these would be authorized too, but the hash acts as the + // secret: a member reaching one (e.g. via a flow they can read) is by design, not a + // vulnerability. .route("/get/h/{hash}", get(get_script_by_hash)) .route("/raw/h/{hash}", get(raw_script_by_hash)) .route("/deployment_status/h/{hash}", get(get_deployment_status)) @@ -1732,7 +1733,7 @@ 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 + // through `raw/h/{hash}`, which applies 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", @@ -4103,6 +4104,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(|| { @@ -4110,6 +4112,7 @@ 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) } @@ -4123,10 +4126,11 @@ struct DeploymentStatus { async fn get_deployment_status( Extension(db): Extension, Path((w_id, hash)): Path<(String, ScriptHash)>, + Extension(authed): Extension, ) -> JsonResult { let mut tx = db.begin().await?; let status_o = sqlx::query!( - "SELECT s.lock, s.lock_error_logs, dm.job_id + "SELECT s.path, s.lock, s.lock_error_logs, dm.job_id FROM script s LEFT JOIN deployment_metadata dm ON s.hash = dm.script_hash AND s.workspace_id = dm.workspace_id WHERE s.hash = $1 AND s.workspace_id = $2", @@ -4137,6 +4141,7 @@ async fn get_deployment_status( .await?; let status = not_found_if_none(status_o, "DeploymentStatus", hash.to_string())?; + check_scopes(&authed, || format!("scripts:read:{}", status.path))?; let deployment_status = DeploymentStatus { lock: status.lock,