adding storage specific filename verification - refs #13640
parent
348fe105b1
commit
4bac595068
@ -0,0 +1,57 @@ |
||||
<?php |
||||
|
||||
/** |
||||
* Copyright (c) 2015 Thomas Müller <deepdiver@owncloud.com> |
||||
* 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 { |
||||
|
||||
/** |
||||
* @var bool |
||||
*/ |
||||
private $retry; |
||||
|
||||
/** |
||||
* @param string $message |
||||
* @param bool $retry |
||||
*/ |
||||
public function __construct($message, $retry = false) { |
||||
parent::__construct($message); |
||||
$this->retry = $retry; |
||||
} |
||||
|
||||
/** |
||||
* Returns the HTTP status code for this exception |
||||
* |
||||
* @return int |
||||
*/ |
||||
public function getHTTPCode() { |
||||
|
||||
return 400; |
||||
|
||||
} |
||||
|
||||
/** |
||||
* This method allows the exception to include additional information into the WebDAV error response |
||||
* |
||||
* @param \Sabre\DAV\Server $server |
||||
* @param \DOMElement $errorNode |
||||
* @return void |
||||
*/ |
||||
public function serialize(\Sabre\DAV\Server $server,\DOMElement $errorNode) { |
||||
|
||||
// set owncloud namespace |
||||
$errorNode->setAttribute('xmlns:o', OC_Connector_Sabre_FilesPlugin::NS_OWNCLOUD); |
||||
|
||||
// adding the retry node |
||||
$error = $errorNode->ownerDocument->createElementNS('o:','o:retry', var_export($this->retry, true)); |
||||
$errorNode->appendChild($error); |
||||
|
||||
// adding the message node |
||||
$error = $errorNode->ownerDocument->createElementNS('o:','o:reason', $this->getMessage()); |
||||
$errorNode->appendChild($error); |
||||
} |
||||
|
||||
} |
@ -0,0 +1,41 @@ |
||||
<?php |
||||
|
||||
/** |
||||
* Copyright (c) 2015 Thomas Müller <deepdiver@owncloud.com> |
||||
* 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 = <<<EOD |
||||
<?xml version="1.0" encoding="utf-8"?> |
||||
<d:error xmlns:d="DAV:" xmlns:s="http://sabredav.org/ns" xmlns:o="http://owncloud.org/ns"> |
||||
<o:retry xmlns:o="o:">false</o:retry> |
||||
<o:reason xmlns:o="o:">1234567890</o:reason> |
||||
</d:error> |
||||
|
||||
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); |
||||
} |
||||
} |
@ -0,0 +1,226 @@ |
||||
<?php |
||||
/** |
||||
* Copyright (c) 2015 Thomas Müller <deepdiver@owncloud.com> |
||||
* 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'] |
||||
]; |
||||
} |
||||
|
||||
} |
Loading…
Reference in new issue