From fea71a81fd7b10758c3258a629f7f8185874f1be Mon Sep 17 00:00:00 2001 From: Roeland Jago Douma Date: Fri, 26 Jun 2015 15:36:06 +0200 Subject: [PATCH 1/2] Cannot share the same file multiple times with remote user Fix for #17183 It should not be possible to create multiple remote shares from user A to user B of the same file/folder. --- lib/private/share/share.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/lib/private/share/share.php b/lib/private/share/share.php index 7fcbb695c68..a52adb8758e 100644 --- a/lib/private/share/share.php +++ b/lib/private/share/share.php @@ -781,6 +781,19 @@ class Share extends Constants { \OCP\Util::writeLog('OCP\Share', sprintf($message, $itemSourceName), \OCP\Util::ERROR); throw new \Exception($message_t); } else if ($shareType === self::SHARE_TYPE_REMOTE) { + + /* + * Check if file is not already shared with the remote user + */ + if ($checkExists = self::getItems($itemType, $itemSource, self::SHARE_TYPE_REMOTE, + $shareWith, $uidOwner, self::FORMAT_NONE, null, 1, true, true)) { + $message = 'Sharing %s failed, because this item is already shared with %s'; + $message_t = $l->t('Sharing %s failed, because this item is already shared with %s', array($itemSourceName, $shareWith)); + \OCP\Util::writeLog('OCP\Share', sprintf($message, $itemSourceName, $shareWith), \OCP\Util::ERROR); + throw new \Exception($message_t); + } + + $token = \OC::$server->getSecureRandom()->getMediumStrengthGenerator()->generate(self::TOKEN_LENGTH, \OCP\Security\ISecureRandom::CHAR_LOWER . \OCP\Security\ISecureRandom::CHAR_UPPER . \OCP\Security\ISecureRandom::CHAR_DIGITS); From e35b97e4c5dd87dd03e409db0e20cd6c844d966b Mon Sep 17 00:00:00 2001 From: Roeland Jago Douma Date: Wed, 15 Jul 2015 12:49:26 +0200 Subject: [PATCH 2/2] Added test --- tests/lib/share/share.php | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/tests/lib/share/share.php b/tests/lib/share/share.php index e225f2a4002..7cfd3e1942c 100644 --- a/tests/lib/share/share.php +++ b/tests/lib/share/share.php @@ -1531,6 +1531,42 @@ class Test_Share extends \Test\TestCase { \OC\Share\Share::setPassword($userSession, $connection, $config, 1, 'pass'); } + /** + * Make sure that a user cannot have multiple identical shares to remote users + */ + public function testOnlyOneRemoteShare() { + $oldHttpHelper = \OC::$server->query('HTTPHelper'); + $httpHelperMock = $this->getMockBuilder('OC\HttpHelper') + ->disableOriginalConstructor() + ->getMock(); + $this->setHttpHelper($httpHelperMock); + + $httpHelperMock->expects($this->at(0)) + ->method('post') + ->with($this->stringStartsWith('https://localhost/ocs/v1.php/cloud/shares'), $this->anything()) + ->willReturn(['success' => true, 'result' => json_encode(['ocs' => ['meta' => ['statuscode' => 100]]])]); + + \OCP\Share::shareItem('test', 'test.txt', \OCP\Share::SHARE_TYPE_REMOTE, 'foo@localhost', \OCP\Constants::PERMISSION_READ); + $shares = \OCP\Share::getItemShared('test', 'test.txt'); + $share = array_shift($shares); + + //Try share again + try { + \OCP\Share::shareItem('test', 'test.txt', \OCP\Share::SHARE_TYPE_REMOTE, 'foo@localhost', \OCP\Constants::PERMISSION_READ); + $this->fail('Identical remote shares are not allowed'); + } catch (\Exception $e) { + $this->assertEquals('Sharing test.txt failed, because this item is already shared with foo@localhost', $e->getMessage()); + } + + $httpHelperMock->expects($this->at(0)) + ->method('post') + ->with($this->stringStartsWith('https://localhost/ocs/v1.php/cloud/shares/' . $share['id'] . '/unshare'), $this->anything()) + ->willReturn(['success' => true, 'result' => json_encode(['ocs' => ['meta' => ['statuscode' => 100]]])]); + + \OCP\Share::unshare('test', 'test.txt', \OCP\Share::SHARE_TYPE_REMOTE, 'foo@localhost'); + $this->setHttpHelper($oldHttpHelper); + } + } class DummyShareClass extends \OC\Share\Share {