|
16 | 16 |
|
17 | 17 | from core import factories |
18 | 18 | from core.enums import MailboxRoleChoices |
19 | | -from core.services.calendar.service import CalDAVService |
| 19 | +from core.services.calendar.service import CalDAVError, CalDAVService |
20 | 20 |
|
21 | 21 |
|
22 | 22 | class _SilentHandler(WSGIRequestHandler): |
@@ -726,6 +726,93 @@ def test_leaves_other_attendees_untouched(self): |
726 | 726 | assert by_email["mailto:me@example.com"].params["PARTSTAT"] == "ACCEPTED" |
727 | 727 | assert by_email["mailto:other@example.com"].params["PARTSTAT"] == "NEEDS-ACTION" |
728 | 728 |
|
| 729 | + def test_substring_email_does_not_match(self): |
| 730 | + """An attendee whose address contains the target as a substring must |
| 731 | + not be updated; only an exact email match is.""" |
| 732 | + ics = ( |
| 733 | + "BEGIN:VCALENDAR\r\n" |
| 734 | + "VERSION:2.0\r\n" |
| 735 | + "PRODID:-//Test//Test//EN\r\n" |
| 736 | + "BEGIN:VEVENT\r\n" |
| 737 | + "UID:x@example.com\r\n" |
| 738 | + "DTSTART:20260101T120000Z\r\n" |
| 739 | + "DTEND:20260101T130000Z\r\n" |
| 740 | + "SUMMARY:X\r\n" |
| 741 | + "ATTENDEE;PARTSTAT=NEEDS-ACTION:mailto:notme@example.com\r\n" |
| 742 | + "ATTENDEE;PARTSTAT=NEEDS-ACTION;RSVP=TRUE:mailto:me@example.com\r\n" |
| 743 | + "END:VEVENT\r\n" |
| 744 | + "END:VCALENDAR\r\n" |
| 745 | + ) |
| 746 | + cal = ICalendar.from_ical(ics) |
| 747 | + CalDAVService._update_partstat(cal, "me@example.com", "ACCEPTED") |
| 748 | + |
| 749 | + attendees = cal.walk("VEVENT")[0].get("ATTENDEE") |
| 750 | + by_email = {str(a).lower(): a for a in attendees} |
| 751 | + assert by_email["mailto:me@example.com"].params["PARTSTAT"] == "ACCEPTED" |
| 752 | + assert by_email["mailto:notme@example.com"].params["PARTSTAT"] == "NEEDS-ACTION" |
| 753 | + |
| 754 | + |
| 755 | +class TestPickCalendarUrl: |
| 756 | + """Direct tests for calendar URL selection / SSRF guard.""" |
| 757 | + |
| 758 | + def _service_with_calendars(self, calendar_ids): |
| 759 | + service = CalDAVService(url="https://caldav.example.com/") |
| 760 | + service.list_calendars = lambda: [ # type: ignore[method-assign] |
| 761 | + {"id": cid, "name": cid} for cid in calendar_ids |
| 762 | + ] |
| 763 | + return service |
| 764 | + |
| 765 | + def test_returns_first_when_no_id_given(self): |
| 766 | + service = self._service_with_calendars( |
| 767 | + ["https://caldav.example.com/u/cal1/", "https://caldav.example.com/u/cal2/"] |
| 768 | + ) |
| 769 | + assert service._pick_calendar_url(None) == "https://caldav.example.com/u/cal1/" |
| 770 | + |
| 771 | + def test_accepts_known_calendar_id(self): |
| 772 | + service = self._service_with_calendars( |
| 773 | + ["https://caldav.example.com/u/cal1/", "https://caldav.example.com/u/cal2/"] |
| 774 | + ) |
| 775 | + assert ( |
| 776 | + service._pick_calendar_url("https://caldav.example.com/u/cal2/") |
| 777 | + == "https://caldav.example.com/u/cal2/" |
| 778 | + ) |
| 779 | + |
| 780 | + def test_rejects_arbitrary_url(self): |
| 781 | + """An attacker-controlled URL must not be used as a calendar target.""" |
| 782 | + service = self._service_with_calendars(["https://caldav.example.com/u/cal1/"]) |
| 783 | + with pytest.raises(CalDAVError): |
| 784 | + service._pick_calendar_url("https://attacker.example.org/evil/") |
| 785 | + |
| 786 | + def test_rejects_cross_origin_before_listing(self): |
| 787 | + """The origin check must reject foreign hosts even if list_calendars() |
| 788 | + somehow returned a matching entry — defense in depth.""" |
| 789 | + service = CalDAVService(url="https://caldav.example.com/") |
| 790 | + called = {"n": 0} |
| 791 | + |
| 792 | + def _spy(): |
| 793 | + called["n"] += 1 |
| 794 | + return [{"id": "https://attacker.example.org/evil/", "name": "evil"}] |
| 795 | + |
| 796 | + service.list_calendars = _spy # type: ignore[method-assign] |
| 797 | + with pytest.raises(CalDAVError): |
| 798 | + service._pick_calendar_url("https://attacker.example.org/evil/") |
| 799 | + assert called["n"] == 0 |
| 800 | + |
| 801 | + def test_rejects_scheme_relative_or_malformed(self): |
| 802 | + service = self._service_with_calendars(["https://caldav.example.com/u/cal1/"]) |
| 803 | + with pytest.raises(CalDAVError): |
| 804 | + service._pick_calendar_url("/u/cal1/") |
| 805 | + |
| 806 | + def test_rejects_unknown_calendar_on_same_host(self): |
| 807 | + service = self._service_with_calendars(["https://caldav.example.com/u/cal1/"]) |
| 808 | + with pytest.raises(CalDAVError): |
| 809 | + service._pick_calendar_url("https://caldav.example.com/u/other/") |
| 810 | + |
| 811 | + def test_raises_when_no_calendars(self): |
| 812 | + service = self._service_with_calendars([]) |
| 813 | + with pytest.raises(CalDAVError): |
| 814 | + service._pick_calendar_url(None) |
| 815 | + |
729 | 816 |
|
730 | 817 | # --------------------------------------------------------------------------- |
731 | 818 | # Credential contract: Basic Auth user = mailbox email, password = setting |
|
0 commit comments