mirror of
https://github.com/yhirose/cpp-httplib.git
synced 2026-11-20 19:51:54 +00:00
Refuse a bare host:port Location in ClientImpl::redirect (#2684)
ClientImpl::redirect() resolved a Location with resolve_relative_location(), which hands a value with a ':' in its first segment back unchanged as having a scheme, and then parsed it with parse_url(), which also accepts a bare host[:port] so that Client("localhost:8080") works. A Location such as "internal:8080" or "127.0.0.1:6379" therefore came out with an empty scheme and a host, slipped past the http/https check, and was followed as a cross-host redirect to that host and port. A value such as "localhost:8080/x" was sent to the same host as a path without a leading '/'.
Follow only http/https URLs and references starting with '/', and refuse anything else, as an unsupported scheme already was. Network-path references such as "//host:port/x" are still followed.
This commit is contained in:
1 parent
3544f47edd
commit
e7a004f509
2 files changed
+67
-7
No files matched your search
@@ -16601,14 +16601,13 @@ inline bool ClientImpl::redirect(Request &req, Response &res, Error &error) {
|
||||
auto location = res.get_header_value("location");
|
||||
if (location.empty()) { return false; }
|
||||
|
||||
detail::UrlComponents uc;
|
||||
if (!detail::parse_url(detail::resolve_relative_location(location, req.path),
|
||||
uc)) {
|
||||
return false;
|
||||
}
|
||||
auto next_url = detail::resolve_relative_location(location, req.path);
|
||||
|
||||
// Only follow http/https redirects
|
||||
if (!uc.scheme.empty() && uc.scheme != "http" && uc.scheme != "https") {
|
||||
detail::UrlComponents uc;
|
||||
if (!detail::parse_url(next_url, uc)) { return false; }
|
||||
|
||||
// Follow only http/https URLs and references starting with '/'
|
||||
if (next_url[0] != '/' && uc.scheme != "http" && uc.scheme != "https") {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@@ -4761,6 +4761,67 @@ TEST(RedirectToDifferentPort, TrailingCharactersInPort) {
|
||||
EXPECT_FALSE(res);
|
||||
}
|
||||
|
||||
TEST(RedirectToDifferentPort, BareHostAndPortIsNotFollowed) {
|
||||
Server target_svr;
|
||||
std::atomic<int> target_hits{0};
|
||||
target_svr.Get("/", [&](const Request & /*req*/, Response &res) {
|
||||
target_hits++;
|
||||
res.set_content("target", "text/plain");
|
||||
});
|
||||
target_svr.Get("/target", [&](const Request & /*req*/, Response &res) {
|
||||
res.set_content("target", "text/plain");
|
||||
});
|
||||
|
||||
int target_port = 0;
|
||||
auto target_thread = std::thread([&]() {
|
||||
target_port = target_svr.bind_to_any_port("127.0.0.1");
|
||||
target_svr.listen_after_bind();
|
||||
});
|
||||
|
||||
Server origin_svr;
|
||||
origin_svr.Get("/redir", [&](const Request &req, Response &res) {
|
||||
res.set_redirect(req.get_param_value("to"));
|
||||
});
|
||||
|
||||
int origin_port = 0;
|
||||
auto origin_thread = std::thread([&]() {
|
||||
origin_port = origin_svr.bind_to_any_port(HOST);
|
||||
origin_svr.listen_after_bind();
|
||||
});
|
||||
auto se = detail::scope_exit([&] {
|
||||
origin_svr.stop();
|
||||
origin_thread.join();
|
||||
target_svr.stop();
|
||||
target_thread.join();
|
||||
ASSERT_FALSE(origin_svr.is_running());
|
||||
ASSERT_FALSE(target_svr.is_running());
|
||||
});
|
||||
|
||||
target_svr.wait_until_ready();
|
||||
origin_svr.wait_until_ready();
|
||||
|
||||
auto port = std::to_string(target_port);
|
||||
|
||||
for (const auto &to :
|
||||
{"localhost:" + port, "127.0.0.1:" + port, "localhost:" + port + "/"}) {
|
||||
Client cli(HOST, origin_port);
|
||||
cli.set_follow_location(true);
|
||||
|
||||
auto res = cli.Get("/redir?to=" + encode_query_component(to));
|
||||
EXPECT_FALSE(res) << to;
|
||||
}
|
||||
EXPECT_EQ(0, target_hits);
|
||||
|
||||
Client cli(HOST, origin_port);
|
||||
cli.set_follow_location(true);
|
||||
|
||||
auto res = cli.Get("/redir?to=" +
|
||||
encode_query_component("//127.0.0.1:" + port + "/target"));
|
||||
ASSERT_TRUE(res) << to_string(res.error());
|
||||
EXPECT_EQ(StatusCode::OK_200, res->status);
|
||||
EXPECT_EQ("target", res->body);
|
||||
}
|
||||
|
||||
TEST(RedirectTest, OtherStatusesWithLocationAreNotFollowed) {
|
||||
Server svr;
|
||||
std::atomic<bool> followed{false};
|
||||
|
||||
Reference in new issue
Block a user