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

Enforce writing the content type without accepting user input

Oliver Günther9323b89bace8
diff --git a/app/models/attachment.rb b/app/models/attachment.rb
index b70b90ede94..3ad52806285 100644
--- a/app/models/attachment.rb
+++ b/app/models/attachment.rb
@@ -175,9 +175,7 @@ class Attachment < ApplicationRecord
end
def set_content_type(file)
- return if content_type.present?
-
- self.content_type = file.try(:content_type) || self.class.content_type_for(file.path)
+ self.content_type = self.class.content_type_for(file.path)
end
def set_digest(file)
diff --git a/app/services/attachments/set_attributes_service.rb b/app/services/attachments/set_attributes_service.rb
index 334ada1fac0..3f7e5c5b522 100644
--- a/app/services/attachments/set_attributes_service.rb
+++ b/app/services/attachments/set_attributes_service.rb
@@ -30,6 +30,12 @@
module Attachments
class SetAttributesService < ::BaseServices::SetAttributes
+ def set_attributes(params)
+ # Don't set the content type manually,
+ # we always want to infer it
+ super params.except :content_type
+ end
+
def set_default_attributes(params)
model.author = user if model.author.nil?
end
diff --git a/app/services/attachments/set_prepared_attributes_service.rb b/app/services/attachments/set_prepared_attributes_service.rb
index b97f25ac3ff..f46d99334cc 100644
--- a/app/services/attachments/set_prepared_attributes_service.rb
+++ b/app/services/attachments/set_prepared_attributes_service.rb
@@ -50,7 +50,9 @@ module Attachments
model.extend(OpenProject::ChangedBySystem)
model.change_by_system do
model.downloads = -1
- model.content_type = params[:content_type] || 'application/octet-stream'
+ # Set a content type as the file is not present
+ # The real content type will be set by FinishDirectUploadJob
+ model.content_type = params[:content_type] || OpenProject::ContentTypeDetector::SENSIBLE_DEFAULT
end
end
diff --git a/app/workers/attachments/finish_direct_upload_job.rb b/app/workers/attachments/finish_direct_upload_job.rb
index f1893f00e5a..0b319208d16 100644
--- a/app/workers/attachments/finish_direct_upload_job.rb
+++ b/app/workers/attachments/finish_direct_upload_job.rb
@@ -55,9 +55,9 @@ class Attachments::FinishDirectUploadJob < ApplicationJob
def set_attributes_from_file(attachment, local_file)
attachment.downloads = 0
- attachment.set_file_size local_file unless attachment.filesize && attachment.filesize > 0
- attachment.set_content_type local_file unless attachment.content_type.present?
- attachment.set_digest local_file unless attachment.digest.present?
+ attachment.set_file_size local_file
+ attachment.set_content_type local_file
+ attachment.set_digest local_file
end
def save_attachment(attachment)