1 // Copyright 2015 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "url/scheme_host_port.h"
6
7 #include <stdint.h>
8 #include <string.h>
9
10 #include <ostream>
11 #include <string_view>
12 #include <tuple>
13
14 #include "base/check_op.h"
15 #include "base/containers/contains.h"
16 #include "base/notreached.h"
17 #include "base/numerics/safe_conversions.h"
18 #include "base/strings/string_number_conversions.h"
19 #include "base/trace_event/memory_usage_estimator.h"
20 #include "url/gurl.h"
21 #include "url/third_party/mozilla/url_parse.h"
22 #include "url/url_canon.h"
23 #include "url/url_canon_stdstring.h"
24 #include "url/url_constants.h"
25 #include "url/url_util.h"
26
27 namespace url {
28
29 namespace {
30
IsCanonicalHost(const std::string_view & host)31 bool IsCanonicalHost(const std::string_view& host) {
32 std::string canon_host;
33
34 // Try to canonicalize the host (copy/pasted from net/base. :( ).
35 const Component raw_host_component(0,
36 base::checked_cast<int>(host.length()));
37 StdStringCanonOutput canon_host_output(&canon_host);
38 CanonHostInfo host_info;
39 CanonicalizeHostVerbose(host.data(), raw_host_component,
40 &canon_host_output, &host_info);
41
42 if (host_info.out_host.is_nonempty() &&
43 host_info.family != CanonHostInfo::BROKEN) {
44 // Success! Assert that there's no extra garbage.
45 canon_host_output.Complete();
46 DCHECK_EQ(host_info.out_host.len, static_cast<int>(canon_host.length()));
47 } else {
48 // Empty host, or canonicalization failed.
49 canon_host.clear();
50 }
51
52 return host == canon_host;
53 }
54
55 // Note: When changing IsValidInput, consider also updating
56 // ShouldTreatAsOpaqueOrigin in Blink (there might be existing differences in
57 // behavior between these 2 layers, but we should avoid introducing new
58 // differences).
IsValidInput(const std::string_view & scheme,const std::string_view & host,uint16_t port,SchemeHostPort::ConstructPolicy policy)59 bool IsValidInput(const std::string_view& scheme,
60 const std::string_view& host,
61 uint16_t port,
62 SchemeHostPort::ConstructPolicy policy) {
63 // Empty schemes are never valid.
64 if (scheme.empty())
65 return false;
66
67 // about:blank and other no-access schemes translate into an opaque origin.
68 // This helps consistency with ShouldTreatAsOpaqueOrigin in Blink.
69 if (base::Contains(GetNoAccessSchemes(), scheme))
70 return false;
71
72 SchemeType scheme_type = SCHEME_WITH_HOST_PORT_AND_USER_INFORMATION;
73 bool is_standard = GetStandardSchemeType(
74 scheme.data(),
75 Component(0, base::checked_cast<int>(scheme.length())),
76 &scheme_type);
77 if (!is_standard) {
78 // To be consistent with ShouldTreatAsOpaqueOrigin in Blink, local
79 // non-standard schemes are currently allowed to be tuple origins.
80 // Nonstandard schemes don't have hostnames, so their tuple is just
81 // ("protocol", "", 0).
82 //
83 // TODO: Migrate "content:" and "externalfile:" to be standard schemes, and
84 // remove this local scheme exception.
85 if (base::Contains(GetLocalSchemes(), scheme) && host.empty() && port == 0)
86 return true;
87
88 // Otherwise, allow non-standard schemes only if the Android WebView
89 // workaround is enabled.
90 return AllowNonStandardSchemesForAndroidWebView();
91 }
92
93 switch (scheme_type) {
94 case SCHEME_WITH_HOST_AND_PORT:
95 case SCHEME_WITH_HOST_PORT_AND_USER_INFORMATION:
96 // A URL with |scheme| is required to have the host and port, so return an
97 // invalid instance if host is not given. Note that a valid port is
98 // always provided by SchemeHostPort(const GURL&) constructor (a missing
99 // port is replaced with a default port if needed by
100 // GURL::EffectiveIntPort()).
101 if (host.empty())
102 return false;
103
104 // Don't do an expensive canonicalization if the host is already
105 // canonicalized.
106 DCHECK(policy == SchemeHostPort::CHECK_CANONICALIZATION ||
107 IsCanonicalHost(host));
108 if (policy == SchemeHostPort::CHECK_CANONICALIZATION &&
109 !IsCanonicalHost(host)) {
110 return false;
111 }
112
113 return true;
114
115 case SCHEME_WITH_HOST:
116 if (port != 0) {
117 // Return an invalid object if a URL with the scheme never represents
118 // the port data but the given |port| is non-zero.
119 return false;
120 }
121
122 // Don't do an expensive canonicalization if the host is already
123 // canonicalized.
124 DCHECK(policy == SchemeHostPort::CHECK_CANONICALIZATION ||
125 IsCanonicalHost(host));
126 if (policy == SchemeHostPort::CHECK_CANONICALIZATION &&
127 !IsCanonicalHost(host)) {
128 return false;
129 }
130
131 return true;
132
133 case SCHEME_WITHOUT_AUTHORITY:
134 return false;
135
136 default:
137 NOTREACHED();
138 return false;
139 }
140 }
141
142 } // namespace
143
144 SchemeHostPort::SchemeHostPort() = default;
145
SchemeHostPort(std::string scheme,std::string host,uint16_t port,ConstructPolicy policy)146 SchemeHostPort::SchemeHostPort(std::string scheme,
147 std::string host,
148 uint16_t port,
149 ConstructPolicy policy) {
150 if (!IsValidInput(scheme, host, port, policy)) {
151 DCHECK(!IsValid());
152 return;
153 }
154
155 scheme_ = std::move(scheme);
156 host_ = std::move(host);
157 port_ = port;
158 DCHECK(IsValid()) << "Scheme: " << scheme_ << " Host: " << host_
159 << " Port: " << port;
160 }
161
SchemeHostPort(std::string_view scheme,std::string_view host,uint16_t port)162 SchemeHostPort::SchemeHostPort(std::string_view scheme,
163 std::string_view host,
164 uint16_t port)
165 : SchemeHostPort(std::string(scheme),
166 std::string(host),
167 port,
168 ConstructPolicy::CHECK_CANONICALIZATION) {}
169
SchemeHostPort(const GURL & url)170 SchemeHostPort::SchemeHostPort(const GURL& url) {
171 if (!url.is_valid())
172 return;
173
174 std::string_view scheme = url.scheme_piece();
175 std::string_view host = url.host_piece();
176
177 // A valid GURL never returns PORT_INVALID.
178 int port = url.EffectiveIntPort();
179 if (port == PORT_UNSPECIFIED) {
180 port = 0;
181 } else {
182 DCHECK_GE(port, 0);
183 DCHECK_LE(port, 65535);
184 }
185
186 if (!IsValidInput(scheme, host, port, ALREADY_CANONICALIZED))
187 return;
188
189 scheme_ = std::string(scheme);
190 host_ = std::string(host);
191 port_ = port;
192 }
193
194 SchemeHostPort::~SchemeHostPort() = default;
195
IsValid() const196 bool SchemeHostPort::IsValid() const {
197 // It suffices to just check |scheme_| for emptiness; the other fields are
198 // never present without it.
199 DCHECK(!scheme_.empty() || host_.empty());
200 DCHECK(!scheme_.empty() || port_ == 0);
201 return !scheme_.empty();
202 }
203
Serialize() const204 std::string SchemeHostPort::Serialize() const {
205 // Null checking for |parsed| in SerializeInternal is probably slower than
206 // just filling it in and discarding it here.
207 url::Parsed parsed;
208 return SerializeInternal(&parsed);
209 }
210
GetURL() const211 GURL SchemeHostPort::GetURL() const {
212 url::Parsed parsed;
213 std::string serialized = SerializeInternal(&parsed);
214
215 if (!IsValid())
216 return GURL(std::move(serialized), parsed, false);
217
218 // SchemeHostPort does not have enough information to determine if an empty
219 // host is valid or not for the given scheme. Force re-parsing.
220 DCHECK(!scheme_.empty());
221 if (host_.empty())
222 return GURL(serialized);
223
224 // If the serialized string is passed to GURL for parsing, it will append an
225 // empty path "/". Add that here. Note: per RFC 6454 we cannot do this for
226 // normal Origin serialization.
227 DCHECK(!parsed.path.is_valid());
228 parsed.path = Component(serialized.length(), 1);
229 serialized.append("/");
230 return GURL(std::move(serialized), parsed, true);
231 }
232
EstimateMemoryUsage() const233 size_t SchemeHostPort::EstimateMemoryUsage() const {
234 return base::trace_event::EstimateMemoryUsage(scheme_) +
235 base::trace_event::EstimateMemoryUsage(host_);
236 }
237
operator <(const SchemeHostPort & other) const238 bool SchemeHostPort::operator<(const SchemeHostPort& other) const {
239 return std::tie(port_, scheme_, host_) <
240 std::tie(other.port_, other.scheme_, other.host_);
241 }
242
SerializeInternal(url::Parsed * parsed) const243 std::string SchemeHostPort::SerializeInternal(url::Parsed* parsed) const {
244 std::string result;
245 if (!IsValid())
246 return result;
247
248 // Reserve enough space for the "normal" case of scheme://host/.
249 result.reserve(scheme_.size() + host_.size() + 4);
250
251 if (!scheme_.empty()) {
252 parsed->scheme = Component(0, scheme_.length());
253 result.append(scheme_);
254 }
255
256 result.append(kStandardSchemeSeparator);
257
258 if (!host_.empty()) {
259 parsed->host = Component(result.length(), host_.length());
260 result.append(host_);
261 }
262
263 // Omit the port component if the port matches with the default port
264 // defined for the scheme, if any.
265 int default_port = DefaultPortForScheme(scheme_.data(),
266 static_cast<int>(scheme_.length()));
267 if (default_port == PORT_UNSPECIFIED)
268 return result;
269 if (port_ != default_port) {
270 result.push_back(':');
271 std::string port(base::NumberToString(port_));
272 parsed->port = Component(result.length(), port.length());
273 result.append(std::move(port));
274 }
275
276 return result;
277 }
278
operator <<(std::ostream & out,const SchemeHostPort & scheme_host_port)279 std::ostream& operator<<(std::ostream& out,
280 const SchemeHostPort& scheme_host_port) {
281 return out << scheme_host_port.Serialize();
282 }
283
284 } // namespace url
285