antigen 0.2.0-beta.1

Structural memory of failure-classes for Rust. Make implicit immunity explicit.
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
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
//! Substrate-witness evaluation for Supply-Chain Defense Family (ADR-025).
//!
//! Drives the witness-leaf state types from [`super::witness`] against a
//! workspace root + the recorded sidecars under `.attest/supply-chain/`.

use std::path::{Path, PathBuf};

use antigen_macros::presents;

use super::manifest::{read_manifest_deps, DepEntry};
use super::schema::{ContentHashRecord, DepAttestation, MaintainerSnapshot, SandboxKind};
use super::witness::{
    ContentHashState, DepAttestedState, DepPinnedState, MaintainerState, SandboxState,
};

// ============================================================================
// Sidecar paths
// ============================================================================

/// Workspace-root-relative path to the supply-chain sidecar root.
#[must_use]
pub fn supply_chain_root(workspace_root: &Path) -> PathBuf {
    workspace_root.join(".attest").join("supply-chain")
}

/// Path to a [`DepAttestation`] sidecar for `<crate>@<version>`.
///
/// Defense-in-depth (sub-clause F): `crate_name` is validated against the
/// safe character set HERE, at the path-building primitive, not only at the
/// call sites. An invalid crate name (path-traversal sequences, separators)
/// resolves to the dep-attest directory itself rather than being joined as a
/// path component, so it cannot escape `supply_chain_root` — a subsequent file
/// read finds no sidecar (the safe failure) instead of probing an attacker-
/// chosen path. This guards the `pub fn` primitive against callers (present or
/// future) that reach it without pre-validating — `evaluate_dep_attested` /
/// `load_content_hash_record` did exactly that.
///
/// Defends [`crate::stdlib::dogfood::PathTraversalViaUnvalidatedComponent`].
#[must_use]
// ADR-029 migration: this path-builder `#[presents]` PathTraversalViaUnvalidatedComponent
// (a `pub fn` reachable by callers that don't pre-validate). The test
// `path_builders_reject_traversal_crate_name` declares it defends the class via
// `#[defended_by]`; the audit cross-references and observes the verdict.
#[presents(PathTraversalViaUnvalidatedComponent)]
pub fn dep_attest_path(workspace_root: &Path, crate_name: &str, version: &str) -> PathBuf {
    let dir = supply_chain_root(workspace_root).join("dep-attest");
    if !is_valid_crate_name(crate_name) || !is_valid_version(version) {
        return dir;
    }
    dir.join(format!("{crate_name}@{version}.json"))
}

/// Path to a [`ContentHashRecord`] sidecar for `<crate>@<version>`.
///
/// Same defense-in-depth as [`dep_attest_path`]: invalid `crate_name`/`version`
/// resolves to the content-hash directory (in-root, non-escaping), never an
/// attacker-controlled traversal.
#[must_use]
// ADR-029 migration: this path-builder `#[presents]` PathTraversalViaUnvalidatedComponent
// (a `pub fn` reachable by callers that don't pre-validate). The test
// `path_builders_reject_traversal_crate_name` declares it defends the class via
// `#[defended_by]`; the audit cross-references and observes the verdict.
#[presents(PathTraversalViaUnvalidatedComponent)]
pub fn content_hash_path(workspace_root: &Path, crate_name: &str, version: &str) -> PathBuf {
    let dir = supply_chain_root(workspace_root).join("content-hash");
    if !is_valid_crate_name(crate_name) || !is_valid_version(version) {
        return dir;
    }
    dir.join(format!("{crate_name}@{version}.json"))
}

/// Path to a [`MaintainerSnapshot`] sidecar for `<crate>`.
///
/// Same defense-in-depth as [`dep_attest_path`]: invalid `crate_name` resolves
/// to the maintainer directory (in-root, non-escaping).
#[must_use]
// ADR-029 migration: this path-builder `#[presents]` PathTraversalViaUnvalidatedComponent
// (a `pub fn` reachable by callers that don't pre-validate). The test
// `path_builders_reject_traversal_crate_name` declares it defends the class via
// `#[defended_by]`; the audit cross-references and observes the verdict.
#[presents(PathTraversalViaUnvalidatedComponent)]
pub fn maintainer_path(workspace_root: &Path, crate_name: &str) -> PathBuf {
    let dir = supply_chain_root(workspace_root).join("maintainer");
    if !is_valid_crate_name(crate_name) {
        return dir;
    }
    dir.join(format!("{crate_name}.json"))
}

/// Returns `true` iff `name` is a safe crate name: only ASCII alphanumeric, `_`, or `-`.
/// Rejects path traversal sequences (`..`, `/`, `\`) and other shell-special characters.
#[must_use]
pub fn is_valid_crate_name(name: &str) -> bool {
    !name.is_empty()
        && name
            .chars()
            .all(|ch| ch.is_ascii_alphanumeric() || ch == '_' || ch == '-')
}

/// Returns `true` iff `version` is safe for path composition.
///
/// Allows ASCII alphanumeric plus `.`, `-`, `+` (the `SemVer` pre-release/build
/// set); rejects path-traversal sequences and separators. Mirrors the version
/// character-set the CLI's `parse_crate_at_version` enforces, applied here at
/// the path-building primitive for defense-in-depth.
#[must_use]
pub fn is_valid_version(version: &str) -> bool {
    !version.is_empty()
        && version
            .chars()
            .all(|ch| ch.is_ascii_alphanumeric() || matches!(ch, '.' | '-' | '+'))
}

// ============================================================================
// dep_pinned
// ============================================================================

/// Evaluate `dep_pinned(crate?)` against the workspace's `Cargo.toml`.
///
/// When `crate_name` is `None`, all deps in the manifest must be exact-
/// pinned for the leaf to pass.
#[must_use]
pub fn evaluate_dep_pinned(workspace_root: &Path, crate_name: Option<&str>) -> DepPinnedState {
    let manifest = workspace_root.join("Cargo.toml");
    let entries = read_manifest_deps(&manifest);
    evaluate_dep_pinned_against(&entries, crate_name)
}

/// Evaluate `dep_pinned(crate?)` against a pre-collected slice of
/// dep entries. Exposed for testing.
#[must_use]
pub fn evaluate_dep_pinned_against(
    entries: &[DepEntry],
    crate_name: Option<&str>,
) -> DepPinnedState {
    let mut unpinned: Vec<String> = Vec::new();
    let mut saw_named = false;

    for entry in entries {
        if let Some(named) = crate_name {
            if entry.name != named {
                continue;
            }
            saw_named = true;
        }

        if !entry.is_exact_pinned() {
            unpinned.push(entry.name.clone());
        }
    }

    if let Some(named) = crate_name {
        if !saw_named {
            return DepPinnedState::NotInManifest {
                crate_name: named.to_string(),
            };
        }
    }

    if unpinned.is_empty() {
        DepPinnedState::AllPinned
    } else {
        DepPinnedState::Unpinned {
            unpinned_deps: unpinned,
        }
    }
}

// ============================================================================
// dep_attested
// ============================================================================

/// Evaluate `dep_attested(crate, version, exact_version)` against the
/// sidecar at `.attest/supply-chain/dep-attest/<crate>@<version>.json`.
#[must_use]
pub fn evaluate_dep_attested(
    workspace_root: &Path,
    crate_name: &str,
    version: &str,
    exact_version: bool,
) -> DepAttestedState {
    let path = dep_attest_path(workspace_root, crate_name, version);
    let Ok(content) = std::fs::read_to_string(&path) else {
        // Try the floor of the dep-attest directory for any version
        // match if exact_version=false.
        if exact_version {
            return DepAttestedState::SidecarMissing;
        }
        return find_any_attest_for_crate(workspace_root, crate_name)
            .unwrap_or(DepAttestedState::SidecarMissing);
    };

    let att = match serde_json::from_str::<DepAttestation>(&content) {
        Err(e) => {
            return DepAttestedState::SidecarMalformed {
                error: e.to_string(),
            };
        }
        Ok(a) => a,
    };

    if !att.has_reviewable_artifact() {
        return DepAttestedState::AttestedWithoutReviewableArtifact;
    }
    if exact_version && att.version != version {
        return DepAttestedState::AttestationStale {
            attested_version: att.version,
            requested_version: version.to_string(),
        };
    }
    DepAttestedState::Attested {
        review_scope: att.review_scope,
    }
}

/// Best-effort scan of the dep-attest directory looking for any version
/// of the named crate. Used by `exact_version=false` callers.
fn find_any_attest_for_crate(workspace_root: &Path, crate_name: &str) -> Option<DepAttestedState> {
    let dir = supply_chain_root(workspace_root).join("dep-attest");
    let entries = std::fs::read_dir(&dir).ok()?;
    for entry in entries.flatten() {
        let path = entry.path();
        let stem = path.file_stem()?.to_string_lossy().to_string();
        // file_stem strips `.json` extension; format is `<crate>@<version>`
        if let Some(at) = stem.find('@') {
            let candidate = &stem[..at];
            if candidate == crate_name {
                let content = std::fs::read_to_string(&path).ok()?;
                let att: DepAttestation = serde_json::from_str(&content).ok()?;
                if !att.has_reviewable_artifact() {
                    return Some(DepAttestedState::AttestedWithoutReviewableArtifact);
                }
                return Some(DepAttestedState::Attested {
                    review_scope: att.review_scope,
                });
            }
        }
    }
    None
}

// ============================================================================
// content_hash_matches
// ============================================================================

/// Evaluate `content_hash_matches(crate, version)` against
/// `.attest/supply-chain/content-hash/<crate>@<version>.json` PLUS the
/// current lockfile checksum.
///
/// **v0.2 limitation**: `current_hash_from_lockfile` reads the
/// `[[package]] checksum = "..."` field. Direct crates.io tarball
/// re-fetch is v0.3+.
///
/// **Three failure modes are structurally distinguished** (per ATK-SC-2-A):
/// - `NoAttestation` — sidecar file missing
/// - `SidecarMalformed` — sidecar present but JSON-corrupt
/// - `CrateNotInLockfile` — lockfile missing or crate@version not present
///
/// Collapsing malformed-sidecar into `NoAttestation` would let an attacker
/// convert a high-severity Mismatch into a low-severity `NoAttestation` by
/// corrupting the sidecar JSON. Each state must surface its own hint.
#[must_use]
pub fn evaluate_content_hash_matches(
    workspace_root: &Path,
    crate_name: &str,
    version: &str,
) -> ContentHashState {
    let recorded = match load_content_hash_record(workspace_root, crate_name, version) {
        Ok(Some(r)) => r,
        Ok(None) => return ContentHashState::NoAttestation,
        Err(e) => return ContentHashState::SidecarMalformed { error: e },
    };

    let lockfile = workspace_root.join("Cargo.lock");
    let Some(current) = current_hash_from_lockfile(&lockfile, crate_name, version) else {
        return ContentHashState::CrateNotInLockfile {
            crate_name: crate_name.to_string(),
        };
    };

    if current == recorded.content_hash {
        ContentHashState::Matches
    } else {
        ContentHashState::Mismatch {
            recorded: recorded.content_hash,
            current,
        }
    }
}

/// Load a content-hash record from disk.
///
/// Returns:
/// - `Ok(Some(record))` — file exists and is well-formed
/// - `Ok(None)` — file is missing (no first-attestation yet)
/// - `Err(parse_error)` — file exists but does NOT deserialize cleanly
///
/// **Per ATK-SC-2-A**: malformed sidecars MUST be distinguishable from
/// missing sidecars. A `Result<Option<_>, _>` separates the two so the
/// caller can surface different audit hints.
///
/// # Errors
///
/// Returns the JSON parse error string if the file exists but fails
/// to deserialize as `ContentHashRecord`.
pub fn load_content_hash_record(
    workspace_root: &Path,
    crate_name: &str,
    version: &str,
) -> Result<Option<ContentHashRecord>, String> {
    let path = content_hash_path(workspace_root, crate_name, version);
    let Ok(content) = std::fs::read_to_string(&path) else {
        return Ok(None);
    };
    serde_json::from_str(&content)
        .map(Some)
        .map_err(|e| e.to_string())
}

/// Persist a content-hash record. Creates the `.attest/supply-chain/
/// content-hash/` directory if missing.
///
/// # Errors
///
/// Returns the IO error from create-dir or write.
pub fn save_content_hash_record(
    workspace_root: &Path,
    record: &ContentHashRecord,
) -> std::io::Result<PathBuf> {
    let path = content_hash_path(workspace_root, &record.crate_name, &record.version);
    if let Some(parent) = path.parent() {
        std::fs::create_dir_all(parent)?;
    }
    let json = serde_json::to_string_pretty(record).map_err(std::io::Error::other)?;
    std::fs::write(&path, &json)?;
    Ok(path)
}

/// Extract the `checksum = "..."` value for a `[[package]]` entry whose
/// `name = ...` AND `version = ...` match. v0.2 implementation: a
/// minimal line-based scanner — Cargo.lock is structurally simple.
#[must_use]
pub fn current_hash_from_lockfile(
    lockfile_path: &Path,
    crate_name: &str,
    version: &str,
) -> Option<String> {
    let content = std::fs::read_to_string(lockfile_path).ok()?;
    let mut in_package = false;
    let mut name_match = false;
    let mut version_match = false;
    let mut checksum: Option<String> = None;

    for line in content.lines() {
        let line = line.trim();
        if line == "[[package]]" {
            // commit any pending match
            if in_package && name_match && version_match && checksum.is_some() {
                return checksum;
            }
            in_package = true;
            name_match = false;
            version_match = false;
            checksum = None;
            continue;
        }
        if !in_package {
            continue;
        }
        if let Some(rest) = line.strip_prefix("name = ") {
            if let Some(v) = strip_quotes_simple(rest) {
                name_match = v == crate_name;
            }
        } else if let Some(rest) = line.strip_prefix("version = ") {
            if let Some(v) = strip_quotes_simple(rest) {
                version_match = v == version;
            }
        } else if let Some(rest) = line.strip_prefix("checksum = ") {
            if let Some(v) = strip_quotes_simple(rest) {
                checksum = Some(v.to_string());
            }
        }
    }
    // EOF: commit the last package if it matched.
    if in_package && name_match && version_match && checksum.is_some() {
        return checksum;
    }
    None
}

fn strip_quotes_simple(s: &str) -> Option<&str> {
    let s = s.trim();
    s.strip_prefix('"').and_then(|s| s.strip_suffix('"'))
}

// ============================================================================
// maintainer_unchanged
// ============================================================================

/// Evaluate `maintainer_unchanged(crate, since_version)` against the
/// maintainer-snapshot sidecar.
///
/// **v0.2**: live crates.io query is not implemented; this evaluator
/// returns `CratesIoQueryUnavailable` if no snapshot is present, and
/// `Unchanged` if the snapshot's `since_version` matches.
#[must_use]
pub fn evaluate_maintainer_unchanged(
    workspace_root: &Path,
    crate_name: &str,
    since_version: &str,
) -> MaintainerState {
    if !is_valid_crate_name(crate_name) {
        return MaintainerState::SnapshotMissing;
    }
    let Some(snap) = load_maintainer_snapshot(workspace_root, crate_name) else {
        return MaintainerState::SnapshotMissing;
    };

    if snap.since_version != since_version {
        // Snapshot version differs from request — we don't have a way
        // to live-query crates.io in v0.2 to detect actual change;
        // surface the unavailable hint.
        return MaintainerState::CratesIoQueryUnavailable;
    }

    // If the snapshot is current we trust it; live re-query is v0.3+.
    MaintainerState::Unchanged
}

/// Load a maintainer snapshot from disk; `None` on missing/malformed.
#[must_use]
pub fn load_maintainer_snapshot(
    workspace_root: &Path,
    crate_name: &str,
) -> Option<MaintainerSnapshot> {
    let path = maintainer_path(workspace_root, crate_name);
    let content = std::fs::read_to_string(&path).ok()?;
    serde_json::from_str(&content).ok()
}

// ============================================================================
// sandbox_clean
// ============================================================================

/// Evaluate `sandbox_clean(crate, sandbox_kind)`.
///
/// **v0.2**: returns `ToolingNotYetAvailable` unconditionally — actual
/// sandbox execution is deferred to v0.4+ per ADR-025 tooling-phase 3.
#[must_use]
pub const fn evaluate_sandbox_clean(_crate_name: &str, sandbox_kind: SandboxKind) -> SandboxState {
    SandboxState::ToolingNotYetAvailable { sandbox_kind }
}

#[cfg(test)]
mod tests {
    use super::*;
    // `#[defended_by]` (ADR-029) is used only on the witness test below; import
    // it here rather than at module scope (where it would be unused).
    use crate::supply_chain::schema::ReviewScope;
    use antigen_macros::defended_by;
    use tempfile::TempDir;

    fn write_manifest(dir: &Path, content: &str) {
        std::fs::write(dir.join("Cargo.toml"), content).unwrap();
    }

    #[test]
    fn dep_pinned_all_pinned() {
        let tmp = TempDir::new().unwrap();
        write_manifest(
            tmp.path(),
            r#"
[dependencies]
serde = "=1.0.197"
"#,
        );
        assert_eq!(
            evaluate_dep_pinned(tmp.path(), None),
            DepPinnedState::AllPinned
        );
    }

    #[test]
    fn dep_pinned_flags_caret() {
        let tmp = TempDir::new().unwrap();
        write_manifest(
            tmp.path(),
            r#"
[dependencies]
serde = "1.0"
clap = "=4.0"
"#,
        );
        match evaluate_dep_pinned(tmp.path(), None) {
            DepPinnedState::Unpinned { unpinned_deps } => {
                assert_eq!(unpinned_deps, vec!["serde".to_string()]);
            }
            other => panic!("expected Unpinned, got {other:?}"),
        }
    }

    #[test]
    fn dep_pinned_named_not_in_manifest() {
        let tmp = TempDir::new().unwrap();
        write_manifest(
            tmp.path(),
            r#"
[dependencies]
serde = "=1.0.197"
"#,
        );
        match evaluate_dep_pinned(tmp.path(), Some("missing")) {
            DepPinnedState::NotInManifest { crate_name } => assert_eq!(crate_name, "missing"),
            other => panic!("expected NotInManifest, got {other:?}"),
        }
    }

    #[test]
    fn dep_attested_sidecar_missing() {
        let tmp = TempDir::new().unwrap();
        let state = evaluate_dep_attested(tmp.path(), "serde", "1.0.197", true);
        assert_eq!(state, DepAttestedState::SidecarMissing);
    }

    #[test]
    fn dep_attested_empty_artifact_flags_rubber_stamp() {
        let tmp = TempDir::new().unwrap();
        let att = DepAttestation {
            crate_name: "serde".to_string(),
            version: "1.0.197".to_string(),
            exact_version: true,
            reviewable_artifact: PathBuf::new(),
            review_scope: ReviewScope::MetadataOnly,
            signed_by: "alice".to_string(),
            date: "2026-05-22".to_string(),
            rationale: None,
        };
        let path = dep_attest_path(tmp.path(), "serde", "1.0.197");
        std::fs::create_dir_all(path.parent().unwrap()).unwrap();
        std::fs::write(&path, serde_json::to_string(&att).unwrap()).unwrap();

        assert_eq!(
            evaluate_dep_attested(tmp.path(), "serde", "1.0.197", true),
            DepAttestedState::AttestedWithoutReviewableArtifact
        );
    }

    #[test]
    fn content_hash_no_attestation_state() {
        let tmp = TempDir::new().unwrap();
        assert_eq!(
            evaluate_content_hash_matches(tmp.path(), "serde", "1.0.197"),
            ContentHashState::NoAttestation
        );
    }

    #[test]
    fn content_hash_match_path() {
        let tmp = TempDir::new().unwrap();
        // Record
        let record = ContentHashRecord {
            crate_name: "serde".to_string(),
            version: "1.0.197".to_string(),
            content_hash: "abc123".to_string(),
            hash_source: "cargo-lock-checksum".to_string(),
            signed_by: "alice".to_string(),
            date: "2026-05-22".to_string(),
        };
        save_content_hash_record(tmp.path(), &record).unwrap();
        // Cargo.lock with matching checksum
        std::fs::write(
            tmp.path().join("Cargo.lock"),
            r#"
[[package]]
name = "serde"
version = "1.0.197"
checksum = "abc123"
"#,
        )
        .unwrap();
        assert_eq!(
            evaluate_content_hash_matches(tmp.path(), "serde", "1.0.197"),
            ContentHashState::Matches
        );
    }

    #[test]
    fn content_hash_mismatch_path() {
        let tmp = TempDir::new().unwrap();
        let record = ContentHashRecord {
            crate_name: "serde".to_string(),
            version: "1.0.197".to_string(),
            content_hash: "recorded-hash".to_string(),
            hash_source: "cargo-lock-checksum".to_string(),
            signed_by: "alice".to_string(),
            date: "2026-05-22".to_string(),
        };
        save_content_hash_record(tmp.path(), &record).unwrap();
        std::fs::write(
            tmp.path().join("Cargo.lock"),
            r#"
[[package]]
name = "serde"
version = "1.0.197"
checksum = "swapped-hash"
"#,
        )
        .unwrap();
        match evaluate_content_hash_matches(tmp.path(), "serde", "1.0.197") {
            ContentHashState::Mismatch { recorded, current } => {
                assert_eq!(recorded, "recorded-hash");
                assert_eq!(current, "swapped-hash");
            }
            other => panic!("expected Mismatch, got {other:?}"),
        }
    }

    #[test]
    fn content_hash_crate_not_in_lockfile() {
        let tmp = TempDir::new().unwrap();
        let record = ContentHashRecord {
            crate_name: "serde".to_string(),
            version: "1.0.197".to_string(),
            content_hash: "abc".to_string(),
            hash_source: "cargo-lock-checksum".to_string(),
            signed_by: "alice".to_string(),
            date: "2026-05-22".to_string(),
        };
        save_content_hash_record(tmp.path(), &record).unwrap();
        // No Cargo.lock at all.
        match evaluate_content_hash_matches(tmp.path(), "serde", "1.0.197") {
            ContentHashState::CrateNotInLockfile { crate_name } => assert_eq!(crate_name, "serde"),
            other => panic!("expected CrateNotInLockfile, got {other:?}"),
        }
    }

    #[test]
    fn maintainer_missing_snapshot() {
        let tmp = TempDir::new().unwrap();
        assert_eq!(
            evaluate_maintainer_unchanged(tmp.path(), "serde", "1.0.197"),
            MaintainerState::SnapshotMissing
        );
    }

    #[test]
    fn sandbox_v02_returns_tooling_unavailable() {
        let state = evaluate_sandbox_clean("serde", SandboxKind::Build);
        assert_eq!(
            state,
            SandboxState::ToolingNotYetAvailable {
                sandbox_kind: SandboxKind::Build
            }
        );
    }

    // ========================================================================
    // Path-traversal defense-in-depth: the path-builder primitives must refuse
    // to escape supply_chain_root on a malicious crate_name/version, regardless
    // of whether the caller pre-validated. (observer's finding: evaluate_dep_
    // attested + load_content_hash_record reached the path-builders without the
    // is_valid_crate_name check that evaluate_maintainer_unchanged has.)
    // ========================================================================

    /// A path-traversal `crate_name` must NOT produce a path that escapes the
    /// supply-chain root. The guarded path-builder resolves it to the in-root
    /// directory instead of joining the traversal component.
    #[test]
    #[defended_by(PathTraversalViaUnvalidatedComponent)]
    fn path_builders_reject_traversal_crate_name() {
        let root = Path::new("/ws");
        let sc_root = supply_chain_root(root);
        for evil in [
            "../../../etc/passwd",
            "..",
            "foo/bar",
            "foo\\bar",
            "a/../../b",
            "", // empty is also invalid
        ] {
            for built in [
                dep_attest_path(root, evil, "1.0.0"),
                content_hash_path(root, evil, "1.0.0"),
                maintainer_path(root, evil),
            ] {
                assert!(
                    built.starts_with(&sc_root),
                    "path-builder must stay within supply_chain_root for evil \
                     crate_name {evil:?}; got {built:?}"
                );
                // And specifically: it must NOT have joined the traversal — the
                // built path is the bare in-root directory, no escaping component.
                assert!(
                    !built.to_string_lossy().contains(".."),
                    "built path must not carry a `..` traversal component for \
                     {evil:?}; got {built:?}"
                );
            }
        }
    }

    /// A malicious `version` is also blocked (`dep_attest`/`content_hash` compose
    /// it into the filename).
    #[test]
    fn path_builders_reject_traversal_version() {
        let root = Path::new("/ws");
        let sc_root = supply_chain_root(root);
        for evil_version in ["../../../etc", "1.0.0/../../.."] {
            for built in [
                dep_attest_path(root, "serde", evil_version),
                content_hash_path(root, "serde", evil_version),
            ] {
                assert!(built.starts_with(&sc_root));
                assert!(!built.to_string_lossy().contains(".."));
            }
        }
    }

    /// Control: a VALID `crate_name` + version still composes the real sidecar
    /// path (the guard must not over-reject legitimate input).
    #[test]
    fn path_builders_compose_valid_names() {
        let root = Path::new("/ws");
        let p = dep_attest_path(root, "serde_json", "1.0.197");
        assert!(p.ends_with("serde_json@1.0.197.json"));
        let m = maintainer_path(root, "tokio-util");
        assert!(m.ends_with("tokio-util.json"));
    }
}