alef-component-runtime 0.132.5

Verified downloader and dynamic loader for Alef native components
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
use crate::download::{DownloadOptions, TrackedReader, is_transient_request_error, local_file_url};
use crate::identity::{FileIdentity, VerifiedFile};
use crate::keys::decode_public_key;
use crate::manifest::{COMPONENT_ABI_VERSION, COMPONENT_MANIFEST_SCHEMA};
use crate::stamp;
use crate::{ComponentError, ComponentLockEntry, ComponentManifest, ComponentSignature};
use base64::Engine as _;
use ed25519_dalek::Signature;
use flate2::read::GzDecoder;
use sha2::{Digest as _, Sha256};
use std::borrow::Cow;
use std::collections::BTreeMap;
use std::fs::{self, File, OpenOptions};
use std::io::{Read, Seek as _, SeekFrom, Write as _};
use std::path::{Component, Path, PathBuf};
use std::sync::Arc;

/// Rewrites a lock entry's download URL at runtime; see [`ArtifactCache::with_url_override`].
pub(crate) type UrlOverride = Arc<dyn Fn(&ComponentLockEntry) -> String + Send + Sync>;

#[derive(Clone, Debug)]
pub enum TrustPolicy {
    DigestOnly,
    EmbeddedKeys(BTreeMap<String, String>),
}

/// Whether a component lock must carry signatures.
///
/// [`SignaturePolicy::AllowUnsigned`] (the default) keeps the historical behaviour: a lock with no
/// public keys and entries with no `key_id` is verified by digest alone, which only proves the
/// bytes match a lock that could itself have been edited. [`SignaturePolicy::Require`] makes
/// [`crate::ComponentManager::from_lock_with_signature_policy`] reject such a lock and the cache
/// refuse any artifact whose manifest signature it did not verify.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum SignaturePolicy {
    #[default]
    AllowUnsigned,
    Require,
}

#[derive(Clone)]
pub struct ArtifactCache {
    pub(crate) root: PathBuf,
    trust: TrustPolicy,
    offline: bool,
    pub(crate) download: DownloadOptions,
    url_override: Option<UrlOverride>,
    verified_stamp: bool,
    signatures: SignaturePolicy,
}

impl std::fmt::Debug for ArtifactCache {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("ArtifactCache")
            .field("root", &self.root)
            .field("trust", &self.trust)
            .field("offline", &self.offline)
            .field("download", &self.download)
            .field("url_override", &self.url_override.as_ref().map(|_| "<fn>"))
            .field("verified_stamp", &self.verified_stamp)
            .field("signatures", &self.signatures)
            .finish()
    }
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct CachedArtifact {
    pub root: PathBuf,
    pub library: PathBuf,
    pub manifest: ComponentManifest,
}

impl ArtifactCache {
    pub fn new(root: impl Into<PathBuf>, trust: TrustPolicy) -> Self {
        Self {
            root: root.into(),
            trust,
            offline: false,
            download: DownloadOptions::default(),
            url_override: None,
            verified_stamp: false,
            signatures: SignaturePolicy::AllowUnsigned,
        }
    }

    /// With [`SignaturePolicy::Require`], refuse every artifact whose manifest signature was not
    /// verified against a key in this cache's [`TrustPolicy::EmbeddedKeys`], instead of accepting
    /// a digest-only lock entry.
    #[must_use]
    pub fn with_signature_policy(mut self, policy: SignaturePolicy) -> Self {
        self.signatures = policy;
        self
    }

    /// Replace the download tuning (timeouts, retries, progress, cancellation).
    #[must_use]
    pub fn with_download_options(mut self, options: DownloadOptions) -> Self {
        self.download = options;
        self
    }

    #[must_use]
    pub fn download_options(&self) -> &DownloadOptions {
        &self.download
    }

    /// Fetch artifacts from `resolve(entry)` instead of the lock entry's own `url`.
    ///
    /// Intended for air-gapped or corporate mirrors. Integrity never depends on the URL: the
    /// archive size, SHA-256 and the manifest signature are still checked against the lock.
    /// A returned `file://` URL is read from disk.
    #[must_use]
    pub fn with_url_override(
        mut self,
        resolve: impl Fn(&ComponentLockEntry) -> String + Send + Sync + 'static,
    ) -> Self {
        self.url_override = Some(Arc::new(resolve));
        self
    }

    /// Trust a verified stamp instead of re-hashing an installed library on every process
    /// start. Off by default.
    ///
    /// After the first full check a stamp (archive sha, size, mtime in nanoseconds, and on Unix
    /// device, inode and ctime) is written next to the object. A later process that finds the
    /// library still matching the stamp skips reading and hashing it; the manifest digest,
    /// identity and signature are still verified every time.
    ///
    /// Still caught: replacing the file (new inode), size changes, and edits that change the
    /// mtime or, on Unix, the ctime. Not caught: a same-size in-place edit that restores the
    /// mtime on Windows, or by an attacker who can forge Unix ctime. Anyone able to do that can
    /// already rewrite the cache (including the stamp), so the stamp is honoured only while the
    /// cache root and its object directory are owned by the current user and not writable by
    /// group or others (Unix); otherwise every verification hashes the library. The cache creates
    /// its directories `0700`.
    #[must_use]
    pub fn with_verified_stamp(mut self, enabled: bool) -> Self {
        self.verified_stamp = enabled;
        self
    }

    fn resolve_url(&self, entry: &ComponentLockEntry) -> String {
        match &self.url_override {
            Some(resolve) => resolve(entry),
            None => entry.url.clone(),
        }
    }

    /// Disable network downloads: [`Self::install`] fails a missing artifact with
    /// [`ComponentError::Offline`] instead of attempting a request, and a damaged installed
    /// artifact with [`ComponentError::CachedObjectInvalid`]. An artifact already verified on
    /// disk (or served via a `file://` URL) is unaffected.
    #[must_use]
    pub fn offline(mut self, offline: bool) -> Self {
        self.offline = offline;
        self
    }

    #[must_use]
    pub fn root(&self) -> &Path {
        &self.root
    }

    pub fn object_path(&self, entry: &ComponentLockEntry) -> Result<PathBuf, ComponentError> {
        Ok(self.root.join("sha256").join(canonical_entry(entry)?.sha256.as_str()))
    }

    /// Verify an installed object in place: checks it against the lock entry (skipping the
    /// library hash when a current verified stamp says it is unchanged) but never downloads or
    /// removes anything, and writes nothing but the verified stamp when the cache was built with
    /// [`Self::with_verified_stamp(true)`].
    ///
    /// Unlike [`Self::install`], a failing check is reported instead of repaired, and the error
    /// says which check failed (digest, size, manifest, signature) -- so a caller can tell
    /// "installed and damaged" apart from "never installed" and log or report a repair.
    pub fn verify_cached(&self, entry: &ComponentLockEntry) -> Result<CachedArtifact, ComponentError> {
        self.verify_cached_file(entry).map(|(cached, _)| cached)
    }

    /// [`Self::verify_cached`], also returning the open, verified library, which is what a caller
    /// that loads or executes the artifact must use (see [`VerifiedFile`]).
    pub(crate) fn verify_cached_file(
        &self,
        entry: &ComponentLockEntry,
    ) -> Result<(CachedArtifact, VerifiedFile), ComponentError> {
        let entry = canonical_entry(entry)?;
        let destination = self.object_path(&entry)?;
        self.verify_cached_at(&entry, &destination)
    }

    pub fn install(&self, entry: &ComponentLockEntry) -> Result<CachedArtifact, ComponentError> {
        self.install_verified(entry).map(|(cached, _)| cached)
    }

    /// [`Self::install`], also returning the open, verified library (see [`VerifiedFile`]).
    pub(crate) fn install_verified(
        &self,
        entry: &ComponentLockEntry,
    ) -> Result<(CachedArtifact, VerifiedFile), ComponentError> {
        let entry = canonical_entry(entry)?;
        let entry: &ComponentLockEntry = &entry;
        let destination = self.object_path(entry)?;
        let mut damaged = None;
        if destination.is_dir() {
            match self.verify_cached_at(entry, &destination) {
                Ok(verified) => {
                    self.touch_used(&entry.sha256);
                    return Ok(verified);
                }
                Err(error) => damaged = Some(error),
            }
        }
        let url = self.resolve_url(entry);
        if let Some(path) = local_file_url(&url) {
            return self.install_reader_verified(entry, File::open(path?)?);
        }
        if self.offline {
            return match damaged {
                Some(source) => Err(ComponentError::CachedObjectInvalid {
                    component: entry.identity.component.clone(),
                    source: Box::new(source),
                }),
                None => Err(ComponentError::Offline {
                    component: entry.identity.component.clone(),
                    url,
                }),
            };
        }
        let attempts = self.download.max_attempts.max(1);
        let mut attempt = 1;
        loop {
            if self.download.is_cancelled() {
                return Err(ComponentError::Cancelled);
            }
            let (error, transient) = match self.download_once(entry, &url) {
                Ok(verified) => return Ok(verified),
                Err(failure) => failure,
            };
            if !transient || attempt >= attempts {
                return Err(error);
            }
            std::thread::sleep(self.download.backoff_delay(attempt));
            attempt += 1;
        }
    }

    /// One request-and-install attempt; the flag says whether a failure is worth retrying.
    fn download_once(
        &self,
        entry: &ComponentLockEntry,
        url: &str,
    ) -> Result<(CachedArtifact, VerifiedFile), (ComponentError, bool)> {
        let tls = self.download.tls_config(url).map_err(|error| (error, false))?;
        let response = ureq::get(url)
            .config()
            .tls_config(tls)
            .timeout_connect(Some(self.download.connect_timeout))
            .timeout_global(Some(self.download.total_timeout))
            .build()
            .header(
                "User-Agent",
                concat!("alef-component-runtime/", env!("CARGO_PKG_VERSION")),
            )
            .call()
            .map_err(|error| {
                let transient = is_transient_request_error(&error);
                (
                    ComponentError::Download {
                        url: url.to_owned(),
                        message: error.to_string(),
                    },
                    transient,
                )
            })?;
        let mut body = TrackedReader::new(response.into_body().into_reader());
        let result = self.install_reader_verified(entry, &mut body);
        result.map_err(|error| {
            let transient = body.read_failed && matches!(error, ComponentError::Io(_));
            (error, transient)
        })
    }

    /// Install an archive from `reader`, honouring this cache's progress and cancellation
    /// settings (see [`DownloadOptions`]). Never retries: the reader cannot be rewound.
    pub fn install_from_reader(
        &self,
        entry: &ComponentLockEntry,
        reader: impl Read,
    ) -> Result<CachedArtifact, ComponentError> {
        self.install_reader_verified(entry, reader).map(|(cached, _)| cached)
    }

    fn install_reader_verified(
        &self,
        entry: &ComponentLockEntry,
        reader: impl Read,
    ) -> Result<(CachedArtifact, VerifiedFile), ComponentError> {
        let entry = canonical_entry(entry)?;
        let verified = self.install_stream(&entry, reader)?;
        self.touch_used(&entry.sha256);
        Ok(verified)
    }

    fn install_stream(
        &self,
        entry: &ComponentLockEntry,
        mut reader: impl Read,
    ) -> Result<(CachedArtifact, VerifiedFile), ComponentError> {
        let expected = decode_digest(&entry.sha256)?;
        let objects = self.root.join("sha256");
        create_private_dir_all(&objects)?;
        let lock = self.digest_lock_file(&entry.sha256)?;
        lock.lock()?;

        let destination = self.object_path(entry)?;
        if destination.is_dir()
            && let Ok(verified) = self.verify_cached_at(entry, &destination)
        {
            return Ok(verified);
        }

        let mut archive = tempfile::NamedTempFile::new_in(&objects)?;
        let mut hasher = Sha256::new();
        let mut copied = 0_u64;
        let mut buffer = [0_u8; 64 * 1024];
        loop {
            if self.download.is_cancelled() {
                return Err(ComponentError::Cancelled);
            }
            let count = reader.read(&mut buffer)?;
            if count == 0 {
                break;
            }
            archive.write_all(&buffer[..count])?;
            hasher.update(&buffer[..count]);
            copied += count as u64;
            if copied > entry.size {
                return Err(ComponentError::SizeMismatch {
                    expected: entry.size,
                    actual: copied,
                });
            }
            self.download.report(copied, entry.size);
        }
        if entry.size != copied {
            return Err(ComponentError::SizeMismatch {
                expected: entry.size,
                actual: copied,
            });
        }
        let actual: [u8; 32] = hasher.finalize().into();
        if actual != expected {
            return Err(ComponentError::DigestMismatch {
                expected: entry.sha256.clone(),
                actual: hex::encode(actual),
            });
        }

        archive.as_file_mut().seek(SeekFrom::Start(0))?;
        let staging = tempfile::Builder::new().prefix(".install-").tempdir_in(&objects)?;
        extract_safely(archive.as_file_mut(), staging.path())?;
        let (cached, verified) = self.verify_installation(entry, staging.path(), None)?;
        // Windows refuses to rename a directory while a file inside it is open, whatever share
        // mode the file was opened with (ERROR_ACCESS_DENIED), so the staging descriptor is closed
        // before the rename and the library is verified again at its installed path below.
        #[cfg(windows)]
        drop(verified);

        if destination.exists() {
            fs::remove_dir_all(&destination)?;
        }
        let _ = fs::remove_file(self.stamp_path(&entry.sha256));
        let staging_path = staging.keep();
        if let Err(error) = fs::rename(&staging_path, &destination) {
            let _ = fs::remove_dir_all(&staging_path);
            return Err(ComponentError::Io(error));
        }
        let library = destination.join(&cached.manifest.library.file);
        // A rename keeps the inode, so the descriptor verified in staging is the installed file.
        #[cfg(not(windows))]
        let verified = verified.relocated(library.clone());
        // The staging descriptor was closed for the rename, so the returned descriptor is a fresh
        // one, fully hashed (no stamp) under the digest lock this install still holds.
        #[cfg(windows)]
        let (_, verified) = self.verify_installation(entry, &destination, None)?;
        // Stamped from the verified descriptor, not the path: a file swapped in after the rename
        // must not inherit the stamp.
        if self.stamp_enabled()
            && let Ok(stamp) = verified
                .metadata()
                .and_then(|metadata| stamp::from_metadata(&entry.sha256, &metadata))
        {
            stamp::write(&self.stamp_path(&entry.sha256), &stamp);
        }
        Ok((
            CachedArtifact {
                root: destination.clone(),
                library,
                manifest: cached.manifest,
            },
            verified,
        ))
    }

    /// Open (creating it if needed) the per-digest lock file every install and removal of that
    /// object holds. Lock files are never deleted: unlinking one while another process waits on
    /// it would let a third process lock a fresh file at the same path, and two writers would
    /// then each believe they hold the object exclusively.
    pub(crate) fn digest_lock_file(&self, sha256: &str) -> std::io::Result<File> {
        OpenOptions::new()
            .read(true)
            .write(true)
            .create(true)
            .truncate(false)
            .open(self.objects_dir().join(format!("{sha256}.lock")))
    }

    pub(crate) fn stamp_path(&self, sha256: &str) -> PathBuf {
        self.root.join("sha256").join(format!("{sha256}.verified"))
    }

    /// Verify an object already in the cache, using the verified stamp when enabled.
    fn verify_cached_at(
        &self,
        entry: &ComponentLockEntry,
        root: &Path,
    ) -> Result<(CachedArtifact, VerifiedFile), ComponentError> {
        let stamp_path = self.stamp_path(&entry.sha256);
        self.verify_installation(entry, root, self.stamp_enabled().then_some(stamp_path.as_path()))
    }

    /// Whether the verified stamp may be read and written: it was requested, and only the
    /// current user can write the cache. A stamp in a directory others can write vouches for
    /// nothing, so such a cache is fully re-verified every time instead (ADR-0027).
    fn stamp_enabled(&self) -> bool {
        self.verified_stamp && is_private_dir(&self.root) && is_private_dir(&self.objects_dir())
    }

    fn verify_installation(
        &self,
        entry: &ComponentLockEntry,
        root: &Path,
        stamp_path: Option<&Path>,
    ) -> Result<(CachedArtifact, VerifiedFile), ComponentError> {
        let manifest_bytes = fs::read(root.join("component.json"))?;
        if sha256(&manifest_bytes) != decode_digest(&entry.manifest_sha256)? {
            return Err(ComponentError::ManifestDigestMismatch);
        }
        let manifest: ComponentManifest = serde_json::from_slice(&manifest_bytes)?;
        if manifest.canonical_bytes()? != manifest_bytes {
            return Err(ComponentError::NonCanonicalManifest);
        }
        if manifest.schema_version != COMPONENT_MANIFEST_SCHEMA || manifest.abi_version != COMPONENT_ABI_VERSION {
            return Err(ComponentError::UnsupportedManifest);
        }
        if manifest.identity != entry.identity {
            return Err(ComponentError::ManifestIdentityMismatch);
        }
        let library_relative = safe_relative_path(&manifest.library.file)?;
        let library = root.join(&library_relative);
        let missing = |error: std::io::Error| {
            if error.kind() == std::io::ErrorKind::NotFound {
                ComponentError::MissingLibrary(library.clone())
            } else {
                ComponentError::Io(error)
            }
        };
        // Every check below reads the library through this one descriptor, and the caller loads
        // or executes that same file (see `identity`), so swapping the path afterwards cannot
        // substitute unverified code.
        let file = File::open(&library).map_err(missing)?;
        let metadata = file.metadata()?;
        if metadata.len() != manifest.library.size {
            return Err(ComponentError::SizeMismatch {
                expected: manifest.library.size,
                actual: metadata.len(),
            });
        }
        let trusted = stamp_path.is_some_and(|path| stamp::matches(path, &entry.sha256, &metadata));
        if !trusted {
            let expected_library = decode_digest(&manifest.library.sha256)?;
            // Bounded one byte past the declared size, so a file that grew after the metadata
            // check is caught as a size mismatch rather than hashed without limit.
            let (actual_library, hashed) = sha256_reader((&file).take(manifest.library.size.saturating_add(1)))?;
            if hashed != manifest.library.size {
                return Err(ComponentError::SizeMismatch {
                    expected: manifest.library.size,
                    actual: hashed,
                });
            }
            if expected_library != actual_library {
                return Err(ComponentError::DigestMismatch {
                    expected: manifest.library.sha256.clone(),
                    actual: hex::encode(actual_library),
                });
            }
        }
        self.verify_manifest_signature(entry, root, &manifest_bytes)?;
        // Record the stamp only when the file did not change while it was being hashed.
        if let Some(path) = stamp_path
            && !trusted
            && file
                .metadata()
                .is_ok_and(|after| FileIdentity::of(&after) == FileIdentity::of(&metadata))
            && let Ok(stamp) = stamp::from_metadata(&entry.sha256, &metadata)
        {
            stamp::write(path, &stamp);
        }
        let verified = VerifiedFile::new(library.clone(), file, &metadata);
        Ok((
            CachedArtifact {
                root: root.to_owned(),
                library,
                manifest,
            },
            verified,
        ))
    }

    fn verify_manifest_signature(
        &self,
        entry: &ComponentLockEntry,
        root: &Path,
        manifest_bytes: &[u8],
    ) -> Result<(), ComponentError> {
        let required = self.signatures == SignaturePolicy::Require;
        let TrustPolicy::EmbeddedKeys(keys) = &self.trust else {
            return if required {
                Err(ComponentError::SignatureRequired)
            } else {
                Ok(())
            };
        };
        if entry.key_id.is_empty() && keys.is_empty() {
            return if required {
                Err(ComponentError::SignatureRequired)
            } else {
                Ok(())
            };
        }
        let signature_bytes = fs::read(root.join("component.sig")).map_err(|error| {
            if error.kind() == std::io::ErrorKind::NotFound {
                ComponentError::SignatureRequired
            } else {
                ComponentError::Io(error)
            }
        })?;
        let signature: ComponentSignature = serde_json::from_slice(&signature_bytes)?;
        if signature.algorithm != "ed25519" {
            return Err(ComponentError::UnsupportedSignatureAlgorithm(signature.algorithm));
        }
        if signature.key_id != entry.key_id {
            return Err(ComponentError::SignatureKeyMismatch);
        }
        let encoded_key = keys
            .get(&signature.key_id)
            .ok_or_else(|| ComponentError::UnknownSignatureKey(signature.key_id.clone()))?;
        let key = decode_public_key(encoded_key)?;
        let raw_signature = base64::engine::general_purpose::STANDARD
            .decode(&signature.signature)
            .map_err(|_| ComponentError::InvalidSignatureEncoding)?;
        let signature = Signature::from_slice(&raw_signature).map_err(|_| ComponentError::InvalidSignatureEncoding)?;
        // `verify_strict`, as the CLI uses: plain `verify` accepts signatures made with a
        // small-order public key, which match every message.
        key.verify_strict(manifest_bytes, &signature)
            .map_err(|_| ComponentError::SignatureVerification)
    }
}

/// Create `path` and any missing parents readable and writable only by the current user. Existing
/// directories keep their permissions; [`is_private_dir`] decides what the cache trusts in them.
fn create_private_dir_all(path: &Path) -> std::io::Result<()> {
    #[cfg(unix)]
    {
        use std::os::unix::fs::DirBuilderExt as _;
        fs::DirBuilder::new().recursive(true).mode(0o700).create(path)
    }
    #[cfg(not(unix))]
    {
        fs::create_dir_all(path)
    }
}

/// Whether `path` is a directory owned by the current user that neither its group nor others can
/// write. On non-Unix platforms permissions are ACLs this does not inspect, so it trusts the
/// per-user cache location (`%LOCALAPPDATA%`) the host chose.
pub(crate) fn is_private_dir(path: &Path) -> bool {
    #[cfg(unix)]
    {
        use std::os::unix::fs::MetadataExt as _;
        // SAFETY: `geteuid` takes no arguments, cannot fail and touches no memory.
        let user = unsafe { libc::geteuid() };
        fs::metadata(path)
            .is_ok_and(|metadata| metadata.is_dir() && metadata.uid() == user && metadata.mode() & 0o022 == 0)
    }
    #[cfg(not(unix))]
    {
        fs::metadata(path).is_ok_and(|metadata| metadata.is_dir())
    }
}

/// `entry` with its archive digest in lower case. Hex is case-insensitive, and the digest names the
/// object's directory, lock and stamp, so one object must have one spelling: an upper-case lock
/// entry would otherwise install a second copy beside the first, under a different lock.
pub(crate) fn canonical_entry(entry: &ComponentLockEntry) -> Result<Cow<'_, ComponentLockEntry>, ComponentError> {
    decode_digest(&entry.sha256)?;
    if entry.sha256.bytes().any(|byte| byte.is_ascii_uppercase()) {
        let mut owned = entry.clone();
        owned.sha256.make_ascii_lowercase();
        Ok(Cow::Owned(owned))
    } else {
        Ok(Cow::Borrowed(entry))
    }
}

/// The lower-case spelling of a hex digest, after checking it is one.
pub(crate) fn canonical_digest(value: &str) -> Result<String, ComponentError> {
    decode_digest(value)?;
    Ok(value.to_ascii_lowercase())
}

pub(crate) fn decode_digest(value: &str) -> Result<[u8; 32], ComponentError> {
    let bytes = hex::decode(value).map_err(|_| ComponentError::InvalidDigest(value.to_owned()))?;
    bytes
        .try_into()
        .map_err(|_| ComponentError::InvalidDigest(value.to_owned()))
}

fn sha256(bytes: &[u8]) -> [u8; 32] {
    Sha256::digest(bytes).into()
}

/// Size of the read buffer used to hash installed libraries.
const HASH_CHUNK: usize = 64 * 1024;

/// SHA-256 of everything `reader` yields, read in [`HASH_CHUNK`] pieces so a large library is
/// never held in memory whole. Returns the digest and the number of bytes hashed.
fn sha256_reader(mut reader: impl Read) -> std::io::Result<([u8; 32], u64)> {
    let mut hasher = Sha256::new();
    let mut buffer = vec![0_u8; HASH_CHUNK];
    let mut total = 0_u64;
    loop {
        let count = match reader.read(&mut buffer) {
            Ok(0) => break,
            Ok(count) => count,
            Err(error) if error.kind() == std::io::ErrorKind::Interrupted => continue,
            Err(error) => return Err(error),
        };
        hasher.update(&buffer[..count]);
        total += count as u64;
    }
    Ok((hasher.finalize().into(), total))
}

/// Names the archive's own metadata files occupy; a library under either would overwrite one
/// during extraction. Compared case-insensitively for case-insensitive file systems.
const RESERVED_LIBRARY_NAMES: [&str; 2] = ["component.json", "component.sig"];

fn safe_relative_path(value: &str) -> Result<PathBuf, ComponentError> {
    let path = Path::new(value);
    let safe = !path.as_os_str().is_empty()
        && path
            .components()
            .all(|component| matches!(component, Component::Normal(_)))
        && !RESERVED_LIBRARY_NAMES
            .iter()
            .any(|reserved| value.eq_ignore_ascii_case(reserved));
    if safe {
        Ok(path.to_owned())
    } else {
        Err(ComponentError::UnsafeLibraryPath(value.to_owned()))
    }
}

/// Generous ceiling for the manifest and signature entries: both are small JSON documents,
/// orders of magnitude smaller than any native component library, so a declared size above
/// this already means the archive is not one `alef component package` produced.
const METADATA_ENTRY_SIZE_CEILING: u64 = 64 * 1024;
/// Headroom added to the manifest's declared library size before an oversized decompressed
/// library entry is rejected, covering tar block padding around the real content.
const LIBRARY_SIZE_HEADROOM: u64 = 4096;
/// `alef component package` always writes at most three archive entries: the manifest, an
/// optional signature, and the library. Anything else is not an archive this loader made.
const MAX_ARCHIVE_ENTRIES: usize = 3;

/// Extract a downloaded component archive, bounding how much a corrupted or malicious
/// archive can force this to decompress, and rejecting anything that is not exactly the
/// shape `alef component package` produces.
///
/// The manifest must be the first entry (`component.json`) because its declared
/// `library.size` is what bounds the decompressed size of the library entry that follows --
/// without reading it first there is nothing to size the library entry's ceiling against,
/// and an attacker-supplied archive could otherwise reorder entries to smuggle an oversized
/// library past a check performed too late. Every entry must be a regular file at a safe
/// relative path (no absolute paths, `..`, or symlinks/hardlinks), and the total entry count
/// is capped so a maliciously padded archive cannot force unbounded header parsing either.
fn extract_safely(reader: impl Read, destination: &Path) -> Result<(), ComponentError> {
    let decoder = GzDecoder::new(reader);
    let mut archive = tar::Archive::new(decoder);
    let mut entries = archive.entries()?;

    let mut first = entries
        .next()
        .ok_or_else(|| ComponentError::UnsafeArchiveEntry("archive is empty".to_owned()))??;
    let first_path = first.path()?.into_owned();
    if first_path != Path::new("component.json") || !first.header().entry_type().is_file() {
        return Err(ComponentError::UnsafeArchiveEntry(first_path.display().to_string()));
    }
    let manifest_bytes = read_bounded_entry(&mut first, METADATA_ENTRY_SIZE_CEILING)?;
    fs::write(destination.join("component.json"), &manifest_bytes)?;
    let manifest: ComponentManifest = serde_json::from_slice(&manifest_bytes)?;
    let library_relative = safe_relative_path(&manifest.library.file)?;
    let library_ceiling = manifest.library.size.saturating_add(LIBRARY_SIZE_HEADROOM);

    let mut seen_library = false;
    let mut entry_count = 1_usize;
    for entry in entries {
        entry_count += 1;
        if entry_count > MAX_ARCHIVE_ENTRIES {
            return Err(ComponentError::TooManyArchiveEntries {
                max: MAX_ARCHIVE_ENTRIES,
                actual: entry_count,
            });
        }
        let mut entry = entry?;
        let path = entry.path()?.into_owned();
        let is_safe_relative = !path.as_os_str().is_empty()
            && path
                .components()
                .all(|component| matches!(component, Component::Normal(_)));
        if !is_safe_relative || !entry.header().entry_type().is_file() {
            return Err(ComponentError::UnsafeArchiveEntry(path.display().to_string()));
        }
        if path == Path::new("component.sig") {
            let bytes = read_bounded_entry(&mut entry, METADATA_ENTRY_SIZE_CEILING)?;
            fs::write(destination.join("component.sig"), bytes)?;
        } else if path == library_relative && !seen_library {
            seen_library = true;
            stream_library_entry(&mut entry, destination, &library_relative, library_ceiling, &manifest)?;
        } else {
            return Err(ComponentError::UnsafeArchiveEntry(path.display().to_string()));
        }
    }
    if !seen_library {
        return Err(ComponentError::MissingLibrary(destination.join(&library_relative)));
    }
    Ok(())
}

/// Read a small, fully-buffered entry (the manifest or its signature), rejecting it before
/// any bytes are copied if its declared size already exceeds `ceiling`.
fn read_bounded_entry<R: Read>(entry: &mut tar::Entry<'_, R>, ceiling: u64) -> Result<Vec<u8>, ComponentError> {
    let declared = entry.header().size()?;
    if declared > ceiling {
        return Err(ComponentError::ExtractedSizeExceeded {
            limit: ceiling,
            actual: declared,
        });
    }
    let mut buffer = Vec::new();
    entry.read_to_end(&mut buffer)?;
    Ok(buffer)
}

/// Stream the library entry straight to disk in fixed-size chunks -- rather than buffering
/// the whole (potentially large) decompressed library in memory -- hashing it as it is
/// written so the digest is verified in the same pass instead of a second read from disk.
fn stream_library_entry<R: Read>(
    entry: &mut tar::Entry<'_, R>,
    destination: &Path,
    relative: &Path,
    ceiling: u64,
    manifest: &ComponentManifest,
) -> Result<(), ComponentError> {
    let declared = entry.header().size()?;
    if declared > ceiling {
        return Err(ComponentError::ExtractedSizeExceeded {
            limit: ceiling,
            actual: declared,
        });
    }
    let target = destination.join(relative);
    if let Some(parent) = target.parent() {
        fs::create_dir_all(parent)?;
    }
    let mut file = File::create(&target)?;
    let mut hasher = Sha256::new();
    let mut buffer = [0_u8; 64 * 1024];
    let mut copied = 0_u64;
    loop {
        let count = entry.read(&mut buffer)?;
        if count == 0 {
            break;
        }
        copied += count as u64;
        if copied > ceiling {
            return Err(ComponentError::ExtractedSizeExceeded {
                limit: ceiling,
                actual: copied,
            });
        }
        hasher.update(&buffer[..count]);
        file.write_all(&buffer[..count])?;
    }
    if copied != manifest.library.size {
        return Err(ComponentError::SizeMismatch {
            expected: manifest.library.size,
            actual: copied,
        });
    }
    let actual: [u8; 32] = hasher.finalize().into();
    let expected = decode_digest(&manifest.library.sha256)?;
    if actual != expected {
        return Err(ComponentError::DigestMismatch {
            expected: manifest.library.sha256.clone(),
            actual: hex::encode(actual),
        });
    }
    Ok(())
}

#[cfg(test)]
mod archive_tests;
#[cfg(test)]
mod signature_tests;
#[cfg(test)]
mod tests;