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 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
|
// 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 "net/base/scheme_host_port_matcher_rule.h"
#include "base/strings/pattern.h"
#include "base/strings/strcat.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/trace_event/memory_usage_estimator.h"
#include "net/base/host_port_pair.h"
#include "net/base/parse_number.h"
#include "net/base/url_util.h"
#include "url/url_util.h"
namespace net {
namespace {
std::string AddBracketsIfIPv6(const IPAddress& ip_address) {
std::string ip_host = ip_address.ToString();
if (ip_address.IsIPv6())
return base::StringPrintf("[%s]", ip_host.c_str());
return ip_host;
}
} // namespace
// static
std::unique_ptr<SchemeHostPortMatcherRule>
SchemeHostPortMatcherRule::FromUntrimmedRawString(
base::StringPiece raw_untrimmed) {
base::StringPiece raw =
base::TrimWhitespaceASCII(raw_untrimmed, base::TRIM_ALL);
// Extract any scheme-restriction.
std::string::size_type scheme_pos = raw.find("://");
std::string scheme;
if (scheme_pos != std::string::npos) {
scheme = std::string(raw.substr(0, scheme_pos));
raw = raw.substr(scheme_pos + 3);
if (scheme.empty())
return nullptr;
}
if (raw.empty())
return nullptr;
// If there is a forward slash in the input, it is probably a CIDR style
// mask.
if (raw.find('/') != std::string::npos) {
IPAddress ip_prefix;
size_t prefix_length_in_bits;
if (!ParseCIDRBlock(raw, &ip_prefix, &prefix_length_in_bits))
return nullptr;
return std::make_unique<SchemeHostPortMatcherIPBlockRule>(
std::string(raw), scheme, ip_prefix, prefix_length_in_bits);
}
// Check if we have an <ip-address>[:port] input. We need to treat this
// separately since the IP literal may not be in a canonical form.
std::string host;
int port;
if (ParseHostAndPort(raw, &host, &port)) {
IPAddress ip_address;
if (ip_address.AssignFromIPLiteral(host)) {
// Instead of -1, 0 is invalid for IPEndPoint.
int adjusted_port = port == -1 ? 0 : port;
return std::make_unique<SchemeHostPortMatcherIPHostRule>(
scheme, IPEndPoint(ip_address, adjusted_port));
}
}
// Otherwise assume we have <hostname-pattern>[:port].
std::string::size_type pos_colon = raw.rfind(':');
port = -1;
if (pos_colon != std::string::npos) {
if (!ParseInt32(
base::MakeStringPiece(raw.begin() + pos_colon + 1, raw.end()),
ParseIntFormat::NON_NEGATIVE, &port) ||
port > 0xFFFF) {
return nullptr; // Port was invalid.
}
raw = raw.substr(0, pos_colon);
}
// Special-case hostnames that begin with a period.
// For example, we remap ".google.com" --> "*.google.com".
std::string hostname_pattern;
if (base::StartsWith(raw, ".", base::CompareCase::SENSITIVE)) {
hostname_pattern = base::StrCat({"*", raw});
} else {
hostname_pattern = std::string(raw);
}
return std::make_unique<SchemeHostPortMatcherHostnamePatternRule>(
scheme, hostname_pattern, port);
}
bool SchemeHostPortMatcherRule::IsHostnamePatternRule() const {
return false;
}
#if !BUILDFLAG(CRONET_BUILD)
size_t SchemeHostPortMatcherRule::EstimateMemoryUsage() const {
return 0;
}
#endif // !BUILDFLAG(CRONET_BUILD)
SchemeHostPortMatcherHostnamePatternRule::
SchemeHostPortMatcherHostnamePatternRule(
const std::string& optional_scheme,
const std::string& hostname_pattern,
int optional_port)
: optional_scheme_(base::ToLowerASCII(optional_scheme)),
hostname_pattern_(base::ToLowerASCII(hostname_pattern)),
optional_port_(optional_port) {
// |hostname_pattern| shouldn't be an IP address.
DCHECK(!url::HostIsIPAddress(hostname_pattern));
}
SchemeHostPortMatcherResult SchemeHostPortMatcherHostnamePatternRule::Evaluate(
const GURL& url) const {
if (optional_port_ != -1 && url.EffectiveIntPort() != optional_port_) {
// Didn't match port expectation.
return SchemeHostPortMatcherResult::kNoMatch;
}
if (!optional_scheme_.empty() && url.scheme() != optional_scheme_) {
// Didn't match scheme expectation.
return SchemeHostPortMatcherResult::kNoMatch;
}
// Note it is necessary to lower-case the host, since GURL uses capital
// letters for percent-escaped characters.
return base::MatchPattern(url.host(), hostname_pattern_)
? SchemeHostPortMatcherResult::kInclude
: SchemeHostPortMatcherResult::kNoMatch;
}
std::string SchemeHostPortMatcherHostnamePatternRule::ToString() const {
std::string str;
if (!optional_scheme_.empty())
base::StringAppendF(&str, "%s://", optional_scheme_.c_str());
str += hostname_pattern_;
if (optional_port_ != -1)
base::StringAppendF(&str, ":%d", optional_port_);
return str;
}
bool SchemeHostPortMatcherHostnamePatternRule::IsHostnamePatternRule() const {
return true;
}
std::unique_ptr<SchemeHostPortMatcherHostnamePatternRule>
SchemeHostPortMatcherHostnamePatternRule::GenerateSuffixMatchingRule() const {
if (!base::StartsWith(hostname_pattern_, "*", base::CompareCase::SENSITIVE)) {
return std::make_unique<SchemeHostPortMatcherHostnamePatternRule>(
optional_scheme_, "*" + hostname_pattern_, optional_port_);
}
// return a new SchemeHostPortMatcherHostNamePatternRule with the same data.
return std::make_unique<SchemeHostPortMatcherHostnamePatternRule>(
optional_scheme_, hostname_pattern_, optional_port_);
}
#if !BUILDFLAG(CRONET_BUILD)
size_t SchemeHostPortMatcherHostnamePatternRule::EstimateMemoryUsage() const {
return base::trace_event::EstimateMemoryUsage(optional_scheme_) +
base::trace_event::EstimateMemoryUsage(hostname_pattern_);
}
#endif // !BUILDFLAG(CRONET_BUILD)
SchemeHostPortMatcherIPHostRule::SchemeHostPortMatcherIPHostRule(
const std::string& optional_scheme,
const IPEndPoint& ip_end_point)
: optional_scheme_(base::ToLowerASCII(optional_scheme)),
ip_host_(AddBracketsIfIPv6(ip_end_point.address())),
optional_port_(ip_end_point.port()) {}
SchemeHostPortMatcherResult SchemeHostPortMatcherIPHostRule::Evaluate(
const GURL& url) const {
if (optional_port_ != 0 && url.EffectiveIntPort() != optional_port_) {
// Didn't match port expectation.
return SchemeHostPortMatcherResult::kNoMatch;
}
if (!optional_scheme_.empty() && url.scheme() != optional_scheme_) {
// Didn't match scheme expectation.
return SchemeHostPortMatcherResult::kNoMatch;
}
// Note it is necessary to lower-case the host, since GURL uses capital
// letters for percent-escaped characters.
return base::MatchPattern(url.host(), ip_host_)
? SchemeHostPortMatcherResult::kInclude
: SchemeHostPortMatcherResult::kNoMatch;
}
std::string SchemeHostPortMatcherIPHostRule::ToString() const {
std::string str;
if (!optional_scheme_.empty())
base::StringAppendF(&str, "%s://", optional_scheme_.c_str());
str += ip_host_;
if (optional_port_ != 0)
base::StringAppendF(&str, ":%d", optional_port_);
return str;
}
#if !BUILDFLAG(CRONET_BUILD)
size_t SchemeHostPortMatcherIPHostRule::EstimateMemoryUsage() const {
return base::trace_event::EstimateMemoryUsage(optional_scheme_) +
base::trace_event::EstimateMemoryUsage(ip_host_);
}
#endif // !BUILDFLAG(CRONET_BUILD)
SchemeHostPortMatcherIPBlockRule::SchemeHostPortMatcherIPBlockRule(
const std::string& description,
const std::string& optional_scheme,
const IPAddress& ip_prefix,
size_t prefix_length_in_bits)
: description_(description),
optional_scheme_(optional_scheme),
ip_prefix_(ip_prefix),
prefix_length_in_bits_(prefix_length_in_bits) {}
SchemeHostPortMatcherResult SchemeHostPortMatcherIPBlockRule::Evaluate(
const GURL& url) const {
if (!url.HostIsIPAddress())
return SchemeHostPortMatcherResult::kNoMatch;
if (!optional_scheme_.empty() && url.scheme() != optional_scheme_) {
// Didn't match scheme expectation.
return SchemeHostPortMatcherResult::kNoMatch;
}
// Parse the input IP literal to a number.
IPAddress ip_address;
if (!ip_address.AssignFromIPLiteral(url.HostNoBracketsPiece()))
return SchemeHostPortMatcherResult::kNoMatch;
// Test if it has the expected prefix.
return IPAddressMatchesPrefix(ip_address, ip_prefix_, prefix_length_in_bits_)
? SchemeHostPortMatcherResult::kInclude
: SchemeHostPortMatcherResult::kNoMatch;
}
std::string SchemeHostPortMatcherIPBlockRule::ToString() const {
return description_;
}
#if !BUILDFLAG(CRONET_BUILD)
size_t SchemeHostPortMatcherIPBlockRule::EstimateMemoryUsage() const {
return base::trace_event::EstimateMemoryUsage(description_) +
base::trace_event::EstimateMemoryUsage(optional_scheme_) +
base::trace_event::EstimateMemoryUsage(ip_prefix_);
}
#endif // !BUILDFLAG(CRONET_BUILD)
} // namespace net
|