Skip to content

Commit 848c0c9

Browse files
hamza221backportbot[bot]
authored andcommitted
fix: access shared trashbin objects
fix: access shared trashbin objects Signed-off-by: Hamza <hamzamahjoubi221@gmail.com> Assisted-by: claude:Opus 4.7 (1M context) [skip ci]
1 parent 91178eb commit 848c0c9

4 files changed

Lines changed: 304 additions & 17 deletions

File tree

apps/dav/lib/CalDAV/CalDavBackend.php

Lines changed: 259 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1183,20 +1183,48 @@ public function getDeletedCalendarObjects(int $deletedBefore): array {
11831183
}
11841184

11851185
/**
1186-
* Return all deleted calendar objects by the given principal that are not
1187-
* in deleted calendars.
1186+
* Return all deleted calendar objects accessible to the given principal:
1187+
* - Calendars owned by the principal.
1188+
* - Calendars shared with the principal.
1189+
* - Calendars owned by users who delegated the principal (calendar-proxy-*),
1190+
* plus calendars shared with those delegators (transitively).
11881191
*
11891192
* @param string $principalUri
11901193
* @return array
11911194
* @throws Exception
11921195
*/
11931196
public function getDeletedCalendarObjectsByPrincipal(string $principalUri): array {
1197+
$result = [];
1198+
$this->collectDeletedCalendarObjectsForPrincipal($principalUri, $result, null);
1199+
foreach ($this->getProxyDelegators($principalUri) as $delegator => $hasProxyWrite) {
1200+
$overlay = $hasProxyWrite ? Backend::ACCESS_READ_WRITE : Backend::ACCESS_READ;
1201+
$this->collectDeletedCalendarObjectsForPrincipal($delegator, $result, $overlay);
1202+
}
1203+
return array_values($result);
1204+
}
1205+
1206+
/**
1207+
* Run the owned + shared trashbin queries for $principalUri and merge the
1208+
* results into $result, keyed by calendar object id.
1209+
*
1210+
* @param string $principalUri principal whose calendars to scan.
1211+
* @param array $result accumulator keyed by calendar object id; merged in-place.
1212+
* @param int|null $proxyOverlay if non-null, the entries are being collected on
1213+
* behalf of a different accessor via calendar-proxy; the value caps the
1214+
* effective share access for that accessor (READ_WRITE for proxy-write,
1215+
* READ for proxy-read). null means $principalUri is the accessor itself.
1216+
*/
1217+
private function collectDeletedCalendarObjectsForPrincipal(string $principalUri, array &$result, ?int $proxyOverlay): void {
1218+
[$principalUri, $principals] = $this->resolvePrincipal($principalUri);
1219+
1220+
// Owned calendars
11941221
$query = $this->db->getQueryBuilder();
11951222
$query->select(['co.id', 'co.uri', 'co.lastmodified', 'co.etag', 'co.calendarid', 'co.size', 'co.componenttype', 'co.classification', 'co.deleted_at'])
11961223
->selectAlias('c.uri', 'calendaruri')
1224+
->selectAlias('c.principaluri', 'calendarprincipaluri')
11971225
->from('calendarobjects', 'co')
11981226
->join('co', 'calendars', 'c', $query->expr()->eq('c.id', 'co.calendarid', IQueryBuilder::PARAM_INT))
1199-
->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri)))
1227+
->where($query->expr()->eq('c.principaluri', $query->createNamedParameter($principalUri)))
12001228
->andWhere($query->expr()->isNotNull('co.deleted_at'))
12011229
->andWhere($query->expr()->isNull('c.deleted_at'));
12021230
$stmt = $query->executeQuery();
@@ -1218,9 +1246,146 @@ public function getDeletedCalendarObjectsByPrincipal(string $principalUri): arra
12181246
}
12191247
$stmt->closeCursor();
12201248

1249+
// Shared calendars — multiple share rows may match (user + group, etc.),
1250+
// so we dedupe in PHP keeping the most permissive effective access.
1251+
$select = $this->db->getQueryBuilder();
1252+
$select->select(['co.id', 'co.uri', 'co.lastmodified', 'co.etag', 'co.calendarid', 'co.size', 'co.componenttype', 'co.classification', 'co.deleted_at'])
1253+
->selectAlias('c.uri', 'calendaruri')
1254+
->selectAlias('c.principaluri', 'calendarprincipaluri')
1255+
->selectAlias('s.access', 'shareaccess')
1256+
->from('calendarobjects', 'co')
1257+
->join('co', 'calendars', 'c', $select->expr()->eq('c.id', 'co.calendarid', IQueryBuilder::PARAM_INT))
1258+
->andWhere($select->expr()->isNotNull('co.deleted_at'))
1259+
->andWhere($select->expr()->isNull('c.deleted_at'));
1260+
$this->applySharedCalendarFilters($select, $principals, $principalUri);
1261+
1262+
$stmt = $select->executeQuery();
1263+
while ($row = $stmt->fetchAssociative()) {
1264+
$effective = $this->effectiveAccess((int)$row['shareaccess'], $proxyOverlay);
1265+
if ($this->resultHasMorePermissiveEntry($result, $row['id'], $effective)) {
1266+
continue;
1267+
}
1268+
[, $ownerName] = Uri\split($row['calendarprincipaluri']);
1269+
$result[$row['id']] = $this->rowToDeletedCalendarObject($row, $row['calendaruri'] . '_shared_by_' . $ownerName, false, $effective, null);
1270+
}
1271+
$stmt->closeCursor();
1272+
}
1273+
1274+
/**
1275+
* Effective access for an entry surfaced via a proxy delegator.
1276+
* Lower int = more permissive (READ_WRITE=2, READ=3); the more restrictive of
1277+
* the share access and the proxy overlay wins (max of the two ints).
1278+
*/
1279+
private function effectiveAccess(int $shareAccess, ?int $proxyOverlay): int {
1280+
if ($proxyOverlay === null) {
1281+
return $shareAccess;
1282+
}
1283+
return max($shareAccess, $proxyOverlay);
1284+
}
1285+
1286+
/**
1287+
* @param array<int,array<string,mixed>> $result keyed by object id.
1288+
* @param int|string $id the candidate row id.
1289+
* @param int|null $candidateAccess effective access of the candidate row.
1290+
* Owned/no-overlay rows pass null and always win over null entries.
1291+
*/
1292+
private function resultHasMorePermissiveEntry(array $result, int|string $id, ?int $candidateAccess): bool {
1293+
$existing = $result[$id] ?? null;
1294+
if ($existing === null) {
1295+
return false;
1296+
}
1297+
$existingAccess = $existing['shared_access'] ?? null;
1298+
if ($existingAccess === null) {
1299+
// Owned-by-accessor (no overlay) is the most permissive; keep it.
1300+
return true;
1301+
}
1302+
if ($candidateAccess === null) {
1303+
// Candidate is owned-by-accessor; replace.
1304+
return false;
1305+
}
1306+
return $existingAccess <= $candidateAccess;
1307+
}
1308+
1309+
/**
1310+
* Return the principals (users) for whom $principalUri acts as a calendar
1311+
* proxy. The value is true for proxy-write, false for proxy-read.
1312+
*
1313+
* @return array<string,bool> map of delegator-principal => has-write-proxy
1314+
*/
1315+
private function getProxyDelegators(string $principalUri): array {
1316+
$memberships = $this->principalBackend->getGroupMembership($principalUri, true);
1317+
$delegators = [];
1318+
foreach ($memberships as $membership) {
1319+
if (str_ends_with($membership, '/calendar-proxy-write')) {
1320+
$delegator = substr($membership, 0, -strlen('/calendar-proxy-write'));
1321+
$delegators[$delegator] = true;
1322+
} elseif (str_ends_with($membership, '/calendar-proxy-read')) {
1323+
$delegator = substr($membership, 0, -strlen('/calendar-proxy-read'));
1324+
$delegators[$delegator] ??= false;
1325+
}
1326+
}
1327+
return $delegators;
1328+
}
1329+
1330+
private function rowToDeletedCalendarObject(array $row, string $calendarUri, bool $includeData = false, ?int $sharedAccess = null, ?string $delegator = null): array {
1331+
$deletedAt = isset($row['deleted_at']) ? (int)$row['deleted_at'] : null;
1332+
$result = [
1333+
'id' => $row['id'],
1334+
'uri' => $row['uri'],
1335+
'lastmodified' => $row['lastmodified'],
1336+
'etag' => '"' . $row['etag'] . '"',
1337+
'calendarid' => $row['calendarid'],
1338+
'calendaruri' => $calendarUri,
1339+
'sourcecalendaruri' => $row['calendaruri'],
1340+
'calendarprincipaluri' => $row['calendarprincipaluri'],
1341+
'size' => (int)$row['size'],
1342+
'component' => strtolower($row['componenttype']),
1343+
'classification' => (int)$row['classification'],
1344+
'deleted_at' => $deletedAt,
1345+
'delegator' => $delegator,
1346+
'{' . \OCA\DAV\DAV\Sharing\Plugin::NS_NEXTCLOUD . '}deleted-at' => $deletedAt,
1347+
];
1348+
if ($sharedAccess !== null) {
1349+
$result['shared_access'] = $sharedAccess;
1350+
}
1351+
if ($includeData) {
1352+
$result['calendardata'] = $this->readBlob($row['calendardata']);
1353+
}
12211354
return $result;
12221355
}
12231356

1357+
/**
1358+
* Resolve a principal URI into its converted form and all group/circle memberships.
1359+
*
1360+
* @return array{string, string[]} [$convertedUri, $allPrincipals]
1361+
*/
1362+
private function resolvePrincipal(string $principalUri): array {
1363+
$principals = $this->principalBackend->getGroupMembership($principalUri, true);
1364+
$principals = array_merge($principals, $this->principalBackend->getCircleMembership($principalUri));
1365+
$converted = $this->convertPrincipal($principalUri, true);
1366+
$principals[] = $converted;
1367+
return [$converted, $principals];
1368+
}
1369+
1370+
/**
1371+
* Add joins and WHERE conditions to $query to restrict results to calendars
1372+
* shared with any of $principals, excluding calendars explicitly unshared and
1373+
* calendars owned by $principalUri (already covered by the owned query).
1374+
*/
1375+
private function applySharedCalendarFilters(IQueryBuilder $query, array $principals, string $principalUri): void {
1376+
$subSelect = $this->db->getQueryBuilder();
1377+
$subSelect->select('resourceid')
1378+
->from('dav_shares', 'd')
1379+
->where($subSelect->expr()->eq('d.access', $query->createNamedParameter(Backend::ACCESS_UNSHARED, IQueryBuilder::PARAM_INT), IQueryBuilder::PARAM_INT))
1380+
->andWhere($subSelect->expr()->in('d.principaluri', $query->createNamedParameter($principals, IQueryBuilder::PARAM_STR_ARRAY), IQueryBuilder::PARAM_STR_ARRAY));
1381+
1382+
$query->join('c', 'dav_shares', 's', $query->expr()->eq('s.resourceid', 'c.id', IQueryBuilder::PARAM_INT))
1383+
->andWhere($query->expr()->in('s.principaluri', $query->createNamedParameter($principals, IQueryBuilder::PARAM_STR_ARRAY), IQueryBuilder::PARAM_STR_ARRAY))
1384+
->andWhere($query->expr()->eq('s.type', $query->createNamedParameter('calendar', IQueryBuilder::PARAM_STR), IQueryBuilder::PARAM_STR))
1385+
->andWhere($query->expr()->neq('c.principaluri', $query->createNamedParameter($principalUri, IQueryBuilder::PARAM_STR)))
1386+
->andWhere($query->expr()->notIn('c.id', $query->createFunction($subSelect->getSQL()), IQueryBuilder::PARAM_INT_ARRAY));
1387+
}
1388+
12241389
/**
12251390
* Returns information from a single calendar object, based on it's object
12261391
* uri.
@@ -2491,6 +2656,97 @@ public function getCalendarObjectById(string $principalUri, int $id): ?array {
24912656
];
24922657
}
24932658

2659+
/**
2660+
* Return a deleted calendar object by its ID, accessible to $principalUri
2661+
* via ownership, sharing, or proxy delegation. Returns the sharee-facing URI
2662+
* for shared/delegated entries.
2663+
*
2664+
* @param int $id
2665+
* @param string $principalUri
2666+
* @return array|null
2667+
*/
2668+
public function getDeletedCalendarObjectByIdForPrincipal(int $id, string $principalUri): ?array {
2669+
// Visit every accessible path (self + delegators) and keep the most
2670+
// permissive row, so canModify() doesn't get a read-only view when a
2671+
// write path also exists.
2672+
$candidates = [];
2673+
$row = $this->findDeletedCalendarObjectForPrincipal($id, $principalUri, null);
2674+
if ($row !== null) {
2675+
$candidates[$id] = $row;
2676+
}
2677+
foreach ($this->getProxyDelegators($principalUri) as $delegator => $hasProxyWrite) {
2678+
$overlay = $hasProxyWrite ? Backend::ACCESS_READ_WRITE : Backend::ACCESS_READ;
2679+
$row = $this->findDeletedCalendarObjectForPrincipal($id, $delegator, $overlay);
2680+
if ($row === null) {
2681+
continue;
2682+
}
2683+
if ($this->resultHasMorePermissiveEntry($candidates, $id, $row['shared_access'] ?? null)) {
2684+
continue;
2685+
}
2686+
$candidates[$id] = $row;
2687+
}
2688+
return $candidates[$id] ?? null;
2689+
}
2690+
2691+
/**
2692+
* Look up a single deleted calendar object by id for $principalUri.
2693+
*
2694+
* @param int $id
2695+
* @param string $principalUri
2696+
* @param int|null $proxyOverlay see collectDeletedCalendarObjectsForPrincipal.
2697+
* @return array|null
2698+
*/
2699+
private function findDeletedCalendarObjectForPrincipal(int $id, string $principalUri, ?int $proxyOverlay): ?array {
2700+
[$principalUri, $principals] = $this->resolvePrincipal($principalUri);
2701+
2702+
// Check owned calendars first
2703+
$query = $this->db->getQueryBuilder();
2704+
$query->select(['co.id', 'co.uri', 'co.lastmodified', 'co.etag', 'co.calendarid', 'co.size', 'co.calendardata', 'co.componenttype', 'co.classification', 'co.deleted_at'])
2705+
->selectAlias('c.uri', 'calendaruri')
2706+
->selectAlias('c.principaluri', 'calendarprincipaluri')
2707+
->from('calendarobjects', 'co')
2708+
->join('co', 'calendars', 'c', $query->expr()->eq('c.id', 'co.calendarid', IQueryBuilder::PARAM_INT))
2709+
->where($query->expr()->eq('co.id', $query->createNamedParameter($id, IQueryBuilder::PARAM_INT), IQueryBuilder::PARAM_INT))
2710+
->andWhere($query->expr()->eq('c.principaluri', $query->createNamedParameter($principalUri)))
2711+
->andWhere($query->expr()->isNotNull('co.deleted_at'));
2712+
$stmt = $query->executeQuery();
2713+
$row = $stmt->fetchAssociative();
2714+
$stmt->closeCursor();
2715+
2716+
if ($row) {
2717+
[, $ownerName] = Uri\split($row['calendarprincipaluri']);
2718+
$isDelegated = $proxyOverlay !== null ;
2719+
$calendarUri = $isDelegated ? $row['calendaruri'] . '_delegated_by_' . $ownerName : $row['calendaruri'];
2720+
return $this->rowToDeletedCalendarObject($row, $calendarUri, true, $proxyOverlay, $isDelegated ? $principalUri : null);
2721+
}
2722+
2723+
// Check shared calendars; order by access ASC so the most permissive
2724+
// row wins when the principal matches multiple share entries.
2725+
$select = $this->db->getQueryBuilder();
2726+
$select->select(['co.id', 'co.uri', 'co.lastmodified', 'co.etag', 'co.calendarid', 'co.size', 'co.calendardata', 'co.componenttype', 'co.classification', 'co.deleted_at'])
2727+
->selectAlias('c.uri', 'calendaruri')
2728+
->selectAlias('c.principaluri', 'calendarprincipaluri')
2729+
->selectAlias('s.access', 'shareaccess')
2730+
->from('calendarobjects', 'co')
2731+
->join('co', 'calendars', 'c', $select->expr()->eq('c.id', 'co.calendarid', IQueryBuilder::PARAM_INT))
2732+
->andWhere($select->expr()->eq('co.id', $select->createNamedParameter($id, IQueryBuilder::PARAM_INT), IQueryBuilder::PARAM_INT))
2733+
->andWhere($select->expr()->isNotNull('co.deleted_at'))
2734+
->orderBy('s.access', 'ASC');
2735+
$this->applySharedCalendarFilters($select, $principals, $principalUri);
2736+
2737+
$stmt = $select->executeQuery();
2738+
$row = $stmt->fetchAssociative();
2739+
$stmt->closeCursor();
2740+
2741+
if (!$row) {
2742+
return null;
2743+
}
2744+
2745+
$effective = $this->effectiveAccess((int)$row['shareaccess'], $proxyOverlay);
2746+
[, $ownerName] = Uri\split($row['calendarprincipaluri']);
2747+
return $this->rowToDeletedCalendarObject($row, $row['calendaruri'] . '_shared_by_' . $ownerName, true, $effective, null);
2748+
}
2749+
24942750
/**
24952751
* The getChanges method returns all the changes that have happened, since
24962752
* the specified syncToken in the specified calendar.

apps/dav/lib/CalDAV/Trashbin/DeletedCalendarObject.php

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
use OCA\DAV\CalDAV\CalDavBackend;
1212
use OCA\DAV\CalDAV\IRestorable;
13+
use OCA\DAV\DAV\Sharing\Backend;
1314
use Sabre\CalDAV\ICalendarObject;
1415
use Sabre\DAV\Exception\Forbidden;
1516
use Sabre\DAVACL\ACLTrait;
@@ -28,6 +29,9 @@ public function __construct(
2829
}
2930

3031
public function delete() {
32+
if (!$this->canModify()) {
33+
throw new Forbidden('Read-only sharees cannot permanently delete trashbin entries');
34+
}
3135
$this->calDavBackend->deleteCalendarObject(
3236
$this->objectData['calendarid'],
3337
$this->objectData['uri'],
@@ -74,6 +78,9 @@ public function getSize() {
7478
}
7579

7680
public function restore(): void {
81+
if (!$this->canModify()) {
82+
throw new Forbidden('Read-only sharees cannot restore trashbin entries');
83+
}
7784
$this->calDavBackend->restoreCalendarObject($this->objectData);
7885
}
7986

@@ -86,19 +93,14 @@ public function getCalendarUri(): string {
8693
}
8794

8895
public function getACL(): array {
89-
return [
96+
$acl = [
9097
[
9198
'privilege' => '{DAV:}read', // For queries
9299
'principal' => $this->getOwner(),
93100
'protected' => true,
94101
],
95102
[
96-
'privilege' => '{DAV:}unbind', // For moving and deletion
97-
'principal' => $this->getOwner(),
98-
'protected' => true,
99-
],
100-
[
101-
'privilege' => '{DAV:}all',
103+
'privilege' => '{DAV:}read',
102104
'principal' => $this->getOwner() . '/calendar-proxy-write',
103105
'protected' => true,
104106
],
@@ -108,6 +110,23 @@ public function getACL(): array {
108110
'protected' => true,
109111
],
110112
];
113+
114+
if ($this->canModify()) {
115+
$acl[] = [
116+
'privilege' => '{DAV:}unbind',
117+
'principal' => $this->getOwner(),
118+
'protected' => true,
119+
];
120+
121+
$acl[] = [
122+
'privilege' => '{DAV:}unbind',
123+
'principal' => $this->getOwner() . '/calendar-proxy-write',
124+
'protected' => true,
125+
];
126+
127+
}
128+
129+
return $acl;
111130
}
112131

113132
public function getOwner() {

0 commit comments

Comments
 (0)