Skip to content

Commit f8c6f7c

Browse files
committed
fix: truncating caldav subscription data before saving on DB
Signed-off-by: Roberto Guido <info@madbob.org>
1 parent d3aba32 commit f8c6f7c

2 files changed

Lines changed: 46 additions & 10 deletions

File tree

apps/dav/lib/CalDAV/CalDavBackend.php

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -161,13 +161,13 @@ class CalDavBackend extends AbstractBackend implements SyncSupport, Subscription
161161
* @var array
162162
*/
163163
public array $subscriptionPropertyMap = [
164-
'{DAV:}displayname' => ['displayname', 'string'],
165-
'{http://apple.com/ns/ical/}refreshrate' => ['refreshrate', 'string'],
166-
'{http://apple.com/ns/ical/}calendar-order' => ['calendarorder', 'int'],
167-
'{http://apple.com/ns/ical/}calendar-color' => ['calendarcolor', 'string'],
168-
'{http://calendarserver.org/ns/}subscribed-strip-todos' => ['striptodos', 'bool'],
169-
'{http://calendarserver.org/ns/}subscribed-strip-alarms' => ['stripalarms', 'string'],
170-
'{http://calendarserver.org/ns/}subscribed-strip-attachments' => ['stripattachments', 'string'],
164+
'{DAV:}displayname' => ['displayname', 'string', 100],
165+
'{http://apple.com/ns/ical/}refreshrate' => ['refreshrate', 'string', 10],
166+
'{http://apple.com/ns/ical/}calendar-order' => ['calendarorder', 'int', 0],
167+
'{http://apple.com/ns/ical/}calendar-color' => ['calendarcolor', 'string', 255],
168+
'{http://calendarserver.org/ns/}subscribed-strip-todos' => ['striptodos', 'bool', 0],
169+
'{http://calendarserver.org/ns/}subscribed-strip-alarms' => ['stripalarms', 'bool', 0],
170+
'{http://calendarserver.org/ns/}subscribed-strip-attachments' => ['stripattachments', 'bool', 0],
171171
];
172172

173173
/**
@@ -3040,9 +3040,14 @@ public function createSubscription($principalUri, $uri, array $properties) {
30403040

30413041
$propertiesBoolean = ['striptodos', 'stripalarms', 'stripattachments'];
30423042

3043-
foreach ($this->subscriptionPropertyMap as $xmlName => [$dbName, $type]) {
3043+
foreach ($this->subscriptionPropertyMap as $xmlName => [$dbName, $type, $length]) {
30443044
if (array_key_exists($xmlName, $properties)) {
3045-
$values[$dbName] = $properties[$xmlName];
3045+
if ($type == 'string') {
3046+
$values[$dbName] = mb_substr($properties[$xmlName], 0, $length);
3047+
} else {
3048+
$values[$dbName] = $properties[$xmlName];
3049+
}
3050+
30463051
if (in_array($dbName, $propertiesBoolean)) {
30473052
$values[$dbName] = true;
30483053
}
@@ -3099,7 +3104,14 @@ public function updateSubscription($subscriptionId, PropPatch $propPatch) {
30993104
$newValues['source'] = $propertyValue->getHref();
31003105
} else {
31013106
$fieldName = $this->subscriptionPropertyMap[$propertyName][0];
3102-
$newValues[$fieldName] = $propertyValue;
3107+
$fieldType = $this->subscriptionPropertyMap[$propertyName][1];
3108+
3109+
if ($fieldType === 'string') {
3110+
$fieldLength = $this->subscriptionPropertyMap[$propertyName][2];
3111+
$newValues[$fieldName] = mb_substr($propertyValue, 0, $fieldLength);
3112+
} else {
3113+
$newValues[$fieldName] = $propertyValue;
3114+
}
31033115
}
31043116
}
31053117

apps/dav/tests/unit/CalDAV/CalDavBackendTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -729,6 +729,30 @@ public function testSubscriptions(): void {
729729
$this->assertCount(0, $subscriptions);
730730
}
731731

732+
public function testSubscriptionsHugeProps(): void {
733+
$first_longname = 'This is a very long name, longer than 100 characters, used on purpose with the intention to test truncation';
734+
$second_longname = 'Another very long name, longer than 100 characters, used to test truncation while updating the subscription';
735+
736+
$id = $this->backend->createSubscription(self::UNIT_TEST_USER, 'Subscription', [
737+
'{DAV:}displayname' => $first_longname,
738+
'{http://calendarserver.org/ns/}source' => new Href('test-source'),
739+
'{http://apple.com/ns/ical/}calendar-color' => '#1C4587',
740+
'{http://calendarserver.org/ns/}subscribed-strip-todos' => ''
741+
]);
742+
743+
$subscriptions = $this->backend->getSubscriptionsForUser(self::UNIT_TEST_USER);
744+
$this->assertEquals(mb_substr($first_longname, 0, 100), $subscriptions[0]['{DAV:}displayname']);
745+
746+
$patch = new PropPatch([
747+
'{DAV:}displayname' => $second_longname,
748+
]);
749+
$this->backend->updateSubscription($id, $patch);
750+
$patch->commit();
751+
752+
$subscriptions = $this->backend->getSubscriptionsForUser(self::UNIT_TEST_USER);
753+
$this->assertEquals(mb_substr($second_longname, 0, 100), $subscriptions[0]['{DAV:}displayname']);
754+
}
755+
732756
public static function providesSchedulingData(): array {
733757
$data = <<<EOS
734758
BEGIN:VCALENDAR

0 commit comments

Comments
 (0)