ostrya-remote 0.6.0

The push and pull protocol of ostrya: the wire codec, the push client session, and the ssh and HTTP transports
Documentation
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
//! The keys of a bucket repository, and their encoding in a request.

use ostrya_core::{Checksum, is_collection_id, is_ref_name};

use super::sigv4::uri_encode;
use crate::{Error, Result};

/// The longest collection id of a key under `refs/mirrors/`: 255 bytes. The
/// id is the name of one directory.
const MAX_COLLECTION_ID_LEN: usize = 255;

/// The keys of a bucket repository that hold one file each.
const SINGLE_KEYS: [&str; 5] = [
    "config",
    "summary",
    "summary.sig",
    "extensions/ostrya/lock",
    "extensions/ostrya/lock-probe",
];

/// The extensions of an object key. A bucket repository is in `archive`
/// form, so a file object is `filez`.
const OBJECT_EXTENSIONS: [&str; 7] = [
    "commit",
    "commitmeta",
    "dirtree",
    "dirmeta",
    "filez",
    "tombstone-commit",
    "cfs",
];

/// Returns `true` if `key`, relative to the prefix of the repository, is a
/// key of a bucket repository.
///
/// Each call of [`Bucket`](super::Bucket) on a key refuses each other key,
/// and [`Bucket::list_uploads`](super::Bucket::list_uploads) gives the
/// uploads of these keys alone. The keys are:
///
/// - `config`, `summary`, `summary.sig`, `extensions/ostrya/lock`, and
///   `extensions/ostrya/lock-probe`
/// - `objects/XX/<62 hex digits>.<ext>`, with lowercase hex digits and an
///   extension `commit`, `commitmeta`, `dirtree`, `dirmeta`, `filez`,
///   `tombstone-commit`, or `cfs`
/// - `refs/heads/<name>`, with a name that [`is_ref_name`] accepts
/// - `refs/mirrors/<id>/<name>`, with an id of at most 255 bytes that
///   [`is_collection_id`] accepts, and a name that [`is_ref_name`] accepts
/// - `deltas/<2>/<41>/<target>` and `deltas/<2>/<41>-<43>/<target>`, with
///   checksums in modified base64, and the target `superblock` or a part
///   number with no leading zero
/// - `delta-indexes/<2>/<41>.index`, with a checksum in modified base64
/// - `state/<64 hex digits>.commitpartial`, with lowercase hex digits: the
///   marker of a partial commit.
pub fn is_repository_key(key: &str) -> bool {
    if SINGLE_KEYS.contains(&key) {
        return true;
    }
    if let Some(rest) = key.strip_prefix("objects/") {
        return is_object_key(rest);
    }
    if let Some(name) = key.strip_prefix("refs/heads/") {
        return is_ref_name(name);
    }
    if let Some(rest) = key.strip_prefix("refs/mirrors/") {
        return rest.split_once('/').is_some_and(|(id, name)| {
            id.len() <= MAX_COLLECTION_ID_LEN && is_collection_id(id) && is_ref_name(name)
        });
    }
    if let Some(rest) = key.strip_prefix("deltas/") {
        return is_delta_key(rest);
    }
    if let Some(rest) = key.strip_prefix("delta-indexes/") {
        return is_delta_index_key(rest);
    }
    if let Some(rest) = key.strip_prefix("state/") {
        return rest
            .strip_suffix(".commitpartial")
            .is_some_and(|hex| is_lower_hex(hex, 64));
    }
    false
}

/// Refuses `key` if it is not a key of a bucket repository.
pub(crate) fn check_key(key: &str) -> Result<()> {
    if is_repository_key(key) {
        Ok(())
    } else {
        Err(Error::InvalidInput(format!(
            "S3 key: {key:?} is not a key of a bucket repository"
        )))
    }
}

/// Refuses `prefix` if it is not the prefix of a list in a bucket
/// repository.
///
/// A list prefix is relative to the prefix of the repository and ends in
/// `/`, so a list never reaches the keys of a sibling prefix. Each segment
/// is not empty and is not `.` or `..`. It holds ASCII letters, digits, `-`,
/// `.`, `_`, `~`, `+`, and the non-ASCII letters and digits of a ref name.
pub(crate) fn check_list_prefix(prefix: &str) -> Result<()> {
    let refused = |why: &str| {
        Err(Error::InvalidInput(format!(
            "S3 list prefix: {prefix:?} {why}"
        )))
    };
    let Some(segments) = prefix.strip_suffix('/') else {
        return refused("does not end in '/'");
    };
    for segment in segments.split('/') {
        if segment.is_empty() || segment == "." || segment == ".." {
            return refused("holds an empty segment, '.', or '..'");
        }
        let allowed = |c: char| {
            if c.is_ascii() {
                c.is_ascii_alphanumeric() || matches!(c, '-' | '.' | '_' | '~' | '+')
            } else {
                c.is_alphanumeric()
            }
        };
        if !segment.chars().all(allowed) {
            return refused("holds a character outside the alphabet of a key");
        }
    }
    Ok(())
}

/// Returns `key` as a request path: each byte other than `A-Z`, `a-z`,
/// `0-9`, `-`, `.`, `_`, `~`, and `/` as `%XX` in uppercase.
///
/// This is the canonical URI of SigV4, so the signature of a key does not
/// depend on how a server reads a raw `+` of a path. A pull over `s3://`
/// sends each path in the same form, through `SigV4::request_path`.
pub(crate) fn encode_path(key: &str) -> String {
    let mut path = String::with_capacity(key.len());
    uri_encode(key.as_bytes(), false, &mut path);
    path
}

/// Returns `value` as a part of a query: each byte other than `A-Z`, `a-z`,
/// `0-9`, `-`, `.`, `_`, and `~` as `%XX` in uppercase. A `/` and a `+` are
/// encoded.
pub(crate) fn encode_query(value: &str) -> String {
    let mut encoded = String::with_capacity(value.len());
    uri_encode(value.as_bytes(), true, &mut encoded);
    encoded
}

/// Returns a key or a prefix of a listing as text.
///
/// With `encoded` set, the listing has the encoding type `url`. Each `%XX`
/// is then the byte that it names, and each `+` is a space, as S3 writes a
/// space in that encoding. A text whose decoded bytes are not UTF-8, or
/// that holds a malformed escape, stays as it is. Such a text is never a key
/// of a bucket repository.
pub(crate) fn decode_listed(text: &str, encoded: bool) -> String {
    if !encoded {
        return text.to_owned();
    }
    let bytes = text.as_bytes();
    let mut decoded = Vec::with_capacity(bytes.len());
    let mut at = 0;
    while at < bytes.len() {
        match bytes[at] {
            b'+' => {
                decoded.push(b' ');
                at += 1;
            }
            b'%' => {
                let Some(byte) = bytes
                    .get(at + 1..at + 3)
                    .and_then(|hex| std::str::from_utf8(hex).ok())
                    .filter(|hex| hex.bytes().all(|b| b.is_ascii_hexdigit()))
                    .and_then(|hex| u8::from_str_radix(hex, 16).ok())
                else {
                    return text.to_owned();
                };
                decoded.push(byte);
                at += 3;
            }
            byte => {
                decoded.push(byte);
                at += 1;
            }
        }
    }
    String::from_utf8(decoded).unwrap_or_else(|_| text.to_owned())
}

/// Returns `true` if `text` is `len` lowercase hex digits.
fn is_lower_hex(text: &str, len: usize) -> bool {
    text.len() == len
        && text
            .bytes()
            .all(|b| b.is_ascii_digit() || (b'a'..=b'f').contains(&b))
}

/// Returns `true` if `rest` is `XX/<62 hex digits>.<ext>`.
fn is_object_key(rest: &str) -> bool {
    let Some((fan, name)) = rest.split_once('/') else {
        return false;
    };
    let Some((hex, ext)) = name.split_once('.') else {
        return false;
    };
    is_lower_hex(fan, 2) && is_lower_hex(hex, 62) && OBJECT_EXTENSIONS.contains(&ext)
}

/// Returns `true` if `head` and `tail` join to a checksum in modified
/// base64.
fn is_split_checksum(head: &str, tail: &str) -> bool {
    head.len() == 2 && Checksum::from_base64_modified(&format!("{head}{tail}")).is_ok()
}

/// Returns `true` if `rest` is the path of a delta file under `deltas/`.
fn is_delta_key(rest: &str) -> bool {
    let mut parts = rest.split('/');
    let (Some(head), Some(tail), Some(target), None) =
        (parts.next(), parts.next(), parts.next(), parts.next())
    else {
        return false;
    };
    let names_delta = match tail.split_once('-') {
        // A modified base64 checksum holds no `-`, so the first `-` splits
        // the source from the target.
        Some((from, to)) => {
            is_split_checksum(head, from) && Checksum::from_base64_modified(to).is_ok()
        }
        None => is_split_checksum(head, tail),
    };
    names_delta && is_delta_target(target)
}

/// Returns `true` if `target` is `superblock` or a part number with no
/// leading zero.
fn is_delta_target(target: &str) -> bool {
    target == "superblock"
        || (!target.is_empty()
            && target.len() <= 10
            && target.bytes().all(|b| b.is_ascii_digit())
            && (target == "0" || !target.starts_with('0')))
}

/// Returns `true` if `rest` is the path of a delta index under
/// `delta-indexes/`.
fn is_delta_index_key(rest: &str) -> bool {
    let Some((head, file)) = rest.split_once('/') else {
        return false;
    };
    let Some(tail) = file.strip_suffix(".index") else {
        return false;
    };
    !tail.contains('/') && is_split_checksum(head, tail)
}

#[cfg(test)]
mod tests {
    use super::*;

    /// A checksum whose modified base64 holds `+` and `_`.
    fn checksum_b64() -> String {
        let mut bytes = [0u8; 32];
        bytes[0] = 0xfb;
        bytes[1] = 0xff;
        bytes[2] = 0xbf;
        let b64 = Checksum::from_bytes(bytes).to_base64_modified();
        assert!(b64.contains('+') && b64.contains('_'), "{b64}");
        b64
    }

    fn other_b64() -> String {
        Checksum::from_bytes([7u8; 32]).to_base64_modified()
    }

    #[test]
    fn each_kind_of_repository_key_passes() {
        let hex = "0123456789abcdef".repeat(4);
        let to = checksum_b64();
        let from = other_b64();
        let mut keys = vec![
            "config".to_owned(),
            "summary".to_owned(),
            "summary.sig".to_owned(),
            "extensions/ostrya/lock".to_owned(),
            "extensions/ostrya/lock-probe".to_owned(),
            "refs/heads/main".to_owned(),
            "refs/heads/exampleos/x86_64/stable".to_owned(),
            "refs/heads/a-b.c_d9".to_owned(),
            "refs/heads/grün".to_owned(),
            "refs/mirrors/org.example.Os/main".to_owned(),
            "refs/mirrors/org.example.Os/exampleos/x86_64/stable".to_owned(),
            format!("refs/mirrors/org.{}/main", "a".repeat(251)),
            format!("deltas/{}/{}/superblock", &to[..2], &to[2..]),
            format!("deltas/{}/{}/0", &to[..2], &to[2..]),
            format!("deltas/{}/{}/12", &to[..2], &to[2..]),
            format!("deltas/{}/{}-{to}/superblock", &from[..2], &from[2..]),
            format!("delta-indexes/{}/{}.index", &to[..2], &to[2..]),
            format!("state/{hex}.commitpartial"),
        ];
        for ext in OBJECT_EXTENSIONS {
            keys.push(format!("objects/{}/{}.{ext}", &hex[..2], &hex[2..]));
        }
        for key in keys {
            assert!(is_repository_key(&key), "{key}");
            check_key(&key).unwrap();
        }
    }

    #[test]
    fn a_key_out_of_the_rules_is_refused() {
        let hex = "0123456789abcdef".repeat(4);
        let to = checksum_b64();
        let keys = [
            String::new(),
            "/config".to_owned(),
            "../config".to_owned(),
            "config/".to_owned(),
            "summary.sig.tmp".to_owned(),
            "extensions/ostrya/other".to_owned(),
            "extensions/ostrya/lock-probe/x".to_owned(),
            "extensions/ostrya/lock-probe2".to_owned(),
            format!("objects/{}/{}.commit", &hex[..2], &hex[2..63]),
            format!("objects/AB/{}.commit", &hex[2..]),
            format!("objects/ab/{}.commit", hex[2..].to_uppercase()),
            format!("objects/{}/{}.COMMIT", &hex[..2], &hex[2..]),
            format!("objects/{}/{}.file", &hex[..2], &hex[2..]),
            format!("objects/{}/{}.unknown", &hex[..2], &hex[2..]),
            format!("objects/zz/{}.commit", &hex[2..]),
            "objects/ab/..".to_owned(),
            "objects/abc".to_owned(),
            "refs/heads/-x".to_owned(),
            "refs/heads/".to_owned(),
            "refs/heads/a//b".to_owned(),
            "refs/heads/a b".to_owned(),
            "refs/mirrors/x".to_owned(),
            "refs/mirrors/org.example.Os/".to_owned(),
            "refs/mirrors//main".to_owned(),
            "refs/mirrors/nodot/main".to_owned(),
            "refs/mirrors/org.example.Os/-x".to_owned(),
            format!("refs/mirrors/org.{}/main", "a".repeat(252)),
            "refs/remotes/origin/main".to_owned(),
            format!("deltas/{}/{}/01", &to[..2], &to[2..]),
            format!("deltas/{}/{}/meta", &to[..2], &to[2..]),
            format!("deltas/{}/{}/", &to[..2], &to[2..]),
            format!("deltas/{}/{}x/superblock", &to[..2], &to[2..]),
            format!("deltas/{}/{}/superblock/x", &to[..2], &to[2..]),
            format!("deltas/{}/{}/superblock", &to[..3], &to[3..]),
            "deltas/AA/!!!/superblock".to_owned(),
            format!("delta-indexes/{}/{}.idx", &to[..2], &to[2..]),
            format!("state/{}.commitpartial", hex.to_uppercase()),
            format!("state/{}.commitpartial", &hex[..63]),
            format!("state/{hex}0.commitpartial"),
            format!("state/{hex}.commit"),
            format!("state/{hex}"),
            format!("state/ab/{hex}.commitpartial"),
        ];
        for key in keys {
            assert!(!is_repository_key(&key), "{key}");
            let err = check_key(&key).unwrap_err();
            assert!(matches!(err, Error::InvalidInput(_)), "{err}");
        }
    }

    #[test]
    fn a_list_prefix_ends_in_a_slash() {
        for prefix in [
            "objects/",
            "objects/ab/",
            "refs/heads/",
            "deltas/",
            "refs/heads/grün/",
        ] {
            check_list_prefix(prefix).unwrap();
        }
        for prefix in [
            "",
            "/",
            "objects",
            "objects//",
            "./",
            "../",
            "a/../",
            "a b/",
            "a%2F/",
        ] {
            assert!(check_list_prefix(prefix).is_err(), "{prefix}");
        }
    }

    #[test]
    fn a_path_encodes_each_byte_outside_the_unreserved_set() {
        assert_eq!(encode_path("repo/config"), "repo/config");
        assert_eq!(encode_path("deltas/a+/b_c~d"), "deltas/a%2B/b_c~d");
        assert_eq!(encode_path("refs/heads/grün"), "refs/heads/gr%C3%BCn");
        assert_eq!(encode_path("a b%"), "a%20b%25");
    }

    #[test]
    fn a_query_value_encodes_slash_and_plus() {
        assert_eq!(encode_query("repo/objects/"), "repo%2Fobjects%2F");
        assert_eq!(encode_query("a+b=c/d"), "a%2Bb%3Dc%2Fd");
        assert_eq!(encode_query("1ueGcx~_-."), "1ueGcx~_-.");
    }

    #[test]
    fn a_listed_key_decodes_as_s3_encodes_it() {
        assert_eq!(decode_listed("enc/a%2Bb", true), "enc/a+b");
        assert_eq!(decode_listed("enc/sp+ace", true), "enc/sp ace");
        assert_eq!(decode_listed("enc/%C3%BC", true), "enc/ü");
        assert_eq!(decode_listed("enc/pct%2541", true), "enc/pct%41");
        assert_eq!(decode_listed("enc/a+b", false), "enc/a+b");
        // A malformed escape and bytes that are not UTF-8 keep the text.
        assert_eq!(decode_listed("enc/%G1", true), "enc/%G1");
        assert_eq!(decode_listed("enc/%E9", true), "enc/%E9");
        assert_eq!(decode_listed("enc/%4", true), "enc/%4");
    }
}