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;
|
||||
}
|
||||
|
||||
// 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.
|
||||
|
||||
Reference in new issue
Block a user