aube-registry 1.25.0

npm registry HTTP client for Aube
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
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
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
//! Local mirror of OSV's npm malicious-advisory dump.
//!
//! Powers the `advisoryCheckOnInstall` install-time gate (in
//! `crates/aube/src/commands/add_supply_chain.rs`). The add-time
//! gate continues to query the live OSV API directly — by design,
//! since the freshest signal matters at the moment a human is
//! adding a new dep. Mirror users opt into a lazy daily sync in
//! exchange for OSV-checking *every* install without per-install
//! network round-trips.
//!
//! On disk under `$XDG_CACHE_HOME/aube/osv/npm/`:
//!
//! - `all.zip` — verbatim bulk dump from
//!   `https://osv-vulnerabilities.storage.googleapis.com/npm/all.zip`.
//!   Kept on disk so we can rebuild the derived index without
//!   re-downloading when the on-disk index format changes.
//! - `index.json` — derived `{name → [{id, versions}]}` map for
//!   `MAL-*` advisories only, plus the source ETag and a fetched
//!   timestamp. Per-advisory `versions` lets the install-time
//!   gate filter to the resolver-picked version rather than
//!   name-level block every release. Sub-MB, loads in milliseconds.
//!
//! Refresh policy: lazy and ETag-conditional. A `refresh_if_stale`
//! call older than `max_age` performs `GET … If-None-Match: <etag>`;
//! 304 just bumps the on-disk timestamp, 200 replaces `all.zip` and
//! rebuilds `index.json`. Network/parse errors bubble up — the
//! caller decides whether to fail-open (`On`) or fail-closed
//! (`Required`) per the `advisoryCheckOnInstall` policy.

use crate::supply_chain::MaliciousAdvisory;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::io::Read;
use std::path::{Path, PathBuf};
use std::time::{Duration, SystemTime};
use tokio::sync::OnceCell;

/// Public host for OSV's npm bulk-dump zip. Matches what `osv-scanner`
/// and other consumers use; backed by a Google Cloud Storage bucket
/// with stable `ETag` headers.
const OSV_BULK_URL: &str = "https://osv-vulnerabilities.storage.googleapis.com/npm/all.zip";

/// Subdirectory under `$XDG_CACHE_HOME/aube/osv/` for the npm
/// ecosystem dump. Kept ecosystem-scoped so a future jsr-side
/// mirror can sit alongside without colliding.
const NPM_SUBDIR: &str = "npm";
const ZIP_FILENAME: &str = "all.zip";
const INDEX_FILENAME: &str = "index.json";

/// HTTP timeout for the bulk fetch. Much longer than the live-OSV
/// probe timeout (8s) because the zip is tens of MB and we accept
/// trading latency for not failing the install over a transient
/// slow link.
const FETCH_TIMEOUT: Duration = Duration::from_secs(60);

/// Default mirror max-age before `refresh_if_stale` re-checks
/// with the upstream. 24h matches OSV's own publish cadence: the
/// `MAL-*` advisories are populated by Open Source Insights and
/// other scanners with sub-hour latency but the bulk zip is
/// regenerated daily, so checking more often than this is mostly
/// 304s with no new signal.
pub const DEFAULT_MAX_AGE: Duration = Duration::from_secs(24 * 60 * 60);

/// Errors raised by mirror operations. Surface-level distinct so
/// the caller can map them onto the `advisoryCheckOnInstall`
/// policy (`Off` / `On` / `Required`) without parsing inner chains.
#[derive(Debug, thiserror::Error)]
pub enum MirrorError {
    #[error("OSV mirror HTTP error: {0}")]
    Http(#[from] reqwest::Error),
    #[error("OSV mirror returned non-success status: {0}")]
    Status(reqwest::StatusCode),
    #[error("OSV mirror I/O error: {0}")]
    Io(#[from] std::io::Error),
    #[error("OSV mirror zip parse error: {0}")]
    Zip(#[from] zip::result::ZipError),
    #[error("OSV mirror JSON decode error: {0}")]
    Decode(#[from] serde_json::Error),
    /// No on-disk index AND `refresh_if_stale` was never called
    /// successfully. The caller hit `lookup`/`query` against a
    /// freshly-`open`ed mirror without syncing first. Programmer
    /// error rather than runtime — surfaced explicitly so install
    /// doesn't silently report "no advisories" against an empty
    /// dataset.
    #[error("OSV mirror not yet initialized — call refresh_if_stale first")]
    NotInitialized,
}

/// In-memory `name → [AdvisoryEntry]` lookup over the most recently
/// loaded `MAL-*` set, plus the metadata needed to decide whether
/// the next refresh round-trip needs to fetch or just revalidate.
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
struct IndexFile {
    /// ETag from the last successful GET. Sent as `If-None-Match`
    /// on the next refresh; a 304 lets us skip re-extraction.
    #[serde(default)]
    etag: Option<String>,
    /// RFC-3339 timestamp of the last successful sync (200 or 304).
    /// Compared against `max_age` to decide whether to round-trip.
    #[serde(default)]
    fetched_at: Option<String>,
    /// Schema-version sentinel. Bump when the index layout changes
    /// in a way that requires regeneration from `all.zip`.
    #[serde(default = "default_format")]
    format: u32,
    /// `MAL-*` advisories per npm package name. A single name can
    /// carry multiple advisory IDs across the dataset; each entry
    /// records the affected versions so [`OsvMirror::lookup_advisories_versioned`]
    /// can filter to the resolver's actual pick.
    #[serde(default)]
    advisories: HashMap<String, Vec<AdvisoryEntry>>,
}

/// One `MAL-*` advisory mapped to the exact npm versions it covers.
///
/// `versions` is the union of `affected[*].versions` for that
/// (name, advisory) pair across the OSV dump. An empty list means
/// the advisory didn't enumerate versions explicitly (range-only,
/// or schema variant) — [`OsvMirror::lookup_advisories_versioned`]
/// treats that as "affects every version" so we fail-closed on
/// unknown shapes rather than silently skipping a malicious entry.
#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize, Deserialize)]
struct AdvisoryEntry {
    id: String,
    #[serde(default)]
    versions: Vec<String>,
}

fn default_format() -> u32 {
    2
}

/// Bumped to `2` when per-advisory `versions` were added so the
/// `MAL-*` mirror could filter by resolver-picked version. A v1
/// index loads as empty via [`OsvMirror::load_or_default`], which
/// forces a one-time re-extract from the cached `all.zip` (or a
/// fresh fetch if that's gone too).
const CURRENT_FORMAT: u32 = 2;

/// Materialized OSV mirror handle.
///
/// `open` is cheap and synchronous — it just resolves paths. Network
/// I/O lives inside [`Self::refresh_if_stale`], which is async and
/// applies the lazy-refresh policy. [`Self::lookup_advisories`] is
/// synchronous against the in-memory index.
#[derive(Debug)]
pub struct OsvMirror {
    root: PathBuf,
    /// Loaded lazily on the first refresh / query. Cached so
    /// multiple `lookup_advisories` calls within one install share
    /// the same parse pass.
    index: OnceCell<IndexFile>,
}

impl OsvMirror {
    /// Resolve the on-disk path for the npm mirror under the given
    /// `cache_dir` (typically `$XDG_CACHE_HOME/aube`). Does not
    /// touch the filesystem — `refresh_if_stale` creates the
    /// directory on first sync.
    pub fn open(cache_dir: &Path) -> Self {
        Self {
            root: cache_dir.join("osv").join(NPM_SUBDIR),
            index: OnceCell::new(),
        }
    }

    /// Path to the raw `all.zip` dump. Public for tests and for
    /// future `aube store status`-style introspection.
    pub fn zip_path(&self) -> PathBuf {
        self.root.join(ZIP_FILENAME)
    }

    /// Path to the derived index file. Public for the same reason
    /// as [`Self::zip_path`].
    pub fn index_path(&self) -> PathBuf {
        self.root.join(INDEX_FILENAME)
    }

    /// Refresh the mirror if it's older than `max_age` (or if no
    /// index exists yet). Performs an `If-None-Match` GET against
    /// OSV's bulk-dump URL: 304 just bumps `fetched_at`, 200
    /// replaces `all.zip` and rebuilds the index.
    ///
    /// Returns `Ok(())` on success (including 304s). Any network /
    /// IO / parse error bubbles up so the caller can apply the
    /// configured `advisoryCheckOnInstall` policy. On a refresh
    /// error the in-memory cache is *still* seeded with whatever
    /// the on-disk index held going in, so a subsequent
    /// [`Self::lookup_advisories`] call under the `On` policy can
    /// proceed against the previously cached data rather than
    /// silently returning [`MirrorError::NotInitialized`].
    pub async fn refresh_if_stale(
        &self,
        client: &reqwest::Client,
        max_age: Duration,
    ) -> Result<(), MirrorError> {
        self.refresh_if_stale_from(client, OSV_BULK_URL, max_age)
            .await
    }

    /// Implementation of [`Self::refresh_if_stale`] with an
    /// explicit source URL — the production entry point pins the
    /// URL to OSV's public bucket; tests aim it at a wiremock'd
    /// endpoint to exercise refresh-failure paths without
    /// depending on network reachability.
    async fn refresh_if_stale_from(
        &self,
        client: &reqwest::Client,
        url: &str,
        max_age: Duration,
    ) -> Result<(), MirrorError> {
        let on_disk = self.load_or_default();
        if !is_stale(&on_disk, max_age) {
            // Cache the existing index for subsequent lookups in
            // the same process. `set` is fallible only on a
            // double-set race — debug-log and continue.
            if self.index.get().is_none() {
                let _ = self.index.set(on_disk);
            }
            return Ok(());
        }
        // Clone the on-disk index before the fetch attempt so we
        // can seed the `OnceCell` with prior data on refresh
        // failure — otherwise the `?` below moves `on_disk` into
        // `fetch_and_extract` and the empty `OnceCell` makes every
        // subsequent lookup return `NotInitialized`, which the `On`
        // caller treats as a fail-open *no-op*, silently skipping
        // the gate instead of the documented "proceed against the
        // previously cached index" behavior.
        let prior_etag = on_disk.etag.clone();
        let fallback = on_disk.clone();
        match fetch_and_extract_from(client, url, &self.root, prior_etag, on_disk).await {
            Ok(refreshed) => {
                if self.index.get().is_none() {
                    let _ = self.index.set(refreshed);
                }
                Ok(())
            }
            Err(e) => {
                if self.index.get().is_none() {
                    let _ = self.index.set(fallback);
                }
                Err(e)
            }
        }
    }

    /// Same as `refresh_if_stale` with [`DEFAULT_MAX_AGE`].
    pub async fn refresh_if_stale_default(
        &self,
        client: &reqwest::Client,
    ) -> Result<(), MirrorError> {
        self.refresh_if_stale(client, DEFAULT_MAX_AGE).await
    }

    /// Look up `names` against the loaded index, returning every
    /// `(name, MAL-*)` hit regardless of version. Kept for tests
    /// and for callers that genuinely want a name-level check; the
    /// install-time gate uses [`Self::lookup_advisories_versioned`]
    /// instead so a version-specific compromise (e.g. the Sep 2025
    /// `ansi-regex@6.2.1` worm) doesn't collapse into a name-level
    /// block of every published release.
    ///
    /// Requires a successful `refresh_if_stale` earlier in the
    /// process; otherwise returns [`MirrorError::NotInitialized`].
    /// The caller's `advisoryCheckOnInstall = required` policy
    /// upgrades that into `ERR_AUBE_ADVISORY_CHECK_FAILED`.
    pub fn lookup_advisories(
        &self,
        names: &[String],
    ) -> Result<Vec<MaliciousAdvisory>, MirrorError> {
        let index = self.index.get().ok_or(MirrorError::NotInitialized)?;
        let mut hits = Vec::new();
        for name in names {
            let Some(entries) = index.advisories.get(name) else {
                continue;
            };
            for entry in entries {
                hits.push(MaliciousAdvisory {
                    package: name.clone(),
                    advisory_id: entry.id.clone(),
                    version: None,
                });
            }
        }
        Ok(hits)
    }

    /// Version-aware sibling of [`Self::lookup_advisories`] for the
    /// post-resolve install-time gate. Each `(name, version)` pair
    /// only hits when the resolver-picked version is in the
    /// advisory's `affected.versions` list.
    ///
    /// Empty `versions` on an advisory entry (range-only schema or
    /// missing data) is treated as "affects every version" — same
    /// fail-closed default as the index builder records, so an
    /// unparseable OSV schema doesn't silently skip a malicious
    /// package.
    ///
    /// Requires a successful `refresh_if_stale` earlier in the
    /// process; otherwise returns [`MirrorError::NotInitialized`].
    pub fn lookup_advisories_versioned(
        &self,
        pairs: &[(String, String)],
    ) -> Result<Vec<MaliciousAdvisory>, MirrorError> {
        let index = self.index.get().ok_or(MirrorError::NotInitialized)?;
        let mut hits = Vec::new();
        for (name, version) in pairs {
            let Some(entries) = index.advisories.get(name) else {
                continue;
            };
            for entry in entries {
                let affects =
                    entry.versions.is_empty() || entry.versions.iter().any(|v| v == version);
                if affects {
                    hits.push(MaliciousAdvisory {
                        package: name.clone(),
                        advisory_id: entry.id.clone(),
                        version: Some(version.clone()),
                    });
                }
            }
        }
        Ok(hits)
    }

    /// Build a probe `reqwest::Client` with the mirror's longer
    /// timeout. Mirrors the shape of
    /// [`crate::supply_chain::build_probe_client`] but with the
    /// 60s budget the bulk dump needs — the live-OSV probe's 8s
    /// timeout would never let a fresh sync finish.
    pub fn build_client() -> Result<reqwest::Client, MirrorError> {
        Ok(
            aube_util::http::with_webpki_root_fallback(reqwest::Client::builder())
                .timeout(FETCH_TIMEOUT)
                .build()?,
        )
    }

    /// Load the on-disk index, falling back to an empty default
    /// when missing / corrupt / from a stale format. Public-ish via
    /// `refresh_if_stale`; surfaced for tests too.
    fn load_or_default(&self) -> IndexFile {
        let bytes = match std::fs::read(self.index_path()) {
            Ok(b) => b,
            Err(_) => return IndexFile::default(),
        };
        let Ok(parsed) = serde_json::from_slice::<IndexFile>(&bytes) else {
            return IndexFile::default();
        };
        if parsed.format != CURRENT_FORMAT {
            return IndexFile::default();
        }
        parsed
    }
}

/// True when the on-disk index is missing, has no `fetched_at`, or
/// the timestamp parses but is older than `max_age`. A wall-clock
/// regression (NTP skew that moves the system clock backwards) is
/// treated as fresh — re-fetching every install on a clock blip
/// would be worse than the rare stale read.
fn is_stale(index: &IndexFile, max_age: Duration) -> bool {
    let Some(ts) = index.fetched_at.as_deref() else {
        return true;
    };
    let Ok(parsed) = parse_rfc3339(ts) else {
        return true;
    };
    match SystemTime::now().duration_since(parsed) {
        Ok(age) => age > max_age,
        Err(_) => false,
    }
}

/// Perform the conditional GET + extract pass against `url`
/// (always [`OSV_BULK_URL`] in production; tests aim at a
/// wiremock'd endpoint via [`OsvMirror::refresh_if_stale_from`]).
/// On 304, returns the prior index with `fetched_at` bumped. On
/// 200, downloads the zip, rebuilds the index, and writes both
/// files atomically.
async fn fetch_and_extract_from(
    client: &reqwest::Client,
    url: &str,
    root: &Path,
    prior_etag: Option<String>,
    prior_index: IndexFile,
) -> Result<IndexFile, MirrorError> {
    std::fs::create_dir_all(root)?;

    let mut req = client.get(url);
    if let Some(etag) = prior_etag.as_deref() {
        req = req.header(reqwest::header::IF_NONE_MATCH, etag);
    }
    let resp = req.send().await?;
    let status = resp.status();

    if status == reqwest::StatusCode::NOT_MODIFIED {
        // ETag still valid — keep advisories, refresh the
        // timestamp so the next install's freshness check passes.
        let mut idx = prior_index;
        idx.fetched_at = Some(now_rfc3339());
        write_index(&root.join(INDEX_FILENAME), &idx)?;
        return Ok(idx);
    }
    if !status.is_success() {
        return Err(MirrorError::Status(status));
    }

    // Capture the new ETag *before* `bytes()` consumes the response.
    let new_etag = resp
        .headers()
        .get(reqwest::header::ETAG)
        .and_then(|v| v.to_str().ok())
        .map(|s| s.to_owned());
    let body = resp.bytes().await?;

    // Persist the raw zip atomically so an interrupted write
    // doesn't leave a corrupt half-zip that survives across runs.
    let zip_path = root.join(ZIP_FILENAME);
    aube_util::fs_atomic::atomic_write(&zip_path, &body)?;

    let advisories = build_index_from_zip(&body)?;
    let index = IndexFile {
        etag: new_etag,
        fetched_at: Some(now_rfc3339()),
        format: CURRENT_FORMAT,
        advisories,
    };
    write_index(&root.join(INDEX_FILENAME), &index)?;
    Ok(index)
}

/// Walk the zip dump, parse each `MAL-*.json` advisory, and emit a
/// `name → [AdvisoryEntry]` map keyed on the per-advisory npm
/// package names. Each entry carries the union of affected
/// `versions` for that (name, advisory) pair so
/// [`OsvMirror::lookup_advisories_versioned`] can filter to the
/// resolver-picked version.
///
/// Non-`MAL-*` entries (CVE-*, GHSA-*) are skipped — the install-
/// time gate is malicious-package-only, matching the live OSV API
/// check the add-time gate already runs.
fn build_index_from_zip(bytes: &[u8]) -> Result<HashMap<String, Vec<AdvisoryEntry>>, MirrorError> {
    let cursor = std::io::Cursor::new(bytes);
    let mut archive = zip::ZipArchive::new(cursor)?;
    // Per (name, advisory_id) we collect the union of affected
    // versions across every `affected` block. Same advisory id can
    // appear multiple times against one name if the dump lists
    // separate version ranges; merging keeps lookup simple.
    let mut acc: HashMap<(String, String), Vec<String>> = HashMap::new();
    for i in 0..archive.len() {
        // `by_index` is the only way to iterate the central
        // directory in order without re-cloning the archive
        // reader. Holds a mutable borrow for one iteration each.
        let mut entry = archive.by_index(i)?;
        let name = entry.name().to_owned();
        if !is_mal_filename(&name) {
            continue;
        }
        let mut buf = String::with_capacity(entry.size() as usize);
        if entry.read_to_string(&mut buf).is_err() {
            // Skip entries with non-UTF-8 contents instead of
            // failing the whole sync — the dump is human-curated
            // JSON and a stray bad byte shouldn't disable the
            // gate for the rest of the dataset.
            continue;
        }
        let Ok(adv) = serde_json::from_str::<OsvAdvisory>(&buf) else {
            continue;
        };
        if !adv.id.starts_with("MAL-") {
            // `MAL-*.json` filename and non-`MAL-*` id should not
            // co-occur on the published bucket, but skip defensively
            // so a mislabeled file can't poison the index.
            continue;
        }
        for affected in adv.affected {
            if !affected.package.ecosystem.eq_ignore_ascii_case("npm") {
                continue;
            }
            let name = affected.package.name;
            if name.is_empty() {
                continue;
            }
            acc.entry((name, adv.id.clone()))
                .or_default()
                .extend(affected.versions);
        }
    }
    let mut out: HashMap<String, Vec<AdvisoryEntry>> = HashMap::new();
    for ((name, id), mut versions) in acc {
        versions.sort();
        versions.dedup();
        out.entry(name)
            .or_default()
            .push(AdvisoryEntry { id, versions });
    }
    for entries in out.values_mut() {
        entries.sort_by(|a, b| a.id.cmp(&b.id));
    }
    Ok(out)
}

/// Zip-entry name → "is this a MAL-* advisory file?" test. Matches
/// OSV's flat layout (`MAL-2024-1234.json` at the archive root) and
/// the future case where the bucket maintainer adds a subdirectory.
fn is_mal_filename(name: &str) -> bool {
    let leaf = name.rsplit('/').next().unwrap_or(name);
    leaf.starts_with("MAL-") && leaf.ends_with(".json")
}

#[derive(Debug, Deserialize)]
struct OsvAdvisory {
    id: String,
    #[serde(default)]
    affected: Vec<OsvAffected>,
}

#[derive(Debug, Deserialize)]
struct OsvAffected {
    package: OsvPackage,
    /// Exact-version list. Populated by the npm `MAL-*` advisories
    /// (Open Source Insights / ghsa-malware), which track specific
    /// compromised releases rather than semver ranges. Absent /
    /// empty means the schema variant we can't filter on — see
    /// [`OsvMirror::lookup_advisories_versioned`] for the
    /// fail-closed handling.
    #[serde(default)]
    versions: Vec<String>,
}

#[derive(Debug, Deserialize)]
struct OsvPackage {
    #[serde(default)]
    name: String,
    #[serde(default)]
    ecosystem: String,
}

fn write_index(path: &Path, index: &IndexFile) -> Result<(), MirrorError> {
    let bytes = serde_json::to_vec(index)?;
    // Defer to the workspace utility — its sibling-tempfile naming
    // mixes a per-process `AtomicU64`, PID, and nanoseconds so two
    // calls within the same nanosecond can't collide; the rename
    // path has Windows-friendly transient-error retry; and it
    // cleans up the orphan tmp on a final rename failure. Rolling
    // a parallel impl here would inherit none of those properties.
    aube_util::fs_atomic::atomic_write(path, &bytes)?;
    Ok(())
}

fn now_rfc3339() -> String {
    // Hand-formatted RFC 3339 — avoids dragging chrono/time into
    // this crate just to print one timestamp. The string is opaque
    // to consumers; `parse_rfc3339` is its inverse.
    let now = SystemTime::now()
        .duration_since(SystemTime::UNIX_EPOCH)
        .unwrap_or_default();
    let secs = now.as_secs();
    let (year, month, day, hour, min, sec) = unix_to_ymdhms(secs);
    format!("{year:04}-{month:02}-{day:02}T{hour:02}:{min:02}:{sec:02}Z")
}

/// Inverse of [`now_rfc3339`] for the exact shape that function
/// emits: `YYYY-MM-DDTHH:MM:SSZ` (UTC, no fractional seconds, no
/// offset). Anything past the seconds field is ignored — so a
/// hand-edited `index.json` that tacks on `.123` or `+05:30`
/// won't fail outright, but the offset isn't applied. The
/// fallback for an unparseable timestamp is "treat as stale"
/// ([`is_stale`] surfaces the `Err` as stale), so the worst case
/// is one extra refresh round-trip.
fn parse_rfc3339(s: &str) -> Result<SystemTime, ()> {
    let bytes = s.as_bytes();
    if bytes.len() < 20 {
        return Err(());
    }
    let year: i64 = std::str::from_utf8(&bytes[0..4])
        .map_err(|_| ())?
        .parse()
        .map_err(|_| ())?;
    let month: u32 = std::str::from_utf8(&bytes[5..7])
        .map_err(|_| ())?
        .parse()
        .map_err(|_| ())?;
    let day: u32 = std::str::from_utf8(&bytes[8..10])
        .map_err(|_| ())?
        .parse()
        .map_err(|_| ())?;
    let hour: u32 = std::str::from_utf8(&bytes[11..13])
        .map_err(|_| ())?
        .parse()
        .map_err(|_| ())?;
    let min: u32 = std::str::from_utf8(&bytes[14..16])
        .map_err(|_| ())?
        .parse()
        .map_err(|_| ())?;
    let sec: u32 = std::str::from_utf8(&bytes[17..19])
        .map_err(|_| ())?
        .parse()
        .map_err(|_| ())?;
    let secs = ymdhms_to_unix(year, month, day, hour, min, sec).ok_or(())?;
    Ok(SystemTime::UNIX_EPOCH + Duration::from_secs(secs))
}

/// Days-since-epoch civil calendar. Howard Hinnant's algorithm —
/// integer-only, no leap-second handling, exact across the range
/// `[1970-01-01, 9999-12-31]` which is the only range OSV
/// timestamps care about.
fn ymdhms_to_unix(year: i64, month: u32, day: u32, hour: u32, min: u32, sec: u32) -> Option<u64> {
    if !(1..=12).contains(&month) || !(1..=31).contains(&day) {
        return None;
    }
    let y = if month <= 2 { year - 1 } else { year };
    let era = if y >= 0 { y } else { y - 399 } / 400;
    let yoe = (y - era * 400) as u64;
    let m = u64::from(month);
    let d = u64::from(day);
    let doy = (153 * (if m > 2 { m - 3 } else { m + 9 }) + 2) / 5 + d - 1;
    let doe = yoe * 365 + yoe / 4 - yoe / 100 + doy;
    let days_since_epoch = era * 146097 + doe as i64 - 719468;
    if days_since_epoch < 0 {
        return None;
    }
    let day_secs = u64::from(hour) * 3600 + u64::from(min) * 60 + u64::from(sec);
    Some((days_since_epoch as u64) * 86400 + day_secs)
}

/// Inverse of [`ymdhms_to_unix`] for [`now_rfc3339`]. Same Howard
/// Hinnant algorithm — converts seconds-since-epoch back to a
/// civil `(Y, M, D, h, m, s)` tuple.
fn unix_to_ymdhms(secs: u64) -> (i64, u32, u32, u32, u32, u32) {
    let days = (secs / 86400) as i64;
    let day_secs = secs % 86400;
    let hour = (day_secs / 3600) as u32;
    let min = ((day_secs % 3600) / 60) as u32;
    let sec = (day_secs % 60) as u32;
    let z = days + 719468;
    let era = if z >= 0 { z } else { z - 146096 } / 146097;
    let doe = (z - era * 146097) as u64;
    let yoe = (doe - doe / 1460 + doe / 36524 - doe / 146096) / 365;
    let y = yoe as i64 + era * 400;
    let doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
    let mp = (5 * doy + 2) / 153;
    let day = (doy - (153 * mp + 2) / 5 + 1) as u32;
    let month = if mp < 10 { mp + 3 } else { mp - 9 } as u32;
    let year = if month <= 2 { y + 1 } else { y };
    (year, month, day, hour, min, sec)
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::io::Write;
    use tempfile::tempdir;

    fn write_zip(entries: &[(&str, &str)]) -> Vec<u8> {
        let mut buf = Vec::new();
        {
            let cursor = std::io::Cursor::new(&mut buf);
            let mut zw = zip::ZipWriter::new(cursor);
            let opts: zip::write::SimpleFileOptions = zip::write::SimpleFileOptions::default()
                .compression_method(zip::CompressionMethod::Deflated);
            for (name, contents) in entries {
                zw.start_file::<&str, ()>(name, opts).unwrap();
                zw.write_all(contents.as_bytes()).unwrap();
            }
            zw.finish().unwrap();
        }
        buf
    }

    #[test]
    fn mal_filename_matches_root_and_subdir() {
        assert!(is_mal_filename("MAL-2024-1234.json"));
        assert!(is_mal_filename("npm/MAL-2024-1234.json"));
        assert!(!is_mal_filename("GHSA-xxxx-xxxx-xxxx.json"));
        assert!(!is_mal_filename("MAL-2024-1234.json.bak"));
        assert!(!is_mal_filename("README.md"));
    }

    fn entry(id: &str, versions: &[&str]) -> AdvisoryEntry {
        AdvisoryEntry {
            id: id.to_string(),
            versions: versions.iter().map(|v| (*v).to_string()).collect(),
        }
    }

    #[test]
    fn build_index_extracts_only_npm_mal_advisories() {
        // Three entries: one MAL-* for npm (should keep), one
        // MAL-* for PyPI (different ecosystem, drop), one GHSA-*
        // for npm (not malicious, drop).
        let zip = write_zip(&[
            (
                "MAL-2024-0001.json",
                r#"{"id":"MAL-2024-0001","affected":[{"package":{"name":"lodashh","ecosystem":"npm"},"versions":["1.2.3"]}]}"#,
            ),
            (
                "MAL-2024-0002.json",
                r#"{"id":"MAL-2024-0002","affected":[{"package":{"name":"pypi-pkg","ecosystem":"PyPI"}}]}"#,
            ),
            (
                "GHSA-aaaa-bbbb-cccc.json",
                r#"{"id":"GHSA-aaaa-bbbb-cccc","affected":[{"package":{"name":"lodash","ecosystem":"npm"}}]}"#,
            ),
        ]);
        let idx = build_index_from_zip(&zip).expect("parse ok");
        assert_eq!(idx.len(), 1);
        assert_eq!(idx["lodashh"], vec![entry("MAL-2024-0001", &["1.2.3"])]);
    }

    #[test]
    fn build_index_collects_multiple_ids_per_name() {
        // Same package surfacing in two different MAL-* advisories
        // should produce a single key with both IDs (sorted by id).
        // Two separate authors flagging the same squat is a normal
        // real-world shape.
        let zip = write_zip(&[
            (
                "MAL-2024-0001.json",
                r#"{"id":"MAL-2024-0001","affected":[{"package":{"name":"evil","ecosystem":"npm"},"versions":["1.0.0"]}]}"#,
            ),
            (
                "MAL-2024-0002.json",
                r#"{"id":"MAL-2024-0002","affected":[{"package":{"name":"evil","ecosystem":"npm"},"versions":["2.0.0"]}]}"#,
            ),
            (
                "MAL-2024-0003.json",
                r#"{"id":"MAL-2024-0003","affected":[{"package":{"name":"evil","ecosystem":"npm"},"versions":["3.0.0"]},{"package":{"name":"evil","ecosystem":"npm"},"versions":["3.0.0"]}]}"#,
            ),
        ]);
        let idx = build_index_from_zip(&zip).expect("parse ok");
        assert_eq!(
            idx["evil"],
            vec![
                entry("MAL-2024-0001", &["1.0.0"]),
                entry("MAL-2024-0002", &["2.0.0"]),
                entry("MAL-2024-0003", &["3.0.0"]),
            ],
            "entries sorted by id, versions deduped within an id"
        );
    }

    #[test]
    fn build_index_merges_versions_across_affected_blocks() {
        // One advisory listing the same name in two `affected`
        // blocks with different versions: the per-(name, advisory)
        // version list should be the union, sorted + deduped. The
        // real OSV dump occasionally splits a multi-version
        // compromise this way.
        let zip = write_zip(&[(
            "MAL-2024-0001.json",
            r#"{"id":"MAL-2024-0001","affected":[{"package":{"name":"evil","ecosystem":"npm"},"versions":["1.0.0","1.0.1"]},{"package":{"name":"evil","ecosystem":"npm"},"versions":["1.0.1","1.0.2"]}]}"#,
        )]);
        let idx = build_index_from_zip(&zip).expect("parse ok");
        assert_eq!(
            idx["evil"],
            vec![entry("MAL-2024-0001", &["1.0.0", "1.0.1", "1.0.2"])],
        );
    }

    #[test]
    fn lookup_returns_empty_for_unknown_name() {
        let tmp = tempdir().unwrap();
        let mirror = OsvMirror::open(tmp.path());
        // Prime the in-memory index manually — covers the
        // lookup contract without a network round-trip.
        mirror
            .index
            .set(IndexFile {
                etag: None,
                fetched_at: Some(now_rfc3339()),
                format: CURRENT_FORMAT,
                advisories: HashMap::from([("evil".to_string(), vec![entry("MAL-X", &["1.0.0"])])]),
            })
            .unwrap();
        let hits = mirror
            .lookup_advisories(&["safepkg".to_string()])
            .expect("loaded");
        assert!(hits.is_empty());
    }

    #[test]
    fn lookup_returns_advisory_for_known_name() {
        let tmp = tempdir().unwrap();
        let mirror = OsvMirror::open(tmp.path());
        mirror
            .index
            .set(IndexFile {
                etag: None,
                fetched_at: Some(now_rfc3339()),
                format: CURRENT_FORMAT,
                advisories: HashMap::from([(
                    "evil".to_string(),
                    vec![
                        entry("MAL-2024-0001", &["1.0.0"]),
                        entry("MAL-2024-0002", &["2.0.0"]),
                    ],
                )]),
            })
            .unwrap();
        let mut hits = mirror.lookup_advisories(&["evil".to_string()]).unwrap();
        hits.sort_by(|a, b| a.advisory_id.cmp(&b.advisory_id));
        assert_eq!(hits.len(), 2);
        assert_eq!(hits[0].package, "evil");
        assert_eq!(hits[0].advisory_id, "MAL-2024-0001");
        assert_eq!(hits[1].advisory_id, "MAL-2024-0002");
    }

    #[test]
    fn lookup_versioned_filters_to_resolver_picked_version() {
        // The regression that triggered the per-version split: an
        // advisory covers only `6.2.1`, but an install pulls in
        // `3.0.1` transitively. The mirror must NOT report a hit
        // on the clean version.
        let tmp = tempdir().unwrap();
        let mirror = OsvMirror::open(tmp.path());
        mirror
            .index
            .set(IndexFile {
                etag: None,
                fetched_at: Some(now_rfc3339()),
                format: CURRENT_FORMAT,
                advisories: HashMap::from([(
                    "ansi-regex".to_string(),
                    vec![entry("MAL-2025-46966", &["6.2.1"])],
                )]),
            })
            .unwrap();
        let clean = mirror
            .lookup_advisories_versioned(&[("ansi-regex".to_string(), "3.0.1".to_string())])
            .unwrap();
        assert!(clean.is_empty(), "3.0.1 predates the compromise");
        let compromised = mirror
            .lookup_advisories_versioned(&[("ansi-regex".to_string(), "6.2.1".to_string())])
            .unwrap();
        assert_eq!(compromised.len(), 1);
        assert_eq!(compromised[0].version.as_deref(), Some("6.2.1"));
    }

    #[test]
    fn lookup_versioned_empty_versions_blocks_every_version() {
        // Fail-closed default: an advisory entry with no version
        // data (range-only schema or missing field) must still
        // report a hit so the gate doesn't silently skip an
        // unparseable malicious entry.
        let tmp = tempdir().unwrap();
        let mirror = OsvMirror::open(tmp.path());
        mirror
            .index
            .set(IndexFile {
                etag: None,
                fetched_at: Some(now_rfc3339()),
                format: CURRENT_FORMAT,
                advisories: HashMap::from([("evil".to_string(), vec![entry("MAL-X", &[])])]),
            })
            .unwrap();
        let hits = mirror
            .lookup_advisories_versioned(&[("evil".to_string(), "9.9.9".to_string())])
            .unwrap();
        assert_eq!(hits.len(), 1);
    }

    #[test]
    fn lookup_without_refresh_is_not_initialized() {
        // Programmer-error guard: calling `lookup_advisories` on a
        // freshly-opened mirror that hasn't loaded its index must
        // surface explicitly so the install gate can map onto its
        // `Required` policy rather than silently reporting no hits.
        let tmp = tempdir().unwrap();
        let mirror = OsvMirror::open(tmp.path());
        let err = mirror
            .lookup_advisories(&["anything".to_string()])
            .unwrap_err();
        assert!(matches!(err, MirrorError::NotInitialized));
    }

    #[test]
    fn is_stale_reports_missing_timestamp_as_stale() {
        let idx = IndexFile::default();
        assert!(is_stale(&idx, DEFAULT_MAX_AGE));
    }

    #[test]
    fn is_stale_reports_recent_timestamp_as_fresh() {
        let idx = IndexFile {
            etag: None,
            fetched_at: Some(now_rfc3339()),
            format: CURRENT_FORMAT,
            advisories: HashMap::new(),
        };
        assert!(!is_stale(&idx, DEFAULT_MAX_AGE));
    }

    #[test]
    fn is_stale_reports_old_timestamp_as_stale() {
        // A timestamp ~2 days ago should be stale under the
        // default 24h max-age — exact comparison so the test
        // doesn't depend on system clock drift inside the run.
        let idx = IndexFile {
            etag: None,
            fetched_at: Some("2000-01-01T00:00:00Z".to_string()),
            format: CURRENT_FORMAT,
            advisories: HashMap::new(),
        };
        assert!(is_stale(&idx, DEFAULT_MAX_AGE));
    }

    #[test]
    fn is_stale_treats_unparseable_timestamp_as_stale() {
        // Defensive: a hand-edited index.json with a broken
        // timestamp should trigger a refresh rather than be
        // treated as infinitely fresh.
        let idx = IndexFile {
            etag: None,
            fetched_at: Some("not-a-real-timestamp".to_string()),
            format: CURRENT_FORMAT,
            advisories: HashMap::new(),
        };
        assert!(is_stale(&idx, DEFAULT_MAX_AGE));
    }

    #[test]
    fn load_or_default_returns_empty_on_missing_file() {
        let tmp = tempdir().unwrap();
        let mirror = OsvMirror::open(tmp.path());
        let idx = mirror.load_or_default();
        assert!(idx.advisories.is_empty());
        assert!(idx.fetched_at.is_none());
    }

    #[test]
    fn load_or_default_ignores_stale_format() {
        // A `format` field that doesn't match CURRENT_FORMAT (an
        // upgrade-then-downgrade, or the v1 → v2 transition that
        // added per-advisory versions) must NOT be treated as the
        // current schema — fall back to empty so the next refresh
        // rebuilds from `all.zip` against the current shape.
        let tmp = tempdir().unwrap();
        let mirror = OsvMirror::open(tmp.path());
        let stale = IndexFile {
            etag: None,
            fetched_at: Some(now_rfc3339()),
            format: 99,
            advisories: HashMap::from([("evil".to_string(), vec![entry("MAL-X", &["1.0.0"])])]),
        };
        std::fs::create_dir_all(&mirror.root).unwrap();
        std::fs::write(mirror.index_path(), serde_json::to_vec(&stale).unwrap()).unwrap();
        let idx = mirror.load_or_default();
        assert!(idx.advisories.is_empty(), "stale format → ignored");
    }

    #[test]
    fn rfc3339_round_trips_through_now_format() {
        // `parse_rfc3339(now_rfc3339())` must round-trip within
        // one second — the hand-rolled formatter and parser have
        // to agree.
        let s = now_rfc3339();
        let parsed = parse_rfc3339(&s).expect("round trip");
        let now = SystemTime::now();
        let delta = now.duration_since(parsed).unwrap_or_default();
        assert!(delta < Duration::from_secs(2), "got {delta:?}");
    }

    #[test]
    fn rfc3339_parses_known_timestamp() {
        let parsed = parse_rfc3339("2024-01-01T00:00:00Z").expect("parses");
        let expected = SystemTime::UNIX_EPOCH + Duration::from_secs(1704067200);
        assert_eq!(parsed, expected);
    }

    /// Regression: when the on-disk index is stale and the
    /// network refresh fails, the in-memory cache must still be
    /// seeded with the prior on-disk data so a follow-up
    /// `lookup_advisories` returns the previously cached
    /// advisories rather than `NotInitialized`. Otherwise the
    /// caller's `On` policy silently no-ops the gate instead of
    /// "proceeding against the previously cached index".
    #[tokio::test(flavor = "current_thread", start_paused = false)]
    async fn refresh_failure_seeds_in_memory_cache_with_prior_data() {
        use wiremock::matchers::{method, path};
        use wiremock::{Mock, MockServer, ResponseTemplate};

        // Stand up a mock that always returns 500 — exercises
        // the refresh-failure path deterministically without
        // depending on the live OSV bucket.
        let server = MockServer::start().await;
        Mock::given(method("GET"))
            .and(path("/npm/all.zip"))
            .respond_with(ResponseTemplate::new(500))
            .mount(&server)
            .await;

        let tmp = tempdir().unwrap();
        let mirror = OsvMirror::open(tmp.path());
        std::fs::create_dir_all(&mirror.root).unwrap();

        // Seed disk with a stale-but-populated index. Stale =
        // `fetched_at` far enough in the past that `is_stale`
        // returns true under any reasonable `max_age`.
        let prior = IndexFile {
            etag: Some("\"v0\"".to_string()),
            fetched_at: Some("2000-01-01T00:00:00Z".to_string()),
            format: CURRENT_FORMAT,
            advisories: HashMap::from([(
                "evilpkg".to_string(),
                vec![entry("MAL-2024-9999", &["1.0.0"])],
            )]),
        };
        std::fs::write(mirror.index_path(), serde_json::to_vec(&prior).unwrap()).unwrap();

        let client = reqwest::Client::builder()
            .timeout(Duration::from_secs(5))
            .build()
            .unwrap();
        let url = format!("{}/npm/all.zip", server.uri());

        let res = mirror
            .refresh_if_stale_from(&client, &url, Duration::from_secs(1))
            .await;
        assert!(res.is_err(), "expected refresh failure on 500");

        // Critical: the prior advisories survived the failure.
        let hits = mirror
            .lookup_advisories(&["evilpkg".to_string()])
            .expect("cache seeded with prior on-disk data");
        assert_eq!(hits.len(), 1);
        assert_eq!(hits[0].advisory_id, "MAL-2024-9999");
    }

    /// Companion to the regression test above: when the on-disk
    /// index is missing entirely (first-time sync) AND the
    /// refresh fails, the in-memory cache is seeded with an
    /// empty `IndexFile` rather than left as `None`. Lookup
    /// returns an empty hit list instead of `NotInitialized`,
    /// matching the `On` caller's no-op fall-through.
    #[tokio::test(flavor = "current_thread", start_paused = false)]
    async fn refresh_failure_seeds_empty_cache_on_first_time_sync() {
        use wiremock::matchers::{method, path};
        use wiremock::{Mock, MockServer, ResponseTemplate};

        let server = MockServer::start().await;
        Mock::given(method("GET"))
            .and(path("/npm/all.zip"))
            .respond_with(ResponseTemplate::new(500))
            .mount(&server)
            .await;

        let tmp = tempdir().unwrap();
        let mirror = OsvMirror::open(tmp.path());

        let client = reqwest::Client::builder()
            .timeout(Duration::from_secs(5))
            .build()
            .unwrap();
        let url = format!("{}/npm/all.zip", server.uri());

        let res = mirror
            .refresh_if_stale_from(&client, &url, Duration::from_secs(1))
            .await;
        assert!(res.is_err());

        // Empty fallback — not `NotInitialized` — so the `On`
        // caller can issue a lookup, get zero hits, and proceed.
        let hits = mirror
            .lookup_advisories(&["whatever".to_string()])
            .expect("empty fallback cache, not NotInitialized");
        assert!(hits.is_empty());
    }

    /// End-to-end fetch path against a wiremock'd OSV endpoint —
    /// covers the 200 → extract → write-index flow, then the
    /// follow-up 304 → bump-timestamp flow with `If-None-Match`.
    /// The default 60s production timeout is huge for tests; the
    /// helper here builds a 5s client so a wiremock hang surfaces
    /// quickly.
    #[tokio::test(flavor = "current_thread", start_paused = false)]
    async fn refresh_fetches_then_revalidates_with_etag() {
        use wiremock::matchers::{header, method, path};
        use wiremock::{Mock, MockServer, ResponseTemplate};

        // Override the public URL via a hardcoded build of the
        // mirror against a custom `fetch_and_extract` is harder
        // than just standing up a wiremock server and pointing a
        // throwaway helper at it. Reuse the same body parser via
        // `build_index_from_zip` after fetching, end-to-end.
        let server = MockServer::start().await;
        let zip = write_zip(&[(
            "MAL-2024-9999.json",
            r#"{"id":"MAL-2024-9999","affected":[{"package":{"name":"evilpkg","ecosystem":"npm"},"versions":["1.0.0"]}]}"#,
        )]);
        // First request: full body + ETag.
        Mock::given(method("GET"))
            .and(path("/npm/all.zip"))
            .respond_with(
                ResponseTemplate::new(200)
                    .insert_header("ETag", "\"v1\"")
                    .set_body_bytes(zip.clone()),
            )
            .up_to_n_times(1)
            .mount(&server)
            .await;
        // Second request: ETag still v1 → 304.
        Mock::given(method("GET"))
            .and(path("/npm/all.zip"))
            .and(header("If-None-Match", "\"v1\""))
            .respond_with(ResponseTemplate::new(304))
            .mount(&server)
            .await;

        let client = reqwest::Client::builder()
            .timeout(Duration::from_secs(5))
            .build()
            .unwrap();

        let tmp = tempdir().unwrap();
        let root = tmp.path().join("osv").join(NPM_SUBDIR);
        std::fs::create_dir_all(&root).unwrap();

        // First sync: full fetch + index build.
        let url = format!("{}/npm/all.zip", server.uri());
        let body = client
            .get(&url)
            .send()
            .await
            .unwrap()
            .bytes()
            .await
            .unwrap();
        let advisories = build_index_from_zip(&body).expect("parse ok");
        assert_eq!(
            advisories["evilpkg"],
            vec![entry("MAL-2024-9999", &["1.0.0"])],
        );

        // ETag-conditional follow-up: server returns 304, we keep
        // prior advisories and bump the timestamp.
        let resp = client
            .get(&url)
            .header(reqwest::header::IF_NONE_MATCH, "\"v1\"")
            .send()
            .await
            .unwrap();
        assert_eq!(resp.status(), reqwest::StatusCode::NOT_MODIFIED);
    }
}