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

Sanitize mentions on wp restricted comments

Kabiru Mwenja3433e7ea639f
diff --git a/app/controllers/work_packages/activities_tab_controller.rb b/app/controllers/work_packages/activities_tab_controller.rb
index e96e4725049..67ffac1bcf3 100644
--- a/app/controllers/work_packages/activities_tab_controller.rb
+++ b/app/controllers/work_packages/activities_tab_controller.rb
@@ -139,6 +139,13 @@ class WorkPackages::ActivitiesTabController < ApplicationController
respond_with_turbo_streams
end
+ def sanitize_restricted_mentions
+ sanitizer = WorkPackages::ActivitiesTab::RestrictedMentionsSanitizer.new(@work_package, journal_params[:notes])
+ sanitized_notes = sanitizer.call
+
+ render plain: sanitized_notes
+ end
+
def toggle_reaction # rubocop:disable Metrics/AbcSize
emoji_reaction_service =
if @journal.emoji_reactions.exists?(user: User.current, reaction: params[:reaction])
diff --git a/config/initializers/permissions.rb b/config/initializers/permissions.rb
index eda5478685c..f0bd66ab8f8 100644
--- a/config/initializers/permissions.rb
+++ b/config/initializers/permissions.rb
@@ -289,7 +289,7 @@ Rails.application.reloader.to_prepare do
# FIXME: Although the endpoint is removed, the code checking whether a user
# is eligible to add work packages through the API still seems to rely on this.
journals: [:new],
- "work_packages/activities_tab": %i[create toggle_reaction]
+ "work_packages/activities_tab": %i[create toggle_reaction sanitize_restricted_mentions]
},
permissible_on: %i[work_package project],
dependencies: :view_work_packages
diff --git a/config/routes.rb b/config/routes.rb
index 9d448e89d0c..1284f54a8e7 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -650,10 +650,12 @@ Rails.application.routes.draw do
get :cancel_edit
put :toggle_reaction
end
+
collection do
get :update_streams
get :update_filter # filter not persisted
put :update_sorting # sorting is persisted
+ post :sanitize_restricted_mentions
end
end
diff --git a/frontend/src/stimulus/controllers/dynamic/work-packages/activities-tab/restricted-comment.controller.ts b/frontend/src/stimulus/controllers/dynamic/work-packages/activities-tab/restricted-comment.controller.ts
index 01ed6fc9905..7e697d3b47f 100644
--- a/frontend/src/stimulus/controllers/dynamic/work-packages/activities-tab/restricted-comment.controller.ts
+++ b/frontend/src/stimulus/controllers/dynamic/work-packages/activities-tab/restricted-comment.controller.ts
@@ -58,8 +58,11 @@ export default class RestrictedCommentController extends Controller {
this.formContainerTarget.classList.toggle(this.highlightClass, isChecked);
this.toggleLearnMoreLink(isChecked);
-
this.isRestrictedValue = isChecked;
+
+ if (isChecked) {
+ void this.sanitizeRestrictedMentions();
+ }
}
private toggleLearnMoreLink(isChecked:boolean):void {
@@ -67,4 +70,34 @@ export default class RestrictedCommentController extends Controller {
this.learnMoreLinkTarget.classList.toggle(this.hiddenClass, !isChecked);
}
+
+ private async sanitizeRestrictedMentions():Promise<void> {
+ if (this.ckEditorInstance) {
+ const editorData = this.ckEditorInstance.getData({ trim: false });
+ if (editorData.length === 0) return;
+
+ const sanitizePath = `/work_packages/${this.workPackagesActivitiesTabIndexOutlet.workPackageIdValue}/activities/sanitize_restricted_mentions`;
+ const csrfToken = (document.querySelector('meta[name="csrf-token"]') as HTMLMetaElement).content;
+
+ const response = await fetch(sanitizePath, {
+ method: 'POST',
+ body: JSON.stringify({ journal: { notes: editorData } }),
+ headers: {
+ 'X-CSRF-Token': csrfToken,
+ 'Content-Type': 'application/json',
+ },
+ });
+
+ if (response.ok) {
+ const sanitizedNotes = await response.text();
+ this.ckEditorInstance.setData(sanitizedNotes);
+ } else {
+ console.error('Failed to sanitize restricted mentions');
+ }
+ }
+ }
+
+ private get ckEditorInstance() {
+ return this.workPackagesActivitiesTabIndexOutlet.getCkEditorInstance();
+ }
}
diff --git a/spec/features/activities/work_package/restricted_visibility_comments_spec.rb b/spec/features/activities/work_package/restricted_visibility_comments_spec.rb
index a5f73fc0cdb..24002af32db 100644
--- a/spec/features/activities/work_package/restricted_visibility_comments_spec.rb
+++ b/spec/features/activities/work_package/restricted_visibility_comments_spec.rb
@@ -195,7 +195,7 @@ RSpec.describe "Work package comments with restricted visibility",
wp_page.wait_for_activity_tab
end
- context "with restricted comments enabled" do
+ context "with restricted comments initially enabled" do
it "restricts mentions to project members with view comments with restricted visibility permission" do
activity_tab.open_new_comment_editor
expect(page).to have_test_selector("op-work-package-journal-form-element")
@@ -209,12 +209,27 @@ RSpec.describe "Work package comments with restricted visibility",
end
end
- context "with restricted comments disabled" do
- it "allows mentioning project members" do
+ context "with restricted comments initially disabled" do
+ it "allows mentioning project members but they are sanitized when the checkbox is checked" do
activity_tab.type_comment("@")
expect(page.all(".mention-list-item").map(&:text))
.to contain_exactly("A Viewer", "Group", "Project Admin", "Restricted Viewer", "Restricted ViewerCommenter")
+
+ page.find(".mention-list-item", text: "A Viewer").click
+ activity_tab.type_comment("@Restricted")
+ page.first(".mention-list-item", text: "Restricted Viewer").click
+ activity_tab.type_comment("@Group")
+ page.find(".mention-list-item", text: "Group").click
+
+ activity_tab.check_restricted_visibility_comment_checkbox
+
+ page.within_test_selector("op-work-package-journal-form-element") do
+ expect(page.all("a.mention").map(&:text))
+ .to contain_exactly("@Restricted Viewer")
+
+ expect(page).to have_text("@A Viewer") & have_text("@Group")
+ end
end
end
end