From 5727edcedc79172addf0c3b9c6f531254ebbf1cf 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 1/5] 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> fix: Also check if null Signed-off-by: Git'Fellow <12234510+solracsf@users.noreply.github.com> --- lib/private/Files/Storage/Local.php | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/lib/private/Files/Storage/Local.php b/lib/private/Files/Storage/Local.php index a65d60bf278dd..57aedaf833453 100644 --- a/lib/private/Files/Storage/Local.php +++ b/lib/private/Files/Storage/Local.php @@ -400,8 +400,20 @@ 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 || !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) { From 9d6cb8a8d5b1d14af23b25005b2aa1997c99565c Mon Sep 17 00:00:00 2001 From: Git'Fellow <12234510+solracsf@users.noreply.github.com> Date: Thu, 1 Aug 2024 11:06:44 +0200 Subject: [PATCH 2/5] fix: add phpDoc MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Côme Chilliet <91878298+come-nc@users.noreply.github.com> Signed-off-by: Git'Fellow <12234510+solracsf@users.noreply.github.com> --- lib/private/Files/Storage/Local.php | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/private/Files/Storage/Local.php b/lib/private/Files/Storage/Local.php index 57aedaf833453..56d8c46ed2086 100644 --- a/lib/private/Files/Storage/Local.php +++ b/lib/private/Files/Storage/Local.php @@ -400,6 +400,7 @@ public function fopen($path, $mode) { return $result; } + /** @return string|false */ public function hash($type, $path, $raw = false): string|bool { $sourcePath = $this->getSourcePath($path); if ($sourcePath === null || !file_exists($sourcePath) || !is_readable($sourcePath)) { From 596111ca9509abe1d7c082b3aa00453e36794206 Mon Sep 17 00:00:00 2001 From: Git'Fellow <12234510+solracsf@users.noreply.github.com> Date: Thu, 1 Aug 2024 11:07:42 +0200 Subject: [PATCH 3/5] fix: remove check for null Signed-off-by: Git'Fellow <12234510+solracsf@users.noreply.github.com> --- lib/private/Files/Storage/Local.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/private/Files/Storage/Local.php b/lib/private/Files/Storage/Local.php index 56d8c46ed2086..6bbfb0544cdf4 100644 --- a/lib/private/Files/Storage/Local.php +++ b/lib/private/Files/Storage/Local.php @@ -403,7 +403,7 @@ public function fopen($path, $mode) { /** @return string|false */ public function hash($type, $path, $raw = false): string|bool { $sourcePath = $this->getSourcePath($path); - if ($sourcePath === null || !file_exists($sourcePath) || !is_readable($sourcePath)) { + 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; } From a4f4cbe22d1319c9adb81c6f11faa6a59ea7aeda Mon Sep 17 00:00:00 2001 From: Git'Fellow <12234510+solracsf@users.noreply.github.com> Date: Thu, 15 Aug 2024 00:05:31 +0200 Subject: [PATCH 4/5] fix: stronger type hint MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Côme Chilliet <91878298+come-nc@users.noreply.github.com> Signed-off-by: Git'Fellow <12234510+solracsf@users.noreply.github.com> --- lib/private/Files/Storage/Local.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/private/Files/Storage/Local.php b/lib/private/Files/Storage/Local.php index 6bbfb0544cdf4..41bac73d8bf81 100644 --- a/lib/private/Files/Storage/Local.php +++ b/lib/private/Files/Storage/Local.php @@ -401,7 +401,7 @@ public function fopen($path, $mode) { } /** @return string|false */ - public function hash($type, $path, $raw = false): string|bool { + public function hash($type, $path, $raw = false): string|false { $sourcePath = $this->getSourcePath($path); 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']); From 44afd9c8b7b21db4169e13fda0cd67fc219d0316 Mon Sep 17 00:00:00 2001 From: Git'Fellow <12234510+solracsf@users.noreply.github.com> Date: Thu, 15 Aug 2024 00:08:16 +0200 Subject: [PATCH 5/5] fix: lint Signed-off-by: Git'Fellow <12234510+solracsf@users.noreply.github.com> --- lib/private/Files/Storage/Local.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/private/Files/Storage/Local.php b/lib/private/Files/Storage/Local.php index 41bac73d8bf81..cc828b1371f81 100644 --- a/lib/private/Files/Storage/Local.php +++ b/lib/private/Files/Storage/Local.php @@ -400,20 +400,19 @@ public function fopen($path, $mode) { return $result; } - /** @return string|false */ public function hash($type, $path, $raw = false): string|false { $sourcePath = $this->getSourcePath($path); 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); }