Skip to content
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

Do not allow empty name in package ID spec #13152

Merged
merged 5 commits into from
Dec 13, 2023
Merged
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
10 changes: 10 additions & 0 deletions src/cargo/util_schemas/core/package_id_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ impl PackageIdSpec {
Some(version) => Some(version.parse::<PartialVersion>()?),
None => None,
};
if name.is_empty() {
bail!("package ID specification must have a name: `{spec}`");
}
validate_package_name(name, "pkgid", "")?;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should put the empty check into this function?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That seems like where it should belong but we already have that check be separate for manifests

    let package_name = package.name.trim();
    if package_name.is_empty() {
        bail!("package name cannot be an empty string")
    }

    validate_package_name(package_name, "package name", "")?;

This makes me think some more investigation is needed.

Note: empty feature names were disallowed as of #12928 but that was because we relied on the behavior of our TOML parser to disallow it which wasn't conformant behavior.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have checked all the references for this function and it seems okay to include the check in the validate_package_name. However, I have also discovered some bugs in those places. For example, if you attempt to type cargo add @1.2.3, you will receive a panic instead of an error. Therefore, I suggest we move forward with this pull request first. Once that's done, I can work on moving it into the validate_package_name and also add tests to cover these problematic cases. This PR is more focused on fixing bugs from package ID spec.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a heads up, I'm looking at touching the name validation code as well, so it'll be good to coordinate so we don't cause each other a lot of pain

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it. I will prioritize ensuring that empty names are not allowed. I will make sure I only change one thing each time and do not bring any off-topic changes. Hope that can help.

Ok(PackageIdSpec {
name: String::from(name),
Expand Down Expand Up @@ -194,6 +197,10 @@ impl PackageIdSpec {
None => (String::from(path_name), None),
}
};
if name.is_empty() {
bail!("package ID specification must have a name: `{url}`");
}
validate_package_name(name.as_str(), "pkgid", "")?;
Ok(PackageIdSpec {
name,
version,
Expand Down Expand Up @@ -597,5 +604,8 @@ mod tests {
"sparse+https://github.com/rust-lang/cargo#0.52.0?branch=dev"
)
.is_err());
assert!(PackageIdSpec::parse("@1.2.3").is_err());
assert!(PackageIdSpec::parse("registry+https://github.com").is_err());
assert!(PackageIdSpec::parse("https://crates.io/1foo#1.2.3").is_err())
}
}