MDL-67545 Antivirus: new option for clam scan failure

This commit is contained in:
John Beedell
2020-01-24 13:05:45 +00:00
parent 4e41ace0be
commit 7aa3444d72
6 changed files with 45 additions and 4 deletions
+5
View File
@@ -93,6 +93,11 @@ class scanner extends \core\antivirus\scanner {
// return SCAN_RESULT_FOUND result.
if ($this->get_config('clamfailureonupload') === 'actlikevirus') {
return self::SCAN_RESULT_FOUND;
} else if ($this->get_config('clamfailureonupload') === 'tryagain') {
// Do not upload the file, just give a message to the user to try again later.
unlink($file);
throw new \core\antivirus\scanner_exception('antivirusfailed', '', ['item' => $filename],
null, 'antivirus_clamav');
}
}
return $return;
@@ -22,9 +22,11 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
$string['antivirusfailed'] = 'There is a problem with AntiVirus scanning at the moment. Your file {$a->item} has not been uploaded. Please try again later.';
$string['configclamactlikevirus'] = 'Treat files like viruses';
$string['configclamdonothing'] = 'Treat files as OK';
$string['configclamfailureonupload'] = 'If you have configured clam to scan uploaded files, but it is configured incorrectly or fails to run for some unknown reason, how should it behave? If you choose \'Treat files like viruses\', they\'ll be moved into the quarantine area, or deleted. If you choose \'Treat files as OK\', the files will be moved to the destination directory like normal. Either way, admins will be alerted that clam has failed. If you choose \'Treat files like viruses\' and for some reason clam fails to run (usually because you have entered an invalid pathtoclam), ALL files that are uploaded will be moved to the given quarantine area, or deleted. Be careful with this setting.';
$string['configclamfailureonupload'] = 'If you have configured clam to scan uploaded files, but it is configured incorrectly or fails to run for some unknown reason, how should it behave? If you choose \'Treat files like viruses\', they\'ll be moved into the quarantine area, or deleted. If you choose \'Treat files as OK\', the files will be moved to the destination directory like normal. If you choose \'Refuse upload, try again\' (useful if failures occur during regular virus updating periods) a try again later message will be displayed to the user. Either way, admins will be alerted that clam has failed. If you choose \'Treat files like viruses\' and for some reason clam fails to run (usually because you have entered an invalid pathtoclam), ALL files that are uploaded will be moved to the given quarantine area, or deleted. Be careful with this setting.';
$string['configclamtryagain'] = 'Refuse upload, try again';
$string['clamfailed'] = 'ClamAV has failed to run. The return error message was "{$a}". Here is the output from ClamAV:';
$string['clamfailureonupload'] = 'On ClamAV failure';
$string['errorcantopensocket'] = 'Connecting to Unix domain socket resulted in error {$a}';
+1
View File
@@ -62,6 +62,7 @@ if ($ADMIN->fulltree) {
$options = array(
'donothing' => new lang_string('configclamdonothing', 'antivirus_clamav'),
'actlikevirus' => new lang_string('configclamactlikevirus', 'antivirus_clamav'),
'tryagain' => new lang_string('configclamtryagain', 'antivirus_clamav')
);
$settings->add(new admin_setting_configselect('antivirus_clamav/clamfailureonupload',
new lang_string('clamfailureonupload', 'antivirus_clamav'),
@@ -234,6 +234,38 @@ class antivirus_clamav_scanner_testcase extends advanced_testcase {
$this->assertEquals(1, $antivirus->scan_file($this->tempfile, ''));
}
public function test_scan_file_error_tryagain() {
$methods = array(
'scan_file_execute_commandline',
'scan_file_execute_unixsocket',
'message_admins',
'get_config',
'get_scanning_notice',
);
$antivirus = $this->getMockBuilder('\antivirus_clamav\scanner')->setMethods($methods)->getMock();
// Configure scan_file_execute_commandline and scan_file_execute_unixsocket
// method stubs to behave as if there is a scanning error (SCAN_RESULT_ERROR).
$antivirus->method('scan_file_execute_commandline')->willReturn(2);
$antivirus->method('scan_file_execute_unixsocket')->willReturn(2);
$antivirus->method('get_scanning_notice')->willReturn('someerror');
// Set expectation that message_admins is called.
$antivirus->expects($this->atLeastOnce())->method('message_admins')->with($this->equalTo('someerror'));
// Initiate mock scanning with configuration setting to act like virus on
// scanning error and using commandline.
$configmap = array(array('clamfailureonupload', 'tryagain'), array('runningmethod', 'commandline'));
$antivirus->method('get_config')->will($this->returnValueMap($configmap));
// Run mock scanning.
$this->assertFileExists($this->tempfile);
$this->expectException(\core\antivirus\scanner_exception::class);
$antivirus->scan_file($this->tempfile, '');
$this->assertEquals('antivirusfailed', $this->getExpectedExceptionCode());
$this->assertFileNotExists($this->tempfile);
}
public function test_scan_data_no_virus() {
$methods = array(
'scan_data_execute_socket',
+1 -1
View File
@@ -24,6 +24,6 @@
defined('MOODLE_INTERNAL') || die();
$plugin->version = 2019122900; // The current plugin version (Date: YYYYMMDDXX).
$plugin->version = 2020012400; // The current plugin version (Date: YYYYMMDDXX).
$plugin->requires = 2019111200; // Requires this Moodle version.
$plugin->component = 'antivirus_clamav'; // Full name of the plugin (used for diagnostics).
+3 -2
View File
@@ -42,8 +42,9 @@ class scanner_exception extends \moodle_exception {
* @param string $link
* @param mixed $a
* @param mixed $debuginfo
* @param string $module optional plugin name
*/
public function __construct($errorcode, $link = '', $a = null, $debuginfo = null) {
parent::__construct($errorcode, 'antivirus', $link, $a, $debuginfo);
public function __construct($errorcode, $link = '', $a = null, $debuginfo = null, $module = 'antivirus') {
parent::__construct($errorcode, $module, $link, $a, $debuginfo);
}
}