Chromium Code Reviews| 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 #include <string> | |
| 9 | 10 |
| 10 #include "base/base64.h" | 11 #include "base/base64.h" |
| 11 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
| 12 #include "base/bind.h" | 13 #include "base/bind.h" |
| 13 #include "base/containers/hash_tables.h" | 14 #include "base/containers/hash_tables.h" |
| 14 #include "base/stl_util.h" | 15 #include "base/stl_util.h" |
| 15 #include "base/strings/string_util.h" | 16 #include "base/strings/string_util.h" |
| 17 #include "base/strings/stringprintf.h" | |
| 16 #include "crypto/random.h" | 18 #include "crypto/random.h" |
| 17 #include "net/http/http_request_headers.h" | 19 #include "net/http/http_request_headers.h" |
| 18 #include "net/http/http_request_info.h" | 20 #include "net/http/http_request_info.h" |
| 19 #include "net/http/http_response_body_drainer.h" | 21 #include "net/http/http_response_body_drainer.h" |
| 20 #include "net/http/http_response_headers.h" | 22 #include "net/http/http_response_headers.h" |
| 21 #include "net/http/http_status_code.h" | 23 #include "net/http/http_status_code.h" |
| 22 #include "net/http/http_stream_parser.h" | 24 #include "net/http/http_stream_parser.h" |
| 23 #include "net/socket/client_socket_handle.h" | 25 #include "net/socket/client_socket_handle.h" |
| 24 #include "net/websockets/websocket_basic_stream.h" | 26 #include "net/websockets/websocket_basic_stream.h" |
| 27 #include "net/websockets/websocket_extension_parser.h" | |
| 25 #include "net/websockets/websocket_handshake_constants.h" | 28 #include "net/websockets/websocket_handshake_constants.h" |
| 26 #include "net/websockets/websocket_handshake_handler.h" | 29 #include "net/websockets/websocket_handshake_handler.h" |
| 27 #include "net/websockets/websocket_stream.h" | 30 #include "net/websockets/websocket_stream.h" |
| 28 | 31 |
| 29 namespace net { | 32 namespace net { |
| 30 namespace { | 33 namespace { |
| 31 | 34 |
| 35 enum GetHeaderResult { | |
| 36 GET_HEADER_OK, | |
| 37 GET_HEADER_MISSING, | |
| 38 GET_HEADER_MULTIPLE, | |
| 39 }; | |
| 40 | |
| 41 std::string MissingHeaderMessage(const std::string& header_name) { | |
| 42 return std::string("'") + header_name + "' header is missing"; | |
| 43 } | |
| 44 | |
| 45 std::string MultipleHeaderValuesMessage(const std::string& header_name) { | |
| 46 return | |
| 47 std::string("'") + | |
| 48 header_name + | |
| 49 "' header must not appear more than once in a response"; | |
| 50 } | |
| 51 | |
| 32 std::string GenerateHandshakeChallenge() { | 52 std::string GenerateHandshakeChallenge() { |
| 33 std::string raw_challenge(websockets::kRawChallengeLength, '\0'); | 53 std::string raw_challenge(websockets::kRawChallengeLength, '\0'); |
| 34 crypto::RandBytes(string_as_array(&raw_challenge), raw_challenge.length()); | 54 crypto::RandBytes(string_as_array(&raw_challenge), raw_challenge.length()); |
| 35 std::string encoded_challenge; | 55 std::string encoded_challenge; |
| 36 bool encode_success = base::Base64Encode(raw_challenge, &encoded_challenge); | 56 bool encode_success = base::Base64Encode(raw_challenge, &encoded_challenge); |
| 37 DCHECK(encode_success); | 57 DCHECK(encode_success); |
| 38 return encoded_challenge; | 58 return encoded_challenge; |
| 39 } | 59 } |
| 40 | 60 |
| 41 void AddVectorHeaderIfNonEmpty(const char* name, | 61 void AddVectorHeaderIfNonEmpty(const char* name, |
| 42 const std::vector<std::string>& value, | 62 const std::vector<std::string>& value, |
| 43 HttpRequestHeaders* headers) { | 63 HttpRequestHeaders* headers) { |
| 44 if (value.empty()) | 64 if (value.empty()) |
| 45 return; | 65 return; |
| 46 headers->SetHeader(name, JoinString(value, ", ")); | 66 headers->SetHeader(name, JoinString(value, ", ")); |
| 47 } | 67 } |
| 48 | 68 |
| 49 // If |case_sensitive| is false, then |value| must be in lower-case. | 69 GetHeaderResult GetSingleHeaderValue(const HttpResponseHeaders* headers, |
| 50 bool ValidateSingleTokenHeader( | 70 const base::StringPiece& name, |
| 51 const scoped_refptr<HttpResponseHeaders>& headers, | 71 std::string* value) { |
| 52 const base::StringPiece& name, | |
| 53 const std::string& value, | |
| 54 bool case_sensitive) { | |
| 55 void* state = NULL; | 72 void* state = NULL; |
| 73 int tokens = 0; | |
| 56 std::string token; | 74 std::string token; |
| 57 int tokens = 0; | |
| 58 bool has_value = false; | |
| 59 while (headers->EnumerateHeader(&state, name, &token)) { | 75 while (headers->EnumerateHeader(&state, name, &token)) { |
| 60 if (++tokens > 1) | 76 if (++tokens > 1) |
| 61 return false; | 77 return GET_HEADER_MULTIPLE; |
| 62 has_value = case_sensitive ? value == token | 78 *value = token; |
| 63 : LowerCaseEqualsASCII(token, value.c_str()); | |
| 64 } | 79 } |
| 65 return has_value; | 80 return tokens > 0 ? GET_HEADER_OK : GET_HEADER_MISSING; |
| 81 } | |
| 82 | |
| 83 bool ValidateHeaderHasSingleValue(GetHeaderResult result, | |
| 84 const std::string& header_name, | |
| 85 std::string* failure_message) { | |
| 86 if (result == GET_HEADER_MISSING) { | |
| 87 *failure_message = MissingHeaderMessage(header_name); | |
| 88 return false; | |
| 89 } | |
| 90 if (result == GET_HEADER_MULTIPLE) { | |
| 91 *failure_message = MultipleHeaderValuesMessage(header_name); | |
| 92 return false; | |
| 93 } | |
| 94 DCHECK_EQ(result, GET_HEADER_OK); | |
| 95 return true; | |
| 96 } | |
| 97 | |
| 98 bool ValidateUpgrade(const HttpResponseHeaders* headers, | |
| 99 std::string* failure_message) { | |
| 100 std::string value; | |
| 101 GetHeaderResult result = | |
| 102 GetSingleHeaderValue(headers, websockets::kUpgrade, &value); | |
| 103 if (!ValidateHeaderHasSingleValue(result, | |
| 104 websockets::kUpgrade, | |
| 105 failure_message)) { | |
| 106 return false; | |
| 107 } | |
| 108 | |
| 109 if (!LowerCaseEqualsASCII(value, websockets::kWebSocketLowercase)) { | |
| 110 *failure_message = | |
| 111 "'Upgrade' header value is not 'WebSocket': " + value; | |
| 112 return false; | |
| 113 } | |
| 114 return true; | |
| 115 } | |
| 116 | |
| 117 bool ValidateSecWebSocketAccept(const HttpResponseHeaders* headers, | |
| 118 const std::string& expected, | |
| 119 std::string* failure_message) { | |
| 120 std::string actual; | |
| 121 GetHeaderResult result = | |
| 122 GetSingleHeaderValue(headers, websockets::kSecWebSocketAccept, &actual); | |
| 123 if (!ValidateHeaderHasSingleValue(result, | |
| 124 websockets::kSecWebSocketAccept, | |
| 125 failure_message)) { | |
| 126 return false; | |
| 127 } | |
| 128 | |
| 129 if (expected != actual) { | |
| 130 *failure_message = "Incorrect 'Sec-WebSocket-Accept' header value"; | |
| 131 return false; | |
| 132 } | |
| 133 return true; | |
| 134 } | |
| 135 | |
| 136 bool ValidateConnection(const HttpResponseHeaders* headers, | |
| 137 std::string* failure_message) { | |
| 138 // Connection header is permitted to contain other tokens. | |
| 139 if (!headers->HasHeader(HttpRequestHeaders::kConnection)) { | |
| 140 *failure_message = MissingHeaderMessage(HttpRequestHeaders::kConnection); | |
| 141 return false; | |
| 142 } | |
| 143 if (!headers->HasHeaderValue(HttpRequestHeaders::kConnection, | |
| 144 websockets::kUpgrade)) { | |
| 145 *failure_message = "'Connection' header value must contain 'Upgrade'"; | |
| 146 return false; | |
| 147 } | |
| 148 return true; | |
| 66 } | 149 } |
| 67 | 150 |
| 68 bool ValidateSubProtocol( | 151 bool ValidateSubProtocol( |
| 69 const scoped_refptr<HttpResponseHeaders>& headers, | 152 const HttpResponseHeaders* headers, |
| 70 const std::vector<std::string>& requested_sub_protocols, | 153 const std::vector<std::string>& requested_sub_protocols, |
| 71 std::string* sub_protocol) { | 154 std::string* sub_protocol, |
| 155 std::string* failure_message) { | |
| 72 void* state = NULL; | 156 void* state = NULL; |
| 73 std::string token; | 157 std::string token; |
| 158 std::string last_token; | |
| 74 base::hash_set<std::string> requested_set(requested_sub_protocols.begin(), | 159 base::hash_set<std::string> requested_set(requested_sub_protocols.begin(), |
| 75 requested_sub_protocols.end()); | 160 requested_sub_protocols.end()); |
| 76 int accepted = 0; | 161 int count = 0; |
| 162 bool has_multiple_protocols = false; | |
| 163 bool has_invalid_protocol = false; | |
| 164 | |
| 77 while (headers->EnumerateHeader( | 165 while (headers->EnumerateHeader( |
| 78 &state, websockets::kSecWebSocketProtocol, &token)) { | 166 &state, websockets::kSecWebSocketProtocol, &token) && |
| 167 !(has_multiple_protocols && has_invalid_protocol)) { | |
| 79 if (requested_set.count(token) == 0) | 168 if (requested_set.count(token) == 0) |
| 80 return false; | 169 has_invalid_protocol = true; |
| 170 if (++count > 1) | |
| 171 has_multiple_protocols = true; | |
| 172 last_token = token; | |
| 173 } | |
| 81 | 174 |
| 82 *sub_protocol = token; | 175 if (has_multiple_protocols) { |
| 83 // The server is only allowed to accept one protocol. | 176 *failure_message = |
| 84 if (++accepted > 1) | 177 MultipleHeaderValuesMessage(websockets::kSecWebSocketProtocol); |
| 85 return false; | 178 return false; |
| 179 } else if (count > 0 && requested_sub_protocols.size() == 0) { | |
| 180 *failure_message = | |
| 181 std::string("Response must not include 'Sec-WebSocket-Protocol' " | |
| 182 "header if not present in request: ") | |
| 183 + last_token; | |
| 184 return false; | |
| 185 } else if (has_invalid_protocol) { | |
| 186 *failure_message = | |
| 187 "'Sec-WebSocket-Protocol' header value '" + | |
| 188 last_token + | |
| 189 "' in response does not match any of sent values"; | |
| 190 return false; | |
| 191 } else if (requested_sub_protocols.size() > 0 && count == 0) { | |
| 192 *failure_message = | |
| 193 "Sent non-empty 'Sec-WebSocket-Protocol' header " | |
| 194 "but no response was received"; | |
| 195 return false; | |
| 86 } | 196 } |
| 87 // If the browser requested > 0 protocols, the server is required to accept | 197 *sub_protocol = last_token; |
| 88 // one. | 198 return true; |
| 89 return requested_set.empty() || accepted == 1; | |
| 90 } | 199 } |
| 91 | 200 |
| 92 bool ValidateExtensions(const scoped_refptr<HttpResponseHeaders>& headers, | 201 bool ValidateExtensions(const HttpResponseHeaders* headers, |
| 93 const std::vector<std::string>& requested_extensions, | 202 const std::vector<std::string>& requested_extensions, |
| 94 std::string* extensions) { | 203 std::string* extensions, |
| 204 std::string* failure_message) { | |
| 95 void* state = NULL; | 205 void* state = NULL; |
| 96 std::string token; | 206 std::string token; |
| 97 while (headers->EnumerateHeader( | 207 while (headers->EnumerateHeader( |
| 98 &state, websockets::kSecWebSocketExtensions, &token)) { | 208 &state, websockets::kSecWebSocketExtensions, &token)) { |
|
tyoshino (SeeGerritForStatus)
2013/12/16 07:26:18
let's change "token" to some other name since "tok
yhirano
2013/12/16 08:05:38
Done.
| |
| 209 WebSocketExtensionParser parser; | |
| 210 parser.Parse(token); | |
| 211 if (parser.has_error()) { | |
| 212 // TODO(yhirano) Set appropriate failure message. | |
| 213 *failure_message = | |
| 214 "'WebSocket-Extensions' header value is rejected by the parser: " + | |
|
tyoshino (SeeGerritForStatus)
2013/12/16 07:26:18
Sec-
yhirano
2013/12/16 08:05:38
Done.
| |
| 215 token; | |
| 216 return false; | |
| 217 } | |
| 99 // TODO(ricea): Accept permessage-deflate with valid parameters. | 218 // TODO(ricea): Accept permessage-deflate with valid parameters. |
| 219 *failure_message = | |
| 220 "Found an unsupported extension '" + | |
| 221 parser.extension().name() + | |
| 222 "' in 'Sec-WebSocket-Extensions' header"; | |
| 100 return false; | 223 return false; |
| 101 } | 224 } |
| 102 return true; | 225 return true; |
| 103 } | 226 } |
| 104 | 227 |
| 105 } // namespace | 228 } // namespace |
| 106 | 229 |
| 107 WebSocketBasicHandshakeStream::WebSocketBasicHandshakeStream( | 230 WebSocketBasicHandshakeStream::WebSocketBasicHandshakeStream( |
| 108 scoped_ptr<ClientSocketHandle> connection, | 231 scoped_ptr<ClientSocketHandle> connection, |
| 109 bool using_proxy, | 232 bool using_proxy, |
| (...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 257 state_.read_buf(), | 380 state_.read_buf(), |
| 258 sub_protocol_, | 381 sub_protocol_, |
| 259 extensions_)); | 382 extensions_)); |
| 260 } | 383 } |
| 261 | 384 |
| 262 void WebSocketBasicHandshakeStream::SetWebSocketKeyForTesting( | 385 void WebSocketBasicHandshakeStream::SetWebSocketKeyForTesting( |
| 263 const std::string& key) { | 386 const std::string& key) { |
| 264 handshake_challenge_for_testing_.reset(new std::string(key)); | 387 handshake_challenge_for_testing_.reset(new std::string(key)); |
| 265 } | 388 } |
| 266 | 389 |
| 390 std::string WebSocketBasicHandshakeStream::GetFailureMessage() const { | |
| 391 return failure_message_; | |
| 392 } | |
| 393 | |
| 267 void WebSocketBasicHandshakeStream::ReadResponseHeadersCallback( | 394 void WebSocketBasicHandshakeStream::ReadResponseHeadersCallback( |
| 268 const CompletionCallback& callback, | 395 const CompletionCallback& callback, |
| 269 int result) { | 396 int result) { |
| 270 if (result == OK) | 397 if (result == OK) |
| 271 result = ValidateResponse(); | 398 result = ValidateResponse(); |
| 272 callback.Run(result); | 399 callback.Run(result); |
| 273 } | 400 } |
| 274 | 401 |
| 275 int WebSocketBasicHandshakeStream::ValidateResponse() { | 402 int WebSocketBasicHandshakeStream::ValidateResponse() { |
| 276 DCHECK(http_response_info_); | 403 DCHECK(http_response_info_); |
| 277 const scoped_refptr<HttpResponseHeaders>& headers = | 404 const scoped_refptr<HttpResponseHeaders>& headers = |
| 278 http_response_info_->headers; | 405 http_response_info_->headers; |
| 279 | 406 |
| 280 switch (headers->response_code()) { | 407 switch (headers->response_code()) { |
| 281 case HTTP_SWITCHING_PROTOCOLS: | 408 case HTTP_SWITCHING_PROTOCOLS: |
| 282 return ValidateUpgradeResponse(headers); | 409 return ValidateUpgradeResponse(headers); |
| 283 | 410 |
| 284 // We need to pass these through for authentication to work. | 411 // We need to pass these through for authentication to work. |
| 285 case HTTP_UNAUTHORIZED: | 412 case HTTP_UNAUTHORIZED: |
| 286 case HTTP_PROXY_AUTHENTICATION_REQUIRED: | 413 case HTTP_PROXY_AUTHENTICATION_REQUIRED: |
| 287 return OK; | 414 return OK; |
| 288 | 415 |
| 289 // Other status codes are potentially risky (see the warnings in the | 416 // Other status codes are potentially risky (see the warnings in the |
| 290 // WHATWG WebSocket API spec) and so are dropped by default. | 417 // WHATWG WebSocket API spec) and so are dropped by default. |
| 291 default: | 418 default: |
| 419 failure_message_ = base::StringPrintf("Unexpected status code: %d", | |
| 420 headers->response_code()); | |
| 292 return ERR_INVALID_RESPONSE; | 421 return ERR_INVALID_RESPONSE; |
| 293 } | 422 } |
| 294 } | 423 } |
| 295 | 424 |
| 296 int WebSocketBasicHandshakeStream::ValidateUpgradeResponse( | 425 int WebSocketBasicHandshakeStream::ValidateUpgradeResponse( |
| 297 const scoped_refptr<HttpResponseHeaders>& headers) { | 426 const scoped_refptr<HttpResponseHeaders>& headers) { |
| 298 if (ValidateSingleTokenHeader(headers, | 427 if (ValidateUpgrade(headers.get(), &failure_message_) && |
| 299 websockets::kUpgrade, | 428 ValidateSecWebSocketAccept(headers.get(), |
| 300 websockets::kWebSocketLowercase, | 429 handshake_challenge_response_, |
| 301 false) && | 430 &failure_message_) && |
| 302 ValidateSingleTokenHeader(headers, | 431 ValidateConnection(headers.get(), &failure_message_) && |
| 303 websockets::kSecWebSocketAccept, | 432 ValidateSubProtocol(headers.get(), |
| 304 handshake_challenge_response_, | 433 requested_sub_protocols_, |
| 305 true) && | 434 &sub_protocol_, |
| 306 headers->HasHeaderValue(HttpRequestHeaders::kConnection, | 435 &failure_message_) && |
| 307 websockets::kUpgrade) && | 436 ValidateExtensions(headers.get(), |
| 308 ValidateSubProtocol(headers, requested_sub_protocols_, &sub_protocol_) && | 437 requested_extensions_, |
| 309 ValidateExtensions(headers, requested_extensions_, &extensions_)) { | 438 &extensions_, |
| 439 &failure_message_)) { | |
| 310 return OK; | 440 return OK; |
| 311 } | 441 } |
| 442 failure_message_ = "Error during WebSocket handshake: " + failure_message_; | |
| 312 return ERR_INVALID_RESPONSE; | 443 return ERR_INVALID_RESPONSE; |
| 313 } | 444 } |
| 314 | 445 |
| 315 } // namespace net | 446 } // namespace net |
| OLD | NEW |