From 2aa0b885f622af21f80f6a36e744d4ecf74fdfff Mon Sep 17 00:00:00 2001
From: Roeland Jago Douma <rullzer@owncloud.com>
Date: Tue, 16 Feb 2016 16:04:17 +0100
Subject: [PATCH] OCS Share API should not return invalid shares

Since we have lazy shares it can happen that a share is actually
invalid. See https://github.com/owncloud/core/issues/20908

This add checks for the get methods to handle the NotFound exception.
---
 apps/files_sharing/api/share20ocs.php         | 32 +++++++++++++++----
 .../tests/api/share20ocstest.php              |  6 ++++
 2 files changed, 31 insertions(+), 7 deletions(-)

diff --git a/apps/files_sharing/api/share20ocs.php b/apps/files_sharing/api/share20ocs.php
index 688ed5b3ee5..309e1159fff 100644
--- a/apps/files_sharing/api/share20ocs.php
+++ b/apps/files_sharing/api/share20ocs.php
@@ -20,6 +20,7 @@
  */
 namespace OCA\Files_Sharing\API;
 
+use OCP\Files\NotFoundException;
 use OCP\IGroupManager;
 use OCP\IUserManager;
 use OCP\IRequest;
@@ -83,6 +84,7 @@ class Share20OCS {
 	 *
 	 * @param \OCP\Share\IShare $share
 	 * @return array
+	 * @throws NotFoundException In case the node can't be resolved.
 	 */
 	protected function formatShare(\OCP\Share\IShare $share) {
 		$sharedBy = $this->userManager->get($share->getSharedBy());
@@ -177,11 +179,15 @@ class Share20OCS {
 		}
 
 		if ($this->canAccessShare($share)) {
-			$share = $this->formatShare($share);
-			return new \OC_OCS_Result([$share]);
-		} else {
-			return new \OC_OCS_Result(null, 404, 'wrong share ID, share doesn\'t exist.');
+			try {
+				$share = $this->formatShare($share);
+				return new \OC_OCS_Result([$share]);
+			} catch (NotFoundException $e) {
+				//Fall trough
+			}
 		}
+
+		return new \OC_OCS_Result(null, 404, 'wrong share ID, share doesn\'t exist.');
 	}
 
 	/**
@@ -368,7 +374,11 @@ class Share20OCS {
 		$formatted = [];
 		foreach ($shares as $share) {
 			if ($this->canAccessShare($share)) {
-				$formatted[] = $this->formatShare($share);
+				try {
+					$formatted[] = $this->formatShare($share);
+				} catch (NotFoundException $e) {
+					// Ignore this share
+				}
 			}
 		}
 
@@ -398,7 +408,11 @@ class Share20OCS {
 
 		$formatted = [];
 		foreach ($shares as $share) {
-			$formatted[] = $this->formatShare($share);
+			try {
+				$formatted[] = $this->formatShare($share);
+			} catch (NotFoundException $e) {
+				//Ignore this share
+			}
 		}
 
 		return new \OC_OCS_Result($formatted);
@@ -458,7 +472,11 @@ class Share20OCS {
 
 		$formatted = [];
 		foreach ($shares as $share) {
-			$formatted[] = $this->formatShare($share);
+			try {
+				$formatted[] = $this->formatShare($share);
+			} catch (NotFoundException $e) {
+				//Ignore share
+			}
 		}
 
 		return new \OC_OCS_Result($formatted);
diff --git a/apps/files_sharing/tests/api/share20ocstest.php b/apps/files_sharing/tests/api/share20ocstest.php
index a1094ce4b22..736bf89e683 100644
--- a/apps/files_sharing/tests/api/share20ocstest.php
+++ b/apps/files_sharing/tests/api/share20ocstest.php
@@ -28,6 +28,12 @@ use OCP\IURLGenerator;
 use OCP\IUser;
 use OCP\Files\IRootFolder;
 
+/**
+ * Class Share20OCSTest
+ *
+ * @package OCA\Files_Sharing\Tests\API
+ * @group DB
+ */
 class Share20OCSTest extends \Test\TestCase {
 
 	/** @var \OC\Share20\Manager | \PHPUnit_Framework_MockObject_MockObject */
-- 
GitLab