• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4  *
5  * The contents of this file are subject to the Mozilla Public License Version
6  * 1.1 (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  * http://www.mozilla.org/MPL/
9  *
10  * Software distributed under the License is distributed on an "AS IS" basis,
11  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12  * for the specific language governing rights and limitations under the
13  * License.
14  *
15  * The Original Code is Mozilla Effective-TLD Service
16  *
17  * The Initial Developer of the Original Code is
18  * Google Inc.
19  * Portions created by the Initial Developer are Copyright (C) 2006
20  * the Initial Developer. All Rights Reserved.
21  *
22  * Contributor(s):
23  *   Pamela Greene <pamg.bugs@gmail.com> (original author)
24  *   Daniel Witte <dwitte@stanford.edu>
25  *
26  * Alternatively, the contents of this file may be used under the terms of
27  * either the GNU General Public License Version 2 or later (the "GPL"), or
28  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29  * in which case the provisions of the GPL or the LGPL are applicable instead
30  * of those above. If you wish to allow use of your version of this file only
31  * under the terms of either the GPL or the LGPL, and not to allow others to
32  * use your version of this file under the terms of the MPL, indicate your
33  * decision by deleting the provisions above and replace them with the notice
34  * and other provisions required by the GPL or the LGPL. If you do not delete
35  * the provisions above, a recipient may use your version of this file under
36  * the terms of any one of the MPL, the GPL or the LGPL.
37  *
38  * ***** END LICENSE BLOCK ***** */
39 
40 #include "net/base/registry_controlled_domain.h"
41 
42 #include "base/logging.h"
43 #include "base/singleton.h"
44 #include "base/string_util.h"
45 #include "googleurl/src/gurl.h"
46 #include "googleurl/src/url_parse.h"
47 #include "net/base/net_module.h"
48 #include "net/base/net_util.h"
49 
50 #include "effective_tld_names.cc"
51 
52 namespace net {
53 
54 static const int kExceptionRule = 1;
55 static const int kWildcardRule = 2;
56 
RegistryControlledDomainService()57 RegistryControlledDomainService::RegistryControlledDomainService()
58     : find_domain_function_(Perfect_Hash::FindDomain) {
59 }
60 
61 // static
GetDomainAndRegistry(const GURL & gurl)62 std::string RegistryControlledDomainService::GetDomainAndRegistry(
63     const GURL& gurl) {
64   const url_parse::Component host =
65       gurl.parsed_for_possibly_invalid_spec().host;
66   if ((host.len <= 0) || gurl.HostIsIPAddress())
67     return std::string();
68   return GetDomainAndRegistryImpl(std::string(
69       gurl.possibly_invalid_spec().data() + host.begin, host.len));
70 }
71 
72 // static
GetDomainAndRegistry(const std::string & host)73 std::string RegistryControlledDomainService::GetDomainAndRegistry(
74     const std::string& host) {
75   url_canon::CanonHostInfo host_info;
76   const std::string canon_host(net::CanonicalizeHost(host, &host_info));
77   if (canon_host.empty() || host_info.IsIPAddress())
78     return std::string();
79   return GetDomainAndRegistryImpl(canon_host);
80 }
81 
82 // static
GetDomainAndRegistry(const std::wstring & host)83 std::string RegistryControlledDomainService::GetDomainAndRegistry(
84     const std::wstring& host) {
85   url_canon::CanonHostInfo host_info;
86   const std::string canon_host(net::CanonicalizeHost(host, &host_info));
87   if (canon_host.empty() || host_info.IsIPAddress())
88     return std::string();
89   return GetDomainAndRegistryImpl(canon_host);
90 }
91 
92 // static
SameDomainOrHost(const GURL & gurl1,const GURL & gurl2)93 bool RegistryControlledDomainService::SameDomainOrHost(const GURL& gurl1,
94                                                        const GURL& gurl2) {
95   // See if both URLs have a known domain + registry, and those values are the
96   // same.
97   const std::string domain1(GetDomainAndRegistry(gurl1));
98   const std::string domain2(GetDomainAndRegistry(gurl2));
99   if (!domain1.empty() || !domain2.empty())
100     return domain1 == domain2;
101 
102   // No domains.  See if the hosts are identical.
103   const url_parse::Component host1 =
104       gurl1.parsed_for_possibly_invalid_spec().host;
105   const url_parse::Component host2 =
106       gurl2.parsed_for_possibly_invalid_spec().host;
107   if ((host1.len <= 0) || (host1.len != host2.len))
108     return false;
109   return !strncmp(gurl1.possibly_invalid_spec().data() + host1.begin,
110                   gurl2.possibly_invalid_spec().data() + host2.begin,
111                   host1.len);
112 }
113 
114 // static
GetRegistryLength(const GURL & gurl,bool allow_unknown_registries)115 size_t RegistryControlledDomainService::GetRegistryLength(
116     const GURL& gurl,
117     bool allow_unknown_registries) {
118   const url_parse::Component host =
119       gurl.parsed_for_possibly_invalid_spec().host;
120   if (host.len <= 0)
121     return std::string::npos;
122   if (gurl.HostIsIPAddress())
123     return 0;
124   return GetInstance()->GetRegistryLengthImpl(
125       std::string(gurl.possibly_invalid_spec().data() + host.begin, host.len),
126       allow_unknown_registries);
127 }
128 
129 // static
GetRegistryLength(const std::string & host,bool allow_unknown_registries)130 size_t RegistryControlledDomainService::GetRegistryLength(
131     const std::string& host,
132     bool allow_unknown_registries) {
133   url_canon::CanonHostInfo host_info;
134   const std::string canon_host(net::CanonicalizeHost(host, &host_info));
135   if (canon_host.empty())
136     return std::string::npos;
137   if (host_info.IsIPAddress())
138     return 0;
139   return GetInstance()->GetRegistryLengthImpl(canon_host,
140                                               allow_unknown_registries);
141 }
142 
143 // static
GetRegistryLength(const std::wstring & host,bool allow_unknown_registries)144 size_t RegistryControlledDomainService::GetRegistryLength(
145     const std::wstring& host,
146     bool allow_unknown_registries) {
147   url_canon::CanonHostInfo host_info;
148   const std::string canon_host(net::CanonicalizeHost(host, &host_info));
149   if (canon_host.empty())
150     return std::string::npos;
151   if (host_info.IsIPAddress())
152     return 0;
153   return GetInstance()->GetRegistryLengthImpl(canon_host,
154                                               allow_unknown_registries);
155 }
156 
157 // static
GetDomainAndRegistryImpl(const std::string & host)158 std::string RegistryControlledDomainService::GetDomainAndRegistryImpl(
159     const std::string& host) {
160   DCHECK(!host.empty());
161 
162   // Find the length of the registry for this host.
163   const size_t registry_length =
164       GetInstance()->GetRegistryLengthImpl(host, true);
165   if ((registry_length == std::string::npos) || (registry_length == 0))
166     return std::string();  // No registry.
167   // The "2" in this next line is 1 for the dot, plus a 1-char minimum preceding
168   // subcomponent length.
169   DCHECK(host.length() >= 2);
170   if (registry_length > (host.length() - 2)) {
171     NOTREACHED() <<
172         "Host does not have at least one subcomponent before registry!";
173     return std::string();
174   }
175 
176   // Move past the dot preceding the registry, and search for the next previous
177   // dot.  Return the host from after that dot, or the whole host when there is
178   // no dot.
179   const size_t dot = host.rfind('.', host.length() - registry_length - 2);
180   if (dot == std::string::npos)
181     return host;
182   return host.substr(dot + 1);
183 }
184 
GetRegistryLengthImpl(const std::string & host,bool allow_unknown_registries)185 size_t RegistryControlledDomainService::GetRegistryLengthImpl(
186     const std::string& host,
187     bool allow_unknown_registries) {
188   DCHECK(!host.empty());
189 
190   // Skip leading dots.
191   const size_t host_check_begin = host.find_first_not_of('.');
192   if (host_check_begin == std::string::npos)
193     return 0;  // Host is only dots.
194 
195   // A single trailing dot isn't relevant in this determination, but does need
196   // to be included in the final returned length.
197   size_t host_check_len = host.length();
198   if (host[host_check_len - 1] == '.') {
199     --host_check_len;
200     DCHECK(host_check_len > 0);  // If this weren't true, the host would be ".",
201                                  // and we'd have already returned above.
202     if (host[host_check_len - 1] == '.')
203       return 0;  // Multiple trailing dots.
204   }
205 
206   // Walk up the domain tree, most specific to least specific,
207   // looking for matches at each level.
208   size_t prev_start = std::string::npos;
209   size_t curr_start = host_check_begin;
210   size_t next_dot = host.find('.', curr_start);
211   if (next_dot >= host_check_len)  // Catches std::string::npos as well.
212     return 0;  // This can't have a registry + domain.
213   while (1) {
214     const char* domain_str = host.data() + curr_start;
215     int domain_length = host_check_len - curr_start;
216     const DomainRule* rule = find_domain_function_(domain_str, domain_length);
217 
218     // We need to compare the string after finding a match because the
219     // no-collisions of perfect hashing only refers to items in the set.  Since
220     // we're searching for arbitrary domains, there could be collisions.
221     if (rule &&
222         base::strncasecmp(domain_str, rule->name, domain_length) == 0) {
223       // Exception rules override wildcard rules when the domain is an exact
224       // match, but wildcards take precedence when there's a subdomain.
225       if (rule->type == kWildcardRule && (prev_start != std::string::npos)) {
226         // If prev_start == host_check_begin, then the host is the registry
227         // itself, so return 0.
228         return (prev_start == host_check_begin) ?
229             0 : (host.length() - prev_start);
230       }
231 
232       if (rule->type == kExceptionRule) {
233         if (next_dot == std::string::npos) {
234           // If we get here, we had an exception rule with no dots (e.g.
235           // "!foo").  This would only be valid if we had a corresponding
236           // wildcard rule, which would have to be "*".  But we explicitly
237           // disallow that case, so this kind of rule is invalid.
238           NOTREACHED() << "Invalid exception rule";
239           return 0;
240         }
241         return host.length() - next_dot - 1;
242       }
243 
244       // If curr_start == host_check_begin, then the host is the registry
245       // itself, so return 0.
246       return (curr_start == host_check_begin) ?
247           0 : (host.length() - curr_start);
248     }
249 
250     if (next_dot >= host_check_len)  // Catches std::string::npos as well.
251       break;
252 
253     prev_start = curr_start;
254     curr_start = next_dot + 1;
255     next_dot = host.find('.', curr_start);
256   }
257 
258   // No rule found in the registry.  curr_start now points to the first
259   // character of the last subcomponent of the host, so if we allow unknown
260   // registries, return the length of this subcomponent.
261   return allow_unknown_registries ? (host.length() - curr_start) : 0;
262 }
263 
264 static RegistryControlledDomainService* test_instance_;
265 
266 // static
SetInstance(RegistryControlledDomainService * instance)267 RegistryControlledDomainService* RegistryControlledDomainService::SetInstance(
268     RegistryControlledDomainService* instance) {
269   RegistryControlledDomainService* old_instance = test_instance_;
270   test_instance_ = instance;
271   return old_instance;
272 }
273 
274 // static
GetInstance()275 RegistryControlledDomainService* RegistryControlledDomainService::GetInstance()
276 {
277   if (test_instance_)
278     return test_instance_;
279 
280   return Singleton<RegistryControlledDomainService>::get();
281 }
282 
283 // static
UseFindDomainFunction(FindDomainPtr function)284 void RegistryControlledDomainService::UseFindDomainFunction(
285     FindDomainPtr function) {
286   RegistryControlledDomainService* instance = GetInstance();
287   instance->find_domain_function_ = function;
288 }
289 
290 }  // namespace net
291