From e1f357392076e6c0412b268d3749906588df49e4 Mon Sep 17 00:00:00 2001 From: Jan Schulz-Hofen Date: Sun, 6 Dec 2026 12:04:42 +0100 Subject: [PATCH] Reject a tracker that is not allowed instead of using another one (#44721) Issue#safe_attributes= dropped a tracker_id that the user cannot use in the project. The fallback for a missing tracker_id then assigned the first allowed tracker. The issue was saved with a tracker that nobody asked for, and the REST API answered 201 Created. The model now remembers the rejected tracker_id and the validation adds an inclusion error. The API answers 422. A request without tracker_id still gets the first allowed tracker. Incoming email keeps the old behaviour. The sender cannot correct the tracker and submit again, so MailHandler ignores a tracker keyword that the sender cannot use. --- app/models/issue.rb | 10 ++++- app/models/mail_handler.rb | 17 +++++++- test/functional/issues_controller_test.rb | 8 ++-- test/integration/api_test/issues_test.rb | 51 ++++++++++++++++++++++- test/unit/issue_test.rb | 31 ++++++++++++++ test/unit/mail_handler_test.rb | 36 ++++++++++++++++ 6 files changed, 144 insertions(+), 9 deletions(-) diff --git a/app/models/issue.rb b/app/models/issue.rb index 86732ac..a77a2c6 100644 --- a/app/models/issue.rb +++ b/app/models/issue.rb @@ -284,6 +284,7 @@ class Issue < ApplicationRecord alias :base_reload :reload def reload(*) @workflow_rule_by_attribute = nil + @invalid_tracker_id = nil @assignable_versions = nil @relations = nil @spent_hours = nil @@ -602,6 +603,11 @@ class Issue < ApplicationRecord if (t = attrs.delete('tracker_id')) && safe_attribute?('tracker_id') if allowed_target_trackers(user).where(:id => t.to_i).exists? self.tracker_id = t + @invalid_tracker_id = nil + elsif t.present? && t.to_s != tracker_id.to_s + # Remember the rejected tracker so that the validation can report it + # instead of silently using another tracker + @invalid_tracker_id = t end end if project && tracker.nil? @@ -799,7 +805,9 @@ class Issue < ApplicationRecord end # Checks that the issue can not be added/moved to a disabled tracker - if project && (tracker_id_changed? || project_id_changed?) + if project && @invalid_tracker_id.present? + errors.add :tracker_id, :inclusion + elsif project && (tracker_id_changed? || project_id_changed?) if tracker && !project.trackers.include?(tracker) errors.add :tracker_id, :inclusion end diff --git a/app/models/mail_handler.rb b/app/models/mail_handler.rb index 61d474d..e40b8b2 100644 --- a/app/models/mail_handler.rb +++ b/app/models/mail_handler.rb @@ -206,7 +206,7 @@ class MailHandler < ActionMailer::Base issue.tracker_id ||= project.trackers.first.try(:id) end end - issue.safe_attributes = attributes + issue.safe_attributes = without_unallowed_tracker(issue, attributes) issue.safe_attributes = {'custom_field_values' => custom_field_values_from_keywords(issue)} issue.subject = cleaned_up_subject if issue.subject.blank? @@ -254,7 +254,7 @@ class MailHandler < ActionMailer::Base # If the received email was a reply to a private note, make the added note private issue.private_notes = true end - issue.safe_attributes = issue_attributes_from_keywords(issue) + issue.safe_attributes = without_unallowed_tracker(issue, issue_attributes_from_keywords(issue)) issue.safe_attributes = {'custom_field_values' => custom_field_values_from_keywords(issue)} journal.notes = cleaned_up_text_body @@ -473,6 +473,19 @@ class MailHandler < ActionMailer::Base target end + # Removes a tracker that the sender cannot use from the given attributes. + # The sender of an email cannot correct the tracker and submit again like + # the user of the form or of the API. The issue therefore keeps its tracker + # or gets the default one, and the email is not rejected. + def without_unallowed_tracker(issue, attrs) + tracker_id = attrs['tracker_id'] + if tracker_id && tracker_id != issue.tracker_id && + !issue.allowed_target_trackers(user).exists?(tracker_id) + attrs = attrs.except('tracker_id') + end + attrs + end + # Returns a Hash of issue attributes extracted from keywords in the email body def issue_attributes_from_keywords(issue) attrs = { diff --git a/test/functional/issues_controller_test.rb b/test/functional/issues_controller_test.rb index cecc9e4..fe8984c 100644 --- a/test/functional/issues_controller_test.rb +++ b/test/functional/issues_controller_test.rb @@ -4948,12 +4948,12 @@ class IssuesControllerTest < Redmine::ControllerTest assert_equal '', issue.custom_field_value(cf2) end - def test_create_should_ignore_unallowed_trackers + def test_create_should_reject_unallowed_trackers role = Role.find(1) role.set_permission_trackers :add_issues, [3] role.save! @request.session[:user_id] = 2 - issue = new_record(Issue) do + assert_no_difference 'Issue.count' do post( :create, :params => { @@ -4965,9 +4965,9 @@ class IssuesControllerTest < Redmine::ControllerTest } } ) - assert_response :found end - assert_equal 3, issue.tracker_id + assert_response :success + assert_select_error /Tracker is not included in the list/ end def test_post_create_with_watchers diff --git a/test/integration/api_test/issues_test.rb b/test/integration/api_test/issues_test.rb index c723c67..2b6384e 100644 --- a/test/integration/api_test/issues_test.rb +++ b/test/integration/api_test/issues_test.rb @@ -916,6 +916,51 @@ class Redmine::ApiTest::IssuesTest < Redmine::ApiTest::Base assert json['errors'].include?("Subject cannot be blank") end + test "POST /issues.json with a tracker_id that is disabled in the project should respond with 422" do + project = Project.find(1) + project.trackers.delete(Tracker.find(2)) + + assert_no_difference('Issue.count') do + post( + '/issues.json', + :params => {:issue => {:project_id => 1, :subject => 'API', :tracker_id => 2}}, + :headers => credentials('jsmith')) + end + assert_response :unprocessable_content + json = ActiveSupport::JSON.decode(response.body) + assert_include 'Tracker is not included in the list', json['errors'] + end + + test "POST /issues.json with a tracker_id that the role cannot add should respond with 422" do + role = Role.find(1) + role.set_permission_trackers :add_issues, [3] + role.save! + + assert_no_difference('Issue.count') do + post( + '/issues.json', + :params => {:issue => {:project_id => 1, :subject => 'API', :tracker_id => 1}}, + :headers => credentials('jsmith')) + end + assert_response :unprocessable_content + json = ActiveSupport::JSON.decode(response.body) + assert_include 'Tracker is not included in the list', json['errors'] + end + + test "PUT /issues/:id.json with a tracker_id that is disabled in the project should respond with 422" do + project = Project.find(1) + project.trackers.delete(Tracker.find(2)) + + put( + '/issues/1.json', + :params => {:issue => {:tracker_id => 2}}, + :headers => credentials('jsmith')) + assert_response :unprocessable_content + json = ActiveSupport::JSON.decode(response.body) + assert_include 'Tracker is not included in the list', json['errors'] + assert_equal 1, Issue.find(1).tracker_id + end + test "POST /issues.json with invalid project_id should respond with 422" do post( '/issues.json', @@ -1152,13 +1197,15 @@ class Redmine::ApiTest::IssuesTest < Redmine::ApiTest::Base assert_equal 'A new note', Journal.last.notes end - test "PUT /issues/:id.json authenticated via OAuth should not change the tracker without add_issues scope" do + test "PUT /issues/:id.json authenticated via OAuth should reject a tracker change without add_issues scope" do put( '/issues/1.json', :params => {:issue => {:tracker_id => 2}}, :headers => oauth_credentials('jsmith', 'view_issues edit_issues')) - assert_response :no_content + assert_response :unprocessable_content + json = ActiveSupport::JSON.decode(response.body) + assert_include 'Tracker is not included in the list', json['errors'] assert_equal 1, Issue.find(1).tracker_id end diff --git a/test/unit/issue_test.rb b/test/unit/issue_test.rb index e776367..3a96b79 100644 --- a/test/unit/issue_test.rb +++ b/test/unit/issue_test.rb @@ -847,6 +847,37 @@ class IssueTest < ActiveSupport::TestCase assert_not_equal [], issue.errors[:tracker_id] end + def test_safe_attributes_with_unallowed_tracker_should_add_an_error + project = Project.find(1) + project.trackers.delete(Tracker.find(2)) + + issue = Issue.new(:project => project, :author_id => 2, :subject => 'Unallowed tracker') + issue.send :safe_attributes=, {'tracker_id' => '2'}, User.find(2) + assert !issue.save + assert_equal ['is not included in the list'], issue.errors[:tracker_id] + end + + def test_safe_attributes_without_tracker_should_use_the_first_allowed_tracker + project = Project.find(1) + project.trackers.delete(Tracker.find(1)) + + issue = Issue.new(:project => project, :author_id => 2, :subject => 'No tracker') + issue.send :safe_attributes=, {'priority_id' => '5'}, User.find(2) + assert_equal 2, issue.tracker_id + assert issue.save + end + + def test_safe_attributes_with_allowed_tracker_after_unallowed_tracker_should_save + project = Project.find(1) + project.trackers.delete(Tracker.find(2)) + + issue = Issue.new(:project => project, :author_id => 2, :subject => 'Second try') + issue.send :safe_attributes=, {'tracker_id' => '2'}, User.find(2) + issue.send :safe_attributes=, {'tracker_id' => '3'}, User.find(2) + assert issue.save + assert_equal 3, issue.tracker_id + end + def test_category_based_assignment issue = Issue.create(:project_id => 1, :tracker_id => 1, :author_id => 3, :status_id => 1, :priority => IssuePriority.first, diff --git a/test/unit/mail_handler_test.rb b/test/unit/mail_handler_test.rb index cf552be..21c352b 100644 --- a/test/unit/mail_handler_test.rb +++ b/test/unit/mail_handler_test.rb @@ -226,6 +226,23 @@ class MailHandlerTest < ActiveSupport::TestCase assert issue.description.include?('Lorem ipsum dolor sit amet, consectetuer adipiscing elit.') end + def test_add_issue_with_unallowed_tracker_should_use_the_default_tracker + # This email contains: 'Tracker: Feature Request' + User.find_by_login('jsmith').roles_for_project(Project.find(2)).each do |role| + role.set_permission_trackers :add_issues, [3] + role.save! + end + + issue = + submit_email( + 'ticket_with_attributes.eml', + :allow_override => ['tracker'] + ) + assert issue.is_a?(Issue) + assert !issue.new_record? + assert_equal 'Support request', issue.reload.tracker.to_s + end + def test_add_issue_with_spaces_between_attribute_and_separator issue = submit_email( @@ -1007,6 +1024,25 @@ class MailHandlerTest < ActiveSupport::TestCase assert !journal.notes.match(/^Start Date:/i) end + def test_update_issue_with_unallowed_tracker_should_keep_the_tracker + User.find_by_login('jsmith').roles_for_project(Project.find(1)).each do |role| + role.set_permission_trackers :add_issues, [3] + role.save! + end + + journal = + submit_email( + 'ticket_reply_with_status.eml', + :allow_override => ['tracker', 'status'] + ) do |raw| + raw.sub!('Status: Resolved', "Status: Resolved\nTracker: Bug") + end + assert journal.is_a?(Journal) + issue = Issue.find(2) + assert_equal 'Feature request', issue.tracker.name + assert_equal IssueStatus.find_by_name("Resolved"), issue.status + end + def test_update_issue_with_private_keyword_no Role.find(1).add_permission! :set_issues_private Issue.find(2).update_columns(:is_private => true) -- 2.47.3