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

Implement attachment statuses and virus scanning

Oliver Günther857532e5065e
diff --git a/app/models/attachment.rb b/app/models/attachment.rb
index 4bbb42be8b7..6eb641f39c4 100644
--- a/app/models/attachment.rb
+++ b/app/models/attachment.rb
@@ -30,10 +30,11 @@ require 'digest/md5'
class Attachment < ApplicationRecord
enum status: {
- new: 0,
+ uploaded: 0,
prepared: 1,
- scanned: 2
- }.freeze, _prefix: true, _default: :new
+ scanned: 2,
+ quarantined: 3
+ }.freeze, _prefix: true
belongs_to :container, polymorphic: true
belongs_to :author, class_name: 'User'
@@ -68,7 +69,7 @@ class Attachment < ApplicationRecord
mount_uploader :file, OpenProject::Configuration.file_uploader
- after_commit :extract_fulltext, on: :create
+ after_commit :enqueue_jobs, on: :create
scope :pending_direct_upload, -> { where(digest: "", downloads: -1) }
scope :not_pending_direct_upload, -> { where.not(digest: "", downloads: -1) }
@@ -131,6 +132,10 @@ class Attachment < ApplicationRecord
status_prepared?
end
+ def pending_virus_scan?
+ status_uploaded? && Setting.antivirus_scan_mode != :disabled
+ end
+
# images are sent inline
def inlineable?
is_plain_text? || is_image? || is_movie? || is_pdf?
@@ -241,10 +246,14 @@ class Attachment < ApplicationRecord
attachment.save!
end
- def extract_fulltext
- return unless OpenProject::Database.allows_tsv? && (!container || container.class.attachment_tsv_extracted?)
+ def enqueue_jobs
+ if OpenProject::Database.allows_tsv? && (!container || container.class.attachment_tsv_extracted?)
+ Attachments::ExtractFulltextJob.perform_later(id)
+ end
- ExtractFulltextJob.perform_later(id)
+ if pending_virus_scan?
+ Attachments::VirusScanJob.perform_later(self)
+ end
end
# Extract the fulltext of any attachments where fulltext is still nil.
@@ -258,9 +267,9 @@ class Attachment < ApplicationRecord
.pluck(:id)
.each do |id|
if run_now
- ExtractFulltextJob.perform_now(id)
+ Attachments::ExtractFulltextJob.perform_now(id)
else
- ExtractFulltextJob.perform_later(id)
+ Attachments::ExtractFulltextJob.perform_later(id)
end
end
end
@@ -269,7 +278,7 @@ class Attachment < ApplicationRecord
return unless OpenProject::Database.allows_tsv?
Attachment.pluck(:id).each do |id|
- ExtractFulltextJob.perform_now(id)
+ Attachments::ExtractFulltextJob.perform_now(id)
end
end
diff --git a/app/services/attachments/set_attributes_service.rb b/app/services/attachments/set_attributes_service.rb
index 99c7360cbab..38fb7d53638 100644
--- a/app/services/attachments/set_attributes_service.rb
+++ b/app/services/attachments/set_attributes_service.rb
@@ -36,6 +36,10 @@ module Attachments
def set_default_attributes(_params)
model.author = user if model.author.nil?
+
+ model.change_by_system do
+ model.status = model.internal_container? ? :scanned : :uploaded
+ end
end
end
end
diff --git a/app/views/admin/settings/attachments_settings/_av_form.html.erb b/app/views/admin/settings/attachments_settings/_av_form.html.erb
index 683f01ec5a4..4d0f29a2704 100644
--- a/app/views/admin/settings/attachments_settings/_av_form.html.erb
+++ b/app/views/admin/settings/attachments_settings/_av_form.html.erb
@@ -23,7 +23,6 @@
<div class="form--field">
<%= setting_select :antivirus_scan_action,
[
- [t('settings.antivirus.actions.log'), :log],
[t('settings.antivirus.actions.quarantine'), :quarantine],
[t('settings.antivirus.actions.delete'), :delete],
],
@@ -31,7 +30,6 @@
%>
<div class="form--field-instructions">
<p><%= t('settings.antivirus.actions.instructions_html',
- log_option: t('settings.antivirus.actions.log'),
quarantine_option: t('settings.antivirus.actions.quarantine'),
delete_option: t('settings.antivirus.actions.delete')
) %></p>
diff --git a/app/views/admin/settings/attachments_settings/show.html.erb b/app/views/admin/settings/attachments_settings/show.html.erb
index 3a650a42cde..2f8c1b3060f 100644
--- a/app/views/admin/settings/attachments_settings/show.html.erb
+++ b/app/views/admin/settings/attachments_settings/show.html.erb
@@ -73,6 +73,7 @@ See COPYRIGHT and LICENSE files for more details.
) %></p>
</div>
</div>
+
<%= render partial: 'av_form', locals: { selected: Setting.antivirus_scan_mode } %>
</fieldset>
diff --git a/app/workers/attachments/extract_fulltext_job.rb b/app/workers/attachments/extract_fulltext_job.rb
new file mode 100644
index 00000000000..b00d13c4bb4
--- /dev/null
+++ b/app/workers/attachments/extract_fulltext_job.rb
@@ -0,0 +1,94 @@
+#-- 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 ExtractFulltextJob < ApplicationJob
+ queue_with_priority :low
+
+ def perform(attachment_id)
+ @attachment_id = attachment_id
+ @attachment = nil
+ @text = nil
+ @file = nil
+ @filename = nil
+ @language = OpenProject::Configuration.main_content_language
+
+ return unless OpenProject::Database.allows_tsv?
+ return unless @attachment = find_attachment(attachment_id)
+
+ init
+ update
+ ensure
+ FileUtils.rm @file.path if delete_file?
+ end
+
+ private
+
+ def init
+ carrierwave_uploader = @attachment.file
+ @file = carrierwave_uploader.local_file
+ @filename = carrierwave_uploader.file.filename
+
+ if @attachment.readable?
+ resolver = Plaintext::Resolver.new(@file, @attachment.content_type)
+ @text = resolver.text
+ end
+ rescue StandardError => e
+ Rails.logger.error(
+ "Failed to extract plaintext from file #{@attachment&.id} (On domain #{Setting.host_name}): #{e}: #{e.message}"
+ )
+ end
+
+ def update
+ Attachment
+ .where(id: @attachment_id)
+ .update_all(['fulltext = ?, fulltext_tsv = to_tsvector(?, ?), file_tsv = to_tsvector(?, ?)',
+ @text,
+ @language,
+ OpenProject::FullTextSearch.normalize_text(@text),
+ @language,
+ OpenProject::FullTextSearch.normalize_filename(@filename)])
+ rescue StandardError => e
+ Rails.logger.error(
+ "Failed to update TSV values for attachment #{@attachment&.id} (On domain #{Setting.host_name}): #{e.message[0..499]}[...]"
+ )
+ end
+
+ def find_attachment(id)
+ Attachment.find_by(id:)
+ end
+
+ def remote_file?
+ !@attachment&.file.is_a?(LocalFileUploader)
+ end
+
+ def delete_file?
+ remote_file? && @file
+ end
+ end
+end
diff --git a/app/workers/attachments/finish_direct_upload_job.rb b/app/workers/attachments/finish_direct_upload_job.rb
index 9542cdb507e..4454abc6a0c 100644
--- a/app/workers/attachments/finish_direct_upload_job.rb
+++ b/app/workers/attachments/finish_direct_upload_job.rb
@@ -53,6 +53,7 @@ class Attachments::FinishDirectUploadJob < ApplicationJob
save_attachment(attachment)
journalize_container(attachment)
attachment_created_event(attachment)
+ schedule_jobs(attachment)
rescue StandardError => e
::OpenProject.logger.error e
attachment.destroy
@@ -63,7 +64,7 @@ class Attachments::FinishDirectUploadJob < ApplicationJob
def set_attributes_from_file(attachment, local_file)
attachment.extend(OpenProject::ChangedBySystem)
attachment.change_by_system do
- attachment.downloads = 0
+ attachment.status = :uploaded
attachment.set_file_size local_file
attachment.set_content_type local_file
attachment.set_digest local_file
@@ -78,7 +79,7 @@ class Attachments::FinishDirectUploadJob < ApplicationJob
contract = create_contract attachment, whitelist
unless contract.valid?
- errors = contract.errors.full_messages.join(", ")
+ errors = contracterrors.full_messages.join(", ")
raise "Failed to validate attachment #{attachment.id}: #{errors}"
end
end
@@ -90,6 +91,10 @@ class Attachments::FinishDirectUploadJob < ApplicationJob
options:
end
+ def schedule_jobs
+ attachment.extract_fulltext
+ end
+
def derive_contract_options(whitelist)
case whitelist
when false
diff --git a/app/workers/attachments/virus_scan_job.rb b/app/workers/attachments/virus_scan_job.rb
new file mode 100644
index 00000000000..c32b842df52
--- /dev/null
+++ b/app/workers/attachments/virus_scan_job.rb
@@ -0,0 +1,120 @@
+#-- 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 VirusScanJob < ApplicationJob
+ class VirusScanFailed < StandardError; end
+
+ retry_on VirusScanFailed, wait: 5.seconds, attempts: 3
+ discard_on ActiveJob::DeserializationError
+
+ queue_with_priority :low
+
+ def perform(attachment)
+ return if Setting.antivirus_scan_mode == :disabled
+ return unless attachment.status_uploaded?
+
+ User.execute_as(User.system) do
+ OpenProject::Mutex.with_advisory_lock_transaction(attachment, 'virus_scan') do
+ scan_attachment(attachment)
+ end
+ end
+ rescue StandardError => e
+ Rails.logger.error "Error scanning attachment #{attachment.id} for viruses: #{e.message}"
+ raise VirusScanFailed.new(e.message)
+ end
+
+ private
+
+ def scan_attachment(attachment)
+ file = attachment.diskfile
+ response = clamav_client.execute(ClamAV::Commands::InstreamCommand.new(file))
+ 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}"
+ end
+ end
+
+ def handle_success_response(attachment)
+ Rails.logger.warn { "Scanned file #{attachment.id}. No viruses found." }
+ attachment.update!(status: :scanned)
+ end
+
+ def handle_virus_response(attachment, virus_name)
+ action = Setting.antivirus_scan_action
+ Rails.logger.warn { "Detected virus #{virus_name} in file #{attachment.id}. Will #{action} the file." }
+
+ if action == :delete
+ delete_attachment(attachment)
+ else
+ quarantine_attachment(attachment)
+ end
+ end
+
+ def delete_attachment(attachment)
+ container = attachment.container
+ attachment.destroy!
+ create_journal(container, I18n.t('antivirus_scan.deleted_message', filename: attachment.filename))
+ end
+
+ def quarantine_attachment(attachment)
+ create_journal(attachment.container, I18n.t('antivirus_scan.quarantined_message', filename: attachment.filename))
+ attachment.update!(status: :quarantined)
+ end
+
+ def create_journal(container, notes)
+ return unless container
+
+ ::Journals::CreateService
+ .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/app/workers/extract_fulltext_job.rb b/app/workers/extract_fulltext_job.rb
deleted file mode 100644
index bff360227fc..00000000000
--- a/app/workers/extract_fulltext_job.rb
+++ /dev/null
@@ -1,92 +0,0 @@
-#-- 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.
-#++
-
-class ExtractFulltextJob < ApplicationJob
- queue_with_priority :low
-
- def perform(attachment_id)
- @attachment_id = attachment_id
- @attachment = nil
- @text = nil
- @file = nil
- @filename = nil
- @language = OpenProject::Configuration.main_content_language
-
- return unless OpenProject::Database.allows_tsv?
- return unless @attachment = find_attachment(attachment_id)
-
- init
- update
- ensure
- FileUtils.rm @file.path if delete_file?
- end
-
- private
-
- def init
- carrierwave_uploader = @attachment.file
- @file = carrierwave_uploader.local_file
- @filename = carrierwave_uploader.file.filename
-
- if @attachment.readable?
- resolver = Plaintext::Resolver.new(@file, @attachment.content_type)
- @text = resolver.text
- end
- rescue StandardError => e
- Rails.logger.error(
- "Failed to extract plaintext from file #{@attachment&.id} (On domain #{Setting.host_name}): #{e}: #{e.message}"
- )
- end
-
- def update
- Attachment
- .where(id: @attachment_id)
- .update_all(['fulltext = ?, fulltext_tsv = to_tsvector(?, ?), file_tsv = to_tsvector(?, ?)',
- @text,
- @language,
- OpenProject::FullTextSearch.normalize_text(@text),
- @language,
- OpenProject::FullTextSearch.normalize_filename(@filename)])
- rescue StandardError => e
- Rails.logger.error(
- "Failed to update TSV values for attachment #{@attachment&.id} (On domain #{Setting.host_name}): #{e.message[0..499]}[...]"
- )
- end
-
- def find_attachment(id)
- Attachment.find_by(id:)
- end
-
- def remote_file?
- !@attachment&.file.is_a?(LocalFileUploader)
- end
-
- def delete_file?
- remote_file? && @file
- end
-end
diff --git a/config/constants/settings/definition.rb b/config/constants/settings/definition.rb
index 1a156798382..95586e371bb 100644
--- a/config/constants/settings/definition.rb
+++ b/config/constants/settings/definition.rb
@@ -106,8 +106,8 @@ module Settings
antivirus_scan_action: {
description: 'Virus scanning action for found infected files',
format: :symbol,
- default: :delete,
- allowed: %i[log delete]
+ default: :quarantine,
+ allowed: %i[quarantine delete]
},
auth_source_sso: {
description: 'Configuration for Header-based Single Sign-On',
diff --git a/config/locales/en.yml b/config/locales/en.yml
index 93b45579f08..8af65f96651 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -101,6 +101,11 @@ en:
is_active: currently displayed
is_inactive: currently not displayed
+ antivirus_scan:
+ not_processed_yet_message: "Downloading is blocked, as file was not scanned for viruses yet. Please try again later."
+ quarantine_message: "A virus was detected in file %{filename}. It has been quarantined and is not available for download."
+ delete_message: "A virus was detected in file %{filename}. The file has been deleted."
+
attribute_help_texts:
note_public: "Any text and images you add to this field is publicly visible to all logged in users!"
text_overview: "In this view, you can create custom help texts for attributes view. When defined, these texts can be shown by clicking the help icon next to its belonging attribute."
@@ -2984,14 +2989,12 @@ en:
antivirus:
title: "Virus scanning"
actions:
- log: 'Log only'
delete: 'Delete the file'
quarantine: 'Quarantine the file'
instructions_html: >
Select the action to perform for files on which a virus has been detected:
<br/>
<ul>
- <li><strong>%{log_option}</strong>: Log the found virus, but do not block access to the file.</li>
<li><strong>%{quarantine_option}</strong>: Quarantine the file, preventing users from accessing it. Administrators can manually override the decision or delete the file in the administration.</li>
<li><strong>%{delete_option}</strong>: Delete the file immediately.</li>
</ul>
diff --git a/lib/api/helpers/attachment_renderer.rb b/lib/api/helpers/attachment_renderer.rb
index 80ac0415d01..3026162092c 100644
--- a/lib/api/helpers/attachment_renderer.rb
+++ b/lib/api/helpers/attachment_renderer.rb
@@ -46,6 +46,7 @@ module API
}
end
+
##
# Render an attachment, either by redirecting
# to the external storage,
@@ -56,6 +57,7 @@ module API
# @param cache_seconds [integer] Time in seconds the cache headers signal the browser to cache the attachment.
# Defaults to no cache headers.
def respond_with_attachment(attachment, cache_seconds: nil)
+ validate_attachment_access!(attachment)
prepare_cache_headers(cache_seconds) if cache_seconds
if attachment.external_storage?
@@ -67,6 +69,18 @@ module API
private
+ def validate_attachment_access!(attachment)
+ if attachment.status_quarantined?
+ raise ::API::Errors::Unauthorized.new(message: I18n.t('antivirus_scan.quarantined_message',
+ filename: attachment.filename))
+ end
+
+ if attachment.author != current_user && attachment.pending_virus_scan?
+ raise ::API::Errors::Unauthorized.new(message: I18n.t('antivirus_scan.not_processed_yet_message',
+ filename: attachment.filename))
+ end
+ end
+
def redirect_to_external_attachment(attachment, cache_seconds)
set_cache_headers!
redirect attachment.external_url(expires_in: cache_seconds).to_s
diff --git a/spec/features/work_packages/table/queries/filter_spec.rb b/spec/features/work_packages/table/queries/filter_spec.rb
index ba8367868ac..22c0155c451 100644
--- a/spec/features/work_packages/table/queries/filter_spec.rb
+++ b/spec/features/work_packages/table/queries/filter_spec.rb
@@ -390,10 +390,10 @@ RSpec.describe 'filter work packages', :js do
before do
allow_any_instance_of(Plaintext::Resolver).to receive(:text).and_return('I am the first text $1.99.')
wp_with_attachment_a
- ExtractFulltextJob.perform_now(attachment_a.id)
+ Attachments::ExtractFulltextJob.perform_now(attachment_a.id)
allow_any_instance_of(Plaintext::Resolver).to receive(:text).and_return('I am the second text.')
wp_with_attachment_b
- ExtractFulltextJob.perform_now(attachment_b.id)
+ Attachments::ExtractFulltextJob.perform_now(attachment_b.id)
wp_without_attachment
end
diff --git a/spec/jobs/attachments/virus_scan_job_spec.rb b/spec/jobs/attachments/virus_scan_job_spec.rb
new file mode 100644
index 00000000000..7da93a0e7bd
--- /dev/null
+++ b/spec/jobs/attachments/virus_scan_job_spec.rb
@@ -0,0 +1,97 @@
+require 'rails_helper'
+
+RSpec.describe Attachments::VirusScanJob,
+ with_settings: { antivirus_scan_mode: :clamav_socket } do
+ let(:attachment_status) { :uploaded }
+ let(:attachment) { build_stubbed(:attachment, status: attachment_status) }
+ let(:client_double) { instance_double(ClamAV::Client) }
+ let(:journal_service_double) { instance_double(Journals::CreateService) }
+
+ subject { described_class.perform_now(attachment) }
+
+ before do
+ allow(ClamAV::Client).to receive(:new).and_return(client_double)
+ end
+
+ describe '#perform when disabled', with_settings: { antivirus_scan_mode: :disabled } do
+ it 'does not scan the attachment' do
+ subject
+
+ expect(ClamAV::Client).not_to have_received(:new)
+ end
+ end
+
+ context 'when status is not uploaded' do
+ let(:attachment_status) { :prepared }
+
+ it 'does not scan the attachment' do
+ subject
+
+ expect(ClamAV::Client).not_to have_received(:new)
+ end
+ end
+
+ describe '#perform' do
+ before do
+ allow(client_double)
+ .to receive(:execute).with(instance_of(ClamAV::Commands::InstreamCommand))
+ .and_return(response)
+ end
+
+ context 'when no virus is found' do
+ let(:response) { ClamAV::SuccessResponse.new('wat') }
+
+ it 'updates the file status' do
+ allow(attachment).to receive(:update!)
+
+ subject
+
+ expect(attachment).to have_received(:update!).with(status: :scanned)
+ end
+ end
+
+ context 'when error occurs in clamav' do
+ let(:response) { ClamAV::ErrorResponse.new('Oh noes') }
+
+ it 'does nothing to the file' do
+ allow(attachment).to receive(:update!)
+
+ expect { subject }.not_to raise_error
+
+ expect(attachment).not_to have_received(:update!)
+ end
+ end
+
+ context 'when virus is found' do
+ let(:response) { ClamAV::VirusResponse.new('wat', 'Eicar-Test-Signature') }
+
+ context 'when action is quarantine', with_settings: { antivirus_scan_action: :quarantine } do
+ it 'quarantines the file' do
+ allow(attachment).to receive(:update!)
+ allow(Journals::CreateService).to receive(:new).and_return(journal_service_double)
+ allow(journal_service_double).to receive(:call)
+
+ subject
+
+ expect(attachment).to have_received(:update!).with(status: :quarantined)
+ expect(journal_service_double)
+ .to have_received(:call).with(notes: /It has been quarantined/)
+ end
+ end
+
+ context 'when action is delete', with_settings: { antivirus_scan_action: :delete } do
+ it 'deletes the file' do
+ allow(attachment).to receive(:destroy!)
+ allow(Journals::CreateService).to receive(:new).and_return(journal_service_double)
+ allow(journal_service_double).to receive(:call)
+
+ subject
+
+ expect(attachment).to have_received(:destroy!)
+ expect(journal_service_double)
+ .to have_received(:call).with(notes: /The file has been deleted/)
+ end
+ end
+ end
+ end
+end
diff --git a/spec/models/attachment_spec.rb b/spec/models/attachment_spec.rb
index 7cb4b58a8d3..5f5b9ccbcce 100644
--- a/spec/models/attachment_spec.rb
+++ b/spec/models/attachment_spec.rb
@@ -305,6 +305,35 @@ RSpec.describe Attachment do
end
end
+ describe 'virus scan job on commit' do
+ shared_let(:work_package) { create(:work_package) }
+ let(:created_attachment) do
+ create(:attachment,
+ status: :uploaded,
+ container: work_package)
+ end
+
+ context 'with setting disabled', with_settings: { antivirus_scan_mode: :disabled } do
+ it 'does not run' do
+ attachment.save
+ expect(attachment.pending_virus_scan?).to be false
+
+ expect(Attachments::VirusScanJob)
+ .not_to have_been_enqueued.with(attachment)
+ end
+ end
+
+ context 'with setting enabled', with_settings: { antivirus_scan_mode: :clamav_socket } do
+ it 'runs the job' do
+ attachment.save
+ expect(attachment.pending_virus_scan?).to be true
+
+ expect(Attachments::VirusScanJob)
+ .to have_been_enqueued.with(attachment)
+ end
+ end
+ end
+
describe 'full text extraction job on commit' do
let(:created_attachment) do
create(:attachment,
@@ -316,7 +345,7 @@ RSpec.describe Attachment do
it 'runs extraction' do
extraction_with_id = nil
- allow(ExtractFulltextJob)
+ allow(Attachments::ExtractFulltextJob)
.to receive(:perform_later) do |id|
extraction_with_id = id
end
@@ -331,7 +360,7 @@ RSpec.describe Attachment do
it 'does not run extraction' do
created_attachment
- expect(ExtractFulltextJob)
+ expect(Attachments::ExtractFulltextJob)
.not_to receive(:perform_later)
created_attachment.save
diff --git a/spec/workers/extract_fulltext_job_spec.rb b/spec/workers/extract_fulltext_job_spec.rb
index 55bc7859083..31cd5edf424 100644
--- a/spec/workers/extract_fulltext_job_spec.rb
+++ b/spec/workers/extract_fulltext_job_spec.rb
@@ -28,7 +28,7 @@
require 'spec_helper'
-RSpec.describe ExtractFulltextJob, type: :job do
+RSpec.describe Attachments::ExtractFulltextJob, type: :job do
subject(:extracted_attributes) do
perform_enqueued_jobs
@@ -49,7 +49,7 @@ RSpec.describe ExtractFulltextJob, type: :job do
let(:text) { 'lorem ipsum' }
let(:attachment) do
create(:attachment).tap do |attachment|
- expect(ExtractFulltextJob)
+ expect(Attachments::ExtractFulltextJob)
.to have_been_enqueued
.with(attachment.id)