Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/inspector_socket.cc
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,11 @@ static ws_decode_result decode_frame_hybi17(const std::vector<char>& buffer,
}
size_t payload_length = static_cast<size_t>(payload_length64);

if (buffer.size() - kMaskingKeyWidthInBytes < payload_length)
// The masking key and the payload follow the header `it` already walked
// past, so they have to fit in what is left rather than in the whole buffer.
size_t remaining = static_cast<size_t>(buffer.end() - it);
if (remaining < kMaskingKeyWidthInBytes ||
remaining - kMaskingKeyWidthInBytes < payload_length)
Comment on lines +366 to +367

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This reads to me equivalent to this one liner:

Suggested change
if (remaining < kMaskingKeyWidthInBytes ||
remaining - kMaskingKeyWidthInBytes < payload_length)
if (remaining < kMaskingKeyWidthInBytes + payload_length)

return FRAME_INCOMPLETE;

std::vector<char>::const_iterator masking_key = it;
Expand Down
18 changes: 18 additions & 0 deletions test/cctest/test_inspector_socket.cc
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,24 @@ TEST_F(InspectorSocketTest, ReadsAndWritesInspectorMessage) {
reinterpret_cast<uv_handle_t*>(&client_socket)));
}

TEST_F(InspectorSocketTest, WaitsForFrameBodyToArrive) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test also passed on the main branch without the patch.

do_write(const_cast<char*>(HANDSHAKE_REQ), sizeof(HANDSHAKE_REQ) - 1);
expect_handshake();

// A header announcing a masked five byte payload, with the masking key and
// the payload still on the wire. The frame has to be treated as incomplete
// until the remaining bytes show up.
const char FRAME_HEADER[] = {'\x81', '\x85'};
do_write(FRAME_HEADER, sizeof(FRAME_HEADER));

const char FRAME_BODY[] = {'\x01', '\x02', '\x03', '\x04',
'\x69', '\x67', '\x6F', '\x68', '\x6E'};
do_write(FRAME_BODY, sizeof(FRAME_BODY));

const char CLIENT_MESSAGE[] = "hello";
delegate->ExpectData(CLIENT_MESSAGE, sizeof(CLIENT_MESSAGE) - 1);
}

TEST_F(InspectorSocketTest, BufferEdgeCases) {
do_write(const_cast<char*>(HANDSHAKE_REQ), sizeof(HANDSHAKE_REQ) - 1);
expect_handshake();
Expand Down
Loading