Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 18 additions & 19 deletions src/pacparser.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
}

Expand All @@ -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);
}

Expand All @@ -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;
}

Expand Down
2 changes: 2 additions & 0 deletions src/pymod/pacparser/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
5 changes: 4 additions & 1 deletion src/pymod/pacparser_py.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
20 changes: 20 additions & 0 deletions tests/runtests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.')


Expand Down
18 changes: 18 additions & 0 deletions tests/runtests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Loading