Coverage Report

Created: 2026-10-31 06:46

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
14.1k
long ProxyType(curl::fuzzer::proto::SocksProxyMode mode) {
23
14.1k
  switch (mode) {
24
973
    case curl::fuzzer::proto::SOCKS_PROXY_SOCKS4A:
25
973
      return CURLPROXY_SOCKS4A;
26
255
    case curl::fuzzer::proto::SOCKS_PROXY_SOCKS5:
27
255
      return CURLPROXY_SOCKS5;
28
66
    case curl::fuzzer::proto::SOCKS_PROXY_SOCKS5_HOSTNAME:
29
66
      return CURLPROXY_SOCKS5_HOSTNAME;
30
12.8k
    case curl::fuzzer::proto::SOCKS_PROXY_SOCKS4:
31
12.8k
    default:
32
12.8k
      return CURLPROXY_SOCKS4;
33
14.1k
  }
34
14.1k
}
35
36
14.1k
const char* ProxyUrl(curl::fuzzer::proto::SocksProxyMode mode) {
37
14.1k
  switch (mode) {
38
973
    case curl::fuzzer::proto::SOCKS_PROXY_SOCKS4A:
39
973
      return "socks4a://127.0.0.1:1080";
40
255
    case curl::fuzzer::proto::SOCKS_PROXY_SOCKS5:
41
255
      return "socks5://127.0.0.1:1080";
42
66
    case curl::fuzzer::proto::SOCKS_PROXY_SOCKS5_HOSTNAME:
43
66
      return "socks5h://127.0.0.1:1080";
44
12.8k
    case curl::fuzzer::proto::SOCKS_PROXY_SOCKS4:
45
12.8k
    default:
46
12.8k
      return "socks4://127.0.0.1:1080";
47
14.1k
  }
48
14.1k
}
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
14.1k
    : 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
14.1k
void ProxyTransportMockServer::Install(CURL* easy) {
64
14.1k
  MockServer::Install(easy);
65
14.1k
  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
14.1k
  static constexpr char kProxyUser[] = "fuzz-user";
72
14.1k
  static constexpr char kProxyPassword[] = "fuzz-pass";
73
14.1k
  static constexpr char kHaproxyClientIp[] = "127.0.0.1";
74
14.1k
  (void)curl_easy_setopt(easy, CURLOPT_CONNECT_TO, nullptr);
75
14.1k
  (void)curl_easy_setopt(easy, CURLOPT_PROXY, ProxyUrl(mode_));
76
14.1k
  (void)curl_easy_setopt(easy, CURLOPT_PROXYTYPE, ProxyType(mode_));
77
14.1k
  (void)curl_easy_setopt(easy, CURLOPT_NOPROXY, "");
78
14.1k
  (void)curl_easy_setopt(easy, CURLOPT_PROXYUSERNAME, kProxyUser);
79
14.1k
  (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
14.1k
  (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
14.1k
  (void)curl_easy_setopt(easy, CURLOPT_IPRESOLVE, static_cast<long>(CURL_IPRESOLVE_V4));
87
14.1k
  (void)curl_easy_setopt(easy, CURLOPT_HAPROXY_CLIENT_IP, haproxy_protocol_ ? kHaproxyClientIp : nullptr);
88
14.1k
  (void)curl_easy_setopt(easy, CURLOPT_HAPROXYPROTOCOL, haproxy_protocol_ ? 1L : 0L);
89
14.1k
}
90
91
/// @return Bounded client bytes observed during the latest transfer.
92
14.1k
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
13.8k
std::unique_ptr<MockConnection> ProxyTransportMockServer::CreateConnection() {
97
13.8k
  auto connection = std::make_unique<MockConnection>();
98
13.8k
  connection->SetIncomingDataObserver(this);
99
13.8k
  return connection;
100
13.8k
}
101
102
/// @return True because the proxy peer services each bounded perform turn.
103
803
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
15.5k
void ProxyTransportMockServer::ObserveIncomingData(const unsigned char* data, std::size_t size) {
109
15.5k
  if (data == nullptr || size == 0 || client_transcript_.size() >= kMaxTranscriptBytes) {
110
25
    return;
111
25
  }
112
15.5k
  const std::size_t remaining = kMaxTranscriptBytes - client_transcript_.size();
113
15.5k
  client_transcript_.append(reinterpret_cast<const char*>(data), std::min(size, remaining));
114
15.5k
}
115
116
/// Retain protocol outcomes in coverage and introspector builds.
117
14.1k
void ProxyTransportMockServer::ObserveState() {
118
14.1k
#if defined(PROTO_FUZZER_OBSERVE_STATE)
119
14.1k
  MockServer::ObserveState();
120
14.1k
  const volatile std::size_t proxy_observation = client_transcript().size();
121
14.1k
  (void)proxy_observation;
122
14.1k
#endif
123
14.1k
}
124
125
}  // namespace proto_fuzzer