From c0da548c0f87572ed8e543ab6594af61130cb27c Mon Sep 17 00:00:00 2001 From: Philip Stadermann Date: Wed, 12 Jun 2024 13:43:44 +0200 Subject: [PATCH] On-upload: Tag files immediately #38 On-upload: If file is too big, tag uploaded file as "Won't scan" # Test cases ## Small files Clean -> Clean Pup -> Pup Malicious -> blocks upload ## Large files Any -> Won't scan ## VaaS error (e.g. wrong credentials) Any -> Unscanned --- lib/Service/VerdictService.php | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/lib/Service/VerdictService.php b/lib/Service/VerdictService.php index 0e4fa78a..f6433354 100644 --- a/lib/Service/VerdictService.php +++ b/lib/Service/VerdictService.php @@ -73,7 +73,7 @@ public function __construct(LoggerInterface $logger, IConfig $appConfig, FileSer public function scanFileById(int $fileId): VaasVerdict { $node = $this->fileService->getNodeFromFileId($fileId); $filePath = $node->getStorage()->getLocalFile($node->getInternalPath()); - if ($node->getSize() > self::MAX_FILE_SIZE) { + if (self::isFileTooLargeToScan($filePath)) { $this->tagService->removeAllTagsFromFile($fileId); $this->tagService->setTag($fileId, TagService::WONT_SCAN); throw new EntityTooLargeException("File is too large"); @@ -112,9 +112,6 @@ private function tagFile(int $fileId, string $tagName) { $this->tagService->removeAllTagsFromFile($fileId); switch ($tagName) { - case TagService::CLEAN: - $this->tagService->setTag($fileId, TagService::CLEAN); - break; case TagService::MALICIOUS: $this->tagService->setTag($fileId, TagService::MALICIOUS); try { @@ -123,15 +120,20 @@ private function tagFile(int $fileId, string $tagName) { } catch (Exception) { } break; + case TagService::CLEAN: case TagService::PUP: - $this->tagService->setTag($fileId, TagService::PUP); - break; + case TagService::WONT_SCAN: default: - $this->tagService->setTag($fileId, TagService::UNSCANNED); + $this->tagService->setTag($fileId, $tagName); break; } } + public static function isFileTooLargeToScan($path) { + $size = filesize($path); + return !$size || $size > self::MAX_FILE_SIZE; + } + public function scan(string $filePath): VaasVerdict { $this->lastLocalPath = $filePath; $this->lastVaasVerdict = null; @@ -161,6 +163,10 @@ public function onRename(string $localSource, string $localTarget) } public function tagLastScannedFile(string $localPath, int $fileId) { + if (self::isFileTooLargeToScan($localPath)) { + $this->tagFile($fileId, TagService::WONT_SCAN); + return; + } if ($localPath === $this->lastLocalPath) { if ($this->lastVaasVerdict !== null) { $this->tagFile($fileId, $this->lastVaasVerdict->Verdict->value);