mirror of
https://github.com/yhirose/cpp-httplib.git
synced 2026-11-29 21:18:29 +00:00
Prefer Digest qop=auth and skip challenges without a usable qop
When a server offered both qop=auth and qop=auth-int, the client chose auth-int, which hashes req.body. That is empty when a content provider streams the body, so the response was wrong and the request failed. A challenge whose qop offered neither option (e.g. only auth-conf) was answered without qop, although RFC 7616 makes qop mandatory and has the client use the first challenge it supports. select_digest_qop() now picks auth over auth-int, as curl does, and parse_www_authenticate() treats a challenge whose qop offers neither as unanswerable, so a later challenge is used or the 401 is returned. A challenge without qop is still answered in the RFC 2069 form.
This commit is contained in:
1 parent
c5faf03dfe
commit
e22f398cf2
2 files changed
+58
-31
No files matched your search
+21
-6
@@ -4067,15 +4067,23 @@ TEST(DigestAuthTest, AlgorithmValueIsCaseInsensitive) {
|
||||
EXPECT_EQ(64u, authorization.find('"', b) - b); // SHA-256, not MD5
|
||||
}
|
||||
|
||||
// qop options are matched case-insensitively as whole tokens.
|
||||
// qop options are matched case-insensitively.
|
||||
TEST(DigestAuthTest, QopValueIsCaseInsensitive) {
|
||||
auto authorization = get_digest_retry_authorization(
|
||||
{"Digest realm=\"r\", nonce=\"n\", qop=\"AUTH\""});
|
||||
EXPECT_NE(std::string::npos, authorization.find("qop=auth,"));
|
||||
}
|
||||
|
||||
// auth-int hashes req.body, which is empty when a content provider streams the
|
||||
// body, so it is chosen only when the server offers no auth.
|
||||
TEST(DigestAuthTest, PrefersQopAuthOverAuthInt) {
|
||||
auto authorization = get_digest_retry_authorization(
|
||||
{"Digest realm=\"r\", nonce=\"n\", qop=\"auth-int, auth\""});
|
||||
EXPECT_NE(std::string::npos, authorization.find("qop=auth,"));
|
||||
|
||||
authorization = get_digest_retry_authorization(
|
||||
{"Digest realm=\"r\", nonce=\"n\", qop=\"auth-conf\""});
|
||||
EXPECT_EQ(std::string::npos, authorization.find("qop="));
|
||||
{"Digest realm=\"r\", nonce=\"n\", qop=\"Auth-Int\""});
|
||||
EXPECT_NE(std::string::npos, authorization.find("qop=auth-int,"));
|
||||
}
|
||||
|
||||
// RFC 7616 3.7: the client uses the first Digest challenge it can answer.
|
||||
@@ -4125,9 +4133,9 @@ TEST(DigestAuthTest, UriIsTheRequestTargetSent) {
|
||||
EXPECT_EQ(target, uri);
|
||||
}
|
||||
|
||||
// A Digest challenge the client can't answer (realm or nonce missing, or an
|
||||
// algorithm it doesn't implement (RFC 7616 3.3)) must leave the 401 as it is,
|
||||
// without a retry.
|
||||
// A Digest challenge the client can't answer (realm or nonce missing, or no
|
||||
// algorithm or qop option it implements) must leave the 401 as it is, without
|
||||
// a retry.
|
||||
static void
|
||||
run_unanswerable_digest_challenge_test(const std::string &challenge) {
|
||||
std::atomic<int> hits{0};
|
||||
@@ -4181,6 +4189,13 @@ TEST(DigestAuthTest, UnsupportedAlgorithmIsIgnored) {
|
||||
}
|
||||
}
|
||||
|
||||
// RFC 7616 3.3: qop options the client doesn't implement are ignored, and a
|
||||
// challenge that offers no other can't be answered.
|
||||
TEST(DigestAuthTest, UnsupportedQopIsIgnored) {
|
||||
run_unanswerable_digest_challenge_test(
|
||||
"Digest realm=\"r\", nonce=\"n\", qop=\"auth-conf\"");
|
||||
}
|
||||
|
||||
// A hostile server can put a '"' in realm/nonce/opaque in its challenge.
|
||||
// parse_www_authenticate() de-escapes quoted-pairs when storing the values, so
|
||||
// the header builder has to re-escape them; otherwise the value breaks out of
|
||||
|
||||
Reference in new issue
Block a user