From b221f6cc90937d2505740c70807f4f45da0bff85 Mon Sep 17 00:00:00 2001 From: Nikita Hovratov <nikita.h@live.de> Date: Mon, 6 Mar 2023 09:42:02 +0100 Subject: [PATCH] [TASK] Refactor GU::getFileAbsFileName Improve readability of the method by extracting distinct cases into own blocks with early return statements. Case 1: "EXT:" path Case 2: (already) absolute path Case 3: relative public path Resolves: #100094 Releases: main Change-Id: I664c3611ef86c779c0398c7a4f938297eb591a81 Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/78034 Reviewed-by: Benni Mack <benni@typo3.org> Tested-by: core-ci <typo3@b13.com> Tested-by: Benni Mack <benni@typo3.org> Tested-by: Georg Ringer <georg.ringer@gmail.com> Reviewed-by: Georg Ringer <georg.ringer@gmail.com> --- .../core/Classes/Utility/GeneralUtility.php | 45 ++++++++++--------- 1 file changed, 24 insertions(+), 21 deletions(-) diff --git a/typo3/sysext/core/Classes/Utility/GeneralUtility.php b/typo3/sysext/core/Classes/Utility/GeneralUtility.php index 8c0c656d4e8e..cbdd618b9e44 100644 --- a/typo3/sysext/core/Classes/Utility/GeneralUtility.php +++ b/typo3/sysext/core/Classes/Utility/GeneralUtility.php @@ -2571,31 +2571,34 @@ class GeneralUtility */ public static function getFileAbsFileName($filename) { - if ((string)$filename === '') { + $fileName = (string)$filename; + if ($fileName === '') { return ''; } - // Extension - if (PathUtility::isExtensionPath($filename)) { + $checkForBackPath = fn (string $fileName): string => $fileName !== '' && static::validPathStr($fileName) ? $fileName : ''; + + // Extension "EXT:" path resolving. + if (PathUtility::isExtensionPath($fileName)) { try { - $filename = ExtensionManagementUtility::resolvePackagePath($filename); - } catch (PackageException $e) { - $filename = ''; - } - } elseif (!PathUtility::isAbsolutePath($filename)) { - // is relative. Prepended with the public web folder - $filename = Environment::getPublicPath() . '/' . $filename; - } elseif (!( - str_starts_with($filename, Environment::getProjectPath()) - || str_starts_with($filename, Environment::getPublicPath()) - )) { - // absolute, but set to blank if not allowed - $filename = ''; - } - if ((string)$filename !== '' && static::validPathStr($filename)) { - // checks backpath. - return $filename; + $fileName = ExtensionManagementUtility::resolvePackagePath($fileName); + } catch (PackageException) { + $fileName = ''; + } + return $checkForBackPath($fileName); } - return ''; + + // Absolute path, but set to blank if not inside allowed directories. + if (PathUtility::isAbsolutePath($fileName)) { + if (str_starts_with($fileName, Environment::getProjectPath()) || + str_starts_with($fileName, Environment::getPublicPath())) { + return $checkForBackPath($fileName); + } + return ''; + } + + // Relative path. Prepend with the public web folder. + $fileName = Environment::getPublicPath() . '/' . $fileName; + return $checkForBackPath($fileName); } /** -- GitLab