mirror of
https://github.com/yhirose/cpp-httplib.git
synced 2026-10-19 11:19:36 +00:00
Sign the request-target sent in a Digest uri
The Digest uri and A2 took the raw path, while the request line carried it encoded, with the params and without the fragment, so a server comparing the two rejected the credentials. Build the target in one place for both.
This commit is contained in:
1 parent
f29721c7d8
commit
bf08ec763c
2 files changed
+69
-28
No files matched your search
@@ -3552,6 +3552,40 @@ TEST(DigestAuthTest, RealmContainingCommaIsNotSplit) {
|
||||
"test,realm");
|
||||
}
|
||||
|
||||
// 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) {
|
||||
Server svr;
|
||||
std::string uri, target;
|
||||
svr.Get("/a b", [&](const Request &req, Response &res) {
|
||||
auto auth = req.get_header_value("Authorization");
|
||||
if (auth.empty()) {
|
||||
res.status = StatusCode::Unauthorized_401;
|
||||
res.set_header("WWW-Authenticate",
|
||||
"Digest realm=\"r\", qop=\"auth\", nonce=\"n\"");
|
||||
return;
|
||||
}
|
||||
auto b = auth.find("uri=\"") + 5;
|
||||
uri = auth.substr(b, auth.find('"', b) - b);
|
||||
target = req.target;
|
||||
});
|
||||
|
||||
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("/a b#f", Params{{"x", "1"}}, Headers{});
|
||||
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
|
||||
EXPECT_EQ("/a%20b?x=1", target);
|
||||
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
|
||||
|
||||
Reference in new issue
Block a user