Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions extensions/BMO/Extension.pm
Original file line number Diff line number Diff line change
Expand Up @@ -2026,6 +2026,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,
Expand Down Expand Up @@ -2202,6 +2204,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);
Expand Down
41 changes: 37 additions & 4 deletions extensions/MozChangeField/Extension.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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,
);
Expand Down Expand Up @@ -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) = @_;

Expand All @@ -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;
Expand Down
11 changes: 7 additions & 4 deletions extensions/MozChangeField/lib/Post/TypePriSevEditbugs.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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});
}
}
Expand Down
45 changes: 45 additions & 0 deletions extensions/MozChangeField/lib/Pre/KeywordsCanconfirm.pm
Original file line number Diff line number Diff line change
@@ -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;
56 changes: 42 additions & 14 deletions extensions/MozChangeField/lib/Pre/TypePriSevEditbugs.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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;
154 changes: 154 additions & 0 deletions extensions/MozChangeField/t/bmo/create-restricted-fields.t
Original file line number Diff line number Diff line change
@@ -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;
2 changes: 2 additions & 0 deletions extensions/MozProjectReview/Extension.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading
Loading