From 9975dbe287f83af16e322d53000afb35513e22b2 Mon Sep 17 00:00:00 2001 From: Bart Noordsij Date: Fri, 8 Dec 2023 11:51:57 +0100 Subject: [PATCH 1/3] allow passing stream directly to scanner --- src/ClamavValidator/ClamavValidator.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/ClamavValidator/ClamavValidator.php b/src/ClamavValidator/ClamavValidator.php index 8fd7b34..471d14c 100755 --- a/src/ClamavValidator/ClamavValidator.php +++ b/src/ClamavValidator/ClamavValidator.php @@ -72,14 +72,15 @@ public function validateClamav(string $attribute, $value, array $parameters): bo protected function validateFileWithClamAv($value): bool { $file = $this->getFilePath($value); - if (! is_readable($file)) { + if (! is_resource($file) && ! is_readable($file)) { throw ClamavValidatorException::forNonReadableFile($file); } try { $socket = $this->getClamavSocket(); $scanner = $this->createQuahogScannerClient($socket); - $result = $scanner->scanResourceStream(fopen($file, 'rb')); + $stream = is_resource($file) ? $file : fopen($file, 'rb'); + $result = $scanner->scanResourceStream($stream); } catch (Exception $exception) { if (Config::get('clamav.client_exceptions')) { throw ClamavValidatorException::forClientException($exception); @@ -131,6 +132,10 @@ protected function getFilePath($file): string return $file->getRealPath(); } + if (is_resource($file)) { + return $file; + } + // if we're passed a PHP file upload array, return the "tmp_name" if (is_array($file) && null !== Arr::get($file, 'tmp_name')) { return $file['tmp_name']; From d9bd84e56ac216c8c8d36ad9a25e577c92871c7f Mon Sep 17 00:00:00 2001 From: Bart Noordsij Date: Wed, 13 Dec 2023 06:53:16 +0100 Subject: [PATCH 2/3] moved resource check out of getFilename method to validateFileWithClamAv --- src/ClamavValidator/ClamavValidator.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/ClamavValidator/ClamavValidator.php b/src/ClamavValidator/ClamavValidator.php index 471d14c..f3556fd 100755 --- a/src/ClamavValidator/ClamavValidator.php +++ b/src/ClamavValidator/ClamavValidator.php @@ -45,7 +45,7 @@ public function __construct( */ public function validateClamav(string $attribute, $value, array $parameters): bool { - if (filter_var(Config::get('clamav.skip_validation'), FILTER_VALIDATE_BOOLEAN)) { + if (filter_var(Config::get('clamav.skip_validation'), FILTER_VALIDATE_BOOLEAN)) { return true; } @@ -71,9 +71,13 @@ public function validateClamav(string $attribute, $value, array $parameters): bo */ protected function validateFileWithClamAv($value): bool { - $file = $this->getFilePath($value); - if (! is_resource($file) && ! is_readable($file)) { - throw ClamavValidatorException::forNonReadableFile($file); + if (is_resource($value)) { + $file = $value; + } else { + $file = $this->getFilePath($value); + if (! is_readable($file)) { + throw ClamavValidatorException::forNonReadableFile($file); + } } try { @@ -132,10 +136,6 @@ protected function getFilePath($file): string return $file->getRealPath(); } - if (is_resource($file)) { - return $file; - } - // if we're passed a PHP file upload array, return the "tmp_name" if (is_array($file) && null !== Arr::get($file, 'tmp_name')) { return $file['tmp_name']; From 324315b08b918f4703d5f33e6b22342e24794c04 Mon Sep 17 00:00:00 2001 From: bnoordsij Date: Mon, 18 Dec 2023 09:08:16 +0100 Subject: [PATCH 3/3] removed double spaces --- src/ClamavValidator/ClamavValidator.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ClamavValidator/ClamavValidator.php b/src/ClamavValidator/ClamavValidator.php index f3556fd..e9b0ca6 100755 --- a/src/ClamavValidator/ClamavValidator.php +++ b/src/ClamavValidator/ClamavValidator.php @@ -81,10 +81,10 @@ protected function validateFileWithClamAv($value): bool } try { - $socket = $this->getClamavSocket(); + $socket = $this->getClamavSocket(); $scanner = $this->createQuahogScannerClient($socket); $stream = is_resource($file) ? $file : fopen($file, 'rb'); - $result = $scanner->scanResourceStream($stream); + $result = $scanner->scanResourceStream($stream); } catch (Exception $exception) { if (Config::get('clamav.client_exceptions')) { throw ClamavValidatorException::forClientException($exception);