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

BCF-XML: reject unsupported BCF versions

Wieland Lindenthald8fbe4f68bd8
diff --git a/modules/bim/app/controllers/bim/bcf/issues_controller.rb b/modules/bim/app/controllers/bim/bcf/issues_controller.rb
index e639cb0ac3e..3441d5e3c65 100644
--- a/modules/bim/app/controllers/bim/bcf/issues_controller.rb
+++ b/modules/bim/app/controllers/bim/bcf/issues_controller.rb
@@ -43,6 +43,7 @@ module Bim
before_action :set_import_options, only: %i[perform_import]
before_action :build_importer, only: %i[prepare_import perform_import]
+ before_action :check_bcf_version, only: %i[prepare_import]
menu_item :work_packages
@@ -179,11 +180,11 @@ module Bim
end
def build_importer
- @importer = ::OpenProject::Bim::BcfXml::Importer.new @bcf_xml_file, @project, current_user: current_user
+ @importer = ::OpenProject::Bim::BcfXml::Importer.new(@bcf_xml_file, @project, current_user: current_user)
end
def get_persisted_file
- @bcf_attachment = Attachment.find_by! id: session[:bcf_file_id], author: current_user
+ @bcf_attachment = Attachment.find_by!(id: session[:bcf_file_id], author: current_user)
@bcf_xml_file = File.new @bcf_attachment.local_path
rescue ActiveRecord::RecordNotFound
flash[:error] = I18n.t('bcf.bcf_xml.import.bcf_file_not_found')
@@ -208,6 +209,13 @@ module Bim
redirect_to action: :upload
end
end
+
+ def check_bcf_version
+ unless @importer.bcf_version_valid?
+ flash[:error] = I18n.t('bcf.bcf_xml.import_failed_unsupported_bcf_version', minimal_version: OpenProject::Bim::BcfXml::Importer::MINIMUM_BCF_VERSION)
+ redirect_to action: :upload
+ end
+ end
end
end
end
diff --git a/modules/bim/config/locales/en.yml b/modules/bim/config/locales/en.yml
index 677d762a76c..75fa088ff8d 100644
--- a/modules/bim/config/locales/en.yml
+++ b/modules/bim/config/locales/en.yml
@@ -25,6 +25,7 @@ en:
export: 'Export'
import_update_comment: '(Updated in BCF import)'
import_failed: 'Cannot import BCF file: %{error}'
+ import_failed_unsupported_bcf_version: 'Failed to read the BCF file: The BCF version is not supported. Please ensure the version is at least %{minimal_version} or higher.'
import_successful: 'Imported %{count} BCF issues'
import_canceled: 'BCF-XML import canceled.'
type_not_active: "The issue type is not activated for this project."
diff --git a/modules/bim/lib/api/bim/bcf_xml/v1/bcf_xml_api.rb b/modules/bim/lib/api/bim/bcf_xml/v1/bcf_xml_api.rb
index 6e00840cca9..6658a708547 100644
--- a/modules/bim/lib/api/bim/bcf_xml/v1/bcf_xml_api.rb
+++ b/modules/bim/lib/api/bim/bcf_xml/v1/bcf_xml_api.rb
@@ -88,7 +88,17 @@ module API
importer = ::OpenProject::Bim::BcfXml::Importer.new(file,
project,
current_user: User.current)
+
+ unless importer.bcf_version_valid?
+ error_message = I18n.t('bcf.bcf_xml.import_failed_unsupported_bcf_version',
+ minimal_version: OpenProject::Bim::BcfXml::Importer::MINIMUM_BCF_VERSION)
+
+ raise API::Errors::UnsupportedMediaType.new(error_message)
+ end
+
importer.import!(import_options)
+ rescue API::Errors::UnsupportedMediaType => e
+ raise e
rescue StandardError => e
raise API::Errors::InternalError.new(e.message)
ensure
diff --git a/modules/bim/lib/open_project/bim/bcf_xml/importer.rb b/modules/bim/lib/open_project/bim/bcf_xml/importer.rb
index 65728c1cad4..26fb9bb1269 100644
--- a/modules/bim/lib/open_project/bim/bcf_xml/importer.rb
+++ b/modules/bim/lib/open_project/bim/bcf_xml/importer.rb
@@ -4,6 +4,7 @@ require_relative 'aggregations'
module OpenProject::Bim::BcfXml
class Importer
+ MINIMUM_BCF_VERSION = "2.1"
attr_reader :file, :project, :current_user
DEFAULT_IMPORT_OPTIONS = {
@@ -60,6 +61,19 @@ module OpenProject::Bim::BcfXml
raise
end
+ def bcf_version_valid?
+ Zip::File.open(@file) do |zip|
+ zip_entry = zip.find { |entry| entry.name.end_with?('bcf.version') }
+ markup = zip_entry.get_input_stream.read
+ doc = Nokogiri::XML(markup, nil, 'UTF-8')
+ bcf_version = doc.xpath('/Version').first['VersionId']
+ return Gem::Version.new(bcf_version) >= Gem::Version.new(MINIMUM_BCF_VERSION)
+ end
+ rescue StandardError => e
+ # The uploaded file could be anything.
+ false
+ end
+
private
def create_or_add_missing_members(options)
diff --git a/modules/bim/spec/fixtures/files/bcf_2_0_dummy.bcf b/modules/bim/spec/fixtures/files/bcf_2_0_dummy.bcf
new file mode 100644
index 00000000000..323335e83d3
Binary files /dev/null and b/modules/bim/spec/fixtures/files/bcf_2_0_dummy.bcf differ
diff --git a/modules/bim/spec/requests/api/bcf_xml/v1/bcf_xml_api_spec.rb b/modules/bim/spec/requests/api/bcf_xml/v1/bcf_xml_api_spec.rb
index cd3e56d6cde..7e25e9584be 100644
--- a/modules/bim/spec/requests/api/bcf_xml/v1/bcf_xml_api_spec.rb
+++ b/modules/bim/spec/requests/api/bcf_xml/v1/bcf_xml_api_spec.rb
@@ -151,6 +151,16 @@ describe 'BCF XML API v1 bcf_xml resource', type: :request do
expect(project.work_packages.count).to eql(1)
end
end
+
+ context "with unsupported BCF version (2.0)" do
+ let(:filename) { 'bcf_2_0_dummy.bcf' }
+
+ it "returns a status 415" do
+ expect(subject.status).to eql(415)
+ expect(subject.body).to match /BCF version is not supported/
+ expect(project.work_packages.count).to eql(1)
+ end
+ end
end
def zip_has_file?(zip_string, filename)