Skip to content

Commit 479bf73

Browse files
authored
Fix is_default_registry for sparse index (rustsec#859)
1 parent 664d989 commit 479bf73

1 file changed

Lines changed: 10 additions & 4 deletions

File tree

cargo-lock/src/package/source.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ use std::path::Path;
1717

1818
/// Location of the crates.io index
1919
pub const CRATES_IO_INDEX: &str = "https://github.com/rust-lang/crates.io-index";
20+
/// Location of the crates.io sparse HTTP index
21+
pub const CRATES_IO_SPARSE_INDEX: &str = "sparse+https://index.crates.io/";
2022

2123
/// Default branch name
2224
pub const DEFAULT_BRANCH: &str = "master";
@@ -236,8 +238,9 @@ impl SourceId {
236238

237239
/// Returns `true` if the remote registry is the standard <https://crates.io>.
238240
pub fn is_default_registry(&self) -> bool {
239-
matches!(self.kind, SourceKind::Registry | SourceKind::SparseRegistry)
240-
&& self.url.as_str() == CRATES_IO_INDEX
241+
self.kind == SourceKind::Registry && self.url.as_str() == CRATES_IO_INDEX
242+
|| self.kind == SourceKind::SparseRegistry
243+
&& self.url.as_str() == &CRATES_IO_SPARSE_INDEX[7..]
241244
}
242245
}
243246

@@ -384,7 +387,10 @@ mod tests {
384387
use super::SourceId;
385388

386389
#[test]
387-
fn default_works() {
388-
SourceId::default();
390+
fn identifies_crates_io() {
391+
assert!(SourceId::default().is_default_registry());
392+
assert!(SourceId::from_url(super::CRATES_IO_SPARSE_INDEX)
393+
.expect("failed to parse sparse URL")
394+
.is_default_registry());
389395
}
390396
}

0 commit comments

Comments
 (0)