From 708643b879144df7b88ecc3e92de858b67ad5db2 Mon Sep 17 00:00:00 2001 From: Ruben Fiszel Date: Thu, 3 Dec 2026 08:01:22 +0100 Subject: [PATCH] fix(apps): require the app policy on value deploys (#12829) * fix(apps): rebind run-as identity on raw app source deploys without a policy Co-Authored-By: Claude Opus 5.5 * fix(apps): let a wm_deployers builder keep run-as on source deploys Co-Authored-By: Claude Opus 5.5 * fix(apps): require the policy on value deploys to apps run as another user Co-Authored-By: Claude Opus 5.5 --------- Co-authored-by: Claude Opus 5.5 --- backend/tests/app_access_mode_only.rs | 82 +++++++++++++++++++ backend/windmill-api/openapi.yaml | 4 +- backend/windmill-api/src/apps.rs | 70 +++++++++++++++- .../src/mcp/auto_generated_endpoints.rs | 4 +- frontend/src/lib/mcpEndpointTools.ts | 4 +- 5 files changed, 156 insertions(+), 8 deletions(-) diff --git a/backend/tests/app_access_mode_only.rs b/backend/tests/app_access_mode_only.rs index b113e9de9a..d489c927c5 100644 --- a/backend/tests/app_access_mode_only.rs +++ b/backend/tests/app_access_mode_only.rs @@ -86,6 +86,37 @@ async fn policy(db: &Pool, path: &str) -> anyhow::Result { .await?) } +async fn update_raw( + ws: &str, + path: &str, + token: &str, + app: Value, +) -> anyhow::Result<(u16, String)> { + let form = multipart::Form::new() + .part( + "app", + multipart::Part::text(app.to_string()).mime_str("application/json")?, + ) + .part("js", multipart::Part::text("console.log('app')")); + let resp = client() + .post(format!("{ws}/apps/update_raw/{path}")) + .bearer_auth(token) + .multipart(form) + .send() + .await?; + Ok((resp.status().as_u16(), resp.text().await?)) +} + +async fn versions(db: &Pool, path: &str) -> anyhow::Result { + Ok(sqlx::query_scalar( + "SELECT array_length(versions, 1) FROM app + WHERE path = $1 AND workspace_id = 'test-workspace'", + ) + .bind(path) + .fetch_one(db) + .await?) +} + fn triggerable(key: &str) -> Value { json!({ key: { "static_inputs": {}, "one_of_inputs": {} } }) } @@ -150,6 +181,57 @@ async fn test_access_mode_only_update(db: Pool) -> anyhow::Result<()> assert!(body.contains("wm_deployers"), "{body}"); assert_eq!(policy(&db, SHARED).await?["execution_mode"], "anonymous"); + // Nor can they deploy a value to it without its policy: the app would keep its run-as user, + // and the value chooses what runs as them (the modules and lock of a pinned inline script). + const VALUE_ONLY: &str = "u/test-user-2/value_only"; + create( + &ws, + ADMIN, + VALUE_ONLY, + json!({ "execution_mode": "publisher" }), + ) + .await?; + let (status, body) = update(&ws, VALUE_ONLY, USER, json!({ "value": { "v": 1 } })).await?; + assert_eq!(status, 403, "{body}"); + assert!(body.contains("wm_deployers"), "{body}"); + assert_eq!(versions(&db, VALUE_ONLY).await?, 1); + // Someone who may keep the run-as user does. + let (status, body) = update(&ws, VALUE_ONLY, ADMIN, json!({ "value": { "v": 2 } })).await?; + assert_eq!(status, 200, "{body}"); + assert_eq!( + policy(&db, VALUE_ONLY).await?["on_behalf_of"], + "u/test-user" + ); + // With its policy the deploy goes through and rebinds the app to its deployer, after + // which a value alone is theirs to deploy. + let (status, body) = update( + &ws, + VALUE_ONLY, + USER, + json!({ "value": { "v": 3 }, "policy": { "execution_mode": "publisher" } }), + ) + .await?; + assert_eq!(status, 200, "{body}"); + assert_eq!( + policy(&db, VALUE_ONLY).await?["on_behalf_of"], + "u/test-user-2" + ); + let (status, body) = update(&ws, VALUE_ONLY, USER, json!({ "value": { "v": 4 } })).await?; + assert_eq!(status, 200, "{body}"); + + // The multipart deploy of a full-code app is held to the same rule. + const RAW_VALUE_ONLY: &str = "u/test-user-2/raw_value_only"; + create_raw(&ws, ADMIN, RAW_VALUE_ONLY).await?; + let (status, body) = update_raw( + &ws, + RAW_VALUE_ONLY, + USER, + json!({ "value": { "files": {}, "runnables": {} } }), + ) + .await?; + assert_eq!(status, 403, "{body}"); + assert_eq!(versions(&db, RAW_VALUE_ONLY).await?, 1); + // ======================================== // 3. A policy without a mode keeps the deployed one. // ======================================== diff --git a/backend/windmill-api/openapi.yaml b/backend/windmill-api/openapi.yaml index 0f11e34000..cb0af49c6c 100644 --- a/backend/windmill-api/openapi.yaml +++ b/backend/windmill-api/openapi.yaml @@ -16905,7 +16905,7 @@ paths: summary: get app by path operationId: getAppByPath x-mcp-tool: true - x-mcp-instructions: "Returns the app's whole `value`, which is what updateApp needs: it takes the whole thing, not a patch. A big enough app is truncated by the tool-result limit; sending that back fails the build rather than deploying something partial, so edit those in the app editor or with the CLI. `raw_app` says whether this is a full-code app (its value holds `files`/`runnables`) or a low-code one (a `grid`), and only a full-code app can be deployed through MCP." + x-mcp-instructions: "Returns the app's whole `value` and its `policy`, which is what updateApp needs: it takes the whole thing, not a patch. A big enough app is truncated by the tool-result limit; sending that back fails the build rather than deploying something partial, so edit those in the app editor or with the CLI. `raw_app` says whether this is a full-code app (its value holds `files`/`runnables`) or a low-code one (a `grid`), and only a full-code app can be deployed through MCP." x-mcp-tool-include-query-params: [] tags: - app @@ -17369,7 +17369,7 @@ paths: operationId: updateAppRawSource x-mcp-tool: true x-mcp-tool-name: updateApp - x-mcp-instructions: "Use this to change a raw (full-code) app — an app whose `raw_app` field is true. Send the whole `value` (`files`, `runnables`, `data`), not a patch: read the current one with getAppByPath first and edit it. The sources are compiled on a worker by the same build the editor and the CLI run, so a compile error comes back as the error of this call. Compiling runs the app's own dependencies on a worker, so this tool can execute code there. Low-code apps are legacy and have no MCP tool at all — they are edited in their editor." + x-mcp-instructions: "Use this to change a raw (full-code) app — an app whose `raw_app` field is true. Send the whole `value` (`files`, `runnables`, `data`), not a patch: read the current one with getAppByPath first and edit it. Send the `policy` getAppByPath returned with it: an app that runs as someone else refuses a value sent without its policy unless you are an admin or in wm_deployers, and a deploy with its policy makes the app run as you. The sources are compiled on a worker by the same build the editor and the CLI run, so a compile error comes back as the error of this call. Compiling runs the app's own dependencies on a worker, so this tool can execute code there. Low-code apps are legacy and have no MCP tool at all — they are edited in their editor." x-mcp-tool-include-fields: - path - value diff --git a/backend/windmill-api/src/apps.rs b/backend/windmill-api/src/apps.rs index cbf8552495..3e4637fa63 100644 --- a/backend/windmill-api/src/apps.rs +++ b/backend/windmill-api/src/apps.rs @@ -2707,6 +2707,43 @@ fn policy_runs_as_another(policy: &Policy, authed: &ApiAuthed) -> bool { .is_some_and(|e| e != authed.email) } +/// A value deployed without a policy keeps whoever the app runs as, and the value is not inert +/// under that identity: the policy pins an inline script by the hash of its own code, while the +/// modules and lock it runs with, and a source deploy's pins, come from the value. So only +/// someone who may keep another's identity deploys a value alone; anyone else sends the policy, +/// which rebinds the app to them. An unreadable policy counts as another's. +fn refuse_value_without_policy( + path: &str, + deployed: Option, + authed: &ApiAuthed, +) -> Result<()> { + let Some(deployed) = deployed else { + return Ok(()); + }; + if windmill_common::can_preserve_on_behalf_of(authed) { + return Ok(()); + } + let deployed = serde_json::from_value::(deployed).ok(); + if deployed + .as_ref() + .is_some_and(|p| !policy_runs_as_another(p, authed)) + { + return Ok(()); + } + let runs_as = deployed + .as_ref() + .and_then(|p| { + p.on_behalf_of_email + .as_deref() + .or(p.on_behalf_of.as_deref()) + }) + .unwrap_or("another user"); + Err(Error::PermissionDenied(format!( + "app {path} runs as {runs_as}: only admins and members of wm_deployers can deploy it \ + without its policy, and a deploy with its policy makes it run as you" + ))) +} + async fn require_runnables_readable( authed: &ApiAuthed, db: &DB, @@ -3662,6 +3699,19 @@ async fn update_app_raw_source( if !ns.allow_kind_change.unwrap_or(false) { reject_kind_change(path, true, Some(deployed_raw_app))?; } + // Also before the compile; the binding check runs again under the row lock. + if ns.policy.is_none() && !windmill_common::can_preserve_on_behalf_of(&authed) { + let mut tx = user_db.clone().begin(&authed).await?; + let deployed = sqlx::query_scalar!( + "SELECT policy FROM app WHERE path = $1 AND workspace_id = $2 FOR UPDATE", + path, + w_id + ) + .fetch_optional(&mut *tx) + .await?; + tx.commit().await?; + refuse_value_without_policy(path, deployed, &authed)?; + } let bundled = apps_raw_bundle::bundle_raw_app_sources( &db, @@ -4061,8 +4111,8 @@ async fn update_app_internal<'a>( // Resolved on the (non-RLS) pool before the RLS transaction opens, for the reason // `create_app` states, with the same known, accepted rename race (see // `resolve_on_behalf_of`). Submitting a policy is how a deployer claims the app's execution - // identity; a source deploy that sent none claims nothing, so whoever the app already runs as - // stays. + // identity; a deploy that sent none claims nothing, so whoever the app already runs as + // stays, which `refuse_value_without_policy` allows only to who may keep it. let mut preserved_on_behalf_of: Option = None; if let Some(npolicy) = ns.policy.as_mut() { let should_preserve = ns.preserve_on_behalf_of.unwrap_or(false) @@ -4121,6 +4171,22 @@ async fn update_app_internal<'a>( reject_kind_change(path, raw_app, deployed_raw_app)?; } + // Under the row lock, so a policy naming someone else cannot land between this check and + // the version appended below. + if ns.value.is_some() + && ns.policy.is_none() + && !windmill_common::can_preserve_on_behalf_of(&authed) + { + let deployed = sqlx::query_scalar!( + "SELECT policy FROM app WHERE path = $1 AND workspace_id = $2 FOR UPDATE", + path, + w_id + ) + .fetch_optional(&mut *tx) + .await?; + refuse_value_without_policy(path, deployed, &authed)?; + } + let npath = if ns.policy.is_some() || ns.path.is_some() || ns.summary.is_some() diff --git a/backend/windmill-api/src/mcp/auto_generated_endpoints.rs b/backend/windmill-api/src/mcp/auto_generated_endpoints.rs index 079ee5e519..927946ad72 100644 --- a/backend/windmill-api/src/mcp/auto_generated_endpoints.rs +++ b/backend/windmill-api/src/mcp/auto_generated_endpoints.rs @@ -1238,7 +1238,7 @@ is, a different one moves it there and archives the old path"), EndpointTool { name: Cow::Borrowed("getAppByPath"), description: Cow::Borrowed("get app by path"), - instructions: Cow::Borrowed("Returns the app's whole `value`, which is what updateApp needs: it takes the whole thing, not a patch. A big enough app is truncated by the tool-result limit; sending that back fails the build rather than deploying something partial, so edit those in the app editor or with the CLI. `raw_app` says whether this is a full-code app (its value holds `files`/`runnables`) or a low-code one (a `grid`), and only a full-code app can be deployed through MCP."), + instructions: Cow::Borrowed("Returns the app's whole `value` and its `policy`, which is what updateApp needs: it takes the whole thing, not a patch. A big enough app is truncated by the tool-result limit; sending that back fails the build rather than deploying something partial, so edit those in the app editor or with the CLI. `raw_app` says whether this is a full-code app (its value holds `files`/`runnables`) or a low-code one (a `grid`), and only a full-code app can be deployed through MCP."), path: Cow::Borrowed("/w/{workspace}/apps/get/p/{path}"), method: Cow::Borrowed("GET"), path_params_schema: Some(serde_json::json!({ @@ -1370,7 +1370,7 @@ is, a different one moves it there and archives the old path"), EndpointTool { name: Cow::Borrowed("updateApp"), description: Cow::Borrowed("update a raw app from its sources, compiling them on a worker (which runs the app's own dependencies to do so)"), - instructions: Cow::Borrowed("Use this to change a raw (full-code) app — an app whose `raw_app` field is true. Send the whole `value` (`files`, `runnables`, `data`), not a patch: read the current one with getAppByPath first and edit it. The sources are compiled on a worker by the same build the editor and the CLI run, so a compile error comes back as the error of this call. Compiling runs the app's own dependencies on a worker, so this tool can execute code there. Low-code apps are legacy and have no MCP tool at all — they are edited in their editor."), + instructions: Cow::Borrowed("Use this to change a raw (full-code) app — an app whose `raw_app` field is true. Send the whole `value` (`files`, `runnables`, `data`), not a patch: read the current one with getAppByPath first and edit it. Send the `policy` getAppByPath returned with it: an app that runs as someone else refuses a value sent without its policy unless you are an admin or in wm_deployers, and a deploy with its policy makes the app run as you. The sources are compiled on a worker by the same build the editor and the CLI run, so a compile error comes back as the error of this call. Compiling runs the app's own dependencies on a worker, so this tool can execute code there. Low-code apps are legacy and have no MCP tool at all — they are edited in their editor."), path: Cow::Borrowed("/w/{workspace}/apps/update_raw_source/{path}"), method: Cow::Borrowed("POST"), path_params_schema: Some(serde_json::json!({ diff --git a/frontend/src/lib/mcpEndpointTools.ts b/frontend/src/lib/mcpEndpointTools.ts index 1c0a5d9798..c7ae7e43c8 100644 --- a/frontend/src/lib/mcpEndpointTools.ts +++ b/frontend/src/lib/mcpEndpointTools.ts @@ -1245,7 +1245,7 @@ export const mcpEndpointTools: EndpointTool[] = [ { name: "getAppByPath", description: "get app by path", - instructions: "Returns the app's whole `value`, which is what updateApp needs: it takes the whole thing, not a patch. A big enough app is truncated by the tool-result limit; sending that back fails the build rather than deploying something partial, so edit those in the app editor or with the CLI. `raw_app` says whether this is a full-code app (its value holds `files`/`runnables`) or a low-code one (a `grid`), and only a full-code app can be deployed through MCP.", + instructions: "Returns the app's whole `value` and its `policy`, which is what updateApp needs: it takes the whole thing, not a patch. A big enough app is truncated by the tool-result limit; sending that back fails the build rather than deploying something partial, so edit those in the app editor or with the CLI. `raw_app` says whether this is a full-code app (its value holds `files`/`runnables`) or a low-code one (a `grid`), and only a full-code app can be deployed through MCP.", path: "/w/{workspace}/apps/get/p/{path}", method: "GET", pathParamsSchema: { @@ -1377,7 +1377,7 @@ export const mcpEndpointTools: EndpointTool[] = [ { name: "updateApp", description: "update a raw app from its sources, compiling them on a worker (which runs the app's own dependencies to do so)", - instructions: "Use this to change a raw (full-code) app — an app whose `raw_app` field is true. Send the whole `value` (`files`, `runnables`, `data`), not a patch: read the current one with getAppByPath first and edit it. The sources are compiled on a worker by the same build the editor and the CLI run, so a compile error comes back as the error of this call. Compiling runs the app's own dependencies on a worker, so this tool can execute code there. Low-code apps are legacy and have no MCP tool at all — they are edited in their editor.", + instructions: "Use this to change a raw (full-code) app — an app whose `raw_app` field is true. Send the whole `value` (`files`, `runnables`, `data`), not a patch: read the current one with getAppByPath first and edit it. Send the `policy` getAppByPath returned with it: an app that runs as someone else refuses a value sent without its policy unless you are an admin or in wm_deployers, and a deploy with its policy makes the app run as you. The sources are compiled on a worker by the same build the editor and the CLI run, so a compile error comes back as the error of this call. Compiling runs the app's own dependencies on a worker, so this tool can execute code there. Low-code apps are legacy and have no MCP tool at all — they are edited in their editor.", path: "/w/{workspace}/apps/update_raw_source/{path}", method: "POST", pathParamsSchema: {