Skip to content

Commit 823ab52

Browse files
committed
Address review comments
* moved `is_empty` check into `check_token` * improved error message (is quite long now but should explain the error well) * removed one helper function from new test
1 parent 3d2e107 commit 823ab52

File tree

2 files changed

+32
-15
lines changed

2 files changed

+32
-15
lines changed

crates/crates-io/lib.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -518,16 +518,20 @@ pub fn is_url_crates_io(url: &str) -> bool {
518518
/// It would be easier to check just for alphanumeric tokens, but we can't be sure that all
519519
/// registries only create tokens in that format so that is as less restricted as possible.
520520
pub fn check_token(token: &str) -> Result<()> {
521-
let is_valid = token.bytes().all(|b| {
521+
if token.is_empty() {
522+
bail!("please provide a non-empty token");
523+
}
524+
if token.bytes().all(|b| {
522525
b >= 32 // undefined in ISO-8859-1, in ASCII/ UTF-8 not-printable character
523526
&& b < 128 // utf-8: the first bit signals a multi-byte character
524527
&& b != 127 // 127 is a control character in ascii and not in ISO 8859-1
525528
|| b == b't' // tab is also allowed (even when < 32)
526-
});
527-
528-
if is_valid {
529+
}) {
529530
Ok(())
530531
} else {
531-
Err(anyhow::anyhow!("invalid token."))
532+
Err(anyhow::anyhow!(
533+
"token contains invalid characters.\nOnly printable ISO-8859-1 characters \
534+
are allowed as it is sent in a HTTPS header."
535+
))
532536
}
533537
}

tests/testsuite/login.rs

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ fn invalid_login_token() {
134134
.build();
135135
setup_new_credentials();
136136

137-
let check_ = |stdin: &str, stderr: &str| {
137+
let check = |stdin: &str, stderr: &str| {
138138
cargo_process("login")
139139
.replace_crates_io(registry.index_url())
140140
.with_stdout("please paste the token found on [..]/me below")
@@ -143,19 +143,32 @@ fn invalid_login_token() {
143143
.with_status(101)
144144
.run();
145145
};
146-
let check = |stdin: &str| {
147-
check_(stdin, "[ERROR] invalid token.");
148-
};
149-
// first check updates index so it must be handled differently
150-
check_(
146+
147+
check(
151148
"😄",
152149
"\
153150
[UPDATING] crates.io index
154-
[ERROR] invalid token.",
151+
[ERROR] token contains invalid characters.
152+
Only printable ISO-8859-1 characters are allowed as it is sent in a HTTPS header.",
153+
);
154+
check(
155+
"\u{0016}",
156+
"\
157+
[ERROR] token contains invalid characters.
158+
Only printable ISO-8859-1 characters are allowed as it is sent in a HTTPS header.",
159+
);
160+
check(
161+
"\u{0000}",
162+
"\
163+
[ERROR] token contains invalid characters.
164+
Only printable ISO-8859-1 characters are allowed as it is sent in a HTTPS header.",
165+
);
166+
check(
167+
"你好",
168+
"\
169+
[ERROR] token contains invalid characters.
170+
Only printable ISO-8859-1 characters are allowed as it is sent in a HTTPS header.",
155171
);
156-
check("\u{0016}");
157-
check("\u{0000}");
158-
check("你好");
159172
}
160173

161174
#[cargo_test]

0 commit comments

Comments
 (0)