pinprick 0.22.0

GitHub Actions supply chain security tool
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
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
use minisign_verify::{PublicKey, Signature};
use serde::Deserialize;
use std::collections::{HashMap, HashSet};
use std::path::{Path, PathBuf};

const BUNDLED_JSON: &str = include_str!(concat!(env!("OUT_DIR"), "/bundled_audited_actions.json"));
const REMOTE_URL: &str = "https://pinprick.rs/audited-actions";

/// Minisign public key for the remote catalog, committed at the repo root and
/// embedded at compile time. The remote layer is fail-closed: without a valid
/// key in the build (or a valid signature on the fetched catalog), remote
/// entries are never honored — a compromised CDN must not be able to mark
/// malicious SHAs as audited.
const CATALOG_PUBKEY_FILE: &str = include_str!("../catalog-minisign.pub");

#[derive(Deserialize)]
struct AuditedEntry {
    #[serde(default)]
    action: Option<String>,
    sha: String,
    #[serde(default)]
    pinprick_version: Option<String>,
}

const LOCAL_CACHE_PINPRICK_VERSION: &str = env!("CARGO_PKG_VERSION");

/// Which layer in the lookup satisfied an audited-action check.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AuditSource {
    /// Compiled into the pinprick binary from `audited-actions/`.
    Bundled,
    /// Read from `~/.cache/pinprick/audited/` (populated by previous scans).
    LocalCache,
    /// Fetched from `https://pinprick.rs/audited-actions/`.
    Remote,
}

impl AuditSource {
    pub fn label(&self) -> &'static str {
        match self {
            Self::Bundled => "bundled",
            Self::LocalCache => "local cache",
            Self::Remote => "pinprick.rs",
        }
    }
}

/// Layered lookup for pre-audited action SHAs.
///
/// Resolution order:
/// 1. **Bundled** — compiled into the binary from `audited-actions/`
/// 2. **Local cache** — `~/.cache/pinprick/audited/{owner}/{repo}.json`
/// 3. **Remote** — `https://pinprick.rs/audited-actions/{owner}/{repo}.json` (opt-in)
///
/// Misses are silent — "not audited" just means the action is scanned via
/// GitHub. The exception is a remote catalog that fails signature
/// verification, which warns: that is either misconfigured serving
/// infrastructure or tampering, and should not pass unnoticed.
pub struct AuditedActions {
    bundled: HashMap<String, HashSet<String>>,
    cache_dir: Option<PathBuf>,
    /// Key used to verify remote catalog signatures. `None` (a build without
    /// a real public key) disables the remote layer entirely.
    catalog_key: Option<PublicKey>,
    client: reqwest::Client,
    fetch_remote: bool,
    local: HashMap<String, HashSet<String>>,
    remote: HashMap<String, HashSet<String>>,
    /// Remote catalog origin, [`REMOTE_URL`] in production. Only the in-module
    /// tests repoint it (at a local mock), so the production host is fixed.
    remote_url: String,
}

impl AuditedActions {
    pub fn new(fetch_remote: bool) -> Self {
        let catalog_key = parse_catalog_key(CATALOG_PUBKEY_FILE);
        if fetch_remote && catalog_key.is_none() {
            eprintln!(
                "warning: remote audited-actions catalog disabled — this build carries no catalog public key"
            );
        }
        Self {
            bundled: load_bundled(),
            cache_dir: cache_dir(),
            catalog_key,
            client: crate::github::build_client(),
            fetch_remote,
            local: HashMap::new(),
            remote: HashMap::new(),
            remote_url: REMOTE_URL.to_string(),
        }
    }

    /// Check if an action at a specific SHA has been pre-audited. Returns
    /// which lookup layer satisfied the check, or `None` if no layer matched.
    pub async fn check(
        &mut self,
        owner: &str,
        repo: &str,
        subpath: Option<&str>,
        sha: &str,
    ) -> Option<AuditSource> {
        let keys = action_keys(owner, repo, subpath)?;

        if keys
            .iter()
            .any(|key| self.bundled.get(key).is_some_and(|shas| shas.contains(sha)))
        {
            return Some(AuditSource::Bundled);
        }

        for key in &keys {
            if !self.local.contains_key(key) {
                let shas = self.load_local_cache(owner, repo, key);
                self.local.insert(key.clone(), shas);
            }
            if self.local.get(key).is_some_and(|shas| shas.contains(sha)) {
                return Some(AuditSource::LocalCache);
            }
        }

        if self.fetch_remote {
            for key in &keys {
                if !self.remote.contains_key(key) {
                    let shas = self.fetch_remote_list(key).await.unwrap_or_default();
                    self.remote.insert(key.clone(), shas);
                }
                if self.remote.get(key).is_some_and(|shas| shas.contains(sha)) {
                    return Some(AuditSource::Remote);
                }
            }
        }

        None
    }

    /// Record a clean scan result in the local cache.
    pub fn cache_clean(
        &self,
        owner: &str,
        repo: &str,
        subpath: Option<&str>,
        sha: &str,
        tag: &str,
    ) {
        let Some(cache_dir) = &self.cache_dir else {
            return;
        };
        let Some(path) = cache_path(cache_dir, owner, repo) else {
            return;
        };
        let dir = cache_dir.join(owner);
        let Some(key) = action_key(owner, repo, subpath) else {
            return;
        };

        let mut entries: Vec<serde_json::Value> = std::fs::read_to_string(&path)
            .ok()
            .and_then(|s| serde_json::from_str(&s).ok())
            .unwrap_or_default();

        // Drop entries written by other pinprick versions before the dedup
        // check. The reader (`parse_local_cache_entries`) already ignores them,
        // so without this a stale same-SHA entry would block the write — the
        // cache would never re-warm after an upgrade. Pruning here also keeps
        // the file from accumulating dead entries.
        entries.retain(|e| {
            e.get("pinprick_version").and_then(|v| v.as_str()) == Some(LOCAL_CACHE_PINPRICK_VERSION)
                && e.get("action").and_then(|v| v.as_str()).is_some()
        });

        if entries.iter().any(|e| {
            e.get("action").and_then(|v| v.as_str()) == Some(key.as_str())
                && e.get("sha").and_then(|s| s.as_str()) == Some(sha)
        }) {
            return;
        }

        entries.push(serde_json::json!({
            "action": key,
            "sha": sha,
            "tag": tag,
            "pinprick_version": LOCAL_CACHE_PINPRICK_VERSION
        }));

        if std::fs::create_dir_all(&dir).is_ok()
            && let Some(json) = render_entries(&entries)
        {
            let _ = std::fs::write(&path, json);
        }
    }

    fn load_local_cache(&self, owner: &str, repo: &str, key: &str) -> HashSet<String> {
        let Some(cache_dir) = &self.cache_dir else {
            return HashSet::new();
        };
        let Some(path) = cache_path(cache_dir, owner, repo) else {
            return HashSet::new();
        };
        let Ok(content) = std::fs::read_to_string(path) else {
            return HashSet::new();
        };
        parse_local_cache_entries(&content, key)
    }

    async fn fetch_remote_list(&self, action_key: &str) -> Option<HashSet<String>> {
        // No public key in this build → the remote layer stays dark.
        let key = self.catalog_key.as_ref()?;

        let url = format!("{}/{action_key}.json", self.remote_url);
        let bytes = match self.fetch_url(&url).await {
            Ok(Some(bytes)) => bytes,
            // 404: the action has no remote catalog — a normal, silent miss.
            Ok(None) => return None,
            // Network or server failure is not absence: the action may well be
            // audited, we just couldn't find out. Say so instead of silently
            // degrading coverage for the rest of the run.
            Err(()) => {
                eprintln!(
                    "warning: could not fetch remote catalog for {action_key} — treating it as unaudited for this run"
                );
                return None;
            }
        };

        // The signature is served next to the catalog file (minisign's `.minisig`
        // convention). A catalog without a valid signature is not honored — a
        // 404 on the catalog itself above is a normal miss, but a present
        // catalog with a missing or bad signature is worth a warning: it means
        // either serving infra is misconfigured or someone tampered with it.
        let sig_bytes = match self.fetch_url(&format!("{url}.minisig")).await {
            Ok(Some(b)) => b,
            Ok(None) => {
                eprintln!(
                    "warning: remote catalog for {action_key} has no signature — ignoring it"
                );
                return None;
            }
            Err(()) => {
                eprintln!(
                    "warning: could not fetch the signature for {action_key}'s remote catalog — ignoring it"
                );
                return None;
            }
        };
        let sig_text = String::from_utf8_lossy(&sig_bytes);
        if !verify_catalog_signature(key, &bytes, &sig_text) {
            eprintln!(
                "warning: remote catalog for {action_key} failed signature verification — ignoring it"
            );
            return None;
        }

        Some(parse_entries(&String::from_utf8_lossy(&bytes)))
    }

    /// GET a URL and return the (size-capped) body. `Ok(None)` means the
    /// server said the resource doesn't exist (404) — confirmed absence.
    /// `Err(())` is any other failure (network, non-404 status, oversized
    /// body), where absence was NOT confirmed.
    async fn fetch_url(&self, url: &str) -> Result<Option<Vec<u8>>, ()> {
        let resp = self
            .client
            .get(url)
            .header("User-Agent", "pinprick")
            .send()
            .await
            .map_err(|_| ())?;

        if resp.status().as_u16() == 404 {
            return Ok(None);
        }
        if !resp.status().is_success() {
            return Err(());
        }

        crate::github::read_capped(resp)
            .await
            .map(Some)
            .map_err(|_| ())
    }
}

/// Parse the embedded `.pub` file into a verification key. Comment lines (and
/// the placeholder file, which is all comments) yield `None`.
fn parse_catalog_key(content: &str) -> Option<PublicKey> {
    let line = content
        .lines()
        .map(str::trim)
        .find(|l| !l.is_empty() && !l.starts_with("untrusted comment:"))?;
    PublicKey::from_base64(line).ok()
}

fn verify_catalog_signature(key: &PublicKey, data: &[u8], sig_text: &str) -> bool {
    match Signature::decode(sig_text) {
        // true: also accept legacy (non-prehashed) signatures.
        Ok(sig) => key.verify(data, &sig, true).is_ok(),
        Err(_) => false,
    }
}

fn load_bundled() -> HashMap<String, HashSet<String>> {
    let map: HashMap<String, Vec<String>> = serde_json::from_str(BUNDLED_JSON).unwrap_or_default();
    map.into_iter()
        .map(|(k, v)| (k, v.into_iter().collect()))
        .collect()
}

fn parse_entries(json: &str) -> HashSet<String> {
    let entries: Vec<AuditedEntry> = serde_json::from_str(json).unwrap_or_default();
    entries.into_iter().map(|e| e.sha).collect()
}

fn parse_local_cache_entries(json: &str, key: &str) -> HashSet<String> {
    let entries: Vec<AuditedEntry> = serde_json::from_str(json).unwrap_or_default();
    entries
        .into_iter()
        .filter(|e| e.pinprick_version.as_deref() == Some(LOCAL_CACHE_PINPRICK_VERSION))
        .filter(|e| e.action.as_deref() == Some(key))
        .map(|e| e.sha)
        .collect()
}

/// Serialize cache entries to their on-disk JSON form. Going through serde
/// (rather than hand-formatting strings) guarantees valid output even when a
/// `tag` contains quotes or backslashes — tags come from arbitrary `# comment`
/// text on a `uses:` line, so they can't be assumed escape-safe.
fn render_entries(entries: &[serde_json::Value]) -> Option<String> {
    serde_json::to_string_pretty(entries)
        .ok()
        .map(|s| format!("{s}\n"))
}

pub fn cache_dir() -> Option<PathBuf> {
    let home = std::env::var("HOME").ok()?;
    Some(PathBuf::from(home).join(".cache/pinprick/audited"))
}

/// Build the on-disk cache path for an action, returning `None` if either
/// segment could escape the cache directory. `owner`/`repo` come from `uses:`
/// parsing and are well-formed in practice — this is defense in depth so a
/// crafted ref (`..`, an embedded separator) can't resolve outside the cache.
fn cache_path(cache_dir: &Path, owner: &str, repo: &str) -> Option<PathBuf> {
    (is_safe_segment(owner) && is_safe_segment(repo))
        .then(|| cache_dir.join(owner).join(format!("{repo}.json")))
}

fn action_key(owner: &str, repo: &str, subpath: Option<&str>) -> Option<String> {
    if !is_safe_segment(owner) || !is_safe_segment(repo) {
        return None;
    }
    match subpath {
        Some(subpath) if is_safe_subpath(subpath) => Some(format!("{owner}/{repo}/{subpath}")),
        Some(_) => None,
        None => Some(format!("{owner}/{repo}")),
    }
}

/// A repository-level clean verdict covers every action at that commit, while
/// a subpath verdict covers only that subpath. Keep the fallback one-way so a
/// clean sibling action can never suppress scanning another sibling.
fn action_keys(owner: &str, repo: &str, subpath: Option<&str>) -> Option<Vec<String>> {
    let exact = action_key(owner, repo, subpath)?;
    match subpath {
        Some(_) => Some(vec![action_key(owner, repo, None)?, exact]),
        None => Some(vec![exact]),
    }
}

fn is_safe_subpath(path: &str) -> bool {
    !path.is_empty() && path.split('/').all(is_safe_segment)
}

fn is_safe_segment(s: &str) -> bool {
    !s.is_empty() && s != "." && s != ".." && !s.contains(['/', '\\'])
}

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

    #[test]
    fn safe_segments_accepted() {
        for s in ["actions", "checkout", "setup-node", "a.b", "..foo", "v1"] {
            assert!(is_safe_segment(s), "{s} should be safe");
        }
    }

    #[test]
    fn unsafe_segments_rejected() {
        for s in ["", ".", "..", "a/b", "a\\b", "/etc", "..\\.."] {
            assert!(!is_safe_segment(s), "{s} should be rejected");
        }
    }

    #[test]
    fn action_key_rejects_unsafe_subpath_components() {
        assert_eq!(
            action_key("owner", "repo", Some("a/b.c")),
            Some("owner/repo/a/b.c".to_string())
        );

        for subpath in ["", ".", "..", "a//b", "a/../b", r"a\\b"] {
            assert_eq!(action_key("owner", "repo", Some(subpath)), None);
        }
        assert_eq!(action_key("owner/name", "repo", Some("a")), None);
        assert_eq!(action_key("owner", "repo/name", Some("a")), None);
    }

    #[test]
    fn subpath_lookup_uses_parent_then_exact_identity() {
        assert_eq!(
            action_keys("owner", "repo", Some("a/b")),
            Some(vec!["owner/repo".to_string(), "owner/repo/a/b".to_string()])
        );
        assert_eq!(
            action_keys("owner", "repo", None),
            Some(vec!["owner/repo".to_string()])
        );
        assert_eq!(action_keys("owner", "repo", Some("../a")), None);
    }

    #[tokio::test]
    async fn parent_bundled_verdict_covers_subpath() {
        let mut aa = AuditedActions::new(false);
        aa.bundled.clear();
        aa.bundled
            .insert("owner/repo".to_string(), HashSet::from(["aaa".to_string()]));
        aa.cache_dir = None;

        assert_eq!(
            aa.check("owner", "repo", Some("restore"), "aaa").await,
            Some(AuditSource::Bundled)
        );
    }

    #[tokio::test]
    async fn sibling_bundled_verdict_does_not_cover_subpath() {
        let mut aa = AuditedActions::new(false);
        aa.bundled.clear();
        aa.bundled.insert(
            "owner/repo/save".to_string(),
            HashSet::from(["aaa".to_string()]),
        );
        aa.cache_dir = None;

        assert_eq!(
            aa.check("owner", "repo", Some("restore"), "aaa").await,
            None
        );
    }

    #[tokio::test]
    async fn parent_local_cache_verdict_covers_subpath() {
        let dir = tempfile::TempDir::new().unwrap();
        let mut aa = AuditedActions::new(false);
        aa.bundled.clear();
        aa.cache_dir = Some(dir.path().to_path_buf());
        aa.cache_clean("owner", "repo", None, "aaa", "v1");

        assert_eq!(
            aa.check("owner", "repo", Some("restore"), "aaa").await,
            Some(AuditSource::LocalCache)
        );
    }

    #[test]
    fn cache_path_stays_inside_cache_dir() {
        let base = Path::new("/cache");
        assert_eq!(
            cache_path(base, "actions", "checkout"),
            Some(PathBuf::from("/cache/actions/checkout.json"))
        );
        // A traversal token or separator in either segment yields no path,
        // so the caller silently skips the cache rather than escaping it.
        assert_eq!(cache_path(base, "..", "checkout"), None);
        assert_eq!(cache_path(base, "actions", "../../etc/passwd"), None);
        assert_eq!(cache_path(base, "", "checkout"), None);
    }

    #[test]
    fn render_entries_round_trips() {
        let entries = vec![
            serde_json::json!({ "sha": "aaa", "tag": "v1" }),
            serde_json::json!({ "sha": "bbb", "tag": "v2" }),
        ];
        let rendered = render_entries(&entries).unwrap();
        assert!(rendered.ends_with('\n'));
        let shas = parse_entries(&rendered);
        assert!(shas.contains("aaa"));
        assert!(shas.contains("bbb"));
    }

    #[test]
    fn render_entries_escapes_adversarial_tag() {
        // A tag with quotes and a backslash would corrupt hand-formatted JSON.
        // serde escapes it, so the file stays valid and round-trips.
        let entries = vec![serde_json::json!({
            "sha": "abc123",
            "tag": r#"v1 "stable" \ release"#,
        })];
        let rendered = render_entries(&entries).unwrap();
        let parsed: Vec<serde_json::Value> =
            serde_json::from_str(&rendered).expect("rendered cache must be valid JSON");
        assert_eq!(parsed[0]["sha"], "abc123");
        assert_eq!(parsed[0]["tag"], r#"v1 "stable" \ release"#);
        // The reader still recovers the sha.
        assert!(parse_entries(&rendered).contains("abc123"));
    }

    #[test]
    fn local_cache_ignores_unversioned_legacy_entries() {
        let rendered = r#"[
  { "sha": "aaa", "tag": "v1" }
]"#;
        assert!(parse_entries(rendered).contains("aaa"));
        assert!(!parse_local_cache_entries(rendered, "owner/repo").contains("aaa"));
    }

    #[test]
    fn local_cache_ignores_owner_repo_only_entries() {
        let rendered = serde_json::to_string(&vec![serde_json::json!({
            "sha": "aaa",
            "tag": "v1",
            "pinprick_version": LOCAL_CACHE_PINPRICK_VERSION
        })])
        .unwrap();
        assert!(!parse_local_cache_entries(&rendered, "owner/repo").contains("aaa"));
    }

    #[test]
    fn local_cache_accepts_current_version_entries() {
        let rendered = serde_json::to_string(&vec![serde_json::json!({
            "action": "owner/repo",
            "sha": "aaa",
            "tag": "v1",
            "pinprick_version": LOCAL_CACHE_PINPRICK_VERSION
        })])
        .unwrap();
        assert!(parse_local_cache_entries(&rendered, "owner/repo").contains("aaa"));
        assert!(!parse_local_cache_entries(&rendered, "owner/repo/subdir").contains("aaa"));
    }

    #[test]
    fn local_cache_separates_subpath_verdicts() {
        let dir = tempfile::TempDir::new().unwrap();
        let mut aa = AuditedActions::new(false);
        aa.cache_dir = Some(dir.path().to_path_buf());

        aa.cache_clean("owner", "repo", Some("a"), "aaa", "v1");

        assert!(
            aa.load_local_cache("owner", "repo", "owner/repo/a")
                .contains("aaa")
        );
        assert!(
            !aa.load_local_cache("owner", "repo", "owner/repo/b")
                .contains("aaa")
        );

        aa.cache_clean("owner", "repo", Some("b"), "aaa", "v1");
        assert!(
            aa.load_local_cache("owner", "repo", "owner/repo/b")
                .contains("aaa")
        );

        let path = cache_path(dir.path(), "owner", "repo").unwrap();
        let on_disk: Vec<serde_json::Value> =
            serde_json::from_str(&std::fs::read_to_string(path).unwrap()).unwrap();
        assert_eq!(on_disk.len(), 2);
    }

    #[test]
    fn cache_clean_rewarms_after_version_change() {
        // A legacy entry (written by an older pinprick, no version field) must
        // not permanently block re-warming. The reader already ignores it, so
        // re-recording the same SHA has to replace it under the current version
        // rather than dedup-skipping — otherwise the cache never re-warms.
        let dir = tempfile::TempDir::new().unwrap();
        let mut aa = AuditedActions::new(false);
        aa.cache_dir = Some(dir.path().to_path_buf());

        let path = cache_path(dir.path(), "owner", "repo").unwrap();
        std::fs::create_dir_all(path.parent().unwrap()).unwrap();
        std::fs::write(&path, r#"[{ "sha": "aaa", "tag": "v1" }]"#).unwrap();
        // Pre-state: the legacy entry is invisible to the reader.
        assert!(
            !aa.load_local_cache("owner", "repo", "owner/repo")
                .contains("aaa")
        );

        aa.cache_clean("owner", "repo", None, "aaa", "v1");

        // The SHA is now cached under the current version…
        assert!(
            aa.load_local_cache("owner", "repo", "owner/repo")
                .contains("aaa")
        );
        // …and the stale legacy entry was pruned rather than duplicated.
        let on_disk: Vec<serde_json::Value> =
            serde_json::from_str(&std::fs::read_to_string(&path).unwrap()).unwrap();
        assert_eq!(on_disk.len(), 1);
    }

    #[test]
    fn embedded_pubkey_parses() {
        // The committed catalog key must parse, or every release silently
        // ships with the remote layer disabled.
        assert!(parse_catalog_key(CATALOG_PUBKEY_FILE).is_some());
    }

    #[test]
    fn comment_only_pubkey_yields_no_key() {
        // A placeholder file (all comment lines) must not parse — it keeps
        // the remote layer disabled rather than panicking.
        assert!(parse_catalog_key("untrusted comment: nothing here\n").is_none());
        assert!(parse_catalog_key("").is_none());
    }

    #[test]
    fn real_pubkey_parses() {
        // The production catalog key in the format minisign writes —
        // exercises comment-stripping on a literal, independent of the
        // embedded file. (The minisign-verify docs key would also do, but
        // its base64 trips the typos linter.)
        let file = "untrusted comment: minisign public key\n\
                    RWRwyp1ae8MrgHSws68tQDd94KGWt1cqdTYZOEcIcPh+cQo9rWJIgC0x\n";
        assert!(parse_catalog_key(file).is_some());
    }

    mod remote {
        use super::*;
        use serde_json::json;
        use wiremock::matchers::{method, path};
        use wiremock::{Mock, MockServer, ResponseTemplate};

        /// Ephemeral signing identity for tests: the verification key (as
        /// parsed from a real `.pub` rendering) and a signer closure.
        fn test_identity() -> (PublicKey, impl Fn(&[u8]) -> String) {
            let minisign::KeyPair { pk, sk } =
                minisign::KeyPair::generate_unencrypted_keypair().unwrap();
            let verify_key = parse_catalog_key(&pk.to_box().unwrap().to_string())
                .expect("generated public key must parse");
            let signer = move |data: &[u8]| {
                minisign::sign(None, &sk, std::io::Cursor::new(data), None, None)
                    .unwrap()
                    .to_string()
            };
            (verify_key, signer)
        }

        /// Mount a catalog body and its signature at `{key}.json[.minisig]`.
        async fn mount_signed(server: &MockServer, action_key: &str, body: &str, sig: &str) {
            Mock::given(method("GET"))
                .and(path(format!("/{action_key}.json")))
                .respond_with(
                    ResponseTemplate::new(200).set_body_raw(body.to_string(), "application/json"),
                )
                .mount(server)
                .await;
            Mock::given(method("GET"))
                .and(path(format!("/{action_key}.json.minisig")))
                .respond_with(ResponseTemplate::new(200).set_body_string(sig.to_string()))
                .mount(server)
                .await;
        }

        #[tokio::test]
        async fn fetch_remote_list_parses_signed_entries() {
            let (key, sign) = test_identity();
            let body = serde_json::to_string(&json!([
                { "sha": "aaa", "tag": "v1" },
                { "sha": "bbb", "tag": "v2" }
            ]))
            .unwrap();

            let server = MockServer::start().await;
            mount_signed(&server, "actions/checkout", &body, &sign(body.as_bytes())).await;

            let mut aa = AuditedActions::new(true);
            aa.catalog_key = Some(key);
            aa.remote_url = server.uri();
            let shas = aa.fetch_remote_list("actions/checkout").await.unwrap();
            assert!(shas.contains("aaa"));
            assert!(shas.contains("bbb"));
        }

        #[tokio::test]
        async fn fetch_remote_list_non_success_is_none() {
            let (key, _) = test_identity();
            let server = MockServer::start().await;
            Mock::given(method("GET"))
                .and(path("/actions/missing.json"))
                .respond_with(ResponseTemplate::new(404))
                .mount(&server)
                .await;

            let mut aa = AuditedActions::new(true);
            aa.catalog_key = Some(key);
            aa.remote_url = server.uri();
            assert!(aa.fetch_remote_list("actions/missing").await.is_none());
        }

        #[tokio::test]
        async fn fetch_url_distinguishes_absence_from_failure() {
            let server = MockServer::start().await;
            Mock::given(method("GET"))
                .and(path("/missing.json"))
                .respond_with(ResponseTemplate::new(404))
                .mount(&server)
                .await;
            Mock::given(method("GET"))
                .and(path("/broken.json"))
                .respond_with(ResponseTemplate::new(500))
                .mount(&server)
                .await;

            let aa = AuditedActions::new(false);
            // 404 is confirmed absence; a server error is not.
            assert_eq!(
                aa.fetch_url(&format!("{}/missing.json", server.uri()))
                    .await,
                Ok(None)
            );
            assert_eq!(
                aa.fetch_url(&format!("{}/broken.json", server.uri())).await,
                Err(())
            );
        }

        #[tokio::test]
        async fn fetch_remote_list_server_error_is_none() {
            let (key, _) = test_identity();
            let server = MockServer::start().await;
            Mock::given(method("GET"))
                .and(path("/actions/flaky.json"))
                .respond_with(ResponseTemplate::new(500))
                .mount(&server)
                .await;

            let mut aa = AuditedActions::new(true);
            aa.catalog_key = Some(key);
            aa.remote_url = server.uri();
            assert!(aa.fetch_remote_list("actions/flaky").await.is_none());
        }

        #[tokio::test]
        async fn fetch_remote_list_missing_signature_is_none() {
            let (key, _) = test_identity();
            let body = serde_json::to_string(&json!([{ "sha": "aaa", "tag": "v1" }])).unwrap();

            let server = MockServer::start().await;
            Mock::given(method("GET"))
                .and(path("/actions/unsigned.json"))
                .respond_with(ResponseTemplate::new(200).set_body_raw(body, "application/json"))
                .mount(&server)
                .await;
            // No .minisig mock → 404 on the signature.

            let mut aa = AuditedActions::new(true);
            aa.catalog_key = Some(key);
            aa.remote_url = server.uri();
            assert!(aa.fetch_remote_list("actions/unsigned").await.is_none());
        }

        #[tokio::test]
        async fn fetch_remote_list_tampered_body_is_none() {
            let (key, sign) = test_identity();
            let signed_body =
                serde_json::to_string(&json!([{ "sha": "aaa", "tag": "v1" }])).unwrap();
            let tampered_body =
                serde_json::to_string(&json!([{ "sha": "evil", "tag": "v1" }])).unwrap();

            let server = MockServer::start().await;
            // Signature is over the original body; the server returns a
            // different one — verification must reject it.
            mount_signed(
                &server,
                "actions/tampered",
                &tampered_body,
                &sign(signed_body.as_bytes()),
            )
            .await;

            let mut aa = AuditedActions::new(true);
            aa.catalog_key = Some(key);
            aa.remote_url = server.uri();
            assert!(aa.fetch_remote_list("actions/tampered").await.is_none());
        }

        #[tokio::test]
        async fn fetch_remote_list_wrong_key_is_none() {
            let (_, sign) = test_identity();
            let (other_key, _) = test_identity();
            let body = serde_json::to_string(&json!([{ "sha": "aaa", "tag": "v1" }])).unwrap();

            let server = MockServer::start().await;
            mount_signed(&server, "actions/wrongkey", &body, &sign(body.as_bytes())).await;

            let mut aa = AuditedActions::new(true);
            aa.catalog_key = Some(other_key);
            aa.remote_url = server.uri();
            assert!(aa.fetch_remote_list("actions/wrongkey").await.is_none());
        }

        #[tokio::test]
        async fn keyless_build_never_fetches() {
            // Even with a perfectly signed catalog available, a build without
            // a public key must not honor remote entries.
            let (_, sign) = test_identity();
            let body = serde_json::to_string(&json!([{ "sha": "aaa", "tag": "v1" }])).unwrap();

            let server = MockServer::start().await;
            mount_signed(&server, "actions/keyless", &body, &sign(body.as_bytes())).await;

            let mut aa = AuditedActions::new(true);
            aa.catalog_key = None;
            aa.remote_url = server.uri();
            assert!(aa.fetch_remote_list("actions/keyless").await.is_none());
        }

        #[tokio::test]
        async fn check_falls_through_to_remote_layer() {
            let (key, sign) = test_identity();
            let body = serde_json::to_string(&json!([{ "sha": "feedface", "tag": "v3" }])).unwrap();

            let server = MockServer::start().await;
            mount_signed(&server, "some/action", &body, &sign(body.as_bytes())).await;

            let mut aa = AuditedActions::new(true);
            aa.catalog_key = Some(key);
            aa.remote_url = server.uri();
            aa.cache_dir = None; // don't consult the real ~/.cache during the test
            // Not bundled, no local cache hit → resolved by the remote layer.
            assert_eq!(
                aa.check("some", "action", None, "feedface").await,
                Some(AuditSource::Remote)
            );
            // A SHA the remote list doesn't contain stays unaudited.
            assert_eq!(aa.check("some", "action", None, "0000").await, None);
        }

        #[tokio::test]
        async fn check_uses_subpath_remote_identity() {
            let (key, sign) = test_identity();
            let body = serde_json::to_string(&json!([{ "sha": "feedface", "tag": "v3" }])).unwrap();

            let server = MockServer::start().await;
            mount_signed(&server, "some/action/a", &body, &sign(body.as_bytes())).await;

            let mut aa = AuditedActions::new(true);
            aa.catalog_key = Some(key);
            aa.remote_url = server.uri();
            aa.cache_dir = None;

            assert_eq!(
                aa.check("some", "action", Some("b"), "feedface").await,
                None
            );
            assert_eq!(
                aa.check("some", "action", Some("a"), "feedface").await,
                Some(AuditSource::Remote)
            );
        }

        #[tokio::test]
        async fn parent_remote_verdict_covers_subpath() {
            let (key, sign) = test_identity();
            let body = serde_json::to_string(&json!([{ "sha": "feedface", "tag": "v3" }])).unwrap();

            let server = MockServer::start().await;
            mount_signed(&server, "some/action", &body, &sign(body.as_bytes())).await;

            let mut aa = AuditedActions::new(true);
            aa.bundled.clear();
            aa.catalog_key = Some(key);
            aa.remote_url = server.uri();
            aa.cache_dir = None;

            assert_eq!(
                aa.check("some", "action", Some("sub"), "feedface").await,
                Some(AuditSource::Remote)
            );
        }
    }
}