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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
//! The options of a server.
use fmt;
use ;
use Arc;
use Duration;
use ;
use ;
use crateEndpointOptions;
use crateEventSink;
/// The PEM bytes of the server TLS files.
///
/// The caller reads each file once at start.
/// The debug text holds no byte of the private key or of the passphrase. It
/// shows the key as `<N bytes redacted>`, and a passphrase that is set as
/// `<redacted>`.
/// The options of [`bind`](crate::bind).
///
/// The struct is `#[non_exhaustive]`, so a caller starts from
/// [`ServeOptions::default`] and sets the fields.
///
/// [`bind`](crate::bind) checks the fields that go together. Its
/// [errors](crate::bind#errors) list the refusals.
///
/// The fields `parallel_uploads`, `session_idle_timeout`, `max_sessions`, and
/// `on_report` go to the [`EndpointOptions`] of the receive endpoint. Their
/// defaults are the defaults of [`EndpointOptions::default`]. If
/// [`receive`](ServeOptions::receive) is `None`, [`bind`](crate::bind) does
/// not read these four fields or `allow_cleartext_credentials`. It then
/// refuses no value of them.
///
/// # Authentication
///
/// The server identifies the client of each request by the auth items of the
/// client groups of [`receive`](ServeOptions::receive): the requests of the
/// receive endpoint, and the reads of the archive view and of the ref
/// listing. Identification gives one auth item. The server takes the first
/// step that applies:
///
/// 1. A request with more than one `Authorization` header gets 401.
/// 2. If the connection has no TLS, a bearer or Basic credential gets 403,
/// unless
/// [`allow_cleartext_credentials`](ServeOptions::allow_cleartext_credentials)
/// is set, or a trusted proxy states the scheme `https` (see
/// [Client address](ServeOptions#client-address)).
/// 3. A request with an `Authorization` header gets the item of its
/// credential. A bearer token, `Authorization: Bearer TOKEN`, gets the
/// `token-sha256:HEX` item of the SHA-256 digest of the token. A Basic
/// credential, `Authorization: Basic` with the base64 of `USER:PASSWORD`,
/// gets the `basic-sha256:USER:HEX` item of the user name and of the
/// digest of the password. The user name runs to the first `:`, so the
/// password can hold `:`. A credential that matches no item gets 401, also
/// when the request has a client certificate. An empty token, a Basic
/// credential with no `:`, a user name that is not UTF-8, an empty
/// password, and another scheme match no item.
/// 4. A request with a client certificate gets the first item that matches,
/// in this order: `tls-sha256:HEX` by the SHA-256 digest of the DER
/// bytes, `tls:NAME` by the subject CN, `tls:*`. A certificate that
/// matches no item gets 403. A subject matches `tls:NAME` only if it holds
/// exactly one CN, of type UTF8String or PrintableString. From a trusted
/// proxy with a header of a forwarded certificate, the certificate comes
/// from that header alone, and never from the TLS handshake (see
/// [Forwarded certificate](ServeOptions#forwarded-certificate)).
/// 5. A request whose client address is in the network of a `net:` item gets
/// that item. When two items match, the item with the longer prefix wins.
/// 6. A request gets the `anonymous` item, if one exists.
/// 7. Else the request gets 401 when a `token-sha256` or a `basic-sha256`
/// item exists, and 403 when none exists.
///
/// The server compares the digest of a credential with every `token-sha256`
/// or `basic-sha256` item in constant time. It does not stop at a match. The
/// TLS handshake verifies a client certificate against
/// [`client_ca_pem`](ServeOptions::client_ca_pem). The server reads the
/// certificate of the handshake one time for each connection.
///
/// Each 401 carries two headers: `WWW-Authenticate: Bearer realm="ostrya"`
/// and `WWW-Authenticate: Basic realm="ostrya"`.
///
/// A read gets the [`ReadAccess`](ostrya::access::ReadAccess) of the item,
/// with the claims of its certificate. A refused read gets the status of the
/// refusal and an empty body. [`Server`](crate::Server) states the rules of
/// a read.
///
/// The owner key of a request is the text of its item, as
/// [`AuthItem`](ostrya::access::AuthItem) writes it. All clients with one
/// item share one owner key, for example all anonymous clients. A client
/// certificate that gives a `tls:*`, `tls:NAME`, or `tls-sha256:HEX` item of
/// a group with `tls-accept-claim=true` has the key `tls-sha256:HEX`, the
/// SHA-256 digest of its DER bytes. So two certificates with different
/// claims never share a session. This key is the key of no other client:
/// the certificate sets the item that it matches, and only the certificate
/// of that digest matches the item `tls-sha256:HEX`.
///
/// A `session` request selects the client group that carries the item in the
/// namespace of its `Hello`, with
/// [`ReceivePolicies::select`](ostrya::ReceivePolicies::select). The
/// collection id of `Hello` names the namespace, and no collection or the
/// collection id of the repository names the local refs. The session gets
/// the policy of that group and no hooks. With no such group, and with a
/// group that opens no push session, the request gets 403.
///
/// # Claims
///
/// A client group with `tls-accept-claim=true` reads the claims of the client
/// certificate that gives the item of a `session` request or of a read: the
/// URI SANs with the scheme `ostrya`, as [`Claims`](ostrya::access::Claims)
/// reads them.
/// The claims of the certificate of the TLS handshake are read one time for
/// each connection. The claims of a forwarded certificate are read one time
/// for each cache entry.
///
/// - When the certificate holds one or more claims for the namespace of the
/// session or of the ref that a read names, as
/// [`ClientAccess::claim_rules`](ostrya::access::ClientAccess::claim_rules)
/// gives them, they replace the allow items and the deny items of the
/// group. The other values of the policy come from the group.
/// - When it holds no claim for the namespace, the rules of the group apply.
/// - When the claims do not parse, the request gets 403, and the server
/// reports a [`ServerEvent::BadClaims`](crate::ServerEvent::BadClaims) to
/// [`on_event`](ServeOptions::on_event). The claims do not parse when a
/// URI with the scheme `ostrya`, in any case, is no claim form, when the
/// certificate holds more than [`MAX_CLAIMS`](ostrya::access::MAX_CLAIMS)
/// such URIs, when the certificate does not decode, when it holds more
/// than one subjectAltName extension, and when its subjectAltName
/// extension does not decode. Each case has its own reason.
/// - When the rules of the claims open no push session, the `session`
/// request gets 403.
/// - A read gets 403 when the claims do not parse and a group with
/// `tls-accept-claim=true` carries the item of the certificate, also a read
/// of an object or of `config`. The server reports a
/// [`ServerEvent::BadClaims`](crate::ServerEvent::BadClaims) for each such
/// read.
///
/// A group with `tls-accept-claim=false`, a credential, a `net:` item, and the
/// `anonymous` item read no claim.
///
/// # Client address
///
/// The client address is the TCP peer address of the connection, with these
/// changes:
///
/// - With [`proxy_protocol`](ServeOptions::proxy_protocol), the source
/// address of the PROXY header replaces the peer address. A header with
/// no address keeps the peer address.
/// - When the peer address is in
/// [`trusted_proxies`](ServeOptions::trusted_proxies), the server reads
/// the client address from the one header of
/// [`trusted_proxy_header`](ServeOptions::trusted_proxy_header). It walks
/// the hops of the header from the right, and skips each hop whose address
/// is in a trusted network. The first other hop is the client. When all
/// hops are trusted, the client is the leftmost hop. A hop with no
/// address, for example `unknown`, gives a client with no address, which
/// matches no `net:` item. A header that is absent, or that has one line
/// of white space alone, keeps the peer address. A header that is not
/// UTF-8, that does not parse, or that has text and no hop, for example
/// `X-Forwarded-For: , ,`, gives no address.
/// - From a trusted peer, the request counts as TLS for step 2 when the
/// proxy states the scheme `https`, in any case. With
/// [`ForwardedHeader::Forwarded`], the scheme is the `proto` of the
/// selected hop. With the other headers, it is the rightmost value of
/// `X-Forwarded-Proto`.
/// - From a peer that is not trusted, the server reads no forwarding header,
/// no `X-Forwarded-Proto`, and no forwarded certificate.
///
/// An IPv4-mapped IPv6 address counts as its IPv4 address.
///
/// With [`proxy_protocol`](ServeOptions::proxy_protocol), the server compares
/// the source address of the PROXY header with
/// [`trusted_proxies`](ServeOptions::trusted_proxies), so it does not read
/// the forwarding headers and the forwarded certificate of a proxy that
/// states the address of its client in the PROXY header.
///
/// A trusted proxy must set or remove the forwarding header, the
/// `X-Forwarded-Proto` header, and the header of a forwarded certificate of
/// each request. Else a client can send these headers through the proxy and
/// take another address or another certificate.
///
/// # Forwarded certificate
///
/// [`trusted_proxy_client_cert`](ServeOptions::trusted_proxy_client_cert)
/// names the request header that carries the client certificate from a
/// trusted proxy. The server reads the header for each request, and never
/// keeps the result for a connection, because a proxy sends the requests of
/// many clients over one connection. The value is one of these forms, after
/// the server percent-decodes it, with `+` kept as `+`:
///
/// - a PEM certificate that starts with `-----BEGIN`, as nginx
/// `$ssl_client_escaped_cert` gives it URL-escaped;
/// - the base64 of the DER bytes, as HAProxy `ssl_c_der,base64` gives it, or
/// URL-escaped, as Traefik `passTLSClientCert` gives it.
///
/// From a trusted proxy, this header alone gives the certificate. The server
/// does not use the certificate of the TLS handshake, which is the
/// certificate of the proxy. A request with no such header gives no
/// certificate. From a peer that is not trusted, or with no
/// `trusted_proxy_client_cert`, the certificate of the TLS handshake applies.
///
/// An empty value, or white space alone, is no certificate. These cases get
/// 403: a value with a comma, as the chain of Traefik holds; more than one
/// line of the header; PEM with more than one `CERTIFICATE` section; a value
/// that does not decode; and a certificate that
/// [`client_ca_pem`](ServeOptions::client_ca_pem) does not verify. The
/// server uses no intermediate certificate from the proxy, so the client CA
/// must hold each intermediate CA. The verification checks the validity
/// period and the `clientAuth` extended key usage.
///
/// The server keeps each verified certificate in a cache of 1024 entries,
/// keyed by the SHA-256 digest of its DER bytes, until the end of its
/// validity period. A full cache removes its oldest entry. The cache keeps
/// no refusal.
/// The debug text shows a callback that is set as `<callback>`, a
/// client CA that is set as `<N bytes>`, the trusted proxies as networks,
/// and the receive policies as the
/// names of their client groups. It holds no auth item, so no digest and no
/// user name.