diff --git a/news/+request-status-escaping.bugfix b/news/+request-status-escaping.bugfix new file mode 100644 index 000000000..d6e0c579f --- /dev/null +++ b/news/+request-status-escaping.bugfix @@ -0,0 +1 @@ +Fixed :class:`~icalendar.prop.vRequestStatus` escaping the ``;`` separators between its fields, which produced invalid ``REQUEST-STATUS:2.0\;Success`` lines that other clients cannot read. Parsing now keeps an escaped ``\;`` inside the description or data, instead of splitting the field there. Values written by older versions are still read correctly. I used AI to assist me with this change. @Prajas02 diff --git a/src/icalendar/prop/request_status.py b/src/icalendar/prop/request_status.py index 03f1d8c8b..3e56386c2 100644 --- a/src/icalendar/prop/request_status.py +++ b/src/icalendar/prop/request_status.py @@ -8,12 +8,14 @@ from icalendar.error import InvalidCalendar, JCalParsingError from icalendar.parser.parameter import Parameters +from icalendar.parser.property import unescape_backslash from icalendar.parser.xcal.wrapper import from_xcal_wrapper from .text import vText if TYPE_CHECKING: from icalendar.compatibility import Self + from icalendar.parser.content_line import Contentline from icalendar.parser.xcal.base import XCalParser CODE_REGEX = r"^[0-9]\.[0-9](?:\.[0-9])?$" @@ -27,6 +29,15 @@ ) UNESCAPE = re.compile(r"\\(.)") ESCAPE = re.compile(r"([\\,;])") +NEWLINE = re.compile(r"\r\n|\r|\n") +# icalendar 7.3.0 up to this fix escaped the field separators on output, +# for example ``2.0\;Success``. See :meth:`vRequestStatus.get_value_from_content_line`. +LEGACY_ESCAPED_SEPARATOR = re.compile(r"^[0-9]\.[0-9](?:\.[0-9])?\\;") + + +def _escape_field(text: str) -> str: + """Escape one field of a request status according to :rfc:`5545`.""" + return NEWLINE.sub(r"\\n", ESCAPE.sub(r"\\\1", text)) class vRequestStatus(vText): @@ -102,7 +113,43 @@ def code_string(self) -> str: def _unescape(self, string: str): """Unescape a string.""" - return UNESCAPE.sub(r"\1", string) + return UNESCAPE.sub( + lambda m: "\n" if m.group(1) in "nN" else m.group(1), string + ) + + def to_ical(self) -> bytes: + r"""Serialize the request status according to :rfc:`5545#section-3.8.8.3`. + + The value already holds the escaped fields, separated by ``;``. + The separators must not be escaped again. + + .. code-block:: pycon + + >>> from icalendar.prop import vRequestStatus + >>> vRequestStatus.new("3.1", "Invalid property value", "DTSTART:96-Apr-01").to_ical() + b'3.1;Invalid property value;DTSTART:96-Apr-01' + >>> vRequestStatus.new("2.0", "Success; done").to_ical() + b'2.0;Success\\; done' + """ + return NEWLINE.sub(r"\\n", self).encode(self.encoding) + + @classmethod + def get_value_from_content_line(cls, line: Contentline) -> str: + r"""Return the value of a REQUEST-STATUS content line, still escaped. + + The fields of a request status are separated by ``;``, + and a ``;`` inside a field is escaped as ``\;``. + Unescaping the whole value before splitting it into fields + would lose that difference, so the value is taken verbatim. + + Older versions of icalendar wrote the separators escaped, + for example ``REQUEST-STATUS:2.0\;Success``. + Such values are unescaped once, so they are still read correctly. + """ + value = line.raw_parts()[2] + if LEGACY_ESCAPED_SEPARATOR.match(value): + return unescape_backslash(value) + return value @property def description(self) -> str: @@ -179,10 +226,10 @@ def new( code_string = f"{code[0]}.{code[1]}" if len(code) == 3: code_string += f".{code[2]}" - description = ESCAPE.sub(r"\\\1", description) + description = _escape_field(description) request_status = f"{code_string};{description}" if data is not None: - data = ESCAPE.sub(r"\\\1", data) + data = _escape_field(data) request_status += f";{data}" return cls(request_status, params=params) diff --git a/src/icalendar/tests/prop/test_request_status.py b/src/icalendar/tests/prop/test_request_status.py index 737778396..3ab81f23c 100644 --- a/src/icalendar/tests/prop/test_request_status.py +++ b/src/icalendar/tests/prop/test_request_status.py @@ -2,7 +2,7 @@ import pytest -from icalendar import Event, vRequestStatus, vText +from icalendar import Calendar, Event, vRequestStatus, vText from icalendar.error import InvalidCalendar, JCalParsingError from icalendar.tests.rfc_6321_xcal.common import list2xml, to_xcal_list @@ -299,3 +299,83 @@ def test_from_xcal_preserves_invalid_code(): r = vRequestStatus.from_xcal(xml) assert r.code == () assert r.description == "" + + +def _event_with_request_status(value: str) -> str: + """Return an .ics string of an event with one REQUEST-STATUS line.""" + return ( + "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:test\r\n" + "BEGIN:VEVENT\r\nDTSTAMP:20250101T000000Z\r\nUID:1\r\n" + f"REQUEST-STATUS:{value}\r\n" + "END:VEVENT\r\nEND:VCALENDAR\r\n" + ) + + +def _unfold(ics: bytes) -> str: + """Undo the line folding of long content lines.""" + return ics.decode().replace("\r\n ", "") + + +@mark_examples +def test_serialization_does_not_escape_separators( + text, code, description, data, text_serialized +): + """The ``;`` between the fields are separators and must not be escaped. + + RFC 5545: rstatus = statcode ";" statdesc [";" extdata] + """ + event = Event() + event.add("REQUEST-STATUS", vRequestStatus.new(code, description, data)) + assert f"REQUEST-STATUS:{text}\r\n" in _unfold(event.to_ical()) + + +@mark_examples +def test_parse_from_calendar(text, code, description, data, text_serialized): + """Escaped characters inside a field do not split the field when parsing.""" + calendar = Calendar.from_ical(_event_with_request_status(text)) + request_status = calendar.events[0]["REQUEST-STATUS"] + assert request_status.code == code + assert request_status.description == description + assert request_status.data == data + + +@mark_examples +def test_parse_and_serialize_is_unchanged( + text, code, description, data, text_serialized +): + """A valid REQUEST-STATUS line is written back exactly as it was read.""" + ics = _event_with_request_status(text) + assert _unfold(Calendar.from_ical(ics).to_ical()) == ics + + +@pytest.mark.parametrize( + ("legacy_value", "code", "description", "data"), + [ + (r"2.0\;Success", (2, 0), "Success", None), + ( + r"3.1\;Invalid property value\;DTSTART:96-Apr-01", + (3, 1), + "Invalid property value", + "DTSTART:96-Apr-01", + ), + (r"2.0\;Success\\\; with note\;x\\\,y", (2, 0), "Success; with note", "x,y"), + ], +) +def test_read_values_written_by_older_versions(legacy_value, code, description, data): + """Older versions of icalendar escaped the separators. We can still read them.""" + calendar = Calendar.from_ical(_event_with_request_status(legacy_value)) + request_status = calendar.events[0]["REQUEST-STATUS"] + assert request_status.code == code + assert request_status.description == description + assert request_status.data == data + + +def test_newline_in_description_is_escaped(): + """A newline must not break the content line.""" + request_status = vRequestStatus.new("2.0", "line1\nline2", "a\nb") + assert request_status.to_ical() == rb"2.0;line1\nline2;a\nb" + event = Event() + event.add("REQUEST-STATUS", request_status) + parsed = Event.from_ical(event.to_ical())["REQUEST-STATUS"] + assert parsed.description == "line1\nline2" + assert parsed.data == "a\nb"