cargo/sources/registry/mod.rs
1//! A `Source` for registry-based packages.
2//!
3//! # What's a Registry?
4//!
5//! [Registries] are central locations where packages can be uploaded to,
6//! discovered, and searched for. The purpose of a registry is to have a
7//! location that serves as permanent storage for versions of a crate over time.
8//!
9//! Compared to git sources (see [`GitSource`]), a registry provides many
10//! packages as well as many versions simultaneously. Git sources can also
11//! have commits deleted through rebasings where registries cannot have their
12//! versions deleted.
13//!
14//! In Cargo, [`RegistryData`] is an abstraction over each kind of actual
15//! registry, and [`RegistrySource`] connects those implementations to
16//! [`Source`] trait. Two prominent features these abstractions provide are
17//!
18//! * A way to query the metadata of a package from a registry. The metadata
19//! comes from the index.
20//! * A way to download package contents (a.k.a source files) that are required
21//! when building the package itself.
22//!
23//! We'll cover each functionality later.
24//!
25//! [Registries]: https://doc.rust-lang.org/nightly/cargo/reference/registries.html
26//! [`GitSource`]: super::GitSource
27//!
28//! # Different Kinds of Registries
29//!
30//! Cargo provides multiple kinds of registries. Each of them serves the index
31//! and package contents in a slightly different way. Namely,
32//!
33//! * [`LocalRegistry`] --- Serves the index and package contents entirely on
34//! a local filesystem.
35//! * [`RemoteRegistry`] --- Serves the index ahead of time from a Git
36//! repository, and package contents are downloaded as needed.
37//! * [`HttpRegistry`] --- Serves both the index and package contents on demand
38//! over a HTTP-based registry API. This is the default starting from 1.70.0.
39//!
40//! Each registry has its own [`RegistryData`] implementation, and can be
41//! created from either [`RegistrySource::local`] or [`RegistrySource::remote`].
42//!
43//! [`LocalRegistry`]: local::LocalRegistry
44//! [`RemoteRegistry`]: remote::RemoteRegistry
45//! [`HttpRegistry`]: http_remote::HttpRegistry
46//!
47//! # The Index of a Registry
48//!
49//! One of the major difficulties with a registry is that hosting so many
50//! packages may quickly run into performance problems when dealing with
51//! dependency graphs. It's infeasible for cargo to download the entire contents
52//! of the registry just to resolve one package's dependencies, for example. As
53//! a result, cargo needs some efficient method of querying what packages are
54//! available on a registry, what versions are available, and what the
55//! dependencies for each version is.
56//!
57//! To solve the problem, a registry must provide an index of package metadata.
58//! The index of a registry is essentially an easily query-able version of the
59//! registry's database for a list of versions of a package as well as a list
60//! of dependencies for each version. The exact format of the index is
61//! described later.
62//!
63//! See the [`index`] module for topics about the management, parsing, caching,
64//! and versioning for the on-disk index.
65//!
66//! ## The Format of The Index
67//!
68//! The index is a store for the list of versions for all packages known, so its
69//! format on disk is optimized slightly to ensure that `ls registry` doesn't
70//! produce a list of all packages ever known. The index also wants to ensure
71//! that there's not a million files which may actually end up hitting
72//! filesystem limits at some point. To this end, a few decisions were made
73//! about the format of the registry:
74//!
75//! 1. Each crate will have one file corresponding to it. Each version for a
76//! crate will just be a line in this file (see [`cargo_util_schemas::index::IndexPackage`] for its
77//! representation).
78//! 2. There will be two tiers of directories for crate names, under which
79//! crates corresponding to those tiers will be located.
80//! (See [`cargo_util::registry::make_dep_path`] for the implementation of
81//! this layout hierarchy.)
82//!
83//! As an example, this is an example hierarchy of an index:
84//!
85//! ```notrust
86//! .
87//! ├── 3
88//! │ └── u
89//! │ └── url
90//! ├── bz
91//! │ └── ip
92//! │ └── bzip2
93//! ├── config.json
94//! ├── en
95//! │ └── co
96//! │ └── encoding
97//! └── li
98//! ├── bg
99//! │ └── libgit2
100//! └── nk
101//! └── link-config
102//! ```
103//!
104//! The root of the index contains a `config.json` file with a few entries
105//! corresponding to the registry (see [`RegistryConfig`] below).
106//!
107//! Otherwise, there are three numbered directories (1, 2, 3) for crates with
108//! names 1, 2, and 3 characters in length. The 1/2 directories simply have the
109//! crate files underneath them, while the 3 directory is sharded by the first
110//! letter of the crate name.
111//!
112//! Otherwise the top-level directory contains many two-letter directory names,
113//! each of which has many sub-folders with two letters. At the end of all these
114//! are the actual crate files themselves.
115//!
116//! The purpose of this layout is to hopefully cut down on `ls` sizes as well as
117//! efficient lookup based on the crate name itself.
118//!
119//! See [The Cargo Book: Registry Index][registry-index] for the public
120//! interface on the index format.
121//!
122//! [registry-index]: https://doc.rust-lang.org/nightly/cargo/reference/registry-index.html
123//!
124//! ## The Index Files
125//!
126//! Each file in the index is the history of one crate over time. Each line in
127//! the file corresponds to one version of a crate, stored in JSON format (see
128//! the [`cargo_util_schemas::index::IndexPackage`] structure).
129//!
130//! As new versions are published, new lines are appended to this file. **The
131//! only modifications to this file that should happen over time are yanks of a
132//! particular version.**
133//!
134//! # Downloading Packages
135//!
136//! The purpose of the index was to provide an efficient method to resolve the
137//! dependency graph for a package. After resolution has been performed, we need
138//! to download the contents of packages so we can read the full manifest and
139//! build the source code.
140//!
141//! To accomplish this, [`RegistryData::download`] will "make" an HTTP request
142//! per-package requested to download tarballs into a local cache. These
143//! tarballs will then be unpacked into a destination folder.
144//!
145//! Note that because versions uploaded to the registry are frozen forever that
146//! the HTTP download and unpacking can all be skipped if the version has
147//! already been downloaded and unpacked. This caching allows us to only
148//! download a package when absolutely necessary.
149//!
150//! # Filesystem Hierarchy
151//!
152//! Overall, the `$HOME/.cargo` looks like this when talking about the registry
153//! (remote registries, specifically):
154//!
155//! ```notrust
156//! # A folder under which all registry metadata is hosted (similar to
157//! # $HOME/.cargo/git)
158//! $HOME/.cargo/registry/
159//!
160//! # For each registry that cargo knows about (keyed by hostname + hash)
161//! # there is a folder which is the checked out version of the index for
162//! # the registry in this location. Note that this is done so cargo can
163//! # support multiple registries simultaneously
164//! index/
165//! registry1-<hash>/
166//! registry2-<hash>/
167//! ...
168//!
169//! # This folder is a cache for all downloaded tarballs (`.crate` file)
170//! # from a registry. Once downloaded and verified, a tarball never changes.
171//! cache/
172//! registry1-<hash>/<pkg>-<version>.crate
173//! ...
174//!
175//! # Location in which all tarballs are unpacked. Each tarball is known to
176//! # be frozen after downloading, so transitively this folder is also
177//! # frozen once its unpacked (it's never unpacked again)
178//! # CAVEAT: They are not read-only. See rust-lang/cargo#9455.
179//! src/
180//! registry1-<hash>/<pkg>-<version>/...
181//! ...
182//! ```
183//!
184
185use std::cell::RefCell;
186use std::collections::HashSet;
187use std::fs;
188use std::fs::{File, OpenOptions};
189use std::io;
190use std::io::Read;
191use std::io::Write;
192use std::path::{Path, PathBuf};
193
194use anyhow::Context as _;
195use cargo_util::paths;
196use cargo_util_terminal::report::Level;
197use flate2::read::GzDecoder;
198use futures::FutureExt as _;
199use serde::Deserialize;
200use serde::Serialize;
201use tar::Archive;
202use tracing::debug;
203
204use crate::core::dependency::Dependency;
205use crate::core::global_cache_tracker;
206use crate::core::{Package, PackageId, SourceId};
207use crate::sources::PathSource;
208use crate::sources::source::MaybePackage;
209use crate::sources::source::QueryKind;
210use crate::sources::source::Source;
211use crate::util::cache_lock::CacheLockMode;
212use crate::util::interning::InternedString;
213use crate::util::{CargoResult, Filesystem, GlobalContext, LimitErrorReader, restricted_names};
214use crate::util::{VersionExt, hex};
215
216/// The `.cargo-ok` file is used to track if the source is already unpacked.
217/// See [`RegistrySource::unpack_package`] for more.
218///
219/// Not to be confused with `.cargo-ok` file in git sources.
220const PACKAGE_SOURCE_LOCK: &str = ".cargo-ok";
221
222pub const CRATES_IO_INDEX: &str = "https://github.com/rust-lang/crates.io-index";
223pub const CRATES_IO_HTTP_INDEX: &str = "sparse+https://index.crates.io/";
224pub const CRATES_IO_REGISTRY: &str = "crates-io";
225pub const CRATES_IO_DOMAIN: &str = "crates.io";
226
227/// The content inside `.cargo-ok`.
228/// See [`RegistrySource::unpack_package`] for more.
229#[derive(Deserialize, Serialize)]
230#[serde(rename_all = "kebab-case")]
231struct LockMetadata {
232 /// The version of `.cargo-ok` file
233 v: u32,
234}
235
236/// A [`Source`] implementation for a local or a remote registry.
237///
238/// This contains common functionality that is shared between each registry
239/// kind, with the registry-specific logic implemented as part of the
240/// [`RegistryData`] trait referenced via the `ops` field.
241///
242/// For general concepts of registries, see the [module-level documentation](crate::sources::registry).
243pub struct RegistrySource<'gctx> {
244 /// A unique name of the source (typically used as the directory name
245 /// where its cached content is stored).
246 name: InternedString,
247 /// The unique identifier of this source.
248 source_id: SourceId,
249 /// The path where crate files are extracted (`$CARGO_HOME/registry/src/$REG-HASH`).
250 src_path: Filesystem,
251 /// Local reference to [`GlobalContext`] for convenience.
252 gctx: &'gctx GlobalContext,
253 /// Abstraction for interfacing to the different registry kinds.
254 ops: Box<dyn RegistryData + 'gctx>,
255 /// Interface for managing the on-disk index.
256 index: index::RegistryIndex<'gctx>,
257 /// A set of packages that should be allowed to be used, even if they are
258 /// yanked.
259 ///
260 /// This is populated from the entries in `Cargo.lock` to ensure that
261 /// `cargo update somepkg` won't unlock yanked entries in `Cargo.lock`.
262 /// Otherwise, the resolver would think that those entries no longer
263 /// exist, and it would trigger updates to unrelated packages.
264 yanked_whitelist: RefCell<HashSet<PackageId>>,
265 /// Yanked versions that have already been selected during queries.
266 ///
267 /// As of this writing, this is for not emitting the `--precise <yanked>`
268 /// warning twice, with the assumption of (`dep.package_name()` + `--precise`
269 /// version) being sufficient to uniquely identify the same query result.
270 selected_precise_yanked: RefCell<HashSet<(InternedString, semver::Version)>>,
271}
272
273/// The [`config.json`] file stored in the index.
274///
275/// The config file may look like:
276///
277/// ```json
278/// {
279/// "dl": "https://example.com/api/{crate}/{version}/download",
280/// "api": "https://example.com/api",
281/// "auth-required": false # unstable feature (RFC 3139)
282/// }
283/// ```
284///
285/// [`config.json`]: https://doc.rust-lang.org/nightly/cargo/reference/registry-index.html#index-configuration
286#[derive(Deserialize, Debug, Clone)]
287#[serde(rename_all = "kebab-case")]
288pub struct RegistryConfig {
289 /// Download endpoint for all crates.
290 ///
291 /// The string is a template which will generate the download URL for the
292 /// tarball of a specific version of a crate. The substrings `{crate}` and
293 /// `{version}` will be replaced with the crate's name and version
294 /// respectively. The substring `{prefix}` will be replaced with the
295 /// crate's prefix directory name, and the substring `{lowerprefix}` will
296 /// be replaced with the crate's prefix directory name converted to
297 /// lowercase. The substring `{sha256-checksum}` will be replaced with the
298 /// crate's sha256 checksum.
299 ///
300 /// For backwards compatibility, if the string does not contain any
301 /// markers (`{crate}`, `{version}`, `{prefix}`, or `{lowerprefix}`), it
302 /// will be extended with `/{crate}/{version}/download` to
303 /// support registries like crates.io which were created before the
304 /// templating setup was created.
305 ///
306 /// For more on the template of the download URL, see [Index Configuration](
307 /// https://doc.rust-lang.org/nightly/cargo/reference/registry-index.html#index-configuration).
308 pub dl: String,
309
310 /// API endpoint for the registry. This is what's actually hit to perform
311 /// operations like yanks, owner modifications, publish new crates, etc.
312 /// If this is None, the registry does not support API commands.
313 pub api: Option<String>,
314
315 /// Whether all operations require authentication. See [RFC 3139].
316 ///
317 /// [RFC 3139]: https://rust-lang.github.io/rfcs/3139-cargo-alternative-registry-auth.html
318 #[serde(default)]
319 pub auth_required: bool,
320}
321
322/// Result from loading data from a registry.
323#[derive(Debug, Clone)]
324pub enum LoadResponse {
325 /// The cache is valid. The cached data should be used.
326 CacheValid,
327
328 /// The cache is out of date. Returned data should be used.
329 Data {
330 raw_data: Vec<u8>,
331 /// Version of this data to determine whether it is out of date.
332 index_version: Option<String>,
333 },
334
335 /// The requested crate was found.
336 NotFound,
337}
338
339/// An abstract interface to handle both a local and remote registry.
340///
341/// This allows [`RegistrySource`] to abstractly handle each registry kind.
342///
343/// For general concepts of registries, see the [module-level documentation](crate::sources::registry).
344#[async_trait::async_trait(?Send)]
345pub trait RegistryData {
346 /// Performs initialization for the registry.
347 ///
348 /// This should be safe to call multiple times, the implementation is
349 /// expected to not do any work if it is already prepared.
350 fn prepare(&self) -> CargoResult<()>;
351
352 /// Returns the path to the index.
353 ///
354 /// Note that different registries store the index in different formats
355 /// (remote = git, http & local = files).
356 fn index_path(&self) -> &Filesystem;
357
358 /// Returns the path of the directory that stores the cache of `.crate` files.
359 ///
360 /// The directory is currently expected to contain a flat list of all `.crate` files,
361 /// named `<package-name>-<version>.crate`.
362 fn cache_path(&self) -> &Filesystem;
363
364 /// Loads the JSON for a specific named package from the index.
365 ///
366 /// * `root` is the root path to the index.
367 /// * `path` is the relative path to the package to load (like `ca/rg/cargo`).
368 /// * `index_version` is the version of the requested crate data currently
369 /// in cache. This is useful for checking if a local cache is outdated.
370 async fn load(
371 &self,
372 root: &Path,
373 path: &Path,
374 index_version: Option<&str>,
375 ) -> CargoResult<LoadResponse>;
376
377 /// Loads the `config.json` file and returns it.
378 ///
379 /// Local registries don't have a config, and return `None`.
380 async fn config(&self) -> CargoResult<Option<RegistryConfig>>;
381
382 /// Invalidates locally cached data.
383 fn invalidate_cache(&self);
384
385 /// If quiet, the source should not display any progress or status messages.
386 fn set_quiet(&mut self, quiet: bool);
387
388 /// Is the local cached data up-to-date?
389 fn is_updated(&self) -> bool;
390
391 /// Prepare to start downloading a `.crate` file.
392 ///
393 /// Despite the name, this doesn't actually download anything. If the
394 /// `.crate` is already downloaded, then it returns [`MaybeLock::Ready`].
395 /// If it hasn't been downloaded, then it returns [`MaybeLock::Download`]
396 /// which contains the URL to download. The [`crate::core::package::Downloads`]
397 /// system handles the actual download process. After downloading, it
398 /// calls [`Self::finish_download`] to save the downloaded file.
399 ///
400 /// `checksum` is currently only used by local registries to verify the
401 /// file contents (because local registries never actually download
402 /// anything). Remote registries will validate the checksum in
403 /// `finish_download`. For already downloaded `.crate` files, it does not
404 /// validate the checksum, assuming the filesystem does not suffer from
405 /// corruption or manipulation.
406 fn download(&self, pkg: PackageId, checksum: &str) -> CargoResult<MaybeLock>;
407
408 /// Finish a download by saving a `.crate` file to disk.
409 ///
410 /// After [`crate::core::package::Downloads`] has finished a download,
411 /// it will call this to save the `.crate` file. This is only relevant
412 /// for remote registries. This should validate the checksum and save
413 /// the given data to the on-disk cache.
414 ///
415 /// Returns a [`File`] handle to the `.crate` file, positioned at the start.
416 fn finish_download(&self, pkg: PackageId, checksum: &str, data: &[u8]) -> CargoResult<File>;
417
418 /// Returns whether or not the `.crate` file is already downloaded.
419 fn is_crate_downloaded(&self, _pkg: PackageId) -> bool {
420 true
421 }
422
423 /// Validates that the global package cache lock is held.
424 ///
425 /// Given the [`Filesystem`], this will make sure that the package cache
426 /// lock is held. If not, it will panic. See
427 /// [`GlobalContext::acquire_package_cache_lock`] for acquiring the global lock.
428 ///
429 /// Returns the [`Path`] to the [`Filesystem`].
430 fn assert_index_locked<'a>(&self, path: &'a Filesystem) -> &'a Path;
431}
432
433/// The status of [`RegistryData::download`] which indicates if a `.crate`
434/// file has already been downloaded, or if not then the URL to download.
435pub enum MaybeLock {
436 /// The `.crate` file is already downloaded. [`File`] is a handle to the
437 /// opened `.crate` file on the filesystem.
438 Ready(File),
439 /// The `.crate` file is not downloaded, here's the URL to download it from.
440 ///
441 /// `descriptor` is just a text string to display to the user of what is
442 /// being downloaded.
443 Download {
444 url: String,
445 descriptor: String,
446 authorization: Option<String>,
447 },
448}
449
450mod download;
451mod http_remote;
452pub(crate) mod index;
453pub use index::IndexSummary;
454mod local;
455mod remote;
456
457/// Generates a unique name for [`SourceId`] to have a unique path to put their
458/// index files.
459fn short_name(id: SourceId, is_shallow: bool) -> String {
460 // CAUTION: This should not change between versions. If you change how
461 // this is computed, it will orphan previously cached data, forcing the
462 // cache to be rebuilt and potentially wasting significant disk space. If
463 // you change it, be cautious of the impact. See `test_cratesio_hash` for
464 // a similar discussion.
465 let hash = hex::short_hash(&id);
466 let ident = id.url().host_str().unwrap_or("").to_string();
467 let mut name = format!("{}-{}", ident, hash);
468 if is_shallow {
469 name.push_str("-shallow");
470 }
471 name
472}
473
474impl<'gctx> RegistrySource<'gctx> {
475 /// Creates a [`Source`] of a "remote" registry.
476 /// It could be either an HTTP-based [`http_remote::HttpRegistry`] or
477 /// a Git-based [`remote::RemoteRegistry`].
478 ///
479 /// * `yanked_whitelist` --- Packages allowed to be used, even if they are yanked.
480 pub fn remote(
481 source_id: SourceId,
482 yanked_whitelist: &HashSet<PackageId>,
483 gctx: &'gctx GlobalContext,
484 ) -> CargoResult<RegistrySource<'gctx>> {
485 assert!(source_id.is_remote_registry());
486 let name = short_name(
487 source_id,
488 gctx.cli_unstable()
489 .git
490 .map_or(false, |features| features.shallow_index)
491 && !source_id.is_sparse(),
492 );
493 let ops = if source_id.is_sparse() {
494 Box::new(http_remote::HttpRegistry::new(source_id, gctx, &name)?) as Box<_>
495 } else {
496 Box::new(remote::RemoteRegistry::new(source_id, gctx, &name)) as Box<_>
497 };
498
499 Ok(RegistrySource::new(
500 source_id,
501 gctx,
502 &name,
503 ops,
504 yanked_whitelist,
505 ))
506 }
507
508 /// Creates a [`Source`] of a local registry, with [`local::LocalRegistry`] under the hood.
509 ///
510 /// * `path` --- The root path of a local registry on the file system.
511 /// * `yanked_whitelist` --- Packages allowed to be used, even if they are yanked.
512 pub fn local(
513 source_id: SourceId,
514 path: &Path,
515 yanked_whitelist: &HashSet<PackageId>,
516 gctx: &'gctx GlobalContext,
517 ) -> RegistrySource<'gctx> {
518 let name = short_name(source_id, false);
519 let ops = local::LocalRegistry::new(path, gctx, &name);
520 RegistrySource::new(source_id, gctx, &name, Box::new(ops), yanked_whitelist)
521 }
522
523 /// Creates a source of a registry. This is a inner helper function.
524 ///
525 /// * `name` --- Name of a path segment which may affect where `.crate`
526 /// tarballs, the registry index and cache are stored. Expect to be unique.
527 /// * `ops` --- The underlying [`RegistryData`] type.
528 /// * `yanked_whitelist` --- Packages allowed to be used, even if they are yanked.
529 fn new(
530 source_id: SourceId,
531 gctx: &'gctx GlobalContext,
532 name: &str,
533 ops: Box<dyn RegistryData + 'gctx>,
534 yanked_whitelist: &HashSet<PackageId>,
535 ) -> RegistrySource<'gctx> {
536 // Before starting to work on the registry, make sure that
537 // `<cargo_home>/registry` is marked as excluded from indexing and
538 // backups. Older versions of Cargo didn't do this, so we do it here
539 // regardless of whether `<cargo_home>` exists.
540 //
541 // This does not use `create_dir_all_excluded_from_backups_atomic` for
542 // the same reason: we want to exclude it even if the directory already
543 // exists.
544 //
545 // IO errors in creating and marking it are ignored, e.g. in case we're on a
546 // read-only filesystem.
547 let registry_base = gctx.registry_base_path();
548 let _ = registry_base.create_dir();
549 cargo_util::paths::exclude_from_backups_and_indexing(®istry_base.into_path_unlocked());
550
551 RegistrySource {
552 name: name.into(),
553 src_path: gctx.registry_source_path().join(name),
554 gctx,
555 source_id,
556 index: index::RegistryIndex::new(source_id, ops.index_path(), gctx),
557 yanked_whitelist: RefCell::new(yanked_whitelist.clone()),
558 ops,
559 selected_precise_yanked: RefCell::new(HashSet::new()),
560 }
561 }
562
563 /// Decode the [configuration](RegistryConfig) stored within the registry.
564 ///
565 /// This requires that the index has been at least checked out.
566 pub async fn config(&self) -> CargoResult<Option<RegistryConfig>> {
567 self.ops.config().await
568 }
569
570 /// Unpacks a downloaded package into a location where it's ready to be
571 /// compiled.
572 ///
573 /// No action is taken if the source looks like it's already unpacked.
574 ///
575 /// # History of interruption detection with `.cargo-ok` file
576 ///
577 /// Cargo has always included a `.cargo-ok` file ([`PACKAGE_SOURCE_LOCK`])
578 /// to detect if extraction was interrupted, but it was originally empty.
579 ///
580 /// In 1.34, Cargo was changed to create the `.cargo-ok` file before it
581 /// started extraction to implement fine-grained locking. After it was
582 /// finished extracting, it wrote two bytes to indicate it was complete.
583 /// It would use the length check to detect if it was possibly interrupted.
584 ///
585 /// In 1.36, Cargo changed to not use fine-grained locking, and instead used
586 /// a global lock. The use of `.cargo-ok` was no longer needed for locking
587 /// purposes, but was kept to detect when extraction was interrupted.
588 ///
589 /// In 1.49, Cargo changed to not create the `.cargo-ok` file before it
590 /// started extraction to deal with `.crate` files that inexplicably had
591 /// a `.cargo-ok` file in them.
592 ///
593 /// In 1.64, Cargo changed to detect `.crate` files with `.cargo-ok` files
594 /// in them in response to [CVE-2022-36113], which dealt with malicious
595 /// `.crate` files making `.cargo-ok` a symlink causing cargo to write "ok"
596 /// to any arbitrary file on the filesystem it has permission to.
597 ///
598 /// In 1.71, `.cargo-ok` changed to contain a JSON `{ v: 1 }` to indicate
599 /// the version of it. A failure of parsing will result in a heavy-hammer
600 /// approach that unpacks the `.crate` file again. This is in response to a
601 /// security issue that the unpacking didn't respect umask on Unix systems.
602 ///
603 /// This is all a long-winded way of explaining the circumstances that might
604 /// cause a directory to contain a `.cargo-ok` file that is empty or
605 /// otherwise corrupted. Either this was extracted by a version of Rust
606 /// before 1.34, in which case everything should be fine. However, an empty
607 /// file created by versions 1.36 to 1.49 indicates that the extraction was
608 /// interrupted and that we need to start again.
609 ///
610 /// Another possibility is that the filesystem is simply corrupted, in
611 /// which case deleting the directory might be the safe thing to do. That
612 /// is probably unlikely, though.
613 ///
614 /// To be safe, we delete the directory and start over again if an empty
615 /// `.cargo-ok` file is found.
616 ///
617 /// [CVE-2022-36113]: https://blog.rust-lang.org/2022/09/14/cargo-cves.html#arbitrary-file-corruption-cve-2022-36113
618 fn unpack_package(&self, pkg: PackageId, tarball: &File) -> CargoResult<PathBuf> {
619 let package_dir = format!("{}-{}", pkg.name(), pkg.version());
620 let dst = self.src_path.join(&package_dir);
621 let path = dst.join(PACKAGE_SOURCE_LOCK);
622 let path = self
623 .gctx
624 .assert_package_cache_locked(CacheLockMode::DownloadExclusive, &path);
625 let unpack_dir = path.parent().unwrap();
626 match fs::read_to_string(path) {
627 Ok(ok) => match serde_json::from_str::<LockMetadata>(&ok) {
628 Ok(lock_meta) if lock_meta.v == 1 => {
629 self.gctx
630 .deferred_global_last_use()?
631 .mark_registry_src_used(global_cache_tracker::RegistrySrc {
632 encoded_registry_name: self.name,
633 package_dir: package_dir.into(),
634 size: None,
635 });
636 return Ok(unpack_dir.to_path_buf());
637 }
638 _ => {
639 if ok == "ok" {
640 tracing::debug!("old `ok` content found, clearing cache");
641 } else {
642 tracing::warn!("unrecognized .cargo-ok content, clearing cache: {ok}");
643 }
644 // See comment of `unpack_package` about why removing all stuff.
645 paths::remove_dir_all(dst.as_path_unlocked())?;
646 }
647 },
648 Err(e) if e.kind() == io::ErrorKind::NotFound => {}
649 Err(e) => anyhow::bail!("unable to read .cargo-ok file at {path:?}: {e}"),
650 }
651 dst.create_dir()?;
652
653 let bytes_written = unpack(self.gctx, tarball, unpack_dir, &|_| true)?;
654 update_mtime_for_generated_files(unpack_dir);
655
656 // Now that we've finished unpacking, create and write to the lock file to indicate that
657 // unpacking was successful.
658 let mut ok = OpenOptions::new()
659 .create_new(true)
660 .read(true)
661 .write(true)
662 .open(&path)
663 .with_context(|| format!("failed to open `{}`", path.display()))?;
664
665 let lock_meta = LockMetadata { v: 1 };
666 write!(ok, "{}", serde_json::to_string(&lock_meta).unwrap())?;
667
668 self.gctx
669 .deferred_global_last_use()?
670 .mark_registry_src_used(global_cache_tracker::RegistrySrc {
671 encoded_registry_name: self.name,
672 package_dir: package_dir.into(),
673 size: Some(bytes_written),
674 });
675
676 Ok(unpack_dir.to_path_buf())
677 }
678
679 /// Unpacks the `.crate` tarball of the package in a given directory.
680 ///
681 /// Returns the path to the crate tarball directory,
682 /// which is always `<unpack_dir>/<pkg>-<version>`.
683 ///
684 /// This holds some assumptions
685 ///
686 /// * The associated tarball already exists
687 /// * If this is a local registry,
688 /// the package cache lock must be externally synchronized.
689 /// Cargo does not take care of it being locked or not.
690 pub fn unpack_package_in(
691 &self,
692 pkg: &PackageId,
693 unpack_dir: &Path,
694 include: &dyn Fn(&Path) -> bool,
695 ) -> CargoResult<PathBuf> {
696 let path = self.ops.cache_path().join(pkg.tarball_name());
697 let path = self.ops.assert_index_locked(&path);
698 let dst = unpack_dir.join(format!("{}-{}", pkg.name(), pkg.version()));
699 let tarball =
700 File::open(path).with_context(|| format!("failed to open {}", path.display()))?;
701 unpack(self.gctx, &tarball, &dst, include)?;
702 update_mtime_for_generated_files(&dst);
703 Ok(dst)
704 }
705
706 /// Turns the downloaded `.crate` tarball file into a [`Package`].
707 ///
708 /// This unconditionally sets checksum for the returned package, so it
709 /// should only be called after doing integrity check. That is to say,
710 /// you need to call either [`RegistryData::download`] or
711 /// [`RegistryData::finish_download`] before calling this method.
712 fn get_pkg(&self, package: PackageId, path: &File) -> CargoResult<Package> {
713 let path = self
714 .unpack_package(package, path)
715 .with_context(|| format!("failed to unpack package `{}`", package))?;
716 let src = PathSource::new(&path, self.source_id, self.gctx);
717 src.load()?;
718 let mut pkg = match src.download(package)? {
719 MaybePackage::Ready(pkg) => pkg,
720 MaybePackage::Download { .. } => unreachable!(),
721 };
722
723 // After we've loaded the package configure its summary's `checksum`
724 // field with the checksum we know for this `PackageId`.
725 let cksum = self
726 .index
727 .hash(package, &*self.ops)
728 .now_or_never()
729 .expect("a downloaded dep now pending!?")
730 .expect("summary not found");
731 pkg.manifest_mut()
732 .summary_mut()
733 .set_checksum(cksum.to_string());
734
735 Ok(pkg)
736 }
737}
738
739#[async_trait::async_trait(?Send)]
740impl<'gctx> Source for RegistrySource<'gctx> {
741 async fn query(
742 &self,
743 dep: &Dependency,
744 kind: QueryKind,
745 f: &mut dyn FnMut(IndexSummary),
746 ) -> CargoResult<()> {
747 let mut req = dep.version_req().clone();
748
749 // Handle `cargo update --precise` here.
750 if let Some((_, requested)) = self
751 .source_id
752 .precise_registry_version(dep.package_name().as_str())
753 .filter(|(c, to)| {
754 if to.is_prerelease() && self.gctx.cli_unstable().unstable_options {
755 req.matches_prerelease(c)
756 } else {
757 req.matches(c)
758 }
759 })
760 {
761 req.precise_to(&requested);
762 }
763
764 let mut called = false;
765 let callback = &mut |s| {
766 called = true;
767 f(s);
768 };
769
770 // If this is a locked dependency, then it came from a lock file and in
771 // theory the registry is known to contain this version. If, however, we
772 // come back with no summaries, then our registry may need to be
773 // updated, so we fall back to performing a lazy update.
774 if kind == QueryKind::Exact && req.is_locked() && !self.ops.is_updated() {
775 debug!("attempting query without update");
776 self.index
777 .query_inner(dep.package_name(), &req, &*self.ops, &mut |s| {
778 if matches!(s, IndexSummary::Candidate(_) | IndexSummary::Yanked(_))
779 && dep.matches(s.as_summary())
780 {
781 // We are looking for a package from a lock file so we do not care about yank
782 callback(s)
783 }
784 })
785 .await?;
786 if called {
787 return Ok(());
788 } else {
789 debug!("falling back to an update");
790 self.invalidate_cache();
791 }
792 }
793
794 let mut called = false;
795 let callback = &mut |s| {
796 called = true;
797 f(s);
798 };
799
800 let mut precise_yanked_in_use = false;
801 self.index
802 .query_inner(dep.package_name(), &req, &*self.ops, &mut |s| {
803 let matched = match kind {
804 QueryKind::Exact | QueryKind::RejectedVersions => {
805 if req.is_precise() && self.gctx.cli_unstable().unstable_options {
806 dep.matches_prerelease(s.as_summary())
807 } else {
808 dep.matches(s.as_summary())
809 }
810 }
811 QueryKind::AlternativeNames => true,
812 QueryKind::Normalized => true,
813 };
814 if !matched {
815 return;
816 }
817 // Next filter out all yanked packages. Some yanked packages may
818 // leak through if they're in a whitelist (aka if they were
819 // previously in `Cargo.lock`
820 match s {
821 s @ _ if kind == QueryKind::RejectedVersions => callback(s),
822 s @ IndexSummary::Candidate(_) => callback(s),
823 s @ IndexSummary::Yanked(_) => {
824 if self.yanked_whitelist.borrow().contains(&s.package_id()) {
825 callback(s);
826 } else if req.is_precise() {
827 precise_yanked_in_use = true;
828 callback(s);
829 }
830 }
831 IndexSummary::Unsupported(summary, v) => {
832 tracing::debug!(
833 "unsupported schema version {} ({} {})",
834 v,
835 summary.name(),
836 summary.version()
837 );
838 }
839 IndexSummary::Invalid(summary) => {
840 tracing::debug!("invalid ({} {})", summary.name(), summary.version());
841 }
842 IndexSummary::Offline(summary) => {
843 tracing::debug!("offline ({} {})", summary.name(), summary.version());
844 }
845 }
846 })
847 .await?;
848 if precise_yanked_in_use {
849 let name = dep.package_name();
850 let version = req
851 .precise_version()
852 .expect("--precise <yanked-version> in use");
853 if self
854 .selected_precise_yanked
855 .borrow_mut()
856 .insert((name, version.clone()))
857 {
858 let mut shell = self.gctx.shell();
859 shell.print_report(
860 &[Level::WARNING
861 .secondary_title(format!(
862 "selected package `{name}@{version}` was yanked by the author"
863 ))
864 .element(
865 Level::HELP.message("if possible, try a compatible non-yanked version"),
866 )],
867 false,
868 )?;
869 }
870 }
871 if called {
872 return Ok(());
873 }
874 if kind == QueryKind::AlternativeNames || kind == QueryKind::Normalized {
875 // Attempt to handle misspellings by searching for a chain of related
876 // names to the original name. The resolver will later
877 // reject any candidates that have the wrong name, and with this it'll
878 // have enough information to offer "a similar crate exists" suggestions.
879 // For now we only try canonicalizing `-` to `_` and vice versa.
880 // More advanced fuzzy searching become in the future.
881 for name_permutation in [
882 dep.package_name().replace('-', "_"),
883 dep.package_name().replace('_', "-"),
884 ] {
885 let name_permutation = name_permutation.into();
886 if name_permutation == dep.package_name() {
887 continue;
888 }
889 self.index
890 .query_inner(name_permutation, &req, &*self.ops, &mut |s| {
891 if !s.is_yanked() {
892 f(s);
893 } else if kind == QueryKind::AlternativeNames {
894 f(s);
895 }
896 })
897 .await?;
898 }
899 }
900 Ok(())
901 }
902
903 fn supports_checksums(&self) -> bool {
904 true
905 }
906
907 fn requires_precise(&self) -> bool {
908 false
909 }
910
911 fn source_id(&self) -> SourceId {
912 self.source_id
913 }
914
915 fn invalidate_cache(&self) {
916 self.index.clear_summaries_cache();
917 self.ops.invalidate_cache();
918 }
919
920 fn set_quiet(&mut self, quiet: bool) {
921 self.ops.set_quiet(quiet);
922 }
923
924 fn download(&self, package: PackageId) -> CargoResult<MaybePackage> {
925 let hash = crate::util::block_on(self.index.hash(package, &*self.ops))?;
926 match self.ops.download(package, &hash)? {
927 MaybeLock::Ready(file) => self.get_pkg(package, &file).map(MaybePackage::Ready),
928 MaybeLock::Download {
929 url,
930 descriptor,
931 authorization,
932 } => Ok(MaybePackage::Download {
933 url,
934 descriptor,
935 authorization,
936 }),
937 }
938 }
939
940 fn finish_download(&self, package: PackageId, data: Vec<u8>) -> CargoResult<Package> {
941 let hash = crate::util::block_on(self.index.hash(package, &*self.ops))?;
942 let file = self.ops.finish_download(package, &hash, &data)?;
943 self.get_pkg(package, &file)
944 }
945
946 fn fingerprint(&self, pkg: &Package) -> CargoResult<String> {
947 Ok(pkg.package_id().version().to_string())
948 }
949
950 fn describe(&self) -> String {
951 self.source_id.display_index()
952 }
953
954 fn add_to_yanked_whitelist(&self, pkgs: &[PackageId]) {
955 self.yanked_whitelist.borrow_mut().extend(pkgs);
956 }
957
958 async fn is_yanked(&self, pkg: PackageId) -> CargoResult<bool> {
959 self.index.is_yanked(pkg, &*self.ops).await
960 }
961}
962
963impl RegistryConfig {
964 /// File name of [`RegistryConfig`].
965 const NAME: &'static str = "config.json";
966}
967
968/// Get the maximum unpack size that Cargo permits
969/// based on a given `size` of your compressed file.
970///
971/// Returns the larger one between `size * max compression ratio`
972/// and a fixed max unpacked size.
973///
974/// In reality, the compression ratio usually falls in the range of 2:1 to 10:1.
975/// We choose 20:1 to cover almost all possible cases hopefully.
976/// Any ratio higher than this is considered as a zip bomb.
977///
978/// In the future we might want to introduce a configurable size.
979///
980/// Some of the real world data from common compression algorithms:
981///
982/// * <https://www.zlib.net/zlib_tech.html>
983/// * <https://cran.r-project.org/web/packages/brotli/vignettes/brotli-2015-09-22.pdf>
984/// * <https://blog.cloudflare.com/results-experimenting-brotli/>
985/// * <https://tukaani.org/lzma/benchmarks.html>
986fn max_unpack_size(gctx: &GlobalContext, size: u64) -> u64 {
987 const SIZE_VAR: &str = "__CARGO_TEST_MAX_UNPACK_SIZE";
988 const RATIO_VAR: &str = "__CARGO_TEST_MAX_UNPACK_RATIO";
989 const MAX_UNPACK_SIZE: u64 = 512 * 1024 * 1024; // 512 MiB
990 const MAX_COMPRESSION_RATIO: usize = 20; // 20:1
991
992 let max_unpack_size = if cfg!(debug_assertions) && gctx.get_env(SIZE_VAR).is_ok() {
993 // For integration test only.
994 gctx.get_env(SIZE_VAR)
995 .unwrap()
996 .parse()
997 .expect("a max unpack size in bytes")
998 } else {
999 MAX_UNPACK_SIZE
1000 };
1001 let max_compression_ratio = if cfg!(debug_assertions) && gctx.get_env(RATIO_VAR).is_ok() {
1002 // For integration test only.
1003 gctx.get_env(RATIO_VAR)
1004 .unwrap()
1005 .parse()
1006 .expect("a max compression ratio in bytes")
1007 } else {
1008 MAX_COMPRESSION_RATIO
1009 };
1010
1011 u64::max(max_unpack_size, size * max_compression_ratio as u64)
1012}
1013
1014/// Set the current [`umask`] value for the given tarball. No-op on non-Unix
1015/// platforms.
1016///
1017/// On Windows, tar only looks at user permissions and tries to set the "read
1018/// only" attribute, so no-op as well.
1019///
1020/// [`umask`]: https://man7.org/linux/man-pages/man2/umask.2.html
1021#[allow(unused_variables)]
1022fn set_mask<R: Read>(tar: &mut Archive<R>) {
1023 #[cfg(unix)]
1024 tar.set_mask(crate::util::get_umask());
1025}
1026
1027/// Unpack a tarball with zip bomb and overwrite protections.
1028fn unpack(
1029 gctx: &GlobalContext,
1030 tarball: &File,
1031 unpack_dir: &Path,
1032 include: &dyn Fn(&Path) -> bool,
1033) -> CargoResult<u64> {
1034 let mut tar = {
1035 let size_limit = max_unpack_size(gctx, tarball.metadata()?.len());
1036 let gz = GzDecoder::new(tarball);
1037 let gz = LimitErrorReader::new(gz, size_limit);
1038 let mut tar = Archive::new(gz);
1039 set_mask(&mut tar);
1040 tar
1041 };
1042 let mut bytes_written = 0;
1043 let prefix = unpack_dir.file_name().unwrap();
1044 let parent = unpack_dir.parent().unwrap();
1045 for entry in tar.entries()? {
1046 let mut entry = entry.context("failed to iterate over archive")?;
1047 let entry_path = entry
1048 .path()
1049 .context("failed to read entry path")?
1050 .into_owned();
1051
1052 if let Ok(path) = entry_path.strip_prefix(prefix) {
1053 if !include(path) {
1054 continue;
1055 }
1056 } else {
1057 // We're going to unpack this tarball into the global source
1058 // directory, but we want to make sure that it doesn't accidentally
1059 // (or maliciously) overwrite source code from other crates. Cargo
1060 // itself should never generate a tarball that hits this error, and
1061 // crates.io should also block uploads with these sorts of tarballs,
1062 // but be extra sure by adding a check here as well.
1063 anyhow::bail!(
1064 "invalid tarball downloaded, contains \
1065 a file at {entry_path:?} which isn't under {prefix:?}",
1066 )
1067 }
1068
1069 // Prevent unpacking the lockfile from the crate itself.
1070 if entry_path
1071 .file_name()
1072 .map_or(false, |p| p == PACKAGE_SOURCE_LOCK)
1073 {
1074 continue;
1075 }
1076 // Unpacking failed
1077 bytes_written += entry.size();
1078 let mut result = entry.unpack_in(parent).map_err(anyhow::Error::from);
1079 if cfg!(windows) && restricted_names::is_windows_reserved_path(&entry_path) {
1080 result = result.with_context(|| {
1081 format!(
1082 "`{}` appears to contain a reserved Windows path, \
1083 it cannot be extracted on Windows",
1084 entry_path.display()
1085 )
1086 });
1087 }
1088 result.with_context(|| format!("failed to unpack entry at `{}`", entry_path.display()))?;
1089 }
1090
1091 Ok(bytes_written)
1092}
1093
1094/// Workaround for rust-lang/cargo#16237
1095///
1096/// Generated files should have the same deterministic mtime as other files.
1097/// However, since we forgot to set mtime for those files when uploading, they
1098/// always have older mtime (1973-11-29) that prevents zip from packing (requiring >1980)
1099///
1100/// This workaround updates mtime after we unpack the tarball at the destination.
1101fn update_mtime_for_generated_files(pkg_root: &Path) {
1102 const GENERATED_FILES: &[&str] = &["Cargo.lock", "Cargo.toml", ".cargo_vcs_info.json"];
1103 // Hardcoded value be removed once alexcrichton/tar-rs#420 is merged and released.
1104 // See also rust-lang/cargo#16237
1105 const DETERMINISTIC_TIMESTAMP: i64 = 1153704088;
1106
1107 for file in GENERATED_FILES {
1108 let path = pkg_root.join(file);
1109 let mtime = filetime::FileTime::from_unix_time(DETERMINISTIC_TIMESTAMP, 0);
1110 if let Err(e) = filetime::set_file_mtime(&path, mtime) {
1111 tracing::trace!("failed to set deterministic mtime for {path:?}: {e}");
1112 }
1113 }
1114}