blob: cdc4b62693c2fdeb4b2093aaa711b3ccd3a2ad1c [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 Costinase45d54b2020-03-10 09:21:14 +010042constexpr base::TimeDelta kCurlConnectTimeout = base::TimeDelta::FromMinutes(2);
Andreea Costinas08a5d182020-04-29 22:12:47 +020043constexpr base::TimeDelta kWaitClientConnectTimeout =
44 base::TimeDelta::FromMinutes(2);
Andreea Costinase45d54b2020-03-10 09:21:14 +010045constexpr size_t kMaxBadRequestPrintSize = 120;
46
Andreea Costinasf90a4c02020-06-12 22:30:51 +020047constexpr int64_t kHttpCodeProxyAuthRequired = 407;
48
Andreea Costinase45d54b2020-03-10 09:21:14 +010049// HTTP error codes and messages with origin information for debugging (RFC723,
50// section 6.1).
51const std::string_view kHttpBadRequest =
52 "HTTP/1.1 400 Bad Request - Origin: local proxy\r\n\r\n";
Andreea Costinas08a5d182020-04-29 22:12:47 +020053const std::string_view kHttpConnectionTimeout =
54 "HTTP/1.1 408 Request Timeout - Origin: local proxy\r\n\r\n";
Andreea Costinase45d54b2020-03-10 09:21:14 +010055const std::string_view kHttpInternalServerError =
56 "HTTP/1.1 500 Internal Server Error - Origin: local proxy\r\n\r\n";
57const std::string_view kHttpBadGateway =
58 "HTTP/1.1 502 Bad Gateway - Origin: local proxy\r\n\r\n";
Andreea Costinasf90a4c02020-06-12 22:30:51 +020059const std::string_view kHttpProxyAuthRequired =
60 "HTTP/1.1 407 Credentials required - Origin: local proxy\r\n\r\n";
61constexpr char kHttpErrorTunnelFailed[] =
62 "HTTP/1.1 %s Error creating tunnel - Origin: local proxy\r\n\r\n";
Andreea Costinas90b71642020-06-12 10:18:25 +020063} // namespace
Andreea Costinasa2246592020-04-12 23:24:01 +020064
Andreea Costinas90b71642020-06-12 10:18:25 +020065namespace system_proxy {
Andreea Costinasa2246592020-04-12 23:24:01 +020066// CURLOPT_HEADERFUNCTION callback implementation that only returns the headers
67// from the last response sent by the sever. This is to make sure that we
68// send back valid HTTP replies and auhentication data from the HTTP messages is
69// not being leaked to the client. |userdata| is set on the libcurl CURL handle
70// used to configure the request, using the the CURLOPT_HEADERDATA option. Note,
71// from the libcurl documentation: This callback is being called for all the
72// responses received from the proxy server after intiating the connection
73// request. Multiple responses can be received in an authentication sequence.
74// Only the last response's headers should be forwarded to the System-proxy
75// client. The header callback will be called once for each header and only
76// complete header lines are passed on to the callback.
77static size_t WriteHeadersCallback(char* contents,
78 size_t size,
79 size_t nmemb,
80 void* userdata) {
81 std::vector<char>* vec = (std::vector<char>*)userdata;
82
83 // Check if we are receiving a new HTTP message (after the last one was
84 // terminated with an empty line).
Andreea Costinas90b71642020-06-12 10:18:25 +020085 if (IsEndingWithHttpEmptyLine(base::StringPiece(vec->data(), vec->size()))) {
Andreea Costinasa2246592020-04-12 23:24:01 +020086 VLOG(1) << "Removing the http reply headers from the server "
87 << base::StringPiece(vec->data(), vec->size());
88 vec->clear();
Andreea Costinase45d54b2020-03-10 09:21:14 +010089 }
Andreea Costinasa2246592020-04-12 23:24:01 +020090 vec->insert(vec->end(), contents, contents + (nmemb * size));
Andreea Costinase45d54b2020-03-10 09:21:14 +010091 return size * nmemb;
92}
93
Andreea Costinasa2246592020-04-12 23:24:01 +020094// CONNECT requests may have a reply body. This method will capture the reply
95// and save it in |userdata|. |userdata| is set on the libcurl CURL handle
96// used to configure the request, using the the CURLOPT_WRITEDATA option.
97static size_t WriteCallback(char* contents,
98 size_t size,
99 size_t nmemb,
100 void* userdata) {
101 std::vector<char>* vec = (std::vector<char>*)userdata;
102 vec->insert(vec->end(), contents, contents + (nmemb * size));
103 return size * nmemb;
104}
105
Andreea Costinase45d54b2020-03-10 09:21:14 +0100106ProxyConnectJob::ProxyConnectJob(
Garrick Evans3388a032020-03-24 11:25:55 +0900107 std::unique_ptr<patchpanel::Socket> socket,
Andreea Costinase45d54b2020-03-10 09:21:14 +0100108 const std::string& credentials,
109 ResolveProxyCallback resolve_proxy_callback,
110 OnConnectionSetupFinishedCallback setup_finished_callback)
111 : credentials_(credentials),
112 resolve_proxy_callback_(std::move(resolve_proxy_callback)),
Andreea Costinas08a5d182020-04-29 22:12:47 +0200113 setup_finished_callback_(std::move(setup_finished_callback)),
114 // Safe to use |base::Unretained| because the callback will be canceled
115 // when it goes out of scope.
116 client_connect_timeout_callback_(base::Bind(
117 &ProxyConnectJob::OnClientConnectTimeout, base::Unretained(this))) {
Andreea Costinase45d54b2020-03-10 09:21:14 +0100118 client_socket_ = std::move(socket);
119}
120
121ProxyConnectJob::~ProxyConnectJob() = default;
122
123bool ProxyConnectJob::Start() {
124 // Make the socket non-blocking.
125 if (!base::SetNonBlocking(client_socket_->fd())) {
126 PLOG(ERROR) << *this << " Failed to mark the socket as non-blocking.";
127 client_socket_->SendTo(kHttpInternalServerError.data(),
128 kHttpInternalServerError.size());
129 return false;
130 }
Andreea Costinas08a5d182020-04-29 22:12:47 +0200131 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
132 FROM_HERE, client_connect_timeout_callback_.callback(),
133 kWaitClientConnectTimeout);
Andreea Costinase45d54b2020-03-10 09:21:14 +0100134 read_watcher_ = base::FileDescriptorWatcher::WatchReadable(
Andreea Costinas833eb7c2020-06-12 11:09:15 +0200135 client_socket_->fd(), base::Bind(&ProxyConnectJob::OnClientReadReady,
136 weak_ptr_factory_.GetWeakPtr()));
Andreea Costinase45d54b2020-03-10 09:21:14 +0100137 return true;
138}
139
140void ProxyConnectJob::OnClientReadReady() {
Andreea Costinas08a5d182020-04-29 22:12:47 +0200141 if (!read_watcher_) {
142 // The connection has timed out while waiting for the client's HTTP CONNECT
143 // request. See |OnClientConnectTimeout|.
144 return;
145 }
146 client_connect_timeout_callback_.Cancel();
Andreea Costinase45d54b2020-03-10 09:21:14 +0100147 // Stop watching.
148 read_watcher_.reset();
149 // The first message should be a HTTP CONNECT request.
150 std::vector<char> connect_request;
151 if (!TryReadHttpHeader(&connect_request)) {
152 std::string encoded;
153 base::Base64Encode(
154 base::StringPiece(connect_request.data(), connect_request.size()),
155 &encoded);
156 LOG(ERROR) << *this
157 << " Failure to read proxy CONNECT request. Base 64 encoded "
158 "request message from client: "
159 << encoded;
160 OnError(kHttpBadRequest);
161 return;
162 }
Andreea Costinas90b71642020-06-12 10:18:25 +0200163 base::StringPiece request(connect_request.data(), connect_request.size());
164 target_url_ = GetUriAuthorityFromHttpHeader(request);
Andreea Costinase45d54b2020-03-10 09:21:14 +0100165 if (target_url_.empty()) {
166 LOG(ERROR)
167 << *this
168 << " Failed to extract target url from the HTTP CONNECT request.";
169 OnError(kHttpBadRequest);
170 return;
171 }
172
Andreea Costinasa89309d2020-05-08 15:51:12 +0200173 // The proxy resolution service in Chrome expects a proper URL, formatted as
174 // scheme://host:port. It's safe to assume only https will be used for the
175 // target url.
Andreea Costinase45d54b2020-03-10 09:21:14 +0100176 std::move(resolve_proxy_callback_)
Andreea Costinasa89309d2020-05-08 15:51:12 +0200177 .Run(base::StringPrintf("https://%s", target_url_.c_str()),
178 base::Bind(&ProxyConnectJob::OnProxyResolution,
Andreea Costinas833eb7c2020-06-12 11:09:15 +0200179 weak_ptr_factory_.GetWeakPtr()));
Andreea Costinase45d54b2020-03-10 09:21:14 +0100180}
181
182bool ProxyConnectJob::TryReadHttpHeader(std::vector<char>* raw_request) {
Andreea Costinase45d54b2020-03-10 09:21:14 +0100183 size_t read_byte_count = 0;
184 raw_request->resize(kMaxHttpRequestHeadersSize);
185
186 // Read byte-by-byte and stop when reading an empty line (only CRLF) or when
187 // exceeding the max buffer size.
188 // TODO(acostinas, chromium:1064536) This may have some measurable performance
189 // impact. We should read larger blocks of data, consume the HTTP headers,
190 // cache the tunneled payload that may have already been included (e.g. TLS
191 // ClientHello) and send it to server after the connection is established.
192 while (read_byte_count < kMaxHttpRequestHeadersSize) {
193 if (client_socket_->RecvFrom(raw_request->data() + read_byte_count, 1) <=
194 0) {
195 raw_request->resize(std::min(read_byte_count, kMaxBadRequestPrintSize));
196 return false;
197 }
198 ++read_byte_count;
199
Andreea Costinas90b71642020-06-12 10:18:25 +0200200 if (IsEndingWithHttpEmptyLine(
201 base::StringPiece(raw_request->data(), read_byte_count))) {
Andreea Costinase45d54b2020-03-10 09:21:14 +0100202 raw_request->resize(read_byte_count);
203 return true;
204 }
205 }
206 return false;
207}
208
209void ProxyConnectJob::OnProxyResolution(
210 const std::list<std::string>& proxy_servers) {
211 proxy_servers_ = proxy_servers;
212 DoCurlServerConnection(proxy_servers.front());
213}
214
215void ProxyConnectJob::DoCurlServerConnection(const std::string& proxy_url) {
216 CURL* easyhandle = curl_easy_init();
217 CURLcode res;
Andreea Costinasa2246592020-04-12 23:24:01 +0200218 curl_socket_t newSocket = -1;
Andreea Costinase45d54b2020-03-10 09:21:14 +0100219
220 if (!easyhandle) {
221 // Unfortunately it's not possible to get the failure reason.
222 LOG(ERROR) << *this << " Failure to create curl handle.";
223 curl_easy_cleanup(easyhandle);
224 OnError(kHttpInternalServerError);
225 return;
226 }
227 curl_easy_setopt(easyhandle, CURLOPT_URL, target_url_.c_str());
Andreea Costinasf90a4c02020-06-12 22:30:51 +0200228 std::vector<char> http_response_headers;
229 std::vector<char> http_response_body;
Andreea Costinase45d54b2020-03-10 09:21:14 +0100230 if (proxy_url != brillo::http::kDirectProxy) {
231 curl_easy_setopt(easyhandle, CURLOPT_PROXY, proxy_url.c_str());
232 curl_easy_setopt(easyhandle, CURLOPT_HTTPPROXYTUNNEL, 1L);
233 curl_easy_setopt(easyhandle, CURLOPT_CONNECT_ONLY, 1);
234 // Allow libcurl to pick authentication method. Curl will use the most
235 // secure one the remote site claims to support.
236 curl_easy_setopt(easyhandle, CURLOPT_PROXYAUTH, CURLAUTH_ANY);
237 curl_easy_setopt(easyhandle, CURLOPT_PROXYUSERPWD, credentials_.c_str());
238 }
239 curl_easy_setopt(easyhandle, CURLOPT_CONNECTTIMEOUT_MS,
240 kCurlConnectTimeout.InMilliseconds());
Andreea Costinasa2246592020-04-12 23:24:01 +0200241 curl_easy_setopt(easyhandle, CURLOPT_HEADERFUNCTION, WriteHeadersCallback);
Andreea Costinasf90a4c02020-06-12 22:30:51 +0200242 curl_easy_setopt(easyhandle, CURLOPT_HEADERDATA, &http_response_headers);
Andreea Costinasa2246592020-04-12 23:24:01 +0200243 curl_easy_setopt(easyhandle, CURLOPT_WRITEFUNCTION, WriteCallback);
Andreea Costinasf90a4c02020-06-12 22:30:51 +0200244 curl_easy_setopt(easyhandle, CURLOPT_WRITEDATA, &http_response_body);
Andreea Costinase45d54b2020-03-10 09:21:14 +0100245
246 res = curl_easy_perform(easyhandle);
Andreea Costinasf90a4c02020-06-12 22:30:51 +0200247 curl_easy_getinfo(easyhandle, CURLINFO_HTTP_CONNECTCODE,
248 &http_response_code_);
Andreea Costinase45d54b2020-03-10 09:21:14 +0100249
250 if (res != CURLE_OK) {
Andreea Costinas90b71642020-06-12 10:18:25 +0200251 LOG(ERROR) << *this << " curl_easy_perform() failed with error: "
252 << curl_easy_strerror(res);
Andreea Costinase45d54b2020-03-10 09:21:14 +0100253 curl_easy_cleanup(easyhandle);
Andreea Costinasa2246592020-04-12 23:24:01 +0200254
Andreea Costinasf90a4c02020-06-12 22:30:51 +0200255 SendHttpResponseToClient(/* http_response_headers= */ {},
256 /* http_response_body= */ {});
257 std::move(setup_finished_callback_).Run(nullptr, this);
Andreea Costinase45d54b2020-03-10 09:21:14 +0100258 return;
259 }
260 // Extract the socket from the curl handle.
261 res = curl_easy_getinfo(easyhandle, CURLINFO_ACTIVESOCKET, &newSocket);
262 if (res != CURLE_OK) {
263 LOG(ERROR) << *this << " Failed to get socket from curl with error: "
264 << curl_easy_strerror(res);
265 curl_easy_cleanup(easyhandle);
266 OnError(kHttpBadGateway);
267 return;
268 }
269
270 ScopedCurlEasyhandle scoped_handle(easyhandle, FreeCurlEasyhandle());
271 auto server_conn = std::make_unique<CurlSocket>(base::ScopedFD(newSocket),
272 std::move(scoped_handle));
273
274 // Send the server reply to the client. If the connection is successful, the
Andreea Costinasa2246592020-04-12 23:24:01 +0200275 // reply headers should be "HTTP/1.1 200 Connection Established".
Andreea Costinasf90a4c02020-06-12 22:30:51 +0200276 if (!SendHttpResponseToClient(http_response_headers, http_response_body)) {
277 std::move(setup_finished_callback_).Run(nullptr, this);
Andreea Costinasa2246592020-04-12 23:24:01 +0200278 return;
279 }
Andreea Costinase45d54b2020-03-10 09:21:14 +0100280
Garrick Evans3388a032020-03-24 11:25:55 +0900281 auto fwd = std::make_unique<patchpanel::SocketForwarder>(
Andreea Costinase45d54b2020-03-10 09:21:14 +0100282 base::StringPrintf("%d-%d", client_socket_->fd(), server_conn->fd()),
283 std::move(client_socket_), std::move(server_conn));
284 // Start forwarding data between sockets.
285 fwd->Start();
286 std::move(setup_finished_callback_).Run(std::move(fwd), this);
287}
288
Andreea Costinasf90a4c02020-06-12 22:30:51 +0200289bool ProxyConnectJob::SendHttpResponseToClient(
290 const std::vector<char>& http_response_headers,
291 const std::vector<char>& http_response_body) {
292 if (http_response_code_ == 0) {
293 // No HTTP CONNECT response code is available.
294 return client_socket_->SendTo(kHttpInternalServerError.data(),
295 kHttpInternalServerError.size());
296 }
297
298 if (http_response_code_ == kHttpCodeProxyAuthRequired) {
299 // This will be a hint for the user to authenticate via the Browser or
300 // acquire a Kerberos ticket.
301 return client_socket_->SendTo(kHttpProxyAuthRequired.data(),
302 kHttpProxyAuthRequired.size());
303 }
304
305 if (http_response_code_ >= 400) {
306 VLOG(1) << "Failed to set up HTTP tunnel with code " << http_response_code_;
307 std::string http_error = base::StringPrintf(
308 kHttpErrorTunnelFailed, std::to_string(http_response_code_).c_str());
309 return client_socket_->SendTo(http_error.c_str(), http_error.size());
310 }
311
312 if (http_response_headers.empty()) {
313 return client_socket_->SendTo(kHttpInternalServerError.data(),
314 kHttpInternalServerError.size());
315 }
316
317 VLOG(1) << "Sending server reply to client";
318 if (!client_socket_->SendTo(http_response_headers.data(),
319 http_response_headers.size())) {
320 PLOG(ERROR) << "Failed to send HTTP server response headers to client";
321 return false;
322 }
323 if (!http_response_body.empty()) {
324 if (!client_socket_->SendTo(http_response_body.data(),
325 http_response_body.size())) {
326 PLOG(ERROR) << "Failed to send HTTP server response payload to client";
327 return false;
328 }
329 }
330 return true;
331}
332
Andreea Costinase45d54b2020-03-10 09:21:14 +0100333void ProxyConnectJob::OnError(const std::string_view& http_error_message) {
334 client_socket_->SendTo(http_error_message.data(), http_error_message.size());
335 std::move(setup_finished_callback_).Run(nullptr, this);
336}
337
Andreea Costinas08a5d182020-04-29 22:12:47 +0200338void ProxyConnectJob::OnClientConnectTimeout() {
339 // Stop listening for client connect requests.
340 read_watcher_.reset();
341 LOG(ERROR) << *this
342 << " Connection timed out while waiting for the client to send a "
343 "connect request.";
344 OnError(kHttpConnectionTimeout);
345}
346
Andreea Costinase45d54b2020-03-10 09:21:14 +0100347std::ostream& operator<<(std::ostream& stream, const ProxyConnectJob& job) {
348 stream << "{fd: " << job.client_socket_->fd();
349 if (!job.target_url_.empty()) {
350 stream << ", url: " << job.target_url_;
351 }
352 stream << "}";
353 return stream;
354}
355
356} // namespace system_proxy