Coverage Report

Created: 2025-10-10 06:31

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/PROJ/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
25
#include "curl_setup.h"
26
27
#ifndef CURL_DISABLE_PROXY
28
29
#include <curl/curl.h>  /* for curl_strnequal() */
30
#include "curlx/inet_pton.h"
31
#include "noproxy.h"
32
#include "curlx/strparse.h"
33
34
#ifdef HAVE_NETINET_IN_H
35
#include <netinet/in.h>
36
#endif
37
38
#ifdef HAVE_ARPA_INET_H
39
#include <arpa/inet.h>
40
#endif
41
42
/*
43
 * Curl_cidr4_match() returns TRUE if the given IPv4 address is within the
44
 * specified CIDR address range.
45
 */
46
UNITTEST bool Curl_cidr4_match(const char *ipv4,    /* 1.2.3.4 address */
47
                               const char *network, /* 1.2.3.4 address */
48
                               unsigned int bits)
49
0
{
50
0
  unsigned int address = 0;
51
0
  unsigned int check = 0;
52
53
0
  if(bits > 32)
54
    /* strange input */
55
0
    return FALSE;
56
57
0
  if(curlx_inet_pton(AF_INET, ipv4, &address) != 1)
58
0
    return FALSE;
59
0
  if(curlx_inet_pton(AF_INET, network, &check) != 1)
60
0
    return FALSE;
61
62
0
  if(bits && (bits != 32)) {
63
0
    unsigned int mask = 0xffffffff << (32 - bits);
64
0
    unsigned int haddr = htonl(address);
65
0
    unsigned int hcheck = htonl(check);
66
#if 0
67
    curl_mfprintf(stderr, "Host %s (%x) network %s (%x) "
68
                  "bits %u mask %x => %x\n",
69
                  ipv4, haddr, network, hcheck, bits, mask,
70
                  (haddr ^ hcheck) & mask);
71
#endif
72
0
    if((haddr ^ hcheck) & mask)
73
0
      return FALSE;
74
0
    return TRUE;
75
0
  }
76
0
  return address == check;
77
0
}
78
79
UNITTEST bool Curl_cidr6_match(const char *ipv6,
80
                               const char *network,
81
                               unsigned int bits)
82
0
{
83
0
#ifdef USE_IPV6
84
0
  unsigned int bytes;
85
0
  unsigned int rest;
86
0
  unsigned char address[16];
87
0
  unsigned char check[16];
88
89
0
  if(!bits)
90
0
    bits = 128;
91
92
0
  bytes = bits / 8;
93
0
  rest = bits & 0x07;
94
0
  if((bytes > 16) || ((bytes == 16) && rest))
95
0
    return FALSE;
96
0
  if(curlx_inet_pton(AF_INET6, ipv6, address) != 1)
97
0
    return FALSE;
98
0
  if(curlx_inet_pton(AF_INET6, network, check) != 1)
99
0
    return FALSE;
100
0
  if(bytes && memcmp(address, check, bytes))
101
0
    return FALSE;
102
0
  if(rest && ((address[bytes] ^ check[bytes]) & (0xff << (8 - rest))))
103
0
    return FALSE;
104
105
0
  return TRUE;
106
#else
107
  (void)ipv6;
108
  (void)network;
109
  (void)bits;
110
  return FALSE;
111
#endif
112
0
}
113
114
enum nametype {
115
  TYPE_HOST,
116
  TYPE_IPV4,
117
  TYPE_IPV6
118
};
119
120
/****************************************************************
121
* Checks if the host is in the noproxy list. returns TRUE if it matches and
122
* therefore the proxy should NOT be used.
123
****************************************************************/
124
bool Curl_check_noproxy(const char *name, const char *no_proxy)
125
0
{
126
0
  char hostip[128];
127
128
  /*
129
   * If we do not have a hostname at all, like for example with a FILE
130
   * transfer, we have nothing to interrogate the noproxy list with.
131
   */
132
0
  if(!name || name[0] == '\0')
133
0
    return FALSE;
134
135
  /* no_proxy=domain1.dom,host.domain2.dom
136
   *   (a comma-separated list of hosts which should
137
   *   not be proxied, or an asterisk to override
138
   *   all proxy variables)
139
   */
140
0
  if(no_proxy && no_proxy[0]) {
141
0
    const char *p = no_proxy;
142
0
    size_t namelen;
143
0
    enum nametype type = TYPE_HOST;
144
0
    if(!strcmp("*", no_proxy))
145
0
      return TRUE;
146
147
    /* NO_PROXY was specified and it was not just an asterisk */
148
149
0
    if(name[0] == '[') {
150
0
      char *endptr;
151
      /* IPv6 numerical address */
152
0
      endptr = strchr(name, ']');
153
0
      if(!endptr)
154
0
        return FALSE;
155
0
      name++;
156
0
      namelen = endptr - name;
157
0
      if(namelen >= sizeof(hostip))
158
0
        return FALSE;
159
0
      memcpy(hostip, name, namelen);
160
0
      hostip[namelen] = 0;
161
0
      name = hostip;
162
0
      type = TYPE_IPV6;
163
0
    }
164
0
    else {
165
0
      unsigned int address;
166
0
      namelen = strlen(name);
167
0
      if(curlx_inet_pton(AF_INET, name, &address) == 1)
168
0
        type = TYPE_IPV4;
169
0
      else {
170
        /* ignore trailing dots in the hostname */
171
0
        if(name[namelen - 1] == '.')
172
0
          namelen--;
173
0
      }
174
0
    }
175
176
0
    while(*p) {
177
0
      const char *token;
178
0
      size_t tokenlen = 0;
179
0
      bool match = FALSE;
180
181
      /* pass blanks */
182
0
      curlx_str_passblanks(&p);
183
184
0
      token = p;
185
      /* pass over the pattern */
186
0
      while(*p && !ISBLANK(*p) && (*p != ',')) {
187
0
        p++;
188
0
        tokenlen++;
189
0
      }
190
191
0
      if(tokenlen) {
192
0
        switch(type) {
193
0
        case TYPE_HOST:
194
          /* ignore trailing dots in the token to check */
195
0
          if(token[tokenlen - 1] == '.')
196
0
            tokenlen--;
197
198
0
          if(tokenlen && (*token == '.')) {
199
            /* ignore leading token dot as well */
200
0
            token++;
201
0
            tokenlen--;
202
0
          }
203
          /* A: example.com matches 'example.com'
204
             B: www.example.com matches 'example.com'
205
             C: nonexample.com DOES NOT match 'example.com'
206
          */
207
0
          if(tokenlen == namelen)
208
            /* case A, exact match */
209
0
            match = curl_strnequal(token, name, namelen);
210
0
          else if(tokenlen < namelen) {
211
            /* case B, tailmatch domain */
212
0
            match = (name[namelen - tokenlen - 1] == '.') &&
213
0
              curl_strnequal(token, name + (namelen - tokenlen),
214
0
                             tokenlen);
215
0
          }
216
          /* case C passes through, not a match */
217
0
          break;
218
0
        case TYPE_IPV4:
219
0
        case TYPE_IPV6: {
220
0
          const char *check = token;
221
0
          char *slash;
222
0
          unsigned int bits = 0;
223
0
          char checkip[128];
224
0
          if(tokenlen >= sizeof(checkip))
225
            /* this cannot match */
226
0
            break;
227
          /* copy the check name to a temp buffer */
228
0
          memcpy(checkip, check, tokenlen);
229
0
          checkip[tokenlen] = 0;
230
0
          check = checkip;
231
232
0
          slash = strchr(check, '/');
233
          /* if the slash is part of this token, use it */
234
0
          if(slash) {
235
            /* if the bits variable gets a crazy value here, that is fine as
236
               the value will then be rejected in the cidr function */
237
0
            bits = (unsigned int)atoi(slash + 1);
238
0
            *slash = 0; /* null-terminate there */
239
0
          }
240
0
          if(type == TYPE_IPV6)
241
0
            match = Curl_cidr6_match(name, check, bits);
242
0
          else
243
0
            match = Curl_cidr4_match(name, check, bits);
244
0
          break;
245
0
        }
246
0
        }
247
0
        if(match)
248
0
          return TRUE;
249
0
      } /* if(tokenlen) */
250
      /* pass blanks after pattern */
251
0
      curlx_str_passblanks(&p);
252
      /* if not a comma, this ends the loop */
253
0
      if(*p != ',')
254
0
        break;
255
      /* pass any number of commas */
256
0
      while(*p == ',')
257
0
        p++;
258
0
    } /* while(*p) */
259
0
  } /* NO_PROXY was specified and it was not just an asterisk */
260
261
0
  return FALSE;
262
0
}
263
264
#endif /* CURL_DISABLE_PROXY */