Skip to content

Commit 7cbf886

Browse files
committedDec 20, 2018
Auto merge of #6469 - ehuss:registry-name, r=dwijnand
Restrict registry names to same style as package names. See #4688 (comment)
2 parents 0d1f1bb + 080f0b3 commit 7cbf886

File tree

8 files changed

+84
-45
lines changed

8 files changed

+84
-45
lines changed
 

‎src/cargo/core/package_id_spec.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use url::Url;
77

88
use crate::core::PackageId;
99
use crate::util::errors::{CargoResult, CargoResultExt};
10-
use crate::util::{ToSemver, ToUrl};
10+
use crate::util::{validate_package_name, ToSemver, ToUrl};
1111

1212
/// Some or all of the data required to identify a package:
1313
///
@@ -64,11 +64,7 @@ impl PackageIdSpec {
6464
Some(version) => Some(Version::parse(version)?),
6565
None => None,
6666
};
67-
for ch in name.chars() {
68-
if !ch.is_alphanumeric() && ch != '_' && ch != '-' {
69-
failure::bail!("invalid character in pkgid `{}`: `{}`", spec, ch)
70-
}
71-
}
67+
validate_package_name(name, "pkgid", "")?;
7268
Ok(PackageIdSpec {
7369
name: name.to_string(),
7470
version,

‎src/cargo/ops/cargo_new.rs

+2-15
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use git2::Repository as GitRepository;
1010
use crate::core::{compiler, Workspace};
1111
use crate::util::errors::{CargoResult, CargoResultExt};
1212
use crate::util::{existing_vcs_repo, internal, FossilRepo, GitRepo, HgRepo, PijulRepo};
13-
use crate::util::{paths, Config};
13+
use crate::util::{paths, validate_package_name, Config};
1414

1515
use toml;
1616

@@ -161,20 +161,7 @@ fn check_name(name: &str, opts: &NewOptions) -> CargoResult<()> {
161161
}
162162
}
163163

164-
for c in name.chars() {
165-
if c.is_alphanumeric() {
166-
continue;
167-
}
168-
if c == '_' || c == '-' {
169-
continue;
170-
}
171-
failure::bail!(
172-
"Invalid character `{}` in crate name: `{}`{}",
173-
c,
174-
name,
175-
name_help
176-
)
177-
}
164+
validate_package_name(name, "crate name", name_help)?;
178165
Ok(())
179166
}
180167

‎src/cargo/ops/registry.rs

+10-7
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ use crate::sources::{RegistrySource, SourceConfigMap};
1919
use crate::util::config::{self, Config};
2020
use crate::util::errors::{CargoResult, CargoResultExt};
2121
use crate::util::important_paths::find_root_manifest_for_wd;
22-
use crate::util::paths;
2322
use crate::util::ToUrl;
23+
use crate::util::{paths, validate_package_name};
2424
use crate::version;
2525

2626
pub struct RegistryConfig {
@@ -294,12 +294,15 @@ pub fn registry_configuration(
294294
registry: Option<String>,
295295
) -> CargoResult<RegistryConfig> {
296296
let (index, token) = match registry {
297-
Some(registry) => (
298-
Some(config.get_registry_index(&registry)?.to_string()),
299-
config
300-
.get_string(&format!("registries.{}.token", registry))?
301-
.map(|p| p.val),
302-
),
297+
Some(registry) => {
298+
validate_package_name(&registry, "registry name", "")?;
299+
(
300+
Some(config.get_registry_index(&registry)?.to_string()),
301+
config
302+
.get_string(&format!("registries.{}.token", registry))?
303+
.map(|p| p.val),
304+
)
305+
}
303306
None => {
304307
// Checking out for default index and token
305308
(

‎src/cargo/util/command_prelude.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::core::Workspace;
66
use crate::ops::{CompileFilter, CompileOptions, NewOptions, Packages, VersionControl};
77
use crate::sources::CRATES_IO_REGISTRY;
88
use crate::util::important_paths::find_root_manifest_for_wd;
9-
use crate::util::paths;
9+
use crate::util::{paths, validate_package_name};
1010
use crate::CargoResult;
1111
use clap::{self, SubCommand};
1212

@@ -370,11 +370,12 @@ pub trait ArgMatchesExt {
370370
requires -Zunstable-options to use."
371371
));
372372
}
373+
validate_package_name(registry, "registry name", "")?;
373374

374375
if registry == CRATES_IO_REGISTRY {
375376
// If "crates.io" is specified then we just need to return None
376377
// as that will cause cargo to use crates.io. This is required
377-
// for the case where a default alterative registry is used
378+
// for the case where a default alternative registry is used
378379
// but the user wants to switch back to crates.io for a single
379380
// command.
380381
Ok(None)

‎src/cargo/util/config.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ use crate::core::shell::Verbosity;
2424
use crate::core::{CliUnstable, Shell, SourceId, Workspace};
2525
use crate::ops;
2626
use crate::util::errors::{internal, CargoResult, CargoResultExt};
27-
use crate::util::paths;
2827
use crate::util::toml as cargo_toml;
2928
use crate::util::Filesystem;
3029
use crate::util::Rustc;
3130
use crate::util::ToUrl;
31+
use crate::util::{paths, validate_package_name};
3232
use url::Url;
3333

3434
use self::ConfigValue as CV;
@@ -656,6 +656,7 @@ impl Config {
656656

657657
/// Gets the index for a registry.
658658
pub fn get_registry_index(&self, registry: &str) -> CargoResult<Url> {
659+
validate_package_name(registry, "registry name", "")?;
659660
Ok(
660661
match self.get_string(&format!("registries.{}.index", registry))? {
661662
Some(index) => {

‎src/cargo/util/mod.rs

+16
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,19 @@ pub fn elapsed(duration: Duration) -> String {
5959
format!("{}.{:02}s", secs, duration.subsec_nanos() / 10_000_000)
6060
}
6161
}
62+
63+
/// Check the base requirements for a package name.
64+
///
65+
/// This can be used for other things than package names, to enforce some
66+
/// level of sanity. Note that package names have other restrictions
67+
/// elsewhere. `cargo new` has a few restrictions, such as checking for
68+
/// reserved names. crates.io has even more restrictions.
69+
pub fn validate_package_name(name: &str, what: &str, help: &str) -> CargoResult<()> {
70+
if let Some(ch) = name
71+
.chars()
72+
.find(|ch| !ch.is_alphanumeric() && *ch != '_' && *ch != '-')
73+
{
74+
failure::bail!("Invalid character `{}` in {}: `{}`{}", ch, what, name, help);
75+
}
76+
Ok(())
77+
}

‎src/cargo/util/toml/mod.rs

+2-14
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use crate::core::{GitReference, PackageIdSpec, SourceId, WorkspaceConfig, Worksp
2121
use crate::sources::{CRATES_IO_INDEX, CRATES_IO_REGISTRY};
2222
use crate::util::errors::{CargoResult, CargoResultExt, ManifestError};
2323
use crate::util::paths;
24-
use crate::util::{self, Config, ToUrl};
24+
use crate::util::{self, validate_package_name, Config, ToUrl};
2525

2626
mod targets;
2727
use self::targets::targets;
@@ -809,19 +809,7 @@ impl TomlManifest {
809809
failure::bail!("package name cannot be an empty string")
810810
}
811811

812-
for c in package_name.chars() {
813-
if c.is_alphanumeric() {
814-
continue;
815-
}
816-
if c == '_' || c == '-' {
817-
continue;
818-
}
819-
failure::bail!(
820-
"Invalid character `{}` in package name: `{}`",
821-
c,
822-
package_name
823-
)
824-
}
812+
validate_package_name(package_name, "package name", "")?;
825813

826814
let pkgid = project.to_package_id(source_id)?;
827815

‎tests/testsuite/alt_registry.rs

+47
Original file line numberDiff line numberDiff line change
@@ -558,3 +558,50 @@ fn patch_alt_reg() {
558558
)
559559
.run();
560560
}
561+
562+
#[test]
563+
fn bad_registry_name() {
564+
let p = project()
565+
.file(
566+
"Cargo.toml",
567+
r#"
568+
cargo-features = ["alternative-registries"]
569+
570+
[project]
571+
name = "foo"
572+
version = "0.0.1"
573+
authors = []
574+
575+
[dependencies.bar]
576+
version = "0.0.1"
577+
registry = "bad name"
578+
"#,
579+
)
580+
.file("src/main.rs", "fn main() {}")
581+
.build();
582+
583+
p.cargo("build")
584+
.masquerade_as_nightly_cargo()
585+
.with_status(101)
586+
.with_stderr(
587+
"\
588+
[ERROR] failed to parse manifest at `[CWD]/Cargo.toml`
589+
590+
Caused by:
591+
Invalid character ` ` in registry name: `bad name`",
592+
)
593+
.run();
594+
595+
for cmd in &[
596+
"init", "install", "login", "owner", "publish", "search", "yank",
597+
] {
598+
p.cargo(cmd)
599+
.arg("-Zunstable-options")
600+
.arg("--registry")
601+
.arg("bad name")
602+
.masquerade_as_nightly_cargo()
603+
.with_status(101)
604+
.with_stderr("[ERROR] Invalid character ` ` in registry name: `bad name`")
605+
.run();
606+
}
607+
}

0 commit comments

Comments
 (0)
Please sign in to comment.