OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/websockets/websocket_basic_handshake_stream.h" | 5 #include "net/websockets/websocket_basic_handshake_stream.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <iterator> | 8 #include <iterator> |
9 | 9 |
10 #include "base/base64.h" | 10 #include "base/base64.h" |
11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
12 #include "base/bind.h" | 12 #include "base/bind.h" |
13 #include "base/containers/hash_tables.h" | 13 #include "base/containers/hash_tables.h" |
14 #include "base/stl_util.h" | 14 #include "base/stl_util.h" |
15 #include "base/strings/string_util.h" | 15 #include "base/strings/string_util.h" |
16 #include "crypto/random.h" | 16 #include "crypto/random.h" |
17 #include "net/http/http_request_headers.h" | 17 #include "net/http/http_request_headers.h" |
18 #include "net/http/http_request_info.h" | 18 #include "net/http/http_request_info.h" |
19 #include "net/http/http_response_body_drainer.h" | 19 #include "net/http/http_response_body_drainer.h" |
20 #include "net/http/http_response_headers.h" | 20 #include "net/http/http_response_headers.h" |
21 #include "net/http/http_status_code.h" | 21 #include "net/http/http_status_code.h" |
22 #include "net/http/http_stream_parser.h" | 22 #include "net/http/http_stream_parser.h" |
23 #include "net/socket/client_socket_handle.h" | 23 #include "net/socket/client_socket_handle.h" |
24 #include "net/websockets/websocket_basic_stream.h" | 24 #include "net/websockets/websocket_basic_stream.h" |
25 #include "net/websockets/websocket_extension_parser.h" | |
25 #include "net/websockets/websocket_handshake_constants.h" | 26 #include "net/websockets/websocket_handshake_constants.h" |
26 #include "net/websockets/websocket_handshake_handler.h" | 27 #include "net/websockets/websocket_handshake_handler.h" |
27 #include "net/websockets/websocket_stream.h" | 28 #include "net/websockets/websocket_stream.h" |
28 | 29 |
29 namespace net { | 30 namespace net { |
30 namespace { | 31 namespace { |
31 | 32 |
33 enum GetHeaderResult { | |
34 GET_HEADER_OK, | |
35 GET_HEADER_MISSING, | |
36 GET_HEADER_MULTIPLE, | |
37 }; | |
38 | |
32 std::string GenerateHandshakeChallenge() { | 39 std::string GenerateHandshakeChallenge() { |
33 std::string raw_challenge(websockets::kRawChallengeLength, '\0'); | 40 std::string raw_challenge(websockets::kRawChallengeLength, '\0'); |
34 crypto::RandBytes(string_as_array(&raw_challenge), raw_challenge.length()); | 41 crypto::RandBytes(string_as_array(&raw_challenge), raw_challenge.length()); |
35 std::string encoded_challenge; | 42 std::string encoded_challenge; |
36 bool encode_success = base::Base64Encode(raw_challenge, &encoded_challenge); | 43 bool encode_success = base::Base64Encode(raw_challenge, &encoded_challenge); |
37 DCHECK(encode_success); | 44 DCHECK(encode_success); |
38 return encoded_challenge; | 45 return encoded_challenge; |
39 } | 46 } |
40 | 47 |
41 void AddVectorHeaderIfNonEmpty(const char* name, | 48 void AddVectorHeaderIfNonEmpty(const char* name, |
42 const std::vector<std::string>& value, | 49 const std::vector<std::string>& value, |
43 HttpRequestHeaders* headers) { | 50 HttpRequestHeaders* headers) { |
44 if (value.empty()) | 51 if (value.empty()) |
45 return; | 52 return; |
46 headers->SetHeader(name, JoinString(value, ", ")); | 53 headers->SetHeader(name, JoinString(value, ", ")); |
47 } | 54 } |
48 | 55 |
49 // If |case_sensitive| is false, then |value| must be in lower-case. | 56 GetHeaderResult GetSingleHeaderValue(const HttpResponseHeaders* headers, |
50 bool ValidateSingleTokenHeader( | 57 const base::StringPiece& name, |
51 const scoped_refptr<HttpResponseHeaders>& headers, | 58 std::string* value) { |
52 const base::StringPiece& name, | |
53 const std::string& value, | |
54 bool case_sensitive) { | |
55 void* state = NULL; | 59 void* state = NULL; |
60 int tokens = 0; | |
56 std::string token; | 61 std::string token; |
57 int tokens = 0; | |
58 bool has_value = false; | |
59 while (headers->EnumerateHeader(&state, name, &token)) { | 62 while (headers->EnumerateHeader(&state, name, &token)) { |
60 if (++tokens > 1) | 63 if (++tokens > 1) |
61 return false; | 64 return GET_HEADER_MULTIPLE; |
62 has_value = case_sensitive ? value == token | 65 *value = token; |
63 : LowerCaseEqualsASCII(token, value.c_str()); | |
64 } | 66 } |
65 return has_value; | 67 return tokens > 0 ? GET_HEADER_OK : GET_HEADER_MISSING; |
68 } | |
69 | |
70 bool ValidateUpgrade(const HttpResponseHeaders* headers, | |
71 std::string* failure_message) { | |
72 std::string value; | |
73 GetHeaderResult result = | |
74 GetSingleHeaderValue(headers, websockets::kUpgrade, &value); | |
75 if (result == GET_HEADER_MISSING) { | |
76 *failure_message = "'Upgrade' header is missing"; | |
77 return false; | |
78 } | |
79 if (result == GET_HEADER_MULTIPLE) { | |
80 *failure_message = | |
81 "'Upgrade' header must not appear more than once in a response"; | |
82 return false; | |
83 } | |
84 | |
85 if (!LowerCaseEqualsASCII(value, websockets::kWebSocketLowercase)) { | |
86 *failure_message = | |
87 "'Upgrade' header value is not 'WebSocket': " + value; | |
88 return false; | |
89 } | |
90 return true; | |
91 } | |
92 | |
93 bool ValidateSecWebSocketAccept(const HttpResponseHeaders* headers, | |
94 const std::string& expected, | |
95 std::string* failure_message) { | |
96 std::string actual; | |
97 GetHeaderResult result = | |
98 GetSingleHeaderValue(headers, websockets::kSecWebSocketAccept, &actual); | |
99 if (result == GET_HEADER_MISSING) { | |
100 *failure_message = "'Sec-WebSocket-Accept' header is missing"; | |
Adam Rice
2013/12/06 07:02:31
In ValidateUpgrade() and ValidateSecWebSocketAccep
yhirano
2013/12/06 08:54:56
Done.
| |
101 return false; | |
102 } | |
103 if (result == GET_HEADER_MULTIPLE) { | |
104 *failure_message = | |
105 "'Sec-WebSocket-Accept' header must not appear " | |
106 "more than once in a response"; | |
107 return false; | |
108 } | |
109 | |
110 if (expected != actual) { | |
111 *failure_message = "Incorrect 'Sec-WebSocket-Accept' header value"; | |
112 return false; | |
113 } | |
114 return true; | |
115 } | |
116 | |
117 bool ValidateConnection(const HttpResponseHeaders* headers, | |
118 std::string* failure_message) { | |
Adam Rice
2013/12/06 07:02:31
Indentation.
yhirano
2013/12/06 08:54:56
Done.
| |
119 // Connection header is permitted to contain other tokens. | |
120 if (!headers->HasHeader(HttpRequestHeaders::kConnection)) { | |
121 *failure_message = "'Connection' header is missing"; | |
122 return false; | |
123 } | |
124 if (!headers->HasHeaderValue(HttpRequestHeaders::kConnection, | |
125 websockets::kUpgrade)) { | |
126 *failure_message = "'Connection' header value must contain 'Upgrade'"; | |
127 return false; | |
128 } | |
129 return true; | |
66 } | 130 } |
67 | 131 |
68 bool ValidateSubProtocol( | 132 bool ValidateSubProtocol( |
69 const scoped_refptr<HttpResponseHeaders>& headers, | 133 const HttpResponseHeaders* headers, |
70 const std::vector<std::string>& requested_sub_protocols, | 134 const std::vector<std::string>& requested_sub_protocols, |
71 std::string* sub_protocol) { | 135 std::string* sub_protocol, |
136 std::string* failure_message) { | |
72 void* state = NULL; | 137 void* state = NULL; |
73 std::string token; | 138 std::string token; |
139 std::string last_token; | |
74 base::hash_set<std::string> requested_set(requested_sub_protocols.begin(), | 140 base::hash_set<std::string> requested_set(requested_sub_protocols.begin(), |
75 requested_sub_protocols.end()); | 141 requested_sub_protocols.end()); |
76 int accepted = 0; | 142 int count = 0; |
143 bool has_multiple_protocols = false; | |
144 bool has_invalid_protocol = false; | |
145 | |
77 while (headers->EnumerateHeader( | 146 while (headers->EnumerateHeader( |
78 &state, websockets::kSecWebSocketProtocol, &token)) { | 147 &state, websockets::kSecWebSocketProtocol, &token) && |
Adam Rice
2013/12/06 07:02:31
The indentation looks weird here. I would expect i
yhirano
2013/12/06 08:54:56
Done.
| |
148 !(has_multiple_protocols && has_invalid_protocol)) { | |
79 if (requested_set.count(token) == 0) | 149 if (requested_set.count(token) == 0) |
80 return false; | 150 has_invalid_protocol = true; |
151 if (++count > 1) | |
152 has_multiple_protocols = true; | |
153 last_token = token; | |
154 } | |
81 | 155 |
82 *sub_protocol = token; | 156 if (has_multiple_protocols) { |
83 // The server is only allowed to accept one protocol. | 157 *failure_message = |
84 if (++accepted > 1) | 158 "'Sec-WebSocket-Protocol' header must not " |
85 return false; | 159 "appear more than once in a response"; |
160 return false; | |
161 } else if (count > 0 && requested_sub_protocols.size() == 0) { | |
162 *failure_message = | |
163 std::string("Response must not include 'Sec-WebSocket-Protocol' " | |
164 "header if not present in request: ") | |
165 + last_token; | |
166 return false; | |
167 } else if (has_invalid_protocol) { | |
168 *failure_message = | |
169 "'Sec-WebSocket-Protocol' header value '" + | |
170 last_token + | |
171 "' in response does not match any of sent values"; | |
172 return false; | |
173 } else if (requested_sub_protocols.size() > 0 && count == 0) { | |
174 *failure_message = | |
175 "Sent non-empty 'Sec-WebSocket-Protocol' header " | |
176 "but no response is received"; | |
Adam Rice
2013/12/06 07:02:31
Grammatically, this should be "no response was rec
yhirano
2013/12/06 08:54:56
Done.
| |
177 return false; | |
86 } | 178 } |
87 // If the browser requested > 0 protocols, the server is required to accept | 179 *sub_protocol = last_token; |
88 // one. | 180 return true; |
89 return requested_set.empty() || accepted == 1; | |
90 } | 181 } |
91 | 182 |
92 bool ValidateExtensions(const scoped_refptr<HttpResponseHeaders>& headers, | 183 bool ValidateExtensions(const HttpResponseHeaders* headers, |
93 const std::vector<std::string>& requested_extensions, | 184 const std::vector<std::string>& requested_extensions, |
94 std::string* extensions) { | 185 std::string* extensions, |
186 std::string* failure_message) { | |
95 void* state = NULL; | 187 void* state = NULL; |
96 std::string token; | 188 std::string token; |
97 while (headers->EnumerateHeader( | 189 while (headers->EnumerateHeader( |
98 &state, websockets::kSecWebSocketExtensions, &token)) { | 190 &state, websockets::kSecWebSocketExtensions, &token)) { |
191 WebSocketExtensionParser parser; | |
192 parser.Parse(token); | |
193 if (parser.has_error()) { | |
194 // TODO(yhirano) Set appropriate failure message. | |
195 *failure_message = | |
196 "'WebSocket-Extensions' header value is rejected by the parser: " + | |
197 token; | |
198 return false; | |
199 } | |
99 // TODO(ricea): Accept permessage-deflate with valid parameters. | 200 // TODO(ricea): Accept permessage-deflate with valid parameters. |
201 *failure_message = | |
202 "Found an unsupported extension '" + | |
203 parser.extension().name() + | |
204 "' in 'Sec-WebSocket-Extensions' header"; | |
100 return false; | 205 return false; |
101 } | 206 } |
102 return true; | 207 return true; |
103 } | 208 } |
104 | 209 |
105 } // namespace | 210 } // namespace |
106 | 211 |
107 WebSocketBasicHandshakeStream::WebSocketBasicHandshakeStream( | 212 WebSocketBasicHandshakeStream::WebSocketBasicHandshakeStream( |
108 scoped_ptr<ClientSocketHandle> connection, | 213 scoped_ptr<ClientSocketHandle> connection, |
109 bool using_proxy, | 214 bool using_proxy, |
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
257 state_.read_buf(), | 362 state_.read_buf(), |
258 sub_protocol_, | 363 sub_protocol_, |
259 extensions_)); | 364 extensions_)); |
260 } | 365 } |
261 | 366 |
262 void WebSocketBasicHandshakeStream::SetWebSocketKeyForTesting( | 367 void WebSocketBasicHandshakeStream::SetWebSocketKeyForTesting( |
263 const std::string& key) { | 368 const std::string& key) { |
264 handshake_challenge_for_testing_.reset(new std::string(key)); | 369 handshake_challenge_for_testing_.reset(new std::string(key)); |
265 } | 370 } |
266 | 371 |
372 std::string WebSocketBasicHandshakeStream::FailureMessage() const { | |
373 return failure_message_; | |
374 } | |
375 | |
267 void WebSocketBasicHandshakeStream::ReadResponseHeadersCallback( | 376 void WebSocketBasicHandshakeStream::ReadResponseHeadersCallback( |
268 const CompletionCallback& callback, | 377 const CompletionCallback& callback, |
269 int result) { | 378 int result) { |
270 if (result == OK) | 379 if (result == OK) |
271 result = ValidateResponse(); | 380 result = ValidateResponse(); |
272 callback.Run(result); | 381 callback.Run(result); |
273 } | 382 } |
274 | 383 |
275 int WebSocketBasicHandshakeStream::ValidateResponse() { | 384 int WebSocketBasicHandshakeStream::ValidateResponse() { |
276 DCHECK(http_response_info_); | 385 DCHECK(http_response_info_); |
(...skipping 11 matching lines...) Expand all Loading... | |
288 | 397 |
289 // Other status codes are potentially risky (see the warnings in the | 398 // Other status codes are potentially risky (see the warnings in the |
290 // WHATWG WebSocket API spec) and so are dropped by default. | 399 // WHATWG WebSocket API spec) and so are dropped by default. |
291 default: | 400 default: |
292 return ERR_INVALID_RESPONSE; | 401 return ERR_INVALID_RESPONSE; |
293 } | 402 } |
294 } | 403 } |
295 | 404 |
296 int WebSocketBasicHandshakeStream::ValidateUpgradeResponse( | 405 int WebSocketBasicHandshakeStream::ValidateUpgradeResponse( |
297 const scoped_refptr<HttpResponseHeaders>& headers) { | 406 const scoped_refptr<HttpResponseHeaders>& headers) { |
298 if (ValidateSingleTokenHeader(headers, | 407 if (ValidateUpgrade(headers.get(), &failure_message_) && |
299 websockets::kUpgrade, | 408 ValidateSecWebSocketAccept(headers.get(), |
300 websockets::kWebSocketLowercase, | 409 handshake_challenge_response_, |
301 false) && | 410 &failure_message_) && |
302 ValidateSingleTokenHeader(headers, | 411 ValidateConnection(headers.get(), &failure_message_) && |
303 websockets::kSecWebSocketAccept, | 412 ValidateSubProtocol(headers.get(), |
304 handshake_challenge_response_, | 413 requested_sub_protocols_, |
305 true) && | 414 &sub_protocol_, |
306 headers->HasHeaderValue(HttpRequestHeaders::kConnection, | 415 &failure_message_) && |
307 websockets::kUpgrade) && | 416 ValidateExtensions(headers.get(), |
308 ValidateSubProtocol(headers, requested_sub_protocols_, &sub_protocol_) && | 417 requested_extensions_, |
309 ValidateExtensions(headers, requested_extensions_, &extensions_)) { | 418 &extensions_, |
419 &failure_message_)) { | |
310 return OK; | 420 return OK; |
311 } | 421 } |
422 failure_message_ = "Error during WebSocket handshake: " + failure_message_; | |
312 return ERR_INVALID_RESPONSE; | 423 return ERR_INVALID_RESPONSE; |
313 } | 424 } |
314 | 425 |
315 } // namespace net | 426 } // namespace net |
OLD | NEW |