1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// githttp-fs
//
// Git-based Content Management System
// Copyright: 2026, Valerian Saliou <valerian@valeriansaliou.name>
// License: Mozilla Public License v2.0 (MPL v2.0)
//! Request guards: Bearer API keys, and the read-only gate on a replica.
//!
//! githttp-fs is designed to sit *behind* a trusted application server (the
//! CMS backend that owns user accounts and permissions), not to face end
//! users directly. One shared secret between those two machines is therefore
//! the right amount of auth — per-user tokens, scopes, and rate limiting are
//! the upstream application's job.
//!
//! The key comparison uses a constant-time equality check so that an
//! attacker probing the endpoint cannot use response-time differences to
//! discover the key one prefix byte at a time (a classic timing side
//! channel that `==` on byte slices would expose, since it bails at the
//! first mismatching byte).
//!
//! Two further guards live here, both inert unless `[replication]` is
//! configured: `require_replication_key` protects the replication server
//! (its own listener, its own `replication.secret`), and
//! `enforce_replica_read_only` is what makes a replica a replica — it turns
//! every mutating request on the content API into a `423`.
use ;
use json;
use crate::;
const BEARER_PREFIX: &str = "Bearer ";
/// Validates the `Authorization: Bearer <key>` header on every request.
///
/// Layered onto the whole `/v1` router in `main::build_router`, so handlers
/// never have to think about auth. Rejections return the same generic 401
/// body whether the header is missing, malformed, or simply wrong — no
/// information about *why* auth failed is leaked to the caller.
pub async
/// Validates the `Authorization: Bearer <secret>` header on the replication
/// server against `replication.secret`.
///
/// Still required even though that server listens on its own port: the port
/// keeps the surface off whatever proxies the content API, and the secret
/// keeps it shut to anything that reaches the port anyway. Neither is a
/// substitute for the other.
///
/// A separate credential from `server.api_key` on purpose. The replication
/// surface hands out whole repositories as packfiles and a live change
/// stream — a different grant with a different blast radius from the content
/// API — so it gets a key that can be rotated, scoped, and network-restricted
/// on its own. It also means a compromised content key does not let an
/// attacker attach to the notification stream and enumerate every tenant.
///
/// Rejects with the same opaque 401 as the content API when replication is
/// not configured at all: a node that does not replicate should not announce
/// that fact to an unauthenticated caller.
pub async
/// Guards a replica: refuses writes, and refuses everything while a cold
/// replica is still bootstrapping.
///
/// **Why 423 and not 405, and not 503.** A replica is not a server that
/// lacks these endpoints, so `405` is wrong; nor is it a server having a bad
/// moment, so `503` is wrong too — `503` promises that waiting helps, and
/// waiting never makes a replica accept a write. `423 Locked` says the true
/// thing: this resource is write-locked *on this node*, go to the master.
/// That is also why the refusal carries no `Retry-After`: there is no delay
/// after which the same request would succeed here, and inventing one would
/// send a failover-aware client back to the wrong node on a timer instead of
/// to the right one immediately. The bootstrap refusal below is the opposite
/// case — genuinely transient — and keeps both its `503` and its hint.
///
/// **Why classification is not by verb.** `POST` is not a reliable write
/// signal on this API: `POST /batch/files/read` is a read, while `PUT`,
/// `DELETE`, and every other `POST` (move, reorder, revert, rollback, hook
/// replay) mutate or enqueue. So reads are recognised by method *and*, for
/// `POST`, by the one route suffix that is a read. Matching on the suffix
/// rather than the whole path keeps this correct whether or not the router
/// has stripped the `/v1` prefix by the time the guard runs.
///
/// **Writes are classified before the bootstrap gate**, so a write gets the
/// same `423` whatever this node's catch-up state is: "still bootstrapping"
/// is never the useful half of the answer to a request this node would go on
/// refusing once current.
///
/// The bootstrap gate covers the case where serving would be actively
/// misleading: a replica holding no repositories at all would answer `404`
/// for content that exists, which is worse than refusing. A replica that
/// merely restarted with content on disk is never gated — stale beats
/// absent. The API-key ping is always allowed through so monitors can see
/// *why* a node is refusing; the unauthenticated `/v1/_health` routes say the
/// same thing without a credential, and never reach this guard at all.
// The `Err` variant is an `axum::Response`, which clippy flags as large. It
// is the shape axum's `from_fn` middleware requires, so it cannot be boxed.
pub async