From 3745743f4c00dd90a10c4f1ffada05b0a7e02209 Mon Sep 17 00:00:00 2001
From: Jens Kraemer <jk@jkraemer.net>
Date: Sun, 25 Oct 2026 18:49:04 +0800
Subject: [PATCH 2/2] Limit commit issue updates to what the committer may do
 #13762

- Check visibility, edit permission and workflow of the committer before
  applying the update rule; unmapped committers are checked as Anonymous
- Log time through TimeEntry#safe_attributes=
- Log why a commit could not update an issue or log time on it
- Existing tests now commit as a user allowed to change the issues
---
 app/models/changeset.rb     |  60 +++++++--
 test/unit/changeset_test.rb | 234 ++++++++++++++++++++++++++++++++++--
 2 files changed, 276 insertions(+), 18 deletions(-)

diff --git a/app/models/changeset.rb b/app/models/changeset.rb
index 49c323aca..dbc3c29ca 100644
--- a/app/models/changeset.rb
+++ b/app/models/changeset.rb
@@ -234,6 +234,21 @@ class Changeset < ApplicationRecord
     repository.same_commits_in_scope(issue.changesets, self).any?
   end
 
+  # Returns the user whose permissions limit the issue updates and time
+  # entries of this changeset: its committer
+  def acting_user
+    user || User.anonymous
+  end
+
+  def acting_user_name
+    acting_user.login.presence || acting_user.name
+  end
+
+  # Returns why the commit must not touch +issue+ at all, nil if it may
+  def issue_refusal(issue)
+    "not visible to #{acting_user_name}" unless issue.visible?(acting_user)
+  end
+
   # Updates the +issue+ according to +action+
   def fix_issue(issue, action)
     # the issue may have been updated by the closure of another one (eg. duplicate)
@@ -241,7 +256,13 @@ class Changeset < ApplicationRecord
     # don't change the status is the issue is closed
     return if issue.closed?
 
-    journal = issue.init_journal(user || User.anonymous,
+    # the commit must not change issues its acting user could not change
+    if refusal = issue_refusal(issue)
+      logger.info("Issue ##{issue.id} not updated by changeset #{format_identifier}: #{refusal}") if logger
+      return
+    end
+
+    journal = issue.init_journal(acting_user,
                                  ll(Setting.default_language,
                                     :text_status_changed_by_changeset,
                                     text_tag(issue.project)))
@@ -250,7 +271,12 @@ class Changeset < ApplicationRecord
         (rule['if_tracker_id'].blank? || rule['if_tracker_id'] == issue.tracker_id.to_s)
     end
     if rule
-      issue.assign_attributes rule.slice(*Issue.attribute_names)
+      attributes = rule.slice(*Issue.attribute_names)
+      issue.public_send :safe_attributes=, attributes, acting_user
+      unapplied = attributes.keys.reject {|name| issue.send(name).to_s == attributes[name].to_s}
+      if unapplied.any?
+        logger.info("Issue ##{issue.id} not fully updated by changeset #{format_identifier}: #{unapplied.join(', ')} not applied for #{acting_user_name}") if logger
+      end
     end
     Redmine::Hook.call_hook(:model_changeset_scan_commit_for_issue_ids_pre_issue_update,
                             {:changeset => self, :issue => issue, :action => action})
@@ -266,15 +292,27 @@ class Changeset < ApplicationRecord
   end
 
   def log_time(issue, hours)
-    time_entry =
-      TimeEntry.new(
-        :user => user,
-        :hours => hours,
-        :issue => issue,
-        :spent_on => commit_date,
-        :comments => l(:text_time_logged_by_changeset, :value => text_tag(issue.project),
-                       :locale => Setting.default_language)
-      )
+    # the commit must not log time on issues its acting user cannot see
+    if refusal = issue_refusal(issue)
+      logger.info("No time logged on issue ##{issue.id} by changeset #{format_identifier}: #{refusal}") if logger
+      return
+    end
+
+    # the acting user logs the time for the committer
+    time_entry = TimeEntry.new(:author => acting_user)
+    time_entry.public_send(
+      :safe_attributes=,
+      {
+        'user_id' => user_id,
+        'hours' => hours,
+        'project_id' => issue.project_id,
+        'issue_id' => issue.id,
+        'spent_on' => commit_date,
+        'comments' => l(:text_time_logged_by_changeset, :value => text_tag(issue.project),
+                        :locale => Setting.default_language)
+      },
+      acting_user
+    )
     if activity = issue.project.commit_logtime_activity
       time_entry.activity = activity
     end
diff --git a/test/unit/changeset_test.rb b/test/unit/changeset_test.rb
index 2dc34f996..ce4372c1a 100644
--- a/test/unit/changeset_test.rb
+++ b/test/unit/changeset_test.rb
@@ -32,7 +32,8 @@ class ChangesetTest < ActiveSupport::TestCase
     c = Changeset.new(:repository   => Project.find(1).repository,
                       :committed_on => Time.now,
                       :comments     => 'New commit (#2). Fixes #1',
-                      :revision     => '12345')
+                      :revision     => '12345',
+                      :committer    => 'admin')
     assert c.save
     assert_equal [1, 2], c.issue_ids.sort
     fixed = Issue.find(1)
@@ -210,7 +211,8 @@ class ChangesetTest < ActiveSupport::TestCase
         c = Changeset.
               generate!(
                 :repository => Project.find(1).repository,
-                :comments => "Fixes ##{issue.id}"
+                :comments => "Fixes ##{issue.id}",
+                :committer => 'jsmith'
               )
         assert_include c.id, issue.reload.changeset_ids
         journal = Journal.order(id: :desc).first
@@ -240,7 +242,7 @@ class ChangesetTest < ActiveSupport::TestCase
     ] do
       issue1 = Issue.generate!
       issue2 = Issue.generate!
-      Changeset.generate!(:comments => "Closes ##{issue1.id}\nResolves ##{issue2.id}")
+      Changeset.generate!(:comments => "Closes ##{issue1.id}\nResolves ##{issue2.id}", :committer => 'jsmith')
       assert_equal 5, issue1.reload.status_id
       assert_equal 3, issue2.reload.status_id
     end
@@ -253,7 +255,7 @@ class ChangesetTest < ActiveSupport::TestCase
     ] do
       issue1 = Issue.generate!(:tracker_id => 2)
       issue2 = Issue.generate!
-      Changeset.generate!(:comments => "Fixes ##{issue1.id}, ##{issue2.id}")
+      Changeset.generate!(:comments => "Fixes ##{issue1.id}, ##{issue2.id}", :committer => 'jsmith')
       assert_equal 5, issue1.reload.status_id
       assert_equal 3, issue2.reload.status_id
     end
@@ -266,7 +268,7 @@ class ChangesetTest < ActiveSupport::TestCase
     ] do
       issue1 = Issue.generate!(:tracker_id => 2)
       issue2 = Issue.generate!(:tracker_id => 2)
-      Changeset.generate!(:comments => "Testing ##{issue1.id}, Fixes ##{issue2.id}")
+      Changeset.generate!(:comments => "Testing ##{issue1.id}, Fixes ##{issue2.id}", :committer => 'jsmith')
       issue1.reload
       assert_equal 3, issue1.status_id
       assert_equal 90, issue1.done_ratio
@@ -283,7 +285,7 @@ class ChangesetTest < ActiveSupport::TestCase
     ] do
       issue1 = Issue.generate!(:tracker_id => 2)
       issue2 = Issue.generate!
-      Changeset.generate!(:comments => "Fixes ##{issue1.id}, ##{issue2.id}")
+      Changeset.generate!(:comments => "Fixes ##{issue1.id}, ##{issue2.id}", :committer => 'jsmith')
       assert_equal 5, issue1.reload.status_id
       assert_equal 1, issue2.reload.status_id # no updates
     end
@@ -308,7 +310,8 @@ class ChangesetTest < ActiveSupport::TestCase
         c = Changeset.new(:repository   => Project.find(1).repository,
                           :committed_on => Time.now,
                           :comments     => 'closes #5, a subproject issue',
-                          :revision     => '12345')
+                          :revision     => '12345',
+                          :committer    => 'admin')
         assert c.save
       end
       assert issue.reload.closed?
@@ -372,6 +375,214 @@ class ChangesetTest < ActiveSupport::TestCase
     end
   end
 
+  def test_fixing_an_issue_of_a_project_the_committer_cannot_access_should_not_update_it
+    with_settings :commit_cross_project_ref => '1',
+                  :commit_update_keywords => [{'keywords' => 'fixes', 'status_id' => '5', 'done_ratio' => '100'}] do
+      # dlopper is not a member of the private project onlinestore
+      c = Changeset.new(:repository   => Project.find(1).repository,
+                        :committed_on => Time.now,
+                        :comments     => 'Fixes #4',
+                        :revision     => '12345',
+                        :user         => User.find(3))
+      assert_no_difference 'Journal.count' do
+        c.scan_comment_for_issue_ids
+      end
+      issue = Issue.find(4)
+      assert_equal 1, issue.status_id
+      assert_equal 0, issue.done_ratio
+    end
+  end
+
+  def test_fixing_an_issue_the_committer_cannot_see_should_not_update_it
+    with_settings :commit_cross_project_ref => '1',
+                  :commit_update_keywords => [{'keywords' => 'fixes', 'status_id' => '5'}] do
+      # issue 14 is private and dlopper is neither its author nor its assignee
+      c = Changeset.new(:repository   => Project.find(1).repository,
+                        :committed_on => Time.now,
+                        :comments     => 'Fixes #14',
+                        :revision     => '12345',
+                        :user         => User.find(3))
+      assert_no_difference 'Journal.count' do
+        c.scan_comment_for_issue_ids
+      end
+      assert_equal 1, Issue.find(14).status_id
+    end
+  end
+
+  def test_fixing_an_issue_without_edit_issues_permission_should_not_update_it
+    Role.find(2).remove_permission!(:edit_issues)
+    with_settings :commit_update_keywords => [{'keywords' => 'fixes', 'status_id' => '5', 'done_ratio' => '100'}] do
+      c = Changeset.new(:repository   => Project.find(1).repository,
+                        :committed_on => Time.now,
+                        :comments     => 'Fixes #1',
+                        :revision     => '12345',
+                        :user         => User.find(3))
+      assert_no_difference 'Journal.count' do
+        c.scan_comment_for_issue_ids
+      end
+      issue = Issue.find(1)
+      assert_equal 1, issue.status_id
+      assert_equal 0, issue.done_ratio
+    end
+  end
+
+  def test_fixing_an_issue_should_respect_the_workflow_of_the_committer
+    WorkflowTransition.where(:role_id => 2, :tracker_id => 1, :old_status_id => 1, :new_status_id => 5).delete_all
+    with_settings :commit_update_keywords => [{'keywords' => 'fixes', 'status_id' => '5', 'done_ratio' => '100'}] do
+      c = Changeset.new(:repository   => Project.find(1).repository,
+                        :committed_on => Time.now,
+                        :comments     => 'Fixes #1',
+                        :revision     => '12345',
+                        :user         => User.find(3))
+      c.scan_comment_for_issue_ids
+      issue = Issue.find(1)
+      assert_equal 1, issue.status_id
+      assert_equal 100, issue.done_ratio
+    end
+  end
+
+  def test_fixing_an_issue_by_an_unknown_committer_should_check_anonymous_permissions
+    with_settings :commit_update_keywords => [{'keywords' => 'fixes', 'status_id' => '5'}] do
+      c = Changeset.new(:repository   => Project.find(1).repository,
+                        :committed_on => Time.now,
+                        :comments     => 'Fixes #1',
+                        :revision     => '12345')
+      assert_no_difference 'Journal.count' do
+        c.scan_comment_for_issue_ids
+      end
+      assert_equal 1, Issue.find(1).status_id
+    end
+  end
+
+  def test_logging_time_on_an_issue_of_a_project_the_committer_cannot_access_should_not_create_time_entry
+    with_settings :commit_cross_project_ref => '1', :commit_ref_keywords => '*',
+                  :commit_logtime_enabled => '1' do
+      c = Changeset.new(:repository   => Project.find(1).repository,
+                        :committed_on => Time.now,
+                        :comments     => 'Worked on #4 @1h',
+                        :revision     => '12345',
+                        :user         => User.find(3))
+      assert_no_difference 'TimeEntry.count' do
+        c.scan_comment_for_issue_ids
+      end
+    end
+  end
+
+  def test_logging_time_on_an_issue_the_committer_cannot_see_should_not_create_time_entry
+    with_settings :commit_cross_project_ref => '1', :commit_ref_keywords => '*',
+                  :commit_logtime_enabled => '1' do
+      c = Changeset.new(:repository   => Project.find(1).repository,
+                        :committed_on => Time.now,
+                        :comments     => 'Worked on #14 @1h',
+                        :revision     => '12345',
+                        :user         => User.find(3))
+      assert_no_difference 'TimeEntry.count' do
+        c.scan_comment_for_issue_ids
+      end
+    end
+  end
+
+  def test_logging_time_without_log_time_permission_should_not_create_time_entry
+    Role.find(2).remove_permission!(:log_time)
+    with_settings :commit_ref_keywords => '*', :commit_logtime_enabled => '1' do
+      c = Changeset.new(:repository   => Project.find(1).repository,
+                        :committed_on => Time.now,
+                        :comments     => 'Worked on #1 @1h',
+                        :revision     => '12345',
+                        :user         => User.find(3))
+      assert_no_difference 'TimeEntry.count' do
+        c.scan_comment_for_issue_ids
+      end
+    end
+  end
+
+  def test_fixing_an_issue_the_acting_user_cannot_see_should_be_logged
+    with_settings :commit_cross_project_ref => '1',
+                  :commit_update_keywords => [{'keywords' => 'fixes', 'status_id' => '5'}] do
+      c = Changeset.new(:repository   => Project.find(1).repository,
+                        :committed_on => Time.now,
+                        :comments     => 'Fixes #4',
+                        :revision     => '12345',
+                        :user         => User.find(3))
+      log = capture_changeset_log(c) {c.scan_comment_for_issue_ids}
+      assert_include 'Issue #4 not updated by changeset 12345: not visible to dlopper', log
+    end
+  end
+
+  def test_fixing_an_issue_with_changes_the_acting_user_may_not_make_should_be_logged
+    WorkflowTransition.where(:role_id => 2, :tracker_id => 1, :old_status_id => 1, :new_status_id => 5).delete_all
+    with_settings :commit_update_keywords => [{'keywords' => 'fixes', 'status_id' => '5', 'done_ratio' => '100'}] do
+      c = Changeset.new(:repository   => Project.find(1).repository,
+                        :committed_on => Time.now,
+                        :comments     => 'Fixes #1',
+                        :revision     => '12345',
+                        :user         => User.find(3))
+      log = capture_changeset_log(c) {c.scan_comment_for_issue_ids}
+      assert_include 'Issue #1 not fully updated by changeset 12345: status_id not applied for dlopper', log
+    end
+  end
+
+  def test_logging_time_the_acting_user_may_not_log_should_be_logged
+    Role.find(2).remove_permission!(:log_time)
+    with_settings :commit_ref_keywords => '*', :commit_logtime_enabled => '1' do
+      c = Changeset.new(:repository   => Project.find(1).repository,
+                        :committed_on => Time.now,
+                        :comments     => 'Worked on #1 @1h',
+                        :revision     => '12345',
+                        :user         => User.find(3))
+      log = capture_changeset_log(c) {c.scan_comment_for_issue_ids}
+      assert_include 'TimeEntry could not be created by changeset : ["Issue is invalid"]', log
+    end
+  end
+
+  def test_logged_time_should_be_authored_by_the_committer
+    User.current = User.find(1)
+    with_settings :commit_ref_keywords => '*', :commit_logtime_enabled => '1' do
+      c = Changeset.new(:repository   => Project.find(1).repository,
+                        :committed_on => Time.now,
+                        :comments     => 'Worked on #1 @1h',
+                        :revision     => '12345',
+                        :user         => User.find(2))
+      assert_difference 'TimeEntry.count' do
+        c.scan_comment_for_issue_ids
+      end
+      assert_equal User.find(2), TimeEntry.order(:id => :desc).first.author
+    end
+  end
+
+  def test_logging_time_for_another_user_should_require_the_acting_user_to_log_time_for_other_users
+    with_settings :commit_ref_keywords => '*', :commit_logtime_enabled => '1' do
+      c = Changeset.new(:repository   => Project.find(1).repository,
+                        :committed_on => Time.now,
+                        :comments     => 'Worked on #1 @1h',
+                        :revision     => '12345',
+                        :user         => User.find(3))
+      # jsmith acts for a commit of dlopper, e.g. as the user who pushed it
+      c.stubs(:acting_user).returns(User.find(2))
+      assert_no_difference 'TimeEntry.count' do
+        c.scan_comment_for_issue_ids
+      end
+    end
+  end
+
+  def test_time_logged_for_another_user_should_be_authored_by_the_acting_user
+    Role.find(1).add_permission!(:log_time_for_other_users)
+    with_settings :commit_ref_keywords => '*', :commit_logtime_enabled => '1' do
+      c = Changeset.new(:repository   => Project.find(1).repository,
+                        :committed_on => Time.now,
+                        :comments     => 'Worked on #1 @1h',
+                        :revision     => '12345',
+                        :user         => User.find(3))
+      c.stubs(:acting_user).returns(User.find(2))
+      assert_difference 'TimeEntry.count' do
+        c.scan_comment_for_issue_ids
+      end
+      time_entry = TimeEntry.order(:id => :desc).first
+      assert_equal User.find(3), time_entry.user
+      assert_equal User.find(2), time_entry.author
+    end
+  end
+
   def test_old_commits_should_not_update_issues_nor_log_time
     Setting.commit_ref_keywords = '*'
     Setting.commit_update_keywords = {'fixes , closes' => {'status_id' => '5', 'done_ratio' => '90'}}
@@ -737,4 +948,13 @@ class ChangesetTest < ActiveSupport::TestCase
     c = Changeset.find_by_revision('1')
     assert_equal c.revision, c.identifier
   end
+
+  private
+
+  def capture_changeset_log(changeset)
+    log = StringIO.new
+    changeset.stubs(:logger).returns(Logger.new(log))
+    yield
+    log.string
+  end
 end
-- 
2.56.0

