Skip to content

Commit 57264ed

Browse files
committed
Fix credential token format validation.
1 parent 1b2de21 commit 57264ed

File tree

2 files changed

+32
-26
lines changed

2 files changed

+32
-26
lines changed

crates/crates-io/lib.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -522,10 +522,11 @@ pub fn check_token(token: &str) -> Result<()> {
522522
bail!("please provide a non-empty token");
523523
}
524524
if token.bytes().all(|b| {
525-
b >= 32 // undefined in ISO-8859-1, in ASCII/ UTF-8 not-printable character
526-
&& b < 128 // utf-8: the first bit signals a multi-byte character
527-
&& b != 127 // 127 is a control character in ascii and not in ISO 8859-1
528-
|| b == b't' // tab is also allowed (even when < 32)
525+
// This is essentially the US-ASCII limitation of
526+
// https://www.rfc-editor.org/rfc/rfc9110#name-field-values. That is,
527+
// visible ASCII characters (0x21-0x7e), space, and tab. We want to be
528+
// able to pass this in an HTTP header without encoding.
529+
b >= 32 && b < 127 || b == b'\t'
529530
}) {
530531
Ok(())
531532
} else {

tests/testsuite/login.rs

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -134,40 +134,45 @@ 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, status: i32| {
138138
cargo_process("login")
139139
.replace_crates_io(registry.index_url())
140140
.with_stdout("please paste the token found on [..]/me below")
141141
.with_stdin(stdin)
142142
.with_stderr(stderr)
143-
.with_status(101)
143+
.with_status(status)
144144
.run();
145145
};
146146

147-
check(
148-
"😄",
149-
"\
150-
[UPDATING] crates.io index
151-
[ERROR] token contains invalid characters.
147+
let invalid = |stdin: &str| {
148+
check(
149+
stdin,
150+
"[ERROR] token contains invalid characters.
152151
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-
);
152+
101,
153+
)
154+
};
155+
let valid = |stdin: &str| check(stdin, "[LOGIN] token for `crates.io` saved", 0);
156+
157+
// Update config.json so that the rest of the tests don't need to care
158+
// whether or not `Updating` is printed.
160159
check(
161-
"\u{0000}",
160+
"test",
162161
"\
163-
[ERROR] token contains invalid characters.
164-
Only printable ISO-8859-1 characters are allowed as it is sent in a HTTPS header.",
162+
[UPDATING] crates.io index
163+
[LOGIN] token for `crates.io` saved
164+
",
165+
0,
165166
);
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.",
167+
168+
invalid("😄");
169+
invalid("\u{0016}");
170+
invalid("\u{0000}");
171+
invalid("你好");
172+
valid("foo\tbar");
173+
valid("foo bar");
174+
valid(
175+
r##"!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~"##,
171176
);
172177
}
173178

0 commit comments

Comments
 (0)