From 58cd21449c2c6a20f8fb27eb101f7638049ad969 Mon Sep 17 00:00:00 2001 From: Leng Niu <344562942+vtxe02023274@users.noreply.github.com> Date: Sun, 29 Nov 2026 08:07:39 +0800 Subject: [PATCH] Match Digest challenge parameter names case-insensitively (#2702) --- httplib.h | 3 ++- test/test.cc | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index 1fb2fd5a..2934225b 100644 --- a/httplib.h +++ b/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; diff --git a/test/test.cc b/test/test.cc index 715f172c..0c17952f 100644 --- a/test/test.cc +++ b/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 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) {