Coverage / crates/hub/src/protocol.rs 94.3%
496 of 526 lines run, 104 of 212 functions · run #469 on main · run missed
| 1 | //! The wire protocol, embedded (hub spec §3.3, §5.4): `oxy-server`'s | |
| 2 | //! `Server` over a backend that resolves `owner/name`, authorises reads | |
| 3 | //! and pushes by account role and token scope, and records who pushed. | |
| 4 | | |
| 5 | use crate::auth::{self, Access, Role}; | |
| 6 | use crate::db::{self, accounts::Account}; | |
| 7 | use crate::repos::{RepoName, Repos}; | |
| 8 | use crate::Shared; | |
| 9 | use axum::body::Bytes; | |
| 10 | use axum::extract::{Path, State}; | |
| 11 | use axum::http::{HeaderMap, Method, StatusCode}; | |
| 12 | use axum::response::{IntoResponse, Response}; | |
| 13 | use oxy_core::repo::{PushReport, Repo}; | |
| 14 | use oxy_server::{Backend, Endpoint, Failure}; | |
| 15 | use std::sync::Arc; | |
| 16 | ||
| 17 | pub struct HubBackend { | |
| 18 | repos: Arc<Repos>, | |
| 19 | /// Wikis, reached as `OWNER/NAME.wiki` (hub spec §6.11). | |
| 20 | wikis: Arc<Repos>, | |
| 21 | pool: db::Pool, | |
| 22 | /// Site tokens from `hub.toml`: full access, for bootstrapping. | |
| 23 | site_tokens: Vec<String>, | |
| 24 | runtime: tokio::runtime::Handle, | |
| 25 | /// The application, set once it exists (the backend is built first). | |
| 26 | app_hint: Arc<std::sync::Mutex<Option<std::sync::Weak<crate::App>>>>, | |
| 27 | /// The hub's key, which countersigns when it accepted an update a key | |
| 28 | /// it certified signed (oxygenhub#241 step 4c, Oxygen spec §14.7). | |
| 29 | acceptor: Option<Arc<(oxy_core::identity::Identity, oxy_core::identity::SecretKey)>>, | |
| 30 | /// Push protection's scan budget (#268, `[limits]`). | |
| 31 | scan_budget: crate::push_protection::Budget, | |
| 32 | /// Repositories whose push protection scan stopped early, by id: the | |
| 33 | /// note goes on the push log entry of the push that moves references | |
| 34 | /// (the parts of a long push before it move none). | |
| 35 | scan_notes: Arc<std::sync::Mutex<std::collections::HashMap<i64, String>>>, | |
| 36 | /// The keys that signed what a push to each repository (by name) | |
| 37 | /// brought, noted when it is vetted and recorded once it is accepted | |
| 38 | /// (#286, Settings › Keys' "last used"). | |
| 39 | signers: Arc<std::sync::Mutex<std::collections::HashMap<String, std::collections::BTreeSet<String>>>>, | |
| 40 | } | |
| 41 | ||
| 42 | /// Who a request's credential resolved to. | |
| 43 | enum Caller { | |
| 44 | Anonymous, | |
| 45 | /// A site token from the configuration. | |
| 46 | Site, | |
| 47 | Token(Box<Account>, Box<db::accounts::Token>), | |
| 48 | /// A running OxygenFlow job: reads its own repository and the subrepos | |
| 49 | /// its state pins, nothing else (hub spec §13.2). | |
| 50 | Job { | |
| 51 | repo_id: i64, | |
| 52 | also: Vec<i64>, | |
| 53 | }, | |
| 54 | } | |
| 55 | ||
| 56 | impl HubBackend { | |
| 57 | 190× | pub fn new( |
| 58 | 190× | repos: Arc<Repos>, |
| 59 | 190× | wikis: Arc<Repos>, |
| 60 | 190× | pool: db::Pool, |
| 61 | 190× | site_tokens: Vec<String>, |
| 62 | 190× | runtime: tokio::runtime::Handle, |
| 63 | 190× | ) -> HubBackend { |
| 64 | 190× | HubBackend { |
| 65 | 190× | repos, |
| 66 | 190× | wikis, |
| 67 | 190× | pool, |
| 68 | 190× | site_tokens, |
| 69 | 190× | runtime, |
| 70 | 190× | app_hint: Arc::new(std::sync::Mutex::new(None)), |
| 71 | 190× | acceptor: None, |
| 72 | 190× | scan_budget: crate::push_protection::Budget::default(), |
| 73 | 190× | scan_notes: Arc::default(), |
| 74 | 190× | signers: Arc::default(), |
| 75 | 190× | } |
| 76 | 190× | } |
| 77 | | |
| 78 | /// Scan pushes for secrets within `budget` (#268). | |
| 79 | 190× | pub fn with_scan_budget(mut self, budget: crate::push_protection::Budget) -> Self { |
| 80 | 190× | self.scan_budget = budget; |
| 81 | 190× | self |
| 82 | 190× | } |
| 83 | | |
| 84 | /// Who pushes, for the push log: the account (id 0 for a site token) | |
| 85 | /// and, for an agent's token, the agent's name. | |
| 86 | 253× | fn pusher(&self, headers: &HeaderMap) -> (Option<(i64, String)>, String) { |
| 87 | 253× | match self.caller(headers) { |
| 88 | 40× | Ok(Caller::Token(account, token)) => { |
| 89 | 40× | (Some((account.id, account.username)), if token.agent { token.name.clone() } else { String::new() }) |
| 90 | } | |
| 91 | 213× | Ok(Caller::Site) => (Some((0, "site token".to_string())), String::new()), |
| 92 | 0× | _ => (None, String::new()), |
| 93 | } | |
| 94 | 253× | } |
| 95 | | |
| 96 | /// Countersign accepted updates by keys this hub certified with `identity`. | |
| 97 | 190× | pub fn with_acceptor(mut self, identity: oxy_core::identity::Identity, secret: oxy_core::identity::SecretKey) -> Self { |
| 98 | 190× | self.acceptor = Some(Arc::new((identity, secret))); |
| 99 | 190× | self |
| 100 | 190× | } |
| 101 | | |
| 102 | /// The slot the application registers itself in after construction. | |
| 103 | 190× | pub fn app_slot(&self) -> Arc<std::sync::Mutex<Option<std::sync::Weak<crate::App>>>> { |
| 104 | 190× | self.app_hint.clone() |
| 105 | 190× | } |
| 106 | | |
| 107 | 4467× | fn block<T>(&self, f: impl std::future::Future<Output = T>) -> T { |
| 108 | 4467× | tokio::task::block_in_place(|| self.runtime.block_on(f)) |
| 109 | 4467× | } |
| 110 | | |
| 111 | 3480× | fn row(&self, name: &RepoName) -> Result<db::repos::RepoRow, Failure> { |
| 112 | 3480× | let pool = self.pool.clone(); |
| 113 | 3480× | match self.block(db::repos::get(&pool, &name.owner, &name.name)) { |
| 114 | 3450× | Ok(Some(r)) => Ok(r), |
| 115 | // A transferred repository still answers to its old name (#108). | |
| 116 | 30× | Ok(None) => match self.block(db::repos::moved_to(&pool, &name.owner, &name.name)) { |
| 117 | 29× | Ok(Some(r)) => Ok(r), |
| 118 | 1× | Ok(None) => Err(not_found()), |
| 119 | 0× | Err(e) => Err(Failure(StatusCode::INTERNAL_SERVER_ERROR, e.to_string())), |
| 120 | }, | |
| 121 | 0× | Err(e) => Err(Failure(StatusCode::INTERNAL_SERVER_ERROR, e.to_string())), |
| 122 | } | |
| 123 | 3480× | } |
| 124 | | |
| 125 | 1304× | fn caller(&self, headers: &HeaderMap) -> Result<Caller, Failure> { |
| 126 | 1304× | let Some(presented) = oxy_server::bearer_token(headers) else { return Ok(Caller::Anonymous) }; |
| 127 | 1202× | if self.site_tokens.iter().any(|t| auth::mac_equal(t, presented)) { |
| 128 | 1005× | return Ok(Caller::Site); |
| 129 | 197× | } |
| 130 | 197× | let hash = auth::token_hash(presented); |
| 131 | 197× | let pool = self.pool.clone(); |
| 132 | 197× | if presented.starts_with(crate::flow::JOB_TOKEN_PREFIX) { |
| 133 | 12× | let job = self.block(db::flow::job_by_token(&pool, &hash, crate::flow::JOB_TOKEN_MAX_AGE_MS)).map_err(internal)?; |
| 134 | 12× | let Some(job) = job else { return Err(Failure(StatusCode::UNAUTHORIZED, "this job token is not valid".into())) }; |
| 135 | 10× | let run = self.block(db::flow::run_by_id(&pool, job.run_id)).map_err(internal)?; |
| 136 | 10× | let Some(run) = run else { return Err(Failure(StatusCode::UNAUTHORIZED, "this job token is not valid".into())) }; |
| 137 | 10× | return Ok(Caller::Job { repo_id: run.repo_id, also: job.also_read_ids() }); |
| 138 | 185× | } |
| 139 | // An agent's grant token (keyless agents, #241): its owner's | |
| 140 | // account, as an agent's token, writing when the grant lets its | |
| 141 | // keys move references. Revoking the grant stops it at once. | |
| 142 | 185× | let grant_token = presented.starts_with(crate::keyless::GRANT_TOKEN_PREFIX); |
| 143 | 185× | let token = if grant_token { |
| 144 | 9× | let grant = self.block(db::accounts::agent_grant_by_token(&pool, &hash)).map_err(internal)?; |
| 145 | 9× | let Some(g) = grant.filter(|g| g.live(db::now_ms())) else { |
| 146 | 1× | return Err(Failure(StatusCode::UNAUTHORIZED, "this agent grant is not valid (revoked, ended or unknown)".into())); |
| 147 | }; | |
| 148 | 8× | let _ = self.block(db::accounts::touch_agent_grant(&pool, g.id)); |
| 149 | Some(db::accounts::Token { | |
| 150 | id: 0, | |
| 151 | 8× | account_id: g.account_id, |
| 152 | 8× | name: g.agent.clone(), |
| 153 | 8× | hash: String::new(), |
| 154 | 16× | scopes: if g.scope.split(',').any(|s| s.trim() == "refs") { "repo:read,repo:write" } else { "repo:read" }.into(), |
| 155 | 8× | repos: String::new(), |
| 156 | 8× | created_ms: g.created_ms, |
| 157 | 8× | expires_ms: Some(g.expires_ms), |
| 158 | 8× | last_used_ms: g.last_used_ms, |
| 159 | 8× | renew_ms: None, |
| 160 | agent: true, | |
| 161 | }) | |
| 162 | } else { | |
| 163 | 176× | self.block(db::accounts::token_by_hash(&pool, &hash)).map_err(internal)? |
| 164 | }; | |
| 165 | 184× | let Some(token) = token else { return Err(Failure(StatusCode::UNAUTHORIZED, "unknown token".into())) }; |
| 166 | 184× | if token.expired(db::now_ms()) { |
| 167 | 0× | return Err(Failure(StatusCode::UNAUTHORIZED, "this token has expired".into())); |
| 168 | 184× | } |
| 169 | 184× | let account = self.block(db::accounts::account_by_id(&pool, token.account_id)).map_err(internal)?; |
| 170 | 184× | let Some(account) = account else { return Err(Failure(StatusCode::UNAUTHORIZED, "unknown token".into())) }; |
| 171 | 184× | if account.banned { |
| 172 | 0× | return Err(Failure(StatusCode::UNAUTHORIZED, "this account is banned".into())); |
| 173 | 184× | } |
| 174 | 184× | if account.disabled { |
| 175 | 0× | return Err(Failure(StatusCode::UNAUTHORIZED, "this account is disabled; contact the hub's administrator".into())); |
| 176 | 184× | } |
| 177 | 184× | if !grant_token { |
| 178 | 176× | let _ = self.block(db::accounts::touch_token(&pool, token.id)); |
| 179 | 176× | } |
| 180 | 184× | Ok(Caller::Token(Box::new(account), Box::new(token))) |
| 181 | 1304× | } |
| 182 | } | |
| 183 | ||
| 184 | 0× | fn internal(e: sqlx::Error) -> Failure { |
| 185 | 0× | Failure(StatusCode::INTERNAL_SERVER_ERROR, e.to_string()) |
| 186 | 0× | } |
| 187 | ||
| 188 | 9× | fn not_found() -> Failure { |
| 189 | 9× | Failure(StatusCode::NOT_FOUND, "no such repository".into()) |
| 190 | 9× | } |
| 191 | ||
| 192 | impl Backend for HubBackend { | |
| 193 | 1036× | fn open(&self, name: &str) -> Result<Repo, Failure> { |
| 194 | 1036× | let parsed = RepoName::parse(name).ok_or_else(not_found)?; |
| 195 | // `NAME.wiki` is the repository's wiki (§6.11); the row knows the | |
| 196 | // current name, so a transferred repository still answers to its | |
| 197 | // old one (#108). | |
| 198 | 1036× | let (parent, is_wiki) = crate::wiki::parent_of(&parsed); |
| 199 | 1036× | let row = self.row(&parent)?; |
| 200 | 1036× | let name = RepoName::new(&row.owner, &row.name).ok_or_else(not_found)?; |
| 201 | 1036× | let store = if is_wiki { &self.wikis } else { &self.repos }; |
| 202 | 1036× | let repo = store.open(&name).map_err(|e| match e { |
| 203 | 1× | crate::repos::RepoError::NotFound => not_found(), |
| 204 | 0× | other => Failure(StatusCode::INTERNAL_SERVER_ERROR, other.to_string()), |
| 205 | 1× | })?; |
| 206 | 1035× | Ok(match &self.acceptor { |
| 207 | 1035× | Some(a) => repo.with_acceptor(a.0.clone(), a.1.clone()), |
| 208 | 0× | None => repo, |
| 209 | }) | |
| 210 | 1036× | } |
| 211 | | |
| 212 | 1052× | fn authorize(&self, name: &str, access: oxy_server::Access, headers: &HeaderMap) -> Result<(), Failure> { |
| 213 | 1052× | let parsed = RepoName::parse(name).ok_or_else(not_found)?; |
| 214 | 1052× | let (parent, _) = crate::wiki::parent_of(&parsed); |
| 215 | 1052× | let row = self.row(&parent)?; |
| 216 | 1051× | let caller = self.caller(headers)?; |
| 217 | 1048× | if let Caller::Job { repo_id, also } = &caller { |
| 218 | // Only reads: of the job's own repository, and of the subrepos its | |
| 219 | // state pins that its pusher could read when the job was claimed. | |
| 220 | 9× | return match access { |
| 221 | 9× | oxy_server::Access::Read if *repo_id == row.id || also.contains(&row.id) => Ok(()), |
| 222 | 3× | oxy_server::Access::Read => Err(not_found()), |
| 223 | 1× | oxy_server::Access::Push => Err(Failure(StatusCode::FORBIDDEN, "a job token cannot push".into())), |
| 224 | }; | |
| 225 | 1038× | } |
| 226 | 1038× | let (role, scope_ok) = match &caller { |
| 227 | 0× | Caller::Job { .. } => unreachable!(), |
| 228 | 792× | Caller::Site => (Some(Role::Admin), true), |
| 229 | 102× | Caller::Anonymous => (self.block(auth::roles::role_for(&self.pool, None, &row)).map_err(internal)?, true), |
| 230 | 144× | Caller::Token(account, token) => { |
| 231 | 144× | let role = self.block(auth::roles::role_for(&self.pool, Some(account), &row)).map_err(internal)?; |
| 232 | 144× | let needed = match access { |
| 233 | oxy_server::Access::Read => { | |
| 234 | 102× | token.has_scope("repo:read") || token.has_scope("repo:write") || token.has_scope("repo:admin") |
| 235 | } | |
| 236 | 42× | oxy_server::Access::Push => token.has_scope("repo:write") || token.has_scope("repo:admin"), |
| 237 | }; | |
| 238 | 144× | let current = format!("{}/{}", row.owner, row.name); |
| 239 | 144× | let covered = token.covers(&parsed.to_string()) || token.covers(¤t) || token.covers(&parent.to_string()); |
| 240 | 144× | (role, needed && covered) |
| 241 | } | |
| 242 | }; | |
| 243 | // An unreadable repository does not exist for the caller (hub spec §4.4). | |
| 244 | 1038× | if !auth::roles::allows(role, Access::Read) { |
| 245 | 4× | return Err(not_found()); |
| 246 | 1034× | } |
| 247 | // A wiki is made, empty and unbound, when someone who may write to | |
| 248 | // its repository first reaches it, so the first page can be pushed | |
| 249 | // as well as written on the web (§6.11); the push gives it its id. | |
| 250 | 1034× | let (_, is_wiki) = crate::wiki::parent_of(&parsed); |
| 251 | 1034× | let may_write = match &caller { |
| 252 | 792× | Caller::Site => true, |
| 253 | 143× | Caller::Token(_, token) => token.has_scope("repo:write") || token.has_scope("repo:admin"), |
| 254 | 99× | _ => false, |
| 255 | }; | |
| 256 | 1034× | if is_wiki && may_write && auth::roles::allows(role, Access::Write) && !row.archived { |
| 257 | 6× | if let Some(name) = RepoName::new(&row.owner, &row.name) { |
| 258 | 6× | if !self.wikis.exists(&name) { |
| 259 | 1× | self.wikis |
| 260 | 1× | .create(&name, None) |
| 261 | 1× | .map_err(|e| Failure(StatusCode::INTERNAL_SERVER_ERROR, format!("make the wiki: {e}")))?; |
| 262 | 5× | } |
| 263 | 0× | } |
| 264 | 1028× | } |
| 265 | 775× | match access { |
| 266 | 775× | oxy_server::Access::Read if scope_ok => Ok(()), |
| 267 | 0× | oxy_server::Access::Read => Err(Failure(StatusCode::FORBIDDEN, "this token cannot read repositories".into())), |
| 268 | oxy_server::Access::Push => { | |
| 269 | 259× | if matches!(caller, Caller::Anonymous) { |
| 270 | 1× | return Err(Failure(StatusCode::UNAUTHORIZED, "a personal access token is required to push".into())); |
| 271 | 258× | } |
| 272 | 258× | if !auth::roles::allows(role, Access::Write) { |
| 273 | 1× | return Err(Failure(StatusCode::FORBIDDEN, "you do not have write access to this repository".into())); |
| 274 | 257× | } |
| 275 | 257× | if row.archived { |
| 276 | 1× | return Err(Failure(StatusCode::FORBIDDEN, "this repository is archived and read-only".into())); |
| 277 | 256× | } |
| 278 | 256× | if !scope_ok { |
| 279 | 1× | return Err(Failure(StatusCode::FORBIDDEN, "this token lacks the repo:write scope for this repository".into())); |
| 280 | 255× | } |
| 281 | 255× | Ok(()) |
| 282 | } | |
| 283 | } | |
| 284 | 1052× | } |
| 285 | | |
| 286 | 578× | fn default_bookmark(&self, name: &str) -> Option<String> { |
| 287 | 578× | let parsed = RepoName::parse(name)?; |
| 288 | 578× | if crate::wiki::parent_of(&parsed).1 { |
| 289 | 6× | return Some(crate::wiki::BOOKMARK.into()); |
| 290 | 572× | } |
| 291 | 572× | self.row(&parsed).ok().map(|r| r.default_bookmark) |
| 292 | 578× | } |
| 293 | | |
| 294 | 337× | fn add_log(&self, name: &str, refs: &mut oxy_core::protocol::RefsResponse) { |
| 295 | 337× | let Some(app) = self.app_hint.lock().unwrap().as_ref().and_then(|w| w.upgrade()) else { return }; |
| 296 | 337× | let row = RepoName::parse(name).and_then(|p| self.row(&crate::wiki::parent_of(&p).0).ok()).map(|r| r.id); |
| 297 | 337× | let backfill = crate::tlog::add_log(&app, row, refs); |
| 298 | 337× | if !backfill.is_empty() { |
| 299 | 7× | self.block(crate::tlog::log(&app, backfill)); |
| 300 | 330× | } |
| 301 | 337× | } |
| 302 | | |
| 303 | /// Tailor the refs answer to the client asking (oxygen#131): one that | |
| 304 | /// does not declare `certified-tags` in `Oxy-Features` (oxy 0.5.0 and | |
| 305 | /// older) is not sent a tag that only a certified key's certificate | |
| 306 | /// authorizes, which it would refuse and, with it, the whole clone. | |
| 307 | 337× | fn adjust_refs(&self, _name: &str, repo: &Repo, refs: &mut oxy_core::protocol::RefsResponse, headers: &HeaderMap) { |
| 308 | 337× | oxy_server::tailor_refs(repo, refs, headers); |
| 309 | 337× | } |
| 310 | | |
| 311 | /// Push protection (hub spec §15.4, #268): a push adding what looks | |
| 312 | /// like a secret is refused, saying where, unless the repository turned | |
| 313 | /// it off or a maintainer allowed that finding's fingerprint. | |
| 314 | 255× | fn vet_push( |
| 315 | 255× | &self, |
| 316 | 255× | name: &str, |
| 317 | 255× | repo: &Repo, |
| 318 | 255× | bundle: &oxy_core::protocol::Bundle, |
| 319 | 255× | updates: &[oxy_core::protocol::RefUpdate], |
| 320 | 255× | headers: &HeaderMap, |
| 321 | 255× | ) -> Result<(), Failure> { |
| 322 | 255× | let keys: std::collections::BTreeSet<String> = bundle |
| 323 | 255× | .changes |
| 324 | 255× | .iter() |
| 325 | 283× | .flat_map(|(_, sigs)| sigs.iter()) |
| 326 | 255× | .chain(bundle.attests.iter().flat_map(|a| a.sigs.iter())) |
| 327 | 255× | .map(|sig| sig.key.to_hex()) |
| 328 | 255× | .collect(); |
| 329 | 255× | if !keys.is_empty() { |
| 330 | 93× | self.signers.lock().unwrap().entry(name.to_string()).or_default().extend(keys); |
| 331 | 162× | } |
| 332 | 255× | if bundle.changes.is_empty() { |
| 333 | 13× | return Ok(()); |
| 334 | 242× | } |
| 335 | 242× | let parsed = RepoName::parse(name).ok_or_else(not_found)?; |
| 336 | 242× | let row = self.row(&crate::wiki::parent_of(&parsed).0)?; |
| 337 | 242× | if !row.push_protection { |
| 338 | 10× | return Ok(()); |
| 339 | 232× | } |
| 340 | 232× | let scan = crate::push_protection::scan(bundle, self.scan_budget); |
| 341 | 232× | if let Some(note) = scan.note() { |
| 342 | 1× | tracing::info!("push to {name}: {note}"); |
| 343 | 1× | self.scan_notes.lock().unwrap().insert(row.id, note); |
| 344 | 231× | } |
| 345 | 232× | if scan.findings.is_empty() { |
| 346 | 217× | return Ok(()); |
| 347 | 15× | } |
| 348 | 15× | let allowed = self.block(db::push_protection::allowed(&self.pool, row.id)).map_err(internal)?; |
| 349 | 19× | let mut findings: Vec<_> = scan.findings.into_iter().filter(|f| !allowed.contains(&f.fingerprint)).collect(); |
| 350 | 15× | if findings.is_empty() { |
| 351 | 1× | return Ok(()); |
| 352 | 14× | } |
| 353 | 14× | crate::push_protection::locate(repo, bundle, &mut findings); |
| 354 | 14× | let (pusher, via) = self.pusher(headers); |
| 355 | 14× | let base = self.app_hint.lock().unwrap().as_ref().and_then(|w| w.upgrade()).map(|a| a.config.base_url().to_string()); |
| 356 | 14× | let settings = |
| 357 | 14× | if row.is_drop() { String::new() } else { format!("{}/{}/{}/settings", base.unwrap_or_default(), row.owner, row.name) }; |
| 358 | 14× | let text = crate::push_protection::refusal(&findings, &settings, &via); |
| 359 | // Recorded for the maintainers (Settings › Push protection) and in | |
| 360 | // the push log, as every refused push is. | |
| 361 | 14× | let records: Vec<db::push_protection::NewFinding> = findings |
| 362 | 14× | .iter() |
| 363 | 14× | .map(|f| db::push_protection::NewFinding { |
| 364 | 18× | rule: &f.rule, |
| 365 | 18× | description: &f.description, |
| 366 | 18× | path: &f.path, |
| 367 | 18× | line: f.line as i64, |
| 368 | 18× | fingerprint: &f.fingerprint, |
| 369 | 18× | excerpt: &f.excerpt, |
| 370 | 18× | }) |
| 371 | 14× | .collect(); |
| 372 | 14× | let who = pusher.as_ref().map(|(id, n)| (*id, n.as_str())); |
| 373 | 14× | if let Err(e) = self.block(db::push_protection::record(&self.pool, row.id, who.map(|w| w.1).unwrap_or(""), &via, &records)) { |
| 374 | 0× | tracing::warn!("recording push protection findings for {name}: {e}"); |
| 375 | 14× | } |
| 376 | 14× | let reason = format!( |
| 377 | "push protection: {} possible secret{} ({}){}", | |
| 378 | 14× | findings.len(), |
| 379 | 14× | if findings.len() == 1 { "" } else { "s" }, |
| 380 | 18× | findings.iter().take(3).map(|f| format!("{}:{}", f.path, f.line)).collect::<Vec<_>>().join(", "), |
| 381 | 14× | if row.is_drop() { "" } else { "; see Settings › Push protection" } |
| 382 | ); | |
| 383 | 14× | let refs: Vec<String> = if updates.is_empty() { |
| 384 | 0× | vec![oxy_core::repo::OBJECTS_RESULT.to_string()] |
| 385 | } else { | |
| 386 | 14× | updates.iter().map(|u| u.name.clone()).collect() |
| 387 | }; | |
| 388 | 14× | let outcomes: Vec<db::repos::PushOutcome> = refs |
| 389 | 14× | .into_iter() |
| 390 | 14× | .map(|reference| db::repos::PushOutcome { |
| 391 | 14× | reference, |
| 392 | ok: false, | |
| 393 | 14× | reason: reason.clone(), |
| 394 | 14× | old_state: String::new(), |
| 395 | 14× | new_state: String::new(), |
| 396 | heads: 0, | |
| 397 | changes: 0, | |
| 398 | 14× | }) |
| 399 | 14× | .collect(); |
| 400 | 14× | if let Err(e) = self.block(db::repos::record_push_via(&self.pool, row.id, who, &via, &outcomes)) { |
| 401 | 0× | tracing::warn!("recording refused push to {name}: {e}"); |
| 402 | 14× | } |
| 403 | 14× | tracing::info!("push to {name} refused by push protection: {} finding(s)", findings.len()); |
| 404 | 14× | Err(Failure(StatusCode::UNPROCESSABLE_ENTITY, text)) |
| 405 | 255× | } |
| 406 | | |
| 407 | /// A tag a release names is not withdrawn (#301): the release would | |
| 408 | /// point at nothing. Delete the release first. | |
| 409 | 2× | fn vet_tag_withdrawal(&self, name: &str, tag: &str) -> Result<(), String> { |
| 410 | 2× | let Some(parsed) = RepoName::parse(name) else { return Ok(()) }; |
| 411 | 2× | let Ok(row) = self.row(&parsed) else { return Ok(()) }; |
| 412 | 2× | match self.block(db::releases::get(&self.pool, row.id, tag)) { |
| 413 | 1× | Ok(Some(rel)) => Err(format!("the release {:?} uses tags/{tag}: delete the release first", rel.title)), |
| 414 | 1× | Ok(None) => Ok(()), |
| 415 | 0× | Err(e) => Err(format!("could not check the releases of {name}: {e}")), |
| 416 | } | |
| 417 | 2× | } |
| 418 | | |
| 419 | 241× | fn after_push(&self, name: &str, report: &PushReport, headers: &HeaderMap) { |
| 420 | // The keys that signed what it brought were used (#286): linked | |
| 421 | // ones show it under Settings › Keys. | |
| 422 | 241× | let signers = self.signers.lock().unwrap().remove(name); |
| 423 | 241× | if let Some(keys) = signers.filter(|_| report.results.iter().any(|r| r.ok)) { |
| 424 | 79× | let pool = self.pool.clone(); |
| 425 | 79× | let keys: Vec<String> = keys.into_iter().collect(); |
| 426 | 79× | tokio::spawn(async move { |
| 427 | 79× | if let Err(e) = db::accounts::identities_used(&pool, &keys).await { |
| 428 | 0× | tracing::warn!("noting keys used: {e}"); |
| 429 | 79× | } |
| 430 | 79× | }); |
| 431 | 162× | } |
| 432 | 241× | let Some(parsed) = RepoName::parse(name) else { return }; |
| 433 | // A wiki push starts no run and rings no webhook. | |
| 434 | 241× | if crate::wiki::parent_of(&parsed).1 { |
| 435 | 2× | return; |
| 436 | 239× | } |
| 437 | 239× | let Ok(row) = self.row(&parsed) else { return }; |
| 438 | 239× | let Some(parsed) = RepoName::new(&row.owner, &row.name) else { return }; |
| 439 | 239× | let (pusher, via) = self.pusher(headers); |
| 440 | 239× | let pool = self.pool.clone(); |
| 441 | // A part of a long push (objects alone) moves nothing: the push it | |
| 442 | // belongs to is recorded when its last part names the references. | |
| 443 | 239× | let mut results: Vec<db::repos::PushOutcome> = report |
| 444 | 239× | .results |
| 445 | 239× | .iter() |
| 446 | 259× | .filter(|r| r.name != oxy_core::repo::OBJECTS_RESULT) |
| 447 | 258× | .map(|r| { |
| 448 | 269× | let updated = report.updated.iter().find(|u| u.name == r.name); |
| 449 | db::repos::PushOutcome { | |
| 450 | 258× | reference: r.name.clone(), |
| 451 | 258× | ok: r.ok, |
| 452 | 258× | reason: r.reason.clone(), |
| 453 | 258× | old_state: updated.map(|u| u.old_state.to_hex()).unwrap_or_default(), |
| 454 | 258× | new_state: updated.map(|u| u.new_state.to_hex()).unwrap_or_default(), |
| 455 | 258× | heads: updated.map(|u| u.heads.len() as i64).unwrap_or(0), |
| 456 | 258× | changes: if r.ok { report.imported.len() as i64 } else { 0 }, |
| 457 | } | |
| 458 | 258× | }) |
| 459 | 239× | .collect(); |
| 460 | // Push protection's scan stopped early on this push or a part of it | |
| 461 | // (#268): its accepted references say so in the log. | |
| 462 | 239× | if !results.is_empty() { |
| 463 | 238× | if let Some(note) = self.scan_notes.lock().unwrap().remove(&row.id) { |
| 464 | 1× | for r in results.iter_mut().filter(|r| r.ok) { |
| 465 | 1× | r.reason = if r.reason.is_empty() { note.clone() } else { format!("{}; {note}", r.reason) }; |
| 466 | } | |
| 467 | 237× | } |
| 468 | 1× | } |
| 469 | // Messages of the changes stored: `closes #N` when the default | |
| 470 | // bookmark moved, and `#N` references on any bookmark (hub spec §8). | |
| 471 | 239× | let bookmarks: Vec<String> = report |
| 472 | 239× | .updated |
| 473 | 239× | .iter() |
| 474 | 247× | .filter(|u| !u.name.starts_with("tags/") && report.results.iter().any(|r| r.ok && r.name == u.name)) |
| 475 | 239× | .map(|u| u.name.clone()) |
| 476 | 239× | .collect(); |
| 477 | 239× | let imported: Vec<(String, String)> = if bookmarks.is_empty() { |
| 478 | 4× | vec![] |
| 479 | } else { | |
| 480 | 235× | self.repos |
| 481 | 235× | .open(&parsed) |
| 482 | 235× | .map(|r| { |
| 483 | 267× | report.imported.iter().filter_map(|h| r.store().get_change_meta(h).ok().map(|c| (h.to_hex(), c.message))).collect() |
| 484 | 235× | }) |
| 485 | 235× | .unwrap_or_default() |
| 486 | }; | |
| 487 | 239× | let messages: Vec<(String, String)> = if bookmarks.contains(&row.default_bookmark) { imported.clone() } else { vec![] }; |
| 488 | // Authors whose identity is a successor (a rotation, a renewal, a | |
| 489 | // delegation) of an earlier one: each step kept the key pair, so | |
| 490 | // if the chain reaches a linked key it is the same person, and the | |
| 491 | // successor is linked too, without re-uploading it (hub spec §5.3). | |
| 492 | 239× | let successors: Vec<(String, String, String, Vec<u8>, String)> = self |
| 493 | 239× | .repos |
| 494 | 239× | .open(&parsed) |
| 495 | 239× | .map(|r| { |
| 496 | 239× | let mut seen = std::collections::HashSet::new(); |
| 497 | 239× | let mut out = Vec::new(); |
| 498 | 267× | for h in &report.imported { |
| 499 | 267× | let Ok(meta) = r.store().get_change_meta(h) else { continue }; |
| 500 | 267× | let mut key = meta.author; |
| 501 | 268× | while !key.is_zero() && seen.insert(key) { |
| 502 | 78× | let Ok(id) = r.store().get_identity(&key) else { break }; |
| 503 | 78× | let Some(prev) = id.previous else { break }; |
| 504 | 1× | let Ok(before) = r.store().get_identity(&prev) else { break }; |
| 505 | 1× | if before.ed25519_pk != id.ed25519_pk || before.mldsa65_pk != id.mldsa65_pk { |
| 506 | 0× | break; |
| 507 | 1× | } |
| 508 | 1× | out.push((key.to_hex(), id.name.clone(), id.email.clone(), id.encode(), prev.to_hex())); |
| 509 | 1× | key = prev; |
| 510 | } | |
| 511 | } | |
| 512 | 239× | out |
| 513 | 239× | }) |
| 514 | 239× | .unwrap_or_default(); |
| 515 | 239× | let on = if bookmarks.contains(&row.default_bookmark) { |
| 516 | 203× | row.default_bookmark.clone() |
| 517 | } else { | |
| 518 | 36× | bookmarks.first().cloned().unwrap_or_default() |
| 519 | }; | |
| 520 | // Bookmarks that moved, with their new heads, for OxygenFlow runs. | |
| 521 | 239× | let moved: Vec<(String, String, Vec<oxy_core::hash::ChangeHash>)> = report |
| 522 | 239× | .updated |
| 523 | 239× | .iter() |
| 524 | // A bookmark sent again unchanged starts nothing. | |
| 525 | 258× | .filter(|u| u.old_state != u.new_state && report.results.iter().any(|r| r.ok && r.name == u.name)) |
| 526 | 240× | .map(|u| (u.name.clone(), u.new_state.to_hex(), u.heads.clone())) |
| 527 | 239× | .collect(); |
| 528 | // The transparency log (#238): what the push brought, logged before | |
| 529 | // the push is answered. | |
| 530 | 239× | let app = self.app_hint.lock().unwrap().as_ref().and_then(|w| w.upgrade()); |
| 531 | 239× | if let Some(app) = app { |
| 532 | 239× | let to_log = self.repos.open(&parsed).map(|r| crate::tlog::push_entries(&r, Some(row.id), report)).unwrap_or_default(); |
| 533 | 239× | if !to_log.is_empty() { |
| 534 | 84× | self.block(crate::tlog::log(&app, to_log)); |
| 535 | 155× | } |
| 536 | 0× | } |
| 537 | 239× | let app_hint = self.app_hint.clone(); |
| 538 | 239× | self.runtime.spawn(async move { |
| 539 | 239× | let who = pusher.as_ref().map(|(id, name)| (*id, name.as_str())); |
| 540 | 239× | if let Err(e) = db::repos::record_push_via(&pool, row.id, who, &via, &results).await { |
| 541 | 0× | tracing::warn!("recording push to {}: {e}", parsed); |
| 542 | 239× | } |
| 543 | // Oldest first: a link made for one step lets the next step link. | |
| 544 | 239× | for (key, name, email, object, prev) in successors.iter().rev() { |
| 545 | 1× | let Ok(None) = db::accounts::identity_by_key(&pool, key).await else { continue }; |
| 546 | 1× | let Ok(Some(earlier)) = db::accounts::identity_by_key(&pool, prev).await else { continue }; |
| 547 | 1× | match db::accounts::link_identity(&pool, earlier.account_id, key, name, email, object).await { |
| 548 | Ok(()) => { | |
| 549 | 1× | tracing::info!("linked identity {} to account {} as a successor of {}", &key[..12], earlier.account_id, &prev[..12]); |
| 550 | 1× | db::accounts::audit(&pool, None, "identity.rotated", key, &format!("successor of {}", &prev[..12])).await.ok(); |
| 551 | } | |
| 552 | 0× | Err(e) => tracing::warn!("linking successor identity {}: {e}", &key[..12]), |
| 553 | } | |
| 554 | } | |
| 555 | // A drop (#279) has no issues, webhooks, mirror, Diffusion or | |
| 556 | // runs: its push is recorded, and that is all. | |
| 557 | 239× | if row.is_drop() { |
| 558 | 1× | return; |
| 559 | 238× | } |
| 560 | 238× | let app = app_hint.lock().unwrap().as_ref().and_then(|w| w.upgrade()); |
| 561 | 238× | if let Some(app) = app { |
| 562 | // Issues close from every change newly on the default | |
| 563 | // bookmark, not only those this push imported: a client's | |
| 564 | // landing moves main to changes the hub already held. | |
| 565 | 246× | if let Some(r) = results.iter().find(|r| r.ok && r.reference == row.default_bookmark) { |
| 566 | 202× | let new_heads = moved.iter().find(|(b, _, _)| *b == row.default_bookmark).map(|(_, _, h)| h.clone()).unwrap_or_default(); |
| 567 | 202× | let old = r.old_state.clone(); |
| 568 | 202× | let old_heads = crate::repos::RepoName::new(&row.owner, &row.name); |
| 569 | 202× | let old_heads = match (old_heads, oxy_core::hash::ObjectRef::from_hex(&old)) { |
| 570 | 202× | (Some(n), Some(s)) => app.repos.read(&n, move |repo, _| Ok(repo.store().get_state(&s).map(|st| st.heads).unwrap_or_default())).await.unwrap_or_default(), |
| 571 | 0× | _ => Vec::new(), |
| 572 | }; | |
| 573 | 202× | crate::web::issues::close_from_arrived(&app, &row, old_heads, new_heads, None).await; |
| 574 | } else { | |
| 575 | 36× | crate::web::issues::close_from_messages(&app, &row, &messages).await; |
| 576 | } | |
| 577 | 238× | let actor = pusher.as_ref().map(|(_, n)| n.clone()).unwrap_or_default(); |
| 578 | 238× | crate::web::issues::references_from_messages(&app, &row, &imported, &on, &actor).await; |
| 579 | 246× | if results.iter().any(|r| r.ok && r.reference == row.default_bookmark) { |
| 580 | // Failed, it is done again from the work queue (#276). | |
| 581 | 202× | if crate::mirror::refresh(&app, &row).await.is_err() { |
| 582 | 0× | crate::queue::queue_mirror(&app, row.id).await; |
| 583 | 202× | } |
| 584 | 36× | } |
| 585 | 238× | let accepted: Vec<serde_json::Value> = results |
| 586 | 238× | .iter() |
| 587 | 238× | .filter(|r| r.ok) |
| 588 | 255× | .map(|r| serde_json::json!({ "reference": r.reference, "old_state": r.old_state, "new_state": r.new_state, "heads": r.heads, "changes": r.changes })) |
| 589 | 238× | .collect(); |
| 590 | 238× | if !accepted.is_empty() { |
| 591 | 235× | let actor = pusher.as_ref().map(|(_, n)| n.clone()).unwrap_or_default(); |
| 592 | 235× | crate::hooks::emit(&app, &row, "push", &actor, serde_json::json!({ "updates": accepted })).await; |
| 593 | 3× | } |
| 594 | 236× | let actor = pusher.as_ref().map(|(_, n)| n.clone()).unwrap_or_default(); |
| 595 | // The moved bookmarks' briefs, worked out now so no page | |
| 596 | // waits for them (#145). | |
| 597 | 236× | if let Some(name) = crate::repos::RepoName::new(&row.owner, &row.name) { |
| 598 | 237× | let all: Vec<Vec<oxy_core::hash::ChangeHash>> = moved.iter().map(|(_, _, h)| h.clone()).collect(); |
| 599 | 236× | let warm = app.clone(); |
| 600 | 236× | tokio::spawn(async move { |
| 601 | 234× | warm.repos.read(&name, move |r, cache| Ok(cache.briefs(r, &all).map(|_| ())?)).await.ok(); |
| 602 | 234× | }); |
| 603 | 0× | } |
| 604 | 236× | crate::diffusion::after_push(&app, &row, &moved).await; |
| 605 | // An upstream's default bookmark moved: consumers following | |
| 606 | // every move are bumped now, those following green when | |
| 607 | // its run passes (#148). | |
| 608 | 234× | if let Some((_, _, heads)) = moved.iter().find(|(b, _, _)| *b == row.default_bookmark) { |
| 609 | 191× | let (app, row, heads) = (app.clone(), row.clone(), heads.clone()); |
| 610 | 191× | tokio::spawn(async move { crate::diffusion::propagate(&app, &row, heads, false).await }); |
| 611 | 43× | } |
| 612 | 235× | for (bookmark, state, heads) in &moved { |
| 613 | 235× | crate::flow::on_push(&app, &row, bookmark, state, heads, &actor).await; |
| 614 | 235× | crate::web::proposals::settle_after_push(&app, &row, bookmark, &actor).await; |
| 615 | } | |
| 616 | 0× | } |
| 617 | 235× | }); |
| 618 | 241× | } |
| 619 | } | |
| 620 | ||
| 621 | /// `/{owner}/{name}/oxy/v1/{endpoint}`. | |
| 622 | 1054× | pub async fn dispatch( |
| 623 | 1054× | State(app): State<Shared>, |
| 624 | 1054× | Path((owner, name, endpoint)): Path<(String, String, String)>, |
| 625 | 1054× | method: Method, |
| 626 | 1054× | headers: HeaderMap, |
| 627 | 1054× | body: Bytes, |
| 628 | 1054× | ) -> Response { |
| 629 | 1054× | let Some(endpoint) = Endpoint::parse(&endpoint) else { |
| 630 | 1× | return Failure(StatusCode::NOT_FOUND, "no such endpoint".into()).into_response(); |
| 631 | }; | |
| 632 | 1053× | let Some(repo) = RepoName::new(&owner, &name) else { |
| 633 | 0× | return not_found().into_response(); |
| 634 | }; | |
| 635 | 1053× | let _guard = match endpoint { |
| 636 | 260× | Endpoint::Push | Endpoint::Checkpoint => Guard::Write(app.repos.write_guard(&repo).await), |
| 637 | 793× | _ => Guard::Read(app.repos.read_guard(&repo).await), |
| 638 | }; | |
| 639 | 1053× | let (name, received) = (endpoint.name(), body.len() as u64); |
| 640 | 1053× | let res = app.server.handle(repo.to_string(), endpoint, &method, &headers, body).await; |
| 641 | 1053× | let sent = axum::body::HttpBody::size_hint(res.body()).exact().unwrap_or(0); |
| 642 | 1053× | crate::metrics::protocol_bytes(name, received, sent); |
| 643 | 1053× | res |
| 644 | 1054× | } |
| 645 | ||
| 646 | /// `/{owner}/drops/{id}/oxy/v1/{endpoint}`: a drop is a repository to the | |
| 647 | /// wire protocol, named `owner/drops/ID` (#279). | |
| 648 | 22× | pub async fn dispatch_drop( |
| 649 | 22× | State(app): State<Shared>, |
| 650 | 22× | Path((owner, id, endpoint)): Path<(String, String, String)>, |
| 651 | 22× | method: Method, |
| 652 | 22× | headers: HeaderMap, |
| 653 | 22× | body: Bytes, |
| 654 | 22× | ) -> Response { |
| 655 | 22× | let Some(name) = RepoName::drop(&owner, &id) else { |
| 656 | 0× | return not_found().into_response(); |
| 657 | }; | |
| 658 | 22× | dispatch(State(app), Path((name.owner, name.name, endpoint)), method, headers, body).await |
| 659 | 22× | } |
| 660 | ||
| 661 | enum Guard { | |
| 662 | Read(#[allow(dead_code)] crate::repos::ReadGuard), | |
| 663 | Write(#[allow(dead_code)] crate::repos::WriteGuard), | |
| 664 | } |