fix: enforce scripts:read token scope on raw/h and deployment_status/h

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Ruben Fiszel
2026-11-19 10:30:12 +01:00
co-authored by Claude Opus 5.5
parent 2b8a0790a0
commit 36c2fbe90b
3 changed files with 85 additions and 9 deletions
@@ -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"
}
+65
View File
@@ -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:<path>` 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<Postgres>) -> 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(())
}
+10 -5
View File
@@ -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:<path>`
// 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<DB>,
Path((w_id, hash_str)): Path<(String, String)>,
Extension(authed): Extension<ApiAuthed>,
) -> Result<String> {
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<DB>,
Path((w_id, hash)): Path<(String, ScriptHash)>,
Extension(authed): Extension<ApiAuthed>,
) -> JsonResult<DeploymentStatus> {
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,