Skip to content

Commit 8133258

Browse files
committed
fix(dav): map invalid CalDAV payloads to 400 instead of 500
extractTodo() throws Sabre\VObject\InvalidDataException for malformed ICS or payloads that do not contain exactly one VTODO. That exception does not extend Sabre\DAV\Exception, so the DAV server mapped it to 500 instead of the 400 the PR description promises. Catch it in CalendarObject::put() and rethrow as BadRequest, and cover the put() mapping with a unit test.
1 parent 651a993 commit 8133258

2 files changed

Lines changed: 20 additions & 0 deletions

File tree

lib/DAV/CalendarObject.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Sabre\DAV\Exception\NotFound;
1818
use Sabre\DAVACL\IACL;
1919
use Sabre\VObject\Component\VCalendar;
20+
use Sabre\VObject\InvalidDataException;
2021

2122
class CalendarObject implements ICalendarObject, IACL {
2223

@@ -78,6 +79,8 @@ public function put($data) {
7879
$this->sourceItem = $this->backend->updateCardFromCalendarObject($this->sourceItem, $this->readPutData($data));
7980
} catch (DoesNotExistException $e) {
8081
throw new NotFound($e->getMessage(), 0, $e);
82+
} catch (InvalidDataException $e) {
83+
throw new BadRequest($e->getMessage(), 0, $e);
8184
} catch (BadRequestException $e) {
8285
throw new BadRequest($e->getMessage(), 0, $e);
8386
} catch (StatusException $e) {

tests/unit/DAV/CalendarObjectTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Sabre\DAV\Exception\BadRequest;
1818
use Sabre\DAV\Exception\Forbidden;
1919
use Sabre\DAV\Exception\NotFound;
20+
use Sabre\VObject\InvalidDataException;
2021
use Test\TestCase;
2122

2223
class CalendarObjectTest extends TestCase {
@@ -216,6 +217,22 @@ public function testPutMapsBadRequestExceptionToBadRequest(): void {
216217
$object->put("BEGIN:VCALENDAR\r\nEND:VCALENDAR");
217218
}
218219

220+
public function testPutMapsInvalidDataExceptionToBadRequest(): void {
221+
$backend = $this->createMock(DeckCalendarBackend::class);
222+
$backend->method('updateCardFromCalendarObject')
223+
->willThrowException(new InvalidDataException('Calendar payload must contain exactly one VTODO'));
224+
225+
$object = new CalendarObject(
226+
$this->createCalendarMock(),
227+
'card-1.ics',
228+
$backend,
229+
$this->createCard()
230+
);
231+
232+
$this->expectException(BadRequest::class);
233+
$object->put("BEGIN:VCALENDAR\r\nEND:VCALENDAR");
234+
}
235+
219236
public function testPutMapsDoesNotExistExceptionToNotFound(): void {
220237
$backend = $this->createMock(DeckCalendarBackend::class);
221238
$backend->method('updateCardFromCalendarObject')

0 commit comments

Comments
 (0)