Skip to content

Commit 6dce2aa

Browse files
committed
Auto merge of #6854 - fluffysquirrels:validate-login, r=alexcrichton
Validate registry token before operations that require it. Fixes #6847 .
2 parents b978d11 + f7c424f commit 6dce2aa

File tree

3 files changed

+49
-4
lines changed

3 files changed

+49
-4
lines changed

src/cargo/ops/registry.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ pub fn publish(ws: &Workspace<'_>, opts: &PublishOpts<'_>) -> CargoResult<()> {
6969
opts.index.clone(),
7070
opts.registry.clone(),
7171
true,
72+
!opts.dry_run
7273
)?;
7374
verify_dependencies(pkg, &registry, reg_id)?;
7475

@@ -334,12 +335,13 @@ pub fn registry_configuration(
334335
Ok(RegistryConfig { index, token })
335336
}
336337

337-
pub fn registry(
338+
fn registry(
338339
config: &Config,
339340
token: Option<String>,
340341
index: Option<String>,
341342
registry: Option<String>,
342343
force_update: bool,
344+
validate_token: bool
343345
) -> CargoResult<(Registry, SourceId)> {
344346
// Parse all configuration options
345347
let RegistryConfig {
@@ -363,6 +365,9 @@ pub fn registry(
363365
.ok_or_else(|| format_err!("{} does not support API commands", sid))?
364366
};
365367
let handle = http_handle(config)?;
368+
if validate_token && token.is_none() {
369+
bail!("no upload token found, please run `cargo login`");
370+
};
366371
Ok((Registry::new_handle(api_host, token, handle), sid))
367372
}
368373

@@ -536,7 +541,7 @@ pub fn registry_login(
536541
token: Option<String>,
537542
reg: Option<String>,
538543
) -> CargoResult<()> {
539-
let (registry, _) = registry(config, token.clone(), None, reg.clone(), false)?;
544+
let (registry, _) = registry(config, token.clone(), None, reg.clone(), false, false)?;
540545

541546
let token = match token {
542547
Some(token) => token,
@@ -604,6 +609,7 @@ pub fn modify_owners(config: &Config, opts: &OwnersOptions) -> CargoResult<()> {
604609
opts.index.clone(),
605610
opts.registry.clone(),
606611
true,
612+
true
607613
)?;
608614

609615
if let Some(ref v) = opts.to_add {
@@ -664,7 +670,7 @@ pub fn yank(
664670
None => bail!("a version must be specified to yank"),
665671
};
666672

667-
let (mut registry, _) = registry(config, token, index, reg, true)?;
673+
let (mut registry, _) = registry(config, token, index, reg, true, true)?;
668674

669675
if undo {
670676
config
@@ -720,7 +726,7 @@ pub fn search(
720726
prefix
721727
}
722728

723-
let (mut registry, source_id) = registry(config, None, index, reg, false)?;
729+
let (mut registry, source_id) = registry(config, None, index, reg, false, false)?;
724730
let (crates, total_crates) = registry
725731
.search(query, limit)
726732
.chain_err(|| "failed to retrieve search results from the registry")?;

tests/testsuite/alt_registry.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,9 @@ fn cannot_publish_to_crates_io_with_registry_dependency() {
289289
)
290290
.build();
291291

292+
// Login so that we have the token available
293+
p.cargo("login --registry fakeio TOKEN").run();
294+
292295
p.cargo("publish --registry fakeio")
293296
.with_status(101)
294297
.with_stderr_contains("[ERROR] crates cannot be published to crates.io[..]")

tests/testsuite/publish.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -977,3 +977,39 @@ fn publish_with_patch() {
977977
&["Cargo.toml", "Cargo.toml.orig", "src/main.rs"],
978978
);
979979
}
980+
981+
#[test]
982+
fn publish_checks_for_token_before_verify() {
983+
registry::init();
984+
985+
let p = project()
986+
.file(
987+
"Cargo.toml",
988+
r#"
989+
[project]
990+
name = "foo"
991+
version = "0.0.1"
992+
authors = []
993+
license = "MIT"
994+
description = "foo"
995+
"#,
996+
)
997+
.file("src/main.rs", "fn main() {}")
998+
.build();
999+
1000+
let credentials = paths::home().join(".cargo/credentials");
1001+
fs::remove_file(&credentials).unwrap();
1002+
1003+
// Assert upload token error before the package is verified
1004+
p.cargo("publish")
1005+
.with_status(101)
1006+
.with_stderr_contains("[ERROR] no upload token found, please run `cargo login`")
1007+
.with_stderr_does_not_contain("[VERIFYING] foo v0.0.1 ([CWD])")
1008+
.run();
1009+
1010+
// Assert package verified successfully on dry run
1011+
p.cargo("publish --dry-run")
1012+
.with_status(0)
1013+
.with_stderr_contains("[VERIFYING] foo v0.0.1 ([CWD])")
1014+
.run();
1015+
}

0 commit comments

Comments
 (0)