[email protected] | 24f49bd3 | 2011-04-19 19:01:14 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | 7541206c | 2010-02-19 20:24:06 | [diff] [blame] | 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 "net/proxy/proxy_bypass_rules.h" |
| 6 | |
[email protected] | 7286e3fc | 2011-07-19 22:13:24 | [diff] [blame] | 7 | #include "base/stl_util.h" |
brettw | d97eede | 2015-07-06 22:09:00 | [diff] [blame] | 8 | #include "base/strings/pattern.h" |
[email protected] | fc9be580 | 2013-06-11 10:56:51 | [diff] [blame] | 9 | #include "base/strings/string_number_conversions.h" |
[email protected] | d069c11a | 2013-04-13 00:01:55 | [diff] [blame] | 10 | #include "base/strings/string_piece.h" |
[email protected] | f4ebe77 | 2013-02-02 00:21:39 | [diff] [blame] | 11 | #include "base/strings/string_tokenizer.h" |
eroman | ff374b7f | 2014-09-24 23:42:00 | [diff] [blame] | 12 | #include "base/strings/string_util.h" |
| 13 | #include "base/strings/stringprintf.h" |
| 14 | #include "net/base/host_port_pair.h" |
eroman | c9a6b72 | 2015-06-03 22:19:00 | [diff] [blame] | 15 | #include "net/base/ip_address_number.h" |
tfarina | 7a4a7fd | 2016-01-20 14:23:44 | [diff] [blame^] | 16 | #include "net/base/url_util.h" |
[email protected] | 7541206c | 2010-02-19 20:24:06 | [diff] [blame] | 17 | |
| 18 | namespace net { |
| 19 | |
| 20 | namespace { |
| 21 | |
| 22 | class HostnamePatternRule : public ProxyBypassRules::Rule { |
| 23 | public: |
| 24 | HostnamePatternRule(const std::string& optional_scheme, |
| 25 | const std::string& hostname_pattern, |
| 26 | int optional_port) |
brettw | 8e2106d | 2015-08-11 19:30:22 | [diff] [blame] | 27 | : optional_scheme_(base::ToLowerASCII(optional_scheme)), |
| 28 | hostname_pattern_(base::ToLowerASCII(hostname_pattern)), |
| 29 | optional_port_(optional_port) {} |
[email protected] | 7541206c | 2010-02-19 20:24:06 | [diff] [blame] | 30 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 31 | bool Matches(const GURL& url) const override { |
[email protected] | 7541206c | 2010-02-19 20:24:06 | [diff] [blame] | 32 | if (optional_port_ != -1 && url.EffectiveIntPort() != optional_port_) |
| 33 | return false; // Didn't match port expectation. |
| 34 | |
| 35 | if (!optional_scheme_.empty() && url.scheme() != optional_scheme_) |
| 36 | return false; // Didn't match scheme expectation. |
| 37 | |
| 38 | // Note it is necessary to lower-case the host, since GURL uses capital |
| 39 | // letters for percent-escaped characters. |
brettw | 8e2106d | 2015-08-11 19:30:22 | [diff] [blame] | 40 | return base::MatchPattern(url.host(), hostname_pattern_); |
[email protected] | 7541206c | 2010-02-19 20:24:06 | [diff] [blame] | 41 | } |
| 42 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 43 | std::string ToString() const override { |
[email protected] | 7541206c | 2010-02-19 20:24:06 | [diff] [blame] | 44 | std::string str; |
| 45 | if (!optional_scheme_.empty()) |
[email protected] | a77fa2dc | 2010-11-15 12:11:11 | [diff] [blame] | 46 | base::StringAppendF(&str, "%s://", optional_scheme_.c_str()); |
[email protected] | 7541206c | 2010-02-19 20:24:06 | [diff] [blame] | 47 | str += hostname_pattern_; |
| 48 | if (optional_port_ != -1) |
[email protected] | a77fa2dc | 2010-11-15 12:11:11 | [diff] [blame] | 49 | base::StringAppendF(&str, ":%d", optional_port_); |
[email protected] | 7541206c | 2010-02-19 20:24:06 | [diff] [blame] | 50 | return str; |
| 51 | } |
| 52 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 53 | Rule* Clone() const override { |
[email protected] | af37009 | 2010-12-01 02:06:26 | [diff] [blame] | 54 | return new HostnamePatternRule(optional_scheme_, |
| 55 | hostname_pattern_, |
| 56 | optional_port_); |
| 57 | } |
| 58 | |
[email protected] | 7541206c | 2010-02-19 20:24:06 | [diff] [blame] | 59 | private: |
| 60 | const std::string optional_scheme_; |
| 61 | const std::string hostname_pattern_; |
| 62 | const int optional_port_; |
| 63 | }; |
| 64 | |
| 65 | class BypassLocalRule : public ProxyBypassRules::Rule { |
| 66 | public: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 67 | bool Matches(const GURL& url) const override { |
[email protected] | 7541206c | 2010-02-19 20:24:06 | [diff] [blame] | 68 | const std::string& host = url.host(); |
| 69 | if (host == "127.0.0.1" || host == "[::1]") |
| 70 | return true; |
| 71 | return host.find('.') == std::string::npos; |
| 72 | } |
| 73 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 74 | std::string ToString() const override { return "<local>"; } |
[email protected] | af37009 | 2010-12-01 02:06:26 | [diff] [blame] | 75 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 76 | Rule* Clone() const override { return new BypassLocalRule(); } |
[email protected] | 7541206c | 2010-02-19 20:24:06 | [diff] [blame] | 77 | }; |
| 78 | |
[email protected] | 5439283 | 2010-06-08 23:25:04 | [diff] [blame] | 79 | // Rule for matching a URL that is an IP address, if that IP address falls |
| 80 | // within a certain numeric range. For example, you could use this rule to |
| 81 | // match all the IPs in the CIDR block 10.10.3.4/24. |
| 82 | class BypassIPBlockRule : public ProxyBypassRules::Rule { |
| 83 | public: |
| 84 | // |ip_prefix| + |prefix_length| define the IP block to match. |
| 85 | BypassIPBlockRule(const std::string& description, |
| 86 | const std::string& optional_scheme, |
| 87 | const IPAddressNumber& ip_prefix, |
| 88 | size_t prefix_length_in_bits) |
| 89 | : description_(description), |
| 90 | optional_scheme_(optional_scheme), |
| 91 | ip_prefix_(ip_prefix), |
| 92 | prefix_length_in_bits_(prefix_length_in_bits) { |
| 93 | } |
| 94 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 95 | bool Matches(const GURL& url) const override { |
[email protected] | 5439283 | 2010-06-08 23:25:04 | [diff] [blame] | 96 | if (!url.HostIsIPAddress()) |
| 97 | return false; |
| 98 | |
| 99 | if (!optional_scheme_.empty() && url.scheme() != optional_scheme_) |
| 100 | return false; // Didn't match scheme expectation. |
| 101 | |
| 102 | // Parse the input IP literal to a number. |
| 103 | IPAddressNumber ip_number; |
| 104 | if (!ParseIPLiteralToNumber(url.HostNoBrackets(), &ip_number)) |
| 105 | return false; |
| 106 | |
| 107 | // Test if it has the expected prefix. |
| 108 | return IPNumberMatchesPrefix(ip_number, ip_prefix_, |
| 109 | prefix_length_in_bits_); |
| 110 | } |
| 111 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 112 | std::string ToString() const override { return description_; } |
[email protected] | 5439283 | 2010-06-08 23:25:04 | [diff] [blame] | 113 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 114 | Rule* Clone() const override { |
[email protected] | af37009 | 2010-12-01 02:06:26 | [diff] [blame] | 115 | return new BypassIPBlockRule(description_, |
| 116 | optional_scheme_, |
| 117 | ip_prefix_, |
| 118 | prefix_length_in_bits_); |
| 119 | } |
| 120 | |
[email protected] | 5439283 | 2010-06-08 23:25:04 | [diff] [blame] | 121 | private: |
| 122 | const std::string description_; |
| 123 | const std::string optional_scheme_; |
| 124 | const IPAddressNumber ip_prefix_; |
| 125 | const size_t prefix_length_in_bits_; |
| 126 | }; |
| 127 | |
[email protected] | 7541206c | 2010-02-19 20:24:06 | [diff] [blame] | 128 | // Returns true if the given string represents an IP address. |
eroman | ff374b7f | 2014-09-24 23:42:00 | [diff] [blame] | 129 | // IPv6 addresses are expected to be bracketed. |
[email protected] | 7541206c | 2010-02-19 20:24:06 | [diff] [blame] | 130 | bool IsIPAddress(const std::string& domain) { |
| 131 | // From GURL::HostIsIPAddress() |
[email protected] | ce97ca36 | 2014-04-30 11:35:46 | [diff] [blame] | 132 | url::RawCanonOutputT<char, 128> ignored_output; |
| 133 | url::CanonHostInfo host_info; |
| 134 | url::Component domain_comp(0, domain.size()); |
| 135 | url::CanonicalizeIPAddress(domain.c_str(), domain_comp, &ignored_output, |
| 136 | &host_info); |
[email protected] | 7541206c | 2010-02-19 20:24:06 | [diff] [blame] | 137 | return host_info.IsIPAddress(); |
| 138 | } |
| 139 | |
| 140 | } // namespace |
| 141 | |
[email protected] | af37009 | 2010-12-01 02:06:26 | [diff] [blame] | 142 | ProxyBypassRules::Rule::Rule() { |
| 143 | } |
| 144 | |
| 145 | ProxyBypassRules::Rule::~Rule() { |
| 146 | } |
| 147 | |
| 148 | bool ProxyBypassRules::Rule::Equals(const Rule& rule) const { |
| 149 | return ToString() == rule.ToString(); |
| 150 | } |
| 151 | |
[email protected] | 9349cfb | 2010-08-31 18:00:53 | [diff] [blame] | 152 | ProxyBypassRules::ProxyBypassRules() { |
| 153 | } |
| 154 | |
[email protected] | af37009 | 2010-12-01 02:06:26 | [diff] [blame] | 155 | ProxyBypassRules::ProxyBypassRules(const ProxyBypassRules& rhs) { |
| 156 | AssignFrom(rhs); |
[email protected] | 9349cfb | 2010-08-31 18:00:53 | [diff] [blame] | 157 | } |
| 158 | |
[email protected] | 7541206c | 2010-02-19 20:24:06 | [diff] [blame] | 159 | ProxyBypassRules::~ProxyBypassRules() { |
[email protected] | af37009 | 2010-12-01 02:06:26 | [diff] [blame] | 160 | Clear(); |
[email protected] | 7541206c | 2010-02-19 20:24:06 | [diff] [blame] | 161 | } |
| 162 | |
[email protected] | 9349cfb | 2010-08-31 18:00:53 | [diff] [blame] | 163 | ProxyBypassRules& ProxyBypassRules::operator=(const ProxyBypassRules& rhs) { |
[email protected] | af37009 | 2010-12-01 02:06:26 | [diff] [blame] | 164 | AssignFrom(rhs); |
[email protected] | 9349cfb | 2010-08-31 18:00:53 | [diff] [blame] | 165 | return *this; |
| 166 | } |
| 167 | |
[email protected] | 7541206c | 2010-02-19 20:24:06 | [diff] [blame] | 168 | bool ProxyBypassRules::Matches(const GURL& url) const { |
| 169 | for (RuleList::const_iterator it = rules_.begin(); it != rules_.end(); ++it) { |
| 170 | if ((*it)->Matches(url)) |
| 171 | return true; |
| 172 | } |
| 173 | return false; |
| 174 | } |
| 175 | |
| 176 | bool ProxyBypassRules::Equals(const ProxyBypassRules& other) const { |
[email protected] | af37009 | 2010-12-01 02:06:26 | [diff] [blame] | 177 | if (rules_.size() != other.rules_.size()) |
[email protected] | 7541206c | 2010-02-19 20:24:06 | [diff] [blame] | 178 | return false; |
| 179 | |
| 180 | for (size_t i = 0; i < rules_.size(); ++i) { |
[email protected] | af37009 | 2010-12-01 02:06:26 | [diff] [blame] | 181 | if (!rules_[i]->Equals(*other.rules_[i])) |
[email protected] | 7541206c | 2010-02-19 20:24:06 | [diff] [blame] | 182 | return false; |
| 183 | } |
| 184 | return true; |
| 185 | } |
| 186 | |
| 187 | void ProxyBypassRules::ParseFromString(const std::string& raw) { |
| 188 | ParseFromStringInternal(raw, false); |
| 189 | } |
| 190 | |
| 191 | void ProxyBypassRules::ParseFromStringUsingSuffixMatching( |
| 192 | const std::string& raw) { |
| 193 | ParseFromStringInternal(raw, true); |
| 194 | } |
| 195 | |
| 196 | bool ProxyBypassRules::AddRuleForHostname(const std::string& optional_scheme, |
| 197 | const std::string& hostname_pattern, |
| 198 | int optional_port) { |
| 199 | if (hostname_pattern.empty()) |
| 200 | return false; |
| 201 | |
[email protected] | af37009 | 2010-12-01 02:06:26 | [diff] [blame] | 202 | rules_.push_back(new HostnamePatternRule(optional_scheme, |
| 203 | hostname_pattern, |
| 204 | optional_port)); |
[email protected] | 7541206c | 2010-02-19 20:24:06 | [diff] [blame] | 205 | return true; |
| 206 | } |
| 207 | |
| 208 | void ProxyBypassRules::AddRuleToBypassLocal() { |
[email protected] | af37009 | 2010-12-01 02:06:26 | [diff] [blame] | 209 | rules_.push_back(new BypassLocalRule); |
[email protected] | 7541206c | 2010-02-19 20:24:06 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | bool ProxyBypassRules::AddRuleFromString(const std::string& raw) { |
| 213 | return AddRuleFromStringInternalWithLogging(raw, false); |
| 214 | } |
| 215 | |
[email protected] | 1a59719 | 2010-07-09 16:58:38 | [diff] [blame] | 216 | bool ProxyBypassRules::AddRuleFromStringUsingSuffixMatching( |
| 217 | const std::string& raw) { |
| 218 | return AddRuleFromStringInternalWithLogging(raw, true); |
| 219 | } |
| 220 | |
[email protected] | 24f49bd3 | 2011-04-19 19:01:14 | [diff] [blame] | 221 | std::string ProxyBypassRules::ToString() const { |
| 222 | std::string result; |
| 223 | for (RuleList::const_iterator rule(rules_.begin()); |
| 224 | rule != rules_.end(); |
| 225 | ++rule) { |
| 226 | result += (*rule)->ToString(); |
| 227 | result += ";"; |
| 228 | } |
| 229 | return result; |
| 230 | } |
| 231 | |
[email protected] | 7541206c | 2010-02-19 20:24:06 | [diff] [blame] | 232 | void ProxyBypassRules::Clear() { |
[email protected] | af37009 | 2010-12-01 02:06:26 | [diff] [blame] | 233 | STLDeleteElements(&rules_); |
| 234 | } |
| 235 | |
| 236 | void ProxyBypassRules::AssignFrom(const ProxyBypassRules& other) { |
| 237 | Clear(); |
| 238 | |
| 239 | // Make a copy of the rules list. |
| 240 | for (RuleList::const_iterator it = other.rules_.begin(); |
| 241 | it != other.rules_.end(); ++it) { |
| 242 | rules_.push_back((*it)->Clone()); |
| 243 | } |
[email protected] | 7541206c | 2010-02-19 20:24:06 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | void ProxyBypassRules::ParseFromStringInternal( |
| 247 | const std::string& raw, |
| 248 | bool use_hostname_suffix_matching) { |
| 249 | Clear(); |
| 250 | |
[email protected] | f4ebe77 | 2013-02-02 00:21:39 | [diff] [blame] | 251 | base::StringTokenizer entries(raw, ",;"); |
[email protected] | 7541206c | 2010-02-19 20:24:06 | [diff] [blame] | 252 | while (entries.GetNext()) { |
| 253 | AddRuleFromStringInternalWithLogging(entries.token(), |
| 254 | use_hostname_suffix_matching); |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | bool ProxyBypassRules::AddRuleFromStringInternal( |
| 259 | const std::string& raw_untrimmed, |
| 260 | bool use_hostname_suffix_matching) { |
| 261 | std::string raw; |
[email protected] | 8af69c6c | 2014-03-03 19:05:31 | [diff] [blame] | 262 | base::TrimWhitespaceASCII(raw_untrimmed, base::TRIM_ALL, &raw); |
[email protected] | 7541206c | 2010-02-19 20:24:06 | [diff] [blame] | 263 | |
| 264 | // This is the special syntax used by WinInet's bypass list -- we allow it |
| 265 | // on all platforms and interpret it the same way. |
brettw | bc17d2c8 | 2015-06-09 22:39:08 | [diff] [blame] | 266 | if (base::LowerCaseEqualsASCII(raw, "<local>")) { |
[email protected] | 7541206c | 2010-02-19 20:24:06 | [diff] [blame] | 267 | AddRuleToBypassLocal(); |
| 268 | return true; |
| 269 | } |
| 270 | |
| 271 | // Extract any scheme-restriction. |
| 272 | std::string::size_type scheme_pos = raw.find("://"); |
| 273 | std::string scheme; |
| 274 | if (scheme_pos != std::string::npos) { |
| 275 | scheme = raw.substr(0, scheme_pos); |
| 276 | raw = raw.substr(scheme_pos + 3); |
| 277 | if (scheme.empty()) |
| 278 | return false; |
| 279 | } |
| 280 | |
| 281 | if (raw.empty()) |
| 282 | return false; |
| 283 | |
| 284 | // If there is a forward slash in the input, it is probably a CIDR style |
| 285 | // mask. |
| 286 | if (raw.find('/') != std::string::npos) { |
[email protected] | 5439283 | 2010-06-08 23:25:04 | [diff] [blame] | 287 | IPAddressNumber ip_prefix; |
| 288 | size_t prefix_length_in_bits; |
| 289 | |
| 290 | if (!ParseCIDRBlock(raw, &ip_prefix, &prefix_length_in_bits)) |
| 291 | return false; |
| 292 | |
[email protected] | af37009 | 2010-12-01 02:06:26 | [diff] [blame] | 293 | rules_.push_back( |
| 294 | new BypassIPBlockRule(raw, scheme, ip_prefix, prefix_length_in_bits)); |
[email protected] | 5439283 | 2010-06-08 23:25:04 | [diff] [blame] | 295 | |
| 296 | return true; |
[email protected] | 7541206c | 2010-02-19 20:24:06 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | // Check if we have an <ip-address>[:port] input. We need to treat this |
| 300 | // separately since the IP literal may not be in a canonical form. |
| 301 | std::string host; |
| 302 | int port; |
| 303 | if (ParseHostAndPort(raw, &host, &port)) { |
eroman | ff374b7f | 2014-09-24 23:42:00 | [diff] [blame] | 304 | // Note that HostPortPair is used to merely to convert any IPv6 literals to |
| 305 | // a URL-safe format that can be used by canonicalization below. |
| 306 | std::string bracketed_host = HostPortPair(host, 80).HostForURL(); |
| 307 | if (IsIPAddress(bracketed_host)) { |
[email protected] | 7541206c | 2010-02-19 20:24:06 | [diff] [blame] | 308 | // Canonicalize the IP literal before adding it as a string pattern. |
eroman | ff374b7f | 2014-09-24 23:42:00 | [diff] [blame] | 309 | GURL tmp_url("http://" + bracketed_host); |
[email protected] | 7541206c | 2010-02-19 20:24:06 | [diff] [blame] | 310 | return AddRuleForHostname(scheme, tmp_url.host(), port); |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | // Otherwise assume we have <hostname-pattern>[:port]. |
| 315 | std::string::size_type pos_colon = raw.rfind(':'); |
| 316 | host = raw; |
| 317 | port = -1; |
| 318 | if (pos_colon != std::string::npos) { |
[email protected] | eb72b27 | 2011-12-19 16:10:55 | [diff] [blame] | 319 | if (!base::StringToInt(base::StringPiece(raw.begin() + pos_colon + 1, |
| 320 | raw.end()), |
| 321 | &port) || |
[email protected] | 7541206c | 2010-02-19 20:24:06 | [diff] [blame] | 322 | (port < 0 || port > 0xFFFF)) { |
| 323 | return false; // Port was invalid. |
| 324 | } |
| 325 | raw = raw.substr(0, pos_colon); |
| 326 | } |
| 327 | |
| 328 | // Special-case hostnames that begin with a period. |
| 329 | // For example, we remap ".google.com" --> "*.google.com". |
brettw | 3a2c690 | 2015-07-06 19:43:29 | [diff] [blame] | 330 | if (base::StartsWith(raw, ".", base::CompareCase::SENSITIVE)) |
[email protected] | 7541206c | 2010-02-19 20:24:06 | [diff] [blame] | 331 | raw = "*" + raw; |
| 332 | |
| 333 | // If suffix matching was asked for, make sure the pattern starts with a |
| 334 | // wildcard. |
brettw | 3a2c690 | 2015-07-06 19:43:29 | [diff] [blame] | 335 | if (use_hostname_suffix_matching && |
| 336 | !base::StartsWith(raw, "*", base::CompareCase::SENSITIVE)) |
[email protected] | 7541206c | 2010-02-19 20:24:06 | [diff] [blame] | 337 | raw = "*" + raw; |
| 338 | |
| 339 | return AddRuleForHostname(scheme, raw, port); |
| 340 | } |
| 341 | |
| 342 | bool ProxyBypassRules::AddRuleFromStringInternalWithLogging( |
| 343 | const std::string& raw, |
| 344 | bool use_hostname_suffix_matching) { |
[email protected] | e5e6166 | 2010-03-11 02:32:39 | [diff] [blame] | 345 | return AddRuleFromStringInternal(raw, use_hostname_suffix_matching); |
[email protected] | 7541206c | 2010-02-19 20:24:06 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | } // namespace net |