blob: 2d22ae909c70fdf04c1be124adfc08a1c63cc49e [file] [log] [blame]
[email protected]24f49bd32011-04-19 19:01:141// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]7541206c2010-02-19 20:24:062// 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
brettwd97eede2015-07-06 22:09:007#include "base/strings/pattern.h"
[email protected]d069c11a2013-04-13 00:01:558#include "base/strings/string_piece.h"
[email protected]f4ebe772013-02-02 00:21:399#include "base/strings/string_tokenizer.h"
eromanff374b7f2014-09-24 23:42:0010#include "base/strings/string_util.h"
11#include "base/strings/stringprintf.h"
12#include "net/base/host_port_pair.h"
martijn579658b2016-03-20 20:22:0913#include "net/base/ip_address.h"
eroman55fa65c52016-03-24 18:57:4914#include "net/base/parse_number.h"
tfarina7a4a7fd2016-01-20 14:23:4415#include "net/base/url_util.h"
[email protected]7541206c2010-02-19 20:24:0616
17namespace net {
18
19namespace {
20
21class HostnamePatternRule : public ProxyBypassRules::Rule {
22 public:
23 HostnamePatternRule(const std::string& optional_scheme,
24 const std::string& hostname_pattern,
25 int optional_port)
brettw8e2106d2015-08-11 19:30:2226 : optional_scheme_(base::ToLowerASCII(optional_scheme)),
27 hostname_pattern_(base::ToLowerASCII(hostname_pattern)),
28 optional_port_(optional_port) {}
[email protected]7541206c2010-02-19 20:24:0629
dchengb03027d2014-10-21 12:00:2030 bool Matches(const GURL& url) const override {
[email protected]7541206c2010-02-19 20:24:0631 if (optional_port_ != -1 && url.EffectiveIntPort() != optional_port_)
32 return false; // Didn't match port expectation.
33
34 if (!optional_scheme_.empty() && url.scheme() != optional_scheme_)
35 return false; // Didn't match scheme expectation.
36
37 // Note it is necessary to lower-case the host, since GURL uses capital
38 // letters for percent-escaped characters.
brettw8e2106d2015-08-11 19:30:2239 return base::MatchPattern(url.host(), hostname_pattern_);
[email protected]7541206c2010-02-19 20:24:0640 }
41
dchengb03027d2014-10-21 12:00:2042 std::string ToString() const override {
[email protected]7541206c2010-02-19 20:24:0643 std::string str;
44 if (!optional_scheme_.empty())
[email protected]a77fa2dc2010-11-15 12:11:1145 base::StringAppendF(&str, "%s://", optional_scheme_.c_str());
[email protected]7541206c2010-02-19 20:24:0646 str += hostname_pattern_;
47 if (optional_port_ != -1)
[email protected]a77fa2dc2010-11-15 12:11:1148 base::StringAppendF(&str, ":%d", optional_port_);
[email protected]7541206c2010-02-19 20:24:0649 return str;
50 }
51
avica04a622016-10-07 00:28:4752 std::unique_ptr<Rule> Clone() const override {
Jeremy Roman0579ed62017-08-29 15:56:1953 return std::make_unique<HostnamePatternRule>(
avica04a622016-10-07 00:28:4754 optional_scheme_, hostname_pattern_, optional_port_);
[email protected]af370092010-12-01 02:06:2655 }
56
[email protected]7541206c2010-02-19 20:24:0657 private:
58 const std::string optional_scheme_;
59 const std::string hostname_pattern_;
60 const int optional_port_;
61};
62
63class BypassLocalRule : public ProxyBypassRules::Rule {
64 public:
dchengb03027d2014-10-21 12:00:2065 bool Matches(const GURL& url) const override {
[email protected]7541206c2010-02-19 20:24:0666 const std::string& host = url.host();
67 if (host == "127.0.0.1" || host == "[::1]")
68 return true;
69 return host.find('.') == std::string::npos;
70 }
71
dchengb03027d2014-10-21 12:00:2072 std::string ToString() const override { return "<local>"; }
[email protected]af370092010-12-01 02:06:2673
avica04a622016-10-07 00:28:4774 std::unique_ptr<Rule> Clone() const override {
Jeremy Roman0579ed62017-08-29 15:56:1975 return std::make_unique<BypassLocalRule>();
avica04a622016-10-07 00:28:4776 }
[email protected]7541206c2010-02-19 20:24:0677};
78
[email protected]54392832010-06-08 23:25:0479// 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.
82class 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,
martijn579658b2016-03-20 20:22:0987 const IPAddress& ip_prefix,
[email protected]54392832010-06-08 23:25:0488 size_t prefix_length_in_bits)
89 : description_(description),
90 optional_scheme_(optional_scheme),
91 ip_prefix_(ip_prefix),
martijn579658b2016-03-20 20:22:0992 prefix_length_in_bits_(prefix_length_in_bits) {}
[email protected]54392832010-06-08 23:25:0493
dchengb03027d2014-10-21 12:00:2094 bool Matches(const GURL& url) const override {
[email protected]54392832010-06-08 23:25:0495 if (!url.HostIsIPAddress())
96 return false;
97
98 if (!optional_scheme_.empty() && url.scheme() != optional_scheme_)
99 return false; // Didn't match scheme expectation.
100
101 // Parse the input IP literal to a number.
martijn579658b2016-03-20 20:22:09102 IPAddress ip_address;
ricea1c0de2f2017-07-03 08:21:43103 if (!ip_address.AssignFromIPLiteral(url.HostNoBracketsPiece()))
[email protected]54392832010-06-08 23:25:04104 return false;
105
106 // Test if it has the expected prefix.
martijn579658b2016-03-20 20:22:09107 return IPAddressMatchesPrefix(ip_address, ip_prefix_,
108 prefix_length_in_bits_);
[email protected]54392832010-06-08 23:25:04109 }
110
dchengb03027d2014-10-21 12:00:20111 std::string ToString() const override { return description_; }
[email protected]54392832010-06-08 23:25:04112
avica04a622016-10-07 00:28:47113 std::unique_ptr<Rule> Clone() const override {
Jeremy Roman0579ed62017-08-29 15:56:19114 return std::make_unique<BypassIPBlockRule>(
avica04a622016-10-07 00:28:47115 description_, optional_scheme_, ip_prefix_, prefix_length_in_bits_);
[email protected]af370092010-12-01 02:06:26116 }
117
[email protected]54392832010-06-08 23:25:04118 private:
119 const std::string description_;
120 const std::string optional_scheme_;
martijn579658b2016-03-20 20:22:09121 const IPAddress ip_prefix_;
[email protected]54392832010-06-08 23:25:04122 const size_t prefix_length_in_bits_;
123};
124
[email protected]7541206c2010-02-19 20:24:06125// Returns true if the given string represents an IP address.
eromanff374b7f2014-09-24 23:42:00126// IPv6 addresses are expected to be bracketed.
[email protected]7541206c2010-02-19 20:24:06127bool IsIPAddress(const std::string& domain) {
128 // From GURL::HostIsIPAddress()
[email protected]ce97ca362014-04-30 11:35:46129 url::RawCanonOutputT<char, 128> ignored_output;
130 url::CanonHostInfo host_info;
131 url::Component domain_comp(0, domain.size());
132 url::CanonicalizeIPAddress(domain.c_str(), domain_comp, &ignored_output,
133 &host_info);
[email protected]7541206c2010-02-19 20:24:06134 return host_info.IsIPAddress();
135}
136
137} // namespace
138
[email protected]af370092010-12-01 02:06:26139ProxyBypassRules::Rule::Rule() {
140}
141
142ProxyBypassRules::Rule::~Rule() {
143}
144
145bool ProxyBypassRules::Rule::Equals(const Rule& rule) const {
146 return ToString() == rule.ToString();
147}
148
[email protected]9349cfb2010-08-31 18:00:53149ProxyBypassRules::ProxyBypassRules() {
150}
151
[email protected]af370092010-12-01 02:06:26152ProxyBypassRules::ProxyBypassRules(const ProxyBypassRules& rhs) {
153 AssignFrom(rhs);
[email protected]9349cfb2010-08-31 18:00:53154}
155
[email protected]7541206c2010-02-19 20:24:06156ProxyBypassRules::~ProxyBypassRules() {
[email protected]af370092010-12-01 02:06:26157 Clear();
[email protected]7541206c2010-02-19 20:24:06158}
159
[email protected]9349cfb2010-08-31 18:00:53160ProxyBypassRules& ProxyBypassRules::operator=(const ProxyBypassRules& rhs) {
[email protected]af370092010-12-01 02:06:26161 AssignFrom(rhs);
[email protected]9349cfb2010-08-31 18:00:53162 return *this;
163}
164
[email protected]7541206c2010-02-19 20:24:06165bool ProxyBypassRules::Matches(const GURL& url) const {
166 for (RuleList::const_iterator it = rules_.begin(); it != rules_.end(); ++it) {
167 if ((*it)->Matches(url))
168 return true;
169 }
170 return false;
171}
172
173bool ProxyBypassRules::Equals(const ProxyBypassRules& other) const {
[email protected]af370092010-12-01 02:06:26174 if (rules_.size() != other.rules_.size())
[email protected]7541206c2010-02-19 20:24:06175 return false;
176
177 for (size_t i = 0; i < rules_.size(); ++i) {
[email protected]af370092010-12-01 02:06:26178 if (!rules_[i]->Equals(*other.rules_[i]))
[email protected]7541206c2010-02-19 20:24:06179 return false;
180 }
181 return true;
182}
183
184void ProxyBypassRules::ParseFromString(const std::string& raw) {
185 ParseFromStringInternal(raw, false);
186}
187
188void ProxyBypassRules::ParseFromStringUsingSuffixMatching(
189 const std::string& raw) {
190 ParseFromStringInternal(raw, true);
191}
192
193bool ProxyBypassRules::AddRuleForHostname(const std::string& optional_scheme,
194 const std::string& hostname_pattern,
195 int optional_port) {
196 if (hostname_pattern.empty())
197 return false;
198
Jeremy Roman0579ed62017-08-29 15:56:19199 rules_.push_back(std::make_unique<HostnamePatternRule>(
avica04a622016-10-07 00:28:47200 optional_scheme, hostname_pattern, optional_port));
[email protected]7541206c2010-02-19 20:24:06201 return true;
202}
203
204void ProxyBypassRules::AddRuleToBypassLocal() {
Jeremy Roman0579ed62017-08-29 15:56:19205 rules_.push_back(std::make_unique<BypassLocalRule>());
[email protected]7541206c2010-02-19 20:24:06206}
207
208bool ProxyBypassRules::AddRuleFromString(const std::string& raw) {
209 return AddRuleFromStringInternalWithLogging(raw, false);
210}
211
[email protected]1a597192010-07-09 16:58:38212bool ProxyBypassRules::AddRuleFromStringUsingSuffixMatching(
213 const std::string& raw) {
214 return AddRuleFromStringInternalWithLogging(raw, true);
215}
216
[email protected]24f49bd32011-04-19 19:01:14217std::string ProxyBypassRules::ToString() const {
218 std::string result;
219 for (RuleList::const_iterator rule(rules_.begin());
220 rule != rules_.end();
221 ++rule) {
222 result += (*rule)->ToString();
223 result += ";";
224 }
225 return result;
226}
227
[email protected]7541206c2010-02-19 20:24:06228void ProxyBypassRules::Clear() {
avica04a622016-10-07 00:28:47229 rules_.clear();
[email protected]af370092010-12-01 02:06:26230}
231
232void ProxyBypassRules::AssignFrom(const ProxyBypassRules& other) {
233 Clear();
234
235 // Make a copy of the rules list.
236 for (RuleList::const_iterator it = other.rules_.begin();
237 it != other.rules_.end(); ++it) {
238 rules_.push_back((*it)->Clone());
239 }
[email protected]7541206c2010-02-19 20:24:06240}
241
242void ProxyBypassRules::ParseFromStringInternal(
243 const std::string& raw,
244 bool use_hostname_suffix_matching) {
245 Clear();
246
[email protected]f4ebe772013-02-02 00:21:39247 base::StringTokenizer entries(raw, ",;");
[email protected]7541206c2010-02-19 20:24:06248 while (entries.GetNext()) {
249 AddRuleFromStringInternalWithLogging(entries.token(),
250 use_hostname_suffix_matching);
251 }
252}
253
254bool ProxyBypassRules::AddRuleFromStringInternal(
255 const std::string& raw_untrimmed,
256 bool use_hostname_suffix_matching) {
257 std::string raw;
[email protected]8af69c6c2014-03-03 19:05:31258 base::TrimWhitespaceASCII(raw_untrimmed, base::TRIM_ALL, &raw);
[email protected]7541206c2010-02-19 20:24:06259
260 // This is the special syntax used by WinInet's bypass list -- we allow it
261 // on all platforms and interpret it the same way.
brettwbc17d2c82015-06-09 22:39:08262 if (base::LowerCaseEqualsASCII(raw, "<local>")) {
[email protected]7541206c2010-02-19 20:24:06263 AddRuleToBypassLocal();
264 return true;
265 }
266
267 // Extract any scheme-restriction.
268 std::string::size_type scheme_pos = raw.find("://");
269 std::string scheme;
270 if (scheme_pos != std::string::npos) {
271 scheme = raw.substr(0, scheme_pos);
272 raw = raw.substr(scheme_pos + 3);
273 if (scheme.empty())
274 return false;
275 }
276
277 if (raw.empty())
278 return false;
279
280 // If there is a forward slash in the input, it is probably a CIDR style
281 // mask.
282 if (raw.find('/') != std::string::npos) {
martijn579658b2016-03-20 20:22:09283 IPAddress ip_prefix;
[email protected]54392832010-06-08 23:25:04284 size_t prefix_length_in_bits;
285
286 if (!ParseCIDRBlock(raw, &ip_prefix, &prefix_length_in_bits))
287 return false;
288
Jeremy Roman0579ed62017-08-29 15:56:19289 rules_.push_back(std::make_unique<BypassIPBlockRule>(
avica04a622016-10-07 00:28:47290 raw, scheme, ip_prefix, prefix_length_in_bits));
[email protected]54392832010-06-08 23:25:04291
292 return true;
[email protected]7541206c2010-02-19 20:24:06293 }
294
295 // Check if we have an <ip-address>[:port] input. We need to treat this
296 // separately since the IP literal may not be in a canonical form.
297 std::string host;
298 int port;
299 if (ParseHostAndPort(raw, &host, &port)) {
eromane6264fd2016-03-02 22:46:30300 // TODO(eroman): HostForURL() below DCHECKs() when |host| contains an
301 // embedded NULL.
302 if (host.find('\0') != std::string::npos)
303 return false;
304
eromanff374b7f2014-09-24 23:42:00305 // Note that HostPortPair is used to merely to convert any IPv6 literals to
306 // a URL-safe format that can be used by canonicalization below.
307 std::string bracketed_host = HostPortPair(host, 80).HostForURL();
308 if (IsIPAddress(bracketed_host)) {
[email protected]7541206c2010-02-19 20:24:06309 // Canonicalize the IP literal before adding it as a string pattern.
eromanff374b7f2014-09-24 23:42:00310 GURL tmp_url("http://" + bracketed_host);
[email protected]7541206c2010-02-19 20:24:06311 return AddRuleForHostname(scheme, tmp_url.host(), port);
312 }
313 }
314
315 // Otherwise assume we have <hostname-pattern>[:port].
316 std::string::size_type pos_colon = raw.rfind(':');
[email protected]7541206c2010-02-19 20:24:06317 port = -1;
318 if (pos_colon != std::string::npos) {
eromanae40d9ba2016-04-11 18:40:26319 if (!ParseInt32(base::StringPiece(raw.begin() + pos_colon + 1, raw.end()),
320 ParseIntFormat::NON_NEGATIVE, &port) ||
eroman55fa65c52016-03-24 18:57:49321 port > 0xFFFF) {
[email protected]7541206c2010-02-19 20:24:06322 return false; // Port was invalid.
323 }
324 raw = raw.substr(0, pos_colon);
325 }
326
327 // Special-case hostnames that begin with a period.
328 // For example, we remap ".google.com" --> "*.google.com".
brettw3a2c6902015-07-06 19:43:29329 if (base::StartsWith(raw, ".", base::CompareCase::SENSITIVE))
[email protected]7541206c2010-02-19 20:24:06330 raw = "*" + raw;
331
332 // If suffix matching was asked for, make sure the pattern starts with a
333 // wildcard.
brettw3a2c6902015-07-06 19:43:29334 if (use_hostname_suffix_matching &&
335 !base::StartsWith(raw, "*", base::CompareCase::SENSITIVE))
[email protected]7541206c2010-02-19 20:24:06336 raw = "*" + raw;
337
338 return AddRuleForHostname(scheme, raw, port);
339}
340
341bool ProxyBypassRules::AddRuleFromStringInternalWithLogging(
342 const std::string& raw,
343 bool use_hostname_suffix_matching) {
[email protected]e5e61662010-03-11 02:32:39344 return AddRuleFromStringInternal(raw, use_hostname_suffix_matching);
[email protected]7541206c2010-02-19 20:24:06345}
346
347} // namespace net