From 3abb21a9953e9f73c2b97b9db850eeac1e7c4998 Mon Sep 17 00:00:00 2001
From: Katsuya Hidaka <hidakatsuya@gmail.com>
Date: Sat, 30 Nov 2024 02:25:13 +0900
Subject: Escape labels in principals_check_box_tag

---
 app/helpers/application_helper.rb       | 2 +-
 test/helpers/application_helper_test.rb | 7 +++++++
 2 files changed, 8 insertions(+), 1 deletion(-)

diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb
index 4aa233851..ac25f9f5d 100644
--- a/app/helpers/application_helper.rb
+++ b/app/helpers/application_helper.rb
@@ -655,7 +655,7 @@ module ApplicationHelper
       principal_check_box << check_box_tag(name, principal.id, false, :id => nil)
       principal_check_box << avatar(principal, :size => 16).to_s if principal.is_a?(User)
       principal_check_box << content_tag('span', principal_icon(principal), :class => "name icon icon-#{principal.class.to_s.downcase}")
-      principal_check_box << principal.to_s
+      principal_check_box << h(principal.to_s)
       s << content_tag('label', principal_check_box.html_safe)
     end
     s.html_safe
diff --git a/test/helpers/application_helper_test.rb b/test/helpers/application_helper_test.rb
index dce3c3d25..dfc07f33b 100644
--- a/test/helpers/application_helper_test.rb
+++ b/test/helpers/application_helper_test.rb
@@ -2046,6 +2046,13 @@ class ApplicationHelperTest < Redmine::HelperTest
     end
   end
 
+  def test_principals_check_box_tag_should_escape_principal_name
+    User.find(1).update!(firstname: "firstname<>'", lastname: 'lastname&"')
+
+    tags = principals_check_box_tags('watcher[user_ids][]', [User.find(1)])
+    assert_include 'firstname&lt;&gt;&#39; lastname&amp;&quot;', tags
+  end
+
   def test_principals_options_for_select_with_users
     User.current = nil
     users = [User.find(2), User.find(4)]
-- 
2.45.2

