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

[#49872] Extend CSP frame-ancestors with active nextcloud storage hosts

Pavel Balashou610506eccdd7
diff --git a/modules/storages/lib/open_project/storages/append_storages_hosts_to_csp_hook.rb b/modules/storages/lib/open_project/storages/append_storages_hosts_to_csp_hook.rb
index e7d4a81d4e9..4e31a311e0c 100644
--- a/modules/storages/lib/open_project/storages/append_storages_hosts_to_csp_hook.rb
+++ b/modules/storages/lib/open_project/storages/append_storages_hosts_to_csp_hook.rb
@@ -43,18 +43,31 @@ class ::OpenProject::Storages::AppendStoragesHostsToCspHook < OpenProject::Hook:
# 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)
- .map(&method(:remove_uri_path))
+ controller = context[:controller]
- return if hosts.empty?
+ active_nextcloud_storages_hosts = ::Storages::NextcloudStorage
+ .where(id: ::Storages::ProjectStorage.select(:storage_id))
+ .pluck(:host)
+ .map(&method(:remove_uri_path))
- # 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 })
+ if active_nextcloud_storages_hosts.present?
+ controller.append_content_security_policy_directives(
+ frame_ancestors: active_nextcloud_storages_hosts
+ )
+ end
+
+ storage_ids = ::Storages::ProjectStorage.where(
+ project_id: Project.allowed_to(User.current, :manage_file_links)
+ ).select(:storage_id)
+ storages_hosts = ::Storages::Storage
+ .where(id: storage_ids)
+ .pluck(:host)
+ .map(&method(:remove_uri_path))
+
+ if storages_hosts.present?
+ # secure_headers gem provides this helper method to append to the current content security policy
+ controller.append_content_security_policy_directives(connect_src: storages_hosts)
+ end
end
private
diff --git a/modules/storages/spec/lib/append_storages_hosts_to_csp_hook_spec.rb b/modules/storages/spec/lib/append_storages_hosts_to_csp_hook_spec.rb
index 3c59f6933b6..3e2aa62bcc1 100644
--- a/modules/storages/spec/lib/append_storages_hosts_to_csp_hook_spec.rb
+++ b/modules/storages/spec/lib/append_storages_hosts_to_csp_hook_spec.rb
@@ -52,12 +52,12 @@ RSpec.describe OpenProject::Storages::AppendStoragesHostsToCspHook do
hook_listener.application_controller_before_action(controller:)
end
- shared_examples 'appends storage host to CSP' do
- it 'adds CSP directive to allow connection to storage host' do
+ shared_examples 'modifies CSP' do
+ it 'adds CSP connect_src and frame_ancestors directives' do
trigger_application_controller_before_action_hook
- expect(controller).to have_received(:append_content_security_policy_directives)
- .with({ connect_src: [storage.host] })
+ expect(controller).to have_received(:append_content_security_policy_directives).with(frame_ancestors: [storage.host]).ordered
+ expect(controller).to have_received(:append_content_security_policy_directives).with(connect_src: [storage.host]).ordered
end
end
@@ -73,19 +73,24 @@ RSpec.describe OpenProject::Storages::AppendStoragesHostsToCspHook do
context 'when current user is an admin without being a member of any project' do
current_user { admin }
- include_examples 'appends storage host to CSP'
+ include_examples 'modifies 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]) }
- include_examples 'appends storage host to CSP'
+ include_examples 'modifies 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'
+ it 'adds CSP frame_ancestors directive only' do
+ trigger_application_controller_before_action_hook
+
+ expect(controller).to have_received(:append_content_security_policy_directives).with(frame_ancestors: [storage.host]).once
+ expect(controller).not_to have_received(:append_content_security_policy_directives).with(connect_src: [storage.host])
+ end
end
context 'when the project is archived' do
@@ -95,7 +100,12 @@ RSpec.describe OpenProject::Storages::AppendStoragesHostsToCspHook do
project.update(active: false)
end
- include_examples 'does not change CSP'
+ it 'adds CSP frame_ancestors directive only' do
+ trigger_application_controller_before_action_hook
+
+ expect(controller).to have_received(:append_content_security_policy_directives).with(frame_ancestors: [storage.host])
+ expect(controller).not_to have_received(:append_content_security_policy_directives).with(connect_src: [storage.host])
+ end
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 fd169a550ca..909ec310583 100644
--- a/modules/storages/spec/requests/append_content_security_policy_spec.rb
+++ b/modules/storages/spec/requests/append_content_security_policy_spec.rb
@@ -30,6 +30,13 @@ require 'spec_helper'
require_module_spec_helper
RSpec.describe 'Appendix of default CSP for external file storage hosts' do
+ def parse_csp(csp_string)
+ csp_string
+ .split('; ')
+ .map(&:split)
+ .each_with_object({}) { |csp_part, csp_hash_map| csp_hash_map[csp_part[0]] = csp_part[1..] }
+ end
+
shared_let(:project) { create(:project) }
shared_let(:storage) { create(:storage) }
shared_let(:project_storage) { create(:project_storage, project:, storage:) }
@@ -38,18 +45,22 @@ RSpec.describe 'Appendix of default CSP for external file storage hosts' do
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
+ it 'appends storage host to the connect-src and frame-ancestors CSP' do
get '/'
- expect(last_response.headers['Content-Security-Policy']).to match /#{storage.host}/
+ csp = parse_csp(last_response.headers['Content-Security-Policy'])
+ expect(csp['frame-ancestors']).to eq(["'self'", storage.host])
+ expect(csp['connect-src']).to include(storage.host)
end
end
context 'when not logged in' do
- it 'does not append the storage host to connect-src CSP' do
+ it 'appends storage host to frame-ancestors CSP only' do
get '/'
- expect(last_response.headers['Content-Security-Policy']).not_to match /#{storage.host}/
+ csp = parse_csp(last_response.headers['Content-Security-Policy'])
+ expect(csp['frame-ancestors']).to eq(["'self'", storage.host])
+ expect(csp['connect-src']).not_to include(storage.host)
end
end
end