diff --git a/backend/.sqlx/query-4cc61e16a7f80dbc77462430b335ff08a9b4d2721f886995d9734f46b46d2f08.json b/backend/.sqlx/query-4cc61e16a7f80dbc77462430b335ff08a9b4d2721f886995d9734f46b46d2f08.json new file mode 100644 index 0000000000..b3eb5a13a3 --- /dev/null +++ b/backend/.sqlx/query-4cc61e16a7f80dbc77462430b335ff08a9b4d2721f886995d9734f46b46d2f08.json @@ -0,0 +1,29 @@ +{ + "db_name": "PostgreSQL", + "query": "SELECT r.value, a.url AS \"approved_url?\"\n FROM resource r\n LEFT JOIN github_app_repo_approval a\n ON a.workspace_id = r.workspace_id AND a.path = r.path\n WHERE r.workspace_id = $1 AND r.path = $2", + "describe": { + "columns": [ + { + "ordinal": 0, + "name": "value", + "type_info": "Jsonb" + }, + { + "ordinal": 1, + "name": "approved_url?", + "type_info": "Text" + } + ], + "parameters": { + "Left": [ + "Text", + "Text" + ] + }, + "nullable": [ + true, + false + ] + }, + "hash": "4cc61e16a7f80dbc77462430b335ff08a9b4d2721f886995d9734f46b46d2f08" +} diff --git a/backend/.sqlx/query-7074277ce924e9a1312a430d958d16395c83dfa58164b432e3a41bb9063fc9fa.json b/backend/.sqlx/query-7074277ce924e9a1312a430d958d16395c83dfa58164b432e3a41bb9063fc9fa.json new file mode 100644 index 0000000000..aa8c3d3329 --- /dev/null +++ b/backend/.sqlx/query-7074277ce924e9a1312a430d958d16395c83dfa58164b432e3a41bb9063fc9fa.json @@ -0,0 +1,15 @@ +{ + "db_name": "PostgreSQL", + "query": "DELETE FROM github_app_repo_approval WHERE workspace_id = $1 AND path = $2", + "describe": { + "columns": [], + "parameters": { + "Left": [ + "Text", + "Text" + ] + }, + "nullable": [] + }, + "hash": "7074277ce924e9a1312a430d958d16395c83dfa58164b432e3a41bb9063fc9fa" +} diff --git a/backend/.sqlx/query-c57072d3a127884226ab21b6bda868cdf383eeff4b6c66879ca97df949696886.json b/backend/.sqlx/query-c57072d3a127884226ab21b6bda868cdf383eeff4b6c66879ca97df949696886.json new file mode 100644 index 0000000000..0d1f13e6d8 --- /dev/null +++ b/backend/.sqlx/query-c57072d3a127884226ab21b6bda868cdf383eeff4b6c66879ca97df949696886.json @@ -0,0 +1,15 @@ +{ + "db_name": "PostgreSQL", + "query": "INSERT INTO github_app_repo_approval (workspace_id, path, url)\n SELECT workspace_id, path, value->>'url' FROM resource\n WHERE workspace_id = $1 AND path = $2\n AND value->'is_github_app' = 'true'::jsonb\n AND left(value->>'url', 1) <> '$'", + "describe": { + "columns": [], + "parameters": { + "Left": [ + "Text", + "Text" + ] + }, + "nullable": [] + }, + "hash": "c57072d3a127884226ab21b6bda868cdf383eeff4b6c66879ca97df949696886" +} diff --git a/backend/ee-repo-ref.txt b/backend/ee-repo-ref.txt index 772be70dfa..335bc3cf07 100644 --- a/backend/ee-repo-ref.txt +++ b/backend/ee-repo-ref.txt @@ -1 +1 @@ -3389335a8f58de7a06ae0402ec1c4fdad3bf6122 +2d094f0018c2474cbc47f2e6e81f7d53e158f87d diff --git a/backend/migrations/20261118160046_github_app_repo_approval.down.sql b/backend/migrations/20261118160046_github_app_repo_approval.down.sql new file mode 100644 index 0000000000..13a4c08fb2 --- /dev/null +++ b/backend/migrations/20261118160046_github_app_repo_approval.down.sql @@ -0,0 +1 @@ +DROP TABLE IF EXISTS github_app_repo_approval; diff --git a/backend/migrations/20261118160046_github_app_repo_approval.up.sql b/backend/migrations/20261118160046_github_app_repo_approval.up.sql new file mode 100644 index 0000000000..3c14bf0aa2 --- /dev/null +++ b/backend/migrations/20261118160046_github_app_repo_approval.up.sql @@ -0,0 +1,16 @@ +-- The repository URL a workspace admin last saved on an app-backed git_repository +-- resource. Anyone who can write the resource controls its URL, so the GitHub App +-- installation serves a non-admin only the repository this names. The cascading FK +-- carries it through every rename and takes it with a delete, so a new resource at +-- a freed path never inherits an approval. +CREATE TABLE github_app_repo_approval ( + workspace_id VARCHAR(50) NOT NULL, + path VARCHAR(255) NOT NULL, + url TEXT NOT NULL, + PRIMARY KEY (workspace_id, path), + FOREIGN KEY (workspace_id, path) REFERENCES resource (workspace_id, path) + ON DELETE CASCADE ON UPDATE CASCADE +); + +GRANT ALL ON github_app_repo_approval TO windmill_user; +GRANT ALL ON github_app_repo_approval TO windmill_admin; diff --git a/backend/src/monitor.rs b/backend/src/monitor.rs index 08c5e6b352..66d20a6bc5 100644 --- a/backend/src/monitor.rs +++ b/backend/src/monitor.rs @@ -5330,6 +5330,7 @@ async fn poll_git_auto_pull_inner(db: &Pool) -> error::Result<()> { db, &row.workspace_id, &repo.git_repo_resource_path, + windmill_common::ssrf::GitRemoteCaller::AdminOrSystem, ) .await } @@ -8531,7 +8532,9 @@ mod loggable_setting_value_tests { fn never_prints_string_or_structured_values() { let token = "ghp_secret_token"; for v in [ - json!(format!("[install.scopes]\n\"@org\" = {{ token = \"{token}\" }}")), + json!(format!( + "[install.scopes]\n\"@org\" = {{ token = \"{token}\" }}" + )), json!({ "registry": { "token": token } }), json!([token]), ] { diff --git a/backend/windmill-api-integration-tests/tests/resources.rs b/backend/windmill-api-integration-tests/tests/resources.rs index ebca295339..9f465eed26 100644 --- a/backend/windmill-api-integration-tests/tests/resources.rs +++ b/backend/windmill-api-integration-tests/tests/resources.rs @@ -760,6 +760,127 @@ async fn test_resource_cache_handles_job_context(db: Pool) -> anyhow:: Ok(()) } +/// The installation serves a non-admin only the URL an admin last saved on an +/// app-backed resource, so a write by anyone else must never set it, and a new +/// resource where an approved one used to be must not inherit it. +#[sqlx::test(migrations = "../migrations", fixtures("base"))] +async fn test_github_app_repo_approval_follows_admin_writes( + db: Pool, +) -> anyhow::Result<()> { + initialize_tracing().await; + let server = ApiServer::start(db.clone()).await?; + let base = format!( + "http://localhost:{}/api/w/test-workspace/resources", + server.addr.port() + ); + let (path, renamed) = ("u/test-user-2/repo", "u/test-user-2/repo2"); + let (approved, other) = ( + "https://github.com/o/r.git", + "https://github.com/o/other.git", + ); + let value = |url: &str| json!({"value": {"url": url, "is_github_app": true}}); + let create = |url: &str| { + let mut body = value(url); + body["path"] = json!(path); + body["resource_type"] = json!("git_repository"); + body + }; + let post = |token: &'static str, url: String, body: serde_json::Value| async move { + let resp = client() + .post(url) + .bearer_auth(token) + .json(&body) + .send() + .await + .unwrap(); + assert!(resp.status().is_success(), "{}", resp.text().await.unwrap()); + }; + let approved_url = |path: &'static str| { + let db = db.clone(); + async move { + sqlx::query_scalar::<_, String>( + "SELECT url FROM github_app_repo_approval + WHERE workspace_id = 'test-workspace' AND path = $1", + ) + .bind(path) + .fetch_optional(&db) + .await + .unwrap() + } + }; + + post("SECRET_TOKEN_2", format!("{base}/create"), create(approved)).await; + assert_eq!( + approved_url(path).await, + None, + "a non-admin's write approves nothing" + ); + + // Only a save of the value approves: an admin renaming or describing a + // resource has not looked at its URL. + post( + "SECRET_TOKEN", + format!("{base}/update/{path}"), + json!({"description": "ok"}), + ) + .await; + assert_eq!(approved_url(path).await, None); + let mut save = value(approved); + save["description"] = json!("ok"); + post("SECRET_TOKEN", format!("{base}/update/{path}"), save).await; + assert_eq!(approved_url(path).await.as_deref(), Some(approved)); + + post( + "SECRET_TOKEN_2", + format!("{base}/update_value/{path}"), + value(other), + ) + .await; + assert_eq!(approved_url(path).await.as_deref(), Some(approved)); + + post( + "SECRET_TOKEN", + format!("{base}/update_value/{path}"), + value(other), + ) + .await; + assert_eq!(approved_url(path).await.as_deref(), Some(other)); + + post( + "SECRET_TOKEN_2", + format!("{base}/update/{path}"), + json!({"path": renamed}), + ) + .await; + assert_eq!(approved_url(renamed).await.as_deref(), Some(other)); + post("SECRET_TOKEN_2", format!("{base}/create"), create(other)).await; + assert_eq!( + approved_url(path).await, + None, + "nothing is inherited from the old path" + ); + + // An admin save that names no app-backed repository revokes the old one, so a + // non-admin cannot put the earlier URL back and be served it. + post( + "SECRET_TOKEN", + format!("{base}/update_value/{renamed}"), + json!({"value": {"url": other, "is_github_app": false}}), + ) + .await; + assert_eq!(approved_url(renamed).await, None); + + // A `$var:` resolves to whatever its owner sets, so it is never approved. + post( + "SECRET_TOKEN", + format!("{base}/update_value/{path}"), + value("$var:u/test-user-2/repo_url"), + ) + .await; + assert_eq!(approved_url(path).await, None); + Ok(()) +} + /// Without an embeddings index the resource type search reads the table, so it has to apply the /// same scope as the index: the workspace's own types and the global ones in `admins`, never /// another workspace's. diff --git a/backend/windmill-common/src/git_sync_oss.rs b/backend/windmill-common/src/git_sync_oss.rs index 4c7de4fa7b..ef97bc249b 100644 --- a/backend/windmill-common/src/git_sync_oss.rs +++ b/backend/windmill-common/src/git_sync_oss.rs @@ -143,6 +143,40 @@ pub fn prepend_token_to_github_url( )) } +/// Make the URL an admin just saved on an app-backed `git_repository` +/// resource the only repository the GitHub App installation serves non-admins +/// through it, or revoke it when the save names none. Run in the admin's write +/// transaction, after a write that carries the value, so the approval commits +/// with the value it approves and no later write can be overtaken by it. +/// +/// Only a literal URL is approved. A `$var:` resolves to whatever its owner +/// sets, which the admin never saw, so such a resource stays admin-only. +pub async fn approve_github_app_repo( + tx: &mut sqlx::PgConnection, + w_id: &str, + path: &str, +) -> crate::error::Result<()> { + sqlx::query!( + "DELETE FROM github_app_repo_approval WHERE workspace_id = $1 AND path = $2", + w_id, + path + ) + .execute(&mut *tx) + .await?; + sqlx::query!( + "INSERT INTO github_app_repo_approval (workspace_id, path, url) + SELECT workspace_id, path, value->>'url' FROM resource + WHERE workspace_id = $1 AND path = $2 + AND value->'is_github_app' = 'true'::jsonb + AND left(value->>'url', 1) <> '$'", + w_id, + path + ) + .execute(&mut *tx) + .await?; + Ok(()) +} + #[cfg(test)] mod tests { use super::sanitize_git_url; diff --git a/backend/windmill-common/src/ssrf.rs b/backend/windmill-common/src/ssrf.rs index c95137a8bb..b670a08d2c 100644 --- a/backend/windmill-common/src/ssrf.rs +++ b/backend/windmill-common/src/ssrf.rs @@ -216,7 +216,8 @@ fn allow_local_git_remotes() -> bool { } /// Who a git call is made for, which decides whether it may reach a host on a -/// private network. +/// private network, and whether the GitHub App installation serves it any +/// app-backed repository or only one an admin approved. #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum GitRemoteCaller { /// A workspace admin's request, or Windmill's own work (polling, webhook and diff --git a/backend/windmill-store/src/resources.rs b/backend/windmill-store/src/resources.rs index 14382bac1c..85600006ce 100644 --- a/backend/windmill-store/src/resources.rs +++ b/backend/windmill-store/src/resources.rs @@ -1446,6 +1446,10 @@ async fn create_resource( None, ) .await?; + if authed.is_admin { + windmill_common::git_sync_oss::approve_github_app_repo(&mut *tx, &w_id, &resource.path) + .await?; + } tx.commit().await?; handle_deployment_metadata( @@ -2409,6 +2413,9 @@ async fn update_resource( None, ) .await?; + if authed.is_admin && ns.value.is_some() { + windmill_common::git_sync_oss::approve_github_app_repo(&mut *tx, &w_id, &npath).await?; + } tx.commit().await?; // Detect if this was a rename operation @@ -2683,6 +2690,9 @@ async fn set_resource_value( None, ) .await?; + if authed.is_admin { + windmill_common::git_sync_oss::approve_github_app_repo(&mut *tx, w_id, path).await?; + } tx.commit().await?; note_resource_write(w_id, path, &resource_type); @@ -4101,26 +4111,27 @@ async fn get_git_commit_hash( let Some(git_repo_resource_value) = git_repo_resource_value else { return Err(Error::NotFound(format!("Resource {} not found", path)).into()); }; + let caller = if authed.is_admin { + GitRemoteCaller::AdminOrSystem + } else { + GitRemoteCaller::NonAdmin + }; // App-backed repos store a tokenless URL, so the `ls-remote` below can't // authenticate. Reuse the poller's REST head lookup, which mints an // installation token server-side rather than embedding one in a URL here. // It returns `None` for a repo that isn't app-backed, which is exactly the - // ls-remote case below. - // - // Admin-only for the same reason as the archive route: the repository is - // named by the resource's own `url`, which anyone with write on its path - // controls, so reading it would let a caller aim the installation - // credential at any repository it can reach. + // ls-remote case below. It serves a non-admin only the repository an admin + // approved on the resource. #[cfg(all(feature = "enterprise", feature = "private"))] if git_repo_resource_value .get("is_github_app") .and_then(|v| v.as_bool()) .unwrap_or(false) { - require_admin(authed.is_admin, &authed.username)?; if let Some((_, commit_hash)) = - windmill_common::git_sync_ee::get_app_repo_head_for_autopull(&db, &w_id, path).await? + windmill_common::git_sync_ee::get_app_repo_head_for_autopull(&db, &w_id, path, caller) + .await? { return Ok(Json(GitCommitHashResponse { commit_hash })); } @@ -4130,18 +4141,14 @@ async fn get_git_commit_hash( .map_err(|e| { Error::BadRequest(format!("Invalid git repository resource format: {}", e)) })?; - let caller = if authed.is_admin { - GitRemoteCaller::AdminOrSystem - } else { - GitRemoteCaller::NonAdmin - }; git_resource.url = resolve_azure_devops_url(&db_with_opt_authed, &w_id, &git_resource.url, false, caller) .await?; // A credential is stored under the repository it was issued for, so a // resource repointed elsewhere finds none. Which credential can be attached - // is bounded by that; who may use it is bounded here, on the same terms as - // the installation credential above. + // is bounded by that; who may use it is bounded here: admins only, since + // nothing records an admin's approval of the URL as for the installation + // credential above. let plain_url = git_resource.url.clone(); git_resource.url = windmill_common::git_sync_oss::with_stored_credential(&db, &w_id, git_resource.url).await?;