Bladeren bron

Match Digest challenge parameter names case-insensitively (#2702)

Leng Niu 2 dagen geleden
bovenliggende
commit
58cd21449c
2 gewijzigde bestanden met toevoegingen van 41 en 1 verwijderingen
  1. 2 1
      httplib.h
  2. 39 0
      test/test.cc

+ 2 - 1
httplib.h

@@ -11282,7 +11282,8 @@ inline bool parse_www_authenticate(const Response &res,
         if (unquoted.size() != val.size()) {
           unquoted = unescape_quoted_pairs(unquoted);
         }
-        auth[std::move(key_part)] = std::move(unquoted);
+        // auth-param names are case-insensitive (RFC 9110 Section 11.2)
+        auth[case_ignore::to_lower(key_part)] = std::move(unquoted);
       });
 
   // RFC 7616 Section 3.3 requires realm and nonce on every Digest challenge;

+ 39 - 0
test/test.cc

@@ -4034,6 +4034,45 @@ TEST(DigestAuthTest, RealmContainingCommaIsNotSplit) {
       "test,realm");
 }
 
+// RFC 9110 11.2: auth-param names are matched case-insensitively.
+TEST(DigestAuthTest, ParamNamesAreCaseInsensitive) {
+  std::atomic<int> hits{0};
+  std::string authorization;
+
+  Server svr;
+  svr.Get("/x", [&](const Request &req, Response &res) {
+    if (++hits == 1) {
+      res.status = StatusCode::Unauthorized_401;
+      res.set_header("WWW-Authenticate",
+                     "Digest Realm=\"testrealm\", QOP=\"auth\", "
+                     "Nonce=\"abc123\", Algorithm=SHA-256, OPAQUE=\"op\"");
+    } else {
+      authorization = req.get_header_value("Authorization");
+      res.set_content("ok", "text/plain");
+    }
+  });
+
+  auto port = svr.bind_to_any_port(HOST);
+  std::thread t([&]() { svr.listen_after_bind(); });
+  auto se = detail::scope_exit([&] {
+    svr.stop();
+    t.join();
+  });
+  svr.wait_until_ready();
+
+  Client cli(HOST, port);
+  cli.set_digest_auth("hello", "world");
+  auto res = cli.Get("/x");
+  ASSERT_TRUE(res) << "Error: " << to_string(res.error());
+  EXPECT_EQ(2, hits.load());
+
+  EXPECT_NE(std::string::npos, authorization.find("realm=\"testrealm\""));
+  EXPECT_NE(std::string::npos, authorization.find("nonce=\"abc123\""));
+  EXPECT_NE(std::string::npos, authorization.find("algorithm=SHA-256"));
+  EXPECT_NE(std::string::npos, authorization.find("qop=auth"));
+  EXPECT_NE(std::string::npos, authorization.find("opaque=\"op\""));
+}
+
 // RFC 7616 3.4: uri is the request-target as sent, which is encoded, carries
 // the params and leaves out the fragment.
 TEST(DigestAuthTest, UriIsTheRequestTargetSent) {