From 93463fa7056bbb91c49b55f2fb95dd13c2d4efac Mon Sep 17 00:00:00 2001 From: jongiddy Date: Fri, 20 Dec 2024 11:22:27 +0000 Subject: [PATCH] Fix incorrect precedence for inexact match check (#739) 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)); + } }