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
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
Enforce writing the content type without accepting user input
Oliver Günther9323b89bace8
diff --git a/app/models/attachment.rb b/app/models/attachment.rbindex b70b90ede94..3ad52806285 100644--- a/app/models/attachment.rb+++ b/app/models/attachment.rb@@ -175,9 +175,7 @@ class Attachment < ApplicationRecordenddef 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)enddef set_digest(file)diff --git a/app/services/attachments/set_attributes_service.rb b/app/services/attachments/set_attributes_service.rbindex 334ada1fac0..3f7e5c5b522 100644--- a/app/services/attachments/set_attributes_service.rb+++ b/app/services/attachments/set_attributes_service.rb@@ -30,6 +30,12 @@module Attachmentsclass 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?enddiff --git a/app/services/attachments/set_prepared_attributes_service.rb b/app/services/attachments/set_prepared_attributes_service.rbindex 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 Attachmentsmodel.extend(OpenProject::ChangedBySystem)model.change_by_system domodel.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_DEFAULTendenddiff --git a/app/workers/attachments/finish_direct_upload_job.rb b/app/workers/attachments/finish_direct_upload_job.rbindex 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 < ApplicationJobdef 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_fileenddef save_attachment(attachment)