-
Notifications
You must be signed in to change notification settings - Fork 37
Fix: Improve sanitization of folder and file names #209
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
marcelklehr
merged 8 commits into
nextcloud:main
from
AhsanIsEpic:fix/improve-folder-creation-handling
Jun 24, 2025
Merged
Changes from 2 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d3a81f2
Fix: Improve sanitization of folder and file names
AhsanIsEpic a44e74f
Fix: Implement better filename sanitization utility for Google Drive …
AhsanIsEpic bf5ab9b
Merge branch 'nextcloud:main' into fix/improve-folder-creation-handling
AhsanIsEpic 19afaf5
refactor(FileUtils): Refactro filename sanitization logic and excepti…
AhsanIsEpic c360649
Merge branch 'fix/improve-folder-creation-handling' of github.com:Ahs…
marcelklehr a420ede
fix: run cs:fix
marcelklehr dd78c84
refactor: Use dependency injection instead of static class
marcelklehr 4b4d171
fix: fix psalm issues
marcelklehr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| <?php | ||
|
|
||
| namespace OCA\Google\Service\Utils; | ||
|
|
||
| use OCP\Files\FileNameTooLongException; | ||
| use OCP\Files\EmptyFileNameException; | ||
| use OCP\Files\InvalidCharacterInPathException; | ||
| use OCP\Files\InvalidDirectoryException; | ||
| use OCP\Files\ReservedWordException; | ||
| use OCP\Files\InvalidPathException; | ||
| use Psr\Log\LoggerInterface; | ||
| use OC; | ||
|
|
||
| class FileUtils { | ||
|
|
||
| /** | ||
| * Sanitize the filename to ensure it is valid, does not exceed length limits. | ||
| * | ||
| * @param string $filename The original filename to sanitize. | ||
| * @param string $id A unique ID to append if necessary to ensure uniqueness. | ||
| * @param int $recursionDepth The current recursion depth (used to prevent infinite loops). | ||
| * @return string The sanitized and validated filename. | ||
| */ | ||
| public static function sanitizeFilename( | ||
| string $filename, | ||
| string $id, | ||
| LoggerInterface $logger, | ||
| int $recursionDepth = 0, | ||
| string $originalFilename = null | ||
| ): string { | ||
| // Prevent infinite recursion by limiting the depth. | ||
| if ($recursionDepth > 15) { | ||
| $filename = 'Untitled_' . $id; | ||
| $logger->warning('Maximum recursion depth reached while sanitizing filename: ' . $originalFilename . ' renaming to ' . $filename); | ||
| return $filename; | ||
| } | ||
|
|
||
| // If the original filename is not provided, use the current filename. | ||
| if ($originalFilename === null) { | ||
| $originalFilename = $filename; | ||
| } | ||
|
|
||
| // Trim leading/trailing whitespace and trailing dots. | ||
| $filename = rtrim(trim($filename), '.'); | ||
|
|
||
| // Check if trimming altered the filename. | ||
| $trimmed = ($originalFilename !== $filename); | ||
|
|
||
| // Helper function to append the ID before the file extension. | ||
| $appendIdBeforeExtension = function ($filename, $id) { | ||
| $pathInfo = pathinfo($filename); | ||
| if (isset($pathInfo['extension'])) { | ||
| return $pathInfo['filename'] . '_' . $id . '.' . $pathInfo['extension']; | ||
| } else { | ||
| return $filename . '_' . $id; | ||
| } | ||
| }; | ||
|
|
||
| // Append the ID if trimming occurred and the ID is not already present. | ||
| if ($trimmed && !str_contains($filename, $id)) { | ||
| $filename = $appendIdBeforeExtension($filename, $id); | ||
| } | ||
|
|
||
| // Ensure the filename length does not exceed the maximum allowed length. | ||
| $maxLength = 254; | ||
| if (mb_strlen($filename) > $maxLength) { | ||
| $pathInfo = pathinfo($filename); | ||
| $baseLength = $maxLength - mb_strlen($id) - 2; // Account for '_' and '.'. | ||
| if (isset($pathInfo['extension'])) { | ||
| $baseLength -= mb_strlen($pathInfo['extension']); | ||
| $filename = mb_substr($pathInfo['filename'], 0, $baseLength) . '_' . $id . '.' . $pathInfo['extension']; | ||
| } else { | ||
| $filename = mb_substr($filename, 0, $baseLength) . '_' . $id; | ||
| } | ||
| } | ||
|
|
||
| try { | ||
| // Validate the filename using the Nextcloud filename validator. | ||
| \OC::$server->get(\OCP\Files\IFilenameValidator::class)->validateFilename($filename); | ||
|
|
||
| // if recursion depth is greater than 0, log the change. | ||
| if ($recursionDepth > 0) { | ||
| $logger->info('Filename sanitized successfully: "' . $filename . '" (original: "' . $originalFilename . '")'); | ||
| } | ||
|
|
||
| return $filename; | ||
| } catch (InvalidPathException $exception) { | ||
| $logger->warning('Invalid filename detected during sanitization: ' . $filename, ['exception' => $exception]); | ||
| } | ||
|
|
||
| // Handle specific exceptions and adjust the filename accordingly. | ||
| switch (true) { | ||
| case $exception instanceof FileNameTooLongException: | ||
| $filename = mb_substr($filename, 0, $maxLength - mb_strlen($id) - 2); | ||
| break; | ||
|
|
||
| case $exception instanceof EmptyFileNameException: | ||
| $filename = 'Untitled'; | ||
| break; | ||
|
|
||
| case $exception instanceof InvalidCharacterInPathException: | ||
| if (preg_match('/"(.*?)"/', $exception->getMessage(), $matches)) { | ||
| $invalidChars = array_merge(str_split($matches[1]), ['"']); | ||
| $filename = str_replace($invalidChars, '-', $filename); | ||
| } | ||
| break; | ||
|
|
||
| case $exception instanceof InvalidDirectoryException: | ||
| $logger->error('Invalid directory detected in filename: ' . $exception->getMessage()); | ||
| $filename = 'Untitled'; | ||
| break; | ||
|
|
||
| case $exception instanceof ReservedWordException: | ||
| if (preg_match('/"(.*?)"/', $exception->getMessage(), $matches)) { | ||
| $reservedWord = $matches[1]; | ||
| $filename = str_ireplace($reservedWord, '-' . $reservedWord . '-', $filename); | ||
| } | ||
| break; | ||
|
|
||
| default: | ||
| $logger->error('Unknown exception encountered during filename sanitization: ' . $filename); | ||
| $filename = 'Untitled'; | ||
| break; | ||
| } | ||
|
|
||
| // Append the ID if the filename was modified and does not already contain the ID. | ||
| if (!str_contains($filename, $id)) { | ||
| $filename = $appendIdBeforeExtension($filename, $id); | ||
| } | ||
|
|
||
| // Recursively validate the adjusted filename. | ||
| return self::sanitizeFilename($filename, $id, $logger, $recursionDepth + 1, $originalFilename); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wow, this is a really elaborate. Thank you for the effort you put into this! I was actually thinking more of something like this: https://github.com/nextcloud/server/pull/51608/files#diff-911ea9939fad17c78ada50c38706874091e2478e37a2ef5a155481c0c4a81a98R144-R165 But it turns out the methods used there are not in OCP :(
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm inquiring now if we can get a proper sanitization method in OCP to avoid parsing error messages from the validator, which seems a bit brittle
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here we go: nextcloud/server#52688
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nextcloud 32 will have that method in OCP