Skip to content
Merged
Show file tree
Hide file tree
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
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ all: build

# Fetches dependencies and builds it
.PHONY: build
build:
build: oc
ifneq (,$(wildcard $(CURDIR)/composer.json))
make composer
endif
Expand All @@ -36,7 +36,7 @@ endif
# Installs and updates the composer dependencies. If composer is not installed
# a copy is fetched from the web
.PHONY: composer
composer:
composer: oc
ifeq (,$(composer))
@echo "No composer command available, downloading a copy from the web"
mkdir -p $(build_tools_directory)
Expand Down Expand Up @@ -95,7 +95,6 @@ distclean: clean
rm -rf node_modules
rm -rf js/vendor
rm -rf js/node_modules
rm -rf nextcloud-server
rm -rf build
rm -f composer.lock

Expand All @@ -120,6 +119,7 @@ appstore: build
--exclude-vcs \
--exclude="$(source_build_directory)/.git" \
--exclude="$(source_build_directory)/.github" \
--exclude="$(source_build_directory)/nextcloud-server" \
--exclude="$(source_build_directory)/composer.json" \
--exclude="$(source_build_directory)/composer.json.license" \
--exclude="$(source_build_directory)/babel.config.js" \
Expand Down
24 changes: 23 additions & 1 deletion lib/Controller/ScanController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

use Exception;
use OCA\GDataVaas\AppInfo\Application;
use OCA\GDataVaas\Service\FileService;
use OCA\GDataVaas\Service\VerdictService;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
Expand All @@ -17,17 +18,30 @@
use OCP\Files\NotPermittedException;
use OCP\IAppConfig;
use OCP\IRequest;
use Psr\Log\LoggerInterface;
use VaasSdk\Exceptions\VaasAuthenticationException;
use VaasSdk\Verdict;

class ScanController extends Controller {
private readonly LoggerInterface $logger;
private IAppConfig $config;
private VerdictService $verdictService;
private FileService $fileService;

public function __construct($appName, IRequest $request, VerdictService $verdictService, IAppConfig $config) {
public function __construct(
$appName,
IRequest $request,
VerdictService $verdictService,
IAppConfig $config,
FileService $fileService,
LoggerInterface $logger,
) {
parent::__construct($appName, $request);

$this->logger = $logger;
$this->config = $config;
$this->verdictService = $verdictService;
$this->fileService = $fileService;
}

/**
Expand All @@ -39,6 +53,14 @@ public function __construct($appName, IRequest $request, VerdictService $verdict
public function scan(int $fileId): JSONResponse {
try {
$verdict = $this->verdictService->scanFileById($fileId);
if ($verdict->verdict === Verdict::MALICIOUS) {
try {
$this->fileService->setMaliciousPrefixIfActivated($fileId);
$this->fileService->moveFileToQuarantineFolderIfDefined($fileId);
} catch (Exception $e) {
$this->logger->error("Failed to handle malicious file '{$fileId}': {$e->getMessage()}");
}
}
return new JSONResponse(['verdict' => $verdict->verdict->value], 200);
} catch (EntityTooLargeException) {
return new JSONResponse(
Expand Down
27 changes: 21 additions & 6 deletions lib/EventListener/FileEventsListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,27 @@ public function handle(Event $event): void {
}

if ($verdict->verdict->value == TagService::MALICIOUS) {
$this->sendErrorResponse(new VirusFoundException($verdict, $node->getName(), $node->getId()));
$this->fileService->deleteFile($node->getId());
if ($this->appConfig->getValueBool(Application::APP_ID, 'sendMailOnVirusUpload')) {
$this->mailService->notifyMaliciousUpload(
$verdict, $node->getPath(), $this->userSession->getUser()->getUID(), $node->getSize()
);
try {
$this->sendErrorResponse(new VirusFoundException($verdict, $node->getName(), $node->getId()));
$this->fileService->deleteFile($node->getId());
if ($this->appConfig->getValueBool(Application::APP_ID, 'sendMailOnVirusUpload')) {
$this->mailService->notifyMaliciousUpload(
$verdict, $node->getPath(), $this->userSession->getUser()->getUID(), $node->getSize()
);
}
} catch (Exception $e) {
try {
$this->fileService->setMaliciousPrefixIfActivated($node->getId());
$this->fileService->moveFileToQuarantineFolderIfDefined($node->getId());
$this->logger->error(
"Failed to block upload of malicious file '{$node->getName()}'.
File was quarantined instead. Error: {$e->getMessage()}"
);
} catch (Exception $e) {
$this->logger->error(
"Failed to set malicious prefix or move file to quarantine for '{$node->getName()}'. Error: {$e->getMessage()}"
);
}
}
exit;
}
Expand Down
11 changes: 10 additions & 1 deletion lib/Service/ScanService.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use OCP\IAppConfig;
use Psr\Log\LoggerInterface;
use VaasSdk\Exceptions\VaasAuthenticationException;
use VaasSdk\Verdict;

class ScanService {

Expand Down Expand Up @@ -60,7 +61,15 @@ public function run(): int {
$fileIds = $this->getFileIdsToScan();
foreach ($fileIds as $fileId) {
try {
$this->verdictService->scanFileById($fileId);
$verdict = $this->verdictService->scanFileById($fileId);
if ($verdict->verdict === Verdict::MALICIOUS) {
try {
$this->fileService->setMaliciousPrefixIfActivated($fileId);
$this->fileService->moveFileToQuarantineFolderIfDefined($fileId);
} catch (Exception $e) {
$this->logger->error("Failed to handle malicious file '{$fileId}': {$e->getMessage()}");
}
}
$scanned += 1;
} catch (EntityTooLargeException) {
$this->logger->error(
Expand Down
5 changes: 0 additions & 5 deletions lib/Service/VerdictService.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,11 +246,6 @@ private function tagFile(int $fileId, string $tagName): void {
switch ($tagName) {
case TagService::MALICIOUS:
$this->tagService->setTag($fileId, TagService::MALICIOUS, silent: false);
try {
$this->fileService->setMaliciousPrefixIfActivated($fileId);
$this->fileService->moveFileToQuarantineFolderIfDefined($fileId);
} catch (Exception) {
}
break;
case TagService::PUP:
$this->tagService->setTag($fileId, TagService::PUP, silent: false);
Expand Down