Skip to content

Commit 5afc53a

Browse files
committed
Auto merge of #13770 - arlosi:cred-trim-newlines, r=epage
fix(credential): trim newlines in tokens from stdin ### What does this PR try to resolve? `cargo login` when using a credential provider other than `cargo:token` does not automatically trim whitespace from tokens. This can lead to extra whitespace being included in the pasted token value (usually a trailing newline) that makes the token invalid. ### How should we test and review this PR? First commit adds a test showing the problematic behavior. Second commit fixes it.
2 parents 852a316 + 6207f93 commit 5afc53a

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

src/cargo/ops/registry/login.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ pub fn registry_login(
3939
let mut token_from_stdin = None;
4040
let token = token_from_cmdline.or_else(|| {
4141
if !std::io::stdin().is_terminal() {
42-
let token = std::io::read_to_string(std::io::stdin()).unwrap_or_default();
42+
let token = cargo_credential::read_line().unwrap_or_default();
4343
if !token.is_empty() {
4444
token_from_stdin = Some(token);
4545
}

tests/testsuite/credential_process.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -695,3 +695,22 @@ fn alias_builtin_warning() {
695695
)
696696
.run();
697697
}
698+
699+
#[cargo_test]
700+
fn login_token_from_stdin() {
701+
// Test reading a token from stdin, ensuring newlines are trimmed.
702+
let registry = registry::RegistryBuilder::new()
703+
.no_configure_token()
704+
.credential_provider(&[&build_provider("test-cred", r#"{"Ok": {"kind": "login"}}"#)])
705+
.build();
706+
707+
cargo_process("login")
708+
.replace_crates_io(registry.index_url())
709+
.with_stdin("abcdefg\n")
710+
.with_stderr(
711+
r#"[UPDATING] [..]
712+
{"v":1,"registry":{"index-url":"https://github.com/rust-lang/crates.io-index","name":"crates-io"},"kind":"login","token":"abcdefg","login-url":"[..]"}
713+
"#,
714+
)
715+
.run();
716+
}

tests/testsuite/login.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ fn empty_login_token() {
113113
.with_stderr(
114114
"\
115115
[UPDATING] crates.io index
116+
please paste the token found on [..] below
116117
[ERROR] credential provider `cargo:token` failed action `login`
117118
118119
Caused by:

0 commit comments

Comments
 (0)