brain-brew-formats 1.0.0-alpha.8

Implementation package: YAML and CrowdAnki codecs for Brain Brew; no public Rust API commitment
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
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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
use std::collections::BTreeMap;
use std::fmt;

use base64::Engine as _;
use base64::engine::general_purpose::STANDARD as BASE64_STANDARD;
use serde::Deserialize;

use crate::yaml_scalar::{
    is_emittable_key as is_emittable_yaml_key, key as yaml_key, scalar as yaml_scalar,
};

/// Current fail-closed federation lock schema version.
pub const LOCKFILE_VERSION: u32 = 2;

/// Reproducible source lock for a set of Federated Deck package inputs.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct FederationLock {
    pub version: u32,
    pub packages: BTreeMap<String, LockedPackage>,
}

/// One locked package input.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct LockedPackage {
    pub manifest: String,
    pub package: LockedPackageMetadata,
    pub original: OriginalSource,
    pub locked: LockedSource,
}

/// Package metadata captured at lock time.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct LockedPackageMetadata {
    pub version: String,
}

/// Maintainer-requested source selector retained for lock updates and review.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum OriginalSource {
    Path {
        path: String,
    },
    Git {
        url: String,
        reference: Option<String>,
        rev: Option<String>,
    },
    Tarball {
        url: String,
    },
}

impl OriginalSource {
    pub fn source_type(&self) -> &'static str {
        match self {
            Self::Path { .. } => "path",
            Self::Git { .. } => "git",
            Self::Tarball { .. } => "tarball",
        }
    }
}

/// Immutable source identity used to authenticate one package tree.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum LockedSource {
    Path {
        path: String,
        nar_hash: String,
    },
    Git {
        url: String,
        rev: String,
        nar_hash: String,
    },
    Tarball {
        url: String,
        nar_hash: String,
    },
}

impl LockedSource {
    pub fn source_type(&self) -> &'static str {
        match self {
            Self::Path { .. } => "path",
            Self::Git { .. } => "git",
            Self::Tarball { .. } => "tarball",
        }
    }

    pub fn nar_hash(&self) -> &str {
        match self {
            Self::Path { nar_hash, .. }
            | Self::Git { nar_hash, .. }
            | Self::Tarball { nar_hash, .. } => nar_hash,
        }
    }
}

/// Parse a federation lock file from strict YAML.
pub fn from_str(input: &str) -> Result<FederationLock, LockfileError> {
    crate::strict_yaml::reject_duplicate_keys(input).map_err(LockfileError::Parse)?;

    // Check the version before interpreting source fields. In particular, never
    // reinterpret the optional-field v1 schema as a weaker v2 entry.
    let header: LockVersionYaml = serde_yaml::from_str(input).map_err(LockfileError::Parse)?;
    validate_version(header.version)?;

    let yaml: FederationLockYaml = serde_yaml::from_str(input).map_err(LockfileError::Parse)?;
    crate::strict_yaml::reject_unintended_scalars(
        input,
        crate::strict_yaml::ScalarPolicy::Lockfile,
    )
    .map_err(LockfileError::Parse)?;
    yaml.into_lock()
}

/// Parse and re-emit a federation lock file using deterministic formatting.
pub fn format_str(input: &str) -> Result<String, LockfileError> {
    to_string(&from_str(input)?)
}

/// Emit a federation lock file as deterministic YAML.
pub fn to_string(lock: &FederationLock) -> Result<String, LockfileError> {
    validate_version(lock.version)?;
    for (id, package) in &lock.packages {
        validate_yaml_key("packages", id)?;
        validate_package(id, package)?;
    }

    let mut out = String::new();
    out.push_str(&format!("version: {}\n", lock.version));
    if lock.packages.is_empty() {
        out.push_str("packages: {}\n");
        return Ok(out);
    }
    out.push_str("packages:\n");
    for (id, package) in &lock.packages {
        out.push_str(&format!("  {}:\n", emitted_key(id)));
        out.push_str(&format!(
            "    manifest: {}\n",
            yaml_scalar(&package.manifest)
        ));
        out.push_str("    package:\n");
        out.push_str(&format!(
            "      version: {}\n",
            yaml_scalar(&package.package.version)
        ));
        out.push_str("    original:\n");
        write_original_source(&mut out, "      ", &package.original);
        out.push_str("    locked:\n");
        write_locked_source(&mut out, "      ", &package.locked);
    }
    Ok(out)
}

fn emitted_key(value: &str) -> String {
    yaml_key(value).expect("lockfile key was not prevalidated")
}

fn validate_yaml_key(section: &'static str, key: &str) -> Result<(), LockfileError> {
    if is_emittable_yaml_key(key) {
        Ok(())
    } else {
        Err(LockfileError::UnemittableYamlKey {
            section,
            key: key.to_owned(),
        })
    }
}

fn write_original_source(out: &mut String, indent: &str, source: &OriginalSource) {
    out.push_str(&format!("{indent}type: {}\n", source.source_type()));
    match source {
        OriginalSource::Path { path } => {
            out.push_str(&format!("{indent}path: {}\n", yaml_scalar(path)));
        }
        OriginalSource::Git {
            url,
            reference,
            rev,
        } => {
            out.push_str(&format!("{indent}url: {}\n", yaml_scalar(url)));
            if let Some(reference) = reference {
                out.push_str(&format!("{indent}ref: {}\n", yaml_scalar(reference)));
            }
            if let Some(rev) = rev {
                out.push_str(&format!("{indent}rev: {}\n", yaml_scalar(rev)));
            }
        }
        OriginalSource::Tarball { url } => {
            out.push_str(&format!("{indent}url: {}\n", yaml_scalar(url)));
        }
    }
}

fn write_locked_source(out: &mut String, indent: &str, source: &LockedSource) {
    out.push_str(&format!("{indent}type: {}\n", source.source_type()));
    match source {
        LockedSource::Path { path, nar_hash } => {
            out.push_str(&format!("{indent}path: {}\n", yaml_scalar(path)));
            out.push_str(&format!("{indent}nar_hash: {}\n", yaml_scalar(nar_hash)));
        }
        LockedSource::Git { url, rev, nar_hash } => {
            out.push_str(&format!("{indent}url: {}\n", yaml_scalar(url)));
            out.push_str(&format!("{indent}rev: {}\n", yaml_scalar(rev)));
            out.push_str(&format!("{indent}nar_hash: {}\n", yaml_scalar(nar_hash)));
        }
        LockedSource::Tarball { url, nar_hash } => {
            out.push_str(&format!("{indent}url: {}\n", yaml_scalar(url)));
            out.push_str(&format!("{indent}nar_hash: {}\n", yaml_scalar(nar_hash)));
        }
    }
}

#[derive(Debug)]
pub enum LockfileError {
    Parse(serde_yaml::Error),
    LegacyVersion(u32),
    UnsupportedVersion(u32),
    InvalidPackage { package: String, message: String },
    UnemittableYamlKey { section: &'static str, key: String },
}

impl fmt::Display for LockfileError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Parse(error) => write!(f, "failed to parse lock YAML: {error}"),
            Self::LegacyVersion(version) => write!(
                f,
                "federation lock version {version} is insecure and is no longer accepted; move or remove the old lock and regenerate every package with `brainbrew lock update`"
            ),
            Self::UnsupportedVersion(version) => {
                write!(f, "unsupported federation lock version {version}")
            }
            Self::InvalidPackage { package, message } => {
                write!(f, "locked package {package}: {message}")
            }
            Self::UnemittableYamlKey { section, key } => {
                write!(f, "lockfile {section} key {key:?} cannot be emitted safely")
            }
        }
    }
}

impl std::error::Error for LockfileError {}

fn validate_version(version: u32) -> Result<(), LockfileError> {
    match version {
        LOCKFILE_VERSION => Ok(()),
        1 => Err(LockfileError::LegacyVersion(1)),
        other => Err(LockfileError::UnsupportedVersion(other)),
    }
}

fn invalid_package(package: &str, message: impl Into<String>) -> LockfileError {
    LockfileError::InvalidPackage {
        package: package.to_owned(),
        message: message.into(),
    }
}

fn validate_package(package_id: &str, package: &LockedPackage) -> Result<(), LockfileError> {
    crate::package_semver::validate_package_id(package_id)
        .map_err(|error| invalid_package(package_id, format!("invalid package ID: {error}")))?;
    if package.manifest.is_empty() {
        return Err(invalid_package(package_id, "manifest must not be empty"));
    }
    let canonical_version = crate::package_semver::canonical_version(&package.package.version)
        .map_err(|error| {
            invalid_package(
                package_id,
                format!("package.version must be a Semantic Version: {error}"),
            )
        })?;
    if canonical_version != package.package.version {
        return Err(invalid_package(
            package_id,
            format!(
                "package.version {:?} is not canonical; expected {canonical_version:?}",
                package.package.version
            ),
        ));
    }
    if package.original.source_type() != package.locked.source_type() {
        return Err(invalid_package(
            package_id,
            format!(
                "original source type {} does not match locked source type {}",
                package.original.source_type(),
                package.locked.source_type()
            ),
        ));
    }
    validate_original_source(package_id, &package.original)?;
    validate_locked_source(package_id, &package.locked)
}

fn validate_original_source(
    package_id: &str,
    source: &OriginalSource,
) -> Result<(), LockfileError> {
    match source {
        OriginalSource::Path { path } => validate_nonempty(package_id, "original.path", path),
        OriginalSource::Git {
            url,
            reference,
            rev,
        } => {
            validate_nonempty(package_id, "original.url", url)?;
            validate_remote_url(package_id, "original.url", url)?;
            if reference.is_some() && rev.is_some() {
                return Err(invalid_package(
                    package_id,
                    "original git source must not contain both ref and rev",
                ));
            }
            if let Some(reference) = reference {
                validate_nonempty(package_id, "original.ref", reference)?;
            }
            if let Some(rev) = rev {
                validate_nonempty(package_id, "original.rev", rev)?;
            }
            Ok(())
        }
        OriginalSource::Tarball { url } => {
            validate_nonempty(package_id, "original.url", url)?;
            validate_archive_url(package_id, "original.url", url)
        }
    }
}

fn validate_locked_source(package_id: &str, source: &LockedSource) -> Result<(), LockfileError> {
    match source {
        LockedSource::Path { path, nar_hash } => {
            validate_nonempty(package_id, "locked.path", path)?;
            validate_nar_hash(package_id, nar_hash)
        }
        LockedSource::Git { url, rev, nar_hash } => {
            validate_nonempty(package_id, "locked.url", url)?;
            validate_remote_url(package_id, "locked.url", url)?;
            if !is_full_git_commit(rev) {
                return Err(invalid_package(
                    package_id,
                    "locked.rev must be a full lowercase 40-character Git commit",
                ));
            }
            validate_nar_hash(package_id, nar_hash)
        }
        LockedSource::Tarball { url, nar_hash } => {
            validate_nonempty(package_id, "locked.url", url)?;
            validate_archive_url(package_id, "locked.url", url)?;
            validate_nar_hash(package_id, nar_hash)
        }
    }
}

fn validate_remote_url(package_id: &str, field: &str, value: &str) -> Result<(), LockfileError> {
    if value.starts_with("https://") {
        Ok(())
    } else {
        Err(invalid_package(
            package_id,
            format!("{field} must use HTTPS; HTTP and other remote schemes are forbidden"),
        ))
    }
}

fn validate_archive_url(package_id: &str, field: &str, value: &str) -> Result<(), LockfileError> {
    if value.starts_with("https://") || value.starts_with("file://") || !value.contains("://") {
        Ok(())
    } else {
        Err(invalid_package(
            package_id,
            format!("{field} must use HTTPS or an explicit local file path"),
        ))
    }
}

fn validate_nonempty(package_id: &str, field: &str, value: &str) -> Result<(), LockfileError> {
    if value.is_empty() {
        Err(invalid_package(
            package_id,
            format!("{field} must not be empty"),
        ))
    } else {
        Ok(())
    }
}

fn validate_nar_hash(package_id: &str, value: &str) -> Result<(), LockfileError> {
    validate_sha256_sri(value)
        .map_err(|reason| invalid_package(package_id, format!("locked.nar_hash {reason}")))
}

/// Validate exact canonical SRI spelling for one 32-byte SHA-256 digest.
pub fn validate_sha256_sri(value: &str) -> Result<(), &'static str> {
    let Some(encoded) = value.strip_prefix("sha256-") else {
        return Err("must be a canonical SRI SHA-256 string (`sha256-<base64>`)");
    };
    let decoded = BASE64_STANDARD
        .decode(encoded)
        .map_err(|_| "must be a canonical SRI SHA-256 string (`sha256-<base64>`)")?;
    if decoded.len() != 32 || BASE64_STANDARD.encode(&decoded) != encoded {
        return Err("must be a canonical SRI SHA-256 string (`sha256-<base64>`)");
    }
    Ok(())
}

/// Whether a revision is an immutable full GitHub SHA-1 commit id.
pub fn is_full_git_commit(value: &str) -> bool {
    value.len() == 40
        && value
            .bytes()
            .all(|byte| byte.is_ascii_digit() || (b'a'..=b'f').contains(&byte))
}

#[derive(Deserialize)]
struct LockVersionYaml {
    version: u32,
}

#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct FederationLockYaml {
    version: u32,
    #[serde(default)]
    packages: BTreeMap<String, LockedPackageYaml>,
}

impl FederationLockYaml {
    fn into_lock(self) -> Result<FederationLock, LockfileError> {
        validate_version(self.version)?;
        let packages = self
            .packages
            .into_iter()
            .map(|(id, package)| {
                validate_yaml_key("packages", &id)?;
                let package = package.into_locked_package();
                validate_package(&id, &package)?;
                Ok((id, package))
            })
            .collect::<Result<_, LockfileError>>()?;
        Ok(FederationLock {
            version: self.version,
            packages,
        })
    }
}

#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct LockedPackageYaml {
    manifest: String,
    package: LockedPackageMetadataYaml,
    original: OriginalSourceYaml,
    locked: LockedSourceYaml,
}

impl LockedPackageYaml {
    fn into_locked_package(self) -> LockedPackage {
        LockedPackage {
            manifest: self.manifest,
            package: self.package.into_metadata(),
            original: self.original.into_source(),
            locked: self.locked.into_source(),
        }
    }
}

#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct LockedPackageMetadataYaml {
    version: String,
}

impl LockedPackageMetadataYaml {
    fn into_metadata(self) -> LockedPackageMetadata {
        LockedPackageMetadata {
            version: self.version,
        }
    }
}

#[derive(Deserialize)]
#[serde(tag = "type", rename_all = "lowercase", deny_unknown_fields)]
enum OriginalSourceYaml {
    Path {
        path: String,
    },
    Git {
        url: String,
        #[serde(default, rename = "ref")]
        reference: Option<String>,
        #[serde(default)]
        rev: Option<String>,
    },
    Tarball {
        url: String,
    },
}

impl OriginalSourceYaml {
    fn into_source(self) -> OriginalSource {
        match self {
            Self::Path { path } => OriginalSource::Path { path },
            Self::Git {
                url,
                reference,
                rev,
            } => OriginalSource::Git {
                url,
                reference,
                rev,
            },
            Self::Tarball { url } => OriginalSource::Tarball { url },
        }
    }
}

#[derive(Deserialize)]
#[serde(tag = "type", rename_all = "lowercase", deny_unknown_fields)]
enum LockedSourceYaml {
    Path {
        path: String,
        nar_hash: String,
    },
    Git {
        url: String,
        rev: String,
        nar_hash: String,
    },
    Tarball {
        url: String,
        nar_hash: String,
    },
}

impl LockedSourceYaml {
    fn into_source(self) -> LockedSource {
        match self {
            Self::Path { path, nar_hash } => LockedSource::Path { path, nar_hash },
            Self::Git { url, rev, nar_hash } => LockedSource::Git { url, rev, nar_hash },
            Self::Tarball { url, nar_hash } => LockedSource::Tarball { url, nar_hash },
        }
    }
}