Commit 711b58c5 authored by Tetiana Chupryna's avatar Tetiana Chupryna

Merge branch '335077-snippets-can-be-accessed-from-another-project' into 'master'

Prevent project snippets from being accessed from non-canonical URLs

See merge request gitlab-org/gitlab!66282
parents 259dfb98 2671a975
...@@ -9,7 +9,7 @@ module FindSnippet ...@@ -9,7 +9,7 @@ module FindSnippet
# rubocop:disable CodeReuse/ActiveRecord # rubocop:disable CodeReuse/ActiveRecord
def snippet def snippet
strong_memoize(:snippet) do strong_memoize(:snippet) do
snippet_klass.inc_relations_for_view.find_by(id: snippet_id) snippet_klass.inc_relations_for_view.find_by(snippet_find_params)
end end
end end
# rubocop:enable CodeReuse/ActiveRecord # rubocop:enable CodeReuse/ActiveRecord
...@@ -21,4 +21,8 @@ module FindSnippet ...@@ -21,4 +21,8 @@ module FindSnippet
def snippet_id def snippet_id
params[:id] params[:id]
end end
def snippet_find_params
{ id: snippet_id }
end
end end
# frozen_string_literal: true # frozen_string_literal: true
class Projects::SnippetsController < Projects::Snippets::ApplicationController class Projects::SnippetsController < Projects::Snippets::ApplicationController
extend ::Gitlab::Utils::Override
include SnippetsActions include SnippetsActions
include ToggleAwardEmoji include ToggleAwardEmoji
include SpammableActions include SpammableActions
...@@ -45,4 +46,9 @@ class Projects::SnippetsController < Projects::Snippets::ApplicationController ...@@ -45,4 +46,9 @@ class Projects::SnippetsController < Projects::Snippets::ApplicationController
def spammable_path def spammable_path
project_snippet_path(@project, @snippet) project_snippet_path(@project, @snippet)
end end
override :snippet_find_params
def snippet_find_params
super.merge(project_id: project.id)
end
end end
...@@ -181,6 +181,24 @@ RSpec.describe Projects::SnippetsController do ...@@ -181,6 +181,24 @@ RSpec.describe Projects::SnippetsController do
end end
end end
end end
context 'when the project snippet is public' do
let_it_be(:project_snippet_public) { create(:project_snippet, :public, :repository, project: project, author: user) }
context 'when attempting to access from a different project route' do
subject { get action, params: { namespace_id: project.namespace, project_id: 42, id: project_snippet_public.to_param } }
before do
sign_in(user)
end
it 'responds with status 404' do
subject
expect(response).to have_gitlab_http_status(:not_found)
end
end
end
end end
end end
......
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