blob: f31fa7be086bda9d72e097d0406940b8c0ee7905 [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
[email protected]7286e3fc2011-07-19 22:13:247#include "base/stl_util.h"
brettwd97eede2015-07-06 22:09:008#include "base/strings/pattern.h"
[email protected]fc9be5802013-06-11 10:56:519#include "base/strings/string_number_conversions.h"
[email protected]d069c11a2013-04-13 00:01:5510#include "base/strings/string_piece.h"
[email protected]f4ebe772013-02-02 00:21:3911#include "base/strings/string_tokenizer.h"
eromanff374b7f2014-09-24 23:42:0012#include "base/strings/string_util.h"
13#include "base/strings/stringprintf.h"
14#include "net/base/host_port_pair.h"
eromanc9a6b722015-06-03 22:19:0015#include "net/base/ip_address_number.h"
tfarina7a4a7fd2016-01-20 14:23:4416#include "net/base/url_util.h"
[email protected]7541206c2010-02-19 20:24:0617
18namespace net {
19
20namespace {
21
22class HostnamePatternRule : public ProxyBypassRules::Rule {
23 public:
24 HostnamePatternRule(const std::string& optional_scheme,
25 const std::string& hostname_pattern,
26 int optional_port)
brettw8e2106d2015-08-11 19:30:2227 : optional_scheme_(base::ToLowerASCII(optional_scheme)),
28 hostname_pattern_(base::ToLowerASCII(hostname_pattern)),
29 optional_port_(optional_port) {}
[email protected]7541206c2010-02-19 20:24:0630
dchengb03027d2014-10-21 12:00:2031 bool Matches(const GURL& url) const override {
[email protected]7541206c2010-02-19 20:24:0632 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.
brettw8e2106d2015-08-11 19:30:2240 return base::MatchPattern(url.host(), hostname_pattern_);
[email protected]7541206c2010-02-19 20:24:0641 }
42
dchengb03027d2014-10-21 12:00:2043 std::string ToString() const override {
[email protected]7541206c2010-02-19 20:24:0644 std::string str;
45 if (!optional_scheme_.empty())
[email protected]a77fa2dc2010-11-15 12:11:1146 base::StringAppendF(&str, "%s://", optional_scheme_.c_str());
[email protected]7541206c2010-02-19 20:24:0647 str += hostname_pattern_;
48 if (optional_port_ != -1)
[email protected]a77fa2dc2010-11-15 12:11:1149 base::StringAppendF(&str, ":%d", optional_port_);
[email protected]7541206c2010-02-19 20:24:0650 return str;
51 }
52
dchengb03027d2014-10-21 12:00:2053 Rule* Clone() const override {
[email protected]af370092010-12-01 02:06:2654 return new HostnamePatternRule(optional_scheme_,
55 hostname_pattern_,
56 optional_port_);
57 }
58
[email protected]7541206c2010-02-19 20:24:0659 private:
60 const std::string optional_scheme_;
61 const std::string hostname_pattern_;
62 const int optional_port_;
63};
64
65class BypassLocalRule : public ProxyBypassRules::Rule {
66 public:
dchengb03027d2014-10-21 12:00:2067 bool Matches(const GURL& url) const override {
[email protected]7541206c2010-02-19 20:24:0668 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
dchengb03027d2014-10-21 12:00:2074 std::string ToString() const override { return "<local>"; }
[email protected]af370092010-12-01 02:06:2675
dchengb03027d2014-10-21 12:00:2076 Rule* Clone() const override { return new BypassLocalRule(); }
[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,
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
dchengb03027d2014-10-21 12:00:2095 bool Matches(const GURL& url) const override {
[email protected]54392832010-06-08 23:25:0496 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
dchengb03027d2014-10-21 12:00:20112 std::string ToString() const override { return description_; }
[email protected]54392832010-06-08 23:25:04113
dchengb03027d2014-10-21 12:00:20114 Rule* Clone() const override {
[email protected]af370092010-12-01 02:06:26115 return new BypassIPBlockRule(description_,
116 optional_scheme_,
117 ip_prefix_,
118 prefix_length_in_bits_);
119 }
120
[email protected]54392832010-06-08 23:25:04121 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]7541206c2010-02-19 20:24:06128// Returns true if the given string represents an IP address.
eromanff374b7f2014-09-24 23:42:00129// IPv6 addresses are expected to be bracketed.
[email protected]7541206c2010-02-19 20:24:06130bool IsIPAddress(const std::string& domain) {
131 // From GURL::HostIsIPAddress()
[email protected]ce97ca362014-04-30 11:35:46132 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]7541206c2010-02-19 20:24:06137 return host_info.IsIPAddress();
138}
139
140} // namespace
141
[email protected]af370092010-12-01 02:06:26142ProxyBypassRules::Rule::Rule() {
143}
144
145ProxyBypassRules::Rule::~Rule() {
146}
147
148bool ProxyBypassRules::Rule::Equals(const Rule& rule) const {
149 return ToString() == rule.ToString();
150}
151
[email protected]9349cfb2010-08-31 18:00:53152ProxyBypassRules::ProxyBypassRules() {
153}
154
[email protected]af370092010-12-01 02:06:26155ProxyBypassRules::ProxyBypassRules(const ProxyBypassRules& rhs) {
156 AssignFrom(rhs);
[email protected]9349cfb2010-08-31 18:00:53157}
158
[email protected]7541206c2010-02-19 20:24:06159ProxyBypassRules::~ProxyBypassRules() {
[email protected]af370092010-12-01 02:06:26160 Clear();
[email protected]7541206c2010-02-19 20:24:06161}
162
[email protected]9349cfb2010-08-31 18:00:53163ProxyBypassRules& ProxyBypassRules::operator=(const ProxyBypassRules& rhs) {
[email protected]af370092010-12-01 02:06:26164 AssignFrom(rhs);
[email protected]9349cfb2010-08-31 18:00:53165 return *this;
166}
167
[email protected]7541206c2010-02-19 20:24:06168bool 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
176bool ProxyBypassRules::Equals(const ProxyBypassRules& other) const {
[email protected]af370092010-12-01 02:06:26177 if (rules_.size() != other.rules_.size())
[email protected]7541206c2010-02-19 20:24:06178 return false;
179
180 for (size_t i = 0; i < rules_.size(); ++i) {
[email protected]af370092010-12-01 02:06:26181 if (!rules_[i]->Equals(*other.rules_[i]))
[email protected]7541206c2010-02-19 20:24:06182 return false;
183 }
184 return true;
185}
186
187void ProxyBypassRules::ParseFromString(const std::string& raw) {
188 ParseFromStringInternal(raw, false);
189}
190
191void ProxyBypassRules::ParseFromStringUsingSuffixMatching(
192 const std::string& raw) {
193 ParseFromStringInternal(raw, true);
194}
195
196bool 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]af370092010-12-01 02:06:26202 rules_.push_back(new HostnamePatternRule(optional_scheme,
203 hostname_pattern,
204 optional_port));
[email protected]7541206c2010-02-19 20:24:06205 return true;
206}
207
208void ProxyBypassRules::AddRuleToBypassLocal() {
[email protected]af370092010-12-01 02:06:26209 rules_.push_back(new BypassLocalRule);
[email protected]7541206c2010-02-19 20:24:06210}
211
212bool ProxyBypassRules::AddRuleFromString(const std::string& raw) {
213 return AddRuleFromStringInternalWithLogging(raw, false);
214}
215
[email protected]1a597192010-07-09 16:58:38216bool ProxyBypassRules::AddRuleFromStringUsingSuffixMatching(
217 const std::string& raw) {
218 return AddRuleFromStringInternalWithLogging(raw, true);
219}
220
[email protected]24f49bd32011-04-19 19:01:14221std::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]7541206c2010-02-19 20:24:06232void ProxyBypassRules::Clear() {
[email protected]af370092010-12-01 02:06:26233 STLDeleteElements(&rules_);
234}
235
236void 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]7541206c2010-02-19 20:24:06244}
245
246void ProxyBypassRules::ParseFromStringInternal(
247 const std::string& raw,
248 bool use_hostname_suffix_matching) {
249 Clear();
250
[email protected]f4ebe772013-02-02 00:21:39251 base::StringTokenizer entries(raw, ",;");
[email protected]7541206c2010-02-19 20:24:06252 while (entries.GetNext()) {
253 AddRuleFromStringInternalWithLogging(entries.token(),
254 use_hostname_suffix_matching);
255 }
256}
257
258bool ProxyBypassRules::AddRuleFromStringInternal(
259 const std::string& raw_untrimmed,
260 bool use_hostname_suffix_matching) {
261 std::string raw;
[email protected]8af69c6c2014-03-03 19:05:31262 base::TrimWhitespaceASCII(raw_untrimmed, base::TRIM_ALL, &raw);
[email protected]7541206c2010-02-19 20:24:06263
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.
brettwbc17d2c82015-06-09 22:39:08266 if (base::LowerCaseEqualsASCII(raw, "<local>")) {
[email protected]7541206c2010-02-19 20:24:06267 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]54392832010-06-08 23:25:04287 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]af370092010-12-01 02:06:26293 rules_.push_back(
294 new BypassIPBlockRule(raw, scheme, ip_prefix, prefix_length_in_bits));
[email protected]54392832010-06-08 23:25:04295
296 return true;
[email protected]7541206c2010-02-19 20:24:06297 }
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)) {
eromanff374b7f2014-09-24 23:42:00304 // 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]7541206c2010-02-19 20:24:06308 // Canonicalize the IP literal before adding it as a string pattern.
eromanff374b7f2014-09-24 23:42:00309 GURL tmp_url("http://" + bracketed_host);
[email protected]7541206c2010-02-19 20:24:06310 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]eb72b272011-12-19 16:10:55319 if (!base::StringToInt(base::StringPiece(raw.begin() + pos_colon + 1,
320 raw.end()),
321 &port) ||
[email protected]7541206c2010-02-19 20:24:06322 (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".
brettw3a2c6902015-07-06 19:43:29330 if (base::StartsWith(raw, ".", base::CompareCase::SENSITIVE))
[email protected]7541206c2010-02-19 20:24:06331 raw = "*" + raw;
332
333 // If suffix matching was asked for, make sure the pattern starts with a
334 // wildcard.
brettw3a2c6902015-07-06 19:43:29335 if (use_hostname_suffix_matching &&
336 !base::StartsWith(raw, "*", base::CompareCase::SENSITIVE))
[email protected]7541206c2010-02-19 20:24:06337 raw = "*" + raw;
338
339 return AddRuleForHostname(scheme, raw, port);
340}
341
342bool ProxyBypassRules::AddRuleFromStringInternalWithLogging(
343 const std::string& raw,
344 bool use_hostname_suffix_matching) {
[email protected]e5e61662010-03-11 02:32:39345 return AddRuleFromStringInternal(raw, use_hostname_suffix_matching);
[email protected]7541206c2010-02-19 20:24:06346}
347
348} // namespace net