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

[#45474] CSP: Allow connect-src for all storages a user knows

Wieland Lindenthalf2525eddbefe
diff --git a/modules/storages/lib/open_project/storages/append_content_security_policy.rb b/modules/storages/lib/open_project/storages/append_content_security_policy.rb
new file mode 100644
index 00000000000..15412b66ba1
--- /dev/null
+++ b/modules/storages/lib/open_project/storages/append_content_security_policy.rb
@@ -0,0 +1,49 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2022 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 ::OpenProject::Storages::AppendContentSecurityPolicy < OpenProject::Hook::Listener
+ # OpenProject's front-end needs to allow the browser to connect to external file servers for direct file uploads.
+ # Therefore it needs to extend its content security policy (CSP) `connect-src` by the hostnames of all servers that
+ # the current user is allowed to upload files. That is the case for all storages that activated in at least one active
+ # project in which the user is member and has the permission to `manage_file_links`.
+ # The allowed values can be different for every user and can change every time a store gets activated, removed, a role
+ # changes, or even project memberships. Caching it, without accessing the DB seems to be pretty impossible. So we
+ # decided to not do it for now.
+ # We extend the CSP for all HTML requests as work packages can pop in many places of OpenProject, and we want to be
+ # able to upload in all those places (work packages module, BCF module, notification center, boards, ...).
+ def application_controller_before_action(context)
+ projects_of_user = Project.allowed_to(User.current, :manage_file_links).select(:id)
+ projects_with_permission = ::Storages::ProjectStorage.where(project_id: projects_of_user)
+ .select(:storage_id)
+ hosts = ::Storages::Storage.where(id: projects_with_permission)
+ .pluck(:host)
+
+ # secure_headers gem provides this helper method to append to the current content security policy
+ controller = context[:controller]
+ controller.append_content_security_policy_directives({ connect_src: hosts })
+ end
+end
diff --git a/modules/storages/lib/open_project/storages/engine.rb b/modules/storages/lib/open_project/storages/engine.rb
index ee2de919a57..6d07068d1b2 100644
--- a/modules/storages/lib/open_project/storages/engine.rb
+++ b/modules/storages/lib/open_project/storages/engine.rb
@@ -92,6 +92,9 @@ module OpenProject::Storages
# This hook is executed when the module is loaded.
config.to_prepare do
+ # Allow the browser to connect to external servers for direct file uploads.
+ AppendContentSecurityPolicy
+
# We have a bunch of filters defined within the module. Here we register the filters.
::Queries::Register.register(::Query) do
[
diff --git a/modules/storages/spec/lib/append_content_security_policy_spec.rb b/modules/storages/spec/lib/append_content_security_policy_spec.rb
new file mode 100644
index 00000000000..a5c78414c4c
--- /dev/null
+++ b/modules/storages/spec/lib/append_content_security_policy_spec.rb
@@ -0,0 +1,99 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2022 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.
+#++
+
+require_relative '../spec_helper'
+
+# These specs mainly check that error messages from a sub-service
+# (about unsafe hosts with HTTP protocol) are passed to the main form.
+describe OpenProject::Storages::AppendContentSecurityPolicy do
+ let(:permissions) { %i(manage_file_links) }
+ let(:project) { create(:project) }
+ let(:current_user) { create(:user, member_in_project: project, member_with_permissions: permissions) }
+ let(:storage) { create(:storage) }
+ let(:project_storage) { create(:project_storage, project:, storage:) }
+
+ before do
+ current_user
+ storage
+ project_storage
+
+ login_as current_user
+ end
+
+ shared_examples 'appends hosts to CSP' do
+ let(:controller) { instance_double(ApplicationController) }
+ let(:context) { { controller: } }
+
+ before do
+ allow(controller).to receive(:append_content_security_policy_directives)
+
+ instance = described_class.instance
+ instance.application_controller_before_action(context)
+ end
+
+ it 'secure_header helper function for appending CSP is called with correct storage hosts' do
+ expect(controller).to have_received(:append_content_security_policy_directives)
+ .with({ connect_src: expected_hosts })
+ end
+ end
+
+ context 'on happy path' do
+ let(:expected_hosts) { [storage.host] }
+
+ it_behaves_like 'appends hosts to CSP'
+ end
+
+ context 'when current user is admin without being a member of any project' do
+ let(:current_user) { create(:admin) }
+ let(:expected_hosts) { [storage.host] }
+
+ it_behaves_like 'appends hosts to CSP'
+ end
+
+ context 'without correct permission' do
+ let(:permissions) { [] }
+ let(:expected_hosts) { [] }
+
+ it_behaves_like 'appends hosts to CSP'
+ end
+
+ context 'without the storage being active in a project' do
+ let(:project_storage) { nil }
+ let(:expected_hosts) { [] }
+
+ it_behaves_like 'appends hosts to CSP'
+ end
+
+ context 'without the storage' do
+ let(:project_storage) { nil }
+ let(:storage) { nil }
+ let(:expected_hosts) { [] }
+
+ it_behaves_like 'appends hosts to CSP'
+ end
+end
diff --git a/modules/storages/spec/requests/append_content_security_policy_spec.rb b/modules/storages/spec/requests/append_content_security_policy_spec.rb
new file mode 100644
index 00000000000..2a07156f7fa
--- /dev/null
+++ b/modules/storages/spec/requests/append_content_security_policy_spec.rb
@@ -0,0 +1,63 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2022 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.
+#++
+
+require 'spec_helper'
+require_module_spec_helper
+
+describe 'Appendix of default CSP for external file storage hosts' do
+ let(:permissions) { %i(manage_file_links) }
+ let(:project) { create(:project) }
+ let(:current_user) { create(:user, member_in_project: project, member_with_permissions: permissions) }
+ let(:storage) { create(:storage) }
+ let(:project_storage) { create(:project_storage, project:, storage:) }
+
+ before do
+ storage
+ project_storage
+ end
+
+ describe 'GET /' do
+ let(:path) { '/' }
+
+ context 'when logged in' do
+ it 'appends to storage host to the connect-src CSP' do
+ login_as current_user
+ get path
+ expect(last_response.headers['Content-Security-Policy']).to match /#{storage.host}/
+ end
+ end
+
+ context 'when not logged in' do
+ it 'does not append the storage host to connect-src CSP' do
+ get path
+
+ expect(last_response.headers['Content-Security-Policy']).not_to match /#{storage.host}/
+ end
+ end
+ end
+end