Skip to content

Commit ab08ff7

Browse files
bpo-42663: Fix parsing TZ strings in zoneinfo module (GH-23825)
zipinfo now supports the full range of values in the TZ string determined by RFC 8536 and detects all invalid formats. Both Python and C implementations now raise exceptions of the same type on invalid data.
1 parent 12deda7 commit ab08ff7

File tree

4 files changed

+326
-258
lines changed

4 files changed

+326
-258
lines changed

Lib/test/test_zoneinfo/test_zoneinfo.py

Lines changed: 118 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1001,23 +1001,114 @@ def test_tzstr_from_utc(self):
10011001

10021002
self.assertEqual(dt_act, dt_utc)
10031003

1004+
def test_extreme_tzstr(self):
1005+
tzstrs = [
1006+
# Extreme offset hour
1007+
"AAA24",
1008+
"AAA+24",
1009+
"AAA-24",
1010+
"AAA24BBB,J60/2,J300/2",
1011+
"AAA+24BBB,J60/2,J300/2",
1012+
"AAA-24BBB,J60/2,J300/2",
1013+
"AAA4BBB24,J60/2,J300/2",
1014+
"AAA4BBB+24,J60/2,J300/2",
1015+
"AAA4BBB-24,J60/2,J300/2",
1016+
# Extreme offset minutes
1017+
"AAA4:00BBB,J60/2,J300/2",
1018+
"AAA4:59BBB,J60/2,J300/2",
1019+
"AAA4BBB5:00,J60/2,J300/2",
1020+
"AAA4BBB5:59,J60/2,J300/2",
1021+
# Extreme offset seconds
1022+
"AAA4:00:00BBB,J60/2,J300/2",
1023+
"AAA4:00:59BBB,J60/2,J300/2",
1024+
"AAA4BBB5:00:00,J60/2,J300/2",
1025+
"AAA4BBB5:00:59,J60/2,J300/2",
1026+
# Extreme total offset
1027+
"AAA24:59:59BBB5,J60/2,J300/2",
1028+
"AAA-24:59:59BBB5,J60/2,J300/2",
1029+
"AAA4BBB24:59:59,J60/2,J300/2",
1030+
"AAA4BBB-24:59:59,J60/2,J300/2",
1031+
# Extreme months
1032+
"AAA4BBB,M12.1.1/2,M1.1.1/2",
1033+
"AAA4BBB,M1.1.1/2,M12.1.1/2",
1034+
# Extreme weeks
1035+
"AAA4BBB,M1.5.1/2,M1.1.1/2",
1036+
"AAA4BBB,M1.1.1/2,M1.5.1/2",
1037+
# Extreme weekday
1038+
"AAA4BBB,M1.1.6/2,M2.1.1/2",
1039+
"AAA4BBB,M1.1.1/2,M2.1.6/2",
1040+
# Extreme numeric offset
1041+
"AAA4BBB,0/2,20/2",
1042+
"AAA4BBB,0/2,0/14",
1043+
"AAA4BBB,20/2,365/2",
1044+
"AAA4BBB,365/2,365/14",
1045+
# Extreme julian offset
1046+
"AAA4BBB,J1/2,J20/2",
1047+
"AAA4BBB,J1/2,J1/14",
1048+
"AAA4BBB,J20/2,J365/2",
1049+
"AAA4BBB,J365/2,J365/14",
1050+
# Extreme transition hour
1051+
"AAA4BBB,J60/167,J300/2",
1052+
"AAA4BBB,J60/+167,J300/2",
1053+
"AAA4BBB,J60/-167,J300/2",
1054+
"AAA4BBB,J60/2,J300/167",
1055+
"AAA4BBB,J60/2,J300/+167",
1056+
"AAA4BBB,J60/2,J300/-167",
1057+
# Extreme transition minutes
1058+
"AAA4BBB,J60/2:00,J300/2",
1059+
"AAA4BBB,J60/2:59,J300/2",
1060+
"AAA4BBB,J60/2,J300/2:00",
1061+
"AAA4BBB,J60/2,J300/2:59",
1062+
# Extreme transition seconds
1063+
"AAA4BBB,J60/2:00:00,J300/2",
1064+
"AAA4BBB,J60/2:00:59,J300/2",
1065+
"AAA4BBB,J60/2,J300/2:00:00",
1066+
"AAA4BBB,J60/2,J300/2:00:59",
1067+
# Extreme total transition time
1068+
"AAA4BBB,J60/167:59:59,J300/2",
1069+
"AAA4BBB,J60/-167:59:59,J300/2",
1070+
"AAA4BBB,J60/2,J300/167:59:59",
1071+
"AAA4BBB,J60/2,J300/-167:59:59",
1072+
]
1073+
1074+
for tzstr in tzstrs:
1075+
with self.subTest(tzstr=tzstr):
1076+
self.zone_from_tzstr(tzstr)
1077+
10041078
def test_invalid_tzstr(self):
10051079
invalid_tzstrs = [
10061080
"PST8PDT", # DST but no transition specified
10071081
"+11", # Unquoted alphanumeric
10081082
"GMT,M3.2.0/2,M11.1.0/3", # Transition rule but no DST
10091083
"GMT0+11,M3.2.0/2,M11.1.0/3", # Unquoted alphanumeric in DST
10101084
"PST8PDT,M3.2.0/2", # Only one transition rule
1011-
# Invalid offsets
1012-
"STD+25",
1013-
"STD-25",
1014-
"STD+374",
1015-
"STD+374DST,M3.2.0/2,M11.1.0/3",
1016-
"STD+23DST+25,M3.2.0/2,M11.1.0/3",
1017-
"STD-23DST-25,M3.2.0/2,M11.1.0/3",
1085+
# Invalid offset hours
1086+
"AAA168",
1087+
"AAA+168",
1088+
"AAA-168",
1089+
"AAA168BBB,J60/2,J300/2",
1090+
"AAA+168BBB,J60/2,J300/2",
1091+
"AAA-168BBB,J60/2,J300/2",
1092+
"AAA4BBB168,J60/2,J300/2",
1093+
"AAA4BBB+168,J60/2,J300/2",
1094+
"AAA4BBB-168,J60/2,J300/2",
1095+
# Invalid offset minutes
1096+
"AAA4:0BBB,J60/2,J300/2",
1097+
"AAA4:100BBB,J60/2,J300/2",
1098+
"AAA4BBB5:0,J60/2,J300/2",
1099+
"AAA4BBB5:100,J60/2,J300/2",
1100+
# Invalid offset seconds
1101+
"AAA4:00:0BBB,J60/2,J300/2",
1102+
"AAA4:00:100BBB,J60/2,J300/2",
1103+
"AAA4BBB5:00:0,J60/2,J300/2",
1104+
"AAA4BBB5:00:100,J60/2,J300/2",
10181105
# Completely invalid dates
10191106
"AAA4BBB,M1443339,M11.1.0/3",
10201107
"AAA4BBB,M3.2.0/2,0349309483959c",
1108+
"AAA4BBB,,J300/2",
1109+
"AAA4BBB,z,J300/2",
1110+
"AAA4BBB,J60/2,",
1111+
"AAA4BBB,J60/2,z",
10211112
# Invalid months
10221113
"AAA4BBB,M13.1.1/2,M1.1.1/2",
10231114
"AAA4BBB,M1.1.1/2,M13.1.1/2",
@@ -1037,6 +1128,26 @@ def test_invalid_tzstr(self):
10371128
# Invalid julian offset
10381129
"AAA4BBB,J0/2,J20/2",
10391130
"AAA4BBB,J20/2,J366/2",
1131+
# Invalid transition time
1132+
"AAA4BBB,J60/2/3,J300/2",
1133+
"AAA4BBB,J60/2,J300/2/3",
1134+
# Invalid transition hour
1135+
"AAA4BBB,J60/168,J300/2",
1136+
"AAA4BBB,J60/+168,J300/2",
1137+
"AAA4BBB,J60/-168,J300/2",
1138+
"AAA4BBB,J60/2,J300/168",
1139+
"AAA4BBB,J60/2,J300/+168",
1140+
"AAA4BBB,J60/2,J300/-168",
1141+
# Invalid transition minutes
1142+
"AAA4BBB,J60/2:0,J300/2",
1143+
"AAA4BBB,J60/2:100,J300/2",
1144+
"AAA4BBB,J60/2,J300/2:0",
1145+
"AAA4BBB,J60/2,J300/2:100",
1146+
# Invalid transition seconds
1147+
"AAA4BBB,J60/2:00:0,J300/2",
1148+
"AAA4BBB,J60/2:00:100,J300/2",
1149+
"AAA4BBB,J60/2,J300/2:00:0",
1150+
"AAA4BBB,J60/2,J300/2:00:100",
10401151
]
10411152

10421153
for invalid_tzstr in invalid_tzstrs:

Lib/zoneinfo/_zoneinfo.py

Lines changed: 53 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -517,8 +517,8 @@ class _DayOffset:
517517
__slots__ = ["d", "julian", "hour", "minute", "second"]
518518

519519
def __init__(self, d, julian, hour=2, minute=0, second=0):
520-
if not (0 + julian) <= d <= 365:
521-
min_day = 0 + julian
520+
min_day = 0 + julian # convert bool to int
521+
if not min_day <= d <= 365:
522522
raise ValueError(f"d must be in [{min_day}, 365], not: {d}")
523523

524524
self.d = d
@@ -560,11 +560,11 @@ class _CalendarOffset:
560560
)
561561

562562
def __init__(self, m, w, d, hour=2, minute=0, second=0):
563-
if not 0 < m <= 12:
564-
raise ValueError("m must be in (0, 12]")
563+
if not 1 <= m <= 12:
564+
raise ValueError("m must be in [1, 12]")
565565

566-
if not 0 < w <= 5:
567-
raise ValueError("w must be in (0, 5]")
566+
if not 1 <= w <= 5:
567+
raise ValueError("w must be in [1, 5]")
568568

569569
if not 0 <= d <= 6:
570570
raise ValueError("d must be in [0, 6]")
@@ -634,18 +634,21 @@ def _parse_tz_str(tz_str):
634634

635635
offset_str, *start_end_str = tz_str.split(",", 1)
636636

637-
# fmt: off
638637
parser_re = re.compile(
639-
r"(?P<std>[^<0-9:.+-]+|<[a-zA-Z0-9+\-]+>)" +
640-
r"((?P<stdoff>[+-]?\d{1,2}(:\d{2}(:\d{2})?)?)" +
641-
r"((?P<dst>[^0-9:.+-]+|<[a-zA-Z0-9+\-]+>)" +
642-
r"((?P<dstoff>[+-]?\d{1,2}(:\d{2}(:\d{2})?)?))?" +
643-
r")?" + # dst
644-
r")?$" # stdoff
638+
r"""
639+
(?P<std>[^<0-9:.+-]+|<[a-zA-Z0-9+-]+>)
640+
(?:
641+
(?P<stdoff>[+-]?\d{1,3}(?::\d{2}(?::\d{2})?)?)
642+
(?:
643+
(?P<dst>[^0-9:.+-]+|<[a-zA-Z0-9+-]+>)
644+
(?P<dstoff>[+-]?\d{1,3}(?::\d{2}(?::\d{2})?)?)?
645+
)? # dst
646+
)? # stdoff
647+
""",
648+
re.ASCII|re.VERBOSE
645649
)
646-
# fmt: on
647650

648-
m = parser_re.match(offset_str)
651+
m = parser_re.fullmatch(offset_str)
649652

650653
if m is None:
651654
raise ValueError(f"{tz_str} is not a valid TZ string")
@@ -696,16 +699,17 @@ def _parse_tz_str(tz_str):
696699

697700

698701
def _parse_dst_start_end(dststr):
699-
date, *time = dststr.split("/")
700-
if date[0] == "M":
702+
date, *time = dststr.split("/", 1)
703+
type = date[:1]
704+
if type == "M":
701705
n_is_julian = False
702-
m = re.match(r"M(\d{1,2})\.(\d).(\d)$", date)
706+
m = re.fullmatch(r"M(\d{1,2})\.(\d).(\d)", date, re.ASCII)
703707
if m is None:
704708
raise ValueError(f"Invalid dst start/end date: {dststr}")
705709
date_offset = tuple(map(int, m.groups()))
706710
offset = _CalendarOffset(*date_offset)
707711
else:
708-
if date[0] == "J":
712+
if type == "J":
709713
n_is_julian = True
710714
date = date[1:]
711715
else:
@@ -715,38 +719,54 @@ def _parse_dst_start_end(dststr):
715719
offset = _DayOffset(doy, n_is_julian)
716720

717721
if time:
718-
time_components = list(map(int, time[0].split(":")))
719-
n_components = len(time_components)
720-
if n_components < 3:
721-
time_components.extend([0] * (3 - n_components))
722-
offset.hour, offset.minute, offset.second = time_components
722+
offset.hour, offset.minute, offset.second = _parse_transition_time(time[0])
723723

724724
return offset
725725

726726

727+
def _parse_transition_time(time_str):
728+
match = re.fullmatch(
729+
r"(?P<sign>[+-])?(?P<h>\d{1,3})(:(?P<m>\d{2})(:(?P<s>\d{2}))?)?",
730+
time_str,
731+
re.ASCII
732+
)
733+
if match is None:
734+
raise ValueError(f"Invalid time: {time_str}")
735+
736+
h, m, s = (int(v or 0) for v in match.group("h", "m", "s"))
737+
738+
if h > 167:
739+
raise ValueError(
740+
f"Hour must be in [0, 167]: {time_str}"
741+
)
742+
743+
if match.group("sign") == "-":
744+
h, m, s = -h, -m, -s
745+
746+
return h, m, s
747+
748+
727749
def _parse_tz_delta(tz_delta):
728-
match = re.match(
729-
r"(?P<sign>[+-])?(?P<h>\d{1,2})(:(?P<m>\d{2})(:(?P<s>\d{2}))?)?",
750+
match = re.fullmatch(
751+
r"(?P<sign>[+-])?(?P<h>\d{1,3})(:(?P<m>\d{2})(:(?P<s>\d{2}))?)?",
730752
tz_delta,
753+
re.ASCII
731754
)
732755
# Anything passed to this function should already have hit an equivalent
733756
# regular expression to find the section to parse.
734757
assert match is not None, tz_delta
735758

736-
h, m, s = (
737-
int(v) if v is not None else 0
738-
for v in map(match.group, ("h", "m", "s"))
739-
)
759+
h, m, s = (int(v or 0) for v in match.group("h", "m", "s"))
740760

741761
total = h * 3600 + m * 60 + s
742762

743-
if not -86400 < total < 86400:
763+
if h > 24:
744764
raise ValueError(
745-
f"Offset must be strictly between -24h and +24h: {tz_delta}"
765+
f"Offset hours must be in [0, 24]: {tz_delta}"
746766
)
747767

748768
# Yes, +5 maps to an offset of -5h
749769
if match.group("sign") != "-":
750-
total *= -1
770+
total = -total
751771

752772
return total
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
:mod:`zipinfo` now supports the full range of values in the TZ string
2+
determined by RFC 8536 and detects all invalid formats.
3+
Both Python and C implementations now raise exceptions of the same
4+
type on invalid data.

0 commit comments

Comments
 (0)