Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adds some sanity checks before the actual scan #169

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading