diff options
author | provokateurin <kate@provokateurin.de> | 2025-02-18 12:30:33 +0100 |
---|---|---|
committer | provokateurin <kate@provokateurin.de> | 2025-03-04 07:49:20 +0100 |
commit | 552eb2678f622458e16ff21a1320ee6ba78aaa7c (patch) | |
tree | d6be5c88ba4cd46d4b9ba641844f4aebee9da44a /lib | |
parent | 745b72b0c56960bbe4b1a0517d430e1e49d4b87b (diff) | |
download | nextcloud-server-backport/51194/stable30.tar.gz nextcloud-server-backport/51194/stable30.zip |
refactor(TempManager): Simplify and unify implementations and remove legacy behaviorbackport/51194/stable30
Signed-off-by: provokateurin <kate@provokateurin.de>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/private/TempManager.php | 78 | ||||
-rw-r--r-- | lib/public/ITempManager.php | 12 |
2 files changed, 27 insertions, 63 deletions
diff --git a/lib/private/TempManager.php b/lib/private/TempManager.php index 68a53f493db..e63b1b32794 100644 --- a/lib/private/TempManager.php +++ b/lib/private/TempManager.php @@ -10,6 +10,7 @@ namespace OC; use bantu\IniGetWrapper\IniGetWrapper; use OCP\IConfig; use OCP\ITempManager; +use OCP\Security\ISecureRandom; use Psr\Log\LoggerInterface; class TempManager implements ITempManager { @@ -34,51 +35,25 @@ class TempManager implements ITempManager { $this->tmpBaseDir = $this->getTempBaseDir(); } - /** - * Builds the filename with suffix and removes potential dangerous characters - * such as directory separators. - * - * @param string $absolutePath Absolute path to the file / folder - * @param string $postFix Postfix appended to the temporary file name, may be user controlled - * @return string - */ - private function buildFileNameWithSuffix($absolutePath, $postFix = '') { + private function generateTemporaryPath(string $postFix): string { + $secureRandom = \OCP\Server::get(ISecureRandom::class); + $absolutePath = $this->tmpBaseDir . '/' . self::TMP_PREFIX . $secureRandom->generate(32, ISecureRandom::CHAR_ALPHANUMERIC); + if ($postFix !== '') { $postFix = '.' . ltrim($postFix, '.'); $postFix = str_replace(['\\', '/'], '', $postFix); - $absolutePath .= '-'; } return $absolutePath . $postFix; } - /** - * Create a temporary file and return the path - * - * @param string $postFix Postfix appended to the temporary file name - * @return string - */ - public function getTemporaryFile($postFix = '') { - if (is_writable($this->tmpBaseDir)) { - // To create an unique file and prevent the risk of race conditions - // or duplicated temporary files by other means such as collisions - // we need to create the file using `tempnam` and append a possible - // postfix to it later - $file = tempnam($this->tmpBaseDir, self::TMP_PREFIX); - $this->current[] = $file; + public function getTemporaryFile($postFix = ''): string|false { + $path = $this->generateTemporaryPath($postFix); - // If a postfix got specified sanitize it and create a postfixed - // temporary file - if ($postFix !== '') { - $fileNameWithPostfix = $this->buildFileNameWithSuffix($file, $postFix); - touch($fileNameWithPostfix); - chmod($fileNameWithPostfix, 0600); - $this->current[] = $fileNameWithPostfix; - return $fileNameWithPostfix; - } - - return $file; - } else { + $old_umask = umask(0077); + $fp = fopen($path, 'x'); + umask($old_umask); + if ($fp === false) { $this->log->warning( 'Can not create a temporary file in directory {dir}. Check it exists and has correct permissions', [ @@ -87,30 +62,16 @@ class TempManager implements ITempManager { ); return false; } - } - /** - * Create a temporary folder and return the path - * - * @param string $postFix Postfix appended to the temporary folder name - * @return string - */ - public function getTemporaryFolder($postFix = '') { - if (is_writable($this->tmpBaseDir)) { - // To create an unique directory and prevent the risk of race conditions - // or duplicated temporary files by other means such as collisions - // we need to create the file using `tempnam` and append a possible - // postfix to it later - $uniqueFileName = tempnam($this->tmpBaseDir, self::TMP_PREFIX); - $this->current[] = $uniqueFileName; + fclose($fp); + $this->current[] = $path; + return $path; + } - // Build a name without postfix - $path = $this->buildFileNameWithSuffix($uniqueFileName . '-folder', $postFix); - mkdir($path, 0700); - $this->current[] = $path; + public function getTemporaryFolder($postFix = ''): string|false { + $path = $this->generateTemporaryPath($postFix) . '/'; - return $path . '/'; - } else { + if (mkdir($path, 0700) === false) { $this->log->warning( 'Can not create a temporary folder in directory {dir}. Check it exists and has correct permissions', [ @@ -119,6 +80,9 @@ class TempManager implements ITempManager { ); return false; } + + $this->current[] = $path; + return $path; } /** diff --git a/lib/public/ITempManager.php b/lib/public/ITempManager.php index 90db91b2ab4..ac8f829f357 100644 --- a/lib/public/ITempManager.php +++ b/lib/public/ITempManager.php @@ -16,20 +16,20 @@ interface ITempManager { /** * Create a temporary file and return the path * - * @param string $postFix - * @return string + * @param string $postFix Postfix appended to the temporary file name + * * @since 8.0.0 */ - public function getTemporaryFile($postFix = ''); + public function getTemporaryFile(string $postFix = ''): string|false; /** * Create a temporary folder and return the path * - * @param string $postFix - * @return string + * @param string $postFix Postfix appended to the temporary folder name + * * @since 8.0.0 */ - public function getTemporaryFolder($postFix = ''); + public function getTemporaryFolder(string $postFix = ''): string|false; /** * Remove the temporary files and folders generated during this request |