Skip to content

Commit

Permalink
adds some sanity checks before the actual scan
Browse files Browse the repository at this point in the history
  • Loading branch information
ata-no-one committed Dec 20, 2024
1 parent 774a7e7 commit b87cec0
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions lib/Service/VerdictService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)) {
Expand All @@ -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);

Expand Down Expand Up @@ -205,7 +211,7 @@ private function getScanOnlyThis(): array {
if (empty($scanOnlyThis)) {
return [];
}
return explode(",", $scanOnlyThis);
return explode(',', $scanOnlyThis);
}

/**
Expand All @@ -218,7 +224,7 @@ private function getDoNotScanThis(): array {
if (empty($doNotScanThis)) {
return [];
}
return explode(",", $doNotScanThis);
return explode(',', $doNotScanThis);
}

/**
Expand All @@ -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
Expand All @@ -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);
}
}

Expand All @@ -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;
Expand All @@ -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;
Expand Down

0 comments on commit b87cec0

Please sign in to comment.