blob: 721ae33face9bd129647dfed6ee780bd7542700e [file] [log] [blame]
QUICHE teama6ef0a62019-03-07 20:34:33 -05001// Copyright (c) 2013 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#include "net/third_party/quiche/src/quic/core/http/spdy_utils.h"
6
7#include <memory>
vasilvv872e7a32019-03-12 16:42:44 -07008#include <string>
QUICHE teama6ef0a62019-03-07 20:34:33 -05009#include <vector>
10
11#include "url/gurl.h"
12#include "net/third_party/quiche/src/quic/platform/api/quic_flag_utils.h"
13#include "net/third_party/quiche/src/quic/platform/api/quic_flags.h"
14#include "net/third_party/quiche/src/quic/platform/api/quic_logging.h"
15#include "net/third_party/quiche/src/quic/platform/api/quic_map_util.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050016#include "net/third_party/quiche/src/quic/platform/api/quic_string_piece.h"
17#include "net/third_party/quiche/src/quic/platform/api/quic_text_utils.h"
18#include "net/third_party/quiche/src/spdy/core/spdy_frame_builder.h"
19#include "net/third_party/quiche/src/spdy/core/spdy_framer.h"
20#include "net/third_party/quiche/src/spdy/core/spdy_protocol.h"
21
22using spdy::SpdyHeaderBlock;
23
24namespace quic {
25
26// static
27bool SpdyUtils::ExtractContentLengthFromHeaders(int64_t* content_length,
28 SpdyHeaderBlock* headers) {
29 auto it = headers->find("content-length");
30 if (it == headers->end()) {
31 return false;
32 } else {
33 // Check whether multiple values are consistent.
34 QuicStringPiece content_length_header = it->second;
35 std::vector<QuicStringPiece> values =
36 QuicTextUtils::Split(content_length_header, '\0');
37 for (const QuicStringPiece& value : values) {
38 uint64_t new_value;
39 if (!QuicTextUtils::StringToUint64(value, &new_value)) {
40 QUIC_DLOG(ERROR)
41 << "Content length was either unparseable or negative.";
42 return false;
43 }
44 if (*content_length < 0) {
45 *content_length = new_value;
46 continue;
47 }
48 if (new_value != static_cast<uint64_t>(*content_length)) {
49 QUIC_DLOG(ERROR)
50 << "Parsed content length " << new_value << " is "
51 << "inconsistent with previously detected content length "
52 << *content_length;
53 return false;
54 }
55 }
56 return true;
57 }
58}
59
60bool SpdyUtils::CopyAndValidateHeaders(const QuicHeaderList& header_list,
61 int64_t* content_length,
62 SpdyHeaderBlock* headers) {
63 for (const auto& p : header_list) {
vasilvvc48c8712019-03-11 13:38:16 -070064 const std::string& name = p.first;
QUICHE teama6ef0a62019-03-07 20:34:33 -050065 if (name.empty()) {
66 QUIC_DLOG(ERROR) << "Header name must not be empty.";
67 return false;
68 }
69
70 if (QuicTextUtils::ContainsUpperCase(name)) {
71 QUIC_DLOG(ERROR) << "Malformed header: Header name " << name
72 << " contains upper-case characters.";
73 return false;
74 }
75
76 headers->AppendValueOrAddHeader(name, p.second);
77 }
78
79 if (QuicContainsKey(*headers, "content-length") &&
80 !ExtractContentLengthFromHeaders(content_length, headers)) {
81 return false;
82 }
83
84 QUIC_DVLOG(1) << "Successfully parsed headers: " << headers->DebugString();
85 return true;
86}
87
88bool SpdyUtils::CopyAndValidateTrailers(const QuicHeaderList& header_list,
bnc5231ee22019-04-15 19:02:13 -070089 bool expect_final_byte_offset,
QUICHE teama6ef0a62019-03-07 20:34:33 -050090 size_t* final_byte_offset,
91 SpdyHeaderBlock* trailers) {
92 bool found_final_byte_offset = false;
93 for (const auto& p : header_list) {
vasilvvc48c8712019-03-11 13:38:16 -070094 const std::string& name = p.first;
QUICHE teama6ef0a62019-03-07 20:34:33 -050095
96 // Pull out the final offset pseudo header which indicates the number of
97 // response body bytes expected.
bnc5231ee22019-04-15 19:02:13 -070098 if (expect_final_byte_offset && !found_final_byte_offset &&
99 name == kFinalOffsetHeaderKey &&
QUICHE teama6ef0a62019-03-07 20:34:33 -0500100 QuicTextUtils::StringToSizeT(p.second, final_byte_offset)) {
101 found_final_byte_offset = true;
102 continue;
103 }
104
105 if (name.empty() || name[0] == ':') {
106 QUIC_DLOG(ERROR)
107 << "Trailers must not be empty, and must not contain pseudo-"
108 << "headers. Found: '" << name << "'";
109 return false;
110 }
111
112 if (QuicTextUtils::ContainsUpperCase(name)) {
113 QUIC_DLOG(ERROR) << "Malformed header: Header name " << name
114 << " contains upper-case characters.";
115 return false;
116 }
117
118 trailers->AppendValueOrAddHeader(name, p.second);
119 }
120
bnc5231ee22019-04-15 19:02:13 -0700121 if (expect_final_byte_offset && !found_final_byte_offset) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500122 QUIC_DLOG(ERROR) << "Required key '" << kFinalOffsetHeaderKey
123 << "' not present";
124 return false;
125 }
126
127 // TODO(rjshade): Check for other forbidden keys, following the HTTP/2 spec.
128
129 QUIC_DVLOG(1) << "Successfully parsed Trailers: " << trailers->DebugString();
130 return true;
131}
132
133// static
vasilvvc48c8712019-03-11 13:38:16 -0700134std::string SpdyUtils::GetPromisedUrlFromHeaders(
QUICHE teama6ef0a62019-03-07 20:34:33 -0500135 const SpdyHeaderBlock& headers) {
136 // RFC 7540, Section 8.1.2.3: All HTTP/2 requests MUST include exactly
137 // one valid value for the ":method", ":scheme", and ":path" pseudo-header
138 // fields, unless it is a CONNECT request.
139
140 // RFC 7540, Section 8.2.1: The header fields in PUSH_PROMISE and any
141 // subsequent CONTINUATION frames MUST be a valid and complete set of request
142 // header fields (Section 8.1.2.3). The server MUST include a method in the
143 // ":method" pseudo-header field that is safe and cacheable.
144 //
145 // RFC 7231, Section 4.2.1: Of the request methods defined by this
146 // specification, the GET, HEAD, OPTIONS, and TRACE methods are defined to be
147 // safe.
148 //
149 // RFC 7231, Section 4.2.1: ... this specification defines GET, HEAD, and
150 // POST as cacheable, ...
151 //
152 // So the only methods allowed in a PUSH_PROMISE are GET and HEAD.
153 SpdyHeaderBlock::const_iterator it = headers.find(":method");
154 if (it == headers.end() || (it->second != "GET" && it->second != "HEAD")) {
vasilvvc48c8712019-03-11 13:38:16 -0700155 return std::string();
QUICHE teama6ef0a62019-03-07 20:34:33 -0500156 }
157
158 it = headers.find(":scheme");
159 if (it == headers.end() || it->second.empty()) {
vasilvvc48c8712019-03-11 13:38:16 -0700160 return std::string();
QUICHE teama6ef0a62019-03-07 20:34:33 -0500161 }
162 QuicStringPiece scheme = it->second;
163
164 // RFC 7540, Section 8.2: The server MUST include a value in the
165 // ":authority" pseudo-header field for which the server is authoritative
166 // (see Section 10.1).
167 it = headers.find(":authority");
168 if (it == headers.end() || it->second.empty()) {
vasilvvc48c8712019-03-11 13:38:16 -0700169 return std::string();
QUICHE teama6ef0a62019-03-07 20:34:33 -0500170 }
171 QuicStringPiece authority = it->second;
172
173 // RFC 7540, Section 8.1.2.3 requires that the ":path" pseudo-header MUST
174 // NOT be empty for "http" or "https" URIs;
175 //
176 // However, to ensure the scheme is consistently canonicalized, that check
177 // is deferred to implementations in QuicUrlUtils::GetPushPromiseUrl().
178 it = headers.find(":path");
179 if (it == headers.end()) {
vasilvvc48c8712019-03-11 13:38:16 -0700180 return std::string();
QUICHE teama6ef0a62019-03-07 20:34:33 -0500181 }
182 QuicStringPiece path = it->second;
183
184 return GetPushPromiseUrl(scheme, authority, path);
185}
186
187// static
vasilvvc48c8712019-03-11 13:38:16 -0700188std::string SpdyUtils::GetPromisedHostNameFromHeaders(
QUICHE teama6ef0a62019-03-07 20:34:33 -0500189 const SpdyHeaderBlock& headers) {
190 // TODO(fayang): Consider just checking out the value of the ":authority" key
191 // in headers.
192 return GURL(GetPromisedUrlFromHeaders(headers)).host();
193}
194
195// static
196bool SpdyUtils::PromisedUrlIsValid(const SpdyHeaderBlock& headers) {
vasilvvc48c8712019-03-11 13:38:16 -0700197 std::string url(GetPromisedUrlFromHeaders(headers));
QUICHE teama6ef0a62019-03-07 20:34:33 -0500198 return !url.empty() && GURL(url).is_valid();
199}
200
201// static
vasilvvc48c8712019-03-11 13:38:16 -0700202bool SpdyUtils::PopulateHeaderBlockFromUrl(const std::string url,
QUICHE teama6ef0a62019-03-07 20:34:33 -0500203 SpdyHeaderBlock* headers) {
204 (*headers)[":method"] = "GET";
205 size_t pos = url.find("://");
vasilvvc48c8712019-03-11 13:38:16 -0700206 if (pos == std::string::npos) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500207 return false;
208 }
209 (*headers)[":scheme"] = url.substr(0, pos);
210 size_t start = pos + 3;
211 pos = url.find("/", start);
vasilvvc48c8712019-03-11 13:38:16 -0700212 if (pos == std::string::npos) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500213 (*headers)[":authority"] = url.substr(start);
214 (*headers)[":path"] = "/";
215 return true;
216 }
217 (*headers)[":authority"] = url.substr(start, pos - start);
218 (*headers)[":path"] = url.substr(pos);
219 return true;
220}
221
222// static
vasilvvc48c8712019-03-11 13:38:16 -0700223std::string SpdyUtils::GetPushPromiseUrl(QuicStringPiece scheme,
224 QuicStringPiece authority,
225 QuicStringPiece path) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500226 // RFC 7540, Section 8.1.2.3: The ":path" pseudo-header field includes the
227 // path and query parts of the target URI (the "path-absolute" production
228 // and optionally a '?' character followed by the "query" production (see
229 // Sections 3.3 and 3.4 of RFC3986). A request in asterisk form includes the
230 // value '*' for the ":path" pseudo-header field.
231 //
232 // This pseudo-header field MUST NOT be empty for "http" or "https" URIs;
233 // "http" or "https" URIs that do not contain a path MUST include a value of
234 // '/'. The exception to this rule is an OPTIONS request for an "http" or
235 // "https" URI that does not include a path component; these MUST include a
236 // ":path" pseudo-header with a value of '*' (see RFC7230, Section 5.3.4).
237 //
238 // In addition to the above restriction from RFC 7540, note that RFC3986
239 // defines the "path-absolute" construction as starting with "/" but not "//".
240 //
241 // RFC 7540, Section 8.2.1: The header fields in PUSH_PROMISE and any
242 // subsequent CONTINUATION frames MUST be a valid and complete set of request
243 // header fields (Section 8.1.2.3). The server MUST include a method in the
244 // ":method" pseudo-header field that is safe and cacheable.
245 //
246 // RFC 7231, Section 4.2.1:
247 // ... this specification defines GET, HEAD, and POST as cacheable, ...
248 //
249 // Since the OPTIONS method is not cacheable, it cannot be the method of a
250 // PUSH_PROMISE. Therefore, the exception mentioned in RFC 7540, Section
251 // 8.1.2.3 about OPTIONS requests does not apply here (i.e. ":path" cannot be
252 // "*").
253 if (path.empty() || path[0] != '/' || (path.size() >= 2 && path[1] == '/')) {
vasilvvc48c8712019-03-11 13:38:16 -0700254 return std::string();
QUICHE teama6ef0a62019-03-07 20:34:33 -0500255 }
256
257 // Validate the scheme; this is to ensure a scheme of "foo://bar" is not
258 // parsed as a URL of "foo://bar://baz" when combined with a host of "baz".
259 std::string canonical_scheme;
260 url::StdStringCanonOutput canon_scheme_output(&canonical_scheme);
261 url::Component canon_component;
262 url::Component scheme_component(0, scheme.size());
263
264 if (!url::CanonicalizeScheme(scheme.data(), scheme_component,
265 &canon_scheme_output, &canon_component) ||
266 !canon_component.is_nonempty() || canon_component.begin != 0) {
vasilvvc48c8712019-03-11 13:38:16 -0700267 return std::string();
QUICHE teama6ef0a62019-03-07 20:34:33 -0500268 }
269 canonical_scheme.resize(canon_component.len + 1);
270
271 // Validate the authority; this is to ensure an authority such as
272 // "host/path" is not accepted, as when combined with a scheme like
273 // "http://", could result in a URL of "http://host/path".
274 url::Component auth_component(0, authority.size());
275 url::Component username_component;
276 url::Component password_component;
277 url::Component host_component;
278 url::Component port_component;
279
280 url::ParseAuthority(authority.data(), auth_component, &username_component,
281 &password_component, &host_component, &port_component);
282
283 // RFC 7540, Section 8.1.2.3: The authority MUST NOT include the deprecated
284 // "userinfo" subcomponent for "http" or "https" schemed URIs.
285 //
286 // Note: Although |canonical_scheme| has not yet been checked for that, as
287 // it is performed later in processing, only "http" and "https" schemed
288 // URIs are supported for PUSH.
289 if (username_component.is_valid() || password_component.is_valid()) {
vasilvvc48c8712019-03-11 13:38:16 -0700290 return std::string();
QUICHE teama6ef0a62019-03-07 20:34:33 -0500291 }
292
293 // Failed parsing or no host present. ParseAuthority() will ensure that
294 // host_component + port_component cover the entire string, if
295 // username_component and password_component are not present.
296 if (!host_component.is_nonempty()) {
vasilvvc48c8712019-03-11 13:38:16 -0700297 return std::string();
QUICHE teama6ef0a62019-03-07 20:34:33 -0500298 }
299
300 // Validate the port (if present; it's optional).
301 int parsed_port_number = url::PORT_INVALID;
302 if (port_component.is_nonempty()) {
303 parsed_port_number = url::ParsePort(authority.data(), port_component);
304 if (parsed_port_number < 0 && parsed_port_number != url::PORT_UNSPECIFIED) {
vasilvvc48c8712019-03-11 13:38:16 -0700305 return std::string();
QUICHE teama6ef0a62019-03-07 20:34:33 -0500306 }
307 }
308
309 // Validate the host by attempting to canonicalize it. Invalid characters
310 // will result in a canonicalization failure (e.g. '/')
311 std::string canon_host;
312 url::StdStringCanonOutput canon_host_output(&canon_host);
313 canon_component.reset();
314 if (!url::CanonicalizeHost(authority.data(), host_component,
315 &canon_host_output, &canon_component) ||
316 !canon_component.is_nonempty() || canon_component.begin != 0) {
vasilvvc48c8712019-03-11 13:38:16 -0700317 return std::string();
QUICHE teama6ef0a62019-03-07 20:34:33 -0500318 }
319
320 // At this point, "authority" has been validated to either be of the form
321 // 'host:port' or 'host', with 'host' being a valid domain or IP address,
322 // and 'port' (if present), being a valid port. Attempt to construct a
323 // URL of just the (scheme, host, port), which should be safe and will not
324 // result in ambiguous parsing.
325 //
326 // This also enforces that all PUSHed URLs are either HTTP or HTTPS-schemed
327 // URIs, consistent with the other restrictions enforced above.
328 //
329 // Note: url::CanonicalizeScheme() will have added the ':' to
330 // |canonical_scheme|.
331 GURL origin_url(canonical_scheme + "//" + std::string(authority));
332 if (!origin_url.is_valid() || !origin_url.SchemeIsHTTPOrHTTPS() ||
333 // The following checks are merely defense in depth.
334 origin_url.has_username() || origin_url.has_password() ||
335 (origin_url.has_path() && origin_url.path_piece() != "/") ||
336 origin_url.has_query() || origin_url.has_ref()) {
vasilvvc48c8712019-03-11 13:38:16 -0700337 return std::string();
QUICHE teama6ef0a62019-03-07 20:34:33 -0500338 }
339
340 // Attempt to parse the path.
341 std::string spec = origin_url.GetWithEmptyPath().spec();
342 spec.pop_back(); // Remove the '/', as ":path" must contain it.
343 spec.append(std::string(path));
344
345 // Attempt to parse the full URL, with the path as well. Ensure there is no
346 // fragment to the query.
347 GURL full_url(spec);
348 if (!full_url.is_valid() || full_url.has_ref()) {
vasilvvc48c8712019-03-11 13:38:16 -0700349 return std::string();
QUICHE teama6ef0a62019-03-07 20:34:33 -0500350 }
351
352 return full_url.spec();
353}
354
355} // namespace quic