Coverage Report

Created: 2026-12-01 06:33

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/curl_fuzzer/proto_fuzzer/socks4_mock_server.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 SOCKS/HAProxy option policy layered over the stream mock.
9
10
#include "proto_fuzzer/socks4_mock_server.h"
11
12
#include <curl/curl.h>
13
14
#include <algorithm>
15
#include <cstddef>
16
#include <memory>
17
18
namespace proto_fuzzer {
19
20
namespace {
21
22
15.6k
long ProxyType(curl::fuzzer::proto::SocksProxyMode mode) {
23
15.6k
  switch (mode) {
24
805
    case curl::fuzzer::proto::SOCKS_PROXY_SOCKS4A:
25
805
      return CURLPROXY_SOCKS4A;
26
225
    case curl::fuzzer::proto::SOCKS_PROXY_SOCKS5:
27
225
      return CURLPROXY_SOCKS5;
28
91
    case curl::fuzzer::proto::SOCKS_PROXY_SOCKS5_HOSTNAME:
29
91
      return CURLPROXY_SOCKS5_HOSTNAME;
30
14.5k
    case curl::fuzzer::proto::SOCKS_PROXY_SOCKS4:
31
14.5k
    default:
32
14.5k
      return CURLPROXY_SOCKS4;
33
15.6k
  }
34
15.6k
}
35
36
15.6k
const char* ProxyUrl(curl::fuzzer::proto::SocksProxyMode mode) {
37
15.6k
  switch (mode) {
38
805
    case curl::fuzzer::proto::SOCKS_PROXY_SOCKS4A:
39
805
      return "socks4a://127.0.0.1:1080";
40
225
    case curl::fuzzer::proto::SOCKS_PROXY_SOCKS5:
41
225
      return "socks5://127.0.0.1:1080";
42
91
    case curl::fuzzer::proto::SOCKS_PROXY_SOCKS5_HOSTNAME:
43
91
      return "socks5h://127.0.0.1:1080";
44
14.5k
    case curl::fuzzer::proto::SOCKS_PROXY_SOCKS4:
45
14.5k
    default:
46
14.5k
      return "socks4://127.0.0.1:1080";
47
15.6k
  }
48
15.6k
}
49
50
}  // namespace
51
52
/// Select a fixed SOCKS wire version, resolution side, and optional HAProxy
53
/// prefix. Script bytes still decide which advertised SOCKS5 authentication
54
/// method the peer selects.
55
/// @param mode Proxy protocol and hostname-resolution mode to install.
56
/// @param haproxy_protocol Whether to send a fixed protocol-v1 prefix after
57
///        SOCKS tunnel establishment.
58
ProxyTransportMockServer::ProxyTransportMockServer(curl::fuzzer::proto::SocksProxyMode mode, bool haproxy_protocol)
59
15.6k
    : mode_(mode), haproxy_protocol_(haproxy_protocol) {}
60
61
/// Install common socket callbacks and the fixed SOCKS proxy policy.
62
/// @param easy Easy handle to configure.
63
15.6k
void ProxyTransportMockServer::Install(CURL* easy) {
64
15.6k
  MockServer::Install(easy);
65
15.6k
  client_transcript_.clear();
66
67
  // CONNECT_TO applies to the origin and would replace the hostname that the
68
  // SOCKS request is meant to exercise. The numeric proxy cannot require DNS,
69
  // while OPENSOCKETFUNCTION still replaces its outbound socket with the
70
  // in-process peer.
71
15.6k
  static constexpr char kProxyUser[] = "fuzz-user";
72
15.6k
  static constexpr char kProxyPassword[] = "fuzz-pass";
73
15.6k
  static constexpr char kHaproxyClientIp[] = "127.0.0.1";
74
15.6k
  (void)curl_easy_setopt(easy, CURLOPT_CONNECT_TO, nullptr);
75
15.6k
  (void)curl_easy_setopt(easy, CURLOPT_PROXY, ProxyUrl(mode_));
76
15.6k
  (void)curl_easy_setopt(easy, CURLOPT_PROXYTYPE, ProxyType(mode_));
77
15.6k
  (void)curl_easy_setopt(easy, CURLOPT_NOPROXY, "");
78
15.6k
  (void)curl_easy_setopt(easy, CURLOPT_PROXYUSERNAME, kProxyUser);
79
15.6k
  (void)curl_easy_setopt(easy, CURLOPT_PROXYPASSWORD, kProxyPassword);
80
  // Advertise no-auth plus username/password only. This keeps the client hello
81
  // deterministic while letting the scripted method-selection response enter
82
  // either SOCKS5 branch.
83
15.6k
  (void)curl_easy_setopt(easy, CURLOPT_SOCKS5_AUTH, static_cast<long>(CURLAUTH_BASIC));
84
  // The locally resolved SOCKS5 mode must be deterministic across hosts that
85
  // expose both ::1 and 127.0.0.1 for localhost.
86
15.6k
  (void)curl_easy_setopt(easy, CURLOPT_IPRESOLVE, static_cast<long>(CURL_IPRESOLVE_V4));
87
15.6k
  (void)curl_easy_setopt(easy, CURLOPT_HAPROXY_CLIENT_IP, haproxy_protocol_ ? kHaproxyClientIp : nullptr);
88
15.6k
  (void)curl_easy_setopt(easy, CURLOPT_HAPROXYPROTOCOL, haproxy_protocol_ ? 1L : 0L);
89
15.6k
}
90
91
/// @return Bounded client bytes observed during the latest transfer.
92
15.6k
const std::string& ProxyTransportMockServer::client_transcript() const { return client_transcript_; }
93
94
/// Create one socketpair peer and attach the raw-byte transcript observer.
95
/// @return A fresh in-process stream connection.
96
15.2k
std::unique_ptr<MockConnection> ProxyTransportMockServer::CreateConnection() {
97
15.2k
  auto connection = std::make_unique<MockConnection>();
98
15.2k
  connection->SetIncomingDataObserver(this);
99
15.2k
  return connection;
100
15.2k
}
101
102
/// @return True because the proxy peer services each bounded perform turn.
103
859
bool ProxyTransportMockServer::SupportsRepeatedPollsetProbe() const { return true; }
104
105
/// Retain a bounded prefix of client bytes for strict integration tests.
106
/// @param data First byte in this drained fragment.
107
/// @param size Number of bytes available at `data`.
108
26.7k
void ProxyTransportMockServer::ObserveIncomingData(const unsigned char* data, std::size_t size) {
109
26.7k
  if (data == nullptr || size == 0 || client_transcript_.size() >= kMaxTranscriptBytes) {
110
33
    return;
111
33
  }
112
26.6k
  const std::size_t remaining = kMaxTranscriptBytes - client_transcript_.size();
113
26.6k
  client_transcript_.append(reinterpret_cast<const char*>(data), std::min(size, remaining));
114
26.6k
}
115
116
/// Retain protocol outcomes in coverage and introspector builds.
117
15.6k
void ProxyTransportMockServer::ObserveState() {
118
15.6k
#if defined(PROTO_FUZZER_OBSERVE_STATE)
119
15.6k
  MockServer::ObserveState();
120
15.6k
  const volatile std::size_t proxy_observation = client_transcript().size();
121
15.6k
  (void)proxy_observation;
122
15.6k
#endif
123
15.6k
}
124
125
}  // namespace proto_fuzzer