diff --git a/news/2087.bugfix b/news/2087.bugfix new file mode 100644 index 000000000..90274225e --- /dev/null +++ b/news/2087.bugfix @@ -0,0 +1 @@ +Reject zero-length periods in vPeriod and vPeriod.from_ical when start and end datetimes are identical. @RajGahoi diff --git a/src/icalendar/prop/dt/period.py b/src/icalendar/prop/dt/period.py index 1deb428cf..1b2a0dc21 100644 --- a/src/icalendar/prop/dt/period.py +++ b/src/icalendar/prop/dt/period.py @@ -96,7 +96,7 @@ class vPeriod(TimeBase): Raises: TypeError: If the start is not a date or a datetime, or if the end is not a date, a datetime, or a duration. - ValueError: If the start is after the end. + ValueError: If the start is after or equal to the end. Examples: A period from 18:00:00 UTC on January 1, 1997 to 07:00:00 UTC on @@ -163,6 +163,8 @@ def __init__( duration = normalize_pytz(end - start) if start > end: raise ValueError("Start time is greater than end time") + if not isinstance(end_or_duration, timedelta) and start == end: + raise ValueError("Start time is equal to end time") self.params = Parameters(params or {"value": "PERIOD"}) # set the timezone identifier @@ -198,7 +200,16 @@ def from_ical(ical, timezone=None): end_or_duration = vDDDTypes.from_ical(end_or_duration, timezone=timezone) except Exception as e: raise ValueError(f"Expected period format, got: {ical}") from e - return _to_period_datetimes(start, end_or_duration) + start, end_or_duration = _to_period_datetimes(start, end_or_duration) + if isinstance(end_or_duration, timedelta): + end = normalize_pytz(start + end_or_duration) + else: + end = end_or_duration + if start > end: + raise ValueError("Start time is greater than end time") + if not isinstance(end_or_duration, timedelta) and start == end: + raise ValueError("Start time is equal to end time") + return start, end_or_duration def __repr__(self) -> str: p = (self.start, self.duration) if self.by_duration else (self.start, self.end) diff --git a/src/icalendar/tests/prop/test_vPeriod.py b/src/icalendar/tests/prop/test_vPeriod.py index b11eb8f05..8ef4a7d77 100644 --- a/src/icalendar/tests/prop/test_vPeriod.py +++ b/src/icalendar/tests/prop/test_vPeriod.py @@ -54,6 +54,7 @@ def test_timezoned_with_timedelta(tzp): (("20000101T000000", datetime(2000, 1, 2)), TypeError), ((datetime(2000, 1, 1), "20000102T000000"), TypeError), ((datetime(2000, 1, 2), datetime(2000, 1, 1)), ValueError), + ((datetime(2000, 1, 1), datetime(2000, 1, 1)), ValueError), ((datetime(2000, 1, 2), timedelta(-1)), ValueError), ], ) diff --git a/src/icalendar/tests/test_issue_2087_zero_length_period.py b/src/icalendar/tests/test_issue_2087_zero_length_period.py new file mode 100644 index 000000000..b3a7442a9 --- /dev/null +++ b/src/icalendar/tests/test_issue_2087_zero_length_period.py @@ -0,0 +1,41 @@ +from datetime import datetime, timezone + +import pytest + +from icalendar import Calendar +from icalendar.prop import vDDDLists, vPeriod +from icalendar.prop.broken import vBroken + + +def test_zero_length_period_identical_datetimes_rejected(): + start = datetime(2026, 11, 10, 10, 0, tzinfo=timezone.utc) + with pytest.raises(ValueError, match="Start time is equal to end time"): + vPeriod((start, start)) + + +def test_zero_length_period_from_ical_rejected(): + with pytest.raises(ValueError, match="Start time is equal to end time"): + vPeriod.from_ical("20261110T100000Z/20261110T100000Z") + + +def test_zero_length_period_in_vdddlists_rejected(): + with pytest.raises(ValueError, match="Start time is equal to end time"): + vDDDLists.from_ical("20261110T100000Z/20261110T100000Z") + + +def test_zero_length_period_in_calendar_marked_broken(): + ics = """BEGIN:VCALENDAR +VERSION:2.0 +PRODID:-//example//test//EN +BEGIN:VEVENT +UID:zero-period +DTSTART:20261110T100000Z +RDATE;VALUE=PERIOD:20261110T100000Z/20261110T100000Z +END:VEVENT +END:VCALENDAR +""" + cal = Calendar.from_ical(ics) + rdate = cal.subcomponents[0]["RDATE"] + assert isinstance(rdate, vBroken) + assert isinstance(rdate.parse_error, ValueError) + assert "Start time is equal to end time" in str(rdate.parse_error)