Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions httplib.h
Original file line number Diff line number Diff line change
Expand Up @@ -8636,7 +8636,7 @@ inline bool ClientImpl::redirect(Request &req, Response &res, Error &error) {
if (location.empty()) { return false; }

thread_local const std::regex re(
R"((?:(https?):)?(?://(?:\[([a-fA-F\d:]+)\]|([^:/?#]+))(?::(\d+))?)?([^?#]*)(\?[^#]*)?(?:#.*)?)");
R"((?:(https?):)?(?://(?:\[([a-fA-F\d:]+)\]|([^:/?#]+))(?::(\d+))?)?([^?#]*)(?:\?([^#]*))?(?:#.*)?)");

std::smatch m;
if (!std::regex_match(location, m, re)) { return false; }
Expand All @@ -8661,16 +8661,16 @@ inline bool ClientImpl::redirect(Request &req, Response &res, Error &error) {
if (next_host.empty()) { next_host = host_; }
if (next_path.empty()) { next_path = "/"; }

auto path = detail::decode_path(next_path, true) + next_query;
if (!next_query.empty()) { detail::parse_query_text(next_query, req.params); }

// Same host redirect - use current client
if (next_scheme == scheme && next_host == host_ && next_port == port_) {
return detail::redirect(*this, req, res, path, location, error);
return detail::redirect(*this, req, res, next_path, location, error);
}

// Cross-host/scheme redirect - create new client with robust setup
return create_redirect_client(next_scheme, next_host, next_port, req, res,
path, location, error);
next_path, location, error);
}

// New method for robust redirect client creation
Expand Down
31 changes: 31 additions & 0 deletions test/test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9942,6 +9942,37 @@ TEST(RedirectTest, RedirectToUrlWithQueryParameters) {
}
}

TEST(RedirectTest, RedirectToUrlWithPlusInQueryParameters) {
Server svr;

svr.Get("/", [](const Request & /*req*/, Response &res) {
res.set_redirect(R"(/hello?key=AByz09+~-._%20%26%3F%C3%BC%2B)");
});

svr.Get("/hello", [](const Request &req, Response &res) {
res.set_content(req.get_param_value("key"), "text/plain");
});

auto thread = std::thread([&]() { svr.listen(HOST, PORT); });
auto se = detail::scope_exit([&] {
svr.stop();
thread.join();
ASSERT_FALSE(svr.is_running());
});

svr.wait_until_ready();

{
Client cli(HOST, PORT);
cli.set_follow_location(true);

auto res = cli.Get("/");
ASSERT_TRUE(res);
EXPECT_EQ(StatusCode::OK_200, res->status);
EXPECT_EQ("AByz09 ~-._ &?ü+", res->body);
}
}

TEST(VulnerabilityTest, CRLFInjection) {
Server svr;

Expand Down
Loading