From cf0a9b53761ad5684146ecf739fe50a8705a9cba Mon Sep 17 00:00:00 2001 From: Valentin Giguere Date: Wed, 24 May 2023 14:48:01 +0200 Subject: [PATCH 1/2] Add a check for eof to avoid adding empty string to the socket data. --- src/ClamAV/ClamAV.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/ClamAV/ClamAV.php b/src/ClamAV/ClamAV.php index 83630ac..55a1562 100644 --- a/src/ClamAV/ClamAV.php +++ b/src/ClamAV/ClamAV.php @@ -104,7 +104,10 @@ public function fileScanInStream(string $file): bool \socket_send($socket, $command, \strlen($command), 0); while (!\feof($handle)) { - $data = \fread($handle, $chunkSize); + if ("" === ($data = \fread($handle, $chunkSize))) { + continue; + } + $packet = \pack(\sprintf("Na%d", $chunkSize), $chunkSize, $data); \socket_send($socket, $packet, $chunkSize + 4, 0); } From ef971c2e6b98098a54e8ad84411c5a7525be743d Mon Sep 17 00:00:00 2001 From: vagiguere Date: Thu, 25 Jul 2024 16:15:36 +0200 Subject: [PATCH 2/2] fix: Do not chunk file to avoid detection failures --- src/ClamAV/ClamAV.php | 33 +++++++++++++-------------------- 1 file changed, 13 insertions(+), 20 deletions(-) diff --git a/src/ClamAV/ClamAV.php b/src/ClamAV/ClamAV.php index 55a1562..ef6df88 100644 --- a/src/ClamAV/ClamAV.php +++ b/src/ClamAV/ClamAV.php @@ -95,31 +95,24 @@ public function shutdown(): ?string */ public function fileScanInStream(string $file): bool { - $socket = $this->getSocket(); + $file_handler = fopen($file, 'r'); + $scanner_handler = socket_export_stream($this->getSocket()); - $handle = \fopen($file, 'rb'); - $chunkSize = \filesize($file) < 8192 ? \filesize($file) : 8192; - $command = "zINSTREAM\0"; + // Push to the ClamAV socket. + $bytes = filesize($file); + fwrite($scanner_handler, "zINSTREAM\0"); + fwrite($scanner_handler, pack("N", $bytes)); + stream_copy_to_stream($file_handler, $scanner_handler); - \socket_send($socket, $command, \strlen($command), 0); + // Send a zero-length block to indicate that we're done sending file data. + fwrite($scanner_handler, pack("N", 0)); - while (!\feof($handle)) { - if ("" === ($data = \fread($handle, $chunkSize))) { - continue; - } + // Request a response from the service. + $response = trim(fgets($scanner_handler)); - $packet = \pack(\sprintf("Na%d", $chunkSize), $chunkSize, $data); - \socket_send($socket, $packet, $chunkSize + 4, 0); - } - - \socket_send($socket, \pack("Nx", 0), 5, 0); - \socket_recv($socket, $out, 20000, 0); - \socket_close($socket); + fclose($scanner_handler); - $out = \explode(':', $out); - $stats = \end($out); - - return \trim($stats) === 'OK'; + return preg_match('/^stream: OK$/', $response); } /**