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

Extract scanning into service

Oliver Günther669a1041bb7f
diff --git a/app/controllers/admin/settings/attachments_settings_controller.rb b/app/controllers/admin/settings/attachments_settings_controller.rb
index b5e20af8811..886492ae379 100644
--- a/app/controllers/admin/settings/attachments_settings_controller.rb
+++ b/app/controllers/admin/settings/attachments_settings_controller.rb
@@ -30,6 +30,8 @@ module Admin::Settings
class AttachmentsSettingsController < ::Admin::SettingsController
menu_item :settings_attachments
+ before_action :check_clamav, only: %i[update], if: -> { params[:settings] }
+
def default_breadcrumb
t(:'attributes.attachments')
end
@@ -48,6 +50,18 @@ module Admin::Settings
private
+ def check_clamav
+ return if params[:settings][:antivirus_scan_mode] == "disabled"
+
+ service = Attachments::ClamAVService.new(params[:settings][:antivirus_scan_mode],
+ 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")
+ end
+
def settings_params
super.tap do |settings|
settings["attachment_whitelist"] = settings["attachment_whitelist"].split(/\r?\n/)
diff --git a/app/services/attachments/clamav_service.rb b/app/services/attachments/clamav_service.rb
new file mode 100644
index 00000000000..be2d5a32c06
--- /dev/null
+++ b/app/services/attachments/clamav_service.rb
@@ -0,0 +1,61 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2024 the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+#++
+
+module Attachments
+ class VirusScanFailed < StandardError; end
+
+ class ClamAVService
+ 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)
+ end
+
+ def scan(attachment)
+ file = attachment.diskfile
+ clamav_client.execute(ClamAV::Commands::InstreamCommand.new(file))
+ end
+
+ def ping
+ clamav_client.execute(ClamAV::Commands::PingCommand.new)
+ end
+
+ private
+
+ def clamav_client_options(scan_mode, connection_target)
+ case scan_mode
+ when :clamav_socket
+ { unix_socket: connection_target }
+ when :clamav_host
+ tcp_host, tcp_port = connection_target.split(':')
+ { tcp_host:, tcp_port: }
+ else
+ raise ArgumentError.new("Unknown clamav scan mode #{scan_mode}")
+ end
+ end
+ end
+end
diff --git a/app/workers/attachments/virus_scan_job.rb b/app/workers/attachments/virus_scan_job.rb
index c32b842df52..f3803bfce88 100644
--- a/app/workers/attachments/virus_scan_job.rb
+++ b/app/workers/attachments/virus_scan_job.rb
@@ -28,8 +28,6 @@
module Attachments
class VirusScanJob < ApplicationJob
- class VirusScanFailed < StandardError; end
-
retry_on VirusScanFailed, wait: 5.seconds, attempts: 3
discard_on ActiveJob::DeserializationError
@@ -52,17 +50,15 @@ module Attachments
private
def scan_attachment(attachment)
- file = attachment.diskfile
- response = clamav_client.execute(ClamAV::Commands::InstreamCommand.new(file))
+ service = Attachments::ClamAVService.new
+ response = service.scan(attachment)
case response
when ClamAV::SuccessResponse
handle_success_response(attachment)
when ClamAV::VirusResponse
handle_virus_response(attachment, response.virus_name)
- when ClamAV::ErrorResponse
- raise VirusScanFailed.new(response.error_str)
else
- raise "Unknown response type #{response.class}"
+ raise VirusScanFailed.new(response&.error_str || "Failed virus scan: #{response.class}")
end
end
@@ -100,21 +96,5 @@ module Attachments
.new(container, User.system)
.call(notes:)
end
-
- def clamav_client
- @clamav_client ||= ClamAV::Client.new(**clamav_client_options)
- end
-
- def clamav_client_options
- case Setting.antivirus_scan_mode
- when :clamav_socket
- { unix_socket: Setting.antivirus_scan_target }
- when :clamav_host
- tcp_host, tcp_port = Setting.antivirus_scan_target.split(':')
- { tcp_host:, tcp_port: }
- else
- raise ArgumentError.new("Unknown clamav scan mode #{Setting.antivirus_scan_mode}")
- end
- end
end
end
diff --git a/config/initializers/inflections.rb b/config/initializers/inflections.rb
index f2441269cf9..70870b30fd8 100644
--- a/config/initializers/inflections.rb
+++ b/config/initializers/inflections.rb
@@ -44,4 +44,5 @@ ActiveSupport::Inflector.inflections(:en) do |inflect|
inflect.acronym 'OAuth'
inflect.acronym 'OpenID'
inflect.acronym 'ICal'
+ inflect.acronym 'ClamAV'
end