Coverage Report

Created: 2026-06-01 07:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/curl_fuzzer/proto_fuzzer/ws_frame.cc
Line
Count
Source
1
/*
2
 * Copyright (C) Max Dymond, <cmeister2@gmail.com>, et al.
3
 *
4
 * SPDX-License-Identifier: curl
5
 */
6
7
/// @file
8
/// @brief Implementation of SerializeWebSocketFrame.
9
10
#include "proto_fuzzer/ws_frame.h"
11
12
#include <cstddef>
13
#include <cstdint>
14
#include <string>
15
16
namespace proto_fuzzer {
17
18
namespace {
19
20
// RFC 6455 §5.2 length-form selectors.
21
constexpr std::uint32_t kLenFormAuto = 0;
22
constexpr std::uint32_t kLenForm7 = 1;
23
constexpr std::uint32_t kLenForm16 = 2;
24
constexpr std::uint32_t kLenForm64 = 3;
25
26
/// Pick a length encoding for 'payload_len' honouring an explicit override.
27
/// When 'form' names a shorter encoding than the payload actually needs, we
28
/// still emit the shorter form and let the decoder reject it — the point is
29
/// to exercise the error path.
30
6.54k
std::uint32_t ResolveLengthForm(std::uint32_t form, std::size_t payload_len) {
31
6.54k
  if (form == kLenForm7 || form == kLenForm16 || form == kLenForm64) {
32
343
    return form;
33
343
  }
34
6.20k
  if (payload_len < 126) {
35
6.03k
    return kLenForm7;
36
6.03k
  }
37
165
  if (payload_len <= 0xFFFF) {
38
165
    return kLenForm16;
39
165
  }
40
0
  return kLenForm64;
41
165
}
42
43
/// Append 'value' as big-endian bytes of width 'width' to 'out'.
44
448
void AppendBigEndian(std::string* out, std::uint64_t value, std::size_t width) {
45
2.11k
  for (std::size_t i = 0; i < width; ++i) {
46
1.66k
    std::size_t shift = (width - 1 - i) * 8;
47
1.66k
    out->push_back(static_cast<char>((value >> shift) & 0xFF));
48
1.66k
  }
49
448
}
50
51
}  // namespace
52
53
/// Serialise a proto WebSocketFrame into RFC 6455 wire bytes. No validation
54
/// is performed — invalid combinations (reserved bits set, oversized
55
/// length_form for a tiny payload, opcode > 15) round-trip to the decoder
56
/// unchanged, which is the point.
57
/// @param frame The WebSocketFrame proto message to render.
58
/// @return The serialised byte string, ready to push onto the mock socket.
59
6.54k
std::string SerializeWebSocketFrame(const curl::fuzzer::proto::WebSocketFrame& frame) {
60
6.54k
  std::string out;
61
6.54k
  const std::string& payload = frame.payload();
62
6.54k
  const std::size_t payload_len = payload.size();
63
64
  // Byte 0: FIN | RSV1 | RSV2 | RSV3 | opcode(4 bits).
65
6.54k
  std::uint8_t byte0 = 0;
66
6.54k
  if (frame.fin()) byte0 |= 0x80;
67
6.54k
  if (frame.rsv1()) byte0 |= 0x40;
68
6.54k
  if (frame.rsv2()) byte0 |= 0x20;
69
6.54k
  if (frame.rsv3()) byte0 |= 0x10;
70
6.54k
  byte0 |= static_cast<std::uint8_t>(frame.opcode() & 0x0F);
71
6.54k
  out.push_back(static_cast<char>(byte0));
72
73
  // Byte 1: MASK bit | payload-length indicator (7 bits).
74
6.54k
  const std::uint32_t length_form = ResolveLengthForm(frame.length_form(), payload_len);
75
6.54k
  std::uint8_t byte1 = frame.masked() ? 0x80 : 0x00;
76
6.54k
  switch (length_form) {
77
6.09k
    case kLenForm7:
78
      // Clamp the 7-bit length to the payload's actual size. If the payload
79
      // is > 125 bytes and the caller forced 7-bit form, low 7 bits of size
80
      // are what the decoder sees — which is exactly the malformed-frame
81
      // path we want to reach.
82
6.09k
      byte1 |= static_cast<std::uint8_t>(payload_len & 0x7F);
83
6.09k
      out.push_back(static_cast<char>(byte1));
84
6.09k
      break;
85
320
    case kLenForm16:
86
320
      byte1 |= 126;
87
320
      out.push_back(static_cast<char>(byte1));
88
320
      AppendBigEndian(&out, static_cast<std::uint64_t>(payload_len & 0xFFFF), 2);
89
320
      break;
90
128
    case kLenForm64:
91
128
    default:
92
128
      byte1 |= 127;
93
128
      out.push_back(static_cast<char>(byte1));
94
128
      AppendBigEndian(&out, static_cast<std::uint64_t>(payload_len), 8);
95
128
      break;
96
6.54k
  }
97
98
  // Masking key (4 bytes) and XORed payload. Client-to-server frames must be
99
  // masked per spec; server-to-client frames must NOT — but we emit whatever
100
  // the scenario says, so the decoder's "masked server frame" error path is
101
  // reachable.
102
6.54k
  if (frame.masked()) {
103
1.80k
    const std::uint32_t key = frame.mask_key();
104
1.80k
    std::uint8_t key_bytes[4] = {
105
1.80k
        static_cast<std::uint8_t>((key >> 24) & 0xFF),
106
1.80k
        static_cast<std::uint8_t>((key >> 16) & 0xFF),
107
1.80k
        static_cast<std::uint8_t>((key >> 8) & 0xFF),
108
1.80k
        static_cast<std::uint8_t>(key & 0xFF),
109
1.80k
    };
110
7.22k
    for (unsigned char b : key_bytes) {
111
7.22k
      out.push_back(static_cast<char>(b));
112
7.22k
    }
113
1.80k
    out.reserve(out.size() + payload_len);
114
87.1k
    for (std::size_t i = 0; i < payload_len; ++i) {
115
85.3k
      out.push_back(static_cast<char>(static_cast<std::uint8_t>(payload[i]) ^ key_bytes[i & 0x3]));
116
85.3k
    }
117
4.73k
  } else {
118
4.73k
    out.append(payload);
119
4.73k
  }
120
121
6.54k
  return out;
122
6.54k
}
123
124
}  // namespace proto_fuzzer