mirror of
https://github.com/windmill-labs/windmill.git
synced 2026-11-20 00:07:01 +00:00
fix: let non-admins read an admin-approved GitHub App repository (#12524)
* fix: let non-admins read an admin-approved GitHub App repository An admin's save of an app-backed git_repository resource records the url it resolves to in github_app_repo_approval. Non-admins who can read the resource are then served that repository through the installation, by repo_archive and git_commit_hash, until the url (or a $var: it names) changes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix: keep the approved repository url on the resource row A path-keyed approval table outlived its resource through every writer that moves or deletes resources by path (username rename, offboarding, deletes), so a new resource at a freed path could inherit an approval. On the row, it follows renames and dies with the resource. Also: an unresolvable $var: no longer fails an admin's save, and the admin-or-not caller reuses GitRemoteCaller. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix: record the approved repository after the write commits Resolving a $var: url takes a second pool connection and maybe a secret backend, which must not be held under the write's row lock. The update applies only while the row still holds the url the admin saved, and an admin save that names no app-backed repository clears the approval. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * chore: point the EE ref at the GitHub App approval branch Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix: keep approvals in a side table and approve only literal urls The approval moves from a resource column to github_app_repo_approval, whose FK cascades on resource renames and deletes like resource_version. It is now written in the admin's write transaction, from the saved row, so it commits with the value it approves and a delayed approval can no longer undo a later revocation. Only a save that carries the value, and only a literal url, is approved: a $var: resolves to whatever its owner sets, which the admin never saw. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * chore: update ee-repo-ref to 2d094f0018c2474cbc47f2e6e81f7d53e158f87d This commit updates the EE repository reference after PR #993 was merged in windmill-ee-private. Previous ee-repo-ref: 2080e6f837108b6a3c0439b96257df66583cc487 New ee-repo-ref: 2d094f0018c2474cbc47f2e6e81f7d53e158f87d Automated by sync-ee-ref workflow. --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: windmill-internal-app[bot] <windmill-internal-app[bot]@users.noreply.github.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
windmill-internal-app[bot]
parent
91de7252b8
commit
a718720fd6
+29
@@ -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"
|
||||
}
|
||||
+15
@@ -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"
|
||||
}
|
||||
+15
@@ -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"
|
||||
}
|
||||
@@ -1 +1 @@
|
||||
3389335a8f58de7a06ae0402ec1c4fdad3bf6122
|
||||
2d094f0018c2474cbc47f2e6e81f7d53e158f87d
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
DROP TABLE IF EXISTS github_app_repo_approval;
|
||||
@@ -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;
|
||||
@@ -5330,6 +5330,7 @@ async fn poll_git_auto_pull_inner(db: &Pool<Postgres>) -> 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]),
|
||||
] {
|
||||
|
||||
@@ -760,6 +760,127 @@ async fn test_resource_cache_handles_job_context(db: Pool<Postgres>) -> 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<Postgres>,
|
||||
) -> 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.
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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?;
|
||||
|
||||
Reference in New Issue
Block a user