ソースを参照

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.
yhirose 2 日 前
親
コミット
e22f398cf2
2 ファイル変更、58 行追加、31 行削除
  1. 37 25
      httplib.h
  2. 21 6
      test/test.cc

+ 37 - 25
httplib.h

@@ -11703,6 +11703,23 @@ inline DigestHash digest_hash(const std::string &algorithm) {
   return nullptr;
 }
 
+// Picks auth over auth-int from the qop options a server offers, as curl does:
+// auth-int hashes req.body, which is empty when a content provider streams the
+// body. Empty when it offers neither.
+inline std::string select_digest_qop(const std::string &options) {
+  std::string qop;
+  split(options.data(), options.data() + options.size(), ',',
+        [&](const char *b, const char *e) {
+          std::string option(b, e);
+          if (case_ignore::equal(option, "auth")) {
+            qop = "auth";
+          } else if (qop.empty() && case_ignore::equal(option, "auth-int")) {
+            qop = "auth-int";
+          }
+        });
+  return qop;
+}
+
 // RFC 9110 Section 11.6.1 defines a challenge list as
 //   WWW-Authenticate = #challenge
 //   challenge        = auth-scheme [ 1*SP ( token68 / [ #auth-param ] ) ]
@@ -11726,21 +11743,26 @@ inline bool parse_www_authenticate(const Response &res,
   // RFC 7616 Section 3.7: a server may offer more than one Digest challenge
   // (e.g. SHA-256 and MD5), and the client uses the first one it supports.
   // A challenge whose algorithm the client doesn't implement is ignored
-  // (Section 3.3), and one without realm and nonce can't be answered. Params
-  // are collected per challenge, so a nonce from one is never paired with
-  // another's algorithm.
+  // (Section 3.3), as is one whose qop offers neither auth nor auth-int, and
+  // one without realm and nonce can't be answered. Params are collected per
+  // challenge, so a nonce from one is never paired with another's algorithm.
+  auto answerable = [](const std::map<std::string, std::string> &c) {
+    if (c.find("realm") == c.end() || c.find("nonce") == c.end()) {
+      return false;
+    }
+    auto algo_it = c.find("algorithm");
+    if (algo_it != c.end() && !digest_hash(algo_it->second)) { return false; }
+    auto qop_it = c.find("qop");
+    return qop_it == c.end() || !select_digest_qop(qop_it->second).empty();
+  };
+
   auto found = false;
   auto in_digest_challenge = false;
   std::map<std::string, std::string> challenge;
   auto take_challenge_if_answerable = [&] {
-    if (!found && in_digest_challenge &&
-        challenge.find("realm") != challenge.end() &&
-        challenge.find("nonce") != challenge.end()) {
-      auto it = challenge.find("algorithm");
-      if (it == challenge.end() || digest_hash(it->second)) {
-        auth = std::move(challenge);
-        found = true;
-      }
+    if (!found && in_digest_challenge && answerable(challenge)) {
+      auth = std::move(challenge);
+      found = true;
     }
     challenge.clear();
   };
@@ -11799,21 +11821,11 @@ inline std::pair<std::string, std::string> make_digest_authentication_header(
     nc = ss.str();
   }
 
-  // auth-int is preferred over auth; other qop options are ignored.
-  std::string qop;
+  // Without qop, the RFC 2069 form is used.
   auto qop_it = auth.find("qop");
-  if (qop_it != auth.end()) {
-    const auto &options = qop_it->second;
-    split(options.data(), options.data() + options.size(), ',',
-          [&](const char *b, const char *e) {
-            std::string option(b, e);
-            if (case_ignore::equal(option, "auth-int")) {
-              qop = "auth-int";
-            } else if (qop.empty() && case_ignore::equal(option, "auth")) {
-              qop = "auth";
-            }
-          });
-  }
+  auto qop =
+      qop_it == auth.end() ? std::string() : select_digest_qop(qop_it->second);
+  assert(qop_it == auth.end() || !qop.empty());
 
   // Sent back as the server wrote it (RFC 7616 Section 3.4).
   // parse_www_authenticate() accepts only one digest_hash() knows.

+ 21 - 6
test/test.cc

@@ -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