From 0703ad5d2472824fca46e947856194d6bfd6bc6d Mon Sep 17 00:00:00 2001 From: David Lawrence Date: Wed, 18 Nov 2026 16:52:45 -0500 Subject: [PATCH] Bug 2078325 --- extensions/BMO/Extension.pm | 4 + extensions/MozChangeField/Extension.pm | 41 ++++- .../lib/Post/TypePriSevEditbugs.pm | 11 +- .../lib/Pre/KeywordsCanconfirm.pm | 45 +++++ .../lib/Pre/TypePriSevEditbugs.pm | 56 +++++-- .../t/bmo/create-restricted-fields.t | 154 ++++++++++++++++++ extensions/MozProjectReview/Extension.pm | 2 + qa/t/rest_bug_create.t | 61 ++++++- qa/t/rest_bug_get.t | 11 +- 9 files changed, 358 insertions(+), 27 deletions(-) create mode 100644 extensions/MozChangeField/lib/Pre/KeywordsCanconfirm.pm create mode 100644 extensions/MozChangeField/t/bmo/create-restricted-fields.t diff --git a/extensions/BMO/Extension.pm b/extensions/BMO/Extension.pm index 5caafea963..6bb33a8af9 100644 --- a/extensions/BMO/Extension.pm +++ b/extensions/BMO/Extension.pm @@ -2023,6 +2023,8 @@ sub _post_employee_incident_bug { $vars, \$comment) || ThrowTemplateError($template->error()); + # Values below are set by this code, not the user (see MozChangeField). + local Bugzilla->request_cache->{mozchangefield_trusted_create} = 1; $ssh_key_bug = Bugzilla::Bug->create({ short_desc => 'Disable/Regenerate SSH Key', product => $bug->product, @@ -2199,6 +2201,8 @@ sub _file_child_bug { Bugzilla->template->process($full_template, $template_vars, \$comment) || ThrowTemplateError(Bugzilla->template->error()); $bug_data->{'comment'} = $comment; + # Values below are set by this code, not the user (see MozChangeField). + local Bugzilla->request_cache->{mozchangefield_trusted_create} = 1; if ($new_bug = Bugzilla::Bug->create($bug_data)) { my $set_all = {dependson => {add => [$new_bug->bug_id]}}; $parent_bug->set_all($set_all); diff --git a/extensions/MozChangeField/Extension.pm b/extensions/MozChangeField/Extension.pm index 860b0fdad7..36f7dcb0f3 100644 --- a/extensions/MozChangeField/Extension.pm +++ b/extensions/MozChangeField/Extension.pm @@ -20,6 +20,7 @@ use Bugzilla::Extension::MozChangeField::Pre::CanConfirm; use Bugzilla::Extension::MozChangeField::Pre::CommentClosedBugs; use Bugzilla::Extension::MozChangeField::Pre::CustomField; use Bugzilla::Extension::MozChangeField::Pre::Graveyard; +use Bugzilla::Extension::MozChangeField::Pre::KeywordsCanconfirm; use Bugzilla::Extension::MozChangeField::Pre::Reopen; use Bugzilla::Extension::MozChangeField::Pre::TypePriSevEditbugs; @@ -28,6 +29,7 @@ my @pre_instances = ( Bugzilla::Extension::MozChangeField::Pre::CommentClosedBugs->new, Bugzilla::Extension::MozChangeField::Pre::CustomField->new, Bugzilla::Extension::MozChangeField::Pre::Graveyard->new, + Bugzilla::Extension::MozChangeField::Pre::KeywordsCanconfirm->new, Bugzilla::Extension::MozChangeField::Pre::Reopen->new, Bugzilla::Extension::MozChangeField::Pre::TypePriSevEditbugs->new, ); @@ -66,6 +68,34 @@ sub bug_check_can_change_field { } } +# check_can_change_field() is only consulted when updating a bug, so the Pre +# rules above never run when filing one. Pre modules that implement +# sanitize_create() get a chance to reset field values the user isn't allowed +# to set, before the bug is inserted. This covers every entry path (REST API, +# post_bug.cgi, guided entry). +# +# Server-side code that files bugs on the user's behalf with values it chose +# itself (never taken from user input) can skip this by wrapping the create: +# +# local Bugzilla->request_cache->{mozchangefield_trusted_create} = 1; +# +# A plain string key is used so callers in other extensions need not depend on +# this one. Never set it on paths where params come from the request. +sub bug_end_of_create_validators { + my ($self, $args) = @_; + + return if Bugzilla->request_cache->{mozchangefield_trusted_create}; + + # The bug doesn't exist yet, so take the product from the validated params. + my $perms = {params => $args->{params}}; + _populate_permissions($perms, $args->{params}->{product_id}); + + foreach my $instance (@pre_instances) { + next if !$instance->can('sanitize_create'); + $instance->sanitize_create($perms); + } +} + sub bug_update_before_logging { my ($self, $args) = @_; @@ -89,13 +119,16 @@ sub bug_end_of_create { } sub _populate_permissions { - my ($args) = @_; + my ($args, $product_id) = @_; my $user = Bugzilla->user; + $product_id //= $args->{bug}->{'product_id'}; # Some modules need these permissions to function so only look up once - $args->{canconfirm} - = $user->in_group('canconfirm', $args->{bug}->{'product_id'}); - $args->{editbugs} = $user->in_group('editbugs', $args->{bug}->{'product_id'}); + $args->{canconfirm} = $user->in_group('canconfirm', $product_id); + $args->{editbugs} = $user->in_group('editbugs', $product_id); + + # can_triage is added to Bugzilla::User by the BMO extension. + $args->{can_triage} = $user->can('can_triage') && $user->can_triage; } __PACKAGE__->NAME; diff --git a/extensions/MozChangeField/lib/Post/TypePriSevEditbugs.pm b/extensions/MozChangeField/lib/Post/TypePriSevEditbugs.pm index 05ee9aeb51..b16d8a5792 100644 --- a/extensions/MozChangeField/lib/Post/TypePriSevEditbugs.pm +++ b/extensions/MozChangeField/lib/Post/TypePriSevEditbugs.pm @@ -11,15 +11,18 @@ use 5.10.1; use Moo; use Bugzilla::Constants; +use Bugzilla::Extension::MozChangeField::Pre::TypePriSevEditbugs; + +my $RULE = 'Bugzilla::Extension::MozChangeField::Pre::TypePriSevEditbugs'; sub evaluate_change { my ($self, $args) = @_; - my $changes = $args->{changes}; - my $editbugs = $args->{editbugs}; + my $changes = $args->{changes}; - # If changing the bug_type, severity, or priority, editbugs is required. + # Backstop for the Pre rule: type needs editbugs; severity and priority need + # editbugs or can_triage. foreach my $field (qw(bug_type bug_severity priority)) { - if (exists $changes->{$field} && !$editbugs) { + if (exists $changes->{$field} && !$RULE->can_set_field($field, $args)) { ThrowUserError('mozchangefield_field_needs_editbugs', {field => $field}); } } diff --git a/extensions/MozChangeField/lib/Pre/KeywordsCanconfirm.pm b/extensions/MozChangeField/lib/Pre/KeywordsCanconfirm.pm new file mode 100644 index 0000000000..7ba2927744 --- /dev/null +++ b/extensions/MozChangeField/lib/Pre/KeywordsCanconfirm.pm @@ -0,0 +1,45 @@ +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. +# +# This Source Code Form is "Incompatible With Secondary Licenses", as +# defined by the Mozilla Public License, v. 2.0. + +package Bugzilla::Extension::MozChangeField::Pre::KeywordsCanconfirm; + +use 5.10.1; +use Moo; + +use Bugzilla::Constants; + +# Keywords are a triage tool, so only users with 'canconfirm' or 'editbugs' +# may add or remove them. Without this the reporter could change them, as core +# grants reporters PRIVILEGES_REQUIRED_REPORTER. +sub _can_set_keywords { + my ($args) = @_; + return $args->{canconfirm} || $args->{editbugs}; +} + +sub evaluate_change { + my ($self, $args) = @_; + + return undef if $args->{'field'} ne 'keywords'; + return undef if _can_set_keywords($args); + + return { + result => PRIVILEGES_REQUIRED_EMPOWERED, + reason => 'You need "canconfirm" permissions to change keywords.', + }; +} + +# Silently drop keywords when a user without 'canconfirm' files a bug, the same +# way severity and priority are reset rather than rejected. +sub sanitize_create { + my ($self, $args) = @_; + return if _can_set_keywords($args); + + my $params = $args->{params}; + $params->{keywords} = [] if exists $params->{keywords}; +} + +1; diff --git a/extensions/MozChangeField/lib/Pre/TypePriSevEditbugs.pm b/extensions/MozChangeField/lib/Pre/TypePriSevEditbugs.pm index f63843264a..8dd6eff9d2 100644 --- a/extensions/MozChangeField/lib/Pre/TypePriSevEditbugs.pm +++ b/extensions/MozChangeField/lib/Pre/TypePriSevEditbugs.pm @@ -10,26 +10,54 @@ package Bugzilla::Extension::MozChangeField::Pre::TypePriSevEditbugs; use 5.10.1; use Moo; +use Bugzilla; use Bugzilla::Constants; +# Fields reset to their default when a user may not set them on a new bug. +# bug_type is deliberately absent: it is required on bug entry and +# check_can_change_field lets anyone set it when filing. +my %CREATE_DEFAULT_PARAM + = (bug_severity => 'defaultseverity', priority => 'defaultpriority'); + +# Who may set each field. Triagers need severity and priority to triage +# (BMO requires an S* severity on triaged defects), so can_triage is enough +# for those; bug_type stays editbugs only. +my %ALLOWED_BY = ( + bug_type => ['editbugs'], + bug_severity => ['editbugs', 'can_triage'], + priority => ['editbugs', 'can_triage'], +); + +# True unless $field is restricted and the user holds none of the permissions +# that allow it. $perms is the hash built by _populate_permissions. Also used +# by Post::TypePriSevEditbugs so update and create share one rule. +sub can_set_field { + my ($class, $field, $perms) = @_; + my $allowed_by = $ALLOWED_BY{$field} or return 1; + return scalar grep { $perms->{$_} } @$allowed_by; +} + sub evaluate_change { my ($self, $args) = @_; + my $field = $args->{'field'}; - my $field = $args->{'field'}; - my $editbugs = $args->{'editbugs'}; - - # The user needs to have 'editbugs' permission to change the type, severity - # and priority fields. - if (($field eq 'bug_type' || $field eq 'bug_severity' || $field eq 'priority') - && !$editbugs) - { - return { - result => PRIVILEGES_REQUIRED_EMPOWERED, - reason => 'You need "editbugs" permissions to change this field.', - }; - } + return undef if $self->can_set_field($field, $args); + + my $who = join(' or ', map {"\"$_\""} @{$ALLOWED_BY{$field}}); + return { + result => PRIVILEGES_REQUIRED_EMPOWERED, + reason => "You need $who permissions to change this field.", + }; +} + +sub sanitize_create { + my ($self, $args) = @_; + my $params = $args->{params}; - return undef; + foreach my $field (keys %CREATE_DEFAULT_PARAM) { + next if $self->can_set_field($field, $args); + $params->{$field} = Bugzilla->params->{$CREATE_DEFAULT_PARAM{$field}}; + } } 1; diff --git a/extensions/MozChangeField/t/bmo/create-restricted-fields.t b/extensions/MozChangeField/t/bmo/create-restricted-fields.t new file mode 100644 index 0000000000..d7f5e16800 --- /dev/null +++ b/extensions/MozChangeField/t/bmo/create-restricted-fields.t @@ -0,0 +1,154 @@ +#!/usr/bin/env perl +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. +# +# This Source Code Form is "Incompatible With Secondary Licenses", as +# defined by the Mozilla Public License, v. 2.0. + +# Users without editbugs must not be able to choose severity or priority when +# filing a bug, whichever entry path they use (REST API, post_bug.cgi). The +# values are silently reset to the configured defaults. Users without +# canconfirm likewise cannot add keywords when filing, nor change them later. +# Triagers (can_triage_bugs) may set severity and priority without editbugs, +# as triaging a defect needs an S* severity. Trusted server-side creates keep +# their values (Bug 2078325). + +use 5.10.1; +use strict; +use warnings; +use lib qw(. lib local/lib/perl5); +use Test::More; + +use Bugzilla; +use Bugzilla::Constants; +use Bugzilla::Bug; +use Bugzilla::Field::Choice; +use Bugzilla::Keyword; +use Bugzilla::Product; +use Bugzilla::User; +BEGIN { Bugzilla->extensions } + +Bugzilla->usage_mode(USAGE_MODE_TEST); +Bugzilla->error_mode(ERROR_MODE_DIE); + +my $default_sev = Bugzilla->params->{defaultseverity}; +my $default_pri = Bugzilla->params->{defaultpriority}; + +# Pick legal values that differ from the defaults so a reset is observable. +# Skip S1: Post::SeverityS1PriorityP1 rewrites the priority of S1 bugs. +sub non_default_value { + my ($field, $default) = @_; + my ($choice) + = grep { $_->is_active && $_->name ne $default && $_->name ne 'S1' } + Bugzilla::Field::Choice->type($field)->get_all; + return $choice ? $choice->name : undef; +} +my $wanted_sev = non_default_value('bug_severity', $default_sev); +my $wanted_pri = non_default_value('priority', $default_pri); +plan skip_all => 'Need a non-default severity and priority' + unless $wanted_sev && $wanted_pri; + +my ($keyword) = grep { $_->name ne 'triaged' } Bugzilla::Keyword->get_all; +plan skip_all => 'Need a keyword other than triaged' unless $keyword; + +my $admin = Bugzilla::User->check({id => 1}); +Bugzilla->set_user($admin); + +my ($product) + = grep { @{$_->components} && @{$_->versions} } Bugzilla::Product->get_all; +plan skip_all => 'Need a product with a component and a version' + unless $product; + +my $reporter = Bugzilla::User->create({ + login_name => "create-restricted-$$\@example.com", + realname => 'Restricted Reporter', + cryptpassword => '*', +}); +ok( + !$reporter->in_group('editbugs', $product->id), + 'Test reporter does not have editbugs' +); + +# A triager with canconfirm but no editbugs: can_triage_bugs is a standalone +# group, so this combination exists. +my $triager = Bugzilla::User->create({ + login_name => "create-restricted-triager-$$\@example.com", + realname => 'Triager Without Editbugs', + cryptpassword => '*', +}); +$triager->set_groups({add => ['can_triage_bugs', 'canconfirm']}); +$triager->update(); +$triager = Bugzilla::User->new({id => $triager->id}); +ok($triager->can_triage, 'Test triager can triage'); +ok( + !$triager->in_group('editbugs', $product->id), + 'Test triager does not have editbugs' +); + +sub file_bug { + my ($user, %opts) = @_; + Bugzilla->set_user($user); + local Bugzilla->request_cache->{mozchangefield_trusted_create} = 1 + if $opts{trusted}; + my $bug = Bugzilla::Bug->create({ + short_desc => "Restricted field test - $$", + product => $product->name, + component => $product->components->[0]->name, + version => $product->versions->[0]->name, + bug_type => 'defect', + op_sys => 'Unspecified', + rep_platform => 'Unspecified', + bug_severity => $wanted_sev, + priority => $wanted_pri, + keywords => [$keyword->name], + comment => 'test', + }); + return Bugzilla::Bug->new($bug->id); +} + +{ + my $bug = file_bug($reporter); + is($bug->bug_severity, $default_sev, + 'Severity reset to default for a reporter without editbugs'); + is($bug->priority, $default_pri, + 'Priority reset to default for a reporter without editbugs'); + is($bug->bug_type, 'defect', 'Reporter can still choose the bug type'); + ok(!$bug->has_keyword($keyword->name), + 'Keywords dropped for a reporter without canconfirm'); + + # Core lets the reporter edit keywords; MozChangeField must not. + Bugzilla->set_user($reporter); + ok(!eval { $bug->modify_keywords($keyword->name, 'add'); 1 }, + 'Reporter without canconfirm cannot add keywords afterwards'); +} + +{ + my $bug = file_bug($admin); + is($bug->bug_severity, $wanted_sev, 'Severity kept for an editbugs user'); + is($bug->priority, $wanted_pri, 'Priority kept for an editbugs user'); + ok($bug->has_keyword($keyword->name), 'Keywords kept for an editbugs user'); +} + +{ + my $bug = file_bug($triager); + is($bug->bug_severity, $wanted_sev, 'Severity kept for a triager'); + is($bug->priority, $wanted_pri, 'Priority kept for a triager'); + ok($bug->has_keyword($keyword->name), 'Keywords kept for a triager'); + + Bugzilla->set_user($triager); + ok(eval { $bug->set_severity($default_sev); 1 }, + 'Triager can change severity afterwards') + or diag $@; + ok(!eval { $bug->set_type('task'); 1 }, + 'Triager without editbugs still cannot change the bug type'); +} + +{ + my $bug = file_bug($reporter, trusted => 1); + is($bug->bug_severity, $wanted_sev, 'Severity kept for a trusted create'); + is($bug->priority, $wanted_pri, 'Priority kept for a trusted create'); + ok($bug->has_keyword($keyword->name), 'Keywords kept for a trusted create'); +} + +done_testing; diff --git a/extensions/MozProjectReview/Extension.pm b/extensions/MozProjectReview/Extension.pm index f651a6d991..22fb921541 100644 --- a/extensions/MozProjectReview/Extension.pm +++ b/extensions/MozProjectReview/Extension.pm @@ -144,6 +144,8 @@ sub _file_child_bug { Bugzilla->template->process($full_template, $template_vars, \$comment) || ThrowTemplateError(Bugzilla->template->error()); $bug_data->{'comment'} = $comment; + # Values below are set by this code, not the user (see MozChangeField). + local Bugzilla->request_cache->{mozchangefield_trusted_create} = 1; if ($new_bug = Bugzilla::Bug->create($bug_data)) { my $set_all = {dependson => {add => [$new_bug->bug_id]}}; $parent_bug->set_all($set_all); diff --git a/qa/t/rest_bug_create.t b/qa/t/rest_bug_create.t index 6f70a206ea..7aef5f6132 100644 --- a/qa/t/rest_bug_create.t +++ b/qa/t/rest_bug_create.t @@ -186,7 +186,10 @@ my @tests = ( qa_contact => $config->{PRIVATE_BUG_USER . '_user_login'}, status => 'UNCONFIRMED' }, - test => "Authorized user can file a bug against a group", + + # PRIVATE_BUG_USER has no editbugs, so severity is reset (Bug 2078325). + expect => {severity => Bugzilla->params->{defaultseverity}}, + test => "Authorized user can file a bug against a group", }, { user => PRIVATE_BUG_USER, @@ -197,7 +200,8 @@ my @tests = ( qa_contact => '', status => 'UNCONFIRMED' }, - test => "Insider can create a private description" + expect => {severity => Bugzilla->params->{defaultseverity}}, + test => "Insider can create a private description" }, { user => 'editbugs', @@ -234,6 +238,7 @@ sub post_success { my $bug = $t->tx->res->json->{bugs}->[0]; my $expect = dclone $test->{args}; + %$expect = (%$expect, %{$test->{expect} || {}}); my $comment_is_private = delete $expect->{comment_is_private}; $expect->{creator} = $config->{$test->{user} . '_user_login'}; @@ -266,4 +271,56 @@ foreach my $test (@tests) { } } +# Severity and priority need editbugs, also when filing a bug (Bug 2078325). +# Without it the requested values are silently reset to the defaults +# (defaultseverity / defaultpriority), so API clients keep working. canconfirm +# is not enough. Requested values differ from the QA defaults ('--' / 'Highest') +# so a reset is observable. +my %restricted_create = ( + product => 'Another Product', + component => 'c1', + version => 'unspecified', + summary => 'Restricted severity/priority on create', + description => 'Filed by rest_bug_create.t', + type => 'defect', + op_sys => 'Linux', + platform => 'PC', + severity => 'critical', + priority => 'High', +); +my $default_severity = Bugzilla->params->{defaultseverity}; +my $default_priority = Bugzilla->params->{defaultpriority}; + +my @restricted_tests = ( + { + user => 'unprivileged', + severity => $default_severity, + priority => $default_priority + }, + { + user => 'canconfirm', + severity => $default_severity, + priority => $default_priority + }, + {user => 'editbugs', severity => 'critical', priority => 'High'}, +); + +foreach my $test (@restricted_tests) { + my $user = $test->{user}; + my $headers = api_headers($config->{"${user}_user_api_key"}); + + $t->post_ok($url . 'rest/bug' => $headers => json => \%restricted_create) + ->status_is(200); + my $id = $t->tx->res->json->{id}; + ok($id, "$user: bug created with id " . ($id // 'undef')); + + $t->get_ok($url . "rest/bug/$id" => $headers)->status_is(200)->json_is( + '/bugs/0/severity' => $test->{severity}, + "$user: severity is $test->{severity}" + )->json_is( + '/bugs/0/priority' => $test->{priority}, + "$user: priority is $test->{priority}" + )->json_is('/bugs/0/type' => 'defect', "$user: type is kept"); +} + done_testing(); diff --git a/qa/t/rest_bug_get.t b/qa/t/rest_bug_get.t index 7416c1d885..bcfb781c56 100644 --- a/qa/t/rest_bug_get.t +++ b/qa/t/rest_bug_get.t @@ -46,7 +46,6 @@ $t->put_ok($url . "rest/bug/$private_id" => blocks => {set => [$public_id]}, dupe_of => $public_id, is_creator_accessible => 0, - keywords => {set => ['test-keyword-1', 'test-keyword-2']}, see_also => { add => [ "${base_url}show_bug.cgi?id=$public_id", @@ -64,7 +63,9 @@ $private_bug->{is_open} = 0; $private_bug->{resolution} = 'DUPLICATE'; $private_bug->{is_creator_accessible} = 0; $private_bug->{is_cc_accessible} = 1; -$private_bug->{keywords} = ['test-keyword-1', 'test-keyword-2']; +# PRIVATE_BUG_USER has no canconfirm, so cannot set keywords (Bug 2078325). +# They are set on the public bug by admin below instead. +$private_bug->{keywords} = []; $private_bug->{see_also} = [ "${base_url}show_bug.cgi?id=$public_id", "https://bugzilla-dev.allizom.org/show_bug.cgi?id=123456" @@ -77,7 +78,7 @@ $public_bug->{resolution} = ''; $public_bug->{is_open} = 1; $public_bug->{is_creator_accessible} = 1; $public_bug->{is_cc_accessible} = 1; -$public_bug->{keywords} = []; +$public_bug->{keywords} = ['test-keyword-1', 'test-keyword-2']; # Local Bugzilla bugs are automatically updated. $public_bug->{cf_qa_status} = []; @@ -87,6 +88,7 @@ $public_bug->{cf_single_select} = '---'; $t->put_ok($url . "rest/bug/$public_id" => {'X-Bugzilla-API-Key' => $config->{admin_user_api_key}} => json => { deadline => '2038-01-01', + keywords => {set => ['test-keyword-1', 'test-keyword-2']}, estimated_time => '10.0', remaining_time => '5.0', })->status_is(200); @@ -101,6 +103,9 @@ $public_bug->{groups} = []; # or qa_contact, so they differ from normal $public_bug values. $private_bug->{qa_contact} = $config->{PRIVATE_BUG_USER . '_user_login'}; +# PRIVATE_BUG_USER has no editbugs, so severity is reset (Bug 2078325). +$private_bug->{severity} = Bugzilla->params->{defaultseverity}; + sub check_bug { my ($test, $json) = @_;