Skip to content

Commit

Permalink
Merge branch 'issue_25064' into 'security'
Browse files Browse the repository at this point in the history
Ensure state param has a valid value when filtering issuables.

Closes https://gitlab.com/gitlab-org/gitlab-ce/issues/25064

This fix makes sure we only call safe methods on issuable when filtering by state.

See merge request !2038
  • Loading branch information
Douwe Maan authored and rspeicher committed Dec 6, 2016
1 parent f0f514a commit 29ceb98
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 6 deletions.
13 changes: 8 additions & 5 deletions app/finders/issuable_finder.rb
Expand Up @@ -7,7 +7,7 @@
# current_user - which user use
# params:
# scope: 'created-by-me' or 'assigned-to-me' or 'all'
# state: 'open' or 'closed' or 'all'
# state: 'opened' or 'closed' or 'all'
# group_id: integer
# project_id: integer
# milestone_title: string
Expand Down Expand Up @@ -207,10 +207,13 @@ def by_scope(items)
end

def by_state(items)
params[:state] ||= 'all'

if items.respond_to?(params[:state])
items.public_send(params[:state])
case params[:state].to_s
when 'closed'
items.closed
when 'merged'
items.respond_to?(:merged) ? items.merged : items.closed
when 'opened'
items.opened
else
items
end
Expand Down
@@ -0,0 +1,4 @@
---
title: Validate state param when filtering issuables
merge_request:
author:
37 changes: 36 additions & 1 deletion spec/finders/issues_finder_spec.rb
Expand Up @@ -10,6 +10,7 @@
let(:issue1) { create(:issue, author: user, assignee: user, project: project1, milestone: milestone, title: 'gitlab') }
let(:issue2) { create(:issue, author: user, assignee: user, project: project2, description: 'gitlab') }
let(:issue3) { create(:issue, author: user2, assignee: user2, project: project2) }
let(:closed_issue) { create(:issue, author: user2, assignee: user2, project: project2, state: 'closed') }
let!(:label_link) { create(:label_link, label: label, target: issue2) }

before do
Expand All @@ -25,7 +26,7 @@
describe '#execute' do
let(:search_user) { user }
let(:params) { {} }
let(:issues) { IssuesFinder.new(search_user, params.merge(scope: scope, state: 'opened')).execute }
let(:issues) { IssuesFinder.new(search_user, params.reverse_merge(scope: scope, state: 'opened')).execute }

context 'scope: all' do
let(:scope) { 'all' }
Expand Down Expand Up @@ -143,6 +144,40 @@
end
end

context 'filtering by state' do
context 'with opened' do
let(:params) { { state: 'opened' } }

it 'returns only opened issues' do
expect(issues).to contain_exactly(issue1, issue2, issue3)
end
end

context 'with closed' do
let(:params) { { state: 'closed' } }

it 'returns only closed issues' do
expect(issues).to contain_exactly(closed_issue)
end
end

context 'with all' do
let(:params) { { state: 'all' } }

it 'returns all issues' do
expect(issues).to contain_exactly(issue1, issue2, issue3, closed_issue)
end
end

context 'with invalid state' do
let(:params) { { state: 'invalid_state' } }

it 'returns all issues' do
expect(issues).to contain_exactly(issue1, issue2, issue3, closed_issue)
end
end
end

context 'when the user is unauthorized' do
let(:search_user) { nil }

Expand Down

0 comments on commit 29ceb98

Please sign in to comment.