Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 1 | // Copyright 2020 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "system-proxy/proxy_connect_job.h" |
| 6 | |
| 7 | #include <algorithm> |
| 8 | #include <utility> |
| 9 | #include <vector> |
| 10 | |
| 11 | #include <curl/curl.h> |
| 12 | #include <curl/easy.h> |
| 13 | |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 14 | #include <base/base64.h> |
| 15 | #include <base/bind.h> |
| 16 | #include <base/bind_helpers.h> |
| 17 | #include <base/callback_helpers.h> |
| 18 | #include <base/files/file_util.h> |
| 19 | #include <base/strings/stringprintf.h> |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 20 | #include <base/strings/string_util.h> |
| 21 | #include <base/time/time.h> |
Andreea Costinas | 08a5d18 | 2020-04-29 22:12:47 +0200 | [diff] [blame] | 22 | #include <base/threading/thread.h> |
| 23 | #include <base/threading/thread_task_runner_handle.h> |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 24 | #include <brillo/http/http_transport.h> |
Garrick Evans | cd8c297 | 2020-04-14 14:35:52 +0900 | [diff] [blame] | 25 | #include <chromeos/patchpanel/net_util.h> |
| 26 | #include <chromeos/patchpanel/socket.h> |
| 27 | #include <chromeos/patchpanel/socket_forwarder.h> |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 28 | |
| 29 | #include "system-proxy/curl_socket.h" |
Andreea Costinas | 90b7164 | 2020-06-12 10:18:25 +0200 | [diff] [blame] | 30 | #include "system-proxy/http_util.h" |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 31 | |
Garrick Evans | 2d5e7c9 | 2020-06-08 14:14:28 +0900 | [diff] [blame] | 32 | // The libpatchpanel-util library overloads << for socket data structures. |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 33 | // By C++'s argument-dependent lookup rules, operators defined in a |
| 34 | // different namespace are not visible. We need the using directive to make |
| 35 | // the overload available this namespace. |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 36 | using patchpanel::operator<<; |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 37 | |
| 38 | namespace { |
| 39 | // There's no RFC recomandation for the max size of http request headers but |
| 40 | // popular http server implementations (Apache, IIS, Tomcat) set the lower limit |
| 41 | // to 8000. |
| 42 | constexpr int kMaxHttpRequestHeadersSize = 8000; |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 43 | constexpr base::TimeDelta kCurlConnectTimeout = base::TimeDelta::FromMinutes(2); |
Andreea Costinas | 08a5d18 | 2020-04-29 22:12:47 +0200 | [diff] [blame] | 44 | constexpr base::TimeDelta kWaitClientConnectTimeout = |
| 45 | base::TimeDelta::FromMinutes(2); |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 46 | constexpr size_t kMaxBadRequestPrintSize = 120; |
| 47 | |
| 48 | // HTTP error codes and messages with origin information for debugging (RFC723, |
| 49 | // section 6.1). |
| 50 | const std::string_view kHttpBadRequest = |
| 51 | "HTTP/1.1 400 Bad Request - Origin: local proxy\r\n\r\n"; |
Andreea Costinas | 08a5d18 | 2020-04-29 22:12:47 +0200 | [diff] [blame] | 52 | const std::string_view kHttpConnectionTimeout = |
| 53 | "HTTP/1.1 408 Request Timeout - Origin: local proxy\r\n\r\n"; |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 54 | const std::string_view kHttpInternalServerError = |
| 55 | "HTTP/1.1 500 Internal Server Error - Origin: local proxy\r\n\r\n"; |
| 56 | const std::string_view kHttpBadGateway = |
| 57 | "HTTP/1.1 502 Bad Gateway - Origin: local proxy\r\n\r\n"; |
| 58 | |
Andreea Costinas | 90b7164 | 2020-06-12 10:18:25 +0200 | [diff] [blame] | 59 | } // namespace |
Andreea Costinas | a224659 | 2020-04-12 23:24:01 +0200 | [diff] [blame] | 60 | |
Andreea Costinas | 90b7164 | 2020-06-12 10:18:25 +0200 | [diff] [blame] | 61 | namespace system_proxy { |
Andreea Costinas | a224659 | 2020-04-12 23:24:01 +0200 | [diff] [blame] | 62 | // CURLOPT_HEADERFUNCTION callback implementation that only returns the headers |
| 63 | // from the last response sent by the sever. This is to make sure that we |
| 64 | // send back valid HTTP replies and auhentication data from the HTTP messages is |
| 65 | // not being leaked to the client. |userdata| is set on the libcurl CURL handle |
| 66 | // used to configure the request, using the the CURLOPT_HEADERDATA option. Note, |
| 67 | // from the libcurl documentation: This callback is being called for all the |
| 68 | // responses received from the proxy server after intiating the connection |
| 69 | // request. Multiple responses can be received in an authentication sequence. |
| 70 | // Only the last response's headers should be forwarded to the System-proxy |
| 71 | // client. The header callback will be called once for each header and only |
| 72 | // complete header lines are passed on to the callback. |
| 73 | static size_t WriteHeadersCallback(char* contents, |
| 74 | size_t size, |
| 75 | size_t nmemb, |
| 76 | void* userdata) { |
| 77 | std::vector<char>* vec = (std::vector<char>*)userdata; |
| 78 | |
| 79 | // Check if we are receiving a new HTTP message (after the last one was |
| 80 | // terminated with an empty line). |
Andreea Costinas | 90b7164 | 2020-06-12 10:18:25 +0200 | [diff] [blame] | 81 | if (IsEndingWithHttpEmptyLine(base::StringPiece(vec->data(), vec->size()))) { |
Andreea Costinas | a224659 | 2020-04-12 23:24:01 +0200 | [diff] [blame] | 82 | VLOG(1) << "Removing the http reply headers from the server " |
| 83 | << base::StringPiece(vec->data(), vec->size()); |
| 84 | vec->clear(); |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 85 | } |
Andreea Costinas | a224659 | 2020-04-12 23:24:01 +0200 | [diff] [blame] | 86 | vec->insert(vec->end(), contents, contents + (nmemb * size)); |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 87 | return size * nmemb; |
| 88 | } |
| 89 | |
Andreea Costinas | a224659 | 2020-04-12 23:24:01 +0200 | [diff] [blame] | 90 | // CONNECT requests may have a reply body. This method will capture the reply |
| 91 | // and save it in |userdata|. |userdata| is set on the libcurl CURL handle |
| 92 | // used to configure the request, using the the CURLOPT_WRITEDATA option. |
| 93 | static size_t WriteCallback(char* contents, |
| 94 | size_t size, |
| 95 | size_t nmemb, |
| 96 | void* userdata) { |
| 97 | std::vector<char>* vec = (std::vector<char>*)userdata; |
| 98 | vec->insert(vec->end(), contents, contents + (nmemb * size)); |
| 99 | return size * nmemb; |
| 100 | } |
| 101 | |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 102 | ProxyConnectJob::ProxyConnectJob( |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 103 | std::unique_ptr<patchpanel::Socket> socket, |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 104 | const std::string& credentials, |
| 105 | ResolveProxyCallback resolve_proxy_callback, |
| 106 | OnConnectionSetupFinishedCallback setup_finished_callback) |
| 107 | : credentials_(credentials), |
| 108 | resolve_proxy_callback_(std::move(resolve_proxy_callback)), |
Andreea Costinas | 08a5d18 | 2020-04-29 22:12:47 +0200 | [diff] [blame] | 109 | setup_finished_callback_(std::move(setup_finished_callback)), |
| 110 | // Safe to use |base::Unretained| because the callback will be canceled |
| 111 | // when it goes out of scope. |
| 112 | client_connect_timeout_callback_(base::Bind( |
| 113 | &ProxyConnectJob::OnClientConnectTimeout, base::Unretained(this))) { |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 114 | client_socket_ = std::move(socket); |
| 115 | } |
| 116 | |
| 117 | ProxyConnectJob::~ProxyConnectJob() = default; |
| 118 | |
| 119 | bool ProxyConnectJob::Start() { |
| 120 | // Make the socket non-blocking. |
| 121 | if (!base::SetNonBlocking(client_socket_->fd())) { |
| 122 | PLOG(ERROR) << *this << " Failed to mark the socket as non-blocking."; |
| 123 | client_socket_->SendTo(kHttpInternalServerError.data(), |
| 124 | kHttpInternalServerError.size()); |
| 125 | return false; |
| 126 | } |
Andreea Costinas | 08a5d18 | 2020-04-29 22:12:47 +0200 | [diff] [blame] | 127 | base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| 128 | FROM_HERE, client_connect_timeout_callback_.callback(), |
| 129 | kWaitClientConnectTimeout); |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 130 | read_watcher_ = base::FileDescriptorWatcher::WatchReadable( |
Andreea Costinas | 833eb7c | 2020-06-12 11:09:15 +0200 | [diff] [blame^] | 131 | client_socket_->fd(), base::Bind(&ProxyConnectJob::OnClientReadReady, |
| 132 | weak_ptr_factory_.GetWeakPtr())); |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 133 | return true; |
| 134 | } |
| 135 | |
| 136 | void ProxyConnectJob::OnClientReadReady() { |
Andreea Costinas | 08a5d18 | 2020-04-29 22:12:47 +0200 | [diff] [blame] | 137 | if (!read_watcher_) { |
| 138 | // The connection has timed out while waiting for the client's HTTP CONNECT |
| 139 | // request. See |OnClientConnectTimeout|. |
| 140 | return; |
| 141 | } |
| 142 | client_connect_timeout_callback_.Cancel(); |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 143 | // Stop watching. |
| 144 | read_watcher_.reset(); |
| 145 | // The first message should be a HTTP CONNECT request. |
| 146 | std::vector<char> connect_request; |
| 147 | if (!TryReadHttpHeader(&connect_request)) { |
| 148 | std::string encoded; |
| 149 | base::Base64Encode( |
| 150 | base::StringPiece(connect_request.data(), connect_request.size()), |
| 151 | &encoded); |
| 152 | LOG(ERROR) << *this |
| 153 | << " Failure to read proxy CONNECT request. Base 64 encoded " |
| 154 | "request message from client: " |
| 155 | << encoded; |
| 156 | OnError(kHttpBadRequest); |
| 157 | return; |
| 158 | } |
Andreea Costinas | 90b7164 | 2020-06-12 10:18:25 +0200 | [diff] [blame] | 159 | base::StringPiece request(connect_request.data(), connect_request.size()); |
| 160 | target_url_ = GetUriAuthorityFromHttpHeader(request); |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 161 | if (target_url_.empty()) { |
| 162 | LOG(ERROR) |
| 163 | << *this |
| 164 | << " Failed to extract target url from the HTTP CONNECT request."; |
| 165 | OnError(kHttpBadRequest); |
| 166 | return; |
| 167 | } |
| 168 | |
Andreea Costinas | a89309d | 2020-05-08 15:51:12 +0200 | [diff] [blame] | 169 | // The proxy resolution service in Chrome expects a proper URL, formatted as |
| 170 | // scheme://host:port. It's safe to assume only https will be used for the |
| 171 | // target url. |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 172 | std::move(resolve_proxy_callback_) |
Andreea Costinas | a89309d | 2020-05-08 15:51:12 +0200 | [diff] [blame] | 173 | .Run(base::StringPrintf("https://%s", target_url_.c_str()), |
| 174 | base::Bind(&ProxyConnectJob::OnProxyResolution, |
Andreea Costinas | 833eb7c | 2020-06-12 11:09:15 +0200 | [diff] [blame^] | 175 | weak_ptr_factory_.GetWeakPtr())); |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | bool ProxyConnectJob::TryReadHttpHeader(std::vector<char>* raw_request) { |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 179 | size_t read_byte_count = 0; |
| 180 | raw_request->resize(kMaxHttpRequestHeadersSize); |
| 181 | |
| 182 | // Read byte-by-byte and stop when reading an empty line (only CRLF) or when |
| 183 | // exceeding the max buffer size. |
| 184 | // TODO(acostinas, chromium:1064536) This may have some measurable performance |
| 185 | // impact. We should read larger blocks of data, consume the HTTP headers, |
| 186 | // cache the tunneled payload that may have already been included (e.g. TLS |
| 187 | // ClientHello) and send it to server after the connection is established. |
| 188 | while (read_byte_count < kMaxHttpRequestHeadersSize) { |
| 189 | if (client_socket_->RecvFrom(raw_request->data() + read_byte_count, 1) <= |
| 190 | 0) { |
| 191 | raw_request->resize(std::min(read_byte_count, kMaxBadRequestPrintSize)); |
| 192 | return false; |
| 193 | } |
| 194 | ++read_byte_count; |
| 195 | |
Andreea Costinas | 90b7164 | 2020-06-12 10:18:25 +0200 | [diff] [blame] | 196 | if (IsEndingWithHttpEmptyLine( |
| 197 | base::StringPiece(raw_request->data(), read_byte_count))) { |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 198 | raw_request->resize(read_byte_count); |
| 199 | return true; |
| 200 | } |
| 201 | } |
| 202 | return false; |
| 203 | } |
| 204 | |
| 205 | void ProxyConnectJob::OnProxyResolution( |
| 206 | const std::list<std::string>& proxy_servers) { |
| 207 | proxy_servers_ = proxy_servers; |
| 208 | DoCurlServerConnection(proxy_servers.front()); |
| 209 | } |
| 210 | |
| 211 | void ProxyConnectJob::DoCurlServerConnection(const std::string& proxy_url) { |
| 212 | CURL* easyhandle = curl_easy_init(); |
| 213 | CURLcode res; |
Andreea Costinas | a224659 | 2020-04-12 23:24:01 +0200 | [diff] [blame] | 214 | curl_socket_t newSocket = -1; |
| 215 | std::vector<char> server_header_reply, server_body_reply; |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 216 | |
| 217 | if (!easyhandle) { |
| 218 | // Unfortunately it's not possible to get the failure reason. |
| 219 | LOG(ERROR) << *this << " Failure to create curl handle."; |
| 220 | curl_easy_cleanup(easyhandle); |
| 221 | OnError(kHttpInternalServerError); |
| 222 | return; |
| 223 | } |
| 224 | curl_easy_setopt(easyhandle, CURLOPT_URL, target_url_.c_str()); |
| 225 | |
| 226 | if (proxy_url != brillo::http::kDirectProxy) { |
| 227 | curl_easy_setopt(easyhandle, CURLOPT_PROXY, proxy_url.c_str()); |
| 228 | curl_easy_setopt(easyhandle, CURLOPT_HTTPPROXYTUNNEL, 1L); |
| 229 | curl_easy_setopt(easyhandle, CURLOPT_CONNECT_ONLY, 1); |
| 230 | // Allow libcurl to pick authentication method. Curl will use the most |
| 231 | // secure one the remote site claims to support. |
| 232 | curl_easy_setopt(easyhandle, CURLOPT_PROXYAUTH, CURLAUTH_ANY); |
| 233 | curl_easy_setopt(easyhandle, CURLOPT_PROXYUSERPWD, credentials_.c_str()); |
| 234 | } |
| 235 | curl_easy_setopt(easyhandle, CURLOPT_CONNECTTIMEOUT_MS, |
| 236 | kCurlConnectTimeout.InMilliseconds()); |
Andreea Costinas | a224659 | 2020-04-12 23:24:01 +0200 | [diff] [blame] | 237 | curl_easy_setopt(easyhandle, CURLOPT_HEADERFUNCTION, WriteHeadersCallback); |
| 238 | curl_easy_setopt(easyhandle, CURLOPT_HEADERDATA, &server_header_reply); |
| 239 | curl_easy_setopt(easyhandle, CURLOPT_WRITEFUNCTION, WriteCallback); |
| 240 | curl_easy_setopt(easyhandle, CURLOPT_WRITEDATA, &server_body_reply); |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 241 | |
| 242 | res = curl_easy_perform(easyhandle); |
| 243 | |
| 244 | if (res != CURLE_OK) { |
Andreea Costinas | 90b7164 | 2020-06-12 10:18:25 +0200 | [diff] [blame] | 245 | LOG(ERROR) << *this << " curl_easy_perform() failed with error: " |
| 246 | << curl_easy_strerror(res); |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 247 | curl_easy_cleanup(easyhandle); |
Andreea Costinas | a224659 | 2020-04-12 23:24:01 +0200 | [diff] [blame] | 248 | |
| 249 | if (server_header_reply.size() > 0) { |
| 250 | // Send the error message from the remote server back to the client. |
| 251 | OnError(std::string_view(server_header_reply.data(), |
| 252 | server_header_reply.size())); |
| 253 | } else { |
| 254 | OnError(kHttpInternalServerError); |
| 255 | } |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 256 | return; |
| 257 | } |
| 258 | // Extract the socket from the curl handle. |
| 259 | res = curl_easy_getinfo(easyhandle, CURLINFO_ACTIVESOCKET, &newSocket); |
| 260 | if (res != CURLE_OK) { |
| 261 | LOG(ERROR) << *this << " Failed to get socket from curl with error: " |
| 262 | << curl_easy_strerror(res); |
| 263 | curl_easy_cleanup(easyhandle); |
| 264 | OnError(kHttpBadGateway); |
| 265 | return; |
| 266 | } |
| 267 | |
| 268 | ScopedCurlEasyhandle scoped_handle(easyhandle, FreeCurlEasyhandle()); |
| 269 | auto server_conn = std::make_unique<CurlSocket>(base::ScopedFD(newSocket), |
| 270 | std::move(scoped_handle)); |
| 271 | |
| 272 | // Send the server reply to the client. If the connection is successful, the |
Andreea Costinas | a224659 | 2020-04-12 23:24:01 +0200 | [diff] [blame] | 273 | // reply headers should be "HTTP/1.1 200 Connection Established". |
| 274 | if (client_socket_->SendTo(server_header_reply.data(), |
| 275 | server_header_reply.size()) != |
| 276 | server_header_reply.size()) { |
| 277 | PLOG(ERROR) << *this << " Failed to send HTTP reply headers to client: " |
| 278 | << base::StringPiece(server_header_reply.data(), |
| 279 | server_header_reply.size()); |
| 280 | OnError(kHttpInternalServerError); |
| 281 | return; |
| 282 | } |
| 283 | // HTTP CONNECT responses can have a payload body which should be forwarded to |
| 284 | // the client. |
| 285 | if (server_body_reply.size() > 0) { |
| 286 | // TODO(acostinas, chromium:1064536) Resend the reply body in case of EAGAIN |
| 287 | // or EWOULDBLOCK errors. |
| 288 | if (client_socket_->SendTo(server_body_reply.data(), |
| 289 | server_body_reply.size()) != |
| 290 | server_body_reply.size()) { |
| 291 | PLOG(ERROR) << *this |
Andreea Costinas | 08a5d18 | 2020-04-29 22:12:47 +0200 | [diff] [blame] | 292 | << " Failed to send HTTP CONNECT reply body to client: " |
Andreea Costinas | a224659 | 2020-04-12 23:24:01 +0200 | [diff] [blame] | 293 | << base::StringPiece(server_body_reply.data(), |
| 294 | server_body_reply.size()); |
| 295 | } |
| 296 | } |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 297 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 298 | auto fwd = std::make_unique<patchpanel::SocketForwarder>( |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 299 | base::StringPrintf("%d-%d", client_socket_->fd(), server_conn->fd()), |
| 300 | std::move(client_socket_), std::move(server_conn)); |
| 301 | // Start forwarding data between sockets. |
| 302 | fwd->Start(); |
| 303 | std::move(setup_finished_callback_).Run(std::move(fwd), this); |
| 304 | } |
| 305 | |
| 306 | void ProxyConnectJob::OnError(const std::string_view& http_error_message) { |
| 307 | client_socket_->SendTo(http_error_message.data(), http_error_message.size()); |
| 308 | std::move(setup_finished_callback_).Run(nullptr, this); |
| 309 | } |
| 310 | |
Andreea Costinas | 08a5d18 | 2020-04-29 22:12:47 +0200 | [diff] [blame] | 311 | void ProxyConnectJob::OnClientConnectTimeout() { |
| 312 | // Stop listening for client connect requests. |
| 313 | read_watcher_.reset(); |
| 314 | LOG(ERROR) << *this |
| 315 | << " Connection timed out while waiting for the client to send a " |
| 316 | "connect request."; |
| 317 | OnError(kHttpConnectionTimeout); |
| 318 | } |
| 319 | |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 320 | std::ostream& operator<<(std::ostream& stream, const ProxyConnectJob& job) { |
| 321 | stream << "{fd: " << job.client_socket_->fd(); |
| 322 | if (!job.target_url_.empty()) { |
| 323 | stream << ", url: " << job.target_url_; |
| 324 | } |
| 325 | stream << "}"; |
| 326 | return stream; |
| 327 | } |
| 328 | |
| 329 | } // namespace system_proxy |