Skip to content

Commit b4c3740

Browse files
committed
Do not implicitly load registry.token with --index.
The intent is to avoid leaking the crates.io token to other servers.
1 parent 2599071 commit b4c3740

File tree

8 files changed

+98
-86
lines changed

8 files changed

+98
-86
lines changed

src/cargo/ops/registry.rs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,26 @@ fn registry(
378378
token: token_config,
379379
index: index_config,
380380
} = registry_configuration(config, registry.clone())?;
381-
let token = token.or(token_config);
381+
let token = match (&index, &token, &token_config) {
382+
// No token.
383+
(None, None, None) => {
384+
if validate_token {
385+
bail!("no upload token found, please run `cargo login` or pass `--token`");
386+
}
387+
None
388+
}
389+
// Token on command-line.
390+
(_, Some(_), _) => token,
391+
// Token in config, no --index, loading from config is OK for crates.io.
392+
(None, None, Some(_)) => token_config,
393+
// --index, no --token
394+
(Some(_), None, _) => {
395+
if validate_token {
396+
bail!("command-line argument --index requires --token to be specified")
397+
}
398+
None
399+
}
400+
};
382401
let sid = get_source_id(config, index_config.or(index), registry)?;
383402
if !sid.is_remote_registry() {
384403
bail!(
@@ -408,9 +427,6 @@ fn registry(
408427
.ok_or_else(|| format_err!("{} does not support API commands", sid))?
409428
};
410429
let handle = http_handle(config)?;
411-
if validate_token && token.is_none() {
412-
bail!("no upload token found, please run `cargo login`");
413-
};
414430
Ok((Registry::new_handle(api_host, token, handle), sid))
415431
}
416432

tests/testsuite/alt_registry.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ fn cannot_publish_to_crates_io_with_registry_dependency() {
298298
.with_stderr_contains("[ERROR] crates cannot be published to crates.io[..]")
299299
.run();
300300

301-
p.cargo("publish --index")
301+
p.cargo("publish --token sekrit --index")
302302
.arg(fakeio_url.to_string())
303303
.with_status(101)
304304
.with_stderr_contains("[ERROR] crates cannot be published to crates.io[..]")
@@ -413,17 +413,18 @@ fn alt_registry_and_crates_io_deps() {
413413

414414
#[cargo_test]
415415
fn block_publish_due_to_no_token() {
416-
let p = project().file("src/main.rs", "fn main() {}").build();
417-
418-
// Setup the registry by publishing a package
419-
Package::new("bar", "0.0.1").alternative(true).publish();
416+
registry::init();
417+
let p = project().file("src/lib.rs", "").build();
420418

421419
fs::remove_file(paths::home().join(".cargo/credentials")).unwrap();
422420

423421
// Now perform the actual publish
424422
p.cargo("publish --registry alternative")
425423
.with_status(101)
426-
.with_stderr_contains("error: no upload token found, please run `cargo login`")
424+
.with_stderr_contains(
425+
"error: no upload token found, \
426+
please run `cargo login` or pass `--token`",
427+
)
427428
.run();
428429
}
429430

tests/testsuite/cargo_features.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -323,8 +323,7 @@ fn publish_allowed() {
323323
)
324324
.file("src/lib.rs", "")
325325
.build();
326-
p.cargo("publish --index")
327-
.arg(registry::registry_url().to_string())
326+
p.cargo("publish --token sekrit")
328327
.masquerade_as_nightly_cargo()
329328
.run();
330329
}

tests/testsuite/cross_publish.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,7 @@ fn publish_with_target() {
9797

9898
let target = cross_compile::alternate();
9999

100-
p.cargo("publish --index")
101-
.arg(registry::registry_url().to_string())
100+
p.cargo("publish --token sekrit")
102101
.arg("--target")
103102
.arg(&target)
104103
.with_stderr(&format!(

tests/testsuite/owner.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::fs;
44

55
use cargo_test_support::paths::CargoPathExt;
66
use cargo_test_support::project;
7-
use cargo_test_support::registry::{self, api_path, registry_url};
7+
use cargo_test_support::registry::{self, api_path};
88

99
fn setup(name: &str, content: Option<&str>) {
1010
let dir = api_path().join(format!("api/v1/crates/{}", name));
@@ -43,9 +43,7 @@ fn simple_list() {
4343
.file("src/main.rs", "fn main() {}")
4444
.build();
4545

46-
p.cargo("owner -l --index")
47-
.arg(registry_url().to_string())
48-
.run();
46+
p.cargo("owner -l --token sekrit").run();
4947
}
5048

5149
#[cargo_test]
@@ -68,8 +66,7 @@ fn simple_add() {
6866
.file("src/main.rs", "fn main() {}")
6967
.build();
7068

71-
p.cargo("owner -a username --index")
72-
.arg(registry_url().to_string())
69+
p.cargo("owner -a username --token sekrit")
7370
.with_status(101)
7471
.with_stderr(
7572
" Updating `[..]` index
@@ -98,8 +95,7 @@ fn simple_remove() {
9895
.file("src/main.rs", "fn main() {}")
9996
.build();
10097

101-
p.cargo("owner -r username --index")
102-
.arg(registry_url().to_string())
98+
p.cargo("owner -r username --token sekrit")
10399
.with_status(101)
104100
.with_stderr(
105101
" Updating `[..]` index

0 commit comments

Comments
 (0)