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
13 changes: 9 additions & 4 deletions Bugzilla/Bug.pm
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,10 @@ sub possible_duplicates {
my $product_ids = join(',', map { $_->id } @$products);
my $product_sql = $product_ids ? "AND product_id IN ($product_ids)" : "";

# Exclude bugs the user cannot see *before* ranking, so restricted bugs
# can't take result slots and leak summary matches.
my $security_sql = 'AND ' . $user->visible_bugs_sql('bugs');

# Because we collapse duplicates, we want to get slightly more bugs
# than were actually asked for.
my $sql_limit = $limit + 5;
Expand All @@ -660,7 +664,7 @@ sub possible_duplicates {
($relevance_sql) AS relevance
FROM bugs
INNER JOIN bugs_fulltext ON bugs.bug_id = bugs_fulltext.bug_id
WHERE ($where_sql) $product_sql
WHERE ($where_sql) $product_sql $security_sql
ORDER BY relevance DESC, bug_id DESC " . $dbh->sql_limit($sql_limit),
{Slice => {}}
);
Expand All @@ -676,11 +680,12 @@ sub possible_duplicates {
push(@actual_dupe_ids, $push_id);
}
@actual_dupe_ids = uniq @actual_dupe_ids;
if (scalar @actual_dupe_ids > $limit) {
@actual_dupe_ids = @actual_dupe_ids[0 .. ($limit - 1)];
}

# Duplicate targets may still be restricted, so filter before truncating.
my $visible = $user->visible_bugs(\@actual_dupe_ids);
if (scalar @$visible > $limit) {
$visible = [@$visible[0 .. ($limit - 1)]];
}
return $class->new_from_list($visible);
}

Expand Down
49 changes: 49 additions & 0 deletions Bugzilla/User.pm
Original file line number Diff line number Diff line change
Expand Up @@ -1666,6 +1666,48 @@ sub visible_bugs {
return [grep { $visible_cache->{blessed $_ ? $_->id : $_} } @$bugs];
}

# SQL equivalent of visible_bugs(), for filtering inside a query (e.g. before
# a LIMIT) rather than afterwards. Keep the two in sync.
sub visible_bugs_sql {
my ($self, $alias) = @_;
$alias ||= 'bugs';
my $user_id = $self->id;

# Public bugs, or bugs whose groups the user is all a member of.
my @terms = (
"NOT EXISTS (SELECT 1 FROM bug_group_map
WHERE bug_group_map.bug_id = $alias.bug_id
AND NOT ("
. $self->groups_in_sql('bug_group_map.group_id') . '))'
);

# Authenticated; users directly associated with the bug have visibility.
if ($user_id) {
push @terms,
"$alias.reporter_accessible = 1 AND $alias.reporter = $user_id",
"$alias.assigned_to = $user_id",
"$alias.cclist_accessible = 1
AND EXISTS (SELECT 1 FROM cc
WHERE cc.bug_id = $alias.bug_id AND cc.who = $user_id)";
if (Bugzilla->params->{'useqacontact'}) {
push @terms, "$alias.qa_contact = $user_id";
}

# Triage owners can see all bugs in their component, but only if they are
# also a member of the mozilla-employee-confidential group.
if ($self->is_employee_confidential) {
push @terms,
"EXISTS (SELECT 1 FROM components
WHERE components.id = $alias.component_id
AND components.triage_owner_id = $user_id)";
}
}

# Like visible_bugs(), never treat unfinished bugs as visible.
return "($alias.creation_ts IS NOT NULL AND (("
. join(') OR (', @terms) . ')))';
}

sub clear_product_cache {
my $self = shift;
delete $self->{enterable_products};
Expand Down Expand Up @@ -3425,6 +3467,13 @@ at all.

Determines if the user can see the specified bug.

=item C<visible_bugs_sql($alias)>

Returns an SQL boolean expression that is true for rows of the C<bugs> table
(aliased as C<$alias>, default C<bugs>) that the user can see. It applies the
same rules as C<visible_bugs>, so callers can filter restricted bugs out
before ranking or limiting results in SQL.

=item C<can_see_product(product_name)>

Returns 1 if the user can access the specified product, and 0 if the user
Expand Down
105 changes: 105 additions & 0 deletions qa/t/rest_bug_possible_duplicates.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
#!/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.

#####################################################
# Test for REST call to Bug.possible_duplicates() #
# GET /rest/bug/possible_duplicates #
#####################################################

use 5.10.1;
use strict;
use warnings;
use lib qw(lib ../../lib ../../local/lib/perl5);

use Bugzilla;
use Mojo::URL;
use QA::Util qw(get_config random_string);
use QA::Tests qw(create_bug_fields PRIVATE_BUG_USER);
use QA::REST::Util qw(api_headers);

use Test::Mojo;
use Test::More;

my $config = get_config();
my $url = Bugzilla->localconfig->urlbase;

my $t = Test::Mojo->new();

# Unique words so the full-text search only matches the bugs created here.
my $public_word = lc random_string(16);
my $secret_word = lc random_string(16);

sub create_bug {
my ($api_key, %fields) = @_;
my $bug = create_bug_fields($config);
%$bug = (%$bug, %fields);
$t->post_ok($url . 'rest/bug' => api_headers($api_key) => json => $bug)
->status_is(200);
return $t->tx->res->json->{id};
}

my $public_id = create_bug(
$config->{editbugs_user_api_key},
summary => "possible duplicates $public_word",
);
my $private_id = create_bug(
$config->{PRIVATE_BUG_USER . '_user_api_key'},
summary => "possible duplicates $secret_word",
product => 'QA-Selenium-TEST',
component => 'QA-Selenium-TEST',
target_milestone => 'QAMilestone',
version => 'QAVersion',
creator => $config->{PRIVATE_BUG_USER . '_user_login'},

# CC a user outside the bug's groups; involvement alone grants visibility.
cc => [$config->{unprivileged_user_login}],
cclist_accessible => 1,
);

sub dupe_ids {
my ($summary, $api_key) = @_;
my $req = Mojo::URL->new($url . 'rest/bug/possible_duplicates')
->query(summary => $summary, limit => 1, include_fields => 'id');
$t->get_ok($req => api_headers($api_key))->status_is(200);
return [map { $_->{id} } @{$t->tx->res->json->{bugs}}];
}

# The private bug ranks first for its own secret word. Make sure the user
# who can see it gets it back, so the anonymous checks below are meaningful.
is_deeply(
dupe_ids("$secret_word $public_word",
$config->{PRIVATE_BUG_USER . '_user_api_key'}),
[$private_id],
'Privileged user gets the top-ranked private bug'
);

# A user who sees the private bug only through involvement (CC), not group
# membership, must not have it filtered out before the limit is applied.
is_deeply(
dupe_ids("$secret_word $public_word", $config->{unprivileged_user_api_key}),
[$private_id],
'CC-only user gets the top-ranked private bug they can see'
);

# Anonymous users must get the same answer whether or not the probe matches
# a hidden summary; an empty result would leak the private summary's words.
is_deeply(dupe_ids("$secret_word $public_word"),
[$public_id], 'Matching a private summary does not consume the result slot');
is_deeply(
dupe_ids(lc(random_string(16)) . " $public_word"),
[$public_id], 'Non-matching probe returns the public bug'
);

# MySQL boolean-mode operators must not reopen the oracle.
my $prefix = substr($secret_word, 0, 4);
is_deeply(dupe_ids(qq{$prefix*"zz"q zz"q"<$public_word}),
[$public_id], 'Boolean-mode prefix probe does not leak private summary');

is_deeply(dupe_ids($secret_word), [], 'Private bug is never returned');

done_testing();
Loading