Project

General

Profile

Actions

Defect #44721

open

Creating an issue with a tracker_id that is not available in the project silently uses another tracker (201 Created instead of 422)

Defect #44721: Creating an issue with a tracker_id that is not available in the project silently uses another tracker (201 Created instead of 422)

Added by Jan from Planio www.plan.io about 1 month ago. Updated 7 minutes ago.

Status:
Needs feedback
Priority:
Normal
Category:
REST API
Resolution:
Affected version:

Description

Steps to reproduce

Project foo has the trackers Feature (2) and Support (3) enabled, Bug (1) is not enabled (or the user's role may not add issues with it). Create an issue through the REST API with the tracker that is not available:

curl -i -H "Content-Type: application/json" -H "X-Redmine-API-Key: $KEY" \
  -d '{"issue":{"project_id":"foo","subject":"test","tracker_id":1}}' \
  https://redmine.example.com/issues.json

Actual result

HTTP/1.1 201 Created. The issue exists with tracker Feature (2), the project's first available tracker. The response body shows the substituted tracker, but there is no error, no warning and no hint that the request was not applied as sent. A client that trusts the 201 does not notice. The same happens in the web UI when a form posts a tracker that is not allowed (see #37007), and PUT /issues/:id with such a tracker_id answers 200 and keeps the old tracker.

Expected result

HTTP/1.1 422 Unprocessable Content with {"errors":["Tracker is not included in the list"]}, the same error the model raises when an existing issue is moved to a tracker that is disabled in the project. A value the client sent explicitly should either be applied or refused, never replaced.

Analysis

In Issue#safe_attributes= (source:trunk/app/models/issue.rb#L602) a tracker_id that is not in allowed_target_trackers(user) is dropped without a trace:

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
  end
end
if project && tracker.nil?
  # Set a default tracker to accept custom field values
  # even if tracker is not specified
  allowed_trackers = allowed_target_trackers(user)
  ...
  self.tracker ||= allowed_trackers.first
end

On a new record tracker is still nil after that, so the fallback that was added in r14083 for #19368 (to accept custom field values when no tracker_id is given) assigns allowed_trackers.first. The check in validate_issue ("Checks that the issue can not be added/moved to a disabled tracker", source:trunk/app/models/issue.rb#L800) never fires, because the disabled tracker was never assigned. The fallback cannot tell "no tracker_id in the request" from "a tracker_id that is not allowed"; the first case is what #19368 wanted, the second should be an error.

allowed_target_trackers also covers the role's per-tracker add_issues permission, so a tracker that is enabled in the project but not permitted for the role is substituted the same way.

Proposed fix

Remember a tracker_id that was sent but not allowed and refuse the record in validation; the fallback for a request without tracker_id stays as it is:

# app/models/issue.rb, safe_attributes=
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
  elsif t.present?
    @unallowed_tracker_id = t
  end
end

# validate_issue
if @unallowed_tracker_id.present?
  errors.add :tracker_id, :inclusion
end

Test for test/integration/api_test/issues_test.rb:

test "POST /issues.json with a tracker that is not enabled in the project should return 422" do
  Project.find(1).update!(:trackers => [Tracker.find(1)])   # Bug only
  assert_no_difference('Issue.count') do
    post '/issues.json',
      :params => {:issue => {:project_id => 1, :tracker_id => 2, :subject => 'API'}},
      :headers => credentials('jsmith')
  end
  assert_response 422
  json = ActiveSupport::JSON.decode(response.body)
  assert_include "Tracker is not included in the list", json['errors']
end

A patch can follow if this direction is agreed.


Files


Related issues 4 (3 open — 1 closed)

Related to Redmine - Defect #31028: Get 200 response even when specifying an id value that does not exist.NewActions
Related to Redmine - Defect #13267: No error message for invalid field valuesNewActions
Related to Redmine - Defect #37007: Switching project in "New Issue" shows wrong fields and uses disallowed default trackerNewActions
Related to Redmine - Defect #19368: Creating an issue without tracker_id attribute ignores custom field valuesClosedJean-Philippe LangActions

Updated by Jan from Planio www.plan.io about 1 month ago Actions #1

  • Related to Defect #31028: Get 200 response even when specifying an id value that does not exist. added

Updated by Jan from Planio www.plan.io about 1 month ago Actions #2

  • Related to Defect #13267: No error message for invalid field values added

Updated by Jan from Planio www.plan.io about 1 month ago Actions #3

  • Related to Defect #37007: Switching project in "New Issue" shows wrong fields and uses disallowed default tracker added

Updated by Jan from Planio www.plan.io about 1 month ago Actions #4

  • Related to Defect #19368: Creating an issue without tracker_id attribute ignores custom field values added

Updated by Marius BĂLTEANU about 3 hours ago · Edited Actions #5

Thanks for posting the issue. I think we should fix this, I can review the patch.

Updated by Jan from Planio www.plan.io 7 minutes ago Actions #6

Here is the patch. It is against the current trunk.

What it does

  • Issue#safe_attributes= remembers a tracker_id that the user cannot use in the project.
  • validate_issue adds an :inclusion error for it. The REST API answers 422 for POST /issues and for PUT /issues/:id.
  • A request without tracker_id still gets the first allowed tracker (#19368).

The model already does the same for the parent issue: safe_attributes= sets @invalid_parent_issue_id, and the validation reports it.

Behaviour changes

Two existing tests asked for the old behaviour. The patch changes both:

  • test_create_should_ignore_unallowed_trackers in IssuesControllerTest: the form now shows the error. Before, the issue got another tracker.
  • The OAuth test for PUT /issues/:id.json with the edit_issues scope and without the add_issues scope: the API now answers 422. Before, it answered 204 and kept the tracker.

Incoming email keeps the old behaviour

The user of the form or of the API sees the error and can submit again. The sender of an email cannot. Redmine sends no error message for a rejected email. With rdm-mailhandler.rb the MTA can return a bounce, but its text does not name the reason. With IMAP and POP3 the sender gets nothing.

I tested what MailHandler does today with a wrong value in each keyword (new issue, all keywords allowed):

Keyword in the email Result
Category that does not exist in the project Issue created, no category
Version that does not exist Issue created, no version
Status that does not exist Issue created, default status
Status that the workflow does not allow Issue created, default status
Priority that does not exist Issue created, default priority
Assignee unknown or not a member Issue created, no assignee
Tracker that does not exist in the project Issue created, default tracker
Version is closed Email rejected
Parent issue does not exist Email rejected
Estimated time is not a number Email rejected
Due date before start date Email rejected

The pattern:

  • MailHandler looks up the value of a list by name among the valid values only. A value that it does not find, or that is not allowed, is dropped. The issue is created without it.
  • The email is rejected only if a value is found and the validation of the issue fails afterwards.
  • test_add_issue_with_invalid_attributes in MailHandlerTest asks for the first behaviour, so it is intended.

The closest case to the tracker is the status that the workflow does not allow: the issue gets the default status. Without an exception, a tracker that the sender cannot use would be the only list value that makes the email fail. So the patch keeps the tracker in line with status, category, priority and assignee:

  • New issue: if the sender cannot use the tracker from the Tracker keyword, the issue gets the first allowed tracker.
  • Reply: if the sender cannot use the tracker from the keyword, the issue keeps its tracker. The other changes of the reply are applied.
  • With no_permission_check=1, a new issue gets the tracker from the keyword as before.

MailHandler removes the tracker from the attributes in these cases. Therefore, this patch also includes two new tests in MailHandlerTest to cover it and manifest the current behaviour as intentional. Both tests pass without the patch, too.

The four rejections in the table are a different topic. I did not change them.

Not in this patch

The REST API has two more attributes with the same silent behaviour. I tested both:

  • status_id: an unknown status is ignored. POST answers 201, PUT answers 204. A status that the workflow does not allow is ignored on PUT, too. For POST I only read this in the code.
  • project_id on PUT: an unknown project, or one that the user cannot move the issue to, is ignored. The answer is 204.

All other core attributes that I tested answer 422: priority, category, version, assignee, parent issue, done ratio, dates, estimated time. I left status and project out to keep this patch small. But we might want to open issues for them, too and create similar patches to this one, so we have consistent behaviour across the API.

Tests

I ran the unit, functional and integration tests on SQLite with and without the patch. The patch adds no failure. I did not run the system tests.

Actions

Also available in: PDF Atom