-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathScanService.php
More file actions
156 lines (140 loc) · 4.8 KB
/
ScanService.php
File metadata and controls
156 lines (140 loc) · 4.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php
// SPDX-FileCopyrightText: 2025 Lennart Dohmann <lennart.dohmann@gdata.de>
//
// SPDX-License-Identifier: AGPL-3.0-or-later
namespace OCA\GDataVaas\Service;
use OCA\GDataVaas\AppInfo\Application;
use OCP\DB\Exception;
use OCP\Files\EntityTooLargeException;
use OCP\Files\NotFoundException;
use OCP\Files\NotPermittedException;
use OCP\IAppConfig;
use Psr\Log\LoggerInterface;
use VaasSdk\Exceptions\VaasAuthenticationException;
use VaasSdk\Verdict;
class ScanService {
private const SCAN_TIME_SECONDS = 120;
private TagService $tagService;
private VerdictService $verdictService;
private FileService $fileService;
private IAppConfig $appConfig;
private LoggerInterface $logger;
public function __construct(
LoggerInterface $logger,
TagService $tagService,
VerdictService $verdictService,
FileService $fileService,
IAppConfig $appConfig,
) {
$this->logger = $logger;
$this->tagService = $tagService;
$this->verdictService = $verdictService;
$this->fileService = $fileService;
$this->appConfig = $appConfig;
}
/**
* @param LoggerInterface $logger
* @return ScanService
*/
public function withLogger(LoggerInterface $logger): ScanService {
$this->logger = $logger;
return $this;
}
/**
* Run the scan service to scan files.
* @return int
* @throws Exception
*/
public function run(): int {
$startTime = time();
$scanned = 0;
$fileIds = $this->getFileIdsToScan();
foreach ($fileIds as $fileId) {
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()}");
}
}
$scanned += 1;
} catch (EntityTooLargeException) {
$this->logger->error(
"File $fileId is larger than
" . $this->appConfig->getValueInt(Application::APP_ID, 'maxScanSizeInMB', 256) . 'MB.'
);
} catch (NotFoundException) {
$this->logger->error("File $fileId not found");
} catch (NotPermittedException) {
$this->logger->error("Current settings do not permit scanning file wit ID $fileId.");
} catch (VaasAuthenticationException) {
$this->logger->error('Authentication for VaaS scan failed. Please check your credentials.');
} catch (\Exception $e) {
$this->logger->error('Unexpected error while scanning file with id ' . $fileId . ': ' . $e->getMessage());
}
$elapsed = time() - $startTime;
if ($elapsed > self::SCAN_TIME_SECONDS) {
break;
}
}
$this->logger->debug('Successfully scanned ' . $scanned . ' files');
return $scanned;
}
/**
* Get file IDs that need to be scanned.
* @return array
* @throws Exception
*/
private function getFileIdsToScan(): array {
$unscannedTagIsDisabled = $this->appConfig->getValueBool(Application::APP_ID, 'disableUnscannedTag');
$maliciousTag = $this->tagService->getTag(TagService::MALICIOUS);
$pupTag = $this->tagService->getTag(TagService::PUP);
$cleanTag = $this->tagService->getTag(TagService::CLEAN);
$wontScanTag = $this->tagService->getTag(TagService::WONT_SCAN);
$limit = 50;
$offset = 0;
$tagParam = $unscannedTagIsDisabled
? [$maliciousTag->getId(), $cleanTag->getId(), $pupTag->getId(), $wontScanTag->getId()]
: TagService::UNSCANNED;
$startTime = time();
$fileIdsToScan = [];
while (true) {
$fileIds
= $unscannedTagIsDisabled
? $this->tagService->getFileIdsWithoutTags($tagParam, $limit, $offset)
: $this->tagService->getFileIdsWithTag($tagParam, $limit, $offset);
if (empty($fileIds)) {
break;
}
foreach ($fileIds as $fileId) {
try {
$node = $this->fileService->getNodeFromFileId($fileId);
$filePath = $node->getStorage()->getLocalFile($node->getInternalPath());
if (is_readable($filePath) && $this->verdictService->isAllowedToScan($filePath)) {
$fileIdsToScan[] = $fileId;
} else {
$this->logger->debug("File with ID $fileId is not readable or not allowed to scan, skipping.");
}
} catch (NotFoundException $e) {
$this->logger->error("File with ID $fileId not found, skipping: " . $e->getMessage());
} catch (NotPermittedException $e) {
$this->logger->error(
"Current settings do not permit scanning file with ID $fileId, skipping: " . $e->getMessage()
);
} catch (\Exception $e) {
$this->logger->error("Unexpected error while processing file with ID $fileId: " . $e->getMessage());
}
}
$offset += $limit;
$elapsed = time() - $startTime;
if (($elapsed > (self::SCAN_TIME_SECONDS / 2)) && count($fileIdsToScan) > 0) {
break;
}
}
$this->logger->debug('Found ' . count($fileIdsToScan) . ' files to scan');
return $fileIdsToScan;
}
}