summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/platform/weborigin/origin_access_entry.cc
blob: d6c49e216d1d63e9008c811f71abe04e19e7ca38 (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
/*
 * Copyright (C) 2009 Google Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 *     * Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above
 * copyright notice, this list of conditions and the following disclaimer
 * in the documentation and/or other materials provided with the
 * distribution.
 *     * Neither the name of Google Inc. nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "third_party/blink/renderer/platform/weborigin/origin_access_entry.h"

#include "third_party/blink/public/platform/platform.h"
#include "third_party/blink/public/platform/web_public_suffix_list.h"
#include "third_party/blink/renderer/platform/weborigin/kurl.h"
#include "third_party/blink/renderer/platform/weborigin/security_origin.h"
#include "url/third_party/mozilla/url_parse.h"
#include "url/url_canon.h"

namespace blink {

namespace {

// TODO(mkwst): This basically replicates GURL::HostIsIPAddress. If/when
// we re-evaluate everything after merging the Blink and Chromium
// repositories, perhaps we can just use that directly.
bool HostIsIPAddress(const String& host) {
  if (host.IsEmpty())
    return false;

  String protocol("https://");
  KURL url(NullURL(), protocol + host + "/");
  if (!url.IsValid())
    return false;

  url::RawCanonOutputT<char, 128> ignored_output;
  url::CanonHostInfo host_info;
  url::Component host_component(0,
                                static_cast<int>(url.Host().Utf8().length()));
  url::CanonicalizeIPAddress(url.Host().Utf8().data(), host_component,
                             &ignored_output, &host_info);
  return host_info.IsIPAddress();
}

bool IsSubdomainOfHost(const String& subdomain, const String& host) {
  if (subdomain.length() <= host.length())
    return false;

  if (subdomain[subdomain.length() - host.length() - 1] != '.')
    return false;

  if (!subdomain.EndsWith(host))
    return false;

  return true;
}
}  // namespace

OriginAccessEntry::OriginAccessEntry(const String& protocol,
                                     const String& host,
                                     SubdomainSetting subdomain_setting)
    : protocol_(protocol),
      host_(host),
      subdomain_settings_(subdomain_setting),
      host_is_public_suffix_(false) {
  DCHECK(subdomain_setting >= kAllowSubdomains ||
         subdomain_setting <= kDisallowSubdomains);

  host_is_ip_address_ = blink::HostIsIPAddress(host);

  // Look for top-level domains, either with or without an additional dot.
  if (!host_is_ip_address_) {
    WebPublicSuffixList* suffix_list = Platform::Current()->PublicSuffixList();
    if (!suffix_list)
      return;

    size_t public_suffix_length = suffix_list->GetPublicSuffixLength(host_);
    if (host_.length() <= public_suffix_length + 1) {
      host_is_public_suffix_ = true;
    } else if (subdomain_setting == kAllowRegisterableDomains &&
               public_suffix_length) {
      // The "2" in the next line is 1 for the '.', plus a 1-char minimum label
      // length.
      const size_t dot =
          host_.ReverseFind('.', host_.length() - public_suffix_length - 2);
      if (dot == kNotFound)
        registerable_domain_ = host;
      else
        registerable_domain_ = host.Substring(dot + 1);
    }
  }
}

OriginAccessEntry::MatchResult OriginAccessEntry::MatchesOrigin(
    const SecurityOrigin& origin) const {
  if (protocol_ != origin.Protocol())
    return kDoesNotMatchOrigin;

  return MatchesDomain(origin);
}

OriginAccessEntry::MatchResult OriginAccessEntry::MatchesDomain(
    const SecurityOrigin& origin) const {
  // Special case: Include subdomains and empty host means "all hosts, including
  // ip addresses".
  if (subdomain_settings_ != kDisallowSubdomains && host_.IsEmpty())
    return kMatchesOrigin;

  // Exact match.
  if (host_ == origin.Host())
    return kMatchesOrigin;

  // Don't try to do subdomain matching on IP addresses.
  if (host_is_ip_address_)
    return kDoesNotMatchOrigin;

  // Match subdomains.
  switch (subdomain_settings_) {
    case kDisallowSubdomains:
      return kDoesNotMatchOrigin;

    case kAllowSubdomains:
      if (!IsSubdomainOfHost(origin.Host(), host_))
        return kDoesNotMatchOrigin;
      break;

    case kAllowRegisterableDomains:
      // Fall back to a simple subdomain check if no registerable domain could
      // be found:
      if (registerable_domain_.IsEmpty()) {
        if (!IsSubdomainOfHost(origin.Host(), host_))
          return kDoesNotMatchOrigin;
      } else if (registerable_domain_ != origin.Host() &&
                 !IsSubdomainOfHost(origin.Host(), registerable_domain_)) {
        return kDoesNotMatchOrigin;
      }
      break;
  };

  if (host_is_public_suffix_)
    return kMatchesOriginButIsPublicSuffix;

  return kMatchesOrigin;
}

}  // namespace blink