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 <noreply@anthropic.com>
This commit is contained in:
Ruben Fiszel
2026-11-08 21:57:55 +01:00
co-authored by Claude Opus 5.5
parent 5d3f239bf2
commit a0818b60d3
2 changed files with 29 additions and 2 deletions
@@ -1032,5 +1032,29 @@ async fn test_list_search_scope_filtering(db: Pool<Postgres>) -> 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::<serde_json::Value>()
.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(())
}
+5 -2
View File
@@ -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<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(|| {
@@ -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)
}