Skip to content

Commit 65274ea

Browse files
committed
Add a warning when using registry.token with source replacement.
1 parent b4c3740 commit 65274ea

File tree

5 files changed

+106
-30
lines changed

5 files changed

+106
-30
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ path = "src/cargo/lib.rs"
2222
atty = "0.2"
2323
bytesize = "1.0"
2424
cargo-platform = { path = "crates/cargo-platform", version = "0.1.1" }
25-
crates-io = { path = "crates/crates-io", version = "0.31" }
25+
crates-io = { path = "crates/crates-io", version = "0.31.1" }
2626
crossbeam-utils = "0.7"
2727
crypto-hash = "0.3.1"
2828
curl = { version = "0.4.23", features = ["http2"] }

crates/crates-io/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "crates-io"
3-
version = "0.31.0"
3+
version = "0.31.1"
44
edition = "2018"
55
authors = ["Alex Crichton <alex@alexcrichton.com>"]
66
license = "MIT OR Apache-2.0"

crates/crates-io/lib.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,7 @@ impl Registry {
139139
}
140140

141141
pub fn host_is_crates_io(&self) -> bool {
142-
Url::parse(self.host())
143-
.map(|u| u.host_str() == Some("crates.io"))
144-
.unwrap_or(false)
142+
is_url_crates_io(&self.host)
145143
}
146144

147145
pub fn add_owners(&mut self, krate: &str, owners: &[&str]) -> Result<String> {
@@ -420,3 +418,10 @@ fn reason(code: u32) -> &'static str {
420418
_ => "<unknown>",
421419
}
422420
}
421+
422+
/// Returns `true` if the host of the given URL is "crates.io".
423+
pub fn is_url_crates_io(url: &str) -> bool {
424+
Url::parse(url)
425+
.map(|u| u.host_str() == Some("crates.io"))
426+
.unwrap_or(false)
427+
}

src/cargo/ops/registry.rs

Lines changed: 49 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::time::Duration;
77
use std::{cmp, env};
88

99
use anyhow::{bail, format_err};
10-
use crates_io::{NewCrate, NewCrateDependency, Registry};
10+
use crates_io::{self, NewCrate, NewCrateDependency, Registry};
1111
use curl::easy::{Easy, InfoType, SslOpt, SslVersion};
1212
use log::{log, Level};
1313
use percent_encoding::{percent_encode, NON_ALPHANUMERIC};
@@ -378,27 +378,8 @@ fn registry(
378378
token: token_config,
379379
index: index_config,
380380
} = registry_configuration(config, registry.clone())?;
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-
};
401-
let sid = get_source_id(config, index_config.or(index), registry)?;
381+
let opt_index = index_config.as_ref().or(index.as_ref());
382+
let sid = get_source_id(config, opt_index, registry.as_ref())?;
402383
if !sid.is_remote_registry() {
403384
bail!(
404385
"{} does not support API commands.\n\
@@ -426,6 +407,50 @@ fn registry(
426407
cfg.and_then(|cfg| cfg.api)
427408
.ok_or_else(|| format_err!("{} does not support API commands", sid))?
428409
};
410+
let token = match (&index, &token, &token_config) {
411+
// No token.
412+
(None, None, None) => {
413+
if validate_token {
414+
bail!("no upload token found, please run `cargo login` or pass `--token`");
415+
}
416+
None
417+
}
418+
// Token on command-line.
419+
(_, Some(_), _) => token,
420+
// Token in config, no --index, loading from config is OK for crates.io.
421+
(None, None, Some(_)) => {
422+
// Check `is_default_registry` so that the crates.io index can
423+
// change config.json's "api" value, and this won't affect most
424+
// people. It will affect those using source replacement, but
425+
// hopefully that's a relatively small set of users.
426+
if registry.is_none()
427+
&& !sid.is_default_registry()
428+
&& !crates_io::is_url_crates_io(&api_host)
429+
{
430+
if validate_token {
431+
config.shell().warn(
432+
"using `registry.token` config value with source \
433+
replacement is deprecated\n\
434+
This may become a hard error in the future; \
435+
see <https://github.com/rust-lang/cargo/issues/xxx>.\n\
436+
Use the --token command-line flag to remove this warning.",
437+
)?;
438+
token_config
439+
} else {
440+
None
441+
}
442+
} else {
443+
token_config
444+
}
445+
}
446+
// --index, no --token
447+
(Some(_), None, _) => {
448+
if validate_token {
449+
bail!("command-line argument --index requires --token to be specified")
450+
}
451+
None
452+
}
453+
};
429454
let handle = http_handle(config)?;
430455
Ok((Registry::new_handle(api_host, token, handle), sid))
431456
}
@@ -782,8 +807,8 @@ pub fn yank(
782807
/// If both are None, returns the source for crates.io.
783808
fn get_source_id(
784809
config: &Config,
785-
index: Option<String>,
786-
reg: Option<String>,
810+
index: Option<&String>,
811+
reg: Option<&String>,
787812
) -> CargoResult<SourceId> {
788813
match (reg, index) {
789814
(Some(r), _) => SourceId::alt_registry(config, &r),

tests/testsuite/publish.rs

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,9 @@ fn old_token_location() {
144144
.with_stderr(&format!(
145145
"\
146146
[UPDATING] `{reg}` index
147+
[WARNING] using `registry.token` config value with source replacement is deprecated
148+
This may become a hard error in the future[..]
149+
Use the --token command-line flag to remove this warning.
147150
[WARNING] manifest has no documentation, [..]
148151
See [..]
149152
[PACKAGING] foo v0.0.1 ([CWD])
@@ -1273,6 +1276,8 @@ fn index_requires_token() {
12731276
// --index will not load registry.token to avoid possibly leaking
12741277
// crates.io token to another server.
12751278
registry::init();
1279+
let credentials = paths::home().join(".cargo/credentials");
1280+
fs::remove_file(&credentials).unwrap();
12761281

12771282
let p = project()
12781283
.file(
@@ -1292,6 +1297,47 @@ fn index_requires_token() {
12921297
p.cargo("publish --no-verify --index")
12931298
.arg(registry_url().to_string())
12941299
.with_status(101)
1295-
.with_stderr("[ERROR] command-line argument --index requires --token to be specified")
1300+
.with_stderr(
1301+
"\
1302+
[UPDATING] [..]
1303+
[ERROR] command-line argument --index requires --token to be specified
1304+
",
1305+
)
1306+
.run();
1307+
}
1308+
1309+
#[cargo_test]
1310+
fn registry_token_with_source_replacement() {
1311+
// publish with source replacement without --token
1312+
registry::init();
1313+
1314+
let p = project()
1315+
.file(
1316+
"Cargo.toml",
1317+
r#"
1318+
[project]
1319+
name = "foo"
1320+
version = "0.0.1"
1321+
authors = []
1322+
license = "MIT"
1323+
description = "foo"
1324+
"#,
1325+
)
1326+
.file("src/lib.rs", "")
1327+
.build();
1328+
1329+
p.cargo("publish --no-verify")
1330+
.with_stderr(
1331+
"\
1332+
[UPDATING] [..]
1333+
[WARNING] using `registry.token` config value with source replacement is deprecated
1334+
This may become a hard error in the future[..]
1335+
Use the --token command-line flag to remove this warning.
1336+
[WARNING] manifest has no documentation, [..]
1337+
See [..]
1338+
[PACKAGING] foo v0.0.1 ([CWD])
1339+
[UPLOADING] foo v0.0.1 ([CWD])
1340+
",
1341+
)
12961342
.run();
12971343
}

0 commit comments

Comments
 (0)