mirror of
https://github.com/yhirose/cpp-httplib.git
synced 2026-11-17 19:30:54 +00:00
Close the tunnel when the TLS handshake through a proxy fails
SSLClient::setup_proxy_connection() left the socket open when the TLS handshake inside the CONNECT tunnel failed, so the client kept a connection with no TLS session on it. Only the next request's reuse check closed it, because tls::is_peer_closed() reports a missing session as closed. Close it where the handshake fails, as ensure_socket_connection() does for a direct connection.
This commit is contained in:
1 parent
f57672b390
commit
b23e16775d
2 files changed
+36
No files matched your search
@@ -19327,6 +19327,8 @@ inline bool SSLClient::setup_proxy_connection(
|
||||
}
|
||||
|
||||
if (!initialize_ssl(socket, error)) {
|
||||
shutdown_socket(socket);
|
||||
close_socket(socket);
|
||||
success = false;
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -32485,6 +32485,40 @@ TEST(ProxyTunnelTest, RedirectKeepsTunnelHostnameAddrMap) {
|
||||
EXPECT_NE(std::string::npos,
|
||||
connect_req.find("\r\nHost: " + target + "\r\n"));
|
||||
}
|
||||
|
||||
// A failed handshake inside the tunnel must not leave the tunnel for reuse
|
||||
TEST(ProxyTunnelTest, FailedHandshakeClosesTunnel) {
|
||||
std::atomic<int> origin_hits{0};
|
||||
proxy_tunnel_test::ScopedSSLServer origin;
|
||||
origin.svr().Get(".*", [&](const Request &, Response &res) {
|
||||
origin_hits++;
|
||||
res.set_content("ok", "text/plain");
|
||||
});
|
||||
origin.listen();
|
||||
|
||||
proxy_tunnel_test::ScopedConnectProxy proxy(origin.port());
|
||||
ASSERT_NE(0, proxy.port());
|
||||
|
||||
SSLClient cli("127.0.0.1", origin.port());
|
||||
cli.enable_server_certificate_verification(false);
|
||||
cli.set_proxy("127.0.0.1", proxy.port());
|
||||
cli.set_keep_alive(true);
|
||||
cli.set_session_verifier(
|
||||
[](tls::session_t) { return SSLVerifierResponse::CertificateRejected; });
|
||||
|
||||
auto res = cli.Get("/x");
|
||||
EXPECT_FALSE(res);
|
||||
ASSERT_FALSE(cli.is_socket_open());
|
||||
|
||||
res = cli.Get("/x");
|
||||
EXPECT_FALSE(res);
|
||||
EXPECT_EQ(2, proxy.connect_hits());
|
||||
|
||||
auto handle = cli.open_stream("GET", "/x");
|
||||
EXPECT_FALSE(handle.is_valid());
|
||||
EXPECT_EQ(3, proxy.connect_hits());
|
||||
EXPECT_EQ(0, origin_hits.load());
|
||||
}
|
||||
#endif
|
||||
|
||||
namespace proxy_tunnel_test {
|
||||
|
||||
Reference in new issue
Block a user