Accept several spaces after the auth scheme

RFC 9110 allows 1*SP between the auth scheme and what follows it in both
challenges and credentials. parse_www_authenticate() took the scheme up
to the last whitespace before "=", so "Digest  realm=..." was not
recognized as Digest and the 401 was returned without a retry, and
get_bearer_token_auth() returned the token of "Bearer  abc" with a
leading space.

The scheme is now the text up to the first whitespace, with the key
after it trimmed, and get_bearer_token_auth() skips the extra spaces.
This commit is contained in:
yhirose committed 2026-11-28 20:47:13 -05:00
1 parent e22f398cf2
commit c76e1b6a98
2 files changed
+15 -6

No files matched your search

+6 -5
View File
@@ -11777,7 +11777,7 @@ inline bool parse_www_authenticate(const Response &res,
// trimmed before its boundaries are inspected.
auto key_part = trim_copy(
eq_pos == std::string::npos ? segment : segment.substr(0, eq_pos));
auto space_pos = key_part.find_last_of(" \t");
auto space_pos = key_part.find_first_of(" \t");
if (space_pos != std::string::npos || eq_pos == std::string::npos) {
// "<scheme>[ <key>]" starts a new challenge.
take_challenge_if_answerable();
@@ -11789,7 +11789,7 @@ inline bool parse_www_authenticate(const Response &res,
// Bare scheme (or a token68), no auth-param on this segment.
return;
}
key_part = key_part.substr(space_pos + 1);
key_part = trim_copy(key_part.substr(space_pos + 1));
}
if (!in_digest_challenge) { return; }
@@ -12330,15 +12330,16 @@ inline bool set_socket_opt(socket_t sock, int level, int optname, int optval) {
}
inline std::string get_bearer_token_auth(const Request &req) {
// The auth scheme is case-insensitive (RFC 9110 11.1), and a value shorter
// than the prefix carries no token.
// The auth scheme is case-insensitive (RFC 9110 11.1) and followed by 1*SP,
// and a value shorter than the prefix carries no token.
constexpr const char bearer_prefix[] = "Bearer ";
constexpr auto bearer_prefix_len = detail::str_len(bearer_prefix);
auto value = req.get_header_value("Authorization");
if (value.size() >= bearer_prefix_len &&
detail::case_ignore::equal(value.substr(0, bearer_prefix_len),
bearer_prefix)) {
return value.substr(bearer_prefix_len);
auto pos = value.find_first_not_of(' ', bearer_prefix_len);
if (pos != std::string::npos) { return value.substr(pos); }
}
return "";
}