Skip to content

Commit 60ce8f0

Browse files
tiranambv
andauthored
bpo-36384: Leading zeros in IPv4 addresses are no longer tolerated (GH-25099)
Reverts commit e653d4d and makes parsing even more strict. Like socket.inet_pton() any leading zero is now treated as invalid input. Signed-off-by: Christian Heimes <christian@python.org> Co-authored-by: Łukasz Langa <lukasz@langa.pl>
1 parent 73766b0 commit 60ce8f0

6 files changed

Lines changed: 59 additions & 8 deletions

File tree

Doc/library/ipaddress.rst

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,7 @@ write code that handles both IP versions correctly. Address objects are
104104
1. A string in decimal-dot notation, consisting of four decimal integers in
105105
the inclusive range 0--255, separated by dots (e.g. ``192.168.0.1``). Each
106106
integer represents an octet (byte) in the address. Leading zeroes are
107-
tolerated only for values less than 8 (as there is no ambiguity
108-
between the decimal and octal interpretations of such strings).
107+
not tolerated to prevent confusion with octal notation.
109108
2. An integer that fits into 32 bits.
110109
3. An integer packed into a :class:`bytes` object of length 4 (most
111110
significant octet first).
@@ -117,6 +116,22 @@ write code that handles both IP versions correctly. Address objects are
117116
>>> ipaddress.IPv4Address(b'\xC0\xA8\x00\x01')
118117
IPv4Address('192.168.0.1')
119118

119+
.. versionchanged:: 3.8
120+
121+
Leading zeros are tolerated, even in ambiguous cases that look like
122+
octal notation.
123+
124+
.. versionchanged:: 3.10
125+
126+
Leading zeros are no longer tolerated and are treated as an error.
127+
IPv4 address strings are now parsed as strict as glibc
128+
:func:`~socket.inet_pton`.
129+
130+
.. versionchanged:: 3.9.5
131+
132+
The above change was also included in Python 3.9 starting with
133+
version 3.9.5.
134+
120135
.. attribute:: version
121136

122137
The appropriate version number: ``4`` for IPv4, ``6`` for IPv6.

Doc/tools/susp-ignored.csv

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,8 @@ library/ipaddress,,:db8,>>> ipaddress.IPv6Address('2001:db8::1000')
149149
library/ipaddress,,::,>>> ipaddress.IPv6Address('2001:db8::1000')
150150
library/ipaddress,,:db8,'2001:db8::1000'
151151
library/ipaddress,,::,'2001:db8::1000'
152-
library/ipaddress,231,:db8,">>> f'{ipaddress.IPv6Address(""2001:db8::1000""):s}'"
153-
library/ipaddress,231,::,">>> f'{ipaddress.IPv6Address(""2001:db8::1000""):s}'"
152+
library/ipaddress,,:db8,">>> f'{ipaddress.IPv6Address(""2001:db8::1000""):s}'"
153+
library/ipaddress,,::,">>> f'{ipaddress.IPv6Address(""2001:db8::1000""):s}'"
154154
library/ipaddress,,::,IPv6Address('ff02::5678%1')
155155
library/ipaddress,,::,fe80::1234
156156
library/ipaddress,,:db8,">>> ipaddress.ip_address(""2001:db8::1"").reverse_pointer"

Doc/whatsnew/3.9.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,10 @@ Scoped IPv6 addresses can be parsed using :class:`ipaddress.IPv6Address`.
537537
If present, scope zone ID is available through the :attr:`~ipaddress.IPv6Address.scope_id` attribute.
538538
(Contributed by Oleksandr Pavliuk in :issue:`34788`.)
539539

540+
Starting with Python 3.9.5 the :mod:`ipaddress` module no longer
541+
accepts any leading zeros in IPv4 address strings.
542+
(Contributed by Christian Heimes in :issue:`36384`).
543+
540544
math
541545
----
542546

@@ -1114,6 +1118,14 @@ Changes in the Python API
11141118
compatible classes that don't inherit from those mentioned types.
11151119
(Contributed by Roger Aiudi in :issue:`34775`).
11161120

1121+
* Starting with Python 3.9.5 the :mod:`ipaddress` module no longer
1122+
accepts any leading zeros in IPv4 address strings. Leading zeros are
1123+
ambiguous and interpreted as octal notation by some libraries. For example
1124+
the legacy function :func:`socket.inet_aton` treats leading zeros as octal
1125+
notatation. glibc implementation of modern :func:`~socket.inet_pton` does
1126+
not accept any leading zeros.
1127+
(Contributed by Christian Heimes in :issue:`36384`).
1128+
11171129
* :func:`codecs.lookup` now normalizes the encoding name the same way as
11181130
:func:`encodings.normalize_encoding`, except that :func:`codecs.lookup` also
11191131
converts the name to lower case. For example, ``"latex+latin1"`` encoding

Lib/ipaddress.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1223,6 +1223,11 @@ def _parse_octet(cls, octet_str):
12231223
if len(octet_str) > 3:
12241224
msg = "At most 3 characters permitted in %r"
12251225
raise ValueError(msg % octet_str)
1226+
# Handle leading zeros as strict as glibc's inet_pton()
1227+
# See security bug bpo-36384
1228+
if octet_str != '0' and octet_str[0] == '0':
1229+
msg = "Leading zeros are not permitted in %r"
1230+
raise ValueError(msg % octet_str)
12261231
# Convert to integer (we know digits are legal)
12271232
octet_int = int(octet_str, 10)
12281233
if octet_int > 255:

Lib/test/test_ipaddress.py

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

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

104117
def test_int(self):
105118
self.assertInstancesEqual(0, "0.0.0.0")
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
:mod:`ipaddress` module no longer accepts any leading zeros in IPv4 address
2+
strings. Leading zeros are ambiguous and interpreted as octal notation by
3+
some libraries. For example the legacy function :func:`socket.inet_aton`
4+
treats leading zeros as octal notatation. glibc implementation of modern
5+
:func:`~socket.inet_pton` does not accept any leading zeros. For a while
6+
the :mod:`ipaddress` module used to accept ambiguous leading zeros.

0 commit comments

Comments
 (0)