Avi Drissman | 6459548 | 2022-09-14 20:52:29 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors |
[email protected] | cf901f5 | 2012-05-10 04:21:48 | [diff] [blame] | 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/websockets/websocket_frame_parser.h" |
| 6 | |
| 7 | #include <algorithm> |
[email protected] | 8308b1d1 | 2012-08-01 08:31:29 | [diff] [blame] | 8 | #include <limits> |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 9 | #include <utility> |
yhirano | 592ff7f | 2015-12-07 08:45:19 | [diff] [blame] | 10 | #include <vector> |
[email protected] | cf901f5 | 2012-05-10 04:21:48 | [diff] [blame] | 11 | |
[email protected] | d9806a97 | 2014-02-26 18:14:57 | [diff] [blame] | 12 | #include "base/big_endian.h" |
[email protected] | cf901f5 | 2012-05-10 04:21:48 | [diff] [blame] | 13 | #include "base/logging.h" |
Bence Béky | 6562397 | 2018-03-05 15:31:56 | [diff] [blame] | 14 | #include "base/memory/scoped_refptr.h" |
[email protected] | 8308b1d1 | 2012-08-01 08:31:29 | [diff] [blame] | 15 | #include "net/base/io_buffer.h" |
[email protected] | cf901f5 | 2012-05-10 04:21:48 | [diff] [blame] | 16 | #include "net/websockets/websocket_frame.h" |
| 17 | |
| 18 | namespace { |
| 19 | |
tfarina | 8a2c66c2 | 2015-10-13 19:14:49 | [diff] [blame] | 20 | const uint8_t kFinalBit = 0x80; |
| 21 | const uint8_t kReserved1Bit = 0x40; |
| 22 | const uint8_t kReserved2Bit = 0x20; |
| 23 | const uint8_t kReserved3Bit = 0x10; |
| 24 | const uint8_t kOpCodeMask = 0xF; |
| 25 | const uint8_t kMaskBit = 0x80; |
| 26 | const uint8_t kPayloadLengthMask = 0x7F; |
| 27 | const uint64_t kMaxPayloadLengthWithoutExtendedLengthField = 125; |
| 28 | const uint64_t kPayloadLengthWithTwoByteExtendedLengthField = 126; |
| 29 | const uint64_t kPayloadLengthWithEightByteExtendedLengthField = 127; |
Yoichi Osato | 9a74219 | 2019-07-29 08:57:36 | [diff] [blame] | 30 | const size_t kMaximumFrameHeaderSize = |
| 31 | net::WebSocketFrameHeader::kBaseHeaderSize + |
| 32 | net::WebSocketFrameHeader::kMaximumExtendedLengthSize + |
| 33 | net::WebSocketFrameHeader::kMaskingKeyLength; |
[email protected] | cf901f5 | 2012-05-10 04:21:48 | [diff] [blame] | 34 | |
tfarina | 20ced66 | 2015-10-13 23:48:39 | [diff] [blame] | 35 | } // namespace. |
[email protected] | cf901f5 | 2012-05-10 04:21:48 | [diff] [blame] | 36 | |
| 37 | namespace net { |
| 38 | |
Tsuyoshi Horo | a0b9c0f | 2022-06-09 01:41:51 | [diff] [blame] | 39 | WebSocketFrameParser::WebSocketFrameParser() = default; |
[email protected] | cf901f5 | 2012-05-10 04:21:48 | [diff] [blame] | 40 | |
Chris Watkins | 28c2fdd | 2017-11-30 06:06:52 | [diff] [blame] | 41 | WebSocketFrameParser::~WebSocketFrameParser() = default; |
[email protected] | cf901f5 | 2012-05-10 04:21:48 | [diff] [blame] | 42 | |
| 43 | bool WebSocketFrameParser::Decode( |
| 44 | const char* data, |
| 45 | size_t length, |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 46 | std::vector<std::unique_ptr<WebSocketFrameChunk>>* frame_chunks) { |
[email protected] | 40e9c62f | 2013-05-07 14:59:21 | [diff] [blame] | 47 | if (websocket_error_ != kWebSocketNormalClosure) |
[email protected] | cf901f5 | 2012-05-10 04:21:48 | [diff] [blame] | 48 | return false; |
| 49 | if (!length) |
| 50 | return true; |
| 51 | |
Yoichi Osato | 9a74219 | 2019-07-29 08:57:36 | [diff] [blame] | 52 | base::span<const char> data_span = base::make_span(data, length); |
| 53 | // If we have incomplete frame header, try to decode a header combining with |
| 54 | // |data|. |
| 55 | bool first_chunk = false; |
| 56 | if (incomplete_header_buffer_.size() > 0) { |
| 57 | DCHECK(!current_frame_header_.get()); |
| 58 | const size_t original_size = incomplete_header_buffer_.size(); |
| 59 | DCHECK_LE(original_size, kMaximumFrameHeaderSize); |
| 60 | incomplete_header_buffer_.insert( |
| 61 | incomplete_header_buffer_.end(), data, |
| 62 | data + std::min(length, kMaximumFrameHeaderSize - original_size)); |
| 63 | const size_t consumed = DecodeFrameHeader(incomplete_header_buffer_); |
| 64 | if (websocket_error_ != kWebSocketNormalClosure) |
| 65 | return false; |
| 66 | if (!current_frame_header_.get()) |
| 67 | return true; |
[email protected] | cf901f5 | 2012-05-10 04:21:48 | [diff] [blame] | 68 | |
Yoichi Osato | 9a74219 | 2019-07-29 08:57:36 | [diff] [blame] | 69 | DCHECK_GE(consumed, original_size); |
| 70 | data_span = data_span.subspan(consumed - original_size); |
| 71 | incomplete_header_buffer_.clear(); |
| 72 | first_chunk = true; |
| 73 | } |
| 74 | |
| 75 | DCHECK(incomplete_header_buffer_.empty()); |
| 76 | while (data_span.size() > 0 || first_chunk) { |
[email protected] | cf901f5 | 2012-05-10 04:21:48 | [diff] [blame] | 77 | if (!current_frame_header_.get()) { |
Yoichi Osato | 9a74219 | 2019-07-29 08:57:36 | [diff] [blame] | 78 | const size_t consumed = DecodeFrameHeader(data_span); |
[email protected] | 40e9c62f | 2013-05-07 14:59:21 | [diff] [blame] | 79 | if (websocket_error_ != kWebSocketNormalClosure) |
[email protected] | cf901f5 | 2012-05-10 04:21:48 | [diff] [blame] | 80 | return false; |
| 81 | // If frame header is incomplete, then carry over the remaining |
| 82 | // data to the next round of Decode(). |
Yoichi Osato | 9a74219 | 2019-07-29 08:57:36 | [diff] [blame] | 83 | if (!current_frame_header_.get()) { |
| 84 | DCHECK(!consumed); |
| 85 | incomplete_header_buffer_.insert(incomplete_header_buffer_.end(), |
| 86 | data_span.data(), |
| 87 | data_span.data() + data_span.size()); |
| 88 | // Sanity check: the size of carried-over data should not exceed |
| 89 | // the maximum possible length of a frame header. |
| 90 | DCHECK_LT(incomplete_header_buffer_.size(), kMaximumFrameHeaderSize); |
| 91 | return true; |
| 92 | } |
| 93 | DCHECK_GE(data_span.size(), consumed); |
| 94 | data_span = data_span.subspan(consumed); |
[email protected] | cf901f5 | 2012-05-10 04:21:48 | [diff] [blame] | 95 | first_chunk = true; |
| 96 | } |
Yoichi Osato | 9a74219 | 2019-07-29 08:57:36 | [diff] [blame] | 97 | DCHECK(incomplete_header_buffer_.empty()); |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 98 | std::unique_ptr<WebSocketFrameChunk> frame_chunk = |
Yoichi Osato | 9a74219 | 2019-07-29 08:57:36 | [diff] [blame] | 99 | DecodeFramePayload(first_chunk, &data_span); |
| 100 | first_chunk = false; |
[email protected] | cf901f5 | 2012-05-10 04:21:48 | [diff] [blame] | 101 | DCHECK(frame_chunk.get()); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 102 | frame_chunks->push_back(std::move(frame_chunk)); |
[email protected] | cf901f5 | 2012-05-10 04:21:48 | [diff] [blame] | 103 | } |
[email protected] | cf901f5 | 2012-05-10 04:21:48 | [diff] [blame] | 104 | return true; |
| 105 | } |
| 106 | |
Yoichi Osato | 9a74219 | 2019-07-29 08:57:36 | [diff] [blame] | 107 | size_t WebSocketFrameParser::DecodeFrameHeader(base::span<const char> data) { |
| 108 | DVLOG(3) << "DecodeFrameHeader buffer size:" |
| 109 | << ", data size:" << data.size(); |
[email protected] | cf901f5 | 2012-05-10 04:21:48 | [diff] [blame] | 110 | typedef WebSocketFrameHeader::OpCode OpCode; |
[email protected] | cf901f5 | 2012-05-10 04:21:48 | [diff] [blame] | 111 | DCHECK(!current_frame_header_.get()); |
| 112 | |
[email protected] | cf901f5 | 2012-05-10 04:21:48 | [diff] [blame] | 113 | // Header needs 2 bytes at minimum. |
Yoichi Osato | 9a74219 | 2019-07-29 08:57:36 | [diff] [blame] | 114 | if (data.size() < 2) |
| 115 | return 0; |
| 116 | size_t current = 0; |
| 117 | const uint8_t first_byte = data[current++]; |
| 118 | const uint8_t second_byte = data[current++]; |
[email protected] | cf901f5 | 2012-05-10 04:21:48 | [diff] [blame] | 119 | |
Yoichi Osato | 9a74219 | 2019-07-29 08:57:36 | [diff] [blame] | 120 | const bool final = (first_byte & kFinalBit) != 0; |
| 121 | const bool reserved1 = (first_byte & kReserved1Bit) != 0; |
| 122 | const bool reserved2 = (first_byte & kReserved2Bit) != 0; |
| 123 | const bool reserved3 = (first_byte & kReserved3Bit) != 0; |
| 124 | const OpCode opcode = first_byte & kOpCodeMask; |
[email protected] | cf901f5 | 2012-05-10 04:21:48 | [diff] [blame] | 125 | |
tfarina | 8a2c66c2 | 2015-10-13 19:14:49 | [diff] [blame] | 126 | uint64_t payload_length = second_byte & kPayloadLengthMask; |
[email protected] | cf901f5 | 2012-05-10 04:21:48 | [diff] [blame] | 127 | if (payload_length == kPayloadLengthWithTwoByteExtendedLengthField) { |
Yoichi Osato | 9a74219 | 2019-07-29 08:57:36 | [diff] [blame] | 128 | if (data.size() < current + 2) |
| 129 | return 0; |
tfarina | 8a2c66c2 | 2015-10-13 19:14:49 | [diff] [blame] | 130 | uint16_t payload_length_16; |
Felix Weilbach | b7f34d81 | 2021-11-17 18:42:45 | [diff] [blame] | 131 | base::ReadBigEndian(reinterpret_cast<const uint8_t*>(&data[current]), |
| 132 | &payload_length_16); |
[email protected] | cf901f5 | 2012-05-10 04:21:48 | [diff] [blame] | 133 | current += 2; |
| 134 | payload_length = payload_length_16; |
Yoichi Osato | 9a74219 | 2019-07-29 08:57:36 | [diff] [blame] | 135 | if (payload_length <= kMaxPayloadLengthWithoutExtendedLengthField) { |
[email protected] | 40e9c62f | 2013-05-07 14:59:21 | [diff] [blame] | 136 | websocket_error_ = kWebSocketErrorProtocolError; |
Yoichi Osato | 9a74219 | 2019-07-29 08:57:36 | [diff] [blame] | 137 | return 0; |
| 138 | } |
[email protected] | cf901f5 | 2012-05-10 04:21:48 | [diff] [blame] | 139 | } else if (payload_length == kPayloadLengthWithEightByteExtendedLengthField) { |
Yoichi Osato | 9a74219 | 2019-07-29 08:57:36 | [diff] [blame] | 140 | if (data.size() < current + 8) |
| 141 | return 0; |
Felix Weilbach | b7f34d81 | 2021-11-17 18:42:45 | [diff] [blame] | 142 | base::ReadBigEndian(reinterpret_cast<const uint8_t*>(&data[current]), |
| 143 | &payload_length); |
[email protected] | cf901f5 | 2012-05-10 04:21:48 | [diff] [blame] | 144 | current += 8; |
tfarina | 20ced66 | 2015-10-13 23:48:39 | [diff] [blame] | 145 | if (payload_length <= UINT16_MAX || |
| 146 | payload_length > static_cast<uint64_t>(INT64_MAX)) { |
[email protected] | 40e9c62f | 2013-05-07 14:59:21 | [diff] [blame] | 147 | websocket_error_ = kWebSocketErrorProtocolError; |
Yoichi Osato | 9a74219 | 2019-07-29 08:57:36 | [diff] [blame] | 148 | return 0; |
| 149 | } |
| 150 | if (payload_length > static_cast<uint64_t>(INT32_MAX)) { |
[email protected] | 40e9c62f | 2013-05-07 14:59:21 | [diff] [blame] | 151 | websocket_error_ = kWebSocketErrorMessageTooBig; |
Yoichi Osato | 9a74219 | 2019-07-29 08:57:36 | [diff] [blame] | 152 | return 0; |
[email protected] | cf901f5 | 2012-05-10 04:21:48 | [diff] [blame] | 153 | } |
| 154 | } |
Yoichi Osato | 9a74219 | 2019-07-29 08:57:36 | [diff] [blame] | 155 | DCHECK_EQ(websocket_error_, kWebSocketNormalClosure); |
[email protected] | cf901f5 | 2012-05-10 04:21:48 | [diff] [blame] | 156 | |
Yutaka Hirano | 29c646f | 2019-09-04 23:48:03 | [diff] [blame] | 157 | WebSocketMaskingKey masking_key = {}; |
Yoichi Osato | 9a74219 | 2019-07-29 08:57:36 | [diff] [blame] | 158 | const bool masked = (second_byte & kMaskBit) != 0; |
| 159 | static const int kMaskingKeyLength = WebSocketFrameHeader::kMaskingKeyLength; |
[email protected] | cf901f5 | 2012-05-10 04:21:48 | [diff] [blame] | 160 | if (masked) { |
Yoichi Osato | 9a74219 | 2019-07-29 08:57:36 | [diff] [blame] | 161 | if (data.size() < current + kMaskingKeyLength) |
| 162 | return 0; |
| 163 | std::copy(&data[current], &data[current] + kMaskingKeyLength, |
Yutaka Hirano | 29c646f | 2019-09-04 23:48:03 | [diff] [blame] | 164 | masking_key.key); |
[email protected] | cf901f5 | 2012-05-10 04:21:48 | [diff] [blame] | 165 | current += kMaskingKeyLength; |
[email protected] | cf901f5 | 2012-05-10 04:21:48 | [diff] [blame] | 166 | } |
| 167 | |
Bence Béky | 6562397 | 2018-03-05 15:31:56 | [diff] [blame] | 168 | current_frame_header_ = std::make_unique<WebSocketFrameHeader>(opcode); |
[email protected] | cf901f5 | 2012-05-10 04:21:48 | [diff] [blame] | 169 | current_frame_header_->final = final; |
| 170 | current_frame_header_->reserved1 = reserved1; |
| 171 | current_frame_header_->reserved2 = reserved2; |
| 172 | current_frame_header_->reserved3 = reserved3; |
[email protected] | cf901f5 | 2012-05-10 04:21:48 | [diff] [blame] | 173 | current_frame_header_->masked = masked; |
Yutaka Hirano | 29c646f | 2019-09-04 23:48:03 | [diff] [blame] | 174 | current_frame_header_->masking_key = masking_key; |
[email protected] | cf901f5 | 2012-05-10 04:21:48 | [diff] [blame] | 175 | current_frame_header_->payload_length = payload_length; |
[email protected] | cf901f5 | 2012-05-10 04:21:48 | [diff] [blame] | 176 | DCHECK_EQ(0u, frame_offset_); |
Yoichi Osato | 9a74219 | 2019-07-29 08:57:36 | [diff] [blame] | 177 | return current; |
[email protected] | cf901f5 | 2012-05-10 04:21:48 | [diff] [blame] | 178 | } |
| 179 | |
danakj | 9c5cab5 | 2016-04-16 00:54:33 | [diff] [blame] | 180 | std::unique_ptr<WebSocketFrameChunk> WebSocketFrameParser::DecodeFramePayload( |
Yoichi Osato | 9a74219 | 2019-07-29 08:57:36 | [diff] [blame] | 181 | bool first_chunk, |
| 182 | base::span<const char>* data) { |
pkasting | 4bff6be | 2014-10-15 17:54:34 | [diff] [blame] | 183 | // The cast here is safe because |payload_length| is already checked to be |
[email protected] | 8308b1d1 | 2012-08-01 08:31:29 | [diff] [blame] | 184 | // less than std::numeric_limits<int>::max() when the header is parsed. |
Yoichi Osato | 9a74219 | 2019-07-29 08:57:36 | [diff] [blame] | 185 | const int chunk_data_size = static_cast<int>( |
| 186 | std::min(static_cast<uint64_t>(data->size()), |
tfarina | 8a2c66c2 | 2015-10-13 19:14:49 | [diff] [blame] | 187 | current_frame_header_->payload_length - frame_offset_)); |
[email protected] | cf901f5 | 2012-05-10 04:21:48 | [diff] [blame] | 188 | |
Bence Béky | 6562397 | 2018-03-05 15:31:56 | [diff] [blame] | 189 | auto frame_chunk = std::make_unique<WebSocketFrameChunk>(); |
[email protected] | cf901f5 | 2012-05-10 04:21:48 | [diff] [blame] | 190 | if (first_chunk) { |
[email protected] | 40e9c62f | 2013-05-07 14:59:21 | [diff] [blame] | 191 | frame_chunk->header = current_frame_header_->Clone(); |
[email protected] | cf901f5 | 2012-05-10 04:21:48 | [diff] [blame] | 192 | } |
| 193 | frame_chunk->final_chunk = false; |
Yutaka Hirano | 76aacb20 | 2019-09-05 16:36:56 | [diff] [blame] | 194 | if (chunk_data_size > 0) { |
Yoichi Osato | 05cd364 | 2019-09-09 18:13:08 | [diff] [blame] | 195 | frame_chunk->payload = data->subspan(0, chunk_data_size); |
Yoichi Osato | 9a74219 | 2019-07-29 08:57:36 | [diff] [blame] | 196 | *data = data->subspan(chunk_data_size); |
Yoichi Osato | 9a74219 | 2019-07-29 08:57:36 | [diff] [blame] | 197 | frame_offset_ += chunk_data_size; |
[email protected] | 8308b1d1 | 2012-08-01 08:31:29 | [diff] [blame] | 198 | } |
[email protected] | cf901f5 | 2012-05-10 04:21:48 | [diff] [blame] | 199 | |
| 200 | DCHECK_LE(frame_offset_, current_frame_header_->payload_length); |
| 201 | if (frame_offset_ == current_frame_header_->payload_length) { |
| 202 | frame_chunk->final_chunk = true; |
| 203 | current_frame_header_.reset(); |
| 204 | frame_offset_ = 0; |
| 205 | } |
| 206 | |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 207 | return frame_chunk; |
[email protected] | cf901f5 | 2012-05-10 04:21:48 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | } // namespace net |