blob: 49d08b12a421f5df7c9dbc9c8a1a754e02982e8b [file] [log] [blame]
Andreea Costinase45d54b2020-03-10 09:21:14 +01001// 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
Andreea Costinase45d54b2020-03-10 09:21:14 +010011#include <curl/easy.h>
12
Andreea Costinase45d54b2020-03-10 09:21:14 +010013#include <base/base64.h>
14#include <base/bind.h>
15#include <base/bind_helpers.h>
16#include <base/callback_helpers.h>
17#include <base/files/file_util.h>
18#include <base/strings/stringprintf.h>
Andreea Costinase45d54b2020-03-10 09:21:14 +010019#include <base/strings/string_util.h>
20#include <base/time/time.h>
Andreea Costinas08a5d182020-04-29 22:12:47 +020021#include <base/threading/thread.h>
22#include <base/threading/thread_task_runner_handle.h>
Andreea Costinase45d54b2020-03-10 09:21:14 +010023#include <brillo/http/http_transport.h>
Garrick Evanscd8c2972020-04-14 14:35:52 +090024#include <chromeos/patchpanel/net_util.h>
25#include <chromeos/patchpanel/socket.h>
26#include <chromeos/patchpanel/socket_forwarder.h>
Andreea Costinase45d54b2020-03-10 09:21:14 +010027
28#include "system-proxy/curl_socket.h"
Andreea Costinas90b71642020-06-12 10:18:25 +020029#include "system-proxy/http_util.h"
Andreea Costinase45d54b2020-03-10 09:21:14 +010030
Garrick Evans2d5e7c92020-06-08 14:14:28 +090031// The libpatchpanel-util library overloads << for socket data structures.
Andreea Costinase45d54b2020-03-10 09:21:14 +010032// By C++'s argument-dependent lookup rules, operators defined in a
33// different namespace are not visible. We need the using directive to make
34// the overload available this namespace.
Garrick Evans3388a032020-03-24 11:25:55 +090035using patchpanel::operator<<;
Andreea Costinase45d54b2020-03-10 09:21:14 +010036
37namespace {
38// There's no RFC recomandation for the max size of http request headers but
39// popular http server implementations (Apache, IIS, Tomcat) set the lower limit
40// to 8000.
41constexpr int kMaxHttpRequestHeadersSize = 8000;
Andreea Costinas350e4aa2020-07-20 20:29:46 +020042constexpr base::TimeDelta kCurlConnectTimeout =
43 base::TimeDelta::FromSeconds(30);
Andreea Costinas08a5d182020-04-29 22:12:47 +020044constexpr base::TimeDelta kWaitClientConnectTimeout =
Andreea Costinas350e4aa2020-07-20 20:29:46 +020045 base::TimeDelta::FromSeconds(15);
Andreea Costinase45d54b2020-03-10 09:21:14 +010046constexpr size_t kMaxBadRequestPrintSize = 120;
47
Andreea Costinasf90a4c02020-06-12 22:30:51 +020048constexpr int64_t kHttpCodeProxyAuthRequired = 407;
49
Andreea Costinase45d54b2020-03-10 09:21:14 +010050// HTTP error codes and messages with origin information for debugging (RFC723,
51// section 6.1).
52const std::string_view kHttpBadRequest =
53 "HTTP/1.1 400 Bad Request - Origin: local proxy\r\n\r\n";
Andreea Costinas08a5d182020-04-29 22:12:47 +020054const std::string_view kHttpConnectionTimeout =
55 "HTTP/1.1 408 Request Timeout - Origin: local proxy\r\n\r\n";
Andreea Costinase45d54b2020-03-10 09:21:14 +010056const std::string_view kHttpInternalServerError =
57 "HTTP/1.1 500 Internal Server Error - Origin: local proxy\r\n\r\n";
58const std::string_view kHttpBadGateway =
59 "HTTP/1.1 502 Bad Gateway - Origin: local proxy\r\n\r\n";
Andreea Costinasf90a4c02020-06-12 22:30:51 +020060const std::string_view kHttpProxyAuthRequired =
61 "HTTP/1.1 407 Credentials required - Origin: local proxy\r\n\r\n";
62constexpr char kHttpErrorTunnelFailed[] =
63 "HTTP/1.1 %s Error creating tunnel - Origin: local proxy\r\n\r\n";
Andreea Costinas90b71642020-06-12 10:18:25 +020064} // namespace
Andreea Costinasa2246592020-04-12 23:24:01 +020065
Andreea Costinas90b71642020-06-12 10:18:25 +020066namespace system_proxy {
Andreea Costinasa2246592020-04-12 23:24:01 +020067// CURLOPT_HEADERFUNCTION callback implementation that only returns the headers
68// from the last response sent by the sever. This is to make sure that we
69// send back valid HTTP replies and auhentication data from the HTTP messages is
70// not being leaked to the client. |userdata| is set on the libcurl CURL handle
71// used to configure the request, using the the CURLOPT_HEADERDATA option. Note,
72// from the libcurl documentation: This callback is being called for all the
73// responses received from the proxy server after intiating the connection
74// request. Multiple responses can be received in an authentication sequence.
75// Only the last response's headers should be forwarded to the System-proxy
76// client. The header callback will be called once for each header and only
77// complete header lines are passed on to the callback.
78static size_t WriteHeadersCallback(char* contents,
79 size_t size,
80 size_t nmemb,
81 void* userdata) {
82 std::vector<char>* vec = (std::vector<char>*)userdata;
83
84 // Check if we are receiving a new HTTP message (after the last one was
85 // terminated with an empty line).
Andreea Costinas90b71642020-06-12 10:18:25 +020086 if (IsEndingWithHttpEmptyLine(base::StringPiece(vec->data(), vec->size()))) {
Andreea Costinasa2246592020-04-12 23:24:01 +020087 VLOG(1) << "Removing the http reply headers from the server "
88 << base::StringPiece(vec->data(), vec->size());
89 vec->clear();
Andreea Costinase45d54b2020-03-10 09:21:14 +010090 }
Andreea Costinasa2246592020-04-12 23:24:01 +020091 vec->insert(vec->end(), contents, contents + (nmemb * size));
Andreea Costinase45d54b2020-03-10 09:21:14 +010092 return size * nmemb;
93}
94
Andreea Costinasa2246592020-04-12 23:24:01 +020095// CONNECT requests may have a reply body. This method will capture the reply
96// and save it in |userdata|. |userdata| is set on the libcurl CURL handle
97// used to configure the request, using the the CURLOPT_WRITEDATA option.
98static size_t WriteCallback(char* contents,
99 size_t size,
100 size_t nmemb,
101 void* userdata) {
102 std::vector<char>* vec = (std::vector<char>*)userdata;
103 vec->insert(vec->end(), contents, contents + (nmemb * size));
104 return size * nmemb;
105}
106
Andreea Costinase45d54b2020-03-10 09:21:14 +0100107ProxyConnectJob::ProxyConnectJob(
Garrick Evans3388a032020-03-24 11:25:55 +0900108 std::unique_ptr<patchpanel::Socket> socket,
Andreea Costinase45d54b2020-03-10 09:21:14 +0100109 const std::string& credentials,
110 ResolveProxyCallback resolve_proxy_callback,
Andreea Costinasbb2aa022020-06-13 00:03:23 +0200111 AuthenticationRequiredCallback auth_required_callback,
Andreea Costinase45d54b2020-03-10 09:21:14 +0100112 OnConnectionSetupFinishedCallback setup_finished_callback)
113 : credentials_(credentials),
Andreea Costinasbb2aa022020-06-13 00:03:23 +0200114 first_http_connect_attempt_(true),
Andreea Costinase45d54b2020-03-10 09:21:14 +0100115 resolve_proxy_callback_(std::move(resolve_proxy_callback)),
Andreea Costinasbb2aa022020-06-13 00:03:23 +0200116 auth_required_callback_(std::move(auth_required_callback)),
Andreea Costinas08a5d182020-04-29 22:12:47 +0200117 setup_finished_callback_(std::move(setup_finished_callback)),
118 // Safe to use |base::Unretained| because the callback will be canceled
119 // when it goes out of scope.
120 client_connect_timeout_callback_(base::Bind(
121 &ProxyConnectJob::OnClientConnectTimeout, base::Unretained(this))) {
Andreea Costinase45d54b2020-03-10 09:21:14 +0100122 client_socket_ = std::move(socket);
123}
124
125ProxyConnectJob::~ProxyConnectJob() = default;
126
127bool ProxyConnectJob::Start() {
128 // Make the socket non-blocking.
129 if (!base::SetNonBlocking(client_socket_->fd())) {
130 PLOG(ERROR) << *this << " Failed to mark the socket as non-blocking.";
131 client_socket_->SendTo(kHttpInternalServerError.data(),
132 kHttpInternalServerError.size());
133 return false;
134 }
Andreea Costinas08a5d182020-04-29 22:12:47 +0200135 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
136 FROM_HERE, client_connect_timeout_callback_.callback(),
137 kWaitClientConnectTimeout);
Andreea Costinase45d54b2020-03-10 09:21:14 +0100138 read_watcher_ = base::FileDescriptorWatcher::WatchReadable(
Andreea Costinas833eb7c2020-06-12 11:09:15 +0200139 client_socket_->fd(), base::Bind(&ProxyConnectJob::OnClientReadReady,
140 weak_ptr_factory_.GetWeakPtr()));
Andreea Costinase45d54b2020-03-10 09:21:14 +0100141 return true;
142}
143
144void ProxyConnectJob::OnClientReadReady() {
Andreea Costinas08a5d182020-04-29 22:12:47 +0200145 if (!read_watcher_) {
146 // The connection has timed out while waiting for the client's HTTP CONNECT
147 // request. See |OnClientConnectTimeout|.
148 return;
149 }
150 client_connect_timeout_callback_.Cancel();
Andreea Costinase45d54b2020-03-10 09:21:14 +0100151 // Stop watching.
152 read_watcher_.reset();
153 // The first message should be a HTTP CONNECT request.
154 std::vector<char> connect_request;
155 if (!TryReadHttpHeader(&connect_request)) {
156 std::string encoded;
157 base::Base64Encode(
158 base::StringPiece(connect_request.data(), connect_request.size()),
159 &encoded);
160 LOG(ERROR) << *this
161 << " Failure to read proxy CONNECT request. Base 64 encoded "
162 "request message from client: "
163 << encoded;
164 OnError(kHttpBadRequest);
165 return;
166 }
Andreea Costinas90b71642020-06-12 10:18:25 +0200167 base::StringPiece request(connect_request.data(), connect_request.size());
168 target_url_ = GetUriAuthorityFromHttpHeader(request);
Andreea Costinase45d54b2020-03-10 09:21:14 +0100169 if (target_url_.empty()) {
170 LOG(ERROR)
171 << *this
172 << " Failed to extract target url from the HTTP CONNECT request.";
173 OnError(kHttpBadRequest);
174 return;
175 }
176
Andreea Costinasa89309d2020-05-08 15:51:12 +0200177 // The proxy resolution service in Chrome expects a proper URL, formatted as
178 // scheme://host:port. It's safe to assume only https will be used for the
179 // target url.
Andreea Costinase45d54b2020-03-10 09:21:14 +0100180 std::move(resolve_proxy_callback_)
Andreea Costinasa89309d2020-05-08 15:51:12 +0200181 .Run(base::StringPrintf("https://%s", target_url_.c_str()),
182 base::Bind(&ProxyConnectJob::OnProxyResolution,
Andreea Costinas833eb7c2020-06-12 11:09:15 +0200183 weak_ptr_factory_.GetWeakPtr()));
Andreea Costinase45d54b2020-03-10 09:21:14 +0100184}
185
186bool ProxyConnectJob::TryReadHttpHeader(std::vector<char>* raw_request) {
Andreea Costinase45d54b2020-03-10 09:21:14 +0100187 size_t read_byte_count = 0;
188 raw_request->resize(kMaxHttpRequestHeadersSize);
189
190 // Read byte-by-byte and stop when reading an empty line (only CRLF) or when
191 // exceeding the max buffer size.
192 // TODO(acostinas, chromium:1064536) This may have some measurable performance
193 // impact. We should read larger blocks of data, consume the HTTP headers,
194 // cache the tunneled payload that may have already been included (e.g. TLS
195 // ClientHello) and send it to server after the connection is established.
196 while (read_byte_count < kMaxHttpRequestHeadersSize) {
197 if (client_socket_->RecvFrom(raw_request->data() + read_byte_count, 1) <=
198 0) {
199 raw_request->resize(std::min(read_byte_count, kMaxBadRequestPrintSize));
200 return false;
201 }
202 ++read_byte_count;
203
Andreea Costinas90b71642020-06-12 10:18:25 +0200204 if (IsEndingWithHttpEmptyLine(
205 base::StringPiece(raw_request->data(), read_byte_count))) {
Andreea Costinase45d54b2020-03-10 09:21:14 +0100206 raw_request->resize(read_byte_count);
207 return true;
208 }
209 }
210 return false;
211}
212
213void ProxyConnectJob::OnProxyResolution(
214 const std::list<std::string>& proxy_servers) {
215 proxy_servers_ = proxy_servers;
Andreea Costinasbb2aa022020-06-13 00:03:23 +0200216 DoCurlServerConnection();
Andreea Costinase45d54b2020-03-10 09:21:14 +0100217}
218
Andreea Costinasbb2aa022020-06-13 00:03:23 +0200219void ProxyConnectJob::AuthenticationRequired(
220 const std::vector<char>& http_response_headers) {
221 DCHECK(!proxy_servers_.empty());
222 SchemeRealmPairList scheme_realm_pairs = ParseAuthChallenge(base::StringPiece(
223 http_response_headers.data(), http_response_headers.size()));
224 if (scheme_realm_pairs.empty()) {
225 LOG(ERROR) << "Failed to parse authentication challenge";
226 OnError(kHttpBadGateway);
227 return;
228 }
229
230 std::move(auth_required_callback_)
231 .Run(proxy_servers_.front(), scheme_realm_pairs.front().first,
232 scheme_realm_pairs.front().second,
233 base::Bind(&ProxyConnectJob::OnAuthCredentialsProvided,
234 base::Unretained(this)));
235}
236
237void ProxyConnectJob::OnAuthCredentialsProvided(
238 const std::string& credentials) {
239 if (credentials.empty()) {
240 SendHttpResponseToClient(/* http_response_headers= */ {},
241 /* http_response_body= */ {});
242 std::move(setup_finished_callback_).Run(nullptr, this);
243 return;
244 }
245 credentials_ = credentials;
246 VLOG(1) << "Connecting to the remote server with provided credentials";
247 DoCurlServerConnection();
248}
249
250bool ProxyConnectJob::AreAuthCredentialsRequired(CURL* easyhandle) {
251 if (http_response_code_ != kHttpCodeProxyAuthRequired) {
252 return false;
253 }
254
255 CURLcode res;
256 int64_t server_proxy_auth_scheme = 0;
257 res = curl_easy_getinfo(easyhandle, CURLINFO_PROXYAUTH_AVAIL,
258 &server_proxy_auth_scheme);
259 if (res != CURLE_OK || !server_proxy_auth_scheme) {
260 return false;
261 }
262
263 // If kerberos is enabled, then we need to wait for the user to request a
264 // kerberos ticket from Chrome.
265 return !(server_proxy_auth_scheme & CURLAUTH_NEGOTIATE);
266}
267
268void ProxyConnectJob::DoCurlServerConnection() {
269 DCHECK(!proxy_servers_.empty());
Andreea Costinase45d54b2020-03-10 09:21:14 +0100270 CURL* easyhandle = curl_easy_init();
271 CURLcode res;
Andreea Costinasa2246592020-04-12 23:24:01 +0200272 curl_socket_t newSocket = -1;
Andreea Costinase45d54b2020-03-10 09:21:14 +0100273
274 if (!easyhandle) {
275 // Unfortunately it's not possible to get the failure reason.
276 LOG(ERROR) << *this << " Failure to create curl handle.";
277 curl_easy_cleanup(easyhandle);
278 OnError(kHttpInternalServerError);
279 return;
280 }
281 curl_easy_setopt(easyhandle, CURLOPT_URL, target_url_.c_str());
Andreea Costinasf90a4c02020-06-12 22:30:51 +0200282 std::vector<char> http_response_headers;
283 std::vector<char> http_response_body;
Andreea Costinasbb2aa022020-06-13 00:03:23 +0200284
285 if (proxy_servers_.front().c_str() != brillo::http::kDirectProxy) {
286 curl_easy_setopt(easyhandle, CURLOPT_PROXY, proxy_servers_.front().c_str());
Andreea Costinase45d54b2020-03-10 09:21:14 +0100287 curl_easy_setopt(easyhandle, CURLOPT_HTTPPROXYTUNNEL, 1L);
288 curl_easy_setopt(easyhandle, CURLOPT_CONNECT_ONLY, 1);
289 // Allow libcurl to pick authentication method. Curl will use the most
290 // secure one the remote site claims to support.
291 curl_easy_setopt(easyhandle, CURLOPT_PROXYAUTH, CURLAUTH_ANY);
292 curl_easy_setopt(easyhandle, CURLOPT_PROXYUSERPWD, credentials_.c_str());
293 }
294 curl_easy_setopt(easyhandle, CURLOPT_CONNECTTIMEOUT_MS,
295 kCurlConnectTimeout.InMilliseconds());
Andreea Costinasa2246592020-04-12 23:24:01 +0200296 curl_easy_setopt(easyhandle, CURLOPT_HEADERFUNCTION, WriteHeadersCallback);
Andreea Costinasf90a4c02020-06-12 22:30:51 +0200297 curl_easy_setopt(easyhandle, CURLOPT_HEADERDATA, &http_response_headers);
Andreea Costinasa2246592020-04-12 23:24:01 +0200298 curl_easy_setopt(easyhandle, CURLOPT_WRITEFUNCTION, WriteCallback);
Andreea Costinasf90a4c02020-06-12 22:30:51 +0200299 curl_easy_setopt(easyhandle, CURLOPT_WRITEDATA, &http_response_body);
Andreea Costinase45d54b2020-03-10 09:21:14 +0100300
301 res = curl_easy_perform(easyhandle);
Andreea Costinasf90a4c02020-06-12 22:30:51 +0200302 curl_easy_getinfo(easyhandle, CURLINFO_HTTP_CONNECTCODE,
303 &http_response_code_);
Andreea Costinase45d54b2020-03-10 09:21:14 +0100304
305 if (res != CURLE_OK) {
Andreea Costinas90b71642020-06-12 10:18:25 +0200306 LOG(ERROR) << *this << " curl_easy_perform() failed with error: "
307 << curl_easy_strerror(res);
Andreea Costinasbb2aa022020-06-13 00:03:23 +0200308 if (first_http_connect_attempt_ && AreAuthCredentialsRequired(easyhandle)) {
309 first_http_connect_attempt_ = false;
310 AuthenticationRequired(http_response_headers);
311 curl_easy_cleanup(easyhandle);
312 return;
313 }
Andreea Costinase45d54b2020-03-10 09:21:14 +0100314 curl_easy_cleanup(easyhandle);
Andreea Costinasa2246592020-04-12 23:24:01 +0200315
Andreea Costinasf90a4c02020-06-12 22:30:51 +0200316 SendHttpResponseToClient(/* http_response_headers= */ {},
317 /* http_response_body= */ {});
318 std::move(setup_finished_callback_).Run(nullptr, this);
Andreea Costinase45d54b2020-03-10 09:21:14 +0100319 return;
320 }
321 // Extract the socket from the curl handle.
322 res = curl_easy_getinfo(easyhandle, CURLINFO_ACTIVESOCKET, &newSocket);
323 if (res != CURLE_OK) {
324 LOG(ERROR) << *this << " Failed to get socket from curl with error: "
325 << curl_easy_strerror(res);
326 curl_easy_cleanup(easyhandle);
327 OnError(kHttpBadGateway);
328 return;
329 }
330
331 ScopedCurlEasyhandle scoped_handle(easyhandle, FreeCurlEasyhandle());
332 auto server_conn = std::make_unique<CurlSocket>(base::ScopedFD(newSocket),
333 std::move(scoped_handle));
334
335 // Send the server reply to the client. If the connection is successful, the
Andreea Costinasa2246592020-04-12 23:24:01 +0200336 // reply headers should be "HTTP/1.1 200 Connection Established".
Andreea Costinasf90a4c02020-06-12 22:30:51 +0200337 if (!SendHttpResponseToClient(http_response_headers, http_response_body)) {
338 std::move(setup_finished_callback_).Run(nullptr, this);
Andreea Costinasa2246592020-04-12 23:24:01 +0200339 return;
340 }
Andreea Costinase45d54b2020-03-10 09:21:14 +0100341
Garrick Evans3388a032020-03-24 11:25:55 +0900342 auto fwd = std::make_unique<patchpanel::SocketForwarder>(
Andreea Costinase45d54b2020-03-10 09:21:14 +0100343 base::StringPrintf("%d-%d", client_socket_->fd(), server_conn->fd()),
344 std::move(client_socket_), std::move(server_conn));
345 // Start forwarding data between sockets.
346 fwd->Start();
347 std::move(setup_finished_callback_).Run(std::move(fwd), this);
348}
349
Andreea Costinasf90a4c02020-06-12 22:30:51 +0200350bool ProxyConnectJob::SendHttpResponseToClient(
351 const std::vector<char>& http_response_headers,
352 const std::vector<char>& http_response_body) {
353 if (http_response_code_ == 0) {
354 // No HTTP CONNECT response code is available.
355 return client_socket_->SendTo(kHttpInternalServerError.data(),
356 kHttpInternalServerError.size());
357 }
358
359 if (http_response_code_ == kHttpCodeProxyAuthRequired) {
360 // This will be a hint for the user to authenticate via the Browser or
361 // acquire a Kerberos ticket.
362 return client_socket_->SendTo(kHttpProxyAuthRequired.data(),
363 kHttpProxyAuthRequired.size());
364 }
365
366 if (http_response_code_ >= 400) {
367 VLOG(1) << "Failed to set up HTTP tunnel with code " << http_response_code_;
368 std::string http_error = base::StringPrintf(
369 kHttpErrorTunnelFailed, std::to_string(http_response_code_).c_str());
370 return client_socket_->SendTo(http_error.c_str(), http_error.size());
371 }
372
373 if (http_response_headers.empty()) {
374 return client_socket_->SendTo(kHttpInternalServerError.data(),
375 kHttpInternalServerError.size());
376 }
377
378 VLOG(1) << "Sending server reply to client";
379 if (!client_socket_->SendTo(http_response_headers.data(),
380 http_response_headers.size())) {
381 PLOG(ERROR) << "Failed to send HTTP server response headers to client";
382 return false;
383 }
384 if (!http_response_body.empty()) {
385 if (!client_socket_->SendTo(http_response_body.data(),
386 http_response_body.size())) {
387 PLOG(ERROR) << "Failed to send HTTP server response payload to client";
388 return false;
389 }
390 }
391 return true;
392}
393
Andreea Costinase45d54b2020-03-10 09:21:14 +0100394void ProxyConnectJob::OnError(const std::string_view& http_error_message) {
395 client_socket_->SendTo(http_error_message.data(), http_error_message.size());
396 std::move(setup_finished_callback_).Run(nullptr, this);
397}
398
Andreea Costinas08a5d182020-04-29 22:12:47 +0200399void ProxyConnectJob::OnClientConnectTimeout() {
400 // Stop listening for client connect requests.
401 read_watcher_.reset();
402 LOG(ERROR) << *this
403 << " Connection timed out while waiting for the client to send a "
404 "connect request.";
405 OnError(kHttpConnectionTimeout);
406}
407
Andreea Costinase45d54b2020-03-10 09:21:14 +0100408std::ostream& operator<<(std::ostream& stream, const ProxyConnectJob& job) {
409 stream << "{fd: " << job.client_socket_->fd();
410 if (!job.target_url_.empty()) {
411 stream << ", url: " << job.target_url_;
412 }
413 stream << "}";
414 return stream;
415}
416
417} // namespace system_proxy