mirror of
https://github.com/windmill-labs/windmill.git
synced 2026-11-19 00:20:08 +00:00
fix: hold a builder's hub steps to the run-time hub rule (#12401)
* fix: hold a builder's hub steps to the run-time hub rule * fix: show the server's reason when a flow test run is refused * fix: check each distinct hub path once when a builder saves a flow * fix: drop an unused import from the app builder check tests
This commit is contained in:
@@ -0,0 +1,105 @@
|
||||
//! A builder choosing a Hub step in a flow, an agent or an app is held to the run-time rule for
|
||||
//! naming a Hub path: refused without `code`, allowed with it.
|
||||
|
||||
use reqwest::multipart;
|
||||
use serde_json::{json, Value};
|
||||
use sqlx::{Pool, Postgres};
|
||||
use windmill_test_utils::*;
|
||||
|
||||
const CODER: &str = "SECRET_TOKEN_2";
|
||||
const OP: &str = "SECRET_TOKEN_3";
|
||||
const HUB_PATH: &str = "hub/990000794/test/echo";
|
||||
const REFUSAL: &str = "Hub scripts and flows require a role that can write code";
|
||||
|
||||
fn flow(path: &str) -> Value {
|
||||
json!({"modules": [{"id": "a", "value": {"type": "script", "path": path, "input_transforms": {}}}]})
|
||||
}
|
||||
|
||||
#[sqlx::test(fixtures("base"))]
|
||||
async fn a_hub_step_follows_the_code_capability(db: Pool<Postgres>) -> anyhow::Result<()> {
|
||||
initialize_tracing().await;
|
||||
sqlx::raw_sql(
|
||||
r#"UPDATE workspace_settings SET role_overrides = '{
|
||||
"operator": {"capabilities": {"flows": "compose", "apps": "compose"}},
|
||||
"developer": {"capabilities": {"flows": "compose", "apps": "compose"}}
|
||||
}' WHERE workspace_id = 'test-workspace';
|
||||
UPDATE usr SET operator = true WHERE username = 'test-user-3';"#,
|
||||
)
|
||||
.execute(&db)
|
||||
.await?;
|
||||
let server = ApiServer::start(db.clone()).await?;
|
||||
let ws = format!(
|
||||
"http://localhost:{}/api/w/test-workspace",
|
||||
server.addr.port()
|
||||
);
|
||||
let client = reqwest::Client::new();
|
||||
|
||||
let send = |token: &'static str, case: &'static str| {
|
||||
let (ws, client) = (ws.clone(), client.clone());
|
||||
let user = if token == OP {
|
||||
"test-user-3"
|
||||
} else {
|
||||
"test-user-2"
|
||||
};
|
||||
async move {
|
||||
let req = match case {
|
||||
"flow create" => client.post(format!("{ws}/flows/create")).json(&json!({
|
||||
"path": format!("u/{user}/hub_step"), "summary": "", "value": flow(HUB_PATH),
|
||||
})),
|
||||
"flow preview" => client
|
||||
.post(format!("{ws}/jobs/run/preview_flow"))
|
||||
.json(&json!({ "value": flow(HUB_PATH), "args": {} })),
|
||||
"agent write" => client.post(format!("{ws}/resources/create")).json(&json!({
|
||||
"path": format!("u/{user}/agent"), "resource_type": "ai_agent",
|
||||
"value": {"tools": [{"id": "t", "value": {"tool_type": "flowmodule",
|
||||
"type": "script", "path": HUB_PATH, "input_transforms": {}}}]},
|
||||
})),
|
||||
"app create" => {
|
||||
let app = json!({
|
||||
"path": format!("u/{user}/app"), "summary": "",
|
||||
"value": {"files": {}, "runnables": {"a": {"name": "a",
|
||||
"type": "runnableByPath", "path": HUB_PATH, "runType": "hubscript"}}},
|
||||
"policy": {"execution_mode": "publisher", "sandbox": true,
|
||||
"triggerables_v2": {}},
|
||||
});
|
||||
client.post(format!("{ws}/apps/create_raw")).multipart(
|
||||
multipart::Form::new()
|
||||
.part(
|
||||
"app",
|
||||
multipart::Part::text(app.to_string())
|
||||
.mime_str("application/json")
|
||||
.unwrap(),
|
||||
)
|
||||
.part("js", multipart::Part::text("console.log('app')")),
|
||||
)
|
||||
}
|
||||
// A preset handler is allowed at run time to every role, so it is as a step.
|
||||
"flow create, preset handler" => {
|
||||
client.post(format!("{ws}/flows/create")).json(&json!({
|
||||
"path": format!("u/{user}/preset_step"), "summary": "",
|
||||
"value": flow("hub/2431/slack/schedule-error-handler-slack"),
|
||||
}))
|
||||
}
|
||||
_ => unreachable!(),
|
||||
};
|
||||
let resp = req.bearer_auth(token).send().await.unwrap();
|
||||
(resp.status(), resp.text().await.unwrap())
|
||||
}
|
||||
};
|
||||
|
||||
for case in ["flow create", "flow preview", "agent write", "app create"] {
|
||||
let (status, body) = send(OP, case).await;
|
||||
assert!(
|
||||
status == 403 && body.contains(&format!("{HUB_PATH}: {REFUSAL}")),
|
||||
"operator, {case}: {status} {body}"
|
||||
);
|
||||
let (status, body) = send(CODER, case).await;
|
||||
assert!(status.is_success(), "code role, {case}: {status} {body}");
|
||||
}
|
||||
let (status, body) = send(OP, "flow create, preset handler").await;
|
||||
assert!(
|
||||
status.is_success(),
|
||||
"operator, preset handler: {status} {body}"
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
@@ -5,13 +5,34 @@ use sqlx::{Postgres, Transaction};
|
||||
use windmill_common::{
|
||||
error::{self, Error},
|
||||
flows::FlowValue,
|
||||
jobs::{check_can_name_runnable, RunOrigin},
|
||||
roles::AuthorLevel,
|
||||
runnable_ref::RunnableRef,
|
||||
workspaces::{check_operator_can_build, BuilderKind},
|
||||
DB,
|
||||
};
|
||||
|
||||
use crate::{get_scope_tags, is_super_admin_authed, ApiAuthed};
|
||||
|
||||
/// The run-time rule for naming a Hub path, applied to one a builder saves. A save can hold many
|
||||
/// Hub paths, so a refusal names the one it is about.
|
||||
pub async fn check_builder_can_name_hub(
|
||||
runnable: &RunnableRef,
|
||||
authed: &ApiAuthed,
|
||||
db: &DB,
|
||||
w_id: &str,
|
||||
) -> error::Result<()> {
|
||||
let origin = RunOrigin::NamedBy(authed.to_authed_ref());
|
||||
check_can_name_runnable(&origin, runnable, db, w_id)
|
||||
.await
|
||||
.map_err(|e| match e {
|
||||
Error::PermissionDenied(msg) => {
|
||||
Error::PermissionDenied(format!("{}: {msg}", runnable.path()))
|
||||
}
|
||||
e => e,
|
||||
})
|
||||
}
|
||||
|
||||
/// `validate_operator_composed_flow` on a transaction the caller already holds, so a write that
|
||||
/// has one open does not wait on a second connection. It must have been begun as the caller
|
||||
/// (`UserDB::begin`): its row-level security is the check.
|
||||
@@ -24,6 +45,13 @@ pub async fn authorize_composed_flow(
|
||||
w_id: &str,
|
||||
) -> error::Result<()> {
|
||||
let mut refs = windmill_common::flows::check_flow_is_composition_only(value)?;
|
||||
// Each check reads the caller's role; a flow can step through the same Hub script many times.
|
||||
refs.hub_runnables
|
||||
.sort_by(|a, b| (a.is_flow(), a.path()).cmp(&(b.is_flow(), b.path())));
|
||||
refs.hub_runnables.dedup();
|
||||
for runnable in &refs.hub_runnables {
|
||||
check_builder_can_name_hub(runnable, authed, db, w_id).await?;
|
||||
}
|
||||
|
||||
// A builder links only an agent it can read, and binds only the tool inputs it leaves open.
|
||||
for agent in &refs.agents {
|
||||
|
||||
@@ -2399,7 +2399,6 @@ fn check_operator_composed_app(
|
||||
|
||||
referenced.sort();
|
||||
referenced.dedup();
|
||||
refuse_hub_runnables(&referenced)?;
|
||||
Ok(referenced)
|
||||
}
|
||||
|
||||
@@ -2459,25 +2458,6 @@ fn checked_policy_runnable_paths(policy: &Policy) -> Result<Vec<(AppRunnableKind
|
||||
.collect())
|
||||
}
|
||||
|
||||
fn refuse_hub_runnables(referenced: &[(AppRunnableKind, String)]) -> Result<()> {
|
||||
for (kind, path) in referenced {
|
||||
let hub = match kind {
|
||||
AppRunnableKind::Agent => path.starts_with("hub/"),
|
||||
AppRunnableKind::Script | AppRunnableKind::Flow => {
|
||||
RunnableRef::from_path(path, *kind == AppRunnableKind::Flow)?
|
||||
.path()
|
||||
.starts_with("hub/")
|
||||
}
|
||||
};
|
||||
if hub {
|
||||
return Err(Error::PermissionDenied(format!(
|
||||
"Operators with builder rights cannot reference the hub runnable {path}. Deploy it to the workspace first."
|
||||
)));
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Runs on every app write by an operator with builder rights, from inside the create/update
|
||||
/// internals so both raw-app endpoints are covered by one check.
|
||||
///
|
||||
@@ -2506,7 +2486,22 @@ pub(crate) async fn validate_operator_composed_app(
|
||||
}
|
||||
}
|
||||
let referenced = check_operator_composed_app(write, raw_app, value, policy)?;
|
||||
require_runnables_readable(authed, db, user_db, w_id, referenced).await
|
||||
// A Hub path has no workspace row to be readable in: it is held to the rule for naming one.
|
||||
let mut in_workspace = Vec::with_capacity(referenced.len());
|
||||
for (kind, path) in referenced {
|
||||
if kind != AppRunnableKind::Agent {
|
||||
let runnable = RunnableRef::from_path(&path, kind == AppRunnableKind::Flow)?;
|
||||
if runnable.path().starts_with("hub/") {
|
||||
windmill_api_auth::composed::check_builder_can_name_hub(
|
||||
&runnable, authed, db, w_id,
|
||||
)
|
||||
.await?;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
in_workspace.push((kind, path));
|
||||
}
|
||||
require_runnables_readable(authed, db, user_db, w_id, in_workspace).await
|
||||
}
|
||||
|
||||
fn policy_runs_as_another(policy: &Policy, authed: &ApiAuthed) -> bool {
|
||||
@@ -4675,15 +4670,11 @@ async fn execute_component(
|
||||
})?;
|
||||
// A builder testing the app it is composing only ever previews a deployed runnable
|
||||
// (`path`), confined below to what it may read. Inline `raw_code`, which an `id` also
|
||||
// requires, is authoring code, so it stays closed to every operator. So is a hub
|
||||
// path, which `require_path_read_access_for_preview` admits for everyone, and which
|
||||
// `check_can_name_runnable` also refuses further on: it is exactly the unreviewed
|
||||
// code the composition check refuses in a flow.
|
||||
let previews_hub = preview_path.is_some_and(|p| p.starts_with("hub/"));
|
||||
// requires, is authoring code, so it stays closed to every operator. A hub path,
|
||||
// which `require_path_read_access_for_preview` admits for everyone, is held further on
|
||||
// to `check_can_name_runnable`, as it is when a builder saves it as a step.
|
||||
let caps = caller_capabilities(&db, &w_id, authed).await?;
|
||||
if !caps.code
|
||||
&& (caps.apps == AuthorLevel::None || payload.raw_code.is_some() || previews_hub)
|
||||
{
|
||||
if !caps.code && (caps.apps == AuthorLevel::None || payload.raw_code.is_some()) {
|
||||
return Err(Error::PermissionDenied(
|
||||
"Your role cannot run preview jobs in this workspace".to_string(),
|
||||
));
|
||||
@@ -7230,7 +7221,7 @@ mod embed_token_tests {
|
||||
#[cfg(test)]
|
||||
mod operator_app_tests {
|
||||
use super::{check_operator_composed_app, AppRunnableKind, BuilderAppWrite, Policy};
|
||||
use windmill_common::error::{Error, Result};
|
||||
use windmill_common::error::Result;
|
||||
use windmill_common::worker::to_raw_value;
|
||||
|
||||
fn builder_policy(triggerables_v2: serde_json::Value) -> Policy {
|
||||
@@ -7273,22 +7264,24 @@ mod operator_app_tests {
|
||||
vec![(AppRunnableKind::Script, "f/x/s".to_string())]
|
||||
);
|
||||
|
||||
// A hub reference is unreviewed code, refused like it is in a flow.
|
||||
for key in [
|
||||
"a:script/hub/1/x",
|
||||
"a:script//hub/1/x",
|
||||
"a:flow//hub/flows/1/x",
|
||||
// Parses as a workspace flow path, so only the Hub check refuses it.
|
||||
"a:flow/hub/1/x",
|
||||
// A hub reference is reported, for the caller to hold to the rule for naming one.
|
||||
for (key, kind, path) in [
|
||||
("a:script/hub/1/x", AppRunnableKind::Script, "hub/1/x"),
|
||||
("a:script//hub/1/x", AppRunnableKind::Script, "/hub/1/x"),
|
||||
(
|
||||
"a:flow//hub/flows/1/x",
|
||||
AppRunnableKind::Flow,
|
||||
"/hub/flows/1/x",
|
||||
),
|
||||
("a:flow/hub/1/x", AppRunnableKind::Flow, "hub/1/x"),
|
||||
] {
|
||||
let mut policy = builder_policy(
|
||||
serde_json::json!({ key: {"static_inputs": {}, "one_of_inputs": {}} }),
|
||||
);
|
||||
assert!(
|
||||
matches!(
|
||||
composed_app(clean.clone(), &mut policy),
|
||||
Err(Error::PermissionDenied(_))
|
||||
),
|
||||
composed_app(clean.clone(), &mut policy)
|
||||
.unwrap()
|
||||
.contains(&(kind, path.to_string())),
|
||||
"{key}"
|
||||
);
|
||||
}
|
||||
@@ -7338,7 +7331,10 @@ mod operator_app_tests {
|
||||
vec![(AppRunnableKind::Agent, "f/x/bot".to_string())]
|
||||
);
|
||||
let mut policy = builder_policy(serde_json::json!({}));
|
||||
assert!(composed_app(path_runnable("hub/1/x", "hubscript"), &mut policy).is_err());
|
||||
assert_eq!(
|
||||
composed_app(path_runnable("hub/1/x", "hubscript"), &mut policy).unwrap(),
|
||||
vec![(AppRunnableKind::Script, "hub/1/x".to_string())]
|
||||
);
|
||||
|
||||
// `execute_component` looks up `<component>:<path>` with an unrestricted component, so a
|
||||
// second colon must not hide the path from validation: `x:y:script/hub/…` resolves at run
|
||||
@@ -7346,7 +7342,9 @@ mod operator_app_tests {
|
||||
let mut policy = builder_policy(
|
||||
serde_json::json!({"x:y:script/hub/123/foo": {"static_inputs": {}, "one_of_inputs": {}}}),
|
||||
);
|
||||
assert!(composed_app(clean.clone(), &mut policy).is_err());
|
||||
assert!(composed_app(clean.clone(), &mut policy)
|
||||
.unwrap()
|
||||
.contains(&(AppRunnableKind::Script, "hub/123/foo".to_string())));
|
||||
let mut policy = builder_policy(
|
||||
serde_json::json!({"x:y:script/f/x/s": {"static_inputs": {}, "one_of_inputs": {}}}),
|
||||
);
|
||||
|
||||
@@ -245,6 +245,9 @@ pub struct ComposedFlowRefs {
|
||||
pub tags: Vec<String>,
|
||||
/// Every workspace runnable a step references, as `(is_flow, path)`.
|
||||
pub runnables: Vec<(bool, String)>,
|
||||
/// Every Hub script or flow a step references, for the caller to check it may name: a Hub
|
||||
/// path has no workspace row the readability check of `runnables` could find.
|
||||
pub hub_runnables: Vec<RunnableRef>,
|
||||
/// Version-pinned script steps, as `(path, hash)`.
|
||||
pub pinned_scripts: Vec<(String, ScriptHash)>,
|
||||
/// The `ai_agent` resources that agent steps link.
|
||||
@@ -345,16 +348,14 @@ fn check_module_value_is_composition_only(
|
||||
}
|
||||
FlowModuleValue::Identity => {}
|
||||
FlowModuleValue::Script { path, hash, tag_override, .. } => {
|
||||
check_composable_path(path, false, id)?;
|
||||
push_tag(tag_override);
|
||||
refs.runnables.push((false, path.clone()));
|
||||
push_runnable(refs, path, false)?;
|
||||
if let Some(hash) = hash {
|
||||
refs.pinned_scripts.push((path.clone(), *hash));
|
||||
}
|
||||
}
|
||||
FlowModuleValue::Flow { path, .. } => {
|
||||
check_composable_path(path, true, id)?;
|
||||
refs.runnables.push((true, path.clone()));
|
||||
push_runnable(refs, path, true)?;
|
||||
}
|
||||
FlowModuleValue::ForloopFlow { modules, modules_node, .. }
|
||||
| FlowModuleValue::WhileloopFlow { modules, modules_node, .. } => {
|
||||
@@ -427,17 +428,15 @@ fn refused(id: &str, what: &str) -> Error {
|
||||
))
|
||||
}
|
||||
|
||||
fn check_composable_path(path: &str, is_flow: bool, id: &str) -> Result<(), Error> {
|
||||
if !path.is_empty()
|
||||
&& RunnableRef::from_path(path, is_flow)?
|
||||
.path()
|
||||
.starts_with("hub/")
|
||||
{
|
||||
return Err(Error::PermissionDenied(format!(
|
||||
"Step {id}: hub runnables are not available to operators with builder rights. Deploy \
|
||||
it to the workspace first."
|
||||
)));
|
||||
fn push_runnable(refs: &mut ComposedFlowRefs, path: &str, is_flow: bool) -> Result<(), Error> {
|
||||
if !path.is_empty() {
|
||||
let runnable = RunnableRef::from_path(path, is_flow)?;
|
||||
if runnable.path().starts_with("hub/") {
|
||||
refs.hub_runnables.push(runnable);
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
refs.runnables.push((is_flow, path.to_string()));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -595,22 +594,22 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn composition_check_rejects_hub_runnables() {
|
||||
fn composition_check_collects_hub_runnables() {
|
||||
for (kind, path) in [
|
||||
("script", "hub/1234/thing"),
|
||||
("script", "/hub/1234/thing"),
|
||||
("flow", "hub/flows/1234/thing"),
|
||||
("flow", "/hub/flows/1234/thing"),
|
||||
// Parses as a workspace flow path, so only the Hub check refuses it.
|
||||
// Parses as a workspace flow path, so only the path prefix marks it as Hub.
|
||||
("flow", "hub/1234/thing"),
|
||||
] {
|
||||
let value = serde_json::json!({"modules": [{"id": "a",
|
||||
"value": {"type": kind, "path": path}}]});
|
||||
assert!(
|
||||
matches!(
|
||||
check_flow_is_composition_only(&flow(value)),
|
||||
Err(Error::PermissionDenied(_))
|
||||
),
|
||||
let refs = check_flow_is_composition_only(&flow(value)).unwrap();
|
||||
assert!(refs.runnables.is_empty(), "{kind} {path}");
|
||||
assert_eq!(
|
||||
refs.hub_runnables,
|
||||
vec![RunnableRef::from_path(path, kind == "flow").unwrap()],
|
||||
"{kind} {path}"
|
||||
);
|
||||
}
|
||||
|
||||
@@ -56,6 +56,9 @@ permissions:
|
||||
`worker_flow` then applies to the step job. So composing a path is enough to run it, and to run
|
||||
it as whoever it runs as: `validate_operator_composed_flow` re-checks each path under the
|
||||
caller's RLS. This is the general case; the one below is on top of it, not instead of it.
|
||||
A Hub step has no workspace row to read, so it is collected apart and held to the run-time rule
|
||||
for naming a Hub path (`check_can_name_runnable`) against the builder: Hub steps follow the
|
||||
`code` capability, and preset handlers are allowed to every role.
|
||||
- **the `(path, hash)` of every version-pinned step**. A step carrying a `hash` is dispatched by
|
||||
that hash alone, with the path beside it never consulted, so a readable path paired with another
|
||||
script's hash still runs that other script.
|
||||
@@ -88,7 +91,8 @@ that function.
|
||||
|
||||
The same reasoning applies to a builder-authored app, with one extra step. `execute_component`
|
||||
resolves the runnable it runs on the root handle, so `validate_operator_composed_app` checks every
|
||||
referenced path under the caller's RLS and refuses hub ones. For a script it checks the version a
|
||||
referenced path under the caller's RLS and holds a Hub one to `check_can_name_runnable`, as in a
|
||||
flow, so Hub steps follow the `code` capability. For a script it checks the version a
|
||||
run would pick: grants are per version, and a path reused after its versions were archived starts
|
||||
with none while the archived ones keep theirs. But it has to check **two** surfaces,
|
||||
because they are not the same list: the policy's `script/<path>`, `flow/<path>` and
|
||||
@@ -152,7 +156,7 @@ the bundle same-origin with the session of whoever opens it. A builder's draft i
|
||||
held to the same rule as its deploy: `update_draft` refuses one whose `policy.sandbox` is not
|
||||
`true`, so whatever an admin opens of an operator's, deployed or drafted, previews isolated.
|
||||
Turning the sandbox off in their own editor stays that admin's explicit choice. It also refuses
|
||||
a draft naming a runnable the builder cannot read, or a hub one, like the deploy: the editor
|
||||
a draft naming a runnable the builder cannot read, or a Hub one its role may not name, like the deploy: the editor
|
||||
preview runs them as whoever opens it, and admits any path for an admin. That covers the
|
||||
policy's legacy `triggerables` too, `rawscript/` keys included: every deploy of a draft
|
||||
carries it through untouched, so an admin deploying the draft would publish it. Its
|
||||
|
||||
@@ -28,7 +28,7 @@
|
||||
RefreshCw,
|
||||
X
|
||||
} from 'lucide-svelte'
|
||||
import { DynamicInput, sendUserToast, type StateStore } from '$lib/utils'
|
||||
import { apiErrorMessage, DynamicInput, sendUserToast, type StateStore } from '$lib/utils'
|
||||
import { dfs } from './flows/dfs'
|
||||
import { sliceModules } from './flows/flowStateUtils.svelte'
|
||||
import InputSelectedBadge from './schema/InputSelectedBadge.svelte'
|
||||
@@ -234,7 +234,7 @@
|
||||
// The chat follows the turn its conversation is still answering rather than
|
||||
// reporting a failed run, so the refusal goes back to it.
|
||||
if (conversationId && e instanceof ApiError && e.status === 409) throw e
|
||||
sendUserToast('Could not run preview', true, undefined, e.toString())
|
||||
sendUserToast('Could not run preview', true, undefined, apiErrorMessage(e))
|
||||
}
|
||||
schemaFormWithArgPicker?.refreshHistory()
|
||||
return newJobId
|
||||
|
||||
Reference in New Issue
Block a user