Skip to main content

arks_server/
push_policy.rs

1//! Push policy seam for receive-pack: a host (the `arks serve` forge module) may
2//! refuse individual ref updates before any ref moves.
3//!
4//! The server stays forge-agnostic: it hands the policy the repository, who is
5//! pushing (as far as the transport knows) and the requested ref updates, and
6//! reports each refusal as `ng <ref> <reason>` in report-status, so the client
7//! prints `! [remote rejected] <ref> (<reason>)`. Cite:
8//! <https://git-scm.com/docs/gitprotocol-pack> (report-status),
9//! <https://git-scm.com/docs/git-push> (OUTPUT).
10
11use arks_oid::Oid;
12use arks_repo::Repository;
13use std::sync::{Arc, OnceLock, RwLock};
14
15/// One requested ref update; an all-zero `old` creates, an all-zero `new` deletes.
16#[derive(Debug, Clone, PartialEq, Eq)]
17pub struct RefUpdate {
18    pub name: String,
19    pub old: Oid,
20    pub new: Oid,
21}
22
23impl RefUpdate {
24    pub fn is_create(&self) -> bool {
25        self.old.as_bytes().iter().all(|b| *b == 0)
26    }
27
28    pub fn is_delete(&self) -> bool {
29        self.new.as_bytes().iter().all(|b| *b == 0)
30    }
31}
32
33/// What the transport knows about the pusher.
34#[derive(Debug, Clone, Default, PartialEq, Eq)]
35pub struct Pusher {
36    /// `http`, `ssh` or `local`.
37    pub transport: &'static str,
38    /// User verified by the server's own HTTP Basic gate, if one is configured.
39    pub verified_user: Option<String>,
40    /// Raw `Authorization` header sent with the push (HTTP only).
41    pub authorization: Option<String>,
42}
43
44impl Pusher {
45    pub fn local() -> Self {
46        Self {
47            transport: "local",
48            ..Self::default()
49        }
50    }
51
52    pub fn ssh() -> Self {
53        Self {
54            transport: "ssh",
55            ..Self::default()
56        }
57    }
58}
59
60/// Decides, per ref update, whether a push may proceed.
61pub trait PushPolicy: Send + Sync {
62    /// One entry per update, in order: `Some(reason)` refuses that ref.
63    fn check(
64        &self,
65        repo: &Repository,
66        pusher: &Pusher,
67        updates: &[RefUpdate],
68    ) -> Vec<Option<String>>;
69}
70
71fn slot() -> &'static RwLock<Option<Arc<dyn PushPolicy>>> {
72    static SLOT: OnceLock<RwLock<Option<Arc<dyn PushPolicy>>>> = OnceLock::new();
73    SLOT.get_or_init(|| RwLock::new(None))
74}
75
76/// Install (or clear) the process-wide policy used by the serving receive-pack paths.
77pub fn install_push_policy(policy: Option<Arc<dyn PushPolicy>>) {
78    if let Ok(mut g) = slot().write() {
79        *g = policy;
80    }
81}
82
83/// The installed policy, if any.
84pub fn installed_push_policy() -> Option<Arc<dyn PushPolicy>> {
85    slot().read().ok().and_then(|g| g.clone())
86}
87
88/// Run `policy` and normalise its answer to one verdict per update (fail closed).
89pub(crate) fn verdicts(
90    policy: Option<&dyn PushPolicy>,
91    repo: &Repository,
92    pusher: &Pusher,
93    updates: &[RefUpdate],
94) -> Vec<Option<String>> {
95    let Some(policy) = policy else {
96        return vec![None; updates.len()];
97    };
98    let out = policy.check(repo, pusher, updates);
99    if out.len() != updates.len() {
100        return vec![Some("push policy error".into()); updates.len()];
101    }
102    out.into_iter()
103        .map(|v| v.map(|r| r.replace(['\n', '\r'], " ")))
104        .collect()
105}