Coverage Report

Created: 2026-07-16 06:34

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