Skip to content

Commit

Permalink
fix(caldav): Catch invalid events for reminder generation
Browse files Browse the repository at this point in the history
Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
  • Loading branch information
ChristophWurst committed May 16, 2023
1 parent 128cd70 commit cbe36c2
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 3 deletions.
28 changes: 25 additions & 3 deletions apps/dav/lib/CalDAV/Reminder/ReminderService.php
Expand Up @@ -50,6 +50,7 @@
use Sabre\VObject\Recur\EventIterator;
use Sabre\VObject\Recur\MaxInstancesExceededException;
use Sabre\VObject\Recur\NoInstancesException;
use Throwable;
use function count;
use function strcasecmp;

Expand Down Expand Up @@ -243,7 +244,14 @@ public function onCalendarObjectCreate(array $objectData):void {
$isRecurring = $masterItem ? $this->isRecurring($masterItem) : false;

foreach ($recurrenceExceptions as $recurrenceException) {
$eventHash = $this->getEventHash($recurrenceException);
try {
$eventHash = $this->getEventHash($recurrenceException);
} catch (Throwable $e) {
$this->logger->warning("Could not hash event for reminder: " . $e->getMessage(), [
'exception' => $e,
]);
continue;
}

if (!isset($recurrenceException->VALARM)) {
continue;
Expand All @@ -267,7 +275,14 @@ public function onCalendarObjectCreate(array $objectData):void {
if ($masterItem) {
$processedAlarms = [];
$masterAlarms = [];
$masterHash = $this->getEventHash($masterItem);
try {
$masterHash = $this->getEventHash($recurrenceException);

Check notice

Code scanning / Psalm

PossiblyUndefinedVariable Note

Possibly undefined variable $recurrenceException, first seen on line 246
} catch (Throwable $e) {
$this->logger->warning("Could not hash event for reminder: " . $e->getMessage(), [
'exception' => $e,
]);
return;
}

if (!isset($masterItem->VALARM)) {
return;
Expand Down Expand Up @@ -397,7 +412,14 @@ private function getRemindersForVAlarm(VAlarm $valarm,
bool $isRecurring = false,
bool $isRecurrenceException = false):array {
if ($eventHash === null) {
$eventHash = $this->getEventHash($valarm->parent);
try {
$eventHash = $this->getEventHash($valarm->parent);

Check notice

Code scanning / Psalm

ArgumentTypeCoercion Note

Argument 1 of OCA\DAV\CalDAV\Reminder\ReminderService::getEventHash expects Sabre\VObject\Component\VEvent, but parent type Sabre\VObject\Node provided
} catch (Throwable $e) {
$this->logger->warning("Could not hash event for reminder: " . $e->getMessage(), [
'exception' => $e,
]);
return [];
}
}
if ($alarmHash === null) {
$alarmHash = $this->getAlarmHash($valarm);
Expand Down
47 changes: 47 additions & 0 deletions apps/dav/tests/unit/CalDAV/Reminder/ReminderServiceTest.php
Expand Up @@ -342,6 +342,53 @@ public function testOnCalendarObjectCreateSingleEntry():void {
$this->reminderService->onCalendarObjectCreate($objectData);
}

/**
* RFC5545 says DTSTART is REQUIRED, but we have seen event without the prop
*/
public function testOnCalendarObjectCreateNoDtstart(): void {
$calendarData = <<<EOD
BEGIN:VCALENDAR
PRODID:-//Nextcloud calendar v1.6.4
BEGIN:VEVENT
CREATED:20160602T133732
DTSTAMP:20160602T133732
LAST-MODIFIED:20160602T133732
UID:wej2z68l9h
SUMMARY:Test Event
BEGIN:VALARM
ACTION:EMAIL
TRIGGER:-PT15M
END:VALARM
BEGIN:VALARM
ACTION:DISPLAY
TRIGGER;VALUE=DATE-TIME:20160608T000000Z
END:VALARM
END:VEVENT
END:VCALENDAR
EOD;
$objectData = [
'calendardata' => $calendarData,
'id' => '42',
'calendarid' => '1337',
'component' => 'vevent',
];

$this->backend->expects($this->exactly(2))
->method('insertReminder')
->withConsecutive(
[1337, 42, 'wej2z68l9h', false, null, false, '5c70531aab15c92b52518ae10a2f78a4', 'de919af7429d3b5c11e8b9d289b411a6', 'EMAIL', true, 1465429500, false],
[1337, 42, 'wej2z68l9h', false, null, false, '5c70531aab15c92b52518ae10a2f78a4', '35b3eae8e792aa2209f0b4e1a302f105', 'DISPLAY', false, 1465344000, false]
)
->willReturn(1);

$this->timeFactory->expects($this->once())
->method('getDateTime')
->with()
->willReturn(DateTime::createFromFormat(DateTime::ATOM, '2016-06-08T00:00:00+00:00'));

$this->reminderService->onCalendarObjectCreate($objectData);
}

public function testOnCalendarObjectCreateSingleEntryWithRepeat(): void {
$objectData = [
'calendardata' => self::CALENDAR_DATA_REPEAT,
Expand Down

0 comments on commit cbe36c2

Please sign in to comment.