From 4bac595068c813c56d8d5e580e560527ba80194d Mon Sep 17 00:00:00 2001 From: Thomas Müller Date: Wed, 18 Feb 2015 17:44:13 +0100 Subject: adding storage specific filename verification - refs #13640 --- .../connector/sabre/exception/invalidpathtest.php | 41 ++++ tests/lib/files/pathverificationtest.php | 226 +++++++++++++++++++++ 2 files changed, 267 insertions(+) create mode 100644 tests/lib/connector/sabre/exception/invalidpathtest.php create mode 100644 tests/lib/files/pathverificationtest.php (limited to 'tests') diff --git a/tests/lib/connector/sabre/exception/invalidpathtest.php b/tests/lib/connector/sabre/exception/invalidpathtest.php new file mode 100644 index 00000000000..ee3da8c3886 --- /dev/null +++ b/tests/lib/connector/sabre/exception/invalidpathtest.php @@ -0,0 +1,41 @@ + + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ +class Test_OC_Connector_Sabre_Exception_InvalidPath extends \Test\TestCase { + + public function testSerialization() { + + // create xml doc + $DOM = new \DOMDocument('1.0','utf-8'); + $DOM->formatOutput = true; + $error = $DOM->createElementNS('DAV:','d:error'); + $error->setAttribute('xmlns:s', Sabre\DAV\Server::NS_SABREDAV); + $DOM->appendChild($error); + + // serialize the exception + $message = "1234567890"; + $retry = false; + $expectedXml = << + + false + 1234567890 + + +EOD; + + + $ex = new OC_Connector_Sabre_Exception_InvalidPath($message, $retry); + $server = $this->getMock('Sabre\DAV\Server'); + $ex->serialize($server, $error); + + // assert + $xml = $DOM->saveXML(); + $this->assertEquals($expectedXml, $xml); + } +} diff --git a/tests/lib/files/pathverificationtest.php b/tests/lib/files/pathverificationtest.php new file mode 100644 index 00000000000..fafc98cc3cc --- /dev/null +++ b/tests/lib/files/pathverificationtest.php @@ -0,0 +1,226 @@ + + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. */ + +namespace Test\Files; + +use OC\Files\Storage\Local; +use OC\Files\View; + +class PathVerification extends \Test\TestCase { + + /** + * @var \OC\Files\View + */ + private $view; + + protected function setUp() { + parent::setUp(); + $this->view = new View(); + } + + /** + * @dataProvider providesEmptyFiles + * @expectedException \OCP\Files\InvalidPathException + * @expectedExceptionMessage Empty filename is not allowed + */ + public function testPathVerificationEmptyFileName($fileName) { + $this->view->verifyPath('', $fileName); + } + + public function providesEmptyFiles() { + return [ + [''], + [' '], + ]; + } + + /** + * @dataProvider providesDotFiles + * @expectedException \OCP\Files\InvalidPathException + * @expectedExceptionMessage Dot files are not allowed + */ + public function testPathVerificationDotFiles($fileName) { + $this->view->verifyPath('', $fileName); + } + + public function providesDotFiles() { + return [ + ['.'], + ['..'], + [' .'], + [' ..'], + ['. '], + ['.. '], + [' . '], + [' .. '], + ]; + } + + /** + * @dataProvider providesAstralPlane + * @expectedException \OCP\Files\InvalidPathException + * @expectedExceptionMessage 4-byte characters are not supported in file names + */ + public function testPathVerificationAstralPlane($fileName) { + $this->view->verifyPath('', $fileName); + } + + public function providesAstralPlane() { + return [ + // this is the monkey emoji - http://en.wikipedia.org/w/index.php?title=%F0%9F%90%B5&redirect=no + ['🐵'], + ]; + } + + /** + * @dataProvider providesInvalidCharsWindows + * @expectedException \OCP\Files\InvalidPathException + * @expectedExceptionMessage File name contains at least one invalid characters + */ + public function testPathVerificationInvalidCharsWindows($fileName) { + $storage = new Local(['datadir' => '']); + + $fileName = " 123{$fileName}456 "; + \Test_Helper::invokePrivate($storage, 'verifyWindowsPath', [$fileName]); + } + + public function providesInvalidCharsWindows() { + return [ + [\chr(0)], + [\chr(1)], + [\chr(2)], + [\chr(3)], + [\chr(4)], + [\chr(5)], + [\chr(6)], + [\chr(7)], + [\chr(8)], + [\chr(9)], + [\chr(10)], + [\chr(11)], + [\chr(12)], + [\chr(13)], + [\chr(14)], + [\chr(15)], + [\chr(16)], + [\chr(17)], + [\chr(18)], + [\chr(19)], + [\chr(20)], + [\chr(21)], + [\chr(22)], + [\chr(23)], + [\chr(24)], + [\chr(25)], + [\chr(26)], + [\chr(27)], + [\chr(28)], + [\chr(29)], + [\chr(30)], + [\chr(31)], + ['<'], + ['>'], + [':'], + ['"'], + ['/'], + ['\\'], + ['|'], + ['?'], + ['*'], + ]; + } + + /** + * @dataProvider providesInvalidCharsPosix + * @expectedException \OCP\Files\InvalidPathException + * @expectedExceptionMessage File name contains at least one invalid characters + */ + public function testPathVerificationInvalidCharsPosix($fileName) { + $storage = new Local(['datadir' => '']); + + $fileName = " 123{$fileName}456 "; + \Test_Helper::invokePrivate($storage, 'verifyWindowsPath', [$fileName]); + } + + public function providesInvalidCharsPosix() { + return [ + [\chr(0)], + [\chr(1)], + [\chr(2)], + [\chr(3)], + [\chr(4)], + [\chr(5)], + [\chr(6)], + [\chr(7)], + [\chr(8)], + [\chr(9)], + [\chr(10)], + [\chr(11)], + [\chr(12)], + [\chr(13)], + [\chr(14)], + [\chr(15)], + [\chr(16)], + [\chr(17)], + [\chr(18)], + [\chr(19)], + [\chr(20)], + [\chr(21)], + [\chr(22)], + [\chr(23)], + [\chr(24)], + [\chr(25)], + [\chr(26)], + [\chr(27)], + [\chr(28)], + [\chr(29)], + [\chr(30)], + [\chr(31)], + ['/'], + ['\\'], + ]; + } + + /** + * @dataProvider providesReservedNamesWindows + * @expectedException \OCP\Files\InvalidPathException + * @expectedExceptionMessage File name is a reserved word + */ + public function testPathVerificationReservedNamesWindows($fileName) { + $storage = new Local(['datadir' => '']); + + \Test_Helper::invokePrivate($storage, 'verifyWindowsPath', [$fileName]); + } + + public function providesReservedNamesWindows() { + return [ + [' CON '], + ['prn '], + ['AUX'], + ['NUL'], + ['COM1'], + ['COM2'], + ['COM3'], + ['COM4'], + ['COM5'], + ['COM6'], + ['COM7'], + ['COM8'], + ['COM9'], + ['LPT1'], + ['LPT2'], + ['LPT3'], + ['LPT4'], + ['LPT5'], + ['LPT6'], + ['LPT7'], + ['LPT8'], + ['LPT9'] + ]; + } + +} -- cgit v1.2.3 From 49e1a81eba45fe33e00c217758656cf9201ec0cd Mon Sep 17 00:00:00 2001 From: Thomas Müller Date: Wed, 18 Feb 2015 18:28:24 +0100 Subject: fixing namespaces and PHPDoc --- .../connector/sabre/exception/invalidpath.php | 16 ++++++--- lib/private/connector/sabre/node.php | 40 ++++++++++++++-------- lib/private/connector/sabre/objecttree.php | 3 +- lib/private/files/view.php | 5 +++ tests/lib/connector/sabre/directory.php | 2 ++ .../connector/sabre/exception/invalidpathtest.php | 11 +++--- tests/lib/connector/sabre/file.php | 19 ++++++---- tests/lib/connector/sabre/node.php | 3 -- tests/lib/connector/sabre/objecttree.php | 22 ++++++------ 9 files changed, 76 insertions(+), 45 deletions(-) (limited to 'tests') diff --git a/lib/private/connector/sabre/exception/invalidpath.php b/lib/private/connector/sabre/exception/invalidpath.php index 18285994bcc..ecf28f377b0 100644 --- a/lib/private/connector/sabre/exception/invalidpath.php +++ b/lib/private/connector/sabre/exception/invalidpath.php @@ -1,12 +1,17 @@ * This file is licensed under the Affero General Public License version 3 or * later. * See the COPYING-README file. */ -class OC_Connector_Sabre_Exception_InvalidPath extends \Sabre\DAV\Exception { +namespace OC\Connector\Sabre\Exception; + +use Sabre\DAV\Exception; + +class InvalidPath extends Exception { + + const NS_OWNCLOUD = 'http://owncloud.org/ns'; /** * @var bool @@ -34,7 +39,8 @@ class OC_Connector_Sabre_Exception_InvalidPath extends \Sabre\DAV\Exception { } /** - * This method allows the exception to include additional information into the WebDAV error response + * This method allows the exception to include additional information + * into the WebDAV error response * * @param \Sabre\DAV\Server $server * @param \DOMElement $errorNode @@ -42,8 +48,8 @@ class OC_Connector_Sabre_Exception_InvalidPath extends \Sabre\DAV\Exception { */ public function serialize(\Sabre\DAV\Server $server,\DOMElement $errorNode) { - // set owncloud namespace - $errorNode->setAttribute('xmlns:o', OC_Connector_Sabre_FilesPlugin::NS_OWNCLOUD); + // set ownCloud namespace + $errorNode->setAttribute('xmlns:o', self::NS_OWNCLOUD); // adding the retry node $error = $errorNode->ownerDocument->createElementNS('o:','o:retry', var_export($this->retry, true)); diff --git a/lib/private/connector/sabre/node.php b/lib/private/connector/sabre/node.php index 775e18657f1..cdabf26a3fb 100644 --- a/lib/private/connector/sabre/node.php +++ b/lib/private/connector/sabre/node.php @@ -1,28 +1,40 @@ + * @author Bart Visscher + * @author Björn Schießle + * @author Jakob Sack + * @author Jörn Friedrich Dreyer + * @author Klaas Freitag + * @author Markus Goetz + * @author Morris Jobke + * @author Robin Appelman + * @author Sam Tuke + * @author Thomas Müller + * @author Vincent Petry * - * @author Jakob Sack - * @copyright 2011 Jakob Sack kde@jakobsack.de + * @copyright Copyright (c) 2015, ownCloud, Inc. + * @license AGPL-3.0 * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE - * License as published by the Free Software Foundation; either - * version 3 of the License, or any later version. + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. * - * This library is distributed in the hope that it will be useful, + * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU AFFERO GENERAL PUBLIC LICENSE for more details. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. * - * You should have received a copy of the GNU Affero General Public - * License along with this library. If not, see . + * You should have received a copy of the GNU Affero General Public License, version 3, + * along with this program. If not, see * */ namespace OC\Connector\Sabre; +use OC\Connector\Sabre\Exception\InvalidPath; + + abstract class Node implements \Sabre\DAV\INode { /** * Allow configuring the method used to generate Etags @@ -235,7 +247,7 @@ abstract class Node implements \Sabre\DAV\INode { $fileName = basename($this->info->getPath()); $this->fileView->verifyPath($this->path, $fileName); } catch (\OCP\Files\InvalidPathException $ex) { - throw new OC_Connector_Sabre_Exception_InvalidPath($ex->getMessage()); + throw new InvalidPath($ex->getMessage()); } } } diff --git a/lib/private/connector/sabre/objecttree.php b/lib/private/connector/sabre/objecttree.php index 04ca1d7104d..3705aa80586 100644 --- a/lib/private/connector/sabre/objecttree.php +++ b/lib/private/connector/sabre/objecttree.php @@ -8,6 +8,7 @@ namespace OC\Connector\Sabre; +use OC\Connector\Sabre\Exception\InvalidPath; use OC\Files\FileInfo; use OC\Files\Filesystem; use OC\Files\Mount\MoveableMount; @@ -189,7 +190,7 @@ class ObjectTree extends \Sabre\DAV\Tree { try { $this->fileView->verifyPath($destinationDir, $fileName); } catch (\OCP\Files\InvalidPathException $ex) { - throw new OC_Connector_Sabre_Exception_InvalidPath($ex->getMessage()); + throw new InvalidPath($ex->getMessage()); } $renameOkay = $this->fileView->rename($sourcePath, $destinationPath); diff --git a/lib/private/files/view.php b/lib/private/files/view.php index 17fad72e5e7..6a93d7bbf8a 100644 --- a/lib/private/files/view.php +++ b/lib/private/files/view.php @@ -1536,6 +1536,11 @@ class View { return $this->updater; } + /** + * @param string $path + * @param string $fileName + * @throws InvalidPathException + */ public function verifyPath($path, $fileName) { // verify empty and dot files diff --git a/tests/lib/connector/sabre/directory.php b/tests/lib/connector/sabre/directory.php index e7fbd1d27b6..2550f2bcef1 100644 --- a/tests/lib/connector/sabre/directory.php +++ b/tests/lib/connector/sabre/directory.php @@ -8,7 +8,9 @@ */ class Test_OC_Connector_Sabre_Directory extends \Test\TestCase { + /** @var OC\Files\View | PHPUnit_Framework_MockObject_MockObject */ private $view; + /** @var OC\Files\FileInfo | PHPUnit_Framework_MockObject_MockObject */ private $info; protected function setUp() { diff --git a/tests/lib/connector/sabre/exception/invalidpathtest.php b/tests/lib/connector/sabre/exception/invalidpathtest.php index ee3da8c3886..d2d58887d62 100644 --- a/tests/lib/connector/sabre/exception/invalidpathtest.php +++ b/tests/lib/connector/sabre/exception/invalidpathtest.php @@ -1,12 +1,16 @@ * This file is licensed under the Affero General Public License version 3 or * later. * See the COPYING-README file. */ -class Test_OC_Connector_Sabre_Exception_InvalidPath extends \Test\TestCase { +class InvalidPathTest extends \Test\TestCase { public function testSerialization() { @@ -14,7 +18,7 @@ class Test_OC_Connector_Sabre_Exception_InvalidPath extends \Test\TestCase { $DOM = new \DOMDocument('1.0','utf-8'); $DOM->formatOutput = true; $error = $DOM->createElementNS('DAV:','d:error'); - $error->setAttribute('xmlns:s', Sabre\DAV\Server::NS_SABREDAV); + $error->setAttribute('xmlns:s', \Sabre\DAV\Server::NS_SABREDAV); $DOM->appendChild($error); // serialize the exception @@ -29,8 +33,7 @@ class Test_OC_Connector_Sabre_Exception_InvalidPath extends \Test\TestCase { EOD; - - $ex = new OC_Connector_Sabre_Exception_InvalidPath($message, $retry); + $ex = new InvalidPath($message, $retry); $server = $this->getMock('Sabre\DAV\Server'); $ex->serialize($server, $error); diff --git a/tests/lib/connector/sabre/file.php b/tests/lib/connector/sabre/file.php index 2ef5fd794be..f2812e390ac 100644 --- a/tests/lib/connector/sabre/file.php +++ b/tests/lib/connector/sabre/file.php @@ -6,7 +6,12 @@ * See the COPYING-README file. */ -class Test_OC_Connector_Sabre_File extends \Test\TestCase { +namespace Test\Connector\Sabre; + + +use OC_Connector_Sabre_File; + +class File extends \Test\TestCase { /** * @expectedException \Sabre\DAV\Exception @@ -93,7 +98,7 @@ class Test_OC_Connector_Sabre_File extends \Test\TestCase { } /** - * @expectedException \Sabre\DAV\Exception\BadRequest + * @expectedException \OC\Connector\Sabre\Exception\InvalidPath */ public function testSimplePutInvalidChars() { // setup @@ -104,9 +109,9 @@ class Test_OC_Connector_Sabre_File extends \Test\TestCase { $view->expects($this->any()) ->method('getRelativePath') - ->will($this->returnValue('/super*star.txt')); + ->will($this->returnValue('/*')); - $info = new \OC\Files\FileInfo('/super*star.txt', null, null, array( + $info = new \OC\Files\FileInfo('/*', null, null, array( 'permissions' => \OCP\Constants::PERMISSION_ALL ), null); $file = new \OC\Connector\Sabre\File($view, $info); @@ -117,7 +122,7 @@ class Test_OC_Connector_Sabre_File extends \Test\TestCase { /** * Test setting name with setName() with invalid chars - * @expectedException \Sabre\DAV\Exception\BadRequest + * @expectedException \OC\Connector\Sabre\Exception\InvalidPath */ public function testSetNameInvalidChars() { // setup @@ -125,9 +130,9 @@ class Test_OC_Connector_Sabre_File extends \Test\TestCase { $view->expects($this->any()) ->method('getRelativePath') - ->will($this->returnValue('/super*star.txt')); + ->will($this->returnValue('/*')); - $info = new \OC\Files\FileInfo('/super*star.txt', null, null, array( + $info = new \OC\Files\FileInfo('/*', null, null, array( 'permissions' => \OCP\Constants::PERMISSION_ALL ), null); $file = new \OC\Connector\Sabre\File($view, $info); diff --git a/tests/lib/connector/sabre/node.php b/tests/lib/connector/sabre/node.php index e1ae05b2170..3b3a6107813 100644 --- a/tests/lib/connector/sabre/node.php +++ b/tests/lib/connector/sabre/node.php @@ -9,9 +9,6 @@ namespace Test\Connector\Sabre; -use OC\Files\FileInfo; -use OC\Files\View; - class Node extends \Test\TestCase { public function davPermissionsProvider() { return array( diff --git a/tests/lib/connector/sabre/objecttree.php b/tests/lib/connector/sabre/objecttree.php index 0709aa89c63..d2702027b0d 100644 --- a/tests/lib/connector/sabre/objecttree.php +++ b/tests/lib/connector/sabre/objecttree.php @@ -47,29 +47,29 @@ class ObjectTree extends \Test\TestCase { * @dataProvider moveFailedProvider * @expectedException \Sabre\DAV\Exception\Forbidden */ - public function testMoveFailed($source, $dest, $updatables, $deletables) { - $this->moveTest($source, $dest, $updatables, $deletables); + public function testMoveFailed($source, $destination, $updatables, $deletables) { + $this->moveTest($source, $destination, $updatables, $deletables); } /** * @dataProvider moveSuccessProvider */ - public function testMoveSuccess($source, $dest, $updatables, $deletables) { - $this->moveTest($source, $dest, $updatables, $deletables); + public function testMoveSuccess($source, $destination, $updatables, $deletables) { + $this->moveTest($source, $destination, $updatables, $deletables); $this->assertTrue(true); } /** * @dataProvider moveFailedInvalidCharsProvider - * @expectedException \Sabre\DAV\Exception\BadRequest + * @expectedException \OC\Connector\Sabre\Exception\InvalidPath */ - public function testMoveFailedInvalidChars($source, $dest, $updatables, $deletables) { - $this->moveTest($source, $dest, $updatables, $deletables); + public function testMoveFailedInvalidChars($source, $destination, $updatables, $deletables) { + $this->moveTest($source, $destination, $updatables, $deletables); } function moveFailedInvalidCharsProvider() { return array( - array('a/b', 'a/c*', array('a' => false, 'a/b' => true, 'a/c*' => false), array()), + array('a/b', 'a/*', array('a' => false, 'a/b' => true, 'a/c*' => false), array()), ); } @@ -94,10 +94,10 @@ class ObjectTree extends \Test\TestCase { /** * @param $source - * @param $dest + * @param $destination * @param $updatables */ - private function moveTest($source, $dest, $updatables, $deletables) { + private function moveTest($source, $destination, $updatables, $deletables) { $view = new TestDoubleFileView($updatables, $deletables); $info = new FileInfo('', null, null, array(), null); @@ -115,7 +115,7 @@ class ObjectTree extends \Test\TestCase { /** @var $objectTree \OC\Connector\Sabre\ObjectTree */ $mountManager = \OC\Files\Filesystem::getMountManager(); $objectTree->init($rootDir, $view, $mountManager); - $objectTree->move($source, $dest); + $objectTree->move($source, $destination); } /** -- cgit v1.2.3 From 3623f14e73046a51953872fe49853bc200ac736d Mon Sep 17 00:00:00 2001 From: Thomas Müller Date: Wed, 4 Mar 2015 14:03:47 +0100 Subject: no translation service in common storage class --- lib/private/files/storage/common.php | 16 ++++------ lib/private/files/view.php | 14 ++++++-- .../files/invalidcharacterinpathexception.php | 37 ++++++++++++++++++++++ lib/public/files/reservedwordexception.php | 37 ++++++++++++++++++++++ tests/lib/files/pathverificationtest.php | 9 ++---- 5 files changed, 94 insertions(+), 19 deletions(-) create mode 100644 lib/public/files/invalidcharacterinpathexception.php create mode 100644 lib/public/files/reservedwordexception.php (limited to 'tests') diff --git a/lib/private/files/storage/common.php b/lib/private/files/storage/common.php index e948b5aa35a..8549d5a1fad 100644 --- a/lib/private/files/storage/common.php +++ b/lib/private/files/storage/common.php @@ -13,7 +13,9 @@ use OC\Files\Cache\Scanner; use OC\Files\Cache\Storage; use OC\Files\Filesystem; use OC\Files\Cache\Watcher; +use OCP\Files\InvalidCharacterInPathException; use OCP\Files\InvalidPathException; +use OCP\Files\ReservedWordException; /** * Storage backend class for providing common filesystem operation methods @@ -476,7 +478,7 @@ abstract class Common implements \OC\Files\Storage\Storage { $this->scanForInvalidCharacters($fileName, "\\/<>:\"|?*"); $reservedNames = ['CON', 'PRN', 'AUX', 'NUL', 'COM1', 'COM2', 'COM3', 'COM4', 'COM5', 'COM6', 'COM7', 'COM8', 'COM9', 'LPT1', 'LPT2', 'LPT3', 'LPT4', 'LPT5', 'LPT6', 'LPT7', 'LPT8', 'LPT9']; if (in_array(strtoupper($fileName), $reservedNames)) { - throw new InvalidPathException($this->t("File name is a reserved word")); + throw new ReservedWordException(); } } @@ -489,7 +491,7 @@ abstract class Common implements \OC\Files\Storage\Storage { $this->scanForInvalidCharacters($fileName, "\\/"); $reservedNames = ['*']; if (in_array($fileName, $reservedNames)) { - throw new InvalidPathException($this->t('File name is a reserved word')); + throw new ReservedWordException(); } } @@ -501,19 +503,13 @@ abstract class Common implements \OC\Files\Storage\Storage { private function scanForInvalidCharacters($fileName, $invalidChars) { foreach(str_split($invalidChars) as $char) { if (strpos($fileName, $char) !== false) { - throw new InvalidPathException($this->t('File name contains at least one invalid characters')); + throw new InvalidCharacterInPathException(); } } $sanitizedFileName = filter_var($fileName, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW); if($sanitizedFileName !== $fileName) { - throw new InvalidPathException($this->t('File name contains at least one invalid characters')); + throw new InvalidCharacterInPathException(); } } - - private function t($string) { - $l10n = \OC::$server->getL10N('lib'); - return $l10n->t($string); - } - } diff --git a/lib/private/files/view.php b/lib/private/files/view.php index 9d8416d2331..f14209fd925 100644 --- a/lib/private/files/view.php +++ b/lib/private/files/view.php @@ -11,7 +11,9 @@ namespace OC\Files; use OC\Files\Cache\Updater; use OC\Files\Mount\MoveableMount; +use OCP\Files\InvalidCharacterInPathException; use OCP\Files\InvalidPathException; +use OCP\Files\ReservedWordException; /** * Class to provide access to ownCloud filesystem via a "view", and methods for @@ -1563,8 +1565,14 @@ class View { throw new InvalidPathException($l10n->t('4-byte characters are not supported in file names')); } - /** @type \OCP\Files\Storage $storage */ - list($storage, $internalPath) = $this->resolvePath($path); - $storage->verifyPath($internalPath, $fileName); + try { + /** @type \OCP\Files\Storage $storage */ + list($storage, $internalPath) = $this->resolvePath($path); + $storage->verifyPath($internalPath, $fileName); + } catch (ReservedWordException $ex) { + throw new InvalidPathException($l10n->t('File name is a reserved word')); + } catch (InvalidCharacterInPathException $ex) { + throw new InvalidPathException($l10n->t('File name contains at least one invalid characters')); + } } } diff --git a/lib/public/files/invalidcharacterinpathexception.php b/lib/public/files/invalidcharacterinpathexception.php new file mode 100644 index 00000000000..4ae2eb01c19 --- /dev/null +++ b/lib/public/files/invalidcharacterinpathexception.php @@ -0,0 +1,37 @@ +. + * + */ + +/** + * Public interface of ownCloud for apps to use. + * Files/InvalidCharacterInPathException class + */ + +// use OCP namespace for all classes that are considered public. +// This means that they should be used by apps instead of the internal ownCloud classes +namespace OCP\Files; + +/** + * Exception for invalid path + */ +class InvalidCharacterInPathException extends InvalidPathException { + +} diff --git a/lib/public/files/reservedwordexception.php b/lib/public/files/reservedwordexception.php new file mode 100644 index 00000000000..25c618a8ded --- /dev/null +++ b/lib/public/files/reservedwordexception.php @@ -0,0 +1,37 @@ +. + * + */ + +/** + * Public interface of ownCloud for apps to use. + * Files/ReservedWordException class + */ + +// use OCP namespace for all classes that are considered public. +// This means that they should be used by apps instead of the internal ownCloud classes +namespace OCP\Files; + +/** + * Exception for invalid path + */ +class ReservedWordException extends InvalidPathException { + +} diff --git a/tests/lib/files/pathverificationtest.php b/tests/lib/files/pathverificationtest.php index fafc98cc3cc..1a802a48f57 100644 --- a/tests/lib/files/pathverificationtest.php +++ b/tests/lib/files/pathverificationtest.php @@ -78,8 +78,7 @@ class PathVerification extends \Test\TestCase { /** * @dataProvider providesInvalidCharsWindows - * @expectedException \OCP\Files\InvalidPathException - * @expectedExceptionMessage File name contains at least one invalid characters + * @expectedException \OCP\Files\InvalidCharacterInPathException */ public function testPathVerificationInvalidCharsWindows($fileName) { $storage = new Local(['datadir' => '']); @@ -136,8 +135,7 @@ class PathVerification extends \Test\TestCase { /** * @dataProvider providesInvalidCharsPosix - * @expectedException \OCP\Files\InvalidPathException - * @expectedExceptionMessage File name contains at least one invalid characters + * @expectedException \OCP\Files\InvalidCharacterInPathException */ public function testPathVerificationInvalidCharsPosix($fileName) { $storage = new Local(['datadir' => '']); @@ -187,8 +185,7 @@ class PathVerification extends \Test\TestCase { /** * @dataProvider providesReservedNamesWindows - * @expectedException \OCP\Files\InvalidPathException - * @expectedExceptionMessage File name is a reserved word + * @expectedException \OCP\Files\ReservedWordException */ public function testPathVerificationReservedNamesWindows($fileName) { $storage = new Local(['datadir' => '']); -- cgit v1.2.3