From 7da44b6c96bab444e92d8909c28cdf00756455ae Mon Sep 17 00:00:00 2001 From: Git'Fellow <12234510+solracsf@users.noreply.github.com> Date: Mon, 29 Jul 2024 14:26:34 +0200 Subject: [PATCH] fix(files): Catch null possibilities before hash Signed-off-by: Git'Fellow <12234510+solracsf@users.noreply.github.com> fix: don't use OCP Signed-off-by: Git'Fellow <12234510+solracsf@users.noreply.github.com> --- lib/private/Files/Storage/Local.php | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/lib/private/Files/Storage/Local.php b/lib/private/Files/Storage/Local.php index a65d60bf278dd..7921fae1afe8a 100644 --- a/lib/private/Files/Storage/Local.php +++ b/lib/private/Files/Storage/Local.php @@ -400,8 +400,25 @@ public function fopen($path, $mode) { return $result; } - public function hash($type, $path, $raw = false) { - return hash_file($type, $this->getSourcePath($path), $raw); + public function hash($type, $path, $raw = false): string|bool { + $sourcePath = $this->getSourcePath($path); + if ($sourcePath === null) { + \OC::$server->get(LoggerInterface::class)->error('Source path returned null for path: ' . $sourcePath, ['app' => 'core']); + return false; + } + + if (!file_exists($sourcePath) || !is_readable($sourcePath)) { + \OC::$server->get(LoggerInterface::class)->error('Source path does not exist or is not readable: ' . $sourcePath, ['app' => 'core']); + return false; + } + + $validAlgorithms = hash_algos(); + if (!in_array($type, $validAlgorithms)) { + \OC::$server->get(LoggerInterface::class)->error('Invalid hash algorithm: ' . $type, ['app' => 'core']); + return false; + } + + return hash_file($type, $sourcePath, $raw); } public function free_space($path) {