fix: keep view-only grants through copies, renames and restores

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
hugocasa
2026-11-13 17:05:43 +01:00
co-authored by Claude Opus 5.5
parent 634f54b7c2
commit b84ee03f25
25 changed files with 171 additions and 128 deletions
@@ -1,19 +0,0 @@
{
"db_name": "PostgreSQL",
"query": "\n INSERT INTO script\n (workspace_id, hash, path, parent_hashes, summary, description, content, created_by, schema, is_template, extra_perms, lock, language, kind, tag, envs, concurrent_limit, concurrency_time_window_s, cache_ttl, cache_ignore_s3_path, dedicated_worker, ws_error_handler_muted, priority, restart_unless_cancelled, delete_after_use, delete_after_secs, timeout, concurrency_key, visible_to_runner_only, auto_kind, codebase, has_preprocessor, on_behalf_of, on_behalf_of_email, schema_validation, assets, debounce_key, debounce_delay_s, runnable_settings_handle, modules, labels, job_token_scopes, lock_error_logs, created_at)\n\n SELECT workspace_id, $1, path, array_prepend($2::bigint, COALESCE(parent_hashes, '{}'::bigint[])), summary, description, content, created_by, schema, is_template, extra_perms, $4::text, language, kind, tag, envs, concurrent_limit, concurrency_time_window_s, cache_ttl, cache_ignore_s3_path, dedicated_worker, ws_error_handler_muted, priority, restart_unless_cancelled, delete_after_use, delete_after_secs, timeout, concurrency_key, visible_to_runner_only, auto_kind, codebase, has_preprocessor, on_behalf_of, on_behalf_of_email, schema_validation, assets, debounce_key, debounce_delay_s, runnable_settings_handle, COALESCE($5::jsonb, modules), labels, job_token_scopes, $6::text, clock_timestamp()\n\n FROM script WHERE hash = $2 AND workspace_id = $3;\n ",
"describe": {
"columns": [],
"parameters": {
"Left": [
"Int8",
"Int8",
"Text",
"Text",
"Jsonb",
"Text"
]
},
"nullable": []
},
"hash": "05b610ad1809d487c32e85b0392cfca8913ba1129fdf4978a8a4738689786160"
}
@@ -0,0 +1,19 @@
{
"db_name": "PostgreSQL",
"query": "\n INSERT INTO script\n (workspace_id, hash, path, parent_hashes, summary, description, content, created_by, schema, is_template, extra_perms, view_only, lock, language, kind, tag, envs, concurrent_limit, concurrency_time_window_s, cache_ttl, cache_ignore_s3_path, dedicated_worker, ws_error_handler_muted, priority, restart_unless_cancelled, delete_after_use, delete_after_secs, timeout, concurrency_key, visible_to_runner_only, auto_kind, codebase, has_preprocessor, on_behalf_of, on_behalf_of_email, schema_validation, assets, debounce_key, debounce_delay_s, runnable_settings_handle, modules, labels, job_token_scopes, lock_error_logs, created_at)\n\n SELECT workspace_id, $1, path, array_prepend($2::bigint, COALESCE(parent_hashes, '{}'::bigint[])), summary, description, content, created_by, schema, is_template, extra_perms, view_only, $4::text, language, kind, tag, envs, concurrent_limit, concurrency_time_window_s, cache_ttl, cache_ignore_s3_path, dedicated_worker, ws_error_handler_muted, priority, restart_unless_cancelled, delete_after_use, delete_after_secs, timeout, concurrency_key, visible_to_runner_only, auto_kind, codebase, has_preprocessor, on_behalf_of, on_behalf_of_email, schema_validation, assets, debounce_key, debounce_delay_s, runnable_settings_handle, COALESCE($5::jsonb, modules), labels, job_token_scopes, $6::text, clock_timestamp()\n\n FROM script WHERE hash = $2 AND workspace_id = $3;\n ",
"describe": {
"columns": [],
"parameters": {
"Left": [
"Int8",
"Int8",
"Text",
"Text",
"Jsonb",
"Text"
]
},
"nullable": []
},
"hash": "10d82ac4c6c42512110d44bb41f74be5dc49bd2765ac3a81961c9eaaa477e3a4"
}
@@ -1,6 +1,6 @@
{
"db_name": "PostgreSQL",
"query": "UPDATE flow SET extra_perms = extra_perms - ('u/' || $2) || jsonb_build_object(('u/' || $1), extra_perms->('u/' || $2)) WHERE extra_perms ? ('u/' || $2) AND workspace_id = $3",
"query": "UPDATE flow SET extra_perms = extra_perms - ('u/' || $2) || jsonb_build_object(('u/' || $1), extra_perms->('u/' || $2)), view_only = array_replace(view_only, 'u/' || $2, 'u/' || $1) WHERE extra_perms ? ('u/' || $2) AND workspace_id = $3",
"describe": {
"columns": [],
"parameters": {
@@ -12,5 +12,5 @@
},
"nullable": []
},
"hash": "70bad6ac45db6d03c59aa375f915ec55d1c07998b1bd0268725d34f7f0b98103"
"hash": "251c6463cc54705b09ecab2a155628da162490e15743c4bb9a0e8c205bee1bfa"
}
@@ -0,0 +1,26 @@
{
"db_name": "PostgreSQL",
"query": "WITH item AS (\n -- The newest version only: a script reusing a retired path starts with no grants,\n -- and the retired versions' must not run it.\n (SELECT extra_perms, view_only FROM script\n WHERE $5 = 'script' AND workspace_id = $1 AND path = $2\n ORDER BY created_at DESC LIMIT 1)\n UNION ALL SELECT extra_perms, view_only FROM flow\n WHERE $5 = 'flow' AND workspace_id = $1 AND path = $2\n UNION ALL SELECT extra_perms, view_only FROM resource\n WHERE $5 = 'resource' AND workspace_id = $1 AND path = $2\n )\n SELECT EXISTS (\n SELECT 1 FROM item WHERE acl_grants_run(item.extra_perms, item.view_only, $4)\n ) OR EXISTS (\n SELECT 1 FROM folder f\n WHERE f.workspace_id = $1 AND f.name = $3\n AND (f.owners::text[] && $4 OR acl_grants_run(f.extra_perms, f.view_only, $4))\n ) AS \"can_run!\"",
"describe": {
"columns": [
{
"ordinal": 0,
"name": "can_run!",
"type_info": "Bool"
}
],
"parameters": {
"Left": [
"Text",
"Text",
"Text",
"TextArray",
"Text"
]
},
"nullable": [
null
]
},
"hash": "2823efb1bd4f76d2887a08e6f53cfbdf9f4c3037766c287c6a2eaa9a03b4fab9"
}
@@ -1,26 +0,0 @@
{
"db_name": "PostgreSQL",
"query": "WITH item AS (\n SELECT extra_perms, view_only FROM script\n WHERE $5 = 'script' AND workspace_id = $1 AND path = $2\n UNION ALL SELECT extra_perms, view_only FROM flow\n WHERE $5 = 'flow' AND workspace_id = $1 AND path = $2\n UNION ALL SELECT extra_perms, view_only FROM resource\n WHERE $5 = 'resource' AND workspace_id = $1 AND path = $2\n )\n SELECT EXISTS (\n SELECT 1 FROM item WHERE acl_grants_run(item.extra_perms, item.view_only, $4)\n ) OR EXISTS (\n SELECT 1 FROM folder f\n WHERE f.workspace_id = $1 AND f.name = $3\n AND (f.owners::text[] && $4 OR acl_grants_run(f.extra_perms, f.view_only, $4))\n ) AS \"can_run!\"",
"describe": {
"columns": [
{
"ordinal": 0,
"name": "can_run!",
"type_info": "Bool"
}
],
"parameters": {
"Left": [
"Text",
"Text",
"Text",
"TextArray",
"Text"
]
},
"nullable": [
null
]
},
"hash": "2888477e49cf28a47fa4ca719812f944ce910c626d1d022e5f35d52093160593"
}
@@ -1,6 +1,6 @@
{
"db_name": "PostgreSQL",
"query": "UPDATE resource SET extra_perms = extra_perms - ('u/' || $2) || jsonb_build_object(('u/' || $1), extra_perms->('u/' || $2)) WHERE extra_perms ? ('u/' || $2) AND workspace_id = $3",
"query": "UPDATE resource SET extra_perms = extra_perms - ('u/' || $2) || jsonb_build_object(('u/' || $1), extra_perms->('u/' || $2)), view_only = array_replace(view_only, 'u/' || $2, 'u/' || $1) WHERE extra_perms ? ('u/' || $2) AND workspace_id = $3",
"describe": {
"columns": [],
"parameters": {
@@ -12,5 +12,5 @@
},
"nullable": []
},
"hash": "1f003a8a6a9402f5f7021b032281a9aa70a8f89777e95e0415877ffa90ecd87c"
"hash": "696c2f4c76c3876c4ff65ebacfd2aae6d9c8ccdcc0e621ac5ca2bf28a30fe2e4"
}
@@ -0,0 +1,24 @@
{
"db_name": "PostgreSQL",
"query": "WITH inserted AS (\n INSERT INTO flow\n (workspace_id, path, summary, description, archived, extra_perms, view_only, dependency_job, tag, ws_error_handler_muted, dedicated_worker, timeout, visible_to_runner_only, on_behalf_of, on_behalf_of_email, concurrency_key, versions, value, schema, edited_by, edited_at, labels, lock_error_logs, job_token_scopes)\n SELECT workspace_id, REGEXP_REPLACE(path, 'u/' || $2 || '/(.*)', $1 || '/\\1'), summary, description, archived, extra_perms, view_only, dependency_job, tag, ws_error_handler_muted, dedicated_worker, timeout, visible_to_runner_only, on_behalf_of, on_behalf_of_email, concurrency_key, versions, value, schema, edited_by, edited_at, labels, lock_error_logs, job_token_scopes\n FROM flow\n WHERE path LIKE ('u/' || $2 || '/%') AND workspace_id = $3\n RETURNING 1\n ) SELECT COUNT(*) FROM inserted",
"describe": {
"columns": [
{
"ordinal": 0,
"name": "count",
"type_info": "Int8"
}
],
"parameters": {
"Left": [
"Text",
"Text",
"Text"
]
},
"nullable": [
null
]
},
"hash": "91b82ddd4964a23bb69dcdfc5ff7f90a04016cd7fb9d249b7165f0f31c28b665"
}
@@ -1,6 +1,6 @@
{
"db_name": "PostgreSQL",
"query": "UPDATE folder SET extra_perms = extra_perms - ('u/' || $2) || jsonb_build_object(('u/' || $1), extra_perms->('u/' || $2)) WHERE extra_perms ? ('u/' || $2) AND workspace_id = $3",
"query": "UPDATE folder SET extra_perms = extra_perms - ('u/' || $2) || jsonb_build_object(('u/' || $1), extra_perms->('u/' || $2)), view_only = array_replace(view_only, 'u/' || $2, 'u/' || $1) WHERE extra_perms ? ('u/' || $2) AND workspace_id = $3",
"describe": {
"columns": [],
"parameters": {
@@ -12,5 +12,5 @@
},
"nullable": []
},
"hash": "cc9e2460b8fded240eff64cf3cf348f9bd36a86233f3b0a3e73f51698ed353dd"
"hash": "924b4941829f69ff96c55f0a432298de7cf3aa1690c931a5b569fce3203a86c3"
}
@@ -1,6 +1,6 @@
{
"db_name": "PostgreSQL",
"query": "UPDATE script SET extra_perms = extra_perms - ('u/' || $2) || jsonb_build_object(('u/' || $1), extra_perms->('u/' || $2)) WHERE extra_perms ? ('u/' || $2) AND workspace_id = $3",
"query": "UPDATE script SET extra_perms = extra_perms - ('u/' || $2) || jsonb_build_object(('u/' || $1), extra_perms->('u/' || $2)), view_only = array_replace(view_only, 'u/' || $2, 'u/' || $1) WHERE extra_perms ? ('u/' || $2) AND workspace_id = $3",
"describe": {
"columns": [],
"parameters": {
@@ -12,5 +12,5 @@
},
"nullable": []
},
"hash": "e38dd0f093636efa2ec1cda4618fc104f7b8ea88130692ce9c1390bda289627a"
"hash": "a6deb6191f84b084cb523c7385a3977c15deffa9e484945e53d8e0aa46f1fbc1"
}
@@ -0,0 +1,16 @@
{
"db_name": "PostgreSQL",
"query": "INSERT INTO flow\n (workspace_id, path, summary, description, archived, extra_perms, view_only, dependency_job, tag, ws_error_handler_muted, dedicated_worker, timeout, visible_to_runner_only, on_behalf_of, on_behalf_of_email, concurrency_key, versions, value, schema, edited_by, edited_at, job_token_scopes)\n SELECT workspace_id, REGEXP_REPLACE(path,'u/' || $2 || '/(.*)','u/' || $1 || '/\\1'), summary, description, archived, extra_perms, view_only, dependency_job, tag, ws_error_handler_muted, dedicated_worker, timeout, visible_to_runner_only, on_behalf_of, on_behalf_of_email, concurrency_key, versions, value, schema, edited_by, edited_at, job_token_scopes\n FROM flow\n WHERE path LIKE ('u/' || $2 || '/%') AND workspace_id = $3",
"describe": {
"columns": [],
"parameters": {
"Left": [
"Text",
"Text",
"Text"
]
},
"nullable": []
},
"hash": "a875cca3478a982c08e9263876a7399e3000b671a2133f9c9849a899360f1bb1"
}
@@ -1,16 +0,0 @@
{
"db_name": "PostgreSQL",
"query": "INSERT INTO flow\n (workspace_id, path, summary, description, archived, extra_perms, dependency_job, tag, ws_error_handler_muted, dedicated_worker, timeout, visible_to_runner_only, on_behalf_of, on_behalf_of_email, concurrency_key, versions, value, schema, edited_by, edited_at, job_token_scopes)\n SELECT workspace_id, REGEXP_REPLACE(path,'u/' || $2 || '/(.*)','u/' || $1 || '/\\1'), summary, description, archived, extra_perms, dependency_job, tag, ws_error_handler_muted, dedicated_worker, timeout, visible_to_runner_only, on_behalf_of, on_behalf_of_email, concurrency_key, versions, value, schema, edited_by, edited_at, job_token_scopes\n FROM flow\n WHERE path LIKE ('u/' || $2 || '/%') AND workspace_id = $3",
"describe": {
"columns": [],
"parameters": {
"Left": [
"Text",
"Text",
"Text"
]
},
"nullable": []
},
"hash": "b40c9e2d637cf0f2f49fd4c720b64eb6af24e8d8aa99b846f21f2ed49c320bdc"
}
@@ -1,24 +0,0 @@
{
"db_name": "PostgreSQL",
"query": "WITH inserted AS (\n INSERT INTO flow\n (workspace_id, path, summary, description, archived, extra_perms, dependency_job, tag, ws_error_handler_muted, dedicated_worker, timeout, visible_to_runner_only, on_behalf_of, on_behalf_of_email, concurrency_key, versions, value, schema, edited_by, edited_at, labels, lock_error_logs, job_token_scopes)\n SELECT workspace_id, REGEXP_REPLACE(path, 'u/' || $2 || '/(.*)', $1 || '/\\1'), summary, description, archived, extra_perms, dependency_job, tag, ws_error_handler_muted, dedicated_worker, timeout, visible_to_runner_only, on_behalf_of, on_behalf_of_email, concurrency_key, versions, value, schema, edited_by, edited_at, labels, lock_error_logs, job_token_scopes\n FROM flow\n WHERE path LIKE ('u/' || $2 || '/%') AND workspace_id = $3\n RETURNING 1\n ) SELECT COUNT(*) FROM inserted",
"describe": {
"columns": [
{
"ordinal": 0,
"name": "count",
"type_info": "Int8"
}
],
"parameters": {
"Left": [
"Text",
"Text",
"Text"
]
},
"nullable": [
null
]
},
"hash": "c4eaa6e3b1d30a72ef1b54b2b7f28d84f1ca3c91de86d520dce06ac12d557bd5"
}
@@ -218,18 +218,19 @@ async fn add_granular_acl(
} else {
""
};
let obj_o = sqlx::query_scalar::<_, serde_json::Value>(&format!(
let sql = format!(
"UPDATE {table} SET extra_perms = jsonb_set(extra_perms, $1, to_jsonb($2), \
true){set_view_only} WHERE {identifier} = $3 AND workspace_id = $4 RETURNING extra_perms"
))
.bind(vec![owner.clone()])
.bind(write.unwrap_or(false))
.bind(path)
.bind(&w_id)
.bind(&owner)
.bind(level == AclLevel::Read)
.fetch_optional(&mut *tx)
.await?;
);
let mut query = sqlx::query_scalar::<_, serde_json::Value>(&sql)
.bind(vec![owner.clone()])
.bind(write.unwrap_or(false))
.bind(path)
.bind(&w_id);
if has_run_level {
query = query.bind(&owner).bind(level == AclLevel::Read);
}
let obj_o = query.fetch_optional(&mut *tx).await?;
let _ = not_found_if_none(obj_o, &kind, &path)?;
@@ -93,6 +93,12 @@ async fn test_view_only_grants(db: Pool<Postgres>) -> anyhow::Result<()> {
post(&runner, port, "jobs/run/p/f/vo/s", json!({})).await?,
201
);
// The run just cached for the runner must not outlive their demotion.
assert_eq!(grant("u/test-user-3", json!({"level": "read"})).await?, 200);
assert_eq!(
post(&runner, port, "jobs/run/p/f/vo/s", json!({})).await?,
403
);
// Grants add up: an item grant that runs lifts the folder's view-only one.
let item_grant = json!({"owner": "u/test-user-2", "level": "run"});
@@ -105,6 +111,15 @@ async fn test_view_only_grants(db: Pool<Postgres>) -> anyhow::Result<()> {
201
);
// Kinds without a run level share as before.
let var = json!({"path": "u/test-user/v", "value": "x", "is_secret": false, "description": ""});
assert_eq!(post(&admin, port, "variables/create", var).await?, 201);
let share = json!({"owner": "u/test-user-2", "write": false});
assert_eq!(
post(&admin, port, "acls/add/variable/u/test-user/v", share).await?,
200
);
// An item-level view-only grant, kept by the next version of the script.
let own = bash_script("u/test-user/own", None);
assert_eq!(post(&admin, port, "scripts/create", own).await?, 201);
+7 -1
View File
@@ -2612,8 +2612,14 @@ pub async fn delete_workspace_user_internal(
];
// SAFETY: `table` comes from a hardcoded allowlist `extra_perms_tables`, not user input.
for table in &extra_perms_tables {
// A `view_only` entry left behind would make a later grant to a re-added user view-only.
let view_only = if ["script", "flow", "resource", "folder"].contains(table) {
", view_only = array_remove(view_only, 'u/' || $1)"
} else {
""
};
sqlx::query(&format!(
"UPDATE {table} SET extra_perms = extra_perms - ('u/' || $1) \
"UPDATE {table} SET extra_perms = extra_perms - ('u/' || $1){view_only} \
WHERE extra_perms ? ('u/' || $1) AND workspace_id = $2"
))
.bind(username_to_delete)
+5 -1
View File
@@ -2431,9 +2431,13 @@ async fn require_run_on_others_job(
kind: JobKind,
runnable_path: Option<String>,
) -> error::Result<()> {
// A saved agent runs as a flow preview under its own path, and a script wrapped for retries
// or an error handler as a single-step flow under the script's; a preview's path is the label
// `require_preview_label_runnable` holds to the same rule.
let acl_kind = match kind {
JobKind::Script => RunnableAclKind::Script,
JobKind::Script | JobKind::Preview | JobKind::SingleStepFlow => RunnableAclKind::Script,
JobKind::Flow => RunnableAclKind::Flow,
JobKind::FlowPreview => RunnableAclKind::Agent,
_ => return Ok(()),
};
match runnable_path {
+2 -2
View File
@@ -894,8 +894,8 @@ async fn offboard_user_from_workspace<'c>(
let flows_reassigned = sqlx::query_scalar!(
r#"WITH inserted AS (
INSERT INTO flow
(workspace_id, path, summary, description, archived, extra_perms, dependency_job, tag, ws_error_handler_muted, dedicated_worker, timeout, visible_to_runner_only, on_behalf_of, on_behalf_of_email, concurrency_key, versions, value, schema, edited_by, edited_at, labels, lock_error_logs, job_token_scopes)
SELECT workspace_id, REGEXP_REPLACE(path, 'u/' || $2 || '/(.*)', $1 || '/\1'), summary, description, archived, extra_perms, dependency_job, tag, ws_error_handler_muted, dedicated_worker, timeout, visible_to_runner_only, on_behalf_of, on_behalf_of_email, concurrency_key, versions, value, schema, edited_by, edited_at, labels, lock_error_logs, job_token_scopes
(workspace_id, path, summary, description, archived, extra_perms, view_only, dependency_job, tag, ws_error_handler_muted, dedicated_worker, timeout, visible_to_runner_only, on_behalf_of, on_behalf_of_email, concurrency_key, versions, value, schema, edited_by, edited_at, labels, lock_error_logs, job_token_scopes)
SELECT workspace_id, REGEXP_REPLACE(path, 'u/' || $2 || '/(.*)', $1 || '/\1'), summary, description, archived, extra_perms, view_only, dependency_job, tag, ws_error_handler_muted, dedicated_worker, timeout, visible_to_runner_only, on_behalf_of, on_behalf_of_email, concurrency_key, versions, value, schema, edited_by, edited_at, labels, lock_error_logs, job_token_scopes
FROM flow
WHERE path LIKE ('u/' || $2 || '/%') AND workspace_id = $3
RETURNING 1
+6 -4
View File
@@ -206,8 +206,10 @@ async fn restore_script(tx: &mut sqlx::PgConnection, item: &TrashItemWithData) -
.and_then(|v| v.as_array())
.ok_or_else(|| Error::internal_err("Invalid trash data for script"))?;
// The `view_only` default merged under each snapshot of a script, flow or resource: one
// trashed before the column existed lacks the key, which would insert NULL into it.
for script in scripts {
sqlx::query("INSERT INTO script SELECT * FROM jsonb_populate_record(null::script, $1)")
sqlx::query("INSERT INTO script SELECT * FROM jsonb_populate_record(null::script, '{\"view_only\": []}'::jsonb || $1)")
.bind(script)
.execute(&mut *tx)
.await
@@ -253,7 +255,7 @@ async fn restore_flow(tx: &mut sqlx::PgConnection, item: &TrashItemWithData) ->
.get("row")
.ok_or_else(|| Error::internal_err("Invalid trash data for flow"))?;
sqlx::query("INSERT INTO flow SELECT * FROM jsonb_populate_record(null::flow, $1)")
sqlx::query("INSERT INTO flow SELECT * FROM jsonb_populate_record(null::flow, '{\"view_only\": []}'::jsonb || $1)")
.bind(row)
.execute(&mut *tx)
.await?;
@@ -425,7 +427,7 @@ async fn restore_variable(tx: &mut sqlx::PgConnection, item: &TrashItemWithData)
if let Some(linked_resource) = data.get("linked_resource") {
if !linked_resource.is_null() {
sqlx::query(
"INSERT INTO resource SELECT * FROM jsonb_populate_record(null::resource, $1)
"INSERT INTO resource SELECT * FROM jsonb_populate_record(null::resource, '{\"view_only\": []}'::jsonb || $1)
ON CONFLICT DO NOTHING",
)
.bind(linked_resource)
@@ -460,7 +462,7 @@ async fn restore_resource(tx: &mut sqlx::PgConnection, item: &TrashItemWithData)
.get("row")
.ok_or_else(|| Error::internal_err("Invalid trash data for resource"))?;
sqlx::query("INSERT INTO resource SELECT * FROM jsonb_populate_record(null::resource, $1)")
sqlx::query("INSERT INTO resource SELECT * FROM jsonb_populate_record(null::resource, '{\"view_only\": []}'::jsonb || $1)")
.bind(row)
.execute(&mut *tx)
.await?;
+6 -6
View File
@@ -596,7 +596,7 @@ async fn update_username_in_workpsace<'c>(
.await?;
sqlx::query!(
"UPDATE resource SET extra_perms = extra_perms - ('u/' || $2) || jsonb_build_object(('u/' || $1), extra_perms->('u/' || $2)) WHERE extra_perms ? ('u/' || $2) AND workspace_id = $3",
"UPDATE resource SET extra_perms = extra_perms - ('u/' || $2) || jsonb_build_object(('u/' || $1), extra_perms->('u/' || $2)), view_only = array_replace(view_only, 'u/' || $2, 'u/' || $1) WHERE extra_perms ? ('u/' || $2) AND workspace_id = $3",
new_username,
old_username,
w_id
@@ -781,7 +781,7 @@ async fn update_username_in_workpsace<'c>(
.unwrap();
sqlx::query!(
"UPDATE script SET extra_perms = extra_perms - ('u/' || $2) || jsonb_build_object(('u/' || $1), extra_perms->('u/' || $2)) WHERE extra_perms ? ('u/' || $2) AND workspace_id = $3",
"UPDATE script SET extra_perms = extra_perms - ('u/' || $2) || jsonb_build_object(('u/' || $1), extra_perms->('u/' || $2)), view_only = array_replace(view_only, 'u/' || $2, 'u/' || $1) WHERE extra_perms ? ('u/' || $2) AND workspace_id = $3",
new_username,
old_username,
w_id
@@ -806,8 +806,8 @@ async fn update_username_in_workpsace<'c>(
// ---- flows ----
sqlx::query!(
r#"INSERT INTO flow
(workspace_id, path, summary, description, archived, extra_perms, dependency_job, tag, ws_error_handler_muted, dedicated_worker, timeout, visible_to_runner_only, on_behalf_of, on_behalf_of_email, concurrency_key, versions, value, schema, edited_by, edited_at, job_token_scopes)
SELECT workspace_id, REGEXP_REPLACE(path,'u/' || $2 || '/(.*)','u/' || $1 || '/\1'), summary, description, archived, extra_perms, dependency_job, tag, ws_error_handler_muted, dedicated_worker, timeout, visible_to_runner_only, on_behalf_of, on_behalf_of_email, concurrency_key, versions, value, schema, edited_by, edited_at, job_token_scopes
(workspace_id, path, summary, description, archived, extra_perms, view_only, dependency_job, tag, ws_error_handler_muted, dedicated_worker, timeout, visible_to_runner_only, on_behalf_of, on_behalf_of_email, concurrency_key, versions, value, schema, edited_by, edited_at, job_token_scopes)
SELECT workspace_id, REGEXP_REPLACE(path,'u/' || $2 || '/(.*)','u/' || $1 || '/\1'), summary, description, archived, extra_perms, view_only, dependency_job, tag, ws_error_handler_muted, dedicated_worker, timeout, visible_to_runner_only, on_behalf_of, on_behalf_of_email, concurrency_key, versions, value, schema, edited_by, edited_at, job_token_scopes
FROM flow
WHERE path LIKE ('u/' || $2 || '/%') AND workspace_id = $3"#,
new_username,
@@ -876,7 +876,7 @@ async fn update_username_in_workpsace<'c>(
.await?;
sqlx::query!(
"UPDATE flow SET extra_perms = extra_perms - ('u/' || $2) || jsonb_build_object(('u/' || $1), extra_perms->('u/' || $2)) WHERE extra_perms ? ('u/' || $2) AND workspace_id = $3",
"UPDATE flow SET extra_perms = extra_perms - ('u/' || $2) || jsonb_build_object(('u/' || $1), extra_perms->('u/' || $2)), view_only = array_replace(view_only, 'u/' || $2, 'u/' || $1) WHERE extra_perms ? ('u/' || $2) AND workspace_id = $3",
new_username,
old_username,
w_id
@@ -1089,7 +1089,7 @@ async fn update_username_in_workpsace<'c>(
.await?;
sqlx::query!(
"UPDATE folder SET extra_perms = extra_perms - ('u/' || $2) || jsonb_build_object(('u/' || $1), extra_perms->('u/' || $2)) WHERE extra_perms ? ('u/' || $2) AND workspace_id = $3",
"UPDATE folder SET extra_perms = extra_perms - ('u/' || $2) || jsonb_build_object(('u/' || $1), extra_perms->('u/' || $2)), view_only = array_replace(view_only, 'u/' || $2, 'u/' || $1) WHERE extra_perms ? ('u/' || $2) AND workspace_id = $3",
new_username,
old_username,
w_id
+4 -1
View File
@@ -260,8 +260,11 @@ pub async fn require_can_run(
let can_run = sqlx::query_scalar!(
"WITH item AS (
SELECT extra_perms, view_only FROM script
-- The newest version only: a script reusing a retired path starts with no grants,
-- and the retired versions' must not run it.
(SELECT extra_perms, view_only FROM script
WHERE $5 = 'script' AND workspace_id = $1 AND path = $2
ORDER BY created_at DESC LIMIT 1)
UNION ALL SELECT extra_perms, view_only FROM flow
WHERE $5 = 'flow' AND workspace_id = $1 AND path = $2
UNION ALL SELECT extra_perms, view_only FROM resource
+2 -2
View File
@@ -460,7 +460,7 @@ pub async fn deploy_relocked_version(
sqlx::query!("
INSERT INTO script
(workspace_id, hash, path, parent_hashes, summary, description, content, \
created_by, schema, is_template, extra_perms, lock, language, kind, tag, \
created_by, schema, is_template, extra_perms, view_only, lock, language, kind, tag, \
envs, concurrent_limit, concurrency_time_window_s, cache_ttl, cache_ignore_s3_path, \
dedicated_worker, ws_error_handler_muted, priority, restart_unless_cancelled, \
delete_after_use, delete_after_secs, timeout, concurrency_key, visible_to_runner_only, auto_kind, \
@@ -468,7 +468,7 @@ pub async fn deploy_relocked_version(
job_token_scopes, lock_error_logs, created_at)
SELECT workspace_id, $1, path, array_prepend($2::bigint, COALESCE(parent_hashes, '{}'::bigint[])), summary, description, \
content, created_by, schema, is_template, extra_perms, $4::text, language, kind, tag, \
content, created_by, schema, is_template, extra_perms, view_only, $4::text, language, kind, tag, \
envs, concurrent_limit, concurrency_time_window_s, cache_ttl, cache_ignore_s3_path, \
dedicated_worker, ws_error_handler_muted, priority, restart_unless_cancelled, \
delete_after_use, delete_after_secs, timeout, concurrency_key, visible_to_runner_only, auto_kind, \
+4
View File
@@ -540,6 +540,10 @@ async fn require_can_run_target(
workspace_id: &str,
base: &crate::types::BaseTriggerData,
) -> Result<()> {
// A static HTTP route has no runnable.
if base.script_path.is_empty() {
return Ok(());
}
let kind = if base.is_flow {
windmill_common::auth::RunnableAclKind::Flow
} else {
+5 -3
View File
@@ -65,9 +65,11 @@ write are: an item-level view-only grant does not withhold what the folder lets
`windmill_common::auth::require_can_run` enforces it, on top of the RLS read and of the role's
`run`, at each place that starts a deployed runnable for a caller: the run routes, restart, cancel
and run-now of someone else's run, previews labelled with the path, agent and eval runs, and the
saving and firing of schedules and triggers. Flow steps, subflows and agent tools are not checked,
as only the top-level run needs it. Its positive answers are cached, so every write that can take
and run-now of someone else's run, previews labelled with the path, agent and eval runs, the
saving of schedules and triggers, and the firing of triggers, which fetch their target as their
owner on every fire. A schedule's later runs are not re-checked, as they were never re-checked
for read either: demoting its owner does not stop it. Flow steps, subflows and agent tools are not
checked, as only the top-level run needs it. Its positive answers are cached, so every write that can take
run away calls `notify_run_perms_change`. Anything that copies one of those rows (a new script
version, a rename, a fork, a workspace rename) must copy `view_only` too: a copy that drops it
turns its viewers into runners.
+8 -2
View File
@@ -45,10 +45,16 @@ describe('folderPermissionDiff', () => {
})
// `removeowner` leaves a grant that runs, so a view-only one needs a second call.
it('admin → viewer demotes, then withholds run', () => {
expect(folderPermissionDiff([member('admin')], [member('viewer')])).toEqual([
it('admin → viewer demotes, then withholds run, last when it is the caller', () => {
const expected = [
{ kind: 'demoteAdmin', owner: 'u/alice', write: false },
{ kind: 'setAcl', owner: 'u/alice', level: 'read' }
]
expect(folderPermissionDiff([member('admin')], [member('viewer')])).toEqual(expected)
const bob = { owner_name: 'u/bob', role: 'runner' as const }
expect(folderPermissionDiff([member('admin')], [member('viewer'), bob], ['u/alice'])).toEqual([
{ kind: 'setAcl', owner: 'u/bob', level: 'run' },
...expected
])
})
+3 -3
View File
@@ -99,9 +99,9 @@ export function folderPermissionDiff(
// `previousRole === 'admin'` is what makes it a handle: `callerOwners` lists every group
// the caller belongs to, and one holding only a viewer or writer row is not in `owners`,
// so removing it is an ordinary call that should not queue behind the fatal one.
// The `setAcl` that follows an admin's demotion to viewer goes with it: sent before it, the
// member is still an owner and the grant is refused.
const revokesCaller = (call: FolderPermissionCall) =>
(call.kind === 'demoteAdmin' || call.kind === 'remove') &&
(callerOwners?.includes(call.owner) ?? false) &&
previousRole.get(call.owner) === 'admin'
(callerOwners?.includes(call.owner) ?? false) && previousRole.get(call.owner) === 'admin'
return [...calls.filter((c) => !revokesCaller(c)), ...calls.filter(revokesCaller)]
}