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

Validate content type in contract

Oliver Güntherb9875fa0c5d3
diff --git a/app/contracts/attachments/create_contract.rb b/app/contracts/attachments/create_contract.rb
index b7d8aad91b9..60a7b57b13b 100644
--- a/app/contracts/attachments/create_contract.rb
+++ b/app/contracts/attachments/create_contract.rb
@@ -45,6 +45,7 @@ module Attachments
validate :validate_attachments_addable
validate :validate_container_addable
validate :validate_author
+ validate :validate_content_type
private
@@ -67,5 +68,24 @@ module Attachments
errors.add(:base, :error_unauthorized) unless model.container.attachments_addable?(user)
end
+
+ ##
+ # Validates the content type, if a whitelist is set
+ def validate_content_type
+ # In some cases, we do not want to process the whitelist
+ # for the attachment mime types. For example, when
+ # storing backups or exports
+ return unless options.fetch(:validate_content_type, true)
+
+ whitelist = Array(Setting.attachment_whitelist)
+
+ # If the whitelist is empty, assume all files are allowed
+ # as before
+ return if whitelist.empty?
+
+ unless whitelist.include?(model.content_type) || whitelist.include?("*#{model.extension}")
+ errors.add :content_type, :not_whitelisted, value: model.content_type
+ end
+ end
end
end
diff --git a/config/locales/en.yml b/config/locales/en.yml
index 09e42c08dc8..a0a4bbe12af 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -637,6 +637,10 @@ en:
unremovable: "cannot be removed."
wrong_length: "is the wrong length (should be %{count} characters)."
models:
+ attachment:
+ attributes:
+ content_type:
+ not_whitelisted: "is set to '%{value}', which is not whitelisted for uploading."
capability:
context:
global: 'Global'
diff --git a/lib/open_project/content_type_detector.rb b/lib/open_project/content_type_detector.rb
index b4f164bff00..c8146a0ce73 100644
--- a/lib/open_project/content_type_detector.rb
+++ b/lib/open_project/content_type_detector.rb
@@ -91,15 +91,15 @@ module OpenProject
# Returns a String describing the file's content type
def detect
- type = if blank_name?
- SENSIBLE_DEFAULT
- elsif empty_file?
- EMPTY_TYPE
- elsif calculated_type_matches.any?
- calculated_type_matches.first
- else
- type_from_file_command || SENSIBLE_DEFAULT
- end.to_s
+ if blank_name?
+ SENSIBLE_DEFAULT
+ elsif empty_file?
+ EMPTY_TYPE
+ elsif calculated_type_matches.any?
+ calculated_type_matches.first
+ else
+ type_from_file_command || SENSIBLE_DEFAULT
+ end.to_s
end
private