From cf9dbc6e349bc287a414547944fd2a6dff383d64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20M=C3=BCller?= Date: Tue, 24 Sep 2013 14:25:56 +0200 Subject: [PATCH 1/8] adding error handling on file_put_contents within the web dav implementation --- lib/connector/sabre/directory.php | 7 ++++++- lib/connector/sabre/file.php | 7 ++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/lib/connector/sabre/directory.php b/lib/connector/sabre/directory.php index 3181a4b310f..0717d952268 100644 --- a/lib/connector/sabre/directory.php +++ b/lib/connector/sabre/directory.php @@ -72,7 +72,12 @@ class OC_Connector_Sabre_Directory extends OC_Connector_Sabre_Node implements Sa // mark file as partial while uploading (ignored by the scanner) $partpath = $newPath . '.part'; - \OC\Files\Filesystem::file_put_contents($partpath, $data); + $putOkay = \OC\Files\Filesystem::file_put_contents($partpath, $data); + if ($putOkay === false) { + \OC_Log::write('webdav', '\OC\Files\Filesystem::file_put_contents() failed', \OC_Log::ERROR); + \OC\Files\Filesystem::unlink($partpath); + throw new Sabre_DAV_Exception(); + } //detect aborted upload if (isset ($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'PUT' ) { diff --git a/lib/connector/sabre/file.php b/lib/connector/sabre/file.php index 61bdcd5e0ae..57ba94c7b19 100644 --- a/lib/connector/sabre/file.php +++ b/lib/connector/sabre/file.php @@ -58,7 +58,12 @@ class OC_Connector_Sabre_File extends OC_Connector_Sabre_Node implements Sabre_D // mark file as partial while uploading (ignored by the scanner) $partpath = $this->path . '.part'; - \OC\Files\Filesystem::file_put_contents($partpath, $data); + $putOkay = \OC\Files\Filesystem::file_put_contents($partpath, $data); + if ($putOkay === false) { + \OC_Log::write('webdav', '\OC\Files\Filesystem::file_put_contents() failed', \OC_Log::ERROR); + \OC\Files\Filesystem::unlink($partpath); + throw new Sabre_DAV_Exception(); + } //detect aborted upload if (isset ($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'PUT') { From 4e7f82ef04b3713c71b4c2a2732892490e42e0d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20M=C3=BCller?= Date: Tue, 24 Sep 2013 15:14:42 +0200 Subject: [PATCH 2/8] unify duplicate code --- lib/connector/sabre/directory.php | 56 ++----------------------------- lib/connector/sabre/file.php | 32 ++++++++++++++---- 2 files changed, 29 insertions(+), 59 deletions(-) diff --git a/lib/connector/sabre/directory.php b/lib/connector/sabre/directory.php index 0717d952268..fa20a60f3a5 100644 --- a/lib/connector/sabre/directory.php +++ b/lib/connector/sabre/directory.php @@ -54,59 +54,9 @@ class OC_Connector_Sabre_Directory extends OC_Connector_Sabre_Node implements Sa throw new \Sabre_DAV_Exception_Forbidden(); } - if (isset($_SERVER['HTTP_OC_CHUNKED'])) { - $info = OC_FileChunking::decodeName($name); - if (empty($info)) { - throw new Sabre_DAV_Exception_NotImplemented(); - } - $chunk_handler = new OC_FileChunking($info); - $chunk_handler->store($info['index'], $data); - if ($chunk_handler->isComplete()) { - $newPath = $this->path . '/' . $info['name']; - $chunk_handler->file_assemble($newPath); - return OC_Connector_Sabre_Node::getETagPropertyForPath($newPath); - } - } else { - $newPath = $this->path . '/' . $name; - - // mark file as partial while uploading (ignored by the scanner) - $partpath = $newPath . '.part'; - - $putOkay = \OC\Files\Filesystem::file_put_contents($partpath, $data); - if ($putOkay === false) { - \OC_Log::write('webdav', '\OC\Files\Filesystem::file_put_contents() failed', \OC_Log::ERROR); - \OC\Files\Filesystem::unlink($partpath); - throw new Sabre_DAV_Exception(); - } - - //detect aborted upload - if (isset ($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'PUT' ) { - if (isset($_SERVER['CONTENT_LENGTH'])) { - $expected = $_SERVER['CONTENT_LENGTH']; - $actual = \OC\Files\Filesystem::filesize($partpath); - if ($actual != $expected) { - \OC\Files\Filesystem::unlink($partpath); - throw new Sabre_DAV_Exception_BadRequest( - 'expected filesize ' . $expected . ' got ' . $actual); - } - } - } - - // rename to correct path - \OC\Files\Filesystem::rename($partpath, $newPath); - - // allow sync clients to send the mtime along in a header - $mtime = OC_Request::hasModificationTime(); - if ($mtime !== false) { - if(\OC\Files\Filesystem::touch($newPath, $mtime)) { - header('X-OC-MTime: accepted'); - } - } - - return OC_Connector_Sabre_Node::getETagPropertyForPath($newPath); - } - - return null; + $path = $this->path . '/' . $name; + $node = new OC_Connector_Sabre_File($path); + return $node->put($data); } /** diff --git a/lib/connector/sabre/file.php b/lib/connector/sabre/file.php index 57ba94c7b19..aa71b8be217 100644 --- a/lib/connector/sabre/file.php +++ b/lib/connector/sabre/file.php @@ -46,7 +46,8 @@ class OC_Connector_Sabre_File extends OC_Connector_Sabre_Node implements Sabre_D */ public function put($data) { - if (!\OC\Files\Filesystem::isUpdatable($this->path)) { + if (\OC\Files\Filesystem::file_exists($this->path) && + !\OC\Files\Filesystem::isUpdatable($this->path)) { throw new \Sabre_DAV_Exception_Forbidden(); } @@ -54,7 +55,26 @@ class OC_Connector_Sabre_File extends OC_Connector_Sabre_Node implements Sabre_D if (\OC_Util::encryptedFiles()) { throw new \Sabre_DAV_Exception_ServiceUnavailable(); } - + + // chunked handling + if (isset($_SERVER['HTTP_OC_CHUNKED'])) { + list(, $name) = \Sabre_DAV_URLUtil::splitPath($this->path); + + $info = OC_FileChunking::decodeName($name); + if (empty($info)) { + throw new Sabre_DAV_Exception_NotImplemented(); + } + $chunk_handler = new OC_FileChunking($info); + $chunk_handler->store($info['index'], $data); + if ($chunk_handler->isComplete()) { + $newPath = $this->path . '/' . $info['name']; + $chunk_handler->file_assemble($newPath); + return OC_Connector_Sabre_Node::getETagPropertyForPath($newPath); + } + + return null; + } + // mark file as partial while uploading (ignored by the scanner) $partpath = $this->path . '.part'; @@ -66,7 +86,7 @@ class OC_Connector_Sabre_File extends OC_Connector_Sabre_Node implements Sabre_D } //detect aborted upload - if (isset ($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'PUT') { + if (isset ($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'PUT' ) { if (isset($_SERVER['CONTENT_LENGTH'])) { $expected = $_SERVER['CONTENT_LENGTH']; $actual = \OC\Files\Filesystem::filesize($partpath); @@ -81,10 +101,10 @@ class OC_Connector_Sabre_File extends OC_Connector_Sabre_Node implements Sabre_D // rename to correct path \OC\Files\Filesystem::rename($partpath, $this->path); - //allow sync clients to send the mtime along in a header + // allow sync clients to send the mtime along in a header $mtime = OC_Request::hasModificationTime(); if ($mtime !== false) { - if (\OC\Files\Filesystem::touch($this->path, $mtime)) { + if(\OC\Files\Filesystem::touch($this->path, $mtime)) { header('X-OC-MTime: accepted'); } } @@ -99,7 +119,7 @@ class OC_Connector_Sabre_File extends OC_Connector_Sabre_Node implements Sabre_D */ public function get() { - //throw execption if encryption is disabled but files are still encrypted + //throw exception if encryption is disabled but files are still encrypted if (\OC_Util::encryptedFiles()) { throw new \Sabre_DAV_Exception_ServiceUnavailable(); } else { From 84a0e6930b8668a57075159d0c6e31e35e6c5e84 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20M=C3=BCller?= Date: Tue, 24 Sep 2013 15:35:21 +0200 Subject: [PATCH 3/8] creating non static getETagPropertyForPath() adding public $fileView to Node to allow unit testing --- lib/connector/sabre/directory.php | 5 ++--- lib/connector/sabre/file.php | 22 +++++++++++----------- lib/connector/sabre/node.php | 17 +++++++++++++++-- 3 files changed, 28 insertions(+), 16 deletions(-) diff --git a/lib/connector/sabre/directory.php b/lib/connector/sabre/directory.php index fa20a60f3a5..e36ac84652c 100644 --- a/lib/connector/sabre/directory.php +++ b/lib/connector/sabre/directory.php @@ -205,13 +205,12 @@ class OC_Connector_Sabre_Directory extends OC_Connector_Sabre_Node implements Sa * If the array is empty, all properties should be returned * * @param array $properties - * @return void + * @return array */ public function getProperties($properties) { $props = parent::getProperties($properties); if (in_array(self::GETETAG_PROPERTYNAME, $properties) && !isset($props[self::GETETAG_PROPERTYNAME])) { - $props[self::GETETAG_PROPERTYNAME] - = OC_Connector_Sabre_Node::getETagPropertyForPath($this->path); + $props[self::GETETAG_PROPERTYNAME] = $this->getETagPropertyForPath($this->path); } return $props; } diff --git a/lib/connector/sabre/file.php b/lib/connector/sabre/file.php index aa71b8be217..af9a36931ab 100644 --- a/lib/connector/sabre/file.php +++ b/lib/connector/sabre/file.php @@ -45,9 +45,9 @@ class OC_Connector_Sabre_File extends OC_Connector_Sabre_Node implements Sabre_D * @return string|null */ public function put($data) { - - if (\OC\Files\Filesystem::file_exists($this->path) && - !\OC\Files\Filesystem::isUpdatable($this->path)) { + $fs = $this->getFS(); + if ($fs->file_exists($this->path) && + !$fs->isUpdatable($this->path)) { throw new \Sabre_DAV_Exception_Forbidden(); } @@ -69,7 +69,7 @@ class OC_Connector_Sabre_File extends OC_Connector_Sabre_Node implements Sabre_D if ($chunk_handler->isComplete()) { $newPath = $this->path . '/' . $info['name']; $chunk_handler->file_assemble($newPath); - return OC_Connector_Sabre_Node::getETagPropertyForPath($newPath); + return $this->getETagPropertyForPath($newPath); } return null; @@ -78,10 +78,10 @@ class OC_Connector_Sabre_File extends OC_Connector_Sabre_Node implements Sabre_D // mark file as partial while uploading (ignored by the scanner) $partpath = $this->path . '.part'; - $putOkay = \OC\Files\Filesystem::file_put_contents($partpath, $data); + $putOkay = $fs->file_put_contents($partpath, $data); if ($putOkay === false) { \OC_Log::write('webdav', '\OC\Files\Filesystem::file_put_contents() failed', \OC_Log::ERROR); - \OC\Files\Filesystem::unlink($partpath); + $fs->unlink($partpath); throw new Sabre_DAV_Exception(); } @@ -89,9 +89,9 @@ class OC_Connector_Sabre_File extends OC_Connector_Sabre_Node implements Sabre_D if (isset ($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'PUT' ) { if (isset($_SERVER['CONTENT_LENGTH'])) { $expected = $_SERVER['CONTENT_LENGTH']; - $actual = \OC\Files\Filesystem::filesize($partpath); + $actual = $fs->filesize($partpath); if ($actual != $expected) { - \OC\Files\Filesystem::unlink($partpath); + $fs->unlink($partpath); throw new Sabre_DAV_Exception_BadRequest( 'expected filesize ' . $expected . ' got ' . $actual); } @@ -99,17 +99,17 @@ class OC_Connector_Sabre_File extends OC_Connector_Sabre_Node implements Sabre_D } // rename to correct path - \OC\Files\Filesystem::rename($partpath, $this->path); + $fs->rename($partpath, $this->path); // allow sync clients to send the mtime along in a header $mtime = OC_Request::hasModificationTime(); if ($mtime !== false) { - if(\OC\Files\Filesystem::touch($this->path, $mtime)) { + if($fs->touch($this->path, $mtime)) { header('X-OC-MTime: accepted'); } } - return OC_Connector_Sabre_Node::getETagPropertyForPath($this->path); + return $this->getETagPropertyForPath($this->path); } /** diff --git a/lib/connector/sabre/node.php b/lib/connector/sabre/node.php index 0bffa58af78..28679ef8026 100644 --- a/lib/connector/sabre/node.php +++ b/lib/connector/sabre/node.php @@ -32,6 +32,13 @@ abstract class OC_Connector_Sabre_Node implements Sabre_DAV_INode, Sabre_DAV_IPr */ public static $ETagFunction = null; + /** + * is kept public to allow overwrite for unit testing + * + * @var \OC\Files\View + */ + public $fileView; + /** * The path to the current node * @@ -216,12 +223,18 @@ abstract class OC_Connector_Sabre_Node implements Sabre_DAV_INode, Sabre_DAV_IPr * @param string $path Path of the file * @return string|null Returns null if the ETag can not effectively be determined */ - static public function getETagPropertyForPath($path) { - $data = \OC\Files\Filesystem::getFileInfo($path); + protected function getETagPropertyForPath($path) { + $data = $this->getFS()->getFileInfo($path); if (isset($data['etag'])) { return '"'.$data['etag'].'"'; } return null; } + protected function getFS() { + if (is_null($this->fileView)) { + $this->fileView = \OC\Files\Filesystem::getView(); + } + return $this->fileView; + } } From ee75a5b134eaa49b54857ea254b5217ea47e0e2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20M=C3=BCller?= Date: Tue, 24 Sep 2013 15:44:02 +0200 Subject: [PATCH 4/8] adding basic unit test for failing file_put_content operation --- tests/lib/connector/sabre/file.php | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 tests/lib/connector/sabre/file.php diff --git a/tests/lib/connector/sabre/file.php b/tests/lib/connector/sabre/file.php new file mode 100644 index 00000000000..ae95ea9ac41 --- /dev/null +++ b/tests/lib/connector/sabre/file.php @@ -0,0 +1,27 @@ + + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +class Test_OC_Connector_Sabre_File extends PHPUnit_Framework_TestCase { + + public function setUp() { + } + + /** + * @expectedException Sabre_DAV_Exception + */ + public function testSimplePutFails() { + // setup + $file = new OC_Connector_Sabre_File('/test.txt'); + $file->fileView = $this->getMock('\OC\Files\View', array('file_put_contents'), array(), '', FALSE); + $file->fileView->expects($this->any())->method('file_put_contents')->withAnyParameters()->will($this->returnValue(false)); + + // action + $etag = $file->put('test data'); + } + +} From a86c10984a90ba51b8748cb789a93908118c5808 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20M=C3=BCller?= Date: Thu, 26 Sep 2013 11:50:46 +0200 Subject: [PATCH 5/8] catching NotPermittedException and throw it to the dav client as 403 --- lib/connector/sabre/file.php | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/lib/connector/sabre/file.php b/lib/connector/sabre/file.php index af9a36931ab..aa4b886429c 100644 --- a/lib/connector/sabre/file.php +++ b/lib/connector/sabre/file.php @@ -78,11 +78,16 @@ class OC_Connector_Sabre_File extends OC_Connector_Sabre_Node implements Sabre_D // mark file as partial while uploading (ignored by the scanner) $partpath = $this->path . '.part'; - $putOkay = $fs->file_put_contents($partpath, $data); - if ($putOkay === false) { - \OC_Log::write('webdav', '\OC\Files\Filesystem::file_put_contents() failed', \OC_Log::ERROR); - $fs->unlink($partpath); - throw new Sabre_DAV_Exception(); + try { + $putOkay = $fs->file_put_contents($partpath, $data); + if ($putOkay === false) { + \OC_Log::write('webdav', '\OC\Files\Filesystem::file_put_contents() failed', \OC_Log::ERROR); + $fs->unlink($partpath); + // because we have no clue about the cause we can only throw back a 500/Internal Server Error + throw new Sabre_DAV_Exception(); + } + } catch (\OCP\Files\NotPermittedException $e) { + throw new Sabre_DAV_Exception_Forbidden(); } //detect aborted upload From 1ec7dff2fe1922a4690dc2537c0c64369ca86ea1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20M=C3=BCller?= Date: Fri, 27 Sep 2013 13:30:09 +0200 Subject: [PATCH 6/8] remove unused setUp() --- tests/lib/connector/sabre/file.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/tests/lib/connector/sabre/file.php b/tests/lib/connector/sabre/file.php index ae95ea9ac41..ab34937b0c5 100644 --- a/tests/lib/connector/sabre/file.php +++ b/tests/lib/connector/sabre/file.php @@ -8,9 +8,6 @@ class Test_OC_Connector_Sabre_File extends PHPUnit_Framework_TestCase { - public function setUp() { - } - /** * @expectedException Sabre_DAV_Exception */ From 92c02e67973cf3e4e2615eae5a280674df43e084 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20M=C3=BCller?= Date: Mon, 30 Sep 2013 10:58:03 +0200 Subject: [PATCH 7/8] remove commented code --- lib/connector/sabre/directory.php | 56 ------------------------------- 1 file changed, 56 deletions(-) diff --git a/lib/connector/sabre/directory.php b/lib/connector/sabre/directory.php index 1e50d4cc7da..e36ac84652c 100644 --- a/lib/connector/sabre/directory.php +++ b/lib/connector/sabre/directory.php @@ -57,62 +57,6 @@ class OC_Connector_Sabre_Directory extends OC_Connector_Sabre_Node implements Sa $path = $this->path . '/' . $name; $node = new OC_Connector_Sabre_File($path); return $node->put($data); - -// if (isset($_SERVER['HTTP_OC_CHUNKED'])) { -// $info = OC_FileChunking::decodeName($name); -// if (empty($info)) { -// throw new Sabre_DAV_Exception_NotImplemented(); -// } -// $chunk_handler = new OC_FileChunking($info); -// $chunk_handler->store($info['index'], $data); -// if ($chunk_handler->isComplete()) { -// $newPath = $this->path . '/' . $info['name']; -// $chunk_handler->file_assemble($newPath); -// return OC_Connector_Sabre_Node::getETagPropertyForPath($newPath); -// } -// } else { -// $newPath = $this->path . '/' . $name; -// -// // mark file as partial while uploading (ignored by the scanner) -// $partpath = $newPath . '.part'; -// -// \OC\Files\Filesystem::file_put_contents($partpath, $data); -// -// //detect aborted upload -// if (isset ($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'PUT' ) { -// if (isset($_SERVER['CONTENT_LENGTH'])) { -// $expected = $_SERVER['CONTENT_LENGTH']; -// $actual = \OC\Files\Filesystem::filesize($partpath); -// if ($actual != $expected) { -// \OC\Files\Filesystem::unlink($partpath); -// throw new Sabre_DAV_Exception_BadRequest( -// 'expected filesize ' . $expected . ' got ' . $actual); -// } -// } -// } -// -// // rename to correct path -// $renameOkay = \OC\Files\Filesystem::rename($partpath, $newPath); -// $fileExists = \OC\Files\Filesystem::file_exists($newPath); -// if ($renameOkay === false || $fileExists === false) { -// \OC_Log::write('webdav', '\OC\Files\Filesystem::rename() failed', \OC_Log::ERROR); -// \OC\Files\Filesystem::unlink($partpath); -// throw new Sabre_DAV_Exception(); -// } -// -// // allow sync clients to send the mtime along in a header -// $mtime = OC_Request::hasModificationTime(); -// if ($mtime !== false) { -// if(\OC\Files\Filesystem::touch($newPath, $mtime)) { -// header('X-OC-MTime: accepted'); -// } -// } -// -// return OC_Connector_Sabre_Node::getETagPropertyForPath($newPath); -// } -// -// return null; -//>>>>>>> master } /** From fdc87eaeb360ad14ed243368ea8f74aafed32304 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20M=C3=BCller?= Date: Mon, 30 Sep 2013 11:30:34 +0200 Subject: [PATCH 8/8] adding test testSimplePutFailsOnRename() --- tests/lib/connector/sabre/file.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/lib/connector/sabre/file.php b/tests/lib/connector/sabre/file.php index ab34937b0c5..a1dade3d63d 100644 --- a/tests/lib/connector/sabre/file.php +++ b/tests/lib/connector/sabre/file.php @@ -21,4 +21,18 @@ class Test_OC_Connector_Sabre_File extends PHPUnit_Framework_TestCase { $etag = $file->put('test data'); } + /** + * @expectedException Sabre_DAV_Exception + */ + public function testSimplePutFailsOnRename() { + // setup + $file = new OC_Connector_Sabre_File('/test.txt'); + $file->fileView = $this->getMock('\OC\Files\View', array('file_put_contents', 'rename'), array(), '', FALSE); + $file->fileView->expects($this->any())->method('file_put_contents')->withAnyParameters()->will($this->returnValue(true)); + $file->fileView->expects($this->any())->method('rename')->withAnyParameters()->will($this->returnValue(false)); + + // action + $etag = $file->put('test data'); + } + }