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

Make test and documentation more readable

Christophe Bliard30bd4ddec6a2
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
index 15412b66ba1..5de8f6b017d 100644
--- a/modules/storages/lib/open_project/storages/append_content_security_policy.rb
+++ b/modules/storages/lib/open_project/storages/append_content_security_policy.rb
@@ -25,16 +25,23 @@
#
# 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, ...).
+ # 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` with the hostnames of all
+ # servers that the current user is allowed to upload files to. That is all
+ # storages activated in at least one active project for which the user has the
+ # `manage_file_links` permission.
+ #
+ # The allowed values can be different for each user and can change on store
+ # activations, store removals, role changes, and even project membership
+ # changes. Caching it without accessing the database seems almost impossible,
+ # so we decided to not do it for now.
+ #
+ # The CSP is extended 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)
@@ -42,6 +49,8 @@ class ::OpenProject::Storages::AppendContentSecurityPolicy < OpenProject::Hook::
hosts = ::Storages::Storage.where(id: projects_with_permission)
.pluck(:host)
+ return if hosts.empty?
+
# 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 })
diff --git a/modules/storages/spec/lib/append_content_security_policy_spec.rb b/modules/storages/spec/lib/append_content_security_policy_spec.rb
index a5c78414c4c..0865f2779ab 100644
--- a/modules/storages/spec/lib/append_content_security_policy_spec.rb
+++ b/modules/storages/spec/lib/append_content_security_policy_spec.rb
@@ -31,69 +31,83 @@ 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) }
+ shared_let(:admin) { create(:admin) }
+ shared_let(:project) { create(:project) }
let(:storage) { create(:storage) }
let(:project_storage) { create(:project_storage, project:, storage:) }
+ let(:controller) { instance_double(ApplicationController) }
before do
- current_user
storage
project_storage
- login_as current_user
+ allow(controller).to receive(:append_content_security_policy_directives)
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)
+ def trigger_application_controller_before_action_hook
+ hook_listener = described_class.instance
+ hook_listener.application_controller_before_action(controller:)
+ end
- instance = described_class.instance
- instance.application_controller_before_action(context)
- end
+ shared_examples 'appends storage host to CSP' do
+ it 'adds CSP directive to allow connection to storage host' do
+ trigger_application_controller_before_action_hook
- 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 })
+ .with({ connect_src: [storage.host] })
end
end
- context 'on happy path' do
- let(:expected_hosts) { [storage.host] }
+ shared_examples 'does not change CSP' do
+ it 'does not change CSP directives' do
+ trigger_application_controller_before_action_hook
- it_behaves_like 'appends hosts to CSP'
+ expect(controller).not_to have_received(:append_content_security_policy_directives)
+ end
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] }
+ context 'with a project with an active storage' do
+ context 'when current user is an admin without being a member of any project' do
+ current_user { admin }
- it_behaves_like 'appends hosts to CSP'
- end
+ include_examples 'appends storage host to CSP'
+ end
+
+ context 'when current user is a member of the project with permission to manage file links' do
+ current_user { create(:user, member_in_project: project, member_with_permissions: %i[manage_file_links]) }
- context 'without correct permission' do
- let(:permissions) { [] }
- let(:expected_hosts) { [] }
+ include_examples 'appends storage host to CSP'
+ end
+
+ context 'when current user is a member of the project without permission to manage file links' do
+ current_user { create(:user, member_in_project: project, member_with_permissions: []) }
+
+ include_examples 'does not change CSP'
+ end
- it_behaves_like 'appends hosts to CSP'
+ context 'when the project is archived' do
+ current_user { admin }
+
+ before do
+ project.update(active: false)
+ end
+
+ include_examples 'does not change CSP'
+ end
end
- context 'without the storage being active in a project' do
+ context 'with a project without an active storage' do
+ current_user { admin }
let(:project_storage) { nil }
- let(:expected_hosts) { [] }
- it_behaves_like 'appends hosts to CSP'
+ include_examples 'does not change CSP'
end
- context 'without the storage' do
+ context 'with a project without any storages configured' do
+ current_user { admin }
let(:project_storage) { nil }
let(:storage) { nil }
- let(:expected_hosts) { [] }
- it_behaves_like 'appends hosts to CSP'
+ include_examples 'does not change 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
index 2a07156f7fa..bbeb1a71f1d 100644
--- a/modules/storages/spec/requests/append_content_security_policy_spec.rb
+++ b/modules/storages/spec/requests/append_content_security_policy_spec.rb
@@ -30,31 +30,24 @@ 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
+ shared_let(:project) { create(:project) }
+ shared_let(:storage) { create(:storage) }
+ shared_let(:project_storage) { create(:project_storage, project:, storage:) }
describe 'GET /' do
- let(:path) { '/' }
-
context 'when logged in' do
+ current_user { create(:user, member_in_project: project, member_with_permissions: %i[manage_file_links]) }
+
it 'appends to storage host to the connect-src CSP' do
- login_as current_user
- get path
+ get '/'
+
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
+ get '/'
expect(last_response.headers['Content-Security-Policy']).not_to match /#{storage.host}/
end