From 004787459913b1fe13bf0b2ec402a3b0cd1dc098 Mon Sep 17 00:00:00 2001 From: Jonathan Giddy Date: Fri, 20 Dec 2024 10:13:28 +0000 Subject: [PATCH] Fix incorrect precedence for inexact match check Due to the precedence of the logical operators an inexact match is detected even if the hostnames differ. Only the path is checked to be a prefix of the crate path. Adding parentheses fixes the problem. --- src/lib.rs | 41 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 38 insertions(+), 3 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 168f8425..fd0bb7de 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -388,8 +388,9 @@ impl Krate { Source::Sparse(surl) | Source::Registry(surl) | Source::Git { url: surl, .. } => surl, }; - kurl.host() == url.host() && (exact && kurl.path() == url.path()) - || (!exact && kurl.path().starts_with(url.path())) + kurl.host() == url.host() + && ((exact && kurl.path() == url.path()) + || (!exact && kurl.path().starts_with(url.path()))) } #[inline] @@ -614,7 +615,7 @@ pub fn krates_with_index( #[cfg(test)] mod test { - use super::Source; + use super::{Krate, PathBuf, Source, Url}; #[test] fn parses_sources() { @@ -675,4 +676,38 @@ mod test { super::CRATES_IO_SPARSE_DIR ); } + + #[test] + fn inexact_match_fails_for_different_hosts() { + let krate = Krate { + source: Some( + Source::from_metadata( + "git+ssh://git@repo1.test.org/path/test.git".to_owned(), + &PathBuf::new(), + ) + .unwrap(), + ), + ..Krate::default() + }; + let url = Url::parse("ssh://git@repo2.test.org:8000").unwrap(); + + assert!(!krate.matches_url(&url, false)); + } + + #[test] + fn inexact_match_passes_for_same_hosts() { + let krate = Krate { + source: Some( + Source::from_metadata( + "git+ssh://git@repo1.test.org/path/test.git".to_owned(), + &PathBuf::new(), + ) + .unwrap(), + ), + ..Krate::default() + }; + let url = Url::parse("ssh://git@repo1.test.org:8000").unwrap(); + + assert!(krate.matches_url(&url, false)); + } }