Skip to content

Commit e431945

Browse files
committed
backport ipaddress leading zeros in IPv4 address fix from cpython
1 parent 90ef0cc commit e431945

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

graalpython/lib-python/3/ipaddress.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1173,6 +1173,11 @@ def _parse_octet(cls, octet_str):
11731173
if len(octet_str) > 3:
11741174
msg = "At most 3 characters permitted in %r"
11751175
raise ValueError(msg % octet_str)
1176+
# Handle leading zeros as strict as glibc's inet_pton()
1177+
# See security bug bpo-36384
1178+
if octet_str != '0' and octet_str[0] == '0':
1179+
msg = "Leading zeros are not permitted in %r"
1180+
raise ValueError(msg % octet_str)
11761181
# Convert to integer (we know digits are legal)
11771182
octet_int = int(octet_str, 10)
11781183
if octet_int > 255:

graalpython/lib-python/3/test/test_ipaddress.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,23 @@ def pickle_test(self, addr):
9797
class CommonTestMixin_v4(CommonTestMixin):
9898

9999
def test_leading_zeros(self):
100-
self.assertInstancesEqual("000.000.000.000", "0.0.0.0")
101-
self.assertInstancesEqual("192.168.000.001", "192.168.0.1")
102-
self.assertInstancesEqual("016.016.016.016", "16.16.16.16")
103-
self.assertInstancesEqual("001.000.008.016", "1.0.8.16")
100+
# bpo-36384: no leading zeros to avoid ambiguity with octal notation
101+
msg = "Leading zeros are not permitted in '\d+'"
102+
addresses = [
103+
"000.000.000.000",
104+
"192.168.000.001",
105+
"016.016.016.016",
106+
"192.168.000.001",
107+
"001.000.008.016",
108+
"01.2.3.40",
109+
"1.02.3.40",
110+
"1.2.03.40",
111+
"1.2.3.040",
112+
]
113+
for address in addresses:
114+
with self.subTest(address=address):
115+
with self.assertAddressError(msg):
116+
self.factory(address)
104117

105118
def test_int(self):
106119
self.assertInstancesEqual(0, "0.0.0.0")

0 commit comments

Comments
 (0)