Skip to content

Commit f98025b

Browse files
committed
Reject out-of-range UTC offsets in the pure-Python parsers
1 parent d3f44aa commit f98025b

4 files changed

Lines changed: 21 additions & 0 deletions

File tree

‎src/pendulum/formatting/formatter.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,9 @@ def _get_parsed_value(
607607

608608
offset = ((int(off_hour) * 60) + int(off_minute)) * 60
609609

610+
if abs(offset) >= 24 * 3600:
611+
raise ValueError("Invalid date")
612+
610613
if negative:
611614
offset = -1 * offset
612615

‎src/pendulum/parsing/iso8601.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,9 @@ def parse_iso8601(
250250

251251
offset = ((int(off_hour) * 60) + int(off_minute)) * 60
252252

253+
if abs(offset) >= 24 * 3600:
254+
raise ParserError(f"Invalid date string: {text}")
255+
253256
if negative:
254257
offset = -1 * offset
255258

‎tests/datetime/test_from_format.py‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,13 @@ def test_from_format_with_invalid_padded_day():
7272
pendulum.from_format("Apr 2 12:00:00 2020 GMT", "MMM DD HH:mm:ss YYYY z")
7373

7474

75+
def test_from_format_with_out_of_range_offset():
76+
with pytest.raises(ValueError):
77+
pendulum.from_format("1975-05-21 22:32:11 +9959", "YYYY-MM-DD HH:mm:ss ZZ")
78+
with pytest.raises(ValueError):
79+
pendulum.from_format("1975-05-21 22:32:11 +24:00", "YYYY-MM-DD HH:mm:ss Z")
80+
81+
7582
@pytest.mark.parametrize(
7683
"text,fmt,expected,now",
7784
[

‎tests/parsing/test_parse_iso8601.py‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,14 @@ def test_parse_iso8601_invalid():
153153
with pytest.raises(ValueError):
154154
parse_iso8601("2009-05-19 14:39:22+0:6:00")
155155

156+
# Offset out of range (>= 24h)
157+
with pytest.raises(ValueError):
158+
parse_iso8601("2016-10-16T12:34:56+99:59")
159+
with pytest.raises(ValueError):
160+
parse_iso8601("2016-10-16T12:34:56-2400")
161+
with pytest.raises(ValueError):
162+
parse_iso8601("2016-10-16T12:34:56+2400")
163+
156164
# Missing time separator
157165
with pytest.raises(ValueError):
158166
parse_iso8601("2009-05-1914:39")

0 commit comments

Comments
 (0)