Skip to content

Commit be21254

Browse files
committed
gh-143241: Fix infinite loop DoS in zoneinfo._common.load_data
1 parent 61ee048 commit be21254

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

Lib/test/test_zoneinfo/test_zoneinfo.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,8 @@ def test_bad_zones(self):
252252
bad_zones = [
253253
b"", # Empty file
254254
b"AAAA3" + b" " * 15, # Bad magic
255+
# Truncated V2 file (infinite loop DoS)
256+
b"TZif2" + (b"\x00" * 39) + b"TZif2" + (b"\x00" * 39) + b"\n" + b"Part",
255257
]
256258

257259
for bad_zone in bad_zones:

Lib/zoneinfo/_common.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,10 @@ def get_abbr(idx):
119119
assert c == b"\n", c
120120

121121
tz_bytes = b""
122-
while (c := fobj.read(1)) != b"\n":
123-
tz_bytes += c
122+
line = fobj.readline()
123+
if not line.endswith(b"\n"):
124+
raise ValueError("Invalid TZif file: unexpected end of file")
125+
tz_bytes = line.rstrip(b"\n")
124126

125127
tz_str = tz_bytes
126128
else:

0 commit comments

Comments
 (0)