Skip to content

Commit 95f517d

Browse files
Add maximum scan size setting for file uploads (#236)
1 parent ca69689 commit 95f517d

8 files changed

Lines changed: 28 additions & 13 deletions

File tree

lib/Controller/ScanController.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,27 @@
66

77
namespace OCA\GDataVaas\Controller;
88

9-
use Coduo\PHPHumanizer\NumberHumanizer;
109
use Exception;
10+
use OCA\GDataVaas\AppInfo\Application;
1111
use OCA\GDataVaas\Service\VerdictService;
1212
use OCP\AppFramework\Controller;
1313
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
1414
use OCP\AppFramework\Http\JSONResponse;
1515
use OCP\Files\EntityTooLargeException;
1616
use OCP\Files\NotFoundException;
1717
use OCP\Files\NotPermittedException;
18+
use OCP\IAppConfig;
1819
use OCP\IRequest;
1920
use VaasSdk\Exceptions\VaasAuthenticationException;
2021

2122
class ScanController extends Controller {
23+
private IAppConfig $config;
2224
private VerdictService $verdictService;
2325

24-
public function __construct($appName, IRequest $request, VerdictService $verdictService) {
26+
public function __construct($appName, IRequest $request, VerdictService $verdictService, IAppConfig $config) {
2527
parent::__construct($appName, $request);
2628

29+
$this->config = $config;
2730
$this->verdictService = $verdictService;
2831
}
2932

@@ -39,8 +42,8 @@ public function scan(int $fileId): JSONResponse {
3942
return new JSONResponse(['verdict' => $verdict->verdict->value], 200);
4043
} catch (EntityTooLargeException) {
4144
return new JSONResponse(
42-
['error' => "File $fileId is larger than " . NumberHumanizer::binarySuffix(
43-
VerdictService::MAX_FILE_SIZE, 'de')], 413);
45+
['error' => 'File is larger than '
46+
. $this->config->getValueInt(Application::APP_ID, 'maxScanSizeInMB', 256) . 'MB.'], 413);
4447
} catch (NotFoundException) {
4548
return new JSONResponse(['error' => "File $fileId not found"], 404);
4649
} catch (NotPermittedException) {

lib/Controller/SettingsController.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public function setconfig(
4242
$scanOnlyThis,
4343
$doNotScanThis,
4444
$notifyMails,
45+
$maxScanSize,
4546
): JSONResponse {
4647
if (!empty($notifyMails)) {
4748
$mails = explode(',', preg_replace('/\s+/', '', $notifyMails));
@@ -51,6 +52,9 @@ public function setconfig(
5152
}
5253
}
5354
}
55+
if ((int)$maxScanSize < 1) {
56+
return new JSONResponse(['status' => 'error', 'message' => 'Invalid max scan size: ' . $maxScanSize]);
57+
}
5458
$this->config->setValueString($this->appName, 'username', $username);
5559
$this->config->setValueString($this->appName, 'password', $password);
5660
$this->config->setValueString($this->appName, 'clientId', $clientId);
@@ -60,6 +64,7 @@ public function setconfig(
6064
$this->config->setValueString($this->appName, 'scanOnlyThis', $scanOnlyThis);
6165
$this->config->setValueString($this->appName, 'doNotScanThis', $doNotScanThis);
6266
$this->config->setValueString($this->appName, 'notifyMails', $notifyMails);
67+
$this->config->setValueInt($this->appName, 'maxScanSizeInMB', (int)$maxScanSize);
6368
return new JSONResponse(['status' => 'success']);
6469
}
6570

lib/Service/ScanService.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
namespace OCA\GDataVaas\Service;
88

9-
use Coduo\PHPHumanizer\NumberHumanizer;
109
use OCA\GDataVaas\AppInfo\Application;
1110
use OCP\DB\Exception;
1211
use OCP\Files\EntityTooLargeException;
@@ -66,7 +65,8 @@ public function run(): int {
6665
} catch (EntityTooLargeException) {
6766
$this->logger->error(
6867
"File $fileId is larger than
69-
" . NumberHumanizer::binarySuffix(VerdictService::MAX_FILE_SIZE, 'de'));
68+
" . $this->appConfig->getValueInt(Application::APP_ID, 'maxScanSizeInMB', 256) . 'MB.'
69+
);
7070
} catch (NotFoundException) {
7171
$this->logger->error("File $fileId not found");
7272
} catch (NotPermittedException) {

lib/Service/VerdictService.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@
2424
use VaasSdk\VaasVerdict;
2525

2626
class VerdictService {
27-
public const MAX_FILE_SIZE = 1073741824;
28-
2927
private string $username;
3028
private string $password;
3129
private string $clientId;
@@ -115,9 +113,10 @@ public function scanFileById(int $fileId): VaasVerdict {
115113
* @param string $path
116114
* @return bool
117115
*/
118-
public static function isFileTooLargeToScan(string $path): bool {
116+
public function isFileTooLargeToScan(string $path): bool {
119117
$size = filesize($path);
120-
return ($size === false) || $size > self::MAX_FILE_SIZE;
118+
return ($size === false)
119+
|| $size > ($this->appConfig->getValueInt(Application::APP_ID, 'maxScanSizeInMB', 256) * 1024 * 1024);
121120
}
122121

123122
/**

lib/Settings/VaasAdmin.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@ public function getForm(): TemplateResponse {
5555
'notifyMail' => $this->config->getValueString(Application::APP_ID, 'notifyMails'),
5656
'sendMailOnVirusUpload'
5757
=> $this->config->getValueBool(Application::APP_ID, 'sendMailOnVirusUpload'),
58-
'notifyAdminEnabled' => $this->config->getValueBool(Application::APP_ID, 'notifyAdminEnabled')
58+
'notifyAdminEnabled' => $this->config->getValueBool(Application::APP_ID, 'notifyAdminEnabled'),
59+
'maxScanSizeInMB'
60+
=> $this->config->getValueInt(Application::APP_ID, 'maxScanSizeInMB', 256)
5961
];
6062

6163
return new TemplateResponse(Application::APP_ID, 'admin', $params);

src/admin-settings.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ document.addEventListener('DOMContentLoaded', async () => {
6464
const scanOnlyThis = document.querySelector('#scanOnlyThis').value;
6565
const doNotScanThis = document.querySelector('#doNotScanThis').value;
6666
const notifyMails = document.querySelector('#notify_mails').value;
67+
const maxScanSize = document.querySelector('#max-scan-size').value;
6768

6869
const response = await postData(OC.generateUrl('apps/gdatavaas/setconfig'), {
6970
username: username,
@@ -74,7 +75,8 @@ document.addEventListener('DOMContentLoaded', async () => {
7475
quarantineFolder,
7576
scanOnlyThis,
7677
doNotScanThis,
77-
notifyMails
78+
notifyMails,
79+
maxScanSize
7880
});
7981
const msgElement = document.querySelector('#auth_save_msg');
8082

templates/admin.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@
6262
<td><div title="<?php p($l->t('Mail addresses for notifications when malicious files are found or a user tries to upload them. Must be comma-separated.'));?>" class="visible"><label for="notify_mails"><?php p($l->t('Notify Mails'));?></label></div></td>
6363
<td class="input_field"><input id="notify_mails" type="text" name="notify_mails" value="<?php p($_['notifyMail']); ?>"/></td>
6464
</tr>
65+
<tr class="max-scan-size">
66+
<td><div title="<?php p($l->t('The maximum scan size for files to be scanned in MB. Files above this limit are tagged as “Won\'t Scan”.'));?>" class="visible"><label for="max-scan-size"><?php p($l->t('Maximum scan size'));?></label></div></td>
67+
<td class="input_field"><input id="max-scan-size" type="number" min="0" name="max-scan-size" value="<?php p($_['maxScanSizeInMB']); ?>"/></td>
68+
</tr>
6569
</table>
6670
<input class="submit-button" id="auth_submit" type="submit" value="<?php p($l->t('Save'));?>" />
6771
<span id="auth_save_msg"></span>

tests/bats/functionality-parallel.bats

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ setup_file() {
9999
}
100100

101101
@test "test wontscan tag for testuser" {
102-
dd if=/dev/zero of=$FOLDER_PREFIX/too-large.dat bs=1083741824 count=1
102+
dd if=/dev/zero of=$FOLDER_PREFIX/too-large.dat bs=268435457 count=1
103103

104104
docker cp $FOLDER_PREFIX/too-large.dat nextcloud-container:/var/www/html/data/$TESTUSER/files/$TESTUSER.too-large.dat
105105
docker exec -i nextcloud-container chown www-data:www-data /var/www/html/data/$TESTUSER/files/$TESTUSER.too-large.dat

0 commit comments

Comments
 (0)