blob: 641fd59876c5af4534ff88c619d4d88ce5bae462 [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
11#include <curl/curl.h>
12#include <curl/easy.h>
13
Andreea Costinase45d54b2020-03-10 09:21:14 +010014#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 Costinase45d54b2020-03-10 09:21:14 +010020#include <base/strings/string_util.h>
21#include <base/time/time.h>
Andreea Costinas08a5d182020-04-29 22:12:47 +020022#include <base/threading/thread.h>
23#include <base/threading/thread_task_runner_handle.h>
Andreea Costinase45d54b2020-03-10 09:21:14 +010024#include <brillo/http/http_transport.h>
Garrick Evanscd8c2972020-04-14 14:35:52 +090025#include <chromeos/patchpanel/net_util.h>
26#include <chromeos/patchpanel/socket.h>
27#include <chromeos/patchpanel/socket_forwarder.h>
Andreea Costinase45d54b2020-03-10 09:21:14 +010028
29#include "system-proxy/curl_socket.h"
Andreea Costinas90b71642020-06-12 10:18:25 +020030#include "system-proxy/http_util.h"
Andreea Costinase45d54b2020-03-10 09:21:14 +010031
Garrick Evans2d5e7c92020-06-08 14:14:28 +090032// The libpatchpanel-util library overloads << for socket data structures.
Andreea Costinase45d54b2020-03-10 09:21:14 +010033// 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 Evans3388a032020-03-24 11:25:55 +090036using patchpanel::operator<<;
Andreea Costinase45d54b2020-03-10 09:21:14 +010037
38namespace {
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.
42constexpr int kMaxHttpRequestHeadersSize = 8000;
Andreea Costinase45d54b2020-03-10 09:21:14 +010043constexpr base::TimeDelta kCurlConnectTimeout = base::TimeDelta::FromMinutes(2);
Andreea Costinas08a5d182020-04-29 22:12:47 +020044constexpr base::TimeDelta kWaitClientConnectTimeout =
45 base::TimeDelta::FromMinutes(2);
Andreea Costinase45d54b2020-03-10 09:21:14 +010046constexpr size_t kMaxBadRequestPrintSize = 120;
47
48// HTTP error codes and messages with origin information for debugging (RFC723,
49// section 6.1).
50const std::string_view kHttpBadRequest =
51 "HTTP/1.1 400 Bad Request - Origin: local proxy\r\n\r\n";
Andreea Costinas08a5d182020-04-29 22:12:47 +020052const std::string_view kHttpConnectionTimeout =
53 "HTTP/1.1 408 Request Timeout - Origin: local proxy\r\n\r\n";
Andreea Costinase45d54b2020-03-10 09:21:14 +010054const std::string_view kHttpInternalServerError =
55 "HTTP/1.1 500 Internal Server Error - Origin: local proxy\r\n\r\n";
56const std::string_view kHttpBadGateway =
57 "HTTP/1.1 502 Bad Gateway - Origin: local proxy\r\n\r\n";
58
Andreea Costinas90b71642020-06-12 10:18:25 +020059} // namespace
Andreea Costinasa2246592020-04-12 23:24:01 +020060
Andreea Costinas90b71642020-06-12 10:18:25 +020061namespace system_proxy {
Andreea Costinasa2246592020-04-12 23:24:01 +020062// 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.
73static 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 Costinas90b71642020-06-12 10:18:25 +020081 if (IsEndingWithHttpEmptyLine(base::StringPiece(vec->data(), vec->size()))) {
Andreea Costinasa2246592020-04-12 23:24:01 +020082 VLOG(1) << "Removing the http reply headers from the server "
83 << base::StringPiece(vec->data(), vec->size());
84 vec->clear();
Andreea Costinase45d54b2020-03-10 09:21:14 +010085 }
Andreea Costinasa2246592020-04-12 23:24:01 +020086 vec->insert(vec->end(), contents, contents + (nmemb * size));
Andreea Costinase45d54b2020-03-10 09:21:14 +010087 return size * nmemb;
88}
89
Andreea Costinasa2246592020-04-12 23:24:01 +020090// 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.
93static 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 Costinase45d54b2020-03-10 09:21:14 +0100102ProxyConnectJob::ProxyConnectJob(
Garrick Evans3388a032020-03-24 11:25:55 +0900103 std::unique_ptr<patchpanel::Socket> socket,
Andreea Costinase45d54b2020-03-10 09:21:14 +0100104 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 Costinas08a5d182020-04-29 22:12:47 +0200109 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 Costinase45d54b2020-03-10 09:21:14 +0100114 client_socket_ = std::move(socket);
115}
116
117ProxyConnectJob::~ProxyConnectJob() = default;
118
119bool 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 Costinas08a5d182020-04-29 22:12:47 +0200127 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
128 FROM_HERE, client_connect_timeout_callback_.callback(),
129 kWaitClientConnectTimeout);
Andreea Costinase45d54b2020-03-10 09:21:14 +0100130 read_watcher_ = base::FileDescriptorWatcher::WatchReadable(
Andreea Costinas833eb7c2020-06-12 11:09:15 +0200131 client_socket_->fd(), base::Bind(&ProxyConnectJob::OnClientReadReady,
132 weak_ptr_factory_.GetWeakPtr()));
Andreea Costinase45d54b2020-03-10 09:21:14 +0100133 return true;
134}
135
136void ProxyConnectJob::OnClientReadReady() {
Andreea Costinas08a5d182020-04-29 22:12:47 +0200137 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 Costinase45d54b2020-03-10 09:21:14 +0100143 // 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 Costinas90b71642020-06-12 10:18:25 +0200159 base::StringPiece request(connect_request.data(), connect_request.size());
160 target_url_ = GetUriAuthorityFromHttpHeader(request);
Andreea Costinase45d54b2020-03-10 09:21:14 +0100161 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 Costinasa89309d2020-05-08 15:51:12 +0200169 // 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 Costinase45d54b2020-03-10 09:21:14 +0100172 std::move(resolve_proxy_callback_)
Andreea Costinasa89309d2020-05-08 15:51:12 +0200173 .Run(base::StringPrintf("https://%s", target_url_.c_str()),
174 base::Bind(&ProxyConnectJob::OnProxyResolution,
Andreea Costinas833eb7c2020-06-12 11:09:15 +0200175 weak_ptr_factory_.GetWeakPtr()));
Andreea Costinase45d54b2020-03-10 09:21:14 +0100176}
177
178bool ProxyConnectJob::TryReadHttpHeader(std::vector<char>* raw_request) {
Andreea Costinase45d54b2020-03-10 09:21:14 +0100179 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 Costinas90b71642020-06-12 10:18:25 +0200196 if (IsEndingWithHttpEmptyLine(
197 base::StringPiece(raw_request->data(), read_byte_count))) {
Andreea Costinase45d54b2020-03-10 09:21:14 +0100198 raw_request->resize(read_byte_count);
199 return true;
200 }
201 }
202 return false;
203}
204
205void ProxyConnectJob::OnProxyResolution(
206 const std::list<std::string>& proxy_servers) {
207 proxy_servers_ = proxy_servers;
208 DoCurlServerConnection(proxy_servers.front());
209}
210
211void ProxyConnectJob::DoCurlServerConnection(const std::string& proxy_url) {
212 CURL* easyhandle = curl_easy_init();
213 CURLcode res;
Andreea Costinasa2246592020-04-12 23:24:01 +0200214 curl_socket_t newSocket = -1;
215 std::vector<char> server_header_reply, server_body_reply;
Andreea Costinase45d54b2020-03-10 09:21:14 +0100216
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 Costinasa2246592020-04-12 23:24:01 +0200237 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 Costinase45d54b2020-03-10 09:21:14 +0100241
242 res = curl_easy_perform(easyhandle);
243
244 if (res != CURLE_OK) {
Andreea Costinas90b71642020-06-12 10:18:25 +0200245 LOG(ERROR) << *this << " curl_easy_perform() failed with error: "
246 << curl_easy_strerror(res);
Andreea Costinase45d54b2020-03-10 09:21:14 +0100247 curl_easy_cleanup(easyhandle);
Andreea Costinasa2246592020-04-12 23:24:01 +0200248
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 Costinase45d54b2020-03-10 09:21:14 +0100256 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 Costinasa2246592020-04-12 23:24:01 +0200273 // 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 Costinas08a5d182020-04-29 22:12:47 +0200292 << " Failed to send HTTP CONNECT reply body to client: "
Andreea Costinasa2246592020-04-12 23:24:01 +0200293 << base::StringPiece(server_body_reply.data(),
294 server_body_reply.size());
295 }
296 }
Andreea Costinase45d54b2020-03-10 09:21:14 +0100297
Garrick Evans3388a032020-03-24 11:25:55 +0900298 auto fwd = std::make_unique<patchpanel::SocketForwarder>(
Andreea Costinase45d54b2020-03-10 09:21:14 +0100299 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
306void 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 Costinas08a5d182020-04-29 22:12:47 +0200311void 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 Costinase45d54b2020-03-10 09:21:14 +0100320std::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