Skip to content

Commit

Permalink
fix!: Disallow empty registry names
Browse files Browse the repository at this point in the history
This was allowed when switching from `toml` v0.5 to `toml_edit` which
started allowing empty keys when parsing TOML.
This mirrors the change
we made for disallowing empty feature names in rust-lang#12928.
  • Loading branch information
epage committed Dec 13, 2023
1 parent d415790 commit dc5688a
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
6 changes: 6 additions & 0 deletions src/cargo/util/command_prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -833,6 +833,9 @@ Run `{cmd}` to see possible targets."
(None, None) => config.default_registry()?.map(RegistryOrIndex::Registry),
(None, Some(i)) => Some(RegistryOrIndex::Index(i.into_url()?)),
(Some(r), None) => {
if r.is_empty() {
bail!("registry name cannot be empty");
}
restricted_names::validate_package_name(r, "registry name", "")?;
Some(RegistryOrIndex::Registry(r.to_string()))
}
Expand All @@ -848,6 +851,9 @@ Run `{cmd}` to see possible targets."
match self._value_of("registry").map(|s| s.to_string()) {
None => config.default_registry(),
Some(registry) => {
if registry.is_empty() {
bail!("registry name cannot be empty");
}
restricted_names::validate_package_name(&registry, "registry name", "")?;
Ok(Some(registry))
}
Expand Down
3 changes: 3 additions & 0 deletions src/cargo/util/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1551,6 +1551,9 @@ impl Config {

/// Gets the index for a registry.
pub fn get_registry_index(&self, registry: &str) -> CargoResult<Url> {
if registry.is_empty() {
bail!("registry name cannot be empty");
}
validate_package_name(registry, "registry name", "")?;
if let Some(index) = self.get_string(&format!("registries.{}.index", registry))? {
self.resolve_registry_index(&index).with_context(|| {
Expand Down
6 changes: 3 additions & 3 deletions tests/testsuite/alt_registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1568,7 +1568,7 @@ fn config_empty_registry_name() {
.with_status(101)
.with_stderr(
"\
[ERROR] registry index was not found in any configuration: ``",
[ERROR] registry name cannot be empty",
)
.run();
}
Expand All @@ -1583,7 +1583,7 @@ fn empty_registry_flag() {
.with_status(101)
.with_stderr(
"\
[ERROR] registry index was not found in any configuration: ``",
[ERROR] registry name cannot be empty",
)
.run();
}
Expand Down Expand Up @@ -1618,7 +1618,7 @@ fn empty_dependency_registry() {
[ERROR] failed to parse manifest at `[CWD]/Cargo.toml`
Caused by:
registry index was not found in any configuration: ``",
registry name cannot be empty",
)
.run();
}

0 comments on commit dc5688a

Please sign in to comment.