Commit 58d5d171 authored by Markus Koller's avatar Markus Koller

Merge branch 'use_strong_params_for_compare' into 'master'

Use strong parameters for CompareController

See merge request gitlab-org/gitlab!80396
parents d6c6a9f7 557fcc3b
...@@ -28,6 +28,7 @@ class Projects::CompareController < Projects::ApplicationController ...@@ -28,6 +28,7 @@ class Projects::CompareController < Projects::ApplicationController
COMMIT_DIFFS_PER_PAGE = 20 COMMIT_DIFFS_PER_PAGE = 20
def index def index
compare_params
end end
def show def show
...@@ -44,9 +45,9 @@ class Projects::CompareController < Projects::ApplicationController ...@@ -44,9 +45,9 @@ class Projects::CompareController < Projects::ApplicationController
def create def create
from_to_vars = { from_to_vars = {
from: params[:from].presence, from: compare_params[:from].presence,
to: params[:to].presence, to: compare_params[:to].presence,
from_project_id: params[:from_project_id].presence from_project_id: compare_params[:from_project_id].presence
} }
if from_to_vars[:from].blank? || from_to_vars[:to].blank? if from_to_vars[:from].blank? || from_to_vars[:to].blank?
...@@ -87,10 +88,10 @@ class Projects::CompareController < Projects::ApplicationController ...@@ -87,10 +88,10 @@ class Projects::CompareController < Projects::ApplicationController
# target == start_ref == from # target == start_ref == from
def target_project def target_project
strong_memoize(:target_project) do strong_memoize(:target_project) do
next source_project unless params.key?(:from_project_id) next source_project unless compare_params.key?(:from_project_id)
next source_project if params[:from_project_id].to_i == source_project.id next source_project if compare_params[:from_project_id].to_i == source_project.id
target_project = target_projects(source_project).find_by_id(params[:from_project_id]) target_project = target_projects(source_project).find_by_id(compare_params[:from_project_id])
# Just ignore the field if it points at a non-existent or hidden project # Just ignore the field if it points at a non-existent or hidden project
next source_project unless target_project && can?(current_user, :download_code, target_project) next source_project unless target_project && can?(current_user, :download_code, target_project)
...@@ -111,13 +112,13 @@ class Projects::CompareController < Projects::ApplicationController ...@@ -111,13 +112,13 @@ class Projects::CompareController < Projects::ApplicationController
end end
def start_ref def start_ref
@start_ref ||= Addressable::URI.unescape(params[:from]) @start_ref ||= Addressable::URI.unescape(compare_params[:from])
end end
def head_ref def head_ref
return @ref if defined?(@ref) return @ref if defined?(@ref)
@ref = @head_ref = Addressable::URI.unescape(params[:to]) @ref = @head_ref = Addressable::URI.unescape(compare_params[:to])
end end
def define_commits def define_commits
...@@ -146,4 +147,8 @@ class Projects::CompareController < Projects::ApplicationController ...@@ -146,4 +147,8 @@ class Projects::CompareController < Projects::ApplicationController
.find_by(source_project: source_project, source_branch: head_ref, target_branch: start_ref) .find_by(source_project: source_project, source_branch: head_ref, target_branch: start_ref)
end end
# rubocop: enable CodeReuse/ActiveRecord # rubocop: enable CodeReuse/ActiveRecord
def compare_params
@compare_params ||= params.permit(:from, :to, :from_project_id)
end
end end
...@@ -13,4 +13,4 @@ ...@@ -13,4 +13,4 @@
= html_escape(_("Changes are shown as if the %{b_open}source%{b_close} revision was being merged into the %{b_open}target%{b_close} revision.")) % { b_open: '<b>'.html_safe, b_close: '</b>'.html_safe } = html_escape(_("Changes are shown as if the %{b_open}source%{b_close} revision was being merged into the %{b_open}target%{b_close} revision.")) % { b_open: '<b>'.html_safe, b_close: '</b>'.html_safe }
.prepend-top-20 .prepend-top-20
#js-compare-selector{ data: project_compare_selector_data(@project, @merge_request, params) } #js-compare-selector{ data: project_compare_selector_data(@project, @merge_request, @compare_params) }
...@@ -25,15 +25,25 @@ RSpec.describe Projects::CompareController do ...@@ -25,15 +25,25 @@ RSpec.describe Projects::CompareController do
end end
describe 'GET index' do describe 'GET index' do
let(:params) { { namespace_id: project.namespace, project_id: project } }
render_views render_views
before do before do
get :index, params: { namespace_id: project.namespace, project_id: project } get :index, params: params
end end
it 'returns successfully' do it 'returns successfully' do
expect(response).to be_successful expect(response).to be_successful
end end
context 'with incorrect parameters' do
let(:params) { super().merge(from: { invalid: :param }, to: { also: :invalid }) }
it 'returns successfully' do
expect(response).to be_successful
end
end
end end
describe 'GET show' do describe 'GET show' do
...@@ -346,6 +356,7 @@ RSpec.describe Projects::CompareController do ...@@ -346,6 +356,7 @@ RSpec.describe Projects::CompareController do
'' | '' | '1' | { from_project_id: 1 } '' | '' | '1' | { from_project_id: 1 }
'main' | '' | '1' | { from: 'main', from_project_id: 1 } 'main' | '' | '1' | { from: 'main', from_project_id: 1 }
'' | 'main' | '1' | { to: 'main', from_project_id: 1 } '' | 'main' | '1' | { to: 'main', from_project_id: 1 }
['a'] | ['b'] | ['c'] | {}
end end
with_them do with_them do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment