Skip to content

Ensure only exact name matches are added to the index #1550

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/models/dependency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ pub fn add_dependencies(
let git_and_new_dependencies = deps
.iter()
.map(|dep| {
let krate = Crate::by_name(&dep.name)
// Match only identical names to ensure the index always references the original crate name
let krate = Crate::by_exact_name(&dep.name)
.first::<Crate>(&*conn)
.map_err(|_| human(&format_args!("no known crate named `{}`", &*dep.name)))?;
if dep.version_req == semver::VersionReq::parse("*").unwrap() {
Expand Down
5 changes: 5 additions & 0 deletions src/models/krate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ type CanonCrateName<T> = self::canon_crate_name::HelperType<T>;
type All = diesel::dsl::Select<crates::table, AllColumns>;
type WithName<'a> = diesel::dsl::Eq<CanonCrateName<crates::name>, CanonCrateName<&'a str>>;
type ByName<'a> = diesel::dsl::Filter<All, WithName<'a>>;
type ByExactName<'a> = diesel::dsl::Filter<All, diesel::dsl::Eq<crates::name, &'a str>>;

#[derive(Insertable, AsChangeset, Default, Debug)]
#[table_name = "crates"]
Expand Down Expand Up @@ -244,6 +245,10 @@ impl Crate {
Crate::all().filter(Self::with_name(name))
}

pub fn by_exact_name(name: &str) -> ByExactName<'_> {
Crate::all().filter(crates::name.eq(name))
}

pub fn all() -> All {
crates::table.select(ALL_COLUMNS)
}
Expand Down
17 changes: 17 additions & 0 deletions src/tests/krate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,23 @@ fn new_krate_with_dependency() {
token.publish(crate_to_publish).good();
}

#[test]
fn reject_new_krate_with_non_exact_dependency() {
let (app, _, user, token) = TestApp::init().with_token();

app.db(|conn| {
CrateBuilder::new("foo-dep", user.as_model().id).expect_build(&conn);
});

// Use non-exact name for the dependency
let dependency = DependencyBuilder::new("foo_dep");

let crate_to_publish = PublishBuilder::new("new_dep")
.version("1.0.0")
.dependency(dependency);
token.publish(crate_to_publish).bad_with_status(200);
}

#[test]
fn new_krate_with_wildcard_dependency() {
let (app, _, user, token) = TestApp::init().with_token();
Expand Down