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
@@ -11703,6 +11703,23 @@ inline DigestHash digest_hash(const std::string &algorithm) {
|
|||||||
return nullptr;
|
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
|
// RFC 9110 Section 11.6.1 defines a challenge list as
|
||||||
// WWW-Authenticate = #challenge
|
// WWW-Authenticate = #challenge
|
||||||
// challenge = auth-scheme [ 1*SP ( token68 / [ #auth-param ] ) ]
|
// 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
|
// 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.
|
// (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
|
// 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
|
// (Section 3.3), as is one whose qop offers neither auth nor auth-int, and
|
||||||
// are collected per challenge, so a nonce from one is never paired with
|
// one without realm and nonce can't be answered. Params are collected per
|
||||||
// another's algorithm.
|
// 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 found = false;
|
||||||
auto in_digest_challenge = false;
|
auto in_digest_challenge = false;
|
||||||
std::map<std::string, std::string> challenge;
|
std::map<std::string, std::string> challenge;
|
||||||
auto take_challenge_if_answerable = [&] {
|
auto take_challenge_if_answerable = [&] {
|
||||||
if (!found && in_digest_challenge &&
|
if (!found && in_digest_challenge && answerable(challenge)) {
|
||||||
challenge.find("realm") != challenge.end() &&
|
auth = std::move(challenge);
|
||||||
challenge.find("nonce") != challenge.end()) {
|
found = true;
|
||||||
auto it = challenge.find("algorithm");
|
|
||||||
if (it == challenge.end() || digest_hash(it->second)) {
|
|
||||||
auth = std::move(challenge);
|
|
||||||
found = true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
challenge.clear();
|
challenge.clear();
|
||||||
};
|
};
|
||||||
@@ -11799,21 +11821,11 @@ inline std::pair<std::string, std::string> make_digest_authentication_header(
|
|||||||
nc = ss.str();
|
nc = ss.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
// auth-int is preferred over auth; other qop options are ignored.
|
// Without qop, the RFC 2069 form is used.
|
||||||
std::string qop;
|
|
||||||
auto qop_it = auth.find("qop");
|
auto qop_it = auth.find("qop");
|
||||||
if (qop_it != auth.end()) {
|
auto qop =
|
||||||
const auto &options = qop_it->second;
|
qop_it == auth.end() ? std::string() : select_digest_qop(qop_it->second);
|
||||||
split(options.data(), options.data() + options.size(), ',',
|
assert(qop_it == auth.end() || !qop.empty());
|
||||||
[&](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";
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// Sent back as the server wrote it (RFC 7616 Section 3.4).
|
// Sent back as the server wrote it (RFC 7616 Section 3.4).
|
||||||
// parse_www_authenticate() accepts only one digest_hash() knows.
|
// parse_www_authenticate() accepts only one digest_hash() knows.
|
||||||
|
|||||||
+21
-6
@@ -4067,15 +4067,23 @@ TEST(DigestAuthTest, AlgorithmValueIsCaseInsensitive) {
|
|||||||
EXPECT_EQ(64u, authorization.find('"', b) - b); // SHA-256, not MD5
|
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) {
|
TEST(DigestAuthTest, QopValueIsCaseInsensitive) {
|
||||||
auto authorization = get_digest_retry_authorization(
|
auto authorization = get_digest_retry_authorization(
|
||||||
{"Digest realm=\"r\", nonce=\"n\", qop=\"AUTH\""});
|
{"Digest realm=\"r\", nonce=\"n\", qop=\"AUTH\""});
|
||||||
EXPECT_NE(std::string::npos, authorization.find("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(
|
authorization = get_digest_retry_authorization(
|
||||||
{"Digest realm=\"r\", nonce=\"n\", qop=\"auth-conf\""});
|
{"Digest realm=\"r\", nonce=\"n\", qop=\"Auth-Int\""});
|
||||||
EXPECT_EQ(std::string::npos, authorization.find("qop="));
|
EXPECT_NE(std::string::npos, authorization.find("qop=auth-int,"));
|
||||||
}
|
}
|
||||||
|
|
||||||
// RFC 7616 3.7: the client uses the first Digest challenge it can answer.
|
// 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);
|
EXPECT_EQ(target, uri);
|
||||||
}
|
}
|
||||||
|
|
||||||
// A Digest challenge the client can't answer (realm or nonce missing, or an
|
// A Digest challenge the client can't answer (realm or nonce missing, or no
|
||||||
// algorithm it doesn't implement (RFC 7616 3.3)) must leave the 401 as it is,
|
// algorithm or qop option it implements) must leave the 401 as it is, without
|
||||||
// without a retry.
|
// a retry.
|
||||||
static void
|
static void
|
||||||
run_unanswerable_digest_challenge_test(const std::string &challenge) {
|
run_unanswerable_digest_challenge_test(const std::string &challenge) {
|
||||||
std::atomic<int> hits{0};
|
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.
|
// 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
|
// 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
|
// the header builder has to re-escape them; otherwise the value breaks out of
|
||||||
|
|||||||
Reference in new issue
Block a user