Skip to content

Commit

Permalink
WIP try with explicit scan in CacheEntryInsertedEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
lennartdohmann committed Nov 5, 2024
1 parent cd80b83 commit baeb280
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use OCA\GDataVaas\AvirWrapper;
use OCA\GDataVaas\CacheEntryListener;
use OCA\GDataVaas\Db\DbFileMapper;
use OCA\GDataVaas\Listener\CacheEntryInsertedListener;
use OCA\GDataVaas\Service\MailService;
use OCA\GDataVaas\Service\TagService;
use OCA\GDataVaas\Service\VerdictService;
Expand All @@ -20,6 +21,7 @@
use OCP\AppFramework\Bootstrap\IRegistrationContext;
use OCP\Collaboration\Resources\LoadAdditionalScriptsEvent;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Files\Cache\CacheEntryInsertedEvent;
use OCP\Files\IHomeStorage;
use OCP\Files\Storage\IStorage;
use OCP\IAppConfig;
Expand All @@ -46,7 +48,8 @@ public function __construct() {
$eventDispatcher->addListener(LoadAdditionalScriptsEvent::class, function () {
Util::addScript(self::APP_ID, 'gdatavaas-files-action');
});
}
//$eventDispatcher->addServiceListener(CacheEntryInsertedEvent::class, CacheEntryInsertedListener::class);
}

/**
* Load the composer autoloader if it exists
Expand Down
49 changes: 49 additions & 0 deletions lib/Listener/CacheEntryInsertedListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace OCA\GDataVaas\Listener;

use Exception;
use OCA\GDataVaas\Service\VerdictService;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use OCP\Files\Cache\CacheEntryInsertedEvent;
use OCP\Files\EntityTooLargeException;
use OCP\Files\NotFoundException;
use OCP\Files\NotPermittedException;
use Psr\Log\LoggerInterface;
use VaasSdk\Exceptions\FileDoesNotExistException;
use VaasSdk\Exceptions\InvalidSha256Exception;
use VaasSdk\Exceptions\TimeoutException;
use VaasSdk\Exceptions\UploadFailedException;
use VaasSdk\Exceptions\VaasAuthenticationException;

class CacheEntryInsertedListener implements IEventListener
{
private readonly LoggerInterface $logger;
private VerdictService $verdictService;

public function __construct(LoggerInterface $logger, VerdictService $verdictService)
{
$this->logger = $logger;
$this->verdictService = $verdictService;
}

public function handle(Event $event): void
{
if (!($event instanceof CacheEntryInsertedEvent)) {
return;
}

$this->logger->info('Cache entry inserted: ' . $event->getPath());

$fileId = $event->getFileId();
try {
$verdict = $this->verdictService->scanFileById($fileId);
// Delete file if malicious
} catch (EntityTooLargeException|NotFoundException|NotPermittedException|FileDoesNotExistException|InvalidSha256Exception|TimeoutException|UploadFailedException|VaasAuthenticationException $e) {
$this->logger->error($e->getMessage());
} catch (Exception $e) {
$this->logger->error('An unexpected error occurred while scanning file ' . $fileId . ' with GData VaaS. Please check the logs for more information and contact your administrator: ' . $e->getMessage());
}
}
}

0 comments on commit baeb280

Please sign in to comment.