Match Digest algorithm and qop values case-insensitively

The Digest client compared the algorithm and qop values of a challenge
case-sensitively, so algorithm=sha-256 was answered with an MD5 hash and
qop="AUTH" without qop. A challenge whose algorithm it doesn't implement
(SHA-512-256, the -sess variants, or a value that isn't a token) was
answered with an MD5 hash under the server's algorithm name, and only the
first Digest challenge was ever looked at.

- Match algorithm and qop options case-insensitively. qop is split into
  tokens instead of searched as a substring, and sent as auth/auth-int.
- Collect params per Digest challenge and use the first one the client
  can answer (RFC 7616 Sections 3.3 and 3.7): realm and nonce present and
  an algorithm it implements. If there is none, the 401 is returned
  without a retry.
- digest_hash() is the single list of supported algorithms, used by both
  parse_www_authenticate() and make_digest_authentication_header(). The
  algorithm is sent back as the server wrote it (RFC 7616 Section 3.4).
  parse_www_authenticate() moves into the SSL block, next to the hash
  functions and its callers.
This commit is contained in:
yhirose committed 2026-11-28 20:30:40 -05:00
1 parent 58cd21449c
commit c5faf03dfe
2 files changed
+211 -185

No files matched your search

+99 -94
View File
@@ -3963,17 +3963,13 @@ TEST(DigestAuthTest, FromHTTPWatch_Online) {
}
}
// RFC 9110 Section 11.6.1: a WWW-Authenticate field value is a
// comma-separated list of challenges, and each challenge may itself carry a
// comma-separated auth-param list, so a server can legally offer Basic
// before Digest, either as two field lines or packed into one. Runs one 401
// -> Digest-retry round trip with the given field lines (get_combined_header_
// value() joins them in receipt order) and checks that the retry carries a
// well-formed Digest Authorization header naming expected_realm.
static void
run_digest_challenge_list_test(const std::vector<std::string> &challenges,
const std::string &expected_realm) {
// Runs one 401 -> Digest-retry round trip with the given WWW-Authenticate
// field lines (get_combined_header_value() joins them in receipt order) and
// returns the Authorization header of the retry.
static std::string
get_digest_retry_authorization(const std::vector<std::string> &challenges) {
std::atomic<int> hits{0};
std::string authorization;
Server svr;
svr.Get("/x", [&](const Request &req, Response &res) {
@@ -3983,11 +3979,7 @@ run_digest_challenge_list_test(const std::vector<std::string> &challenges,
res.set_header("WWW-Authenticate", challenge);
}
} else {
auto authorization = req.get_header_value("Authorization");
EXPECT_EQ(0u, authorization.rfind("Digest ", 0));
EXPECT_NE(std::string::npos,
authorization.find("realm=\"" + expected_realm + "\""));
EXPECT_EQ(std::string::npos, authorization.find("Basic"));
authorization = req.get_header_value("Authorization");
res.set_content("ok", "text/plain");
}
});
@@ -4003,9 +3995,25 @@ run_digest_challenge_list_test(const std::vector<std::string> &challenges,
Client cli(HOST, port);
cli.set_digest_auth("hello", "world");
auto res = cli.Get("/x");
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
EXPECT_TRUE(res) << "Error: " << to_string(res.error());
EXPECT_EQ(2, hits.load());
return authorization;
}
// RFC 9110 Section 11.6.1: a WWW-Authenticate field value is a
// comma-separated list of challenges, and each challenge may itself carry a
// comma-separated auth-param list, so a server can legally offer Basic
// before Digest, either as two field lines or packed into one. Checks that
// the retry carries a well-formed Digest Authorization header naming
// expected_realm.
static void
run_digest_challenge_list_test(const std::vector<std::string> &challenges,
const std::string &expected_realm) {
auto authorization = get_digest_retry_authorization(challenges);
EXPECT_EQ(0u, authorization.rfind("Digest ", 0));
EXPECT_NE(std::string::npos,
authorization.find("realm=\"" + expected_realm + "\""));
EXPECT_EQ(std::string::npos, authorization.find("Basic"));
}
static const char *kBasicChallenge = "Basic realm=\"decoy\"";
@@ -4036,36 +4044,9 @@ TEST(DigestAuthTest, RealmContainingCommaIsNotSplit) {
// 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());
auto authorization = get_digest_retry_authorization(
{"Digest Realm=\"testrealm\", QOP=\"auth\", Nonce=\"abc123\", "
"Algorithm=SHA-256, OPAQUE=\"op\""});
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"));
@@ -4073,6 +4054,43 @@ TEST(DigestAuthTest, ParamNamesAreCaseInsensitive) {
EXPECT_NE(std::string::npos, authorization.find("opaque=\"op\""));
}
// The algorithm is matched case-insensitively and sent back as the server
// wrote it (RFC 7616 3.4).
TEST(DigestAuthTest, AlgorithmValueIsCaseInsensitive) {
auto authorization = get_digest_retry_authorization(
{"Digest realm=\"r\", nonce=\"n\", qop=\"auth\", algorithm=sha-256"});
EXPECT_NE(std::string::npos, authorization.find("algorithm=sha-256"));
const std::string prefix = "response=\"";
auto b = authorization.find(prefix);
ASSERT_NE(std::string::npos, b);
b += prefix.size();
EXPECT_EQ(64u, authorization.find('"', b) - b); // SHA-256, not MD5
}
// qop options are matched case-insensitively as whole tokens.
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,"));
authorization = get_digest_retry_authorization(
{"Digest realm=\"r\", nonce=\"n\", qop=\"auth-conf\""});
EXPECT_EQ(std::string::npos, authorization.find("qop="));
}
// RFC 7616 3.7: the client uses the first Digest challenge it can answer.
TEST(DigestAuthTest, SkipsUnanswerableDigestChallenges) {
auto authorization = get_digest_retry_authorization(
{"Digest realm=\"r\", nonce=\"n1\", algorithm=SHA-512-256, "
"Digest realm=\"r\", nonce=\"n2\", algorithm=SHA-256"});
EXPECT_NE(std::string::npos, authorization.find("nonce=\"n2\""));
EXPECT_NE(std::string::npos, authorization.find("algorithm=SHA-256"));
authorization = get_digest_retry_authorization(
{"Digest realm=\"r\", qop=\"auth\", Digest realm=\"r\", nonce=\"n2\""});
EXPECT_NE(std::string::npos, authorization.find("nonce=\"n2\""));
}
// 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) {
@@ -4107,13 +4125,16 @@ TEST(DigestAuthTest, UriIsTheRequestTargetSent) {
EXPECT_EQ(target, uri);
}
// RFC 7616 Section 3.3 requires realm and nonce on every Digest challenge.
// make_digest_authentication_header() dereferences both unconditionally, so
// a server sending a challenge missing either one must not be treated as
// usable -- doing so used to crash the client with std::out_of_range.
static void run_digest_challenge_missing_field_test(const char *challenge) {
// 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.
static void
run_unanswerable_digest_challenge_test(const std::string &challenge) {
std::atomic<int> hits{0};
Server svr;
svr.Get("/x", [&](const Request & /*req*/, Response &res) {
++hits;
res.status = StatusCode::Unauthorized_401;
res.set_header("WWW-Authenticate", challenge);
});
@@ -4131,65 +4152,49 @@ static void run_digest_challenge_missing_field_test(const char *challenge) {
auto res = cli.Get("/x");
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
EXPECT_EQ(StatusCode::Unauthorized_401, res->status);
EXPECT_EQ(1, hits.load());
}
TEST(DigestAuthTest, ChallengeMissingRealmAndNonceDoesNotCrash) {
run_digest_challenge_missing_field_test("Digest qop=\"auth\"");
run_unanswerable_digest_challenge_test("Digest qop=\"auth\"");
}
TEST(DigestAuthTest, ChallengeMissingNonceDoesNotCrash) {
run_digest_challenge_missing_field_test("Digest realm=\"r\", qop=\"auth\"");
run_unanswerable_digest_challenge_test("Digest realm=\"r\", qop=\"auth\"");
}
TEST(DigestAuthTest, ChallengeMissingRealmDoesNotCrash) {
run_digest_challenge_missing_field_test("Digest nonce=\"n\", qop=\"auth\"");
run_unanswerable_digest_challenge_test("Digest nonce=\"n\", qop=\"auth\"");
}
// A hostile server can put a '"' in realm/nonce/opaque, or a non-token
// algorithm, in its challenge. parse_www_authenticate() de-escapes quoted-pairs
// when storing the values, so the header builder has to re-escape them (and
// keep algorithm a bare token); otherwise the value breaks out of its
// quoted-string and injects extra auth-params into the client's Authorization.
// RFC 7616 3.3: a challenge with an algorithm the client doesn't implement is
// ignored. The last value isn't a token and would otherwise inject auth-params
// into the Authorization header.
TEST(DigestAuthTest, UnsupportedAlgorithmIsIgnored) {
for (auto algorithm :
{"SHA-512-256", "MD5-sess", "\"MD5, injected=\\\"y\\\"\""}) {
SCOPED_TRACE(algorithm);
run_unanswerable_digest_challenge_test(
std::string("Digest realm=\"r\", nonce=\"n\", qop=\"auth\", "
"algorithm=") +
algorithm);
}
}
// 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
// its quoted-string and injects extra auth-params into the Authorization.
TEST(DigestAuthTest, EscapesInjectedAuthParams) {
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;
// On the wire the quotes embedded in the values are backslash-escaped.
res.set_header("WWW-Authenticate",
"Digest realm=\"testrealm\", "
"nonce=\"n\\\"; injected=\\\"x\", "
"algorithm=\"MD5, injected2=\\\"y\\\"\", qop=\"auth\"");
} 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());
// On the wire the quotes embedded in the value are backslash-escaped.
auto authorization = get_digest_retry_authorization(
{"Digest realm=\"testrealm\", nonce=\"n\\\"; injected=\\\"x\", "
"qop=\"auth\""});
EXPECT_EQ(0u, authorization.rfind("Digest ", 0));
// The nonce (de-escaped to n"; injected="x ) must be re-escaped so it stays
// inside its quoted-string rather than starting an "injected" auth-param.
EXPECT_NE(std::string::npos,
authorization.find("nonce=\"n\\\"; injected=\\\"x\""));
// A non-token algorithm falls back to a bare MD5 token, dropping the payload.
EXPECT_EQ(std::string::npos, authorization.find("injected2"));
}
#endif