summaryrefslogtreecommitdiffstats
path: root/net/base/registry_controlled_domain.cc
blob: b573d17e558100e0349b69b7a4b18355820d0027 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
//* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is Mozilla Effective-TLD Service
 *
 * The Initial Developer of the Original Code is
 * Google Inc.
 * Portions created by the Initial Developer are Copyright (C) 2006
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *   Pamela Greene <pamg.bugs@gmail.com> (original author)
 *   Daniel Witte <dwitte@stanford.edu>
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 *
 * ***** END LICENSE BLOCK ***** */

#include <windows.h>

#include "base/logging.h"
#include "base/string_util.h"
#include "googleurl/src/gurl.h"
#include "googleurl/src/url_parse.h"
#include "net/base/net_module.h"
#include "net/base/net_resources.h"
#include "net/base/net_util.h"
#include "net/base/registry_controlled_domain.h"

// This list of rules is used by unit tests and any other time that the main
// resource file is not available.  It should be kept exceedingly short to
// avoid impacting unit test performance.
static const char kDefaultDomainData[] = "com\n"
                                         "edu\n"
                                         "gov\n"
                                         "net\n"
                                         "org\n"
                                         "co.uk\n";

// static
std::string RegistryControlledDomainService::GetDomainAndRegistry(
    const GURL& gurl) {
  const url_parse::Component host =
      gurl.parsed_for_possibly_invalid_spec().host;
  if ((host.len <= 0) || gurl.HostIsIPAddress())
    return std::string();
  return GetDomainAndRegistryImpl(std::string(
      gurl.possibly_invalid_spec().data() + host.begin, host.len));
}

// static
std::string RegistryControlledDomainService::GetDomainAndRegistry(
    const std::string& host) {
  bool is_ip_address;
  const std::string canon_host(net_util::CanonicalizeHost(host,
                                                          &is_ip_address));
  if (canon_host.empty() || is_ip_address)
    return std::string();
  return GetDomainAndRegistryImpl(canon_host);
}

// static
std::string RegistryControlledDomainService::GetDomainAndRegistry(
    const std::wstring& host) {
  bool is_ip_address;
  const std::string canon_host(net_util::CanonicalizeHost(host,
                                                          &is_ip_address));
  if (canon_host.empty() || is_ip_address)
    return std::string();
  return GetDomainAndRegistryImpl(canon_host);
}

// static
bool RegistryControlledDomainService::SameDomainOrHost(const GURL& gurl1,
                                                       const GURL& gurl2) {
  // See if both URLs have a known domain + registry, and those values are the
  // same.
  const std::string domain1(GetDomainAndRegistry(gurl1));
  const std::string domain2(GetDomainAndRegistry(gurl2));
  if (!domain1.empty() || !domain2.empty())
    return domain1 == domain2;

  // No domains.  See if the hosts are identical.
  const url_parse::Component host1 =
      gurl1.parsed_for_possibly_invalid_spec().host;
  const url_parse::Component host2 =
      gurl2.parsed_for_possibly_invalid_spec().host;
  if ((host1.len <= 0) || (host1.len != host2.len))
    return false;
  return !strncmp(gurl1.possibly_invalid_spec().data() + host1.begin,
                  gurl2.possibly_invalid_spec().data() + host2.begin,
                  host1.len);
}

// static
size_t RegistryControlledDomainService::GetRegistryLength(
    const GURL& gurl,
    bool allow_unknown_registries) {
  const url_parse::Component host =
      gurl.parsed_for_possibly_invalid_spec().host;
  if (host.len <= 0)
    return std::string::npos;
  if (gurl.HostIsIPAddress())
    return 0;
  return GetInstance()->GetRegistryLengthImpl(
      std::string(gurl.possibly_invalid_spec().data() + host.begin, host.len),
      allow_unknown_registries);
}

// static
size_t RegistryControlledDomainService::GetRegistryLength(
    const std::string& host,
    bool allow_unknown_registries) {
  bool is_ip_address;
  const std::string canon_host(net_util::CanonicalizeHost(host,
                                                          &is_ip_address));
  if (canon_host.empty())
    return std::string::npos;
  if (is_ip_address)
    return 0;
  return GetInstance()->GetRegistryLengthImpl(canon_host,
                                              allow_unknown_registries);
}

// static
size_t RegistryControlledDomainService::GetRegistryLength(
    const std::wstring& host,
    bool allow_unknown_registries) {
  bool is_ip_address;
  const std::string canon_host(net_util::CanonicalizeHost(host,
                                                          &is_ip_address));
  if (canon_host.empty())
    return std::string::npos;
  if (is_ip_address)
    return 0;
  return GetInstance()->GetRegistryLengthImpl(canon_host,
                                              allow_unknown_registries);
}

// static
std::string RegistryControlledDomainService::GetDomainAndRegistryImpl(
    const std::string& host) {
  DCHECK(!host.empty());

  // Find the length of the registry for this host.
  const size_t registry_length =
      GetInstance()->GetRegistryLengthImpl(host, true);
  if ((registry_length == std::string::npos) || (registry_length == 0))
    return std::string();  // No registry.
  // The "2" in this next line is 1 for the dot, plus a 1-char minimum preceding
  // subcomponent length.
  if (registry_length > (host.length() - 2)) {
    NOTREACHED() <<
        "Host does not have at least one subcomponent before registry!";
    return std::string();
  }

  // Move past the dot preceding the registry, and search for the next previous
  // dot.  Return the host from after that dot, or the whole host when there is
  // no dot.
  const size_t dot = host.rfind('.', host.length() - registry_length - 2);
  if (dot == std::string::npos)
    return host;
  return host.substr(dot + 1);
}

size_t RegistryControlledDomainService::GetRegistryLengthImpl(
    const std::string& host,
    bool allow_unknown_registries) {
  DCHECK(!host.empty());

  // Skip leading dots.
  const size_t host_check_begin = host.find_first_not_of('.');
  if (host_check_begin == std::string::npos)
    return 0;  // Host is only dots.

  // A single trailing dot isn't relevant in this determination, but does need
  // to be included in the final returned length.
  size_t host_check_len = host.length();
  if (host[host_check_len - 1] == '.') {
    --host_check_len;
    DCHECK(host_check_len > 0);  // If this weren't true, the host would be ".",
                                 // and we'd have already returned above.
    if (host[host_check_len - 1] == '.')
      return 0;  // Multiple trailing dots.
  }

  // Walk up the domain tree, most specific to least specific,
  // looking for matches at each level.
  StringSegment match;
  size_t prev_start = std::string::npos;
  size_t curr_start = host_check_begin;
  size_t next_dot = host.find('.', curr_start);
  if (next_dot >= host_check_len)  // Catches std::string::npos as well.
    return 0;  // This can't have a registry + domain.
  while (1) {
    match.Set(host.data(), curr_start, host_check_len - curr_start);
    DomainMap::iterator iter = domain_map_.find(match);
    if (iter != domain_map_.end()) {
      DomainEntry entry = iter->second;
      // Exception rules override wildcard rules when the domain is an exact
      // match, but wildcards take precedence when there's a subdomain.
      if (entry.wildcard && (prev_start != std::string::npos)) {
        // If prev_start == host_check_begin, then the host is the registry
        // itself, so return 0.
        return (prev_start == host_check_begin) ?
            0 : (host.length() - prev_start);
      }

      if (entry.exception) {
        if (next_dot == std::string::npos) {
          // If we get here, we had an exception rule with no dots (e.g.
          // "!foo").  This would only be valid if we had a corresponding
          // wildcard rule, which would have to be "*".  But we explicitly
          // disallow that case, so this kind of rule is invalid.
          NOTREACHED() << "Invalid exception rule";
          return 0;
        }
        return host.length() - next_dot - 1;
      }

      // If curr_start == host_check_begin, then the host is the registry
      // itself, so return 0.
      return (curr_start == host_check_begin) ?
          0 : (host.length() - curr_start);
    }

    if (next_dot >= host_check_len)  // Catches std::string::npos as well.
      break;

    prev_start = curr_start;
    curr_start = next_dot + 1;
    next_dot = host.find('.', curr_start);
  }

  // No rule found in the registry.  curr_start now points to the first
  // character of the last subcomponent of the host, so if we allow unknown
  // registries, return the length of this subcomponent.
  return allow_unknown_registries ? (host.length() - curr_start) : 0;
}

RegistryControlledDomainService* RegistryControlledDomainService::instance_ =
    NULL;

// static
RegistryControlledDomainService* RegistryControlledDomainService::GetInstance()
{
  if (!instance_) {
    RegistryControlledDomainService* s = new RegistryControlledDomainService();
    s->Init();
    // TODO(darin): use fix_wp64.h once it lives in base/
    if (InterlockedCompareExchangePointer(
        reinterpret_cast<PVOID*>(&instance_), s, NULL)) {
      // Oops, another thread initialized instance_ out from under us.
      delete s;
    }
  }
  return instance_;
}

// static
void RegistryControlledDomainService::UseDomainData(const std::string& data) {
  RegistryControlledDomainService* instance = GetInstance();
  instance->domain_data_ = data;
  instance->ParseDomainData();
}

void RegistryControlledDomainService::Init() {
  domain_data_ = NetModule::GetResource(IDR_EFFECTIVE_TLD_NAMES);
  if (domain_data_.empty()) {
    // The resource file isn't present for some unit tests, for example.  Fall
    // back to a tiny, basic list of rules in that case.
    domain_data_ = kDefaultDomainData;
  }
  ParseDomainData();
}

void RegistryControlledDomainService::ParseDomainData() {
  domain_map_.clear();

  StringSegment rule;
  size_t line_end = 0;
  size_t line_start = 0;
  while (line_start < domain_data_.size()) {
    line_end = domain_data_.find('\n', line_start);
    if (line_end == std::string::npos)
      line_end = domain_data_.size();
    rule.Set(domain_data_.data(), line_start, line_end - line_start);
    AddRule(&rule);
    line_start = line_end + 1;
  }
}

void RegistryControlledDomainService::AddRule(StringSegment* rule) {
  // Determine rule properties.
  size_t property_offset = 0;
  bool exception = false;
  bool wild = false;

  // Valid rules may be either wild or exceptions, but not both.
  if (rule->CharAt(0) == '!') {
    exception = true;
    property_offset = 1;
  } else if (rule->CharAt(0) == '*' && rule->CharAt(1) == '.') {
    wild = true;
    property_offset = 2;
  }

  // Find or create an entry for this host.
  rule->TrimFromStart(property_offset);
  DomainEntry entry;
  DomainMap::iterator iter = domain_map_.find(*rule);
  if (iter != domain_map_.end())
    entry = iter->second;

  entry.exception |= exception;
  entry.wildcard  |= wild;
  domain_map_[*rule] = entry;
}

bool RegistryControlledDomainService::StringSegment::operator<(
    const StringSegment &other) const {
  // If the segments are of equal length, compare their contents; otherwise,
  // the shorter segment is "less than" the longer one.
  if (len_ == other.len_) {
    int comparison = strncmp(data_ + begin_, other.data_ + other.begin_, len_);
    return (comparison < 0);
  }
  return (len_ < other.len_);
}