1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
|
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ash/net/network_diagnostics/https_firewall_routine.h"
#include <algorithm>
#include <string>
#include <utility>
#include <vector>
#include "base/functional/bind.h"
#include "chrome/browser/ash/net/network_diagnostics/network_diagnostics_util.h"
#include "chrome/browser/profiles/profile.h"
#include "content/public/browser/storage_partition.h"
#include "services/network/public/mojom/network_context.mojom.h"
namespace ash {
namespace network_diagnostics {
namespace {
namespace mojom = ::chromeos::network_diagnostics::mojom;
// Https port number.
constexpr int kHttpsPort = 443;
// Https scheme.
constexpr char kHttpsScheme[] = "https://";
// Effetively, the number of random hosts to query.
// total hosts queried by this routine = random hosts + fixed hosts, where
// information about fixed hosts is found in network_diagnostics_util.cc.
constexpr int kTotalAdditionalHostsToQuery = 3;
// The length of a random eight letter prefix obtained by the characters from
// |kPossibleChars|.
constexpr int kHostPrefixLength = 8;
// The threshold describing the number of DNS resolution failures permitted.
// E.g. If 10 host resolution attempts are made, any more than two DNS failures
// would result in a problem.
constexpr double kDnsResolutionFailureRateThreshold = 0.2;
// The threshold describing number of TLS probe failures permitted. E.g. If 10
// TLS probes are attempted, any more than two failures would result in a
// problem. This number does not take into account the number of TLS probes that
// failed due to unsuccessful DNS resolution.
constexpr double kTlsProbeFailureRateThreshold = 0.2;
// For an explanation of error codes, see "net/base/net_error_list.h".
constexpr int kRetryResponseCodes[] = {net::ERR_TIMED_OUT,
net::ERR_DNS_TIMED_OUT};
} // namespace
const int kTotalNumRetries = 3;
HttpsFirewallRoutine::HttpsFirewallRoutine(mojom::RoutineCallSource source)
: NetworkDiagnosticsRoutine(source),
num_retries_(kTotalNumRetries),
tls_prober_getter_callback_(base::BindRepeating(
&HttpsFirewallRoutine::CreateAndExecuteTlsProber)) {
std::vector<std::string> url_strings =
util::GetRandomAndFixedHostsWithSchemeAndPort(
kTotalAdditionalHostsToQuery, kHostPrefixLength, kHttpsScheme,
kHttpsPort);
for (const auto& url_string : url_strings) {
urls_to_query_.push_back(GURL(url_string));
}
num_urls_to_query_ = urls_to_query_.size();
}
HttpsFirewallRoutine::~HttpsFirewallRoutine() = default;
mojom::RoutineType HttpsFirewallRoutine::Type() {
return mojom::RoutineType::kHttpsFirewall;
}
void HttpsFirewallRoutine::Run() {
ProbeNextUrl();
}
void HttpsFirewallRoutine::AnalyzeResultsAndExecuteCallback() {
// There should at least `kTotalAdditionalHostsToQuery` (=3) URLs to query.
DCHECK(num_urls_to_query_);
double dns_resolution_failure_rate =
static_cast<double>(dns_resolution_failures_) /
static_cast<double>(num_urls_to_query_);
if (dns_resolution_failure_rate > kDnsResolutionFailureRateThreshold) {
set_verdict(mojom::RoutineVerdict::kProblem);
problems_.push_back(
mojom::HttpsFirewallProblem::kHighDnsResolutionFailureRate);
} else {
// When `dns_resolution_failure_rate` is below the threshold, there must be
// probes that is not "DNS failure".
DCHECK(num_no_dns_failure_tls_probes_attempted_);
double tls_probe_failure_rate =
static_cast<double>(tls_probe_failures_) /
static_cast<double>(num_no_dns_failure_tls_probes_attempted_);
if (tls_probe_failure_rate <= kTlsProbeFailureRateThreshold) {
set_verdict(mojom::RoutineVerdict::kNoProblem);
} else if (tls_probe_failures_ ==
num_no_dns_failure_tls_probes_attempted_) {
set_verdict(mojom::RoutineVerdict::kProblem);
problems_.push_back(mojom::HttpsFirewallProblem::kFirewallDetected);
} else {
// It cannot be conclusively determined whether a firewall exists;
// however, since reaching this case means tls_probe_failure_rate >
// kTlsProbeFailureRateThreshold, a firewall could potentially
// exist.
set_verdict(mojom::RoutineVerdict::kProblem);
problems_.push_back(mojom::HttpsFirewallProblem::kPotentialFirewall);
}
}
set_problems(mojom::RoutineProblems::NewHttpsFirewallProblems(problems_));
ExecuteCallback();
}
void HttpsFirewallRoutine::ProbeNextUrl() {
DCHECK(urls_to_query_.size() > 0);
auto url = urls_to_query_.back();
urls_to_query_.pop_back();
AttemptProbe(url);
}
void HttpsFirewallRoutine::AttemptProbe(const GURL& url) {
// Store the instance of TlsProber.
tls_prober_ = tls_prober_getter_callback_.Run(
base::BindRepeating(&HttpsFirewallRoutine::GetNetworkContext),
net::HostPortPair::FromURL(url),
/*negotiate_tls=*/true,
base::BindOnce(&HttpsFirewallRoutine::OnProbeComplete, weak_ptr(), url));
}
void HttpsFirewallRoutine::OnProbeComplete(
const GURL& url,
int result,
TlsProber::ProbeExitEnum probe_exit_enum) {
if (probe_exit_enum == TlsProber::ProbeExitEnum::kDnsFailure) {
dns_resolution_failures_++;
} else {
const auto* iter = std::ranges::find(kRetryResponseCodes, result);
if (iter != std::end(kRetryResponseCodes) && num_retries_ > 0) {
num_retries_--;
AttemptProbe(url);
return;
}
if (result < 0) {
tls_probe_failures_++;
}
num_no_dns_failure_tls_probes_attempted_++;
}
if (urls_to_query_.size() == 0) {
AnalyzeResultsAndExecuteCallback();
return;
}
ProbeNextUrl();
}
network::mojom::NetworkContext* HttpsFirewallRoutine::GetNetworkContext() {
Profile* profile = util::GetUserProfile();
DCHECK(profile);
return profile->GetDefaultStoragePartition()->GetNetworkContext();
}
std::unique_ptr<TlsProber> HttpsFirewallRoutine::CreateAndExecuteTlsProber(
network::NetworkContextGetter network_context_getter,
net::HostPortPair host_port_pair,
bool negotiate_tls,
TlsProber::TlsProbeCompleteCallback callback) {
return std::make_unique<TlsProber>(std::move(network_context_getter),
host_port_pair, negotiate_tls,
std::move(callback));
}
} // namespace network_diagnostics
} // namespace ash
|