mirror of
https://github.com/yhirose/cpp-httplib.git
synced 2026-11-29 21:18:29 +00:00
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:
1 parent
e22f398cf2
commit
c76e1b6a98
2 files changed
+14
-5
No files matched your search
@@ -11777,7 +11777,7 @@ inline bool parse_www_authenticate(const Response &res,
|
|||||||
// trimmed before its boundaries are inspected.
|
// trimmed before its boundaries are inspected.
|
||||||
auto key_part = trim_copy(
|
auto key_part = trim_copy(
|
||||||
eq_pos == std::string::npos ? segment : segment.substr(0, eq_pos));
|
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) {
|
if (space_pos != std::string::npos || eq_pos == std::string::npos) {
|
||||||
// "<scheme>[ <key>]" starts a new challenge.
|
// "<scheme>[ <key>]" starts a new challenge.
|
||||||
take_challenge_if_answerable();
|
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.
|
// Bare scheme (or a token68), no auth-param on this segment.
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
key_part = key_part.substr(space_pos + 1);
|
key_part = trim_copy(key_part.substr(space_pos + 1));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!in_digest_challenge) { return; }
|
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) {
|
inline std::string get_bearer_token_auth(const Request &req) {
|
||||||
// The auth scheme is case-insensitive (RFC 9110 11.1), and a value shorter
|
// The auth scheme is case-insensitive (RFC 9110 11.1) and followed by 1*SP,
|
||||||
// than the prefix carries no token.
|
// and a value shorter than the prefix carries no token.
|
||||||
constexpr const char bearer_prefix[] = "Bearer ";
|
constexpr const char bearer_prefix[] = "Bearer ";
|
||||||
constexpr auto bearer_prefix_len = detail::str_len(bearer_prefix);
|
constexpr auto bearer_prefix_len = detail::str_len(bearer_prefix);
|
||||||
auto value = req.get_header_value("Authorization");
|
auto value = req.get_header_value("Authorization");
|
||||||
if (value.size() >= bearer_prefix_len &&
|
if (value.size() >= bearer_prefix_len &&
|
||||||
detail::case_ignore::equal(value.substr(0, bearer_prefix_len),
|
detail::case_ignore::equal(value.substr(0, bearer_prefix_len),
|
||||||
bearer_prefix)) {
|
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 "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2341,6 +2341,7 @@ TEST(BearerTokenAuthTest, SchemeValidation) {
|
|||||||
{"Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==", ""},
|
{"Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==", ""},
|
||||||
{"Bearer abc123", "abc123"},
|
{"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) {
|
for (const auto &c : cases) {
|
||||||
@@ -4042,6 +4043,13 @@ TEST(DigestAuthTest, RealmContainingCommaIsNotSplit) {
|
|||||||
"test,realm");
|
"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.
|
// RFC 9110 11.2: auth-param names are matched case-insensitively.
|
||||||
TEST(DigestAuthTest, ParamNamesAreCaseInsensitive) {
|
TEST(DigestAuthTest, ParamNamesAreCaseInsensitive) {
|
||||||
auto authorization = get_digest_retry_authorization(
|
auto authorization = get_digest_retry_authorization(
|
||||||
|
|||||||
Reference in new issue
Block a user