From b87cec0097b1527be3fc5202126ff0a5e1c06807 Mon Sep 17 00:00:00 2001 From: PT-ATA No One Date: Fri, 20 Dec 2024 12:18:06 +0000 Subject: [PATCH] adds some sanity checks before the actual scan --- lib/Service/VerdictService.php | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/lib/Service/VerdictService.php b/lib/Service/VerdictService.php index d9e9fbaa..4b536a4a 100644 --- a/lib/Service/VerdictService.php +++ b/lib/Service/VerdictService.php @@ -37,7 +37,7 @@ class VerdictService { private ?Vaas $vaas = null; private LoggerInterface $logger; - private string $lastLocalPath = ""; + private string $lastLocalPath = ''; private ?VaasVerdict $lastVaasVerdict = null; public function __construct(LoggerInterface $logger, IAppConfig $appConfig, FileService $fileService, TagService $tagService) { @@ -71,9 +71,15 @@ public function __construct(LoggerInterface $logger, IAppConfig $appConfig, File public function scanFileById(int $fileId): VaasVerdict { $node = $this->fileService->getNodeFromFileId($fileId); $filePath = $node->getStorage()->getLocalFile($node->getInternalPath()); + if (file_exists($filePath) === false) { + $this->logger->debug('Could not Scan File. File does not exist: ' . $filePath); + } + if (is_dir($filePath)) { + $this->logger->debug('Could not Scan File. File is a directory: ' . $filePath); + } if (self::isFileTooLargeToScan($filePath)) { $this->tagService->setTag($fileId, TagService::WONT_SCAN, silent: true); - throw new EntityTooLargeException("File is too large"); + throw new EntityTooLargeException('File is too large'); } if (!$this->isAllowedToScan($filePath)) { @@ -82,9 +88,9 @@ public function scanFileById(int $fileId): VaasVerdict { $verdict = $this->scan($filePath); - $this->logger->info("VaaS scan result for " . $node->getName() . " (" . $fileId . "): Verdict: " - . $verdict->Verdict->value . ", Detection: " . $verdict->Detection . ", SHA256: " . $verdict->Sha256 . - ", FileType: " . $verdict->FileType . ", MimeType: " . $verdict->MimeType . ", UUID: " . $verdict->Guid); + $this->logger->info('VaaS scan result for ' . $node->getName() . ' (' . $fileId . '): Verdict: ' + . $verdict->Verdict->value . ', Detection: ' . $verdict->Detection . ', SHA256: ' . $verdict->Sha256 . + ', FileType: ' . $verdict->FileType . ', MimeType: ' . $verdict->MimeType . ', UUID: ' . $verdict->Guid); $this->tagFile($fileId, $verdict->Verdict->value); @@ -205,7 +211,7 @@ private function getScanOnlyThis(): array { if (empty($scanOnlyThis)) { return []; } - return explode(",", $scanOnlyThis); + return explode(',', $scanOnlyThis); } /** @@ -218,7 +224,7 @@ private function getDoNotScanThis(): array { if (empty($doNotScanThis)) { return []; } - return explode(",", $doNotScanThis); + return explode(',', $doNotScanThis); } /** @@ -238,7 +244,7 @@ public function removeWhitespacesAroundComma(string $s): string { public function getAuthenticator(string $authMethod): ClientCredentialsGrantAuthenticator|ResourceOwnerPasswordGrantAuthenticator { if ($authMethod === 'ResourceOwnerPassword') { return new ResourceOwnerPasswordGrantAuthenticator( - "nextcloud-customer", + 'nextcloud-customer', $this->username, $this->password, $this->tokenEndpoint @@ -250,7 +256,7 @@ public function getAuthenticator(string $authMethod): ClientCredentialsGrantAuth $this->tokenEndpoint ); } else { - throw new VaasAuthenticationException("Invalid auth method: " . $authMethod); + throw new VaasAuthenticationException('Invalid auth method: ' . $authMethod); } } @@ -272,7 +278,7 @@ public function createAndConnectVaas(): Vaas { */ public function isAllowedToScan(string $filePath): bool { $doNotScanThis = $this->getDoNotScanThis(); - $this->logger->debug("doNotScanThis: " . implode(", ", $doNotScanThis)); + $this->logger->debug('doNotScanThis: ' . implode(', ', $doNotScanThis)); foreach ($doNotScanThis as $doNotScanThisItem) { if (str_contains(strtolower($filePath), strtolower($doNotScanThisItem))) { return false; @@ -282,7 +288,7 @@ public function isAllowedToScan(string $filePath): bool { if (count($scanOnlyThis) === 0) { return true; } - $this->logger->debug("scanOnlyThis: " . implode(", ", $scanOnlyThis)); + $this->logger->debug('scanOnlyThis: ' . implode(', ', $scanOnlyThis)); foreach ($scanOnlyThis as $scanOnlyThisItem) { if (str_contains(strtolower($filePath), strtolower($scanOnlyThisItem))) { return true;