X Tutup
Skip to content

Commit 1174638

Browse files
committed
validate reshare attributes based on supershare
1 parent 3024c23 commit 1174638

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-0
lines changed

lib/private/Share20/Manager.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,9 @@ protected function validatePermissions(IShare $share) {
322322
/* Use share node permission as default $maxPermissions */
323323
$maxPermissions = $shareNode->getPermissions();
324324

325+
/* Attributes default is null, as attributes are restricted only in reshare */
326+
$maxAttributes = null;
327+
325328
/*
326329
* Quick fix for #23536
327330
* Non moveable mount points do not have update and delete permissions
@@ -352,6 +355,7 @@ protected function validatePermissions(IShare $share) {
352355
'@phan-var \OCA\Files_Sharing\SharedStorage $shareFileStorage';
353356
$parentShare = $shareFileStorage->getShare();
354357
$maxPermissions = $parentShare->getPermissions();
358+
$maxAttributes = $parentShare->getAttributes();
355359
}
356360
}
357361
}
@@ -361,6 +365,11 @@ protected function validatePermissions(IShare $share) {
361365
$message_t = $this->l->t('Cannot increase permissions of %s', [$share->getNode()->getPath()]);
362366
throw new GenericShareException($message_t, $message_t, 404);
363367
}
368+
369+
if ($maxAttributes !== null && !$this->strictSubsetOfAttributes($maxAttributes, $share->getAttributes())) {
370+
$message_t = $this->l->t('Cannot set attributes of %s', [$share->getNode()->getPath()]);
371+
throw new GenericShareException($message_t, $message_t, 404);
372+
}
364373
}
365374

366375
/**
@@ -1666,4 +1675,29 @@ private function isNewShare(IShare $share) {
16661675
private function strictSubsetOfPermissions($allowedPermissions, $newPermissions) {
16671676
return (($allowedPermissions | $newPermissions) === $allowedPermissions);
16681677
}
1678+
1679+
/**
1680+
* Check $newAttributes attribute is a subset of $allowedAttributes
1681+
*
1682+
* @param IAttributes $allowedAttributes
1683+
* @param IAttributes $newAttributes
1684+
* @return boolean ,true if $allowedAttributes enabled is super set of $newAttributes enabled, else false
1685+
*/
1686+
private function strictSubsetOfAttributes($allowedAttributes, $newAttributes) {
1687+
if ((!$allowedAttributes || empty($allowedAttributes->toArray()))
1688+
&& (!$newAttributes || empty($newAttributes->toArray()))) {
1689+
return true;
1690+
}
1691+
1692+
foreach ($allowedAttributes->toArray() as $allowedAttribute) {
1693+
$enabled = $newAttributes->getAttribute($allowedAttribute['scope'], $allowedAttribute['key']);
1694+
1695+
if (($allowedAttribute['enabled'] === $enabled) ||
1696+
($allowedAttribute['enabled'] === true && $enabled === false)) {
1697+
// set to the same value or disabling attribute
1698+
return true;
1699+
}
1700+
}
1701+
return false;
1702+
}
16691703
}

tests/lib/Share20/ManagerTest.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
use OCP\Security\IHasher;
4343
use OCP\Security\ISecureRandom;
4444
use OCP\Share\Exceptions\ShareNotFound;
45+
use OCP\Share\IAttributes;
4546
use OCP\Share\IProviderFactory;
4647
use OCP\Share\IShare;
4748
use OCP\Share\IShareProvider;
@@ -3494,6 +3495,70 @@ public function testGetAllSharedWith() {
34943495
$this->assertSame($shares, [$share1, $share2]);
34953496
}
34963497

3498+
/**
3499+
* @dataProvider strictSubsetOfAttributesDataProvider
3500+
*
3501+
* @param IAttributes $allowedAttributes
3502+
* @param IAttributes $newAttributes
3503+
* @param boolean $expected
3504+
*/
3505+
public function testStrictSubsetOfAttributes($allowedAttributes, $newAttributes, $expected) {
3506+
$this->assertEquals(
3507+
$expected,
3508+
$this->invokePrivate(
3509+
$this->manager,
3510+
'strictSubsetOfAttributes',
3511+
[$allowedAttributes, $newAttributes]
3512+
)
3513+
);
3514+
}
3515+
3516+
public function strictSubsetOfAttributesDataProvider() {
3517+
$providedValues = [];
3518+
3519+
// removal of attributes should result in false
3520+
$allowedAttributes = $this->createMock(IAttributes::class);
3521+
$newAttributes = $this->createMock(IAttributes::class);
3522+
$allowedAttributes->method('toArray')->willReturn([
3523+
['scope' => 'app1', 'key' => 'perm1', 'enabled' => false]
3524+
]);
3525+
$newAttributes->expects($this->at(0))
3526+
->method('getAttribute')->with('app1', 'perm1')->will($this->returnValue(null));
3527+
$providedValues[] = [$allowedAttributes, $newAttributes, false];
3528+
3529+
// increase of attributes should result in false
3530+
$allowedAttributes = $this->createMock(IAttributes::class);
3531+
$newAttributes = $this->createMock(IAttributes::class);
3532+
$allowedAttributes->method('toArray')->willReturn([
3533+
['scope' => 'app1', 'key' => 'perm1', 'enabled' => false]
3534+
]);
3535+
$newAttributes->expects($this->at(0))
3536+
->method('getAttribute')->with('app1', 'perm1')->will($this->returnValue(true));
3537+
$providedValues[] = [$allowedAttributes, $newAttributes, false];
3538+
3539+
// no change of attributes should result in true
3540+
$allowedAttributes = $this->createMock(IAttributes::class);
3541+
$newAttributes = $this->createMock(IAttributes::class);
3542+
$allowedAttributes->method('toArray')->willReturn([
3543+
['scope' => 'app1', 'key' => 'perm1', 'enabled' => true]
3544+
]);
3545+
$newAttributes->expects($this->at(0))
3546+
->method('getAttribute')->with('app1', 'perm1')->will($this->returnValue(true));
3547+
$providedValues[] = [$allowedAttributes, $newAttributes, true];
3548+
3549+
// decrease of attributes should result in true
3550+
$allowedAttributes = $this->createMock(IAttributes::class);
3551+
$newAttributes = $this->createMock(IAttributes::class);
3552+
$allowedAttributes->method('toArray')->willReturn([
3553+
['scope' => 'app1', 'key' => 'perm1', 'enabled' => true]
3554+
]);
3555+
$newAttributes->expects($this->at(0))
3556+
->method('getAttribute')->with('app1', 'perm1')->will($this->returnValue(false));
3557+
$providedValues[] = [$allowedAttributes, $newAttributes, true];
3558+
3559+
return $providedValues;
3560+
}
3561+
34973562
/**
34983563
* @dataProvider strictSubsetOfPermissionsDataProvider
34993564
*

0 commit comments

Comments
 (0)
X Tutup