From 71d77d85aacb325c3f0ae4f493fcde52d33e2363 Mon Sep 17 00:00:00 2001 From: Christian Hartmann Date: Tue, 14 Jul 2026 21:31:28 +0200 Subject: [PATCH] fix: remove invalid characters constant and use filename validator for sanitization Signed-off-by: Christian Hartmann Signed-off-by: GitHub --- lib/Constants.php | 13 ------------- lib/Helper/FilePathHelper.php | 4 +++- tests/Unit/Controller/ShareApiControllerTest.php | 7 ++++++- tests/Unit/Helper/FilePathHelperTest.php | 7 ++++++- tests/Unit/Service/FormsServiceTest.php | 7 ++++++- 5 files changed, 21 insertions(+), 17 deletions(-) diff --git a/lib/Constants.php b/lib/Constants.php index e25ea7473..67d4d5596 100644 --- a/lib/Constants.php +++ b/lib/Constants.php @@ -229,19 +229,6 @@ class Constants { self::ANSWER_GRID_TYPE_RADIO, ]; - public const FILENAME_INVALID_CHARS = [ - "\n", - '/', - '\\', - ':', - '*', - '?', - '"', - '<', - '>', - '|', - ]; - /** * !! Keep in sync with src/mixins/ShareTypes.js !! */ diff --git a/lib/Helper/FilePathHelper.php b/lib/Helper/FilePathHelper.php index 2da421bab..23e876c0e 100644 --- a/lib/Helper/FilePathHelper.php +++ b/lib/Helper/FilePathHelper.php @@ -10,12 +10,14 @@ use OCA\Forms\Constants; use OCA\Forms\Db\Form; use OCP\Files\Folder; +use OCP\Files\IFilenameValidator; use OCP\Files\IRootFolder; use OCP\Files\NotFoundException; class FilePathHelper { public function __construct( private readonly IRootFolder $rootFolder, + private readonly IFilenameValidator $fileNameValidator, ) { } @@ -23,7 +25,7 @@ public function __construct( * Normalize a filename by replacing invalid characters */ public function normalizeFileName(string $fileName): string { - return trim(str_replace(Constants::FILENAME_INVALID_CHARS, '-', $fileName)); + return $this->fileNameValidator->sanitizeFilename($fileName, '-'); } /** diff --git a/tests/Unit/Controller/ShareApiControllerTest.php b/tests/Unit/Controller/ShareApiControllerTest.php index 7ec6366a1..03ead775b 100644 --- a/tests/Unit/Controller/ShareApiControllerTest.php +++ b/tests/Unit/Controller/ShareApiControllerTest.php @@ -29,6 +29,7 @@ use OCP\AppFramework\OCS\OCSNotFoundException; use OCP\Files\File; use OCP\Files\Folder; +use OCP\Files\IFilenameValidator; use OCP\Files\IRootFolder; use OCP\IGroup; use OCP\IGroupManager; @@ -61,6 +62,7 @@ class ShareApiControllerTest extends TestCase { private CirclesService|MockObject $circlesService; private FilePathHelper $filePathHelper; private IRootFolder|MockObject $rootFolder; + private IFilenameValidator|MockObject $filenameValidator; private IManager|MockObject $shareManager; public function setUp(): void { @@ -76,7 +78,10 @@ public function setUp(): void { $this->circlesService = $this->createMock(CirclesService::class); $this->rootFolder = $this->createMock(IRootFolder::class); - $this->filePathHelper = new FilePathHelper($this->rootFolder); + $this->filenameValidator = $this->createMock(IFilenameValidator::class); + $this->filenameValidator->method('sanitizeFilename') + ->willReturnCallback(static fn (string $fileName, string $replacement): string => str_replace(['/', '\\'], $replacement, trim($fileName))); + $this->filePathHelper = new FilePathHelper($this->rootFolder, $this->filenameValidator); $this->shareManager = $this->createMock(IManager::class); $this->shareApiController = new ShareApiController( diff --git a/tests/Unit/Helper/FilePathHelperTest.php b/tests/Unit/Helper/FilePathHelperTest.php index 8909d1870..82d408de5 100644 --- a/tests/Unit/Helper/FilePathHelperTest.php +++ b/tests/Unit/Helper/FilePathHelperTest.php @@ -11,6 +11,7 @@ use OCA\Forms\Db\Form; use OCA\Forms\Helper\FilePathHelper; use OCP\Files\Folder; +use OCP\Files\IFilenameValidator; use OCP\Files\IRootFolder; use OCP\Files\NotFoundException; use PHPUnit\Framework\MockObject\MockObject; @@ -18,12 +19,16 @@ class FilePathHelperTest extends TestCase { private FilePathHelper $filePathHelper; + private IFilenameValidator|MockObject $filenameValidator; private IRootFolder|MockObject $rootFolder; public function setUp(): void { parent::setUp(); $this->rootFolder = $this->createMock(IRootFolder::class); - $this->filePathHelper = new FilePathHelper($this->rootFolder); + $this->filenameValidator = $this->createMock(IFilenameValidator::class); + $this->filenameValidator->method('sanitizeFilename') + ->willReturnCallback(static fn (string $fileName, string $replacement): string => str_replace(['/', '\\'], $replacement, trim($fileName))); + $this->filePathHelper = new FilePathHelper($this->rootFolder, $this->filenameValidator); } public function testNormalizeFileName() { diff --git a/tests/Unit/Service/FormsServiceTest.php b/tests/Unit/Service/FormsServiceTest.php index d718c3790..881031c05 100644 --- a/tests/Unit/Service/FormsServiceTest.php +++ b/tests/Unit/Service/FormsServiceTest.php @@ -49,6 +49,7 @@ function microtime(bool|float $asFloat = false) { use OCP\AppFramework\Db\DoesNotExistException; use OCP\EventDispatcher\IEventDispatcher; use OCP\Files\Folder; +use OCP\Files\IFilenameValidator; use OCP\Files\IRootFolder; use OCP\Files\NotFoundException; use OCP\IGroup; @@ -80,6 +81,7 @@ class FormsServiceTest extends TestCase { private CirclesService|MockObject $circlesService; private FilePathHelper|MockObject $filePathHelper; private IRootFolder|MockObject $rootFolder; + private IFilenameValidator|MockObject $filenameValidator; private IL10N|MockObject $l10n; private LoggerInterface|MockObject $logger; @@ -112,7 +114,10 @@ public function setUp(): void { ->willReturn($user); $this->rootFolder = $this->createMock(IRootFolder::class); - $this->filePathHelper = new FilePathHelper($this->rootFolder); + $this->filenameValidator = $this->createMock(IFilenameValidator::class); + $this->filenameValidator->method('sanitizeFilename') + ->willReturnCallback(static fn (string $fileName, string $replacement): string => str_replace(['/', '\\', "\n"], $replacement, trim($fileName))); + $this->filePathHelper = new FilePathHelper($this->rootFolder, $this->filenameValidator); $this->l10n = $this->createMock(IL10N::class); $this->l10n->expects($this->any())