From 777443374a0e5b5e41f03ee690bd878c38c46c87 Mon Sep 17 00:00:00 2001
From: Jens Kraemer <jk@jkraemer.net>
Date: Sun, 25 Oct 2026 18:23:29 +0800
Subject: [PATCH 1/2] Check attributes against the given user in
 Issue#safe_attributes=

safe_attributes= takes a user, but asked safe_attribute? for project_id,
tracker_id, status_id and assigned_to_id without it, so User.current decided
whether these could change even if a different user was given.

This didn't surface as a bug anywhere so far since safe_attributes= apparently
is never used with a user other than User.current, but the fix for #13762
depends on it.
---
 app/models/issue.rb     |  8 ++++----
 test/unit/issue_test.rb | 32 ++++++++++++++++++++++++++++++++
 2 files changed, 36 insertions(+), 4 deletions(-)

diff --git a/app/models/issue.rb b/app/models/issue.rb
index 23ab7532d..4b05c9c67 100644
--- a/app/models/issue.rb
+++ b/app/models/issue.rb
@@ -583,7 +583,7 @@ class Issue < ApplicationRecord
     attrs = attrs.deep_dup
 
     # Project and Tracker must be set before since new_statuses_allowed_to depends on it.
-    if (p = attrs.delete('project_id')) && safe_attribute?('project_id')
+    if (p = attrs.delete('project_id')) && safe_attribute?('project_id', user)
       if p.is_a?(String) && !/^\d*$/.match?(p)
         p_id = Project.find_by_identifier(p).try(:id)
       else
@@ -599,7 +599,7 @@ class Issue < ApplicationRecord
       end
     end
 
-    if (t = attrs.delete('tracker_id')) && safe_attribute?('tracker_id')
+    if (t = attrs.delete('tracker_id')) && safe_attribute?('tracker_id', user)
       if allowed_target_trackers(user).where(:id => t.to_i).exists?
         self.tracker_id = t
       end
@@ -618,7 +618,7 @@ class Issue < ApplicationRecord
     end
 
     statuses_allowed = new_statuses_allowed_to(user)
-    if (s = attrs.delete('status_id')) && safe_attribute?('status_id')
+    if (s = attrs.delete('status_id')) && safe_attribute?('status_id', user)
       if statuses_allowed.collect(&:id).include?(s.to_i)
         self.status_id = s
       end
@@ -631,7 +631,7 @@ class Issue < ApplicationRecord
          !attrs.key?('is_private') && safe_attribute?('is_private', user)
       attrs['is_private'] = '1'
     end
-    if (u = attrs.delete('assigned_to_id')) && safe_attribute?('assigned_to_id')
+    if (u = attrs.delete('assigned_to_id')) && safe_attribute?('assigned_to_id', user)
       self.assigned_to_id = u
     end
     attrs = delete_unsafe_attributes(attrs, user)
diff --git a/test/unit/issue_test.rb b/test/unit/issue_test.rb
index e7763675c..64c1f8bac 100644
--- a/test/unit/issue_test.rb
+++ b/test/unit/issue_test.rb
@@ -1171,6 +1171,38 @@ class IssueTest < ActiveSupport::TestCase
     assert_equal Date.parse('2012-07-16'), issue.due_date
   end
 
+  def test_safe_attributes_should_check_status_against_the_given_user
+    User.current = nil
+    issue = Issue.find(1)
+
+    issue.send(:safe_attributes=, {'status_id' => '5'}, User.find(2))
+    assert_equal 5, issue.status_id
+  end
+
+  def test_safe_attributes_should_check_project_against_the_given_user
+    User.current = nil
+    issue = Issue.find(1)
+
+    issue.send(:safe_attributes=, {'project_id' => '2'}, User.find(2))
+    assert_equal 2, issue.project_id
+  end
+
+  def test_safe_attributes_should_check_tracker_against_the_given_user
+    User.current = nil
+    issue = Issue.find(1)
+
+    issue.send(:safe_attributes=, {'tracker_id' => '2'}, User.find(2))
+    assert_equal 2, issue.tracker_id
+  end
+
+  def test_safe_attributes_should_check_assignee_against_the_given_user
+    User.current = nil
+    issue = Issue.find(1)
+
+    issue.send(:safe_attributes=, {'assigned_to_id' => '3'}, User.find(2))
+    assert_equal 3, issue.assigned_to_id
+  end
+
   def test_required_attributes_should_be_validated
     cf = IssueCustomField.create!(:name => 'Foo', :field_format => 'string',
                                   :is_for_all => true, :tracker_ids => [1, 2])
-- 
2.56.0

