From 0788ebf1cb8bb87e76a6e6056ac3b525dc31140c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Pablo=20Villaf=C3=A1=C3=B1ez?= Date: Mon, 7 Aug 2017 10:43:48 +0200 Subject: [PATCH 1/2] Check if the file exists before trying to download it --- apps/dav/lib/Connector/Sabre/File.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/apps/dav/lib/Connector/Sabre/File.php b/apps/dav/lib/Connector/Sabre/File.php index 1dc69ed80a84..fdbcdf3763b0 100644 --- a/apps/dav/lib/Connector/Sabre/File.php +++ b/apps/dav/lib/Connector/Sabre/File.php @@ -331,11 +331,12 @@ private function emitPostHooks($exists, $path = null) { public function get() { //throw exception if encryption is disabled but files are still encrypted try { - if (!$this->info->isReadable()) { + $viewPath = ltrim($this->path, '/'); + if (!$this->info->isReadable() || !$this->fileView->file_exists($viewPath)) { // do a if the file did not exist throw new NotFound(); } - $res = $this->fileView->fopen(ltrim($this->path, '/'), 'rb'); + $res = $this->fileView->fopen($viewPath, 'rb'); if ($res === false) { throw new ServiceUnavailable("Could not open file"); } From 5872861d9d8bffdf2f31e4a9d23df66cc093e90b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Pablo=20Villaf=C3=A1=C3=B1ez?= Date: Mon, 7 Aug 2017 12:59:52 +0200 Subject: [PATCH 2/2] update unittest to match the changes --- .../tests/unit/Connector/Sabre/FileTest.php | 60 +++++++++++++++++-- 1 file changed, 55 insertions(+), 5 deletions(-) diff --git a/apps/dav/tests/unit/Connector/Sabre/FileTest.php b/apps/dav/tests/unit/Connector/Sabre/FileTest.php index 5954cb66a11d..374ca20f3cc6 100644 --- a/apps/dav/tests/unit/Connector/Sabre/FileTest.php +++ b/apps/dav/tests/unit/Connector/Sabre/FileTest.php @@ -351,7 +351,7 @@ private function supportsNegativeMtime() { public function legalMtimeProvider() { return [ - "string" => [ + "string" => [ 'HTTP_X_OC_MTIME' => "string", 'expected result' => 0 ], @@ -359,7 +359,7 @@ public function legalMtimeProvider() { 'HTTP_X_OC_MTIME' => "34", 'expected result' => 34 ], - "castable string (float)" => [ + "castable string (float)" => [ 'HTTP_X_OC_MTIME' => "34.56", 'expected result' => 34 ], @@ -1079,7 +1079,7 @@ private function listPartFiles(View $userView = null, $path = '') { /** * returns an array of file information filesize, mtime, filetype, mimetype - * + * * @param string $path * @param View $userView * @return array @@ -1101,11 +1101,14 @@ private function getFileInfos($path = '', View $userView = null) { */ public function testGetFopenFails() { $view = $this->getMockBuilder(View::class) - ->setMethods(['fopen']) + ->setMethods(['fopen', 'file_exists']) ->getMock(); $view->expects($this->atLeastOnce()) ->method('fopen') ->will($this->returnValue(false)); + $view->expects($this->atLeastOnce()) + ->method('file_exists') + ->will($this->returnValue(true)); $info = new FileInfo('/test.txt', $this->getMockStorage(), null, [ 'permissions' => Constants::PERMISSION_ALL @@ -1121,11 +1124,14 @@ public function testGetFopenFails() { */ public function testGetFopenThrows() { $view = $this->getMockBuilder(View::class) - ->setMethods(['fopen']) + ->setMethods(['fopen', 'file_exists']) ->getMock(); $view->expects($this->atLeastOnce()) ->method('fopen') ->willThrowException(new ForbiddenException('', true)); + $view->expects($this->atLeastOnce()) + ->method('file_exists') + ->will($this->returnValue(true)); $info = new FileInfo('/test.txt', $this->getMockStorage(), null, [ 'permissions' => Constants::PERMISSION_ALL @@ -1154,4 +1160,48 @@ public function testGetThrowsIfNoPermission() { $file->get(); } + + /** + * @expectedException \Sabre\DAV\Exception\NotFound + */ + public function testGetThrowsIfFileNotExists() { + $view = $this->getMockBuilder(View::class) + ->setMethods(['fopen', 'file_exists']) + ->getMock(); + $view->expects($this->never()) + ->method('fopen'); + $view->expects($this->atLeastOnce()) + ->method('file_exists') + ->will($this->returnValue(false)); + + $info = new FileInfo('/test.txt', $this->getMockStorage(), null, [ + 'permissions' => Constants::PERMISSION_ALL + ], null); + + $file = new File($view, $info); + + $file->get(); + } + + /** + * @expectedException \Sabre\DAV\Exception\NotFound + */ + public function testGetThrowsIfNoPermissionsAndFileNotExists() { + $view = $this->getMockBuilder(View::class) + ->setMethods(['fopen', 'file_exists']) + ->getMock(); + $view->expects($this->never()) + ->method('fopen'); + $view->expects($this->any()) + ->method('file_exists') + ->will($this->returnValue(false)); + + $info = new FileInfo('/test.txt', $this->getMockStorage(), null, [ + 'permissions' => Constants::PERMISSION_CREATE + ], null); + + $file = new File($view, $info); + + $file->get(); + } }