Skip to content

Commit 3358883

Browse files
Outsource the consequences of a file scan from tag function, as this is not just tagging and caused a bug in the event handler where the quarantine was created empty.
1 parent 8c74827 commit 3358883

File tree

5 files changed

+57
-16
lines changed

5 files changed

+57
-16
lines changed

Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ all: build
1515

1616
# Fetches dependencies and builds it
1717
.PHONY: build
18-
build:
18+
build: oc
1919
ifneq (,$(wildcard $(CURDIR)/composer.json))
2020
make composer
2121
endif
@@ -36,7 +36,7 @@ endif
3636
# Installs and updates the composer dependencies. If composer is not installed
3737
# a copy is fetched from the web
3838
.PHONY: composer
39-
composer:
39+
composer: oc
4040
ifeq (,$(composer))
4141
@echo "No composer command available, downloading a copy from the web"
4242
mkdir -p $(build_tools_directory)
@@ -95,7 +95,6 @@ distclean: clean
9595
rm -rf node_modules
9696
rm -rf js/vendor
9797
rm -rf js/node_modules
98-
rm -rf nextcloud-server
9998
rm -rf build
10099
rm -f composer.lock
101100

@@ -120,6 +119,7 @@ appstore: build
120119
--exclude-vcs \
121120
--exclude="$(source_build_directory)/.git" \
122121
--exclude="$(source_build_directory)/.github" \
122+
--exclude="$(source_build_directory)/nextcloud-server" \
123123
--exclude="$(source_build_directory)/composer.json" \
124124
--exclude="$(source_build_directory)/composer.json.license" \
125125
--exclude="$(source_build_directory)/babel.config.js" \

lib/Controller/ScanController.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
use Exception;
1010
use OCA\GDataVaas\AppInfo\Application;
11+
use OCA\GDataVaas\Service\FileService;
1112
use OCA\GDataVaas\Service\VerdictService;
1213
use OCP\AppFramework\Controller;
1314
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
@@ -17,17 +18,30 @@
1718
use OCP\Files\NotPermittedException;
1819
use OCP\IAppConfig;
1920
use OCP\IRequest;
21+
use Psr\Log\LoggerInterface;
2022
use VaasSdk\Exceptions\VaasAuthenticationException;
23+
use VaasSdk\Verdict;
2124

2225
class ScanController extends Controller {
26+
private readonly LoggerInterface $logger;
2327
private IAppConfig $config;
2428
private VerdictService $verdictService;
29+
private FileService $fileService;
2530

26-
public function __construct($appName, IRequest $request, VerdictService $verdictService, IAppConfig $config) {
31+
public function __construct(
32+
$appName,
33+
IRequest $request,
34+
VerdictService $verdictService,
35+
IAppConfig $config,
36+
FileService $fileService,
37+
LoggerInterface $logger,
38+
) {
2739
parent::__construct($appName, $request);
2840

41+
$this->logger = $logger;
2942
$this->config = $config;
3043
$this->verdictService = $verdictService;
44+
$this->fileService = $fileService;
3145
}
3246

3347
/**
@@ -39,6 +53,14 @@ public function __construct($appName, IRequest $request, VerdictService $verdict
3953
public function scan(int $fileId): JSONResponse {
4054
try {
4155
$verdict = $this->verdictService->scanFileById($fileId);
56+
if ($verdict->verdict === Verdict::MALICIOUS) {
57+
try {
58+
$this->fileService->setMaliciousPrefixIfActivated($fileId);
59+
$this->fileService->moveFileToQuarantineFolderIfDefined($fileId);
60+
} catch (Exception $e) {
61+
$this->logger->error("Failed to handle malicious file '{$fileId}': {$e->getMessage()}");
62+
}
63+
}
4264
return new JSONResponse(['verdict' => $verdict->verdict->value], 200);
4365
} catch (EntityTooLargeException) {
4466
return new JSONResponse(

lib/EventListener/FileEventsListener.php

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,27 @@ public function handle(Event $event): void {
8080
}
8181

8282
if ($verdict->verdict->value == TagService::MALICIOUS) {
83-
$this->sendErrorResponse(new VirusFoundException($verdict, $node->getName(), $node->getId()));
84-
$this->fileService->deleteFile($node->getId());
85-
if ($this->appConfig->getValueBool(Application::APP_ID, 'sendMailOnVirusUpload')) {
86-
$this->mailService->notifyMaliciousUpload(
87-
$verdict, $node->getPath(), $this->userSession->getUser()->getUID(), $node->getSize()
88-
);
83+
try {
84+
$this->sendErrorResponse(new VirusFoundException($verdict, $node->getName(), $node->getId()));
85+
$this->fileService->deleteFile($node->getId());
86+
if ($this->appConfig->getValueBool(Application::APP_ID, 'sendMailOnVirusUpload')) {
87+
$this->mailService->notifyMaliciousUpload(
88+
$verdict, $node->getPath(), $this->userSession->getUser()->getUID(), $node->getSize()
89+
);
90+
}
91+
} catch (Exception $e) {
92+
try {
93+
$this->fileService->setMaliciousPrefixIfActivated($node->getId());
94+
$this->fileService->moveFileToQuarantineFolderIfDefined($node->getId());
95+
$this->logger->error(
96+
"Failed to block upload of malicious file '{$node->getName()}'.
97+
File was quarantined instead. Error: {$e->getMessage()}"
98+
);
99+
} catch (Exception $e) {
100+
$this->logger->error(
101+
"Failed to set malicious prefix or move file to quarantine for '{$node->getName()}'. Error: {$e->getMessage()}"
102+
);
103+
}
89104
}
90105
exit;
91106
}

lib/Service/ScanService.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use OCP\IAppConfig;
1515
use Psr\Log\LoggerInterface;
1616
use VaasSdk\Exceptions\VaasAuthenticationException;
17+
use VaasSdk\Verdict;
1718

1819
class ScanService {
1920

@@ -60,7 +61,15 @@ public function run(): int {
6061
$fileIds = $this->getFileIdsToScan();
6162
foreach ($fileIds as $fileId) {
6263
try {
63-
$this->verdictService->scanFileById($fileId);
64+
$verdict = $this->verdictService->scanFileById($fileId);
65+
if ($verdict->verdict === Verdict::MALICIOUS) {
66+
try {
67+
$this->fileService->setMaliciousPrefixIfActivated($fileId);
68+
$this->fileService->moveFileToQuarantineFolderIfDefined($fileId);
69+
} catch (Exception $e) {
70+
$this->logger->error("Failed to handle malicious file '{$fileId}': {$e->getMessage()}");
71+
}
72+
}
6473
$scanned += 1;
6574
} catch (EntityTooLargeException) {
6675
$this->logger->error(

lib/Service/VerdictService.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -246,11 +246,6 @@ private function tagFile(int $fileId, string $tagName): void {
246246
switch ($tagName) {
247247
case TagService::MALICIOUS:
248248
$this->tagService->setTag($fileId, TagService::MALICIOUS, silent: false);
249-
try {
250-
$this->fileService->setMaliciousPrefixIfActivated($fileId);
251-
$this->fileService->moveFileToQuarantineFolderIfDefined($fileId);
252-
} catch (Exception) {
253-
}
254249
break;
255250
case TagService::PUP:
256251
$this->tagService->setTag($fileId, TagService::PUP, silent: false);

0 commit comments

Comments
 (0)