PathVerificationTest.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace Test\Files;
  8. use OC\Files\Storage\Local;
  9. use OC\Files\View;
  10. use OCP\Files\InvalidPathException;
  11. /**
  12. * Class PathVerificationTest
  13. *
  14. * @group DB
  15. *
  16. * @package Test\Files
  17. */
  18. class PathVerificationTest extends \Test\TestCase {
  19. /**
  20. * @var \OC\Files\View
  21. */
  22. private $view;
  23. protected function setUp(): void {
  24. parent::setUp();
  25. $this->view = new View();
  26. }
  27. public function testPathVerificationFileNameTooLong() {
  28. $this->expectException(\OCP\Files\InvalidPathException::class);
  29. $this->expectExceptionMessage('Filename is too long');
  30. $fileName = str_repeat('a', 500);
  31. $this->view->verifyPath('', $fileName);
  32. }
  33. /**
  34. * @dataProvider providesEmptyFiles
  35. */
  36. public function testPathVerificationEmptyFileName($fileName) {
  37. $this->expectException(\OCP\Files\InvalidPathException::class);
  38. $this->expectExceptionMessage('Empty filename is not allowed');
  39. $this->view->verifyPath('', $fileName);
  40. }
  41. public function providesEmptyFiles() {
  42. return [
  43. [''],
  44. [' '],
  45. ];
  46. }
  47. /**
  48. * @dataProvider providesDotFiles
  49. */
  50. public function testPathVerificationDotFiles($fileName) {
  51. $this->expectException(\OCP\Files\InvalidPathException::class);
  52. $this->expectExceptionMessage('Dot files are not allowed');
  53. $this->view->verifyPath('', $fileName);
  54. }
  55. public function providesDotFiles() {
  56. return [
  57. ['.'],
  58. ['..'],
  59. [' .'],
  60. [' ..'],
  61. ['. '],
  62. ['.. '],
  63. [' . '],
  64. [' .. '],
  65. ];
  66. }
  67. /**
  68. * @dataProvider providesAstralPlane
  69. */
  70. public function testPathVerificationAstralPlane($fileName) {
  71. $connection = \OC::$server->getDatabaseConnection();
  72. if (!$connection->supports4ByteText()) {
  73. $this->expectException(InvalidPathException::class);
  74. $this->expectExceptionMessage('File name contains at least one invalid character');
  75. } else {
  76. $this->addToAssertionCount(1);
  77. }
  78. $this->view->verifyPath('', $fileName);
  79. }
  80. public function providesAstralPlane() {
  81. return [
  82. // this is the monkey emoji - http://en.wikipedia.org/w/index.php?title=%F0%9F%90%B5&redirect=no
  83. ['🐵'],
  84. ['🐵.txt'],
  85. ['txt.💩'],
  86. ['💩🐵.txt'],
  87. ['💩🐵'],
  88. ];
  89. }
  90. /**
  91. * @dataProvider providesValidPosixPaths
  92. */
  93. public function testPathVerificationValidPaths($fileName) {
  94. $storage = new Local(['datadir' => '']);
  95. self::invokePrivate($storage, 'verifyPosixPath', [$fileName]);
  96. // nothing thrown
  97. $this->addToAssertionCount(1);
  98. }
  99. public function providesValidPosixPaths() {
  100. return [
  101. ['simple'],
  102. ['simple.txt'],
  103. ['\''],
  104. ['`'],
  105. ['%'],
  106. ['()'],
  107. ['[]'],
  108. ['!'],
  109. ['$'],
  110. ['_'],
  111. ];
  112. }
  113. }