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

Add restricted mentions sanitizing service

Kabiru Mwenja3c4902ccb7f2
diff --git a/app/services/work_packages/activities_tab/restricted_mentions_sanitizer.rb b/app/services/work_packages/activities_tab/restricted_mentions_sanitizer.rb
new file mode 100644
index 00000000000..e926a0b263d
--- /dev/null
+++ b/app/services/work_packages/activities_tab/restricted_mentions_sanitizer.rb
@@ -0,0 +1,63 @@
+# frozen_string_literal: true
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 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 WorkPackages::ActivitiesTab::RestrictedMentionsSanitizer
+ def initialize(work_package, notes)
+ @work_package = work_package
+ @parser = Nokogiri::HTML.fragment(notes)
+ end
+
+ def call
+ convert_unmentionable_principals_to_plain_text
+ CGI.unescapeHTML(parser.to_html)
+ end
+
+ private
+
+ attr_reader :work_package, :parser
+
+ def convert_unmentionable_principals_to_plain_text
+ mentionable_principals_ids = mentionable_principals.pluck(:id)
+
+ parser.css("mention").each do |mention|
+ unless mentionable_principals_ids.include?(mention["data-id"].to_i)
+ mention.replace(mention.content)
+ end
+ end
+ end
+
+ def mentionable_principals
+ @mentionable_principals ||= Queries::Principals::PrincipalQuery.new(user: User.current)
+ .where(:restricted_mentionable_on_work_package, "=", [work_package.id])
+ .where(:status, "!", [Principal.statuses[:locked]])
+ .where(:type, "=", %w[User Group])
+ .results
+ end
+end
diff --git a/spec/services/work_packages/activities_tab/restricted_mentions_sanitizer_spec.rb b/spec/services/work_packages/activities_tab/restricted_mentions_sanitizer_spec.rb
new file mode 100644
index 00000000000..94e009cc058
--- /dev/null
+++ b/spec/services/work_packages/activities_tab/restricted_mentions_sanitizer_spec.rb
@@ -0,0 +1,131 @@
+# frozen_string_literal: true
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 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"
+
+RSpec.describe WorkPackages::ActivitiesTab::RestrictedMentionsSanitizer do
+ shared_let(:project) { create(:project) }
+ shared_let(:admin_but_non_member) { create(:admin) }
+ shared_let(:viewer) { create_user_without_restricted_comments_view_permissions }
+ shared_let(:user_with_restricted_comments_view_and_write_permissions) do
+ create_user_with_restricted_comments_view_and_write_permissions
+ end
+ shared_let(:project_admin) { create_user_as_project_admin }
+ shared_let(:work_package) { create(:work_package, project:) }
+
+ let(:input) do
+ <<~HTML
+ <mention class="mention" data-id="#{admin_but_non_member.id}" data-type="user" data-text="@#{admin_but_non_member.firstname}">@#{admin_but_non_member.firstname}</mention> wrote:
+
+ > Well Done!
+
+ <mention class="mention" data-id="#{viewer.id}" data-type="user" data-text="@#{viewer.firstname}">@#{viewer.firstname}</mention> wrote:
+
+ > <mention class="mention" data-id="#{user_with_restricted_comments_view_and_write_permissions.id}" data-type="user" data-text="@#{user_with_restricted_comments_view_and_write_permissions.firstname}">@#{user_with_restricted_comments_view_and_write_permissions.firstname}</mention> wrote:
+ >
+ > > <mention class="mention" data-id="#{project_admin.id}" data-type="user" data-text="@#{project_admin.firstname}">@#{project_admin.firstname}</mention> wrote:
+ > >
+ > > > FooBar
+ > >
+ > > <mention class="mention" data-id="435" data-type="user" data-text="@Firstname">@Firstname</mention> wrote:
+ > >
+ > > > BooBar
+ HTML
+ end
+
+ let(:expected_output) do
+ <<~HTML
+ @#{admin_but_non_member.firstname} wrote:
+
+ > Well Done!
+
+ @#{viewer.firstname} wrote:
+
+ > <mention class="mention" data-id="#{user_with_restricted_comments_view_and_write_permissions.id}" data-type="user" data-text="@#{user_with_restricted_comments_view_and_write_permissions.firstname}">@#{user_with_restricted_comments_view_and_write_permissions.firstname}</mention> wrote:
+ >
+ > > <mention class="mention" data-id="#{project_admin.id}" data-type="user" data-text="@#{project_admin.firstname}">@#{project_admin.firstname}</mention> wrote:
+ > >
+ > > > FooBar
+ > >
+ > > @Firstname wrote:
+ > >
+ > > > BooBar
+ HTML
+ end
+
+ subject { described_class.new(work_package, input).call }
+
+ before { allow(User).to receive(:current).and_return(user_with_restricted_comments_view_and_write_permissions) }
+
+ it "sanitizes the notes" do
+ expect(subject).to eq(expected_output)
+ end
+
+ def create_user_without_restricted_comments_view_permissions
+ viewer_role = create(:project_role, permissions: %i[view_work_packages])
+ create(:user,
+ firstname: "A",
+ lastname: "Viewer",
+ member_with_roles: { project => viewer_role })
+ end
+
+ def create_user_as_project_admin
+ member_role = create(:project_role,
+ permissions: %i[view_work_packages add_work_package_notes
+ edit_own_work_package_notes
+ view_comments_with_restricted_visibility
+ add_comments_with_restricted_visibility
+ edit_own_comments_with_restricted_visibility
+ edit_others_comments_with_restricted_visibility])
+ create(:user, firstname: "Project", lastname: "Admin",
+ member_with_roles: { project => member_role })
+ end
+
+ def create_user_with_restricted_comments_view_permissions
+ viewer_role = create(:project_role, permissions: %i[view_work_packages view_comments_with_restricted_visibility])
+ create(:user,
+ firstname: "Restricted",
+ lastname: "Viewer",
+ member_with_roles: { project => viewer_role })
+ end
+
+ def create_user_with_restricted_comments_view_and_write_permissions
+ viewer_role_with_commenting_permission = create(:project_role,
+ permissions: %i[view_work_packages add_work_package_notes
+ edit_own_work_package_notes
+ view_comments_with_restricted_visibility
+ add_comments_with_restricted_visibility
+ edit_own_comments_with_restricted_visibility])
+ create(:user,
+ firstname: "Restricted",
+ lastname: "ViewerCommenter",
+ member_with_roles: { project => viewer_role_with_commenting_permission })
+ end
+end