|
| 1 | +use std::task::ready; |
| 2 | + |
| 3 | +use tracing::debug; |
| 4 | + |
| 5 | +use crate::sources::IndexSummary; |
| 6 | + |
| 7 | +use super::source::{MaybePackage, Source}; |
| 8 | + |
| 9 | +/// A `Source` that overlays one source over another, pretending that the packages |
| 10 | +/// available in the overlay are actually available in the other one. |
| 11 | +/// |
| 12 | +/// This is a massive footgun and a terrible idea, so we do not (and never will) |
| 13 | +/// expose this publicly. However, it is useful for some very specific private |
| 14 | +/// things, like locally verifying a bunch of packages at a time before any of |
| 15 | +/// them have been published. |
| 16 | +pub struct DependencyConfusionThreatOverlaySource<'gctx> { |
| 17 | + // The overlay source. The naming here comes from the main application of this, |
| 18 | + // where there is a remote registry that we overlay some local packages on. |
| 19 | + local: Box<dyn Source + 'gctx>, |
| 20 | + // The source we're impersonating. |
| 21 | + remote: Box<dyn Source + 'gctx>, |
| 22 | +} |
| 23 | + |
| 24 | +impl<'gctx> DependencyConfusionThreatOverlaySource<'gctx> { |
| 25 | + pub fn new(local: Box<dyn Source + 'gctx>, remote: Box<dyn Source + 'gctx>) -> Self { |
| 26 | + debug!( |
| 27 | + "overlaying {} on {}", |
| 28 | + local.source_id().as_url(), |
| 29 | + remote.source_id().as_url() |
| 30 | + ); |
| 31 | + Self { local, remote } |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +impl<'gctx> Source for DependencyConfusionThreatOverlaySource<'gctx> { |
| 36 | + fn source_id(&self) -> crate::core::SourceId { |
| 37 | + self.remote.source_id() |
| 38 | + } |
| 39 | + |
| 40 | + fn supports_checksums(&self) -> bool { |
| 41 | + self.local.supports_checksums() && self.remote.supports_checksums() |
| 42 | + } |
| 43 | + |
| 44 | + fn requires_precise(&self) -> bool { |
| 45 | + self.local.requires_precise() || self.remote.requires_precise() |
| 46 | + } |
| 47 | + |
| 48 | + fn query( |
| 49 | + &mut self, |
| 50 | + dep: &crate::core::Dependency, |
| 51 | + kind: super::source::QueryKind, |
| 52 | + f: &mut dyn FnMut(super::IndexSummary), |
| 53 | + ) -> std::task::Poll<crate::CargoResult<()>> { |
| 54 | + let local_source = self.local.source_id(); |
| 55 | + let remote_source = self.remote.source_id(); |
| 56 | + |
| 57 | + let local_dep = dep.clone().map_source(remote_source, local_source); |
| 58 | + let mut local_packages = std::collections::HashSet::new(); |
| 59 | + let mut local_callback = |index: IndexSummary| { |
| 60 | + let index = index.map_summary(|s| s.map_source(local_source, remote_source)); |
| 61 | + local_packages.insert(index.as_summary().clone()); |
| 62 | + f(index) |
| 63 | + }; |
| 64 | + ready!(self.local.query(&local_dep, kind, &mut local_callback))?; |
| 65 | + |
| 66 | + let mut package_collision = None; |
| 67 | + let mut remote_callback = |index: IndexSummary| { |
| 68 | + if local_packages.contains(index.as_summary()) { |
| 69 | + package_collision = Some(index.as_summary().clone()); |
| 70 | + } |
| 71 | + f(index) |
| 72 | + }; |
| 73 | + ready!(self.remote.query(dep, kind, &mut remote_callback))?; |
| 74 | + |
| 75 | + if let Some(collision) = package_collision { |
| 76 | + std::task::Poll::Ready(Err(anyhow::anyhow!( |
| 77 | + "found a package in the remote registry and the local overlay: {}@{}", |
| 78 | + collision.name(), |
| 79 | + collision.version() |
| 80 | + ))) |
| 81 | + } else { |
| 82 | + std::task::Poll::Ready(Ok(())) |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + fn invalidate_cache(&mut self) { |
| 87 | + self.local.invalidate_cache(); |
| 88 | + self.remote.invalidate_cache(); |
| 89 | + } |
| 90 | + |
| 91 | + fn set_quiet(&mut self, quiet: bool) { |
| 92 | + self.local.set_quiet(quiet); |
| 93 | + self.remote.set_quiet(quiet); |
| 94 | + } |
| 95 | + |
| 96 | + fn download( |
| 97 | + &mut self, |
| 98 | + package: crate::core::PackageId, |
| 99 | + ) -> crate::CargoResult<super::source::MaybePackage> { |
| 100 | + let local_source = self.local.source_id(); |
| 101 | + let remote_source = self.remote.source_id(); |
| 102 | + |
| 103 | + self.local |
| 104 | + .download(package.map_source(remote_source, local_source)) |
| 105 | + .map(|maybe_pkg| match maybe_pkg { |
| 106 | + MaybePackage::Ready(pkg) => { |
| 107 | + MaybePackage::Ready(pkg.map_source(local_source, remote_source)) |
| 108 | + } |
| 109 | + x => x, |
| 110 | + }) |
| 111 | + .or_else(|_| self.remote.download(package)) |
| 112 | + } |
| 113 | + |
| 114 | + fn finish_download( |
| 115 | + &mut self, |
| 116 | + pkg_id: crate::core::PackageId, |
| 117 | + contents: Vec<u8>, |
| 118 | + ) -> crate::CargoResult<crate::core::Package> { |
| 119 | + // The local registry should never return MaybePackage::Download from `download`, so any |
| 120 | + // downloads that need to be finished come from the remote registry. |
| 121 | + self.remote.finish_download(pkg_id, contents) |
| 122 | + } |
| 123 | + |
| 124 | + fn fingerprint(&self, pkg: &crate::core::Package) -> crate::CargoResult<String> { |
| 125 | + Ok(pkg.package_id().version().to_string()) |
| 126 | + } |
| 127 | + |
| 128 | + fn describe(&self) -> String { |
| 129 | + self.remote.describe() |
| 130 | + } |
| 131 | + |
| 132 | + fn add_to_yanked_whitelist(&mut self, pkgs: &[crate::core::PackageId]) { |
| 133 | + self.local.add_to_yanked_whitelist(pkgs); |
| 134 | + self.remote.add_to_yanked_whitelist(pkgs); |
| 135 | + } |
| 136 | + |
| 137 | + fn is_yanked( |
| 138 | + &mut self, |
| 139 | + pkg: crate::core::PackageId, |
| 140 | + ) -> std::task::Poll<crate::CargoResult<bool>> { |
| 141 | + self.remote.is_yanked(pkg) |
| 142 | + } |
| 143 | + |
| 144 | + fn block_until_ready(&mut self) -> crate::CargoResult<()> { |
| 145 | + self.local.block_until_ready()?; |
| 146 | + self.remote.block_until_ready() |
| 147 | + } |
| 148 | +} |
0 commit comments