noproxy: support proxies specified using cidr notation

For both IPv4 and IPv6 addresses. Now also checks IPv6 addresses "correctly"
and not with string comparisons.

Split out the noproxy checks and functionality into noproxy.c

Added unit test 1614 to verify checking functions.

Reported-by: Mathieu Carbonneaux

Fixes #9773
Fixes #5745
Closes #9775
This commit is contained in:
Daniel Stenberg 2022-10-20 15:21:12 +02:00
parent b15ca64bb0
commit 1e9a538e05
No known key found for this signature in database
GPG key ID: 5CC908FDB71E12C2
10 changed files with 424 additions and 93 deletions

View file

@ -34,7 +34,7 @@ UNITPROGS = unit1300 unit1301 unit1302 unit1303 unit1304 unit1305 unit1307 \
unit1330 unit1394 unit1395 unit1396 unit1397 unit1398 \
unit1399 \
unit1600 unit1601 unit1602 unit1603 unit1604 unit1605 unit1606 unit1607 \
unit1608 unit1609 unit1610 unit1611 unit1612 \
unit1608 unit1609 unit1610 unit1611 unit1612 unit1614 \
unit1620 unit1621 \
unit1650 unit1651 unit1652 unit1653 unit1654 unit1655 \
unit1660 unit1661
@ -132,6 +132,9 @@ unit1611_CPPFLAGS = $(AM_CPPFLAGS)
unit1612_SOURCES = unit1612.c $(UNITFILES)
unit1612_CPPFLAGS = $(AM_CPPFLAGS)
unit1614_SOURCES = unit1614.c $(UNITFILES)
unit1614_CPPFLAGS = $(AM_CPPFLAGS)
unit1620_SOURCES = unit1620.c $(UNITFILES)
unit1620_CPPFLAGS = $(AM_CPPFLAGS)

133
tests/unit/unit1614.c Normal file
View file

@ -0,0 +1,133 @@
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) 1998 - 2022, Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
#include "curlcheck.h"
#include "noproxy.h"
static CURLcode unit_setup(void)
{
return CURLE_OK;
}
static void unit_stop(void)
{
}
struct check {
const char *a;
const char *n;
unsigned int bits;
bool match;
};
struct noproxy {
const char *a;
const char *n;
bool match;
};
UNITTEST_START
#ifdef DEBUGBUILD
{
int i;
int err = 0;
struct check list4[]= {
{ "192.160.0.1", "192.160.0.1", 33, FALSE},
{ "192.160.0.1", "192.160.0.1", 32, TRUE},
{ "192.160.0.1", "192.160.0.1", 0, TRUE},
{ "192.160.0.1", "192.160.0.1", 24, TRUE},
{ "192.160.0.1", "192.160.0.1", 26, TRUE},
{ "192.160.0.1", "192.160.0.1", 20, TRUE},
{ "192.160.0.1", "192.160.0.1", 18, TRUE},
{ "192.160.0.1", "192.160.0.1", 12, TRUE},
{ "192.160.0.1", "192.160.0.1", 8, TRUE},
{ "192.160.0.1", "10.0.0.1", 8, FALSE},
{ "192.160.0.1", "10.0.0.1", 32, FALSE},
{ "192.160.0.1", "10.0.0.1", 0, FALSE},
{ NULL, NULL, 0, FALSE} /* end marker */
};
struct check list6[]= {
{ "::1", "::1", 0, TRUE},
{ "::1", "::1", 128, TRUE},
{ "::1", "0:0::1", 128, TRUE},
{ "::1", "0:0::1", 129, FALSE},
{ "fe80::ab47:4396:55c9:8474", "fe80::ab47:4396:55c9:8474", 64, TRUE},
{ NULL, NULL, 0, FALSE} /* end marker */
};
struct noproxy list[]= {
{ "foobar", "barfoo", FALSE},
{ "foobar", "foobar", TRUE},
{ "192.168.0.1", "foobar", FALSE},
{ "192.168.0.1", "192.168.0.0/16", TRUE},
{ "192.168.0.1", "192.168.0.0/24", TRUE},
{ "192.168.0.1", "192.168.0.0/32", FALSE},
{ "192.168.0.1", "192.168.0.0", FALSE},
{ "192.168.1.1", "192.168.0.0/24", FALSE},
{ "192.168.1.1", "foo, bar, 192.168.0.0/24", FALSE},
{ "192.168.1.1", "foo, bar, 192.168.0.0/16", TRUE},
{ "[::1]", "foo, bar, 192.168.0.0/16", FALSE},
{ "[::1]", "foo, bar, ::1/64", TRUE},
{ "bar", "foo, bar, ::1/64", TRUE},
{ "BAr", "foo, bar, ::1/64", TRUE},
{ "BAr", "foo,,,,, bar, ::1/64", TRUE},
{ "www.example.com", "foo, .example.com", TRUE},
{ "www.example.com", "www2.example.com, .example.net", FALSE},
{ "example.com", ".example.com, .example.net", TRUE},
{ "nonexample.com", ".example.com, .example.net", FALSE},
{ NULL, NULL, FALSE}
};
for(i = 0; list4[i].a; i++) {
bool match = Curl_cidr4_match(list4[i].a, list4[i].n, list4[i].bits);
if(match != list4[i].match) {
fprintf(stderr, "%s in %s/%u should %smatch\n",
list4[i].a, list4[i].n, list4[i].bits,
list4[i].match ? "": "not ");
err++;
}
}
for(i = 0; list6[i].a; i++) {
bool match = Curl_cidr6_match(list6[i].a, list6[i].n, list6[i].bits);
if(match != list6[i].match) {
fprintf(stderr, "%s in %s/%u should %smatch\n",
list6[i].a, list6[i].n, list6[i].bits,
list6[i].match ? "": "not ");
err++;
}
}
for(i = 0; list[i].a; i++) {
bool match = Curl_check_noproxy(list[i].a, list[i].n);
if(match != list[i].match) {
fprintf(stderr, "%s in %s should %smatch\n",
list[i].a, list[i].n,
list[i].match ? "": "not ");
err++;
}
}
return err;
}
#else
return 0;
#endif
UNITTEST_STOP