Skip to content

Commit

Permalink
Fix incorrect precedence for inexact match check
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jongiddy committed Dec 20, 2024
1 parent e7a2511 commit 0047874
Showing 1 changed file with 38 additions and 3 deletions.
41 changes: 38 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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));
}
}

0 comments on commit 0047874

Please sign in to comment.