OXYGENHUB

Coverage / crates/hub/src/protocol.rs 94.3%

496 of 526 lines run, 104 of 212 functions · run #468 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 {
57189×
    pub fn new(
58189×
        repos: Arc<Repos>,
59189×
        wikis: Arc<Repos>,
60189×
        pool: db::Pool,
61189×
        site_tokens: Vec<String>,
62189×
        runtime: tokio::runtime::Handle,
63189×
    ) -> HubBackend {
64189×
        HubBackend {
65189×
            repos,
66189×
            wikis,
67189×
            pool,
68189×
            site_tokens,
69189×
            runtime,
70189×
            app_hint: Arc::new(std::sync::Mutex::new(None)),
71189×
            acceptor: None,
72189×
            scan_budget: crate::push_protection::Budget::default(),
73189×
            scan_notes: Arc::default(),
74189×
            signers: Arc::default(),
75189×
        }
76189×
    }
77
78
    /// Scan pushes for secrets within `budget` (#268).
79189×
    pub fn with_scan_budget(mut self, budget: crate::push_protection::Budget) -> Self {
80189×
        self.scan_budget = budget;
81189×
        self
82189×
    }
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.
86253×
    fn pusher(&self, headers: &HeaderMap) -> (Option<(i64, String)>, String) {
87253×
        match self.caller(headers) {
8840×
            Ok(Caller::Token(account, token)) => {
8940×
                (Some((account.id, account.username)), if token.agent { token.name.clone() } else { String::new() })
90
            }
91213×
            Ok(Caller::Site) => (Some((0, "site token".to_string())), String::new()),
920×
            _ => (None, String::new()),
93
        }
94253×
    }
95
96
    /// Countersign accepted updates by keys this hub certified with `identity`.
97189×
    pub fn with_acceptor(mut self, identity: oxy_core::identity::Identity, secret: oxy_core::identity::SecretKey) -> Self {
98189×
        self.acceptor = Some(Arc::new((identity, secret)));
99189×
        self
100189×
    }
101
102
    /// The slot the application registers itself in after construction.
103189×
    pub fn app_slot(&self) -> Arc<std::sync::Mutex<Option<std::sync::Weak<crate::App>>>> {
104189×
        self.app_hint.clone()
105189×
    }
106
1074467×
    fn block<T>(&self, f: impl std::future::Future<Output = T>) -> T {
1084467×
        tokio::task::block_in_place(|| self.runtime.block_on(f))
1094467×
    }
110
1113480×
    fn row(&self, name: &RepoName) -> Result<db::repos::RepoRow, Failure> {
1123480×
        let pool = self.pool.clone();
1133480×
        match self.block(db::repos::get(&pool, &name.owner, &name.name)) {
1143450×
            Ok(Some(r)) => Ok(r),
115
            // A transferred repository still answers to its old name (#108).
11630×
            Ok(None) => match self.block(db::repos::moved_to(&pool, &name.owner, &name.name)) {
11729×
                Ok(Some(r)) => Ok(r),
1181×
                Ok(None) => Err(not_found()),
1190×
                Err(e) => Err(Failure(StatusCode::INTERNAL_SERVER_ERROR, e.to_string())),
120
            },
1210×
            Err(e) => Err(Failure(StatusCode::INTERNAL_SERVER_ERROR, e.to_string())),
122
        }
1233480×
    }
124
1251304×
    fn caller(&self, headers: &HeaderMap) -> Result<Caller, Failure> {
1261304×
        let Some(presented) = oxy_server::bearer_token(headers) else { return Ok(Caller::Anonymous) };
1271202×
        if self.site_tokens.iter().any(|t| auth::mac_equal(t, presented)) {
1281005×
            return Ok(Caller::Site);
129197×
        }
130197×
        let hash = auth::token_hash(presented);
131197×
        let pool = self.pool.clone();
132197×
        if presented.starts_with(crate::flow::JOB_TOKEN_PREFIX) {
13312×
            let job = self.block(db::flow::job_by_token(&pool, &hash, crate::flow::JOB_TOKEN_MAX_AGE_MS)).map_err(internal)?;
13412×
            let Some(job) = job else { return Err(Failure(StatusCode::UNAUTHORIZED, "this job token is not valid".into())) };
13510×
            let run = self.block(db::flow::run_by_id(&pool, job.run_id)).map_err(internal)?;
13610×
            let Some(run) = run else { return Err(Failure(StatusCode::UNAUTHORIZED, "this job token is not valid".into())) };
13710×
            return Ok(Caller::Job { repo_id: run.repo_id, also: job.also_read_ids() });
138185×
        }
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.
142185×
        let grant_token = presented.starts_with(crate::keyless::GRANT_TOKEN_PREFIX);
143185×
        let token = if grant_token {
1449×
            let grant = self.block(db::accounts::agent_grant_by_token(&pool, &hash)).map_err(internal)?;
1459×
            let Some(g) = grant.filter(|g| g.live(db::now_ms())) else {
1461×
                return Err(Failure(StatusCode::UNAUTHORIZED, "this agent grant is not valid (revoked, ended or unknown)".into()));
147
            };
1488×
            let _ = self.block(db::accounts::touch_agent_grant(&pool, g.id));
149
            Some(db::accounts::Token {
150
                id: 0,
1518×
                account_id: g.account_id,
1528×
                name: g.agent.clone(),
1538×
                hash: String::new(),
15416×
                scopes: if g.scope.split(',').any(|s| s.trim() == "refs") { "repo:read,repo:write" } else { "repo:read" }.into(),
1558×
                repos: String::new(),
1568×
                created_ms: g.created_ms,
1578×
                expires_ms: Some(g.expires_ms),
1588×
                last_used_ms: g.last_used_ms,
1598×
                renew_ms: None,
160
                agent: true,
161
            })
162
        } else {
163176×
            self.block(db::accounts::token_by_hash(&pool, &hash)).map_err(internal)?
164
        };
165184×
        let Some(token) = token else { return Err(Failure(StatusCode::UNAUTHORIZED, "unknown token".into())) };
166184×
        if token.expired(db::now_ms()) {
1670×
            return Err(Failure(StatusCode::UNAUTHORIZED, "this token has expired".into()));
168184×
        }
169184×
        let account = self.block(db::accounts::account_by_id(&pool, token.account_id)).map_err(internal)?;
170184×
        let Some(account) = account else { return Err(Failure(StatusCode::UNAUTHORIZED, "unknown token".into())) };
171184×
        if account.banned {
1720×
            return Err(Failure(StatusCode::UNAUTHORIZED, "this account is banned".into()));
173184×
        }
174184×
        if account.disabled {
1750×
            return Err(Failure(StatusCode::UNAUTHORIZED, "this account is disabled; contact the hub's administrator".into()));
176184×
        }
177184×
        if !grant_token {
178176×
            let _ = self.block(db::accounts::touch_token(&pool, token.id));
179176×
        }
180184×
        Ok(Caller::Token(Box::new(account), Box::new(token)))
1811304×
    }
182
}
183
1840×
fn internal(e: sqlx::Error) -> Failure {
1850×
    Failure(StatusCode::INTERNAL_SERVER_ERROR, e.to_string())
1860×
}
187
1889×
fn not_found() -> Failure {
1899×
    Failure(StatusCode::NOT_FOUND, "no such repository".into())
1909×
}
191
192
impl Backend for HubBackend {
1931036×
    fn open(&self, name: &str) -> Result<Repo, Failure> {
1941036×
        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).
1981036×
        let (parent, is_wiki) = crate::wiki::parent_of(&parsed);
1991036×
        let row = self.row(&parent)?;
2001036×
        let name = RepoName::new(&row.owner, &row.name).ok_or_else(not_found)?;
2011036×
        let store = if is_wiki { &self.wikis } else { &self.repos };
2021036×
        let repo = store.open(&name).map_err(|e| match e {
2031×
            crate::repos::RepoError::NotFound => not_found(),
2040×
            other => Failure(StatusCode::INTERNAL_SERVER_ERROR, other.to_string()),
2051×
        })?;
2061035×
        Ok(match &self.acceptor {
2071035×
            Some(a) => repo.with_acceptor(a.0.clone(), a.1.clone()),
2080×
            None => repo,
209
        })
2101036×
    }
211
2121052×
    fn authorize(&self, name: &str, access: oxy_server::Access, headers: &HeaderMap) -> Result<(), Failure> {
2131052×
        let parsed = RepoName::parse(name).ok_or_else(not_found)?;
2141052×
        let (parent, _) = crate::wiki::parent_of(&parsed);
2151052×
        let row = self.row(&parent)?;
2161051×
        let caller = self.caller(headers)?;
2171048×
        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.
2209×
            return match access {
2219×
                oxy_server::Access::Read if *repo_id == row.id || also.contains(&row.id) => Ok(()),
2223×
                oxy_server::Access::Read => Err(not_found()),
2231×
                oxy_server::Access::Push => Err(Failure(StatusCode::FORBIDDEN, "a job token cannot push".into())),
224
            };
2251038×
        }
2261038×
        let (role, scope_ok) = match &caller {
2270×
            Caller::Job { .. } => unreachable!(),
228792×
            Caller::Site => (Some(Role::Admin), true),
229102×
            Caller::Anonymous => (self.block(auth::roles::role_for(&self.pool, None, &row)).map_err(internal)?, true),
230144×
            Caller::Token(account, token) => {
231144×
                let role = self.block(auth::roles::role_for(&self.pool, Some(account), &row)).map_err(internal)?;
232144×
                let needed = match access {
233
                    oxy_server::Access::Read => {
234102×
                        token.has_scope("repo:read") || token.has_scope("repo:write") || token.has_scope("repo:admin")
235
                    }
23642×
                    oxy_server::Access::Push => token.has_scope("repo:write") || token.has_scope("repo:admin"),
237
                };
238144×
                let current = format!("{}/{}", row.owner, row.name);
239144×
                let covered = token.covers(&parsed.to_string()) || token.covers(&current) || token.covers(&parent.to_string());
240144×
                (role, needed && covered)
241
            }
242
        };
243
        // An unreadable repository does not exist for the caller (hub spec §4.4).
2441038×
        if !auth::roles::allows(role, Access::Read) {
2454×
            return Err(not_found());
2461034×
        }
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.
2501034×
        let (_, is_wiki) = crate::wiki::parent_of(&parsed);
2511034×
        let may_write = match &caller {
252792×
            Caller::Site => true,
253143×
            Caller::Token(_, token) => token.has_scope("repo:write") || token.has_scope("repo:admin"),
25499×
            _ => false,
255
        };
2561034×
        if is_wiki && may_write && auth::roles::allows(role, Access::Write) && !row.archived {
2576×
            if let Some(name) = RepoName::new(&row.owner, &row.name) {
2586×
                if !self.wikis.exists(&name) {
2591×
                    self.wikis
2601×
                        .create(&name, None)
2611×
                        .map_err(|e| Failure(StatusCode::INTERNAL_SERVER_ERROR, format!("make the wiki: {e}")))?;
2625×
                }
2630×
            }
2641028×
        }
265775×
        match access {
266775×
            oxy_server::Access::Read if scope_ok => Ok(()),
2670×
            oxy_server::Access::Read => Err(Failure(StatusCode::FORBIDDEN, "this token cannot read repositories".into())),
268
            oxy_server::Access::Push => {
269259×
                if matches!(caller, Caller::Anonymous) {
2701×
                    return Err(Failure(StatusCode::UNAUTHORIZED, "a personal access token is required to push".into()));
271258×
                }
272258×
                if !auth::roles::allows(role, Access::Write) {
2731×
                    return Err(Failure(StatusCode::FORBIDDEN, "you do not have write access to this repository".into()));
274257×
                }
275257×
                if row.archived {
2761×
                    return Err(Failure(StatusCode::FORBIDDEN, "this repository is archived and read-only".into()));
277256×
                }
278256×
                if !scope_ok {
2791×
                    return Err(Failure(StatusCode::FORBIDDEN, "this token lacks the repo:write scope for this repository".into()));
280255×
                }
281255×
                Ok(())
282
            }
283
        }
2841052×
    }
285
286578×
    fn default_bookmark(&self, name: &str) -> Option<String> {
287578×
        let parsed = RepoName::parse(name)?;
288578×
        if crate::wiki::parent_of(&parsed).1 {
2896×
            return Some(crate::wiki::BOOKMARK.into());
290572×
        }
291572×
        self.row(&parsed).ok().map(|r| r.default_bookmark)
292578×
    }
293
294337×
    fn add_log(&self, name: &str, refs: &mut oxy_core::protocol::RefsResponse) {
295337×
        let Some(app) = self.app_hint.lock().unwrap().as_ref().and_then(|w| w.upgrade()) else { return };
296337×
        let row = RepoName::parse(name).and_then(|p| self.row(&crate::wiki::parent_of(&p).0).ok()).map(|r| r.id);
297337×
        let backfill = crate::tlog::add_log(&app, row, refs);
298337×
        if !backfill.is_empty() {
2997×
            self.block(crate::tlog::log(&app, backfill));
300330×
        }
301337×
    }
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.
307337×
    fn adjust_refs(&self, _name: &str, repo: &Repo, refs: &mut oxy_core::protocol::RefsResponse, headers: &HeaderMap) {
308337×
        oxy_server::tailor_refs(repo, refs, headers);
309337×
    }
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.
314255×
    fn vet_push(
315255×
        &self,
316255×
        name: &str,
317255×
        repo: &Repo,
318255×
        bundle: &oxy_core::protocol::Bundle,
319255×
        updates: &[oxy_core::protocol::RefUpdate],
320255×
        headers: &HeaderMap,
321255×
    ) -> Result<(), Failure> {
322255×
        let keys: std::collections::BTreeSet<String> = bundle
323255×
            .changes
324255×
            .iter()
325283×
            .flat_map(|(_, sigs)| sigs.iter())
326255×
            .chain(bundle.attests.iter().flat_map(|a| a.sigs.iter()))
327255×
            .map(|sig| sig.key.to_hex())
328255×
            .collect();
329255×
        if !keys.is_empty() {
33093×
            self.signers.lock().unwrap().entry(name.to_string()).or_default().extend(keys);
331162×
        }
332255×
        if bundle.changes.is_empty() {
33313×
            return Ok(());
334242×
        }
335242×
        let parsed = RepoName::parse(name).ok_or_else(not_found)?;
336242×
        let row = self.row(&crate::wiki::parent_of(&parsed).0)?;
337242×
        if !row.push_protection {
33810×
            return Ok(());
339232×
        }
340232×
        let scan = crate::push_protection::scan(bundle, self.scan_budget);
341232×
        if let Some(note) = scan.note() {
3421×
            tracing::info!("push to {name}: {note}");
3431×
            self.scan_notes.lock().unwrap().insert(row.id, note);
344231×
        }
345232×
        if scan.findings.is_empty() {
346217×
            return Ok(());
34715×
        }
34815×
        let allowed = self.block(db::push_protection::allowed(&self.pool, row.id)).map_err(internal)?;
34919×
        let mut findings: Vec<_> = scan.findings.into_iter().filter(|f| !allowed.contains(&f.fingerprint)).collect();
35015×
        if findings.is_empty() {
3511×
            return Ok(());
35214×
        }
35314×
        crate::push_protection::locate(repo, bundle, &mut findings);
35414×
        let (pusher, via) = self.pusher(headers);
35514×
        let base = self.app_hint.lock().unwrap().as_ref().and_then(|w| w.upgrade()).map(|a| a.config.base_url().to_string());
35614×
        let settings =
35714×
            if row.is_drop() { String::new() } else { format!("{}/{}/{}/settings", base.unwrap_or_default(), row.owner, row.name) };
35814×
        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.
36114×
        let records: Vec<db::push_protection::NewFinding> = findings
36214×
            .iter()
36314×
            .map(|f| db::push_protection::NewFinding {
36418×
                rule: &f.rule,
36518×
                description: &f.description,
36618×
                path: &f.path,
36718×
                line: f.line as i64,
36818×
                fingerprint: &f.fingerprint,
36918×
                excerpt: &f.excerpt,
37018×
            })
37114×
            .collect();
37214×
        let who = pusher.as_ref().map(|(id, n)| (*id, n.as_str()));
37314×
        if let Err(e) = self.block(db::push_protection::record(&self.pool, row.id, who.map(|w| w.1).unwrap_or(""), &via, &records)) {
3740×
            tracing::warn!("recording push protection findings for {name}: {e}");
37514×
        }
37614×
        let reason = format!(
377
            "push protection: {} possible secret{} ({}){}",
37814×
            findings.len(),
37914×
            if findings.len() == 1 { "" } else { "s" },
38018×
            findings.iter().take(3).map(|f| format!("{}:{}", f.path, f.line)).collect::<Vec<_>>().join(", "),
38114×
            if row.is_drop() { "" } else { "; see Settings › Push protection" }
382
        );
38314×
        let refs: Vec<String> = if updates.is_empty() {
3840×
            vec![oxy_core::repo::OBJECTS_RESULT.to_string()]
385
        } else {
38614×
            updates.iter().map(|u| u.name.clone()).collect()
387
        };
38814×
        let outcomes: Vec<db::repos::PushOutcome> = refs
38914×
            .into_iter()
39014×
            .map(|reference| db::repos::PushOutcome {
39114×
                reference,
392
                ok: false,
39314×
                reason: reason.clone(),
39414×
                old_state: String::new(),
39514×
                new_state: String::new(),
396
                heads: 0,
397
                changes: 0,
39814×
            })
39914×
            .collect();
40014×
        if let Err(e) = self.block(db::repos::record_push_via(&self.pool, row.id, who, &via, &outcomes)) {
4010×
            tracing::warn!("recording refused push to {name}: {e}");
40214×
        }
40314×
        tracing::info!("push to {name} refused by push protection: {} finding(s)", findings.len());
40414×
        Err(Failure(StatusCode::UNPROCESSABLE_ENTITY, text))
405255×
    }
406
407
    /// A tag a release names is not withdrawn (#301): the release would
408
    /// point at nothing. Delete the release first.
4092×
    fn vet_tag_withdrawal(&self, name: &str, tag: &str) -> Result<(), String> {
4102×
        let Some(parsed) = RepoName::parse(name) else { return Ok(()) };
4112×
        let Ok(row) = self.row(&parsed) else { return Ok(()) };
4122×
        match self.block(db::releases::get(&self.pool, row.id, tag)) {
4131×
            Ok(Some(rel)) => Err(format!("the release {:?} uses tags/{tag}: delete the release first", rel.title)),
4141×
            Ok(None) => Ok(()),
4150×
            Err(e) => Err(format!("could not check the releases of {name}: {e}")),
416
        }
4172×
    }
418
419241×
    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.
422241×
        let signers = self.signers.lock().unwrap().remove(name);
423241×
        if let Some(keys) = signers.filter(|_| report.results.iter().any(|r| r.ok)) {
42479×
            let pool = self.pool.clone();
42579×
            let keys: Vec<String> = keys.into_iter().collect();
42679×
            tokio::spawn(async move {
42779×
                if let Err(e) = db::accounts::identities_used(&pool, &keys).await {
4280×
                    tracing::warn!("noting keys used: {e}");
42979×
                }
43079×
            });
431162×
        }
432241×
        let Some(parsed) = RepoName::parse(name) else { return };
433
        // A wiki push starts no run and rings no webhook.
434241×
        if crate::wiki::parent_of(&parsed).1 {
4352×
            return;
436239×
        }
437239×
        let Ok(row) = self.row(&parsed) else { return };
438239×
        let Some(parsed) = RepoName::new(&row.owner, &row.name) else { return };
439239×
        let (pusher, via) = self.pusher(headers);
440239×
        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.
443239×
        let mut results: Vec<db::repos::PushOutcome> = report
444239×
            .results
445239×
            .iter()
446259×
            .filter(|r| r.name != oxy_core::repo::OBJECTS_RESULT)
447258×
            .map(|r| {
448269×
                let updated = report.updated.iter().find(|u| u.name == r.name);
449
                db::repos::PushOutcome {
450258×
                    reference: r.name.clone(),
451258×
                    ok: r.ok,
452258×
                    reason: r.reason.clone(),
453258×
                    old_state: updated.map(|u| u.old_state.to_hex()).unwrap_or_default(),
454258×
                    new_state: updated.map(|u| u.new_state.to_hex()).unwrap_or_default(),
455258×
                    heads: updated.map(|u| u.heads.len() as i64).unwrap_or(0),
456258×
                    changes: if r.ok { report.imported.len() as i64 } else { 0 },
457
                }
458258×
            })
459239×
            .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.
462239×
        if !results.is_empty() {
463238×
            if let Some(note) = self.scan_notes.lock().unwrap().remove(&row.id) {
4641×
                for r in results.iter_mut().filter(|r| r.ok) {
4651×
                    r.reason = if r.reason.is_empty() { note.clone() } else { format!("{}; {note}", r.reason) };
466
                }
467237×
            }
4681×
        }
469
        // Messages of the changes stored: `closes #N` when the default
470
        // bookmark moved, and `#N` references on any bookmark (hub spec §8).
471239×
        let bookmarks: Vec<String> = report
472239×
            .updated
473239×
            .iter()
474247×
            .filter(|u| !u.name.starts_with("tags/") && report.results.iter().any(|r| r.ok && r.name == u.name))
475239×
            .map(|u| u.name.clone())
476239×
            .collect();
477239×
        let imported: Vec<(String, String)> = if bookmarks.is_empty() {
4784×
            vec![]
479
        } else {
480235×
            self.repos
481235×
                .open(&parsed)
482235×
                .map(|r| {
483267×
                    report.imported.iter().filter_map(|h| r.store().get_change_meta(h).ok().map(|c| (h.to_hex(), c.message))).collect()
484235×
                })
485235×
                .unwrap_or_default()
486
        };
487239×
        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).
492239×
        let successors: Vec<(String, String, String, Vec<u8>, String)> = self
493239×
            .repos
494239×
            .open(&parsed)
495239×
            .map(|r| {
496239×
                let mut seen = std::collections::HashSet::new();
497239×
                let mut out = Vec::new();
498267×
                for h in &report.imported {
499267×
                    let Ok(meta) = r.store().get_change_meta(h) else { continue };
500267×
                    let mut key = meta.author;
501268×
                    while !key.is_zero() && seen.insert(key) {
50278×
                        let Ok(id) = r.store().get_identity(&key) else { break };
50378×
                        let Some(prev) = id.previous else { break };
5041×
                        let Ok(before) = r.store().get_identity(&prev) else { break };
5051×
                        if before.ed25519_pk != id.ed25519_pk || before.mldsa65_pk != id.mldsa65_pk {
5060×
                            break;
5071×
                        }
5081×
                        out.push((key.to_hex(), id.name.clone(), id.email.clone(), id.encode(), prev.to_hex()));
5091×
                        key = prev;
510
                    }
511
                }
512239×
                out
513239×
            })
514239×
            .unwrap_or_default();
515239×
        let on = if bookmarks.contains(&row.default_bookmark) {
516203×
            row.default_bookmark.clone()
517
        } else {
51836×
            bookmarks.first().cloned().unwrap_or_default()
519
        };
520
        // Bookmarks that moved, with their new heads, for OxygenFlow runs.
521239×
        let moved: Vec<(String, String, Vec<oxy_core::hash::ChangeHash>)> = report
522239×
            .updated
523239×
            .iter()
524
            // A bookmark sent again unchanged starts nothing.
525258×
            .filter(|u| u.old_state != u.new_state && report.results.iter().any(|r| r.ok && r.name == u.name))
526240×
            .map(|u| (u.name.clone(), u.new_state.to_hex(), u.heads.clone()))
527239×
            .collect();
528
        // The transparency log (#238): what the push brought, logged before
529
        // the push is answered.
530239×
        let app = self.app_hint.lock().unwrap().as_ref().and_then(|w| w.upgrade());
531239×
        if let Some(app) = app {
532239×
            let to_log = self.repos.open(&parsed).map(|r| crate::tlog::push_entries(&r, Some(row.id), report)).unwrap_or_default();
533239×
            if !to_log.is_empty() {
53484×
                self.block(crate::tlog::log(&app, to_log));
535155×
            }
5360×
        }
537239×
        let app_hint = self.app_hint.clone();
538239×
        self.runtime.spawn(async move {
539239×
            let who = pusher.as_ref().map(|(id, name)| (*id, name.as_str()));
540239×
            if let Err(e) = db::repos::record_push_via(&pool, row.id, who, &via, &results).await {
5410×
                tracing::warn!("recording push to {}: {e}", parsed);
542239×
            }
543
            // Oldest first: a link made for one step lets the next step link.
544239×
            for (key, name, email, object, prev) in successors.iter().rev() {
5451×
                let Ok(None) = db::accounts::identity_by_key(&pool, key).await else { continue };
5461×
                let Ok(Some(earlier)) = db::accounts::identity_by_key(&pool, prev).await else { continue };
5471×
                match db::accounts::link_identity(&pool, earlier.account_id, key, name, email, object).await {
548
                    Ok(()) => {
5491×
                        tracing::info!("linked identity {} to account {} as a successor of {}", &key[..12], earlier.account_id, &prev[..12]);
5501×
                        db::accounts::audit(&pool, None, "identity.rotated", key, &format!("successor of {}", &prev[..12])).await.ok();
551
                    }
5520×
                    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.
557239×
            if row.is_drop() {
5581×
                return;
559238×
            }
560238×
            let app = app_hint.lock().unwrap().as_ref().and_then(|w| w.upgrade());
561238×
            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.
565246×
                if let Some(r) = results.iter().find(|r| r.ok && r.reference == row.default_bookmark) {
566202×
                    let new_heads = moved.iter().find(|(b, _, _)| *b == row.default_bookmark).map(|(_, _, h)| h.clone()).unwrap_or_default();
567202×
                    let old = r.old_state.clone();
568202×
                    let old_heads = crate::repos::RepoName::new(&row.owner, &row.name);
569202×
                    let old_heads = match (old_heads, oxy_core::hash::ObjectRef::from_hex(&old)) {
570202×
                        (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(),
5710×
                        _ => Vec::new(),
572
                    };
573202×
                    crate::web::issues::close_from_arrived(&app, &row, old_heads, new_heads, None).await;
574
                } else {
57536×
                    crate::web::issues::close_from_messages(&app, &row, &messages).await;
576
                }
577238×
                let actor = pusher.as_ref().map(|(_, n)| n.clone()).unwrap_or_default();
578238×
                crate::web::issues::references_from_messages(&app, &row, &imported, &on, &actor).await;
579246×
                if results.iter().any(|r| r.ok && r.reference == row.default_bookmark) {
580
                    // Failed, it is done again from the work queue (#276).
581202×
                    if crate::mirror::refresh(&app, &row).await.is_err() {
5820×
                        crate::queue::queue_mirror(&app, row.id).await;
583202×
                    }
58436×
                }
585238×
                let accepted: Vec<serde_json::Value> = results
586238×
                    .iter()
587238×
                    .filter(|r| r.ok)
588255×
                    .map(|r| serde_json::json!({ "reference": r.reference, "old_state": r.old_state, "new_state": r.new_state, "heads": r.heads, "changes": r.changes }))
589238×
                    .collect();
590238×
                if !accepted.is_empty() {
591235×
                    let actor = pusher.as_ref().map(|(_, n)| n.clone()).unwrap_or_default();
592235×
                    crate::hooks::emit(&app, &row, "push", &actor, serde_json::json!({ "updates": accepted })).await;
5933×
                }
594236×
                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).
597236×
                if let Some(name) = crate::repos::RepoName::new(&row.owner, &row.name) {
598237×
                    let all: Vec<Vec<oxy_core::hash::ChangeHash>> = moved.iter().map(|(_, _, h)| h.clone()).collect();
599236×
                    let warm = app.clone();
600236×
                    tokio::spawn(async move {
601233×
                        warm.repos.read(&name, move |r, cache| Ok(cache.briefs(r, &all).map(|_| ())?)).await.ok();
602233×
                    });
6030×
                }
604236×
                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).
608233×
                if let Some((_, _, heads)) = moved.iter().find(|(b, _, _)| *b == row.default_bookmark) {
609190×
                    let (app, row, heads) = (app.clone(), row.clone(), heads.clone());
610190×
                    tokio::spawn(async move { crate::diffusion::propagate(&app, &row, heads, false).await });
61143×
                }
612234×
                for (bookmark, state, heads) in &moved {
613234×
                    crate::flow::on_push(&app, &row, bookmark, state, heads, &actor).await;
614233×
                    crate::web::proposals::settle_after_push(&app, &row, bookmark, &actor).await;
615
                }
6160×
            }
617233×
        });
618241×
    }
619
}
620
621
/// `/{owner}/{name}/oxy/v1/{endpoint}`.
6221054×
pub async fn dispatch(
6231054×
    State(app): State<Shared>,
6241054×
    Path((owner, name, endpoint)): Path<(String, String, String)>,
6251054×
    method: Method,
6261054×
    headers: HeaderMap,
6271054×
    body: Bytes,
6281054×
) -> Response {
6291054×
    let Some(endpoint) = Endpoint::parse(&endpoint) else {
6301×
        return Failure(StatusCode::NOT_FOUND, "no such endpoint".into()).into_response();
631
    };
6321053×
    let Some(repo) = RepoName::new(&owner, &name) else {
6330×
        return not_found().into_response();
634
    };
6351053×
    let _guard = match endpoint {
636260×
        Endpoint::Push | Endpoint::Checkpoint => Guard::Write(app.repos.write_guard(&repo).await),
637793×
        _ => Guard::Read(app.repos.read_guard(&repo).await),
638
    };
6391053×
    let (name, received) = (endpoint.name(), body.len() as u64);
6401053×
    let res = app.server.handle(repo.to_string(), endpoint, &method, &headers, body).await;
6411053×
    let sent = axum::body::HttpBody::size_hint(res.body()).exact().unwrap_or(0);
6421053×
    crate::metrics::protocol_bytes(name, received, sent);
6431053×
    res
6441054×
}
645
646
/// `/{owner}/drops/{id}/oxy/v1/{endpoint}`: a drop is a repository to the
647
/// wire protocol, named `owner/drops/ID` (#279).
64822×
pub async fn dispatch_drop(
64922×
    State(app): State<Shared>,
65022×
    Path((owner, id, endpoint)): Path<(String, String, String)>,
65122×
    method: Method,
65222×
    headers: HeaderMap,
65322×
    body: Bytes,
65422×
) -> Response {
65522×
    let Some(name) = RepoName::drop(&owner, &id) else {
6560×
        return not_found().into_response();
657
    };
65822×
    dispatch(State(app), Path((name.owner, name.name, endpoint)), method, headers, body).await
65922×
}
660
661
enum Guard {
662
    Read(#[allow(dead_code)] crate::repos::ReadGuard),
663
    Write(#[allow(dead_code)] crate::repos::WriteGuard),
664
}