From c76e1b6a98ae937c43277ed69f5d151202572ecd Mon Sep 17 00:00:00 2001 From: yhirose Date: Sat, 28 Nov 2026 20:47:13 -0500 Subject: [PATCH] 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. --- httplib.h | 11 ++++++----- test/test.cc | 10 +++++++++- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/httplib.h b/httplib.h index 6129bc28..3c58ef04 100644 --- a/httplib.h +++ b/httplib.h @@ -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) { // "[ ]" 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 ""; } diff --git a/test/test.cc b/test/test.cc index 5fc14cd2..640965cc 100644 --- a/test/test.cc +++ b/test/test.cc @@ -2340,7 +2340,8 @@ TEST(BearerTokenAuthTest, SchemeValidation) { {"x", ""}, {"Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==", ""}, {"Bearer abc123", "abc123"}, - {"bearer abc123", "abc123"}, // scheme match is case-insensitive + {"bearer abc123", "abc123"}, // scheme match is case-insensitive + {"Bearer abc123", "abc123"}, // 1*SP after the scheme }; for (const auto &c : cases) { @@ -4042,6 +4043,13 @@ TEST(DigestAuthTest, RealmContainingCommaIsNotSplit) { "test,realm"); } +// RFC 9110 11.6.1: 1*SP separates the scheme from its first auth-param. +TEST(DigestAuthTest, SeveralSpacesAfterScheme) { + auto authorization = + get_digest_retry_authorization({"Digest realm=\"r\", nonce=\"n\""}); + EXPECT_NE(std::string::npos, authorization.find("nonce=\"n\"")); +} + // RFC 9110 11.2: auth-param names are matched case-insensitively. TEST(DigestAuthTest, ParamNamesAreCaseInsensitive) { auto authorization = get_digest_retry_authorization(