blob: b5829d6c08292de5c3e97e4a29bc78fe2b1d2452 [file] [log] [blame]
Avi Drissman64595482022-09-14 20:52:291// Copyright 2012 The Chromium Authors
[email protected]cf901f52012-05-10 04:21:482// 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]8308b1d12012-08-01 08:31:298#include <limits>
dchengc7eeda422015-12-26 03:56:489#include <utility>
yhirano592ff7f2015-12-07 08:45:1910#include <vector>
[email protected]cf901f52012-05-10 04:21:4811
[email protected]d9806a972014-02-26 18:14:5712#include "base/big_endian.h"
[email protected]cf901f52012-05-10 04:21:4813#include "base/logging.h"
Bence Béky65623972018-03-05 15:31:5614#include "base/memory/scoped_refptr.h"
[email protected]8308b1d12012-08-01 08:31:2915#include "net/base/io_buffer.h"
[email protected]cf901f52012-05-10 04:21:4816#include "net/websockets/websocket_frame.h"
17
18namespace {
19
tfarina8a2c66c22015-10-13 19:14:4920const uint8_t kFinalBit = 0x80;
21const uint8_t kReserved1Bit = 0x40;
22const uint8_t kReserved2Bit = 0x20;
23const uint8_t kReserved3Bit = 0x10;
24const uint8_t kOpCodeMask = 0xF;
25const uint8_t kMaskBit = 0x80;
26const uint8_t kPayloadLengthMask = 0x7F;
27const uint64_t kMaxPayloadLengthWithoutExtendedLengthField = 125;
28const uint64_t kPayloadLengthWithTwoByteExtendedLengthField = 126;
29const uint64_t kPayloadLengthWithEightByteExtendedLengthField = 127;
Yoichi Osato9a742192019-07-29 08:57:3630const size_t kMaximumFrameHeaderSize =
31 net::WebSocketFrameHeader::kBaseHeaderSize +
32 net::WebSocketFrameHeader::kMaximumExtendedLengthSize +
33 net::WebSocketFrameHeader::kMaskingKeyLength;
[email protected]cf901f52012-05-10 04:21:4834
tfarina20ced662015-10-13 23:48:3935} // namespace.
[email protected]cf901f52012-05-10 04:21:4836
37namespace net {
38
Tsuyoshi Horoa0b9c0f2022-06-09 01:41:5139WebSocketFrameParser::WebSocketFrameParser() = default;
[email protected]cf901f52012-05-10 04:21:4840
Chris Watkins28c2fdd2017-11-30 06:06:5241WebSocketFrameParser::~WebSocketFrameParser() = default;
[email protected]cf901f52012-05-10 04:21:4842
43bool WebSocketFrameParser::Decode(
44 const char* data,
45 size_t length,
danakj9c5cab52016-04-16 00:54:3346 std::vector<std::unique_ptr<WebSocketFrameChunk>>* frame_chunks) {
[email protected]40e9c62f2013-05-07 14:59:2147 if (websocket_error_ != kWebSocketNormalClosure)
[email protected]cf901f52012-05-10 04:21:4848 return false;
49 if (!length)
50 return true;
51
Yoichi Osato9a742192019-07-29 08:57:3652 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]cf901f52012-05-10 04:21:4868
Yoichi Osato9a742192019-07-29 08:57:3669 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]cf901f52012-05-10 04:21:4877 if (!current_frame_header_.get()) {
Yoichi Osato9a742192019-07-29 08:57:3678 const size_t consumed = DecodeFrameHeader(data_span);
[email protected]40e9c62f2013-05-07 14:59:2179 if (websocket_error_ != kWebSocketNormalClosure)
[email protected]cf901f52012-05-10 04:21:4880 return false;
81 // If frame header is incomplete, then carry over the remaining
82 // data to the next round of Decode().
Yoichi Osato9a742192019-07-29 08:57:3683 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]cf901f52012-05-10 04:21:4895 first_chunk = true;
96 }
Yoichi Osato9a742192019-07-29 08:57:3697 DCHECK(incomplete_header_buffer_.empty());
danakj9c5cab52016-04-16 00:54:3398 std::unique_ptr<WebSocketFrameChunk> frame_chunk =
Yoichi Osato9a742192019-07-29 08:57:3699 DecodeFramePayload(first_chunk, &data_span);
100 first_chunk = false;
[email protected]cf901f52012-05-10 04:21:48101 DCHECK(frame_chunk.get());
dchengc7eeda422015-12-26 03:56:48102 frame_chunks->push_back(std::move(frame_chunk));
[email protected]cf901f52012-05-10 04:21:48103 }
[email protected]cf901f52012-05-10 04:21:48104 return true;
105}
106
Yoichi Osato9a742192019-07-29 08:57:36107size_t WebSocketFrameParser::DecodeFrameHeader(base::span<const char> data) {
108 DVLOG(3) << "DecodeFrameHeader buffer size:"
109 << ", data size:" << data.size();
[email protected]cf901f52012-05-10 04:21:48110 typedef WebSocketFrameHeader::OpCode OpCode;
[email protected]cf901f52012-05-10 04:21:48111 DCHECK(!current_frame_header_.get());
112
[email protected]cf901f52012-05-10 04:21:48113 // Header needs 2 bytes at minimum.
Yoichi Osato9a742192019-07-29 08:57:36114 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]cf901f52012-05-10 04:21:48119
Yoichi Osato9a742192019-07-29 08:57:36120 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]cf901f52012-05-10 04:21:48125
tfarina8a2c66c22015-10-13 19:14:49126 uint64_t payload_length = second_byte & kPayloadLengthMask;
[email protected]cf901f52012-05-10 04:21:48127 if (payload_length == kPayloadLengthWithTwoByteExtendedLengthField) {
Yoichi Osato9a742192019-07-29 08:57:36128 if (data.size() < current + 2)
129 return 0;
tfarina8a2c66c22015-10-13 19:14:49130 uint16_t payload_length_16;
Felix Weilbachb7f34d812021-11-17 18:42:45131 base::ReadBigEndian(reinterpret_cast<const uint8_t*>(&data[current]),
132 &payload_length_16);
[email protected]cf901f52012-05-10 04:21:48133 current += 2;
134 payload_length = payload_length_16;
Yoichi Osato9a742192019-07-29 08:57:36135 if (payload_length <= kMaxPayloadLengthWithoutExtendedLengthField) {
[email protected]40e9c62f2013-05-07 14:59:21136 websocket_error_ = kWebSocketErrorProtocolError;
Yoichi Osato9a742192019-07-29 08:57:36137 return 0;
138 }
[email protected]cf901f52012-05-10 04:21:48139 } else if (payload_length == kPayloadLengthWithEightByteExtendedLengthField) {
Yoichi Osato9a742192019-07-29 08:57:36140 if (data.size() < current + 8)
141 return 0;
Felix Weilbachb7f34d812021-11-17 18:42:45142 base::ReadBigEndian(reinterpret_cast<const uint8_t*>(&data[current]),
143 &payload_length);
[email protected]cf901f52012-05-10 04:21:48144 current += 8;
tfarina20ced662015-10-13 23:48:39145 if (payload_length <= UINT16_MAX ||
146 payload_length > static_cast<uint64_t>(INT64_MAX)) {
[email protected]40e9c62f2013-05-07 14:59:21147 websocket_error_ = kWebSocketErrorProtocolError;
Yoichi Osato9a742192019-07-29 08:57:36148 return 0;
149 }
150 if (payload_length > static_cast<uint64_t>(INT32_MAX)) {
[email protected]40e9c62f2013-05-07 14:59:21151 websocket_error_ = kWebSocketErrorMessageTooBig;
Yoichi Osato9a742192019-07-29 08:57:36152 return 0;
[email protected]cf901f52012-05-10 04:21:48153 }
154 }
Yoichi Osato9a742192019-07-29 08:57:36155 DCHECK_EQ(websocket_error_, kWebSocketNormalClosure);
[email protected]cf901f52012-05-10 04:21:48156
Yutaka Hirano29c646f2019-09-04 23:48:03157 WebSocketMaskingKey masking_key = {};
Yoichi Osato9a742192019-07-29 08:57:36158 const bool masked = (second_byte & kMaskBit) != 0;
159 static const int kMaskingKeyLength = WebSocketFrameHeader::kMaskingKeyLength;
[email protected]cf901f52012-05-10 04:21:48160 if (masked) {
Yoichi Osato9a742192019-07-29 08:57:36161 if (data.size() < current + kMaskingKeyLength)
162 return 0;
163 std::copy(&data[current], &data[current] + kMaskingKeyLength,
Yutaka Hirano29c646f2019-09-04 23:48:03164 masking_key.key);
[email protected]cf901f52012-05-10 04:21:48165 current += kMaskingKeyLength;
[email protected]cf901f52012-05-10 04:21:48166 }
167
Bence Béky65623972018-03-05 15:31:56168 current_frame_header_ = std::make_unique<WebSocketFrameHeader>(opcode);
[email protected]cf901f52012-05-10 04:21:48169 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]cf901f52012-05-10 04:21:48173 current_frame_header_->masked = masked;
Yutaka Hirano29c646f2019-09-04 23:48:03174 current_frame_header_->masking_key = masking_key;
[email protected]cf901f52012-05-10 04:21:48175 current_frame_header_->payload_length = payload_length;
[email protected]cf901f52012-05-10 04:21:48176 DCHECK_EQ(0u, frame_offset_);
Yoichi Osato9a742192019-07-29 08:57:36177 return current;
[email protected]cf901f52012-05-10 04:21:48178}
179
danakj9c5cab52016-04-16 00:54:33180std::unique_ptr<WebSocketFrameChunk> WebSocketFrameParser::DecodeFramePayload(
Yoichi Osato9a742192019-07-29 08:57:36181 bool first_chunk,
182 base::span<const char>* data) {
pkasting4bff6be2014-10-15 17:54:34183 // The cast here is safe because |payload_length| is already checked to be
[email protected]8308b1d12012-08-01 08:31:29184 // less than std::numeric_limits<int>::max() when the header is parsed.
Yoichi Osato9a742192019-07-29 08:57:36185 const int chunk_data_size = static_cast<int>(
186 std::min(static_cast<uint64_t>(data->size()),
tfarina8a2c66c22015-10-13 19:14:49187 current_frame_header_->payload_length - frame_offset_));
[email protected]cf901f52012-05-10 04:21:48188
Bence Béky65623972018-03-05 15:31:56189 auto frame_chunk = std::make_unique<WebSocketFrameChunk>();
[email protected]cf901f52012-05-10 04:21:48190 if (first_chunk) {
[email protected]40e9c62f2013-05-07 14:59:21191 frame_chunk->header = current_frame_header_->Clone();
[email protected]cf901f52012-05-10 04:21:48192 }
193 frame_chunk->final_chunk = false;
Yutaka Hirano76aacb202019-09-05 16:36:56194 if (chunk_data_size > 0) {
Yoichi Osato05cd3642019-09-09 18:13:08195 frame_chunk->payload = data->subspan(0, chunk_data_size);
Yoichi Osato9a742192019-07-29 08:57:36196 *data = data->subspan(chunk_data_size);
Yoichi Osato9a742192019-07-29 08:57:36197 frame_offset_ += chunk_data_size;
[email protected]8308b1d12012-08-01 08:31:29198 }
[email protected]cf901f52012-05-10 04:21:48199
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
dchengc7eeda422015-12-26 03:56:48207 return frame_chunk;
[email protected]cf901f52012-05-10 04:21:48208}
209
210} // namespace net