diff --git a/src/pacparser.c b/src/pacparser.c index 38a6e1d7..da9cc785 100644 --- a/src/pacparser.c +++ b/src/pacparser.c @@ -51,7 +51,10 @@ # define UNUSED(x) UNUSED_ ## x #endif -static char my_ip_buf[INET6_ADDRSTRLEN+1]; +// Client IP set via pacparser_setmyip(). Sized for the longest textual IP +// address (45 characters, an IPv6 address with an embedded IPv4 address) plus +// the terminating NUL. Not INET6_ADDRSTRLEN, which is larger on Windows. +static char my_ip_buf[46]; static int my_ip_set = 0; // Default error printer function. @@ -250,15 +253,13 @@ my_ip(JSContext *ctx, JSValueConst UNUSED(this_val), int UNUSED(argc), JSValueCo char ipaddr[INET6_ADDRSTRLEN]; if (my_ip_set) // If my (client's) IP address is already set. - strcpy(ipaddr, my_ip_buf); - else { - char name[256]; - gethostname(name, sizeof(name)); - if (resolve_host(name, ipaddr, 1, AF_INET)) { - strcpy(ipaddr, "127.0.0.1"); - } - } + return JS_NewString(ctx, my_ip_buf); + char name[256]; + gethostname(name, sizeof(name)); + if (resolve_host(name, ipaddr, 1, AF_INET)) { + strcpy(ipaddr, "127.0.0.1"); + } return JS_NewString(ctx, ipaddr); } @@ -270,15 +271,13 @@ my_ip_ex(JSContext *ctx, JSValueConst UNUSED(this_val), int UNUSED(argc), JSValu char ipaddr[INET6_ADDRSTRLEN * MAX_IP_RESULTS + MAX_IP_RESULTS]; if (my_ip_set) // If my (client's) IP address is already set. - strcpy(ipaddr, my_ip_buf); - else { - char name[256]; - gethostname(name, sizeof(name)); - if (resolve_host(name, ipaddr, MAX_IP_RESULTS, AF_UNSPEC)) { - strcpy(ipaddr, ""); - } - } + return JS_NewString(ctx, my_ip_buf); + char name[256]; + gethostname(name, sizeof(name)); + if (resolve_host(name, ipaddr, MAX_IP_RESULTS, AF_UNSPEC)) { + strcpy(ipaddr, ""); + } return JS_NewString(ctx, ipaddr); } @@ -292,8 +291,8 @@ static const char *proxy_result = NULL; int pacparser_setmyip(const char *ip) { - if (strlen(ip) > INET6_ADDRSTRLEN) { - fprintf(stderr, "pacparser_setmyip: IP too long: %s\n", ip); + if (strlen(ip) >= sizeof(my_ip_buf)) { + print_error("pacparser_setmyip: IP too long: %s\n", ip); return 0; } diff --git a/src/pymod/pacparser/__init__.py b/src/pymod/pacparser/__init__.py index b4a81e1f..739ce11e 100644 --- a/src/pymod/pacparser/__init__.py +++ b/src/pymod/pacparser/__init__.py @@ -114,6 +114,8 @@ def just_find_proxy(pacfile, url, host=None): def setmyip(ip_address): """ Set my ip address. This is the IP address returned by myIpAddress() + + Raises ValueError if ip_address is too long to be an IP address. """ _pacparser.setmyip(ip_address) diff --git a/src/pymod/pacparser_py.c b/src/pymod/pacparser_py.c index e42b1538..9a14ac0b 100644 --- a/src/pymod/pacparser_py.c +++ b/src/pymod/pacparser_py.c @@ -128,7 +128,10 @@ py_pacparser_setmyip(PyObject *self, PyObject *args) const char *ip; if (!PyArg_ParseTuple(args, "s", &ip)) return NULL; - pacparser_setmyip(ip); + if (!pacparser_setmyip(ip)) { + PyErr_Format(PyExc_ValueError, "invalid IP address: %s", ip); + return NULL; + } Py_RETURN_NONE; } diff --git a/tests/runtests.py b/tests/runtests.py index 83b95ec4..5a435463 100644 --- a/tests/runtests.py +++ b/tests/runtests.py @@ -109,6 +109,26 @@ def runtests(pacfile, testdata, tests_dir): raise Exception('Logging test failed: stderr mismatch\nExpected:\n%s\nGot:\n%s' % (expected_stderr, actual_stderr)) + # Client IP length test: the longest textual IP address (45 characters) is + # returned by myIpAddress(); anything longer raises ValueError. + longest_ip = 'ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255' + pacparser.setmyip(longest_ip) + pacparser.init() + pacparser.parse_pac_string( + 'function FindProxyForURL(url, host) { return myIpAddress(); }') + result = pacparser.find_proxy('http://example.com/') + pacparser.cleanup() + if result != longest_ip: + raise Exception('Client IP test failed: got "%s", expected "%s"' % + (result, longest_ip)) + try: + pacparser.setmyip(longest_ip + '0') + except ValueError: + pass + else: + raise Exception('Client IP test failed: a 46-character client IP was ' + 'accepted') + print('All tests were successful.') diff --git a/tests/runtests.sh b/tests/runtests.sh index 63e8aff1..b9afa7e1 100755 --- a/tests/runtests.sh +++ b/tests/runtests.sh @@ -79,4 +79,22 @@ if [ "$actual_stderr" != "$expected_stderr" ]; then exit 1 fi +# Client IP length tests: the longest textual IP address (45 characters) is +# accepted and returned by myIpAddress(); anything longer is rejected. +myip_pac=$(mktemp) +trap 'rm -f "$myip_pac"' EXIT +echo 'function FindProxyForURL(url, host) { return myIpAddress(); }' > "$myip_pac" +longest_ip="ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255" +result=$($pactester -p "$myip_pac" -u http://example.com/ -c $longest_ip) +if [ "$result" != "$longest_ip" ]; then + echo "Client IP test failed: got \"$result\", expected \"$longest_ip\"" + exit 1 +fi +result=$($pactester -p "$myip_pac" -u http://example.com/ -c "${longest_ip}0" 2>&1) +if [ $? = 0 ] || [ "${result/IP too long/}" = "$result" ]; then + echo "Client IP test failed: a 46-character client IP was not rejected" + echo "$result" + exit 1 +fi + echo "All tests were successful."