OXYGENHUB

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

497 of 526 lines run, 104 of 212 functions · run #482 on proposal/333 · 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 {
57193×
    pub fn new(
58193×
        repos: Arc<Repos>,
59193×
        wikis: Arc<Repos>,
60193×
        pool: db::Pool,
61193×
        site_tokens: Vec<String>,
62193×
        runtime: tokio::runtime::Handle,
63193×
    ) -> HubBackend {
64193×
        HubBackend {
65193×
            repos,
66193×
            wikis,
67193×
            pool,
68193×
            site_tokens,
69193×
            runtime,
70193×
            app_hint: Arc::new(std::sync::Mutex::new(None)),
71193×
            acceptor: None,
72193×
            scan_budget: crate::push_protection::Budget::default(),
73193×
            scan_notes: Arc::default(),
74193×
            signers: Arc::default(),
75193×
        }
76193×
    }
77
78
    /// Scan pushes for secrets within `budget` (#268).
79193×
    pub fn with_scan_budget(mut self, budget: crate::push_protection::Budget) -> Self {
80193×
        self.scan_budget = budget;
81193×
        self
82193×
    }
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.
86257×
    fn pusher(&self, headers: &HeaderMap) -> (Option<(i64, String)>, String) {
87257×
        match self.caller(headers) {
8842×
            Ok(Caller::Token(account, token)) => {
8942×
                (Some((account.id, account.username)), if token.agent { token.name.clone() } else { String::new() })
90
            }
91215×
            Ok(Caller::Site) => (Some((0, "site token".to_string())), String::new()),
920×
            _ => (None, String::new()),
93
        }
94257×
    }
95
96
    /// Countersign accepted updates by keys this hub certified with `identity`.
97193×
    pub fn with_acceptor(mut self, identity: oxy_core::identity::Identity, secret: oxy_core::identity::SecretKey) -> Self {
98193×
        self.acceptor = Some(Arc::new((identity, secret)));
99193×
        self
100193×
    }
101
102
    /// The slot the application registers itself in after construction.
103193×
    pub fn app_slot(&self) -> Arc<std::sync::Mutex<Option<std::sync::Weak<crate::App>>>> {
104193×
        self.app_hint.clone()
105193×
    }
106
1074587×
    fn block<T>(&self, f: impl std::future::Future<Output = T>) -> T {
1084587×
        tokio::task::block_in_place(|| self.runtime.block_on(f))
1094587×
    }
110
1113555×
    fn row(&self, name: &RepoName) -> Result<db::repos::RepoRow, Failure> {
1123555×
        let pool = self.pool.clone();
1133555×
        match self.block(db::repos::get(&pool, &name.owner, &name.name)) {
1143525×
            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
        }
1233555×
    }
124
1251336×
    fn caller(&self, headers: &HeaderMap) -> Result<Caller, Failure> {
1261336×
        let Some(presented) = oxy_server::bearer_token(headers) else { return Ok(Caller::Anonymous) };
1271222×
        if self.site_tokens.iter().any(|t| auth::mac_equal(t, presented)) {
1281017×
            return Ok(Caller::Site);
129205×
        }
130205×
        let hash = auth::token_hash(presented);
131205×
        let pool = self.pool.clone();
132205×
        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() });
138193×
        }
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.
142193×
        let grant_token = presented.starts_with(crate::keyless::GRANT_TOKEN_PREFIX);
143193×
        let token = if grant_token {
14417×
            let grant = self.block(db::accounts::agent_grant_by_token(&pool, &hash)).map_err(internal)?;
14517×
            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
            };
14816×
            let _ = self.block(db::accounts::touch_agent_grant(&pool, g.id));
149
            Some(db::accounts::Token {
150
                id: 0,
15116×
                account_id: g.account_id,
15216×
                name: g.agent.clone(),
15316×
                hash: String::new(),
15432×
                scopes: if g.scope.split(',').any(|s| s.trim() == "refs") { "repo:read,repo:write" } else { "repo:read" }.into(),
15516×
                repos: String::new(),
15616×
                created_ms: g.created_ms,
15716×
                expires_ms: Some(g.expires_ms),
15816×
                last_used_ms: g.last_used_ms,
15916×
                renew_ms: None,
160
                agent: true,
161
            })
162
        } else {
163176×
            self.block(db::accounts::token_by_hash(&pool, &hash)).map_err(internal)?
164
        };
165192×
        let Some(token) = token else { return Err(Failure(StatusCode::UNAUTHORIZED, "unknown token".into())) };
166192×
        if token.expired(db::now_ms()) {
1670×
            return Err(Failure(StatusCode::UNAUTHORIZED, "this token has expired".into()));
168192×
        }
169192×
        let account = self.block(db::accounts::account_by_id(&pool, token.account_id)).map_err(internal)?;
170192×
        let Some(account) = account else { return Err(Failure(StatusCode::UNAUTHORIZED, "unknown token".into())) };
171192×
        if account.banned {
1720×
            return Err(Failure(StatusCode::UNAUTHORIZED, "this account is banned".into()));
173192×
        }
174192×
        if account.disabled {
1750×
            return Err(Failure(StatusCode::UNAUTHORIZED, "this account is disabled; contact the hub's administrator".into()));
176192×
        }
177192×
        if !grant_token {
178176×
            let _ = self.block(db::accounts::touch_token(&pool, token.id));
179176×
        }
180192×
        Ok(Caller::Token(Box::new(account), Box::new(token)))
1811336×
    }
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 {
1931064×
    fn open(&self, name: &str) -> Result<Repo, Failure> {
1941064×
        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).
1981064×
        let (parent, is_wiki) = crate::wiki::parent_of(&parsed);
1991064×
        let row = self.row(&parent)?;
2001064×
        let name = RepoName::new(&row.owner, &row.name).ok_or_else(not_found)?;
2011064×
        let store = if is_wiki { &self.wikis } else { &self.repos };
2021064×
        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×
        })?;
2061063×
        Ok(match &self.acceptor {
2071063×
            Some(a) => repo.with_acceptor(a.0.clone(), a.1.clone()),
2080×
            None => repo,
209
        })
2101064×
    }
211
2121080×
    fn authorize(&self, name: &str, access: oxy_server::Access, headers: &HeaderMap) -> Result<(), Failure> {
2131080×
        let parsed = RepoName::parse(name).ok_or_else(not_found)?;
2141080×
        let (parent, _) = crate::wiki::parent_of(&parsed);
2151080×
        let row = self.row(&parent)?;
2161079×
        let caller = self.caller(headers)?;
2171076×
        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
            };
2251066×
        }
2261066×
        let (role, scope_ok) = match &caller {
2270×
            Caller::Job { .. } => unreachable!(),
228802×
            Caller::Site => (Some(Role::Admin), true),
229114×
            Caller::Anonymous => (self.block(auth::roles::role_for(&self.pool, None, &row)).map_err(internal)?, true),
230150×
            Caller::Token(account, token) => {
231150×
                let role = self.block(auth::roles::role_for(&self.pool, Some(account), &row)).map_err(internal)?;
232150×
                let needed = match access {
233
                    oxy_server::Access::Read => {
234106×
                        token.has_scope("repo:read") || token.has_scope("repo:write") || token.has_scope("repo:admin")
235
                    }
23644×
                    oxy_server::Access::Push => token.has_scope("repo:write") || token.has_scope("repo:admin"),
237
                };
238150×
                let current = format!("{}/{}", row.owner, row.name);
239150×
                let covered = token.covers(&parsed.to_string()) || token.covers(&current) || token.covers(&parent.to_string());
240150×
                (role, needed && covered)
241
            }
242
        };
243
        // An unreadable repository does not exist for the caller (hub spec §4.4).
2441066×
        if !auth::roles::allows(role, Access::Read) {
2454×
            return Err(not_found());
2461062×
        }
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.
2501062×
        let (_, is_wiki) = crate::wiki::parent_of(&parsed);
2511062×
        let may_write = match &caller {
252802×
            Caller::Site => true,
253149×
            Caller::Token(_, token) => token.has_scope("repo:write") || token.has_scope("repo:admin"),
254111×
            _ => false,
255
        };
2561062×
        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×
            }
2641056×
        }
265799×
        match access {
266799×
            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 => {
269263×
                if matches!(caller, Caller::Anonymous) {
2701×
                    return Err(Failure(StatusCode::UNAUTHORIZED, "a personal access token is required to push".into()));
271262×
                }
272262×
                if !auth::roles::allows(role, Access::Write) {
2731×
                    return Err(Failure(StatusCode::FORBIDDEN, "you do not have write access to this repository".into()));
274261×
                }
275261×
                if row.archived {
2761×
                    return Err(Failure(StatusCode::FORBIDDEN, "this repository is archived and read-only".into()));
277260×
                }
278260×
                if !scope_ok {
2791×
                    return Err(Failure(StatusCode::FORBIDDEN, "this token lacks the repo:write scope for this repository".into()));
280259×
                }
281259×
                Ok(())
282
            }
283
        }
2841080×
    }
285
286590×
    fn default_bookmark(&self, name: &str) -> Option<String> {
287590×
        let parsed = RepoName::parse(name)?;
288590×
        if crate::wiki::parent_of(&parsed).1 {
2896×
            return Some(crate::wiki::BOOKMARK.into());
290584×
        }
291584×
        self.row(&parsed).ok().map(|r| r.default_bookmark)
292590×
    }
293
294345×
    fn add_log(&self, name: &str, refs: &mut oxy_core::protocol::RefsResponse) {
295345×
        let Some(app) = self.app_hint.lock().unwrap().as_ref().and_then(|w| w.upgrade()) else { return };
296345×
        let row = RepoName::parse(name).and_then(|p| self.row(&crate::wiki::parent_of(&p).0).ok()).map(|r| r.id);
297345×
        let backfill = crate::tlog::add_log(&app, row, refs);
298345×
        if !backfill.is_empty() {
2997×
            self.block(crate::tlog::log(&app, backfill));
300338×
        }
301345×
    }
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.
307345×
    fn adjust_refs(&self, _name: &str, repo: &Repo, refs: &mut oxy_core::protocol::RefsResponse, headers: &HeaderMap) {
308345×
        oxy_server::tailor_refs(repo, refs, headers);
309345×
    }
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.
314259×
    fn vet_push(
315259×
        &self,
316259×
        name: &str,
317259×
        repo: &Repo,
318259×
        bundle: &oxy_core::protocol::Bundle,
319259×
        updates: &[oxy_core::protocol::RefUpdate],
320259×
        headers: &HeaderMap,
321259×
    ) -> Result<(), Failure> {
322259×
        let keys: std::collections::BTreeSet<String> = bundle
323259×
            .changes
324259×
            .iter()
325259×
            .flat_map(|(_, sigs)| sigs.iter())
326259×
            .chain(bundle.attests.iter().flat_map(|a| a.sigs.iter()))
327259×
            .map(|sig| sig.key.to_hex())
328259×
            .collect();
329259×
        if !keys.is_empty() {
33092×
            self.signers.lock().unwrap().entry(name.to_string()).or_default().extend(keys);
331167×
        }
332259×
        if bundle.changes.is_empty() {
33322×
            return Ok(());
334237×
        }
335237×
        let parsed = RepoName::parse(name).ok_or_else(not_found)?;
336237×
        let row = self.row(&crate::wiki::parent_of(&parsed).0)?;
337237×
        if !row.push_protection {
33810×
            return Ok(());
339227×
        }
340227×
        let scan = crate::push_protection::scan(bundle, self.scan_budget);
341227×
        if let Some(note) = scan.note() {
3421×
            tracing::info!("push to {name}: {note}");
3431×
            self.scan_notes.lock().unwrap().insert(row.id, note);
344226×
        }
345227×
        if scan.findings.is_empty() {
346212×
            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))
405259×
    }
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
419245×
    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.
422245×
        let signers = self.signers.lock().unwrap().remove(name);
423245×
        if let Some(keys) = signers.filter(|_| report.results.iter().any(|r| r.ok)) {
42478×
            let pool = self.pool.clone();
42578×
            let keys: Vec<String> = keys.into_iter().collect();
42678×
            tokio::spawn(async move {
42778×
                if let Err(e) = db::accounts::identities_used(&pool, &keys).await {
4280×
                    tracing::warn!("noting keys used: {e}");
42978×
                }
43078×
            });
431167×
        }
432245×
        let Some(parsed) = RepoName::parse(name) else { return };
433
        // A wiki push starts no run and rings no webhook.
434245×
        if crate::wiki::parent_of(&parsed).1 {
4352×
            return;
436243×
        }
437243×
        let Ok(row) = self.row(&parsed) else { return };
438243×
        let Some(parsed) = RepoName::new(&row.owner, &row.name) else { return };
439243×
        let (pusher, via) = self.pusher(headers);
440243×
        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.
443243×
        let mut results: Vec<db::repos::PushOutcome> = report
444243×
            .results
445243×
            .iter()
446264×
            .filter(|r| r.name != oxy_core::repo::OBJECTS_RESULT)
447263×
            .map(|r| {
448274×
                let updated = report.updated.iter().find(|u| u.name == r.name);
449
                db::repos::PushOutcome {
450263×
                    reference: r.name.clone(),
451263×
                    ok: r.ok,
452263×
                    reason: r.reason.clone(),
453263×
                    old_state: updated.map(|u| u.old_state.to_hex()).unwrap_or_default(),
454263×
                    new_state: updated.map(|u| u.new_state.to_hex()).unwrap_or_default(),
455263×
                    heads: updated.map(|u| u.heads.len() as i64).unwrap_or(0),
456263×
                    changes: if r.ok { report.imported.len() as i64 } else { 0 },
457
                }
458263×
            })
459243×
            .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.
462243×
        if !results.is_empty() {
463242×
            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
                }
467241×
            }
4681×
        }
469
        // Messages of the changes stored: `closes #N` when the default
470
        // bookmark moved, and `#N` references on any bookmark (hub spec §8).
471243×
        let bookmarks: Vec<String> = report
472243×
            .updated
473243×
            .iter()
474251×
            .filter(|u| !u.name.starts_with("tags/") && report.results.iter().any(|r| r.ok && r.name == u.name))
475243×
            .map(|u| u.name.clone())
476243×
            .collect();
477243×
        let imported: Vec<(String, String)> = if bookmarks.is_empty() {
4784×
            vec![]
479
        } else {
480239×
            self.repos
481239×
                .open(&parsed)
482239×
                .map(|r| {
483239×
                    report.imported.iter().filter_map(|h| r.store().get_change_meta(h).ok().map(|c| (h.to_hex(), c.message))).collect()
484239×
                })
485239×
                .unwrap_or_default()
486
        };
487243×
        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).
492243×
        let successors: Vec<(String, String, String, Vec<u8>, String)> = self
493243×
            .repos
494243×
            .open(&parsed)
495243×
            .map(|r| {
496243×
                let mut seen = std::collections::HashSet::new();
497243×
                let mut out = Vec::new();
498243×
                for h in &report.imported {
499236×
                    let Ok(meta) = r.store().get_change_meta(h) else { continue };
500236×
                    let mut key = meta.author;
501237×
                    while !key.is_zero() && seen.insert(key) {
50277×
                        let Ok(id) = r.store().get_identity(&key) else { break };
50377×
                        let Some(prev) = id.previous else { break };
5042×
                        let Ok(before) = r.store().get_identity(&prev) else { break };
5052×
                        if before.ed25519_pk != id.ed25519_pk || before.mldsa65_pk != id.mldsa65_pk {
5061×
                            break;
5071×
                        }
5081×
                        out.push((key.to_hex(), id.name.clone(), id.email.clone(), id.encode(), prev.to_hex()));
5091×
                        key = prev;
510
                    }
511
                }
512243×
                out
513243×
            })
514243×
            .unwrap_or_default();
515243×
        let on = if bookmarks.contains(&row.default_bookmark) {
516207×
            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.
521243×
        let moved: Vec<(String, String, Vec<oxy_core::hash::ChangeHash>)> = report
522243×
            .updated
523243×
            .iter()
524
            // A bookmark sent again unchanged starts nothing.
525261×
            .filter(|u| u.old_state != u.new_state && report.results.iter().any(|r| r.ok && r.name == u.name))
526243×
            .map(|u| (u.name.clone(), u.new_state.to_hex(), u.heads.clone()))
527243×
            .collect();
528
        // The transparency log (#238): what the push brought, logged before
529
        // the push is answered.
530243×
        let app = self.app_hint.lock().unwrap().as_ref().and_then(|w| w.upgrade());
531243×
        if let Some(app) = app {
532243×
            let to_log = self.repos.open(&parsed).map(|r| crate::tlog::push_entries(&r, Some(row.id), report)).unwrap_or_default();
533243×
            if !to_log.is_empty() {
53487×
                self.block(crate::tlog::log(&app, to_log));
535156×
            }
5360×
        }
537243×
        let app_hint = self.app_hint.clone();
538243×
        self.runtime.spawn(async move {
539243×
            let who = pusher.as_ref().map(|(id, name)| (*id, name.as_str()));
540243×
            if let Err(e) = db::repos::record_push_via(&pool, row.id, who, &via, &results).await {
5410×
                tracing::warn!("recording push to {}: {e}", parsed);
542243×
            }
543
            // Oldest first: a link made for one step lets the next step link.
544243×
            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.
557243×
            if row.is_drop() {
5581×
                return;
559242×
            }
560242×
            let app = app_hint.lock().unwrap().as_ref().and_then(|w| w.upgrade());
561242×
            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.
565251×
                if let Some(r) = results.iter().find(|r| r.ok && r.reference == row.default_bookmark) {
566206×
                    let new_heads = moved.iter().find(|(b, _, _)| *b == row.default_bookmark).map(|(_, _, h)| h.clone()).unwrap_or_default();
567206×
                    let old = r.old_state.clone();
568206×
                    let old_heads = crate::repos::RepoName::new(&row.owner, &row.name);
569206×
                    let old_heads = match (old_heads, oxy_core::hash::ObjectRef::from_hex(&old)) {
570206×
                        (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
                    };
573206×
                    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
                }
577242×
                let actor = pusher.as_ref().map(|(_, n)| n.clone()).unwrap_or_default();
578242×
                crate::web::issues::references_from_messages(&app, &row, &imported, &on, &actor).await;
579249×
                if results.iter().any(|r| r.ok && r.reference == row.default_bookmark) {
580
                    // Failed, it is done again from the work queue (#276).
581204×
                    if crate::mirror::refresh(&app, &row).await.is_err() {
5820×
                        crate::queue::queue_mirror(&app, row.id).await;
583204×
                    }
58436×
                }
585240×
                let accepted: Vec<serde_json::Value> = results
586240×
                    .iter()
587240×
                    .filter(|r| r.ok)
588258×
                    .map(|r| serde_json::json!({ "reference": r.reference, "old_state": r.old_state, "new_state": r.new_state, "heads": r.heads, "changes": r.changes }))
589240×
                    .collect();
590240×
                if !accepted.is_empty() {
591237×
                    let actor = pusher.as_ref().map(|(_, n)| n.clone()).unwrap_or_default();
592237×
                    crate::hooks::emit(&app, &row, "push", &actor, serde_json::json!({ "updates": accepted })).await;
5933×
                }
594240×
                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).
597240×
                if let Some(name) = crate::repos::RepoName::new(&row.owner, &row.name) {
598240×
                    let all: Vec<Vec<oxy_core::hash::ChangeHash>> = moved.iter().map(|(_, _, h)| h.clone()).collect();
599240×
                    let warm = app.clone();
600240×
                    tokio::spawn(async move {
601238×
                        warm.repos.read(&name, move |r, cache| Ok(cache.briefs(r, &all).map(|_| ())?)).await.ok();
602238×
                    });
6030×
                }
604240×
                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).
608237×
                if let Some((_, _, heads)) = moved.iter().find(|(b, _, _)| *b == row.default_bookmark) {
609193×
                    let (app, row, heads) = (app.clone(), row.clone(), heads.clone());
610193×
                    tokio::spawn(async move { crate::diffusion::propagate(&app, &row, heads, false).await });
61144×
                }
612237×
                for (bookmark, state, heads) in &moved {
613237×
                    crate::flow::on_push(&app, &row, bookmark, state, heads, &actor).await;
614236×
                    crate::web::proposals::settle_after_push(&app, &row, bookmark, &actor).await;
615
                }
6160×
            }
617237×
        });
618245×
    }
619
}
620
621
/// `/{owner}/{name}/oxy/v1/{endpoint}`.
6221082×
pub async fn dispatch(
6231082×
    State(app): State<Shared>,
6241082×
    Path((owner, name, endpoint)): Path<(String, String, String)>,
6251082×
    method: Method,
6261082×
    headers: HeaderMap,
6271082×
    body: Bytes,
6281082×
) -> Response {
6291082×
    let Some(endpoint) = Endpoint::parse(&endpoint) else {
6301×
        return Failure(StatusCode::NOT_FOUND, "no such endpoint".into()).into_response();
631
    };
6321081×
    let Some(repo) = RepoName::new(&owner, &name) else {
6330×
        return not_found().into_response();
634
    };
6351081×
    let _guard = match endpoint {
636264×
        Endpoint::Push | Endpoint::Checkpoint => Guard::Write(app.repos.write_guard(&repo).await),
637817×
        _ => Guard::Read(app.repos.read_guard(&repo).await),
638
    };
6391081×
    let (name, received) = (endpoint.name(), body.len() as u64);
6401081×
    let res = app.server.handle(repo.to_string(), endpoint, &method, &headers, body).await;
6411081×
    let sent = axum::body::HttpBody::size_hint(res.body()).exact().unwrap_or(0);
6421081×
    crate::metrics::protocol_bytes(name, received, sent);
6431081×
    res
6441082×
}
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
}