Coverage Report

Created: 2026-03-11 07:11

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/curl/lib/noproxy.c
Line
Count
Source
1
/***************************************************************************
2
 *                                  _   _ ____  _
3
 *  Project                     ___| | | |  _ \| |
4
 *                             / __| | | | |_) | |
5
 *                            | (__| |_| |  _ <| |___
6
 *                             \___|\___/|_| \_\_____|
7
 *
8
 * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
9
 *
10
 * This software is licensed as described in the file COPYING, which
11
 * you should have received as part of this distribution. The terms
12
 * are also available at https://curl.se/docs/copyright.html.
13
 *
14
 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15
 * copies of the Software, and permit persons to whom the Software is
16
 * furnished to do so, under the terms of the COPYING file.
17
 *
18
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19
 * KIND, either express or implied.
20
 *
21
 * SPDX-License-Identifier: curl
22
 *
23
 ***************************************************************************/
24
#include "curl_setup.h"
25
26
#ifndef CURL_DISABLE_PROXY
27
28
#include "curlx/inet_pton.h"
29
#include "noproxy.h"
30
#include "curlx/strparse.h"
31
32
#ifdef HAVE_NETINET_IN_H
33
#include <netinet/in.h>
34
#endif
35
36
#ifdef HAVE_ARPA_INET_H
37
#include <arpa/inet.h>
38
#endif
39
40
/*
41
 * Curl_cidr4_match() returns TRUE if the given IPv4 address is within the
42
 * specified CIDR address range.
43
 */
44
UNITTEST bool Curl_cidr4_match(const char *ipv4,    /* 1.2.3.4 address */
45
                               const char *network, /* 1.2.3.4 address */
46
                               unsigned int bits)
47
0
{
48
0
  unsigned int address = 0;
49
0
  unsigned int check = 0;
50
51
0
  if(bits > 32)
52
    /* strange input */
53
0
    return FALSE;
54
55
0
  if(curlx_inet_pton(AF_INET, ipv4, &address) != 1)
56
0
    return FALSE;
57
0
  if(curlx_inet_pton(AF_INET, network, &check) != 1)
58
0
    return FALSE;
59
60
0
  if(bits && (bits != 32)) {
61
0
    unsigned int mask = 0xffffffff << (32 - bits);
62
0
    unsigned int haddr = htonl(address);
63
0
    unsigned int hcheck = htonl(check);
64
#if 0
65
    curl_mfprintf(stderr, "Host %s (%x) network %s (%x) "
66
                  "bits %u mask %x => %x\n",
67
                  ipv4, haddr, network, hcheck, bits, mask,
68
                  (haddr ^ hcheck) & mask);
69
#endif
70
0
    if((haddr ^ hcheck) & mask)
71
0
      return FALSE;
72
0
    return TRUE;
73
0
  }
74
0
  return address == check;
75
0
}
76
77
UNITTEST bool Curl_cidr6_match(const char *ipv6,
78
                               const char *network,
79
                               unsigned int bits)
80
0
{
81
0
#ifdef USE_IPV6
82
0
  unsigned int bytes;
83
0
  unsigned int rest;
84
0
  unsigned char address[16];
85
0
  unsigned char check[16];
86
87
0
  if(!bits)
88
0
    bits = 128;
89
90
0
  bytes = bits / 8;
91
0
  rest = bits & 0x07;
92
0
  if((bytes > 16) || ((bytes == 16) && rest))
93
0
    return FALSE;
94
0
  if(curlx_inet_pton(AF_INET6, ipv6, address) != 1)
95
0
    return FALSE;
96
0
  if(curlx_inet_pton(AF_INET6, network, check) != 1)
97
0
    return FALSE;
98
0
  if(bytes && memcmp(address, check, bytes))
99
0
    return FALSE;
100
0
  if(rest && ((address[bytes] ^ check[bytes]) & (0xff << (8 - rest))))
101
0
    return FALSE;
102
103
0
  return TRUE;
104
#else
105
  (void)ipv6;
106
  (void)network;
107
  (void)bits;
108
  return FALSE;
109
#endif
110
0
}
111
112
enum nametype {
113
  TYPE_HOST,
114
  TYPE_IPV4,
115
  TYPE_IPV6
116
};
117
118
static bool match_host(const char *token, size_t tokenlen,
119
                       const char *name, size_t namelen)
120
0
{
121
0
  bool match = FALSE;
122
123
  /* ignore trailing dots in the token to check */
124
0
  if(token[tokenlen - 1] == '.')
125
0
    tokenlen--;
126
127
0
  if(tokenlen && (*token == '.')) {
128
    /* ignore leading token dot as well */
129
0
    token++;
130
0
    tokenlen--;
131
0
  }
132
  /* A: example.com matches 'example.com'
133
     B: www.example.com matches 'example.com'
134
     C: nonexample.com DOES NOT match 'example.com'
135
  */
136
0
  if(tokenlen == namelen)
137
    /* case A, exact match */
138
0
    match = curl_strnequal(token, name, namelen);
139
0
  else if(tokenlen < namelen) {
140
    /* case B, tailmatch domain */
141
0
    match = (name[namelen - tokenlen - 1] == '.') &&
142
0
            curl_strnequal(token, name + (namelen - tokenlen), tokenlen);
143
0
  }
144
  /* case C passes through, not a match */
145
0
  return match;
146
0
}
147
148
static bool match_ip(int type, const char *token, size_t tokenlen,
149
                     const char *name)
150
0
{
151
0
  char *slash;
152
0
  unsigned int bits = 0;
153
0
  char checkip[128];
154
0
  if(tokenlen >= sizeof(checkip))
155
    /* this cannot match */
156
0
    return FALSE;
157
  /* copy the check name to a temp buffer */
158
0
  memcpy(checkip, token, tokenlen);
159
0
  checkip[tokenlen] = 0;
160
161
0
  slash = strchr(checkip, '/');
162
  /* if the slash is part of this token, use it */
163
0
  if(slash) {
164
0
    curl_off_t value;
165
0
    const char *p = &slash[1];
166
0
    if(curlx_str_number(&p, &value, 128) || *p)
167
0
      return FALSE;
168
    /* a too large value is rejected in the cidr function below */
169
0
    bits = (unsigned int)value;
170
0
    *slash = 0; /* null-terminate there */
171
0
  }
172
0
  if(type == TYPE_IPV6)
173
0
    return Curl_cidr6_match(name, checkip, bits);
174
0
  else
175
0
    return Curl_cidr4_match(name, checkip, bits);
176
0
}
177
178
/****************************************************************
179
 * Checks if the host is in the noproxy list. returns TRUE if it matches and
180
 * therefore the proxy should NOT be used.
181
 ****************************************************************/
182
bool Curl_check_noproxy(const char *name, const char *no_proxy)
183
0
{
184
  /*
185
   * If we do not have a hostname at all, like for example with a FILE
186
   * transfer, we have nothing to interrogate the noproxy list with.
187
   */
188
0
  if(!name || name[0] == '\0')
189
0
    return FALSE;
190
191
  /* no_proxy=domain1.dom,host.domain2.dom
192
   *   (a comma-separated list of hosts which should
193
   *   not be proxied, or an asterisk to override
194
   *   all proxy variables)
195
   */
196
0
  if(no_proxy && no_proxy[0]) {
197
0
    const char *p = no_proxy;
198
0
    size_t namelen;
199
0
    char address[16];
200
0
    enum nametype type = TYPE_HOST;
201
0
    if(!strcmp("*", no_proxy))
202
0
      return TRUE;
203
204
    /* NO_PROXY was specified and it was not only an asterisk */
205
206
    /* Check if name is an IP address; if not, assume it being a hostname. */
207
0
    namelen = strlen(name);
208
0
    if(curlx_inet_pton(AF_INET, name, &address) == 1)
209
0
      type = TYPE_IPV4;
210
0
#ifdef USE_IPV6
211
0
    else if(curlx_inet_pton(AF_INET6, name, &address) == 1)
212
0
      type = TYPE_IPV6;
213
0
#endif
214
0
    else {
215
      /* ignore trailing dots in the hostname */
216
0
      if(name[namelen - 1] == '.')
217
0
        namelen--;
218
0
    }
219
220
0
    while(*p) {
221
0
      const char *token;
222
0
      size_t tokenlen = 0;
223
224
      /* pass blanks */
225
0
      curlx_str_passblanks(&p);
226
227
0
      token = p;
228
      /* pass over the pattern */
229
0
      while(*p && !ISBLANK(*p) && (*p != ',')) {
230
0
        p++;
231
0
        tokenlen++;
232
0
      }
233
234
0
      if(tokenlen) {
235
0
        bool match = FALSE;
236
0
        if(type == TYPE_HOST)
237
0
          match = match_host(token, tokenlen, name, namelen);
238
0
        else
239
0
          match = match_ip(type, token, tokenlen, name);
240
241
0
        if(match)
242
0
          return TRUE;
243
0
      }
244
245
      /* pass blanks after pattern */
246
0
      curlx_str_passblanks(&p);
247
      /* if not a comma, this ends the loop */
248
0
      if(*p != ',')
249
0
        break;
250
      /* pass any number of commas */
251
0
      while(*p == ',')
252
0
        p++;
253
0
    } /* while(*p) */
254
0
  } /* NO_PROXY was specified and it was not only an asterisk */
255
256
0
  return FALSE;
257
0
}
258
259
#endif /* CURL_DISABLE_PROXY */