Skip to content
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 41 additions & 5 deletions apps/dav/lib/DAV/CustomPropertiesBackend.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IDBConnection;
use OCP\IUser;
use Sabre\DAV\Exception as DavException;
use Sabre\DAV\PropertyStorage\Backend\BackendInterface;
use Sabre\DAV\PropFind;
use Sabre\DAV\PropPatch;
Expand Down Expand Up @@ -373,7 +374,7 @@
->executeStatement();
}
} else {
[$value, $valueType] = $this->encodeValueForDatabase($propertyValue);
[$value, $valueType] = $this->encodeValueForDatabase($propertyName, $propertyValue);
$dbParameters['propertyValue'] = $value;
$dbParameters['valueType'] = $valueType;

Expand Down Expand Up @@ -415,17 +416,46 @@
return $path;
}

private static function checkIsArrayOfScalar(string $name, array $array): void {
foreach ($array as $item) {
if (is_array($item)) {
self::checkIsArrayOfScalar($name, $item);
} elseif ($item !== null && !is_scalar($item)) {
throw new DavException(
"Property \"$name\" has an invalid value of array containing " . gettype($item),
);
}
}
}

/**
* @param mixed $value
* @return array
*/
private function encodeValueForDatabase($value): array {
private function encodeValueForDatabase(string $name, $value): array {
if (is_scalar($value)) {
$valueType = self::PROPERTY_TYPE_STRING;
} elseif ($value instanceof Complex) {
$valueType = self::PROPERTY_TYPE_XML;
$value = $value->getXml();
} else {
if (is_array($value)) {
// For array only allow scalar values
self::checkIsArrayOfScalar($name, $value);
} elseif (!is_object($value)) {
throw new DavException(
"Property \"$name\" has an invalid value of type " . gettype($value),
);
} else {
if (!str_starts_with($value::class, 'Sabre\\DAV\\Xml\\Property\\')
&& !str_starts_with($value::class, 'Sabre\\CalDAV\\Xml\\Property\\')
&& !str_starts_with($value::class, 'Sabre\\CardDAV\\Xml\\Property\\')
&& !str_starts_with($value::class, 'OCA\\DAV\\')) {
throw new DavException(
"Property \"$name\" has an invalid value of class " . $value::class,
);
}
}
$valueType = self::PROPERTY_TYPE_OBJECT;
$value = serialize($value);
}
Expand All @@ -435,16 +465,22 @@
/**
* @return mixed|Complex|string
*/
private function decodeValueFromDatabase(string $value, int $valueType) {
private function decodeValueFromDatabase(string $value, int $valueType): mixed {

Check failure on line 468 in apps/dav/lib/DAV/CustomPropertiesBackend.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

ReservedWord

apps/dav/lib/DAV/CustomPropertiesBackend.php:468:75: ReservedWord: mixed is a reserved word (see https://psalm.dev/095)

Check failure on line 468 in apps/dav/lib/DAV/CustomPropertiesBackend.php

View workflow job for this annotation

GitHub Actions / Psalm

mixed is a reserved word (see https://psalm.dev/095)
switch ($valueType) {
case self::PROPERTY_TYPE_XML:
return new Complex($value);
case self::PROPERTY_TYPE_OBJECT:
if (preg_match('/^a:/', $value)) {
// Array, unserialize only scalar values
return unserialize(str_replace('\x00', chr(0), $value), ['allowed_classes' => false]);
}
if (!preg_match('/^O\:\d+\:\"(OCA\\\\DAV\\\\|Sabre\\\\(Cal|Card)?DAV\\\\Xml\\\\Property\\\\)/', $value)) {
throw new \LogicException('Found an object class serialized in DB that is not allowed');
}
return unserialize($value);
case self::PROPERTY_TYPE_STRING:
default:
return $value;
}
};
}

private function createDeleteQuery(): IQueryBuilder {
Expand Down
Loading