2019-07-31 22:56:46 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-09-25 12:07:36 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
RSpec.describe Projects::RawController do
|
2019-10-12 21:52:04 +05:30
|
|
|
include RepoHelpers
|
|
|
|
|
2021-02-22 17:27:13 +05:30
|
|
|
let_it_be(:project) { create(:project, :public, :repository) }
|
2020-04-22 19:07:51 +05:30
|
|
|
let(:inline) { nil }
|
2018-11-20 20:47:30 +05:30
|
|
|
|
|
|
|
describe 'GET #show' do
|
2021-02-22 17:27:13 +05:30
|
|
|
def get_show
|
2018-11-20 20:47:30 +05:30
|
|
|
get(:show,
|
2019-02-15 15:39:39 +05:30
|
|
|
params: {
|
|
|
|
namespace_id: project.namespace,
|
|
|
|
project_id: project,
|
2020-04-22 19:07:51 +05:30
|
|
|
id: filepath,
|
|
|
|
inline: inline
|
2019-02-15 15:39:39 +05:30
|
|
|
})
|
2018-11-20 20:47:30 +05:30
|
|
|
end
|
2015-09-25 12:07:36 +05:30
|
|
|
|
2021-02-22 17:27:13 +05:30
|
|
|
subject { get_show }
|
|
|
|
|
|
|
|
shared_examples 'single Gitaly request' do
|
|
|
|
it 'makes a single Gitaly request', :request_store, :clean_gitlab_redis_cache do
|
|
|
|
# Warm up to populate repository cache
|
|
|
|
get_show
|
|
|
|
RequestStore.clear!
|
|
|
|
|
|
|
|
expect { get_show }.to change { Gitlab::GitalyClient.get_request_count }.by(1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-09-25 12:07:36 +05:30
|
|
|
context 'regular filename' do
|
2018-11-20 20:47:30 +05:30
|
|
|
let(:filepath) { 'master/README.md' }
|
2015-09-25 12:07:36 +05:30
|
|
|
|
2019-01-03 12:48:30 +05:30
|
|
|
it 'delivers ASCII file' do
|
|
|
|
subject
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2019-01-03 12:48:30 +05:30
|
|
|
expect(response.header['Content-Type']).to eq('text/plain; charset=utf-8')
|
2020-04-22 19:07:51 +05:30
|
|
|
expect(response.header[Gitlab::Workhorse::DETECT_HEADER]).to eq 'true'
|
2019-01-03 12:48:30 +05:30
|
|
|
expect(response.header[Gitlab::Workhorse::SEND_DATA_HEADER]).to start_with('git-blob:')
|
2015-09-25 12:07:36 +05:30
|
|
|
end
|
2020-04-22 19:07:51 +05:30
|
|
|
|
|
|
|
it_behaves_like 'project cache control headers'
|
|
|
|
it_behaves_like 'content disposition headers'
|
2021-02-22 17:27:13 +05:30
|
|
|
include_examples 'single Gitaly request'
|
2015-09-25 12:07:36 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'image header' do
|
2018-11-20 20:47:30 +05:30
|
|
|
let(:filepath) { 'master/files/images/6049019_460s.jpg' }
|
2015-09-25 12:07:36 +05:30
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
it 'leaves image content disposition' do
|
2019-01-03 12:48:30 +05:30
|
|
|
subject
|
2015-09-25 12:07:36 +05:30
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2019-02-15 15:39:39 +05:30
|
|
|
expect(response.header[Gitlab::Workhorse::DETECT_HEADER]).to eq "true"
|
2019-01-03 12:48:30 +05:30
|
|
|
expect(response.header[Gitlab::Workhorse::SEND_DATA_HEADER]).to start_with('git-blob:')
|
2015-09-25 12:07:36 +05:30
|
|
|
end
|
2020-04-22 19:07:51 +05:30
|
|
|
|
|
|
|
it_behaves_like 'project cache control headers'
|
|
|
|
it_behaves_like 'content disposition headers'
|
2021-02-22 17:27:13 +05:30
|
|
|
include_examples 'single Gitaly request'
|
2015-09-25 12:07:36 +05:30
|
|
|
end
|
2015-12-23 02:04:40 +05:30
|
|
|
|
2020-04-22 19:07:51 +05:30
|
|
|
context 'with LFS files' do
|
2018-11-20 20:47:30 +05:30
|
|
|
let(:filename) { 'lfs_object.iso' }
|
|
|
|
let(:filepath) { "be93687/files/lfs/#{filename}" }
|
2020-04-22 19:07:51 +05:30
|
|
|
|
|
|
|
it_behaves_like 'a controller that can serve LFS files'
|
|
|
|
it_behaves_like 'project cache control headers'
|
2021-02-22 17:27:13 +05:30
|
|
|
include_examples 'single Gitaly request'
|
2015-12-23 02:04:40 +05:30
|
|
|
end
|
2019-10-12 21:52:04 +05:30
|
|
|
|
|
|
|
context 'when the endpoint receives requests above the limit', :clean_gitlab_redis_cache do
|
|
|
|
let(:file_path) { 'master/README.md' }
|
|
|
|
|
|
|
|
before do
|
|
|
|
stub_application_setting(raw_blob_request_limit: 5)
|
|
|
|
end
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
it 'prevents from accessing the raw file', :request_store do
|
|
|
|
execute_raw_requests(requests: 5, project: project, file_path: file_path)
|
|
|
|
|
|
|
|
expect { execute_raw_requests(requests: 1, project: project, file_path: file_path) }
|
|
|
|
.to change { Gitlab::GitalyClient.get_request_count }.by(0)
|
2019-10-12 21:52:04 +05:30
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
expect(response.body).to eq(_('You cannot access the raw file. Please wait a minute.'))
|
|
|
|
expect(response).to have_gitlab_http_status(:too_many_requests)
|
2019-10-12 21:52:04 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'logs the event on auth.log' do
|
|
|
|
attributes = {
|
2020-01-01 13:55:28 +05:30
|
|
|
message: 'Application_Rate_Limiter_Request',
|
2019-10-12 21:52:04 +05:30
|
|
|
env: :raw_blob_request_limit,
|
2019-12-04 20:38:33 +05:30
|
|
|
remote_ip: '0.0.0.0',
|
2019-10-12 21:52:04 +05:30
|
|
|
request_method: 'GET',
|
2020-03-13 15:44:24 +05:30
|
|
|
path: "/#{project.full_path}/-/raw/#{file_path}"
|
2019-10-12 21:52:04 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
expect(Gitlab::AuthLogger).to receive(:error).with(attributes).once
|
|
|
|
|
|
|
|
execute_raw_requests(requests: 6, project: project, file_path: file_path)
|
|
|
|
end
|
|
|
|
|
2020-01-01 13:55:28 +05:30
|
|
|
context 'when receiving an external storage request' do
|
|
|
|
let(:token) { 'letmein' }
|
|
|
|
|
|
|
|
before do
|
|
|
|
stub_application_setting(
|
|
|
|
static_objects_external_storage_url: 'https://cdn.gitlab.com',
|
|
|
|
static_objects_external_storage_auth_token: token
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not prevent from accessing the raw file' do
|
|
|
|
request.headers['X-Gitlab-External-Storage-Token'] = token
|
|
|
|
execute_raw_requests(requests: 6, project: project, file_path: file_path)
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2020-01-01 13:55:28 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-10-12 21:52:04 +05:30
|
|
|
context 'when the request uses a different version of a commit' do
|
|
|
|
it 'prevents from accessing the raw file' do
|
|
|
|
# 3 times with the normal sha
|
|
|
|
commit_sha = project.repository.commit.sha
|
|
|
|
file_path = "#{commit_sha}/README.md"
|
|
|
|
|
|
|
|
execute_raw_requests(requests: 3, project: project, file_path: file_path)
|
|
|
|
|
|
|
|
# 3 times with the modified version
|
|
|
|
modified_sha = commit_sha.gsub(commit_sha[0..5], commit_sha[0..5].upcase)
|
|
|
|
modified_path = "#{modified_sha}/README.md"
|
|
|
|
|
|
|
|
execute_raw_requests(requests: 3, project: project, file_path: modified_path)
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
expect(response.body).to eq(_('You cannot access the raw file. Please wait a minute.'))
|
|
|
|
expect(response).to have_gitlab_http_status(:too_many_requests)
|
2019-10-12 21:52:04 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the throttling has been disabled' do
|
|
|
|
before do
|
|
|
|
stub_application_setting(raw_blob_request_limit: 0)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not prevent from accessing the raw file' do
|
|
|
|
execute_raw_requests(requests: 10, project: project, file_path: file_path)
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2019-10-12 21:52:04 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with case-sensitive files' do
|
|
|
|
it 'prevents from accessing the specific file' do
|
|
|
|
create_file_in_repo(project, 'master', 'master', 'readme.md', 'Add readme.md')
|
|
|
|
create_file_in_repo(project, 'master', 'master', 'README.md', 'Add README.md')
|
|
|
|
|
|
|
|
commit_sha = project.repository.commit.sha
|
|
|
|
file_path = "#{commit_sha}/readme.md"
|
|
|
|
|
|
|
|
# Accessing downcase version of readme
|
|
|
|
execute_raw_requests(requests: 6, project: project, file_path: file_path)
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
expect(response.body).to eq(_('You cannot access the raw file. Please wait a minute.'))
|
|
|
|
expect(response).to have_gitlab_http_status(:too_many_requests)
|
2019-10-12 21:52:04 +05:30
|
|
|
|
|
|
|
# Accessing upcase version of readme
|
|
|
|
file_path = "#{commit_sha}/README.md"
|
|
|
|
|
|
|
|
execute_raw_requests(requests: 1, project: project, file_path: file_path)
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2019-10-12 21:52:04 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-01-01 13:55:28 +05:30
|
|
|
|
|
|
|
context 'as a sessionless user' do
|
|
|
|
let_it_be(:project) { create(:project, :private, :repository) }
|
|
|
|
let_it_be(:user) { create(:user, static_object_token: 'very-secure-token') }
|
|
|
|
let_it_be(:file_path) { 'master/README.md' }
|
|
|
|
|
|
|
|
before do
|
|
|
|
project.add_developer(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when no token is provided' do
|
|
|
|
it 'redirects to sign in page' do
|
|
|
|
execute_raw_requests(requests: 1, project: project, file_path: file_path)
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:found)
|
2020-01-01 13:55:28 +05:30
|
|
|
expect(response.location).to end_with('/users/sign_in')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when a token param is present' do
|
|
|
|
context 'when token is correct' do
|
|
|
|
it 'calls the action normally' do
|
|
|
|
execute_raw_requests(requests: 1, project: project, file_path: file_path, token: user.static_object_token)
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2020-01-01 13:55:28 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when token is incorrect' do
|
|
|
|
it 'redirects to sign in page' do
|
|
|
|
execute_raw_requests(requests: 1, project: project, file_path: file_path, token: 'foobar')
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:found)
|
2020-01-01 13:55:28 +05:30
|
|
|
expect(response.location).to end_with('/users/sign_in')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when a token header is present' do
|
|
|
|
context 'when token is correct' do
|
|
|
|
it 'calls the action normally' do
|
|
|
|
request.headers['X-Gitlab-Static-Object-Token'] = user.static_object_token
|
|
|
|
execute_raw_requests(requests: 1, project: project, file_path: file_path)
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:ok)
|
2020-01-01 13:55:28 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when token is incorrect' do
|
|
|
|
it 'redirects to sign in page' do
|
|
|
|
request.headers['X-Gitlab-Static-Object-Token'] = 'foobar'
|
|
|
|
execute_raw_requests(requests: 1, project: project, file_path: file_path)
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
expect(response).to have_gitlab_http_status(:found)
|
2020-01-01 13:55:28 +05:30
|
|
|
expect(response.location).to end_with('/users/sign_in')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2021-01-08 16:13:35 +05:30
|
|
|
|
|
|
|
describe 'caching' do
|
|
|
|
def request_file
|
|
|
|
get(:show, params: { namespace_id: project.namespace, project_id: project, id: 'master/README.md' })
|
|
|
|
end
|
|
|
|
|
2021-01-29 00:20:46 +05:30
|
|
|
it 'sets appropriate caching headers' do
|
|
|
|
sign_in create(:user)
|
|
|
|
request_file
|
|
|
|
|
|
|
|
expect(response.cache_control[:public]).to eq(true)
|
|
|
|
expect(response.cache_control[:max_age]).to eq(60)
|
|
|
|
expect(response.cache_control[:no_store]).to be_nil
|
|
|
|
end
|
|
|
|
|
2021-01-08 16:13:35 +05:30
|
|
|
context 'when a public project has private repo' do
|
|
|
|
let(:project) { create(:project, :public, :repository, :repository_private) }
|
|
|
|
let(:user) { create(:user, maintainer_projects: [project]) }
|
|
|
|
|
|
|
|
it 'does not set public caching header' do
|
|
|
|
sign_in user
|
|
|
|
request_file
|
|
|
|
|
2021-01-29 00:20:46 +05:30
|
|
|
expect(response.header['Cache-Control']).to include('max-age=60, private')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when If-None-Match header is set' do
|
|
|
|
it 'returns a 304 status' do
|
|
|
|
request_file
|
|
|
|
|
|
|
|
request.headers['If-None-Match'] = response.headers['ETag']
|
|
|
|
request_file
|
|
|
|
|
|
|
|
expect(response).to have_gitlab_http_status(:not_modified)
|
2021-01-08 16:13:35 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-10-12 21:52:04 +05:30
|
|
|
end
|
|
|
|
|
2020-01-01 13:55:28 +05:30
|
|
|
def execute_raw_requests(requests:, project:, file_path:, **params)
|
2019-10-12 21:52:04 +05:30
|
|
|
requests.times do
|
|
|
|
get :show, params: {
|
|
|
|
namespace_id: project.namespace,
|
|
|
|
project_id: project,
|
|
|
|
id: file_path
|
2020-01-01 13:55:28 +05:30
|
|
|
}.merge(params)
|
2019-10-12 21:52:04 +05:30
|
|
|
end
|
2015-09-25 12:07:36 +05:30
|
|
|
end
|
|
|
|
end
|