pac_file_fetcher_impl.cc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. // Copyright (c) 2012 The Chromium 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. #include "net/proxy_resolution/pac_file_fetcher_impl.h"
  5. #include "base/bind.h"
  6. #include "base/compiler_specific.h"
  7. #include "base/location.h"
  8. #include "base/logging.h"
  9. #include "base/memory/ptr_util.h"
  10. #include "base/metrics/histogram_macros.h"
  11. #include "base/ranges/algorithm.h"
  12. #include "base/strings/string_piece.h"
  13. #include "base/strings/string_util.h"
  14. #include "base/task/single_thread_task_runner.h"
  15. #include "base/threading/thread_task_runner_handle.h"
  16. #include "net/base/data_url.h"
  17. #include "net/base/io_buffer.h"
  18. #include "net/base/load_flags.h"
  19. #include "net/base/net_errors.h"
  20. #include "net/base/net_string_util.h"
  21. #include "net/base/request_priority.h"
  22. #include "net/cert/cert_status_flags.h"
  23. #include "net/http/http_response_headers.h"
  24. #include "net/url_request/redirect_info.h"
  25. #include "net/url_request/url_request_context.h"
  26. // TODO(eroman):
  27. // - Support auth-prompts (http://crbug.com/77366)
  28. namespace net {
  29. namespace {
  30. // The maximum size (in bytes) allowed for a PAC script. Responses exceeding
  31. // this will fail with ERR_FILE_TOO_BIG.
  32. const int kDefaultMaxResponseBytes = 1048576; // 1 megabyte
  33. // The maximum duration (in milliseconds) allowed for fetching the PAC script.
  34. // Responses exceeding this will fail with ERR_TIMED_OUT.
  35. //
  36. // This timeout applies to both scripts fetched in the course of WPAD, as well
  37. // as explicitly configured ones.
  38. //
  39. // If the default timeout is too high, auto-detect can stall for a long time,
  40. // and if it is too low then slow loading scripts may be skipped.
  41. //
  42. // 30 seconds is a compromise between those competing goals. This value also
  43. // appears to match Microsoft Edge (based on testing).
  44. constexpr base::TimeDelta kDefaultMaxDuration = base::Seconds(30);
  45. // Returns true if |mime_type| is one of the known PAC mime type.
  46. constexpr bool IsPacMimeType(base::StringPiece mime_type) {
  47. constexpr base::StringPiece kSupportedPacMimeTypes[] = {
  48. "application/x-ns-proxy-autoconfig",
  49. "application/x-javascript-config",
  50. };
  51. return base::ranges::any_of(kSupportedPacMimeTypes, [&](auto pac_mime_type) {
  52. return base::EqualsCaseInsensitiveASCII(pac_mime_type, mime_type);
  53. });
  54. }
  55. struct BomMapping {
  56. base::StringPiece prefix;
  57. const char* charset;
  58. };
  59. const BomMapping kBomMappings[] = {
  60. {"\xFE\xFF", "utf-16be"},
  61. {"\xFF\xFE", "utf-16le"},
  62. {"\xEF\xBB\xBF", "utf-8"},
  63. };
  64. // Converts |bytes| (which is encoded by |charset|) to UTF16, saving the resul
  65. // to |*utf16|.
  66. // If |charset| is empty, then we don't know what it was and guess.
  67. void ConvertResponseToUTF16(const std::string& charset,
  68. const std::string& bytes,
  69. std::u16string* utf16) {
  70. if (charset.empty()) {
  71. // Guess the charset by looking at the BOM.
  72. base::StringPiece bytes_str(bytes);
  73. for (const auto& bom : kBomMappings) {
  74. if (base::StartsWith(bytes_str, bom.prefix)) {
  75. return ConvertResponseToUTF16(
  76. bom.charset,
  77. // Strip the BOM in the converted response.
  78. bytes.substr(bom.prefix.size()), utf16);
  79. }
  80. }
  81. // Otherwise assume ISO-8859-1 if no charset was specified.
  82. return ConvertResponseToUTF16(kCharsetLatin1, bytes, utf16);
  83. }
  84. DCHECK(!charset.empty());
  85. // Be generous in the conversion -- if any characters lie outside of |charset|
  86. // (i.e. invalid), then substitute them with U+FFFD rather than failing.
  87. ConvertToUTF16WithSubstitutions(bytes, charset.c_str(), utf16);
  88. }
  89. } // namespace
  90. std::unique_ptr<PacFileFetcherImpl> PacFileFetcherImpl::Create(
  91. URLRequestContext* url_request_context) {
  92. return base::WrapUnique(new PacFileFetcherImpl(url_request_context));
  93. }
  94. PacFileFetcherImpl::~PacFileFetcherImpl() {
  95. // The URLRequest's destructor will cancel the outstanding request, and
  96. // ensure that the delegate (this) is not called again.
  97. }
  98. base::TimeDelta PacFileFetcherImpl::SetTimeoutConstraint(
  99. base::TimeDelta timeout) {
  100. base::TimeDelta prev = max_duration_;
  101. max_duration_ = timeout;
  102. return prev;
  103. }
  104. size_t PacFileFetcherImpl::SetSizeConstraint(size_t size_bytes) {
  105. size_t prev = max_response_bytes_;
  106. max_response_bytes_ = size_bytes;
  107. return prev;
  108. }
  109. void PacFileFetcherImpl::OnResponseCompleted(URLRequest* request,
  110. int net_error) {
  111. DCHECK_EQ(request, cur_request_.get());
  112. // Use |result_code_| as the request's error if we have already set it to
  113. // something specific.
  114. if (result_code_ == OK && net_error != OK)
  115. result_code_ = net_error;
  116. FetchCompleted();
  117. }
  118. int PacFileFetcherImpl::Fetch(
  119. const GURL& url,
  120. std::u16string* text,
  121. CompletionOnceCallback callback,
  122. const NetworkTrafficAnnotationTag traffic_annotation) {
  123. // It is invalid to call Fetch() while a request is already in progress.
  124. DCHECK(!cur_request_.get());
  125. DCHECK(!callback.is_null());
  126. DCHECK(text);
  127. if (!url_request_context_)
  128. return ERR_CONTEXT_SHUT_DOWN;
  129. if (!IsUrlSchemeAllowed(url))
  130. return ERR_DISALLOWED_URL_SCHEME;
  131. // Handle base-64 encoded data-urls that contain custom PAC scripts.
  132. if (url.SchemeIs("data")) {
  133. std::string mime_type;
  134. std::string charset;
  135. std::string data;
  136. if (!DataURL::Parse(url, &mime_type, &charset, &data))
  137. return ERR_FAILED;
  138. ConvertResponseToUTF16(charset, data, text);
  139. return OK;
  140. }
  141. DCHECK(fetch_start_time_.is_null());
  142. fetch_start_time_ = base::TimeTicks::Now();
  143. // Use highest priority, so if socket pools are being used for other types of
  144. // requests, PAC requests are aren't blocked on them.
  145. cur_request_ = url_request_context_->CreateRequest(url, MAXIMUM_PRIORITY,
  146. this, traffic_annotation);
  147. cur_request_->set_isolation_info(isolation_info());
  148. // Make sure that the PAC script is downloaded using a direct connection,
  149. // to avoid circular dependencies (fetching is a part of proxy resolution).
  150. // Also disable the use of the disk cache. The cache is disabled so that if
  151. // the user switches networks we don't potentially use the cached response
  152. // from old network when we should in fact be re-fetching on the new network.
  153. // If the PAC script is hosted on an HTTPS server we bypass revocation
  154. // checking in order to avoid a circular dependency when attempting to fetch
  155. // the OCSP response or CRL. We could make the revocation check go direct but
  156. // the proxy might be the only way to the outside world. IGNORE_LIMITS is
  157. // used to avoid blocking proxy resolution on other network requests.
  158. cur_request_->SetLoadFlags(LOAD_BYPASS_PROXY | LOAD_DISABLE_CACHE |
  159. LOAD_DISABLE_CERT_NETWORK_FETCHES |
  160. LOAD_IGNORE_LIMITS);
  161. // Save the caller's info for notification on completion.
  162. callback_ = std::move(callback);
  163. result_text_ = text;
  164. bytes_read_so_far_.clear();
  165. // Post a task to timeout this request if it takes too long.
  166. cur_request_id_ = ++next_id_;
  167. base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
  168. FROM_HERE,
  169. base::BindOnce(&PacFileFetcherImpl::OnTimeout, weak_factory_.GetWeakPtr(),
  170. cur_request_id_),
  171. max_duration_);
  172. // Start the request.
  173. cur_request_->Start();
  174. return ERR_IO_PENDING;
  175. }
  176. void PacFileFetcherImpl::Cancel() {
  177. // ResetCurRequestState will free the URLRequest, which will cause
  178. // cancellation.
  179. ResetCurRequestState();
  180. }
  181. URLRequestContext* PacFileFetcherImpl::GetRequestContext() const {
  182. return url_request_context_;
  183. }
  184. void PacFileFetcherImpl::OnShutdown() {
  185. url_request_context_ = nullptr;
  186. if (cur_request_) {
  187. result_code_ = ERR_CONTEXT_SHUT_DOWN;
  188. FetchCompleted();
  189. }
  190. }
  191. void PacFileFetcherImpl::OnReceivedRedirect(URLRequest* request,
  192. const RedirectInfo& redirect_info,
  193. bool* defer_redirect) {
  194. int error = OK;
  195. // Redirection to file:// is never OK. Ordinarily this is handled lower in the
  196. // stack (|FileProtocolHandler::IsSafeRedirectTarget|), but this is reachable
  197. // when built without file:// suppport. Return the same error for consistency.
  198. if (redirect_info.new_url.SchemeIsFile()) {
  199. error = ERR_UNSAFE_REDIRECT;
  200. } else if (!IsUrlSchemeAllowed(redirect_info.new_url)) {
  201. error = ERR_DISALLOWED_URL_SCHEME;
  202. }
  203. if (error != OK) {
  204. // Fail the redirect.
  205. request->CancelWithError(error);
  206. OnResponseCompleted(request, error);
  207. }
  208. }
  209. void PacFileFetcherImpl::OnAuthRequired(URLRequest* request,
  210. const AuthChallengeInfo& auth_info) {
  211. DCHECK_EQ(request, cur_request_.get());
  212. // TODO(eroman): http://crbug.com/77366
  213. LOG(WARNING) << "Auth required to fetch PAC script, aborting.";
  214. result_code_ = ERR_NOT_IMPLEMENTED;
  215. request->CancelAuth();
  216. }
  217. void PacFileFetcherImpl::OnSSLCertificateError(URLRequest* request,
  218. int net_error,
  219. const SSLInfo& ssl_info,
  220. bool fatal) {
  221. DCHECK_EQ(request, cur_request_.get());
  222. LOG(WARNING) << "SSL certificate error when fetching PAC script, aborting.";
  223. // Certificate errors are in same space as net errors.
  224. result_code_ = net_error;
  225. request->Cancel();
  226. }
  227. void PacFileFetcherImpl::OnResponseStarted(URLRequest* request, int net_error) {
  228. DCHECK_EQ(request, cur_request_.get());
  229. DCHECK_NE(ERR_IO_PENDING, net_error);
  230. if (net_error != OK) {
  231. OnResponseCompleted(request, net_error);
  232. return;
  233. }
  234. // Require HTTP responses to have a success status code.
  235. if (request->url().SchemeIsHTTPOrHTTPS()) {
  236. // NOTE about status codes: We are like Firefox 3 in this respect.
  237. // {IE 7, Safari 3, Opera 9.5} do not care about the status code.
  238. if (request->GetResponseCode() != 200) {
  239. VLOG(1) << "Fetched PAC script had (bad) status line: "
  240. << request->response_headers()->GetStatusLine();
  241. result_code_ = ERR_HTTP_RESPONSE_CODE_FAILURE;
  242. request->Cancel();
  243. return;
  244. }
  245. // NOTE about mime types: We do not enforce mime types on PAC files.
  246. // This is for compatibility with {IE 7, Firefox 3, Opera 9.5}. We will
  247. // however log mismatches to help with debugging.
  248. std::string mime_type;
  249. cur_request_->GetMimeType(&mime_type);
  250. if (!IsPacMimeType(mime_type)) {
  251. VLOG(1) << "Fetched PAC script does not have a proper mime type: "
  252. << mime_type;
  253. }
  254. }
  255. ReadBody(request);
  256. }
  257. void PacFileFetcherImpl::OnReadCompleted(URLRequest* request, int num_bytes) {
  258. DCHECK_NE(ERR_IO_PENDING, num_bytes);
  259. DCHECK_EQ(request, cur_request_.get());
  260. if (ConsumeBytesRead(request, num_bytes)) {
  261. // Keep reading.
  262. ReadBody(request);
  263. }
  264. }
  265. PacFileFetcherImpl::PacFileFetcherImpl(URLRequestContext* url_request_context)
  266. : url_request_context_(url_request_context),
  267. buf_(base::MakeRefCounted<IOBuffer>(kBufSize)),
  268. max_response_bytes_(kDefaultMaxResponseBytes),
  269. max_duration_(kDefaultMaxDuration) {
  270. DCHECK(url_request_context);
  271. }
  272. bool PacFileFetcherImpl::IsUrlSchemeAllowed(const GURL& url) const {
  273. // Always allow http://, https://, and data:.
  274. if (url.SchemeIsHTTPOrHTTPS() || url.SchemeIs("data"))
  275. return true;
  276. // Disallow any other URL scheme.
  277. return false;
  278. }
  279. void PacFileFetcherImpl::ReadBody(URLRequest* request) {
  280. // Read as many bytes as are available synchronously.
  281. while (true) {
  282. int num_bytes = request->Read(buf_.get(), kBufSize);
  283. if (num_bytes == ERR_IO_PENDING)
  284. return;
  285. if (num_bytes < 0) {
  286. OnResponseCompleted(request, num_bytes);
  287. return;
  288. }
  289. if (!ConsumeBytesRead(request, num_bytes))
  290. return;
  291. }
  292. }
  293. bool PacFileFetcherImpl::ConsumeBytesRead(URLRequest* request, int num_bytes) {
  294. if (fetch_time_to_first_byte_.is_null())
  295. fetch_time_to_first_byte_ = base::TimeTicks::Now();
  296. if (num_bytes <= 0) {
  297. // Error while reading, or EOF.
  298. OnResponseCompleted(request, num_bytes);
  299. return false;
  300. }
  301. // Enforce maximum size bound.
  302. if (num_bytes + bytes_read_so_far_.size() >
  303. static_cast<size_t>(max_response_bytes_)) {
  304. result_code_ = ERR_FILE_TOO_BIG;
  305. request->Cancel();
  306. return false;
  307. }
  308. bytes_read_so_far_.append(buf_->data(), num_bytes);
  309. return true;
  310. }
  311. void PacFileFetcherImpl::FetchCompleted() {
  312. if (result_code_ == OK) {
  313. // Calculate duration of time for PAC file fetch to complete.
  314. DCHECK(!fetch_start_time_.is_null());
  315. DCHECK(!fetch_time_to_first_byte_.is_null());
  316. UMA_HISTOGRAM_MEDIUM_TIMES("Net.ProxyScriptFetcher.SuccessDuration",
  317. base::TimeTicks::Now() - fetch_start_time_);
  318. UMA_HISTOGRAM_MEDIUM_TIMES("Net.ProxyScriptFetcher.FirstByteDuration",
  319. fetch_time_to_first_byte_ - fetch_start_time_);
  320. // The caller expects the response to be encoded as UTF16.
  321. std::string charset;
  322. cur_request_->GetCharset(&charset);
  323. ConvertResponseToUTF16(charset, bytes_read_so_far_, result_text_);
  324. } else {
  325. // On error, the caller expects empty string for bytes.
  326. result_text_->clear();
  327. }
  328. int result_code = result_code_;
  329. CompletionOnceCallback callback = std::move(callback_);
  330. ResetCurRequestState();
  331. std::move(callback).Run(result_code);
  332. }
  333. void PacFileFetcherImpl::ResetCurRequestState() {
  334. cur_request_.reset();
  335. cur_request_id_ = 0;
  336. callback_.Reset();
  337. result_code_ = OK;
  338. result_text_ = nullptr;
  339. fetch_start_time_ = base::TimeTicks();
  340. fetch_time_to_first_byte_ = base::TimeTicks();
  341. }
  342. void PacFileFetcherImpl::OnTimeout(int id) {
  343. // Timeout tasks may outlive the URLRequest they reference. Make sure it
  344. // is still applicable.
  345. if (cur_request_id_ != id)
  346. return;
  347. DCHECK(cur_request_.get());
  348. result_code_ = ERR_TIMED_OUT;
  349. FetchCompleted();
  350. }
  351. } // namespace net