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