Coverage Report

Created: 2026-05-30 06:25

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.69k
std::uint32_t ResolveLengthForm(std::uint32_t form, std::size_t payload_len) {
31
6.69k
  if (form == kLenForm7 || form == kLenForm16 || form == kLenForm64) {
32
332
    return form;
33
332
  }
34
6.36k
  if (payload_len < 126) {
35
6.20k
    return kLenForm7;
36
6.20k
  }
37
156
  if (payload_len <= 0xFFFF) {
38
156
    return kLenForm16;
39
156
  }
40
0
  return kLenForm64;
41
156
}
42
43
/// Append 'value' as big-endian bytes of width 'width' to 'out'.
44
431
void AppendBigEndian(std::string* out, std::uint64_t value, std::size_t width) {
45
2.07k
  for (std::size_t i = 0; i < width; ++i) {
46
1.64k
    std::size_t shift = (width - 1 - i) * 8;
47
1.64k
    out->push_back(static_cast<char>((value >> shift) & 0xFF));
48
1.64k
  }
49
431
}
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.69k
std::string SerializeWebSocketFrame(const curl::fuzzer::proto::WebSocketFrame& frame) {
60
6.69k
  std::string out;
61
6.69k
  const std::string& payload = frame.payload();
62
6.69k
  const std::size_t payload_len = payload.size();
63
64
  // Byte 0: FIN | RSV1 | RSV2 | RSV3 | opcode(4 bits).
65
6.69k
  std::uint8_t byte0 = 0;
66
6.69k
  if (frame.fin()) byte0 |= 0x80;
67
6.69k
  if (frame.rsv1()) byte0 |= 0x40;
68
6.69k
  if (frame.rsv2()) byte0 |= 0x20;
69
6.69k
  if (frame.rsv3()) byte0 |= 0x10;
70
6.69k
  byte0 |= static_cast<std::uint8_t>(frame.opcode() & 0x0F);
71
6.69k
  out.push_back(static_cast<char>(byte0));
72
73
  // Byte 1: MASK bit | payload-length indicator (7 bits).
74
6.69k
  const std::uint32_t length_form = ResolveLengthForm(frame.length_form(), payload_len);
75
6.69k
  std::uint8_t byte1 = frame.masked() ? 0x80 : 0x00;
76
6.69k
  switch (length_form) {
77
6.26k
    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.26k
      byte1 |= static_cast<std::uint8_t>(payload_len & 0x7F);
83
6.26k
      out.push_back(static_cast<char>(byte1));
84
6.26k
      break;
85
300
    case kLenForm16:
86
300
      byte1 |= 126;
87
300
      out.push_back(static_cast<char>(byte1));
88
300
      AppendBigEndian(&out, static_cast<std::uint64_t>(payload_len & 0xFFFF), 2);
89
300
      break;
90
131
    case kLenForm64:
91
131
    default:
92
131
      byte1 |= 127;
93
131
      out.push_back(static_cast<char>(byte1));
94
131
      AppendBigEndian(&out, static_cast<std::uint64_t>(payload_len), 8);
95
131
      break;
96
6.69k
  }
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.69k
  if (frame.masked()) {
103
1.93k
    const std::uint32_t key = frame.mask_key();
104
1.93k
    std::uint8_t key_bytes[4] = {
105
1.93k
        static_cast<std::uint8_t>((key >> 24) & 0xFF),
106
1.93k
        static_cast<std::uint8_t>((key >> 16) & 0xFF),
107
1.93k
        static_cast<std::uint8_t>((key >> 8) & 0xFF),
108
1.93k
        static_cast<std::uint8_t>(key & 0xFF),
109
1.93k
    };
110
7.75k
    for (unsigned char b : key_bytes) {
111
7.75k
      out.push_back(static_cast<char>(b));
112
7.75k
    }
113
1.93k
    out.reserve(out.size() + payload_len);
114
88.0k
    for (std::size_t i = 0; i < payload_len; ++i) {
115
86.0k
      out.push_back(static_cast<char>(static_cast<std::uint8_t>(payload[i]) ^ key_bytes[i & 0x3]));
116
86.0k
    }
117
4.75k
  } else {
118
4.75k
    out.append(payload);
119
4.75k
  }
120
121
6.69k
  return out;
122
6.69k
}
123
124
}  // namespace proto_fuzzer