diff options
author | Lukas Reschke <lukas@owncloud.com> | 2015-06-15 14:10:10 +0200 |
---|---|---|
committer | Lukas Reschke <lukas@owncloud.com> | 2015-10-25 17:58:21 +0100 |
commit | 13e817e90166d85dbea98043583185778d192212 (patch) | |
tree | e6cf6a7ba301b838a2ded0d3e7b9d81e2c655cfc /apps/files_versions | |
parent | eb10e3abc2cf5d325bc0cd6fecaa64e7665d4b4a (diff) | |
download | nextcloud-server-13e817e90166d85dbea98043583185778d192212.tar.gz nextcloud-server-13e817e90166d85dbea98043583185778d192212.zip |
Throw exception on `getPath` if file does not exist
Currently the `getPath` methods returned `NULL` in case when a file with the specified ID does not exist. This however mandates that developers are checking for the `NULL` case and if they do not the door for bugs with all kind of impact is widely opened.
This is especially harmful if used in context with Views where the final result is limited based on the result of `getPath`, if `getPath` returns `NULL` PHP type juggles this to an empty string resulting in all possible kind of bugs.
While one could argue that this is a misusage of the API the fact is that it is very often misused and an exception will trigger an immediate stop of execution as well as log this behaviour and show a pretty error page.
I also adjusted some usages where I believe that we need to catch these errors, in most cases this is though simply an error that should hard-fail.
Diffstat (limited to 'apps/files_versions')
-rw-r--r-- | apps/files_versions/lib/storage.php | 14 | ||||
-rw-r--r-- | apps/files_versions/tests/versions.php | 7 |
2 files changed, 17 insertions, 4 deletions
diff --git a/apps/files_versions/lib/storage.php b/apps/files_versions/lib/storage.php index dd8af1b8d18..6737bf20f90 100644 --- a/apps/files_versions/lib/storage.php +++ b/apps/files_versions/lib/storage.php @@ -44,6 +44,7 @@ namespace OCA\Files_Versions; use OCA\Files_Versions\AppInfo\Application; use OCA\Files_Versions\Command\Expire; use OCP\Lock\ILockingProvider; +use OCP\Files\NotFoundException; class Storage { @@ -74,15 +75,24 @@ class Storage { /** @var \OCA\Files_Versions\AppInfo\Application */ private static $application; + /** + * @param string $filename + * @return array + * @throws \OC\User\NoUserException + */ public static function getUidAndFilename($filename) { $uid = \OC\Files\Filesystem::getOwner($filename); \OC\Files\Filesystem::initMountPoints($uid); if ( $uid != \OCP\User::getUser() ) { $info = \OC\Files\Filesystem::getFileInfo($filename); $ownerView = new \OC\Files\View('/'.$uid.'/files'); - $filename = $ownerView->getPath($info['fileid']); + try { + $filename = $ownerView->getPath($info['fileid']); + } catch (NotFoundException $e) { + $filename = null; + } } - return array($uid, $filename); + return [$uid, $filename]; } /** diff --git a/apps/files_versions/tests/versions.php b/apps/files_versions/tests/versions.php index 2979de2ac98..b9bc0932a84 100644 --- a/apps/files_versions/tests/versions.php +++ b/apps/files_versions/tests/versions.php @@ -759,7 +759,11 @@ class Test_Files_Versioning extends \Test\TestCase { ); } - private function createAndCheckVersions($view, $path) { + /** + * @param \OC\Files\View $view + * @param string $path + */ + private function createAndCheckVersions(\OC\Files\View $view, $path) { $view->file_put_contents($path, 'test file'); $view->file_put_contents($path, 'version 1'); $view->file_put_contents($path, 'version 2'); @@ -782,7 +786,6 @@ class Test_Files_Versioning extends \Test\TestCase { /** * @param string $user * @param bool $create - * @param bool $password */ public static function loginHelper($user, $create = false) { |