Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Ping check on save, warning when disabling scan

Oliver Günther1aba59a5cac8
diff --git a/app/controllers/admin/settings/attachments_settings_controller.rb b/app/controllers/admin/settings/attachments_settings_controller.rb
index 886492ae379..4d588437d30 100644
--- a/app/controllers/admin/settings/attachments_settings_controller.rb
+++ b/app/controllers/admin/settings/attachments_settings_controller.rb
@@ -30,7 +30,7 @@ module Admin::Settings
class AttachmentsSettingsController < ::Admin::SettingsController
menu_item :settings_attachments
- before_action :check_clamav, only: %i[update], if: -> { params[:settings] }
+ before_action :check_clamav, only: %i[update]
def default_breadcrumb
t(:'attributes.attachments')
@@ -51,15 +51,28 @@ module Admin::Settings
private
def check_clamav
+ return if params[:settings].blank?
return if params[:settings][:antivirus_scan_mode] == "disabled"
- service = Attachments::ClamAVService.new(params[:settings][:antivirus_scan_mode],
- params[:settings][:antivirus_scan_target])
+ service = ::Attachments::ClamAVService.new(params[:settings][:antivirus_scan_mode].to_sym,
+ params[:settings][:antivirus_scan_target])
service.ping
- rescue ClamAV::Error
- flash[:error] = t(:'attachments.settings.antivirus_scan_target_unreachable')
- redirect_to admin_settings_path(tab: "attachments")
+ rescue StandardError => e
+ Rails.logger.error { "Failed to check availability of ClamAV: #{e.message}" }
+ flash[:error] = t(:'settings.antivirus.clamav_ping_failed')
+ redirect_to action: :show
+ end
+
+ def success_callback(_call)
+ if Setting.antivirus_scan_mode == :disabled && Attachment.status_quarantined.any?
+ flash[:info] = t('settings.antivirus.remaining_quarantined_files_html',
+ link: helpers.link_to(t('antivirus_scan.quarantined_attachments.title'), admin_quarantined_attachments_path),
+ file_count: t(:label_x_files, count: Attachment.status_quarantined.count))
+ redirect_to action: :show
+ else
+ super
+ end
end
def settings_params
diff --git a/app/services/attachments/clamav_service.rb b/app/services/attachments/clamav_service.rb
index be2d5a32c06..d069d0d8a5d 100644
--- a/app/services/attachments/clamav_service.rb
+++ b/app/services/attachments/clamav_service.rb
@@ -27,12 +27,12 @@
#++
module Attachments
- class VirusScanFailed < StandardError; end
-
class ClamAVService
+ attr_reader :clamav_client
+
def initialize(scan_mode = Setting.antivirus_scan_mode, connection_target = Setting.antivirus_scan_target)
options = clamav_client_options(scan_mode, connection_target)
- @clamav_client ||= ClamAV::Client.new(**options)
+ @clamav_client = ClamAV::Client.new(**options)
end
def scan(attachment)
diff --git a/app/workers/attachments/virus_scan_job.rb b/app/workers/attachments/virus_scan_job.rb
index f3803bfce88..2072275410f 100644
--- a/app/workers/attachments/virus_scan_job.rb
+++ b/app/workers/attachments/virus_scan_job.rb
@@ -28,6 +28,8 @@
module Attachments
class VirusScanJob < ApplicationJob
+ class VirusScanFailed < StandardError; end
+
retry_on VirusScanFailed, wait: 5.seconds, attempts: 3
discard_on ActiveJob::DeserializationError
diff --git a/config/initializers/zeitwerk.rb b/config/initializers/zeitwerk.rb
index 24e9992930e..2caf8b11a1e 100644
--- a/config/initializers/zeitwerk.rb
+++ b/config/initializers/zeitwerk.rb
@@ -36,6 +36,8 @@ OpenProject::Inflector.rule do |basename, abspath|
"#{default_inflect($1, abspath)}OAuth"
when 'oauth'
'OAuth'
+ when /\Aclamav_(.*)\z/
+ "ClamAV#{default_inflect($1, abspath)}"
when /\A(.*)_sso\z/
"#{default_inflect($1, abspath)}SSO"
end
@@ -62,7 +64,8 @@ OpenProject::Inflector.inflection(
'cors' => 'CORS',
'openid_connect' => 'OpenIDConnect',
'pdf_export' => 'PDFExport',
- 'ical' => 'ICal'
+ 'ical' => 'ICal',
+ 'clamav' => 'ClamAV'
)
Rails.autoloaders.each do |autoloader|
diff --git a/config/locales/en.yml b/config/locales/en.yml
index 786801f9cff..d2e7c31e384 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -2340,6 +2340,10 @@ en:
one: "1 project"
other: "%{count} projects"
zero: "no projects"
+ label_x_files:
+ one: "1 file"
+ other: "%{count} files"
+ zero: "no files"
label_yesterday: "yesterday"
label_role_type: "Type"
label_member_role: "Project role"
@@ -2996,6 +3000,11 @@ en:
Multiple values allowed (one line for each value).
antivirus:
title: "Virus scanning"
+ clamav_ping_failed: "Failed to connect the the ClamAV daemon. Double-check the configuration and try again."
+ remaining_quarantined_files_html: >
+ You have successfully disabled virus scanning.
+ There are however still %{file_count} in quarantine.
+ Visit %{link} to clean them up."
actions:
delete: 'Delete the file'
quarantine: 'Quarantine the file'