2017-09-19 03:44:58 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe Gitlab::Middleware::ReadOnly do
|
|
|
|
include Rack::Test::Methods
|
2018-06-12 22:20:10 -04:00
|
|
|
using RSpec::Parameterized::TableSyntax
|
2017-09-19 03:44:58 -04:00
|
|
|
|
|
|
|
let(:rack_stack) do
|
|
|
|
rack = Rack::Builder.new do
|
|
|
|
use ActionDispatch::Session::CacheStore
|
|
|
|
use ActionDispatch::Flash
|
|
|
|
use ActionDispatch::ParamsParser
|
|
|
|
end
|
|
|
|
|
|
|
|
rack.run(subject)
|
|
|
|
rack.to_app
|
|
|
|
end
|
|
|
|
|
2018-02-02 09:12:28 -05:00
|
|
|
let(:observe_env) do
|
|
|
|
Module.new do
|
|
|
|
attr_reader :env
|
|
|
|
|
|
|
|
def call(env)
|
|
|
|
@env = env
|
|
|
|
super
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-03-02 11:39:42 -05:00
|
|
|
let(:request) { Rack::MockRequest.new(rack_stack) }
|
|
|
|
|
2018-02-02 09:12:28 -05:00
|
|
|
subject do
|
2018-03-02 11:39:42 -05:00
|
|
|
described_class.new(fake_app).tap do |app|
|
|
|
|
app.extend(observe_env)
|
|
|
|
end
|
2018-02-02 09:12:28 -05:00
|
|
|
end
|
2017-09-19 03:44:58 -04:00
|
|
|
|
2018-09-21 08:05:37 -04:00
|
|
|
context 'normal requests to a read-only GitLab instance' do
|
2017-09-19 03:44:58 -04:00
|
|
|
let(:fake_app) { lambda { |env| [200, { 'Content-Type' => 'text/plain' }, ['OK']] } }
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow(Gitlab::Database).to receive(:read_only?) { true }
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'expects PATCH requests to be disallowed' do
|
|
|
|
response = request.patch('/test_request')
|
|
|
|
|
2018-07-07 00:22:44 -04:00
|
|
|
expect(response).to be_redirect
|
2017-09-19 03:44:58 -04:00
|
|
|
expect(subject).to disallow_request
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'expects PUT requests to be disallowed' do
|
|
|
|
response = request.put('/test_request')
|
|
|
|
|
2018-07-07 00:22:44 -04:00
|
|
|
expect(response).to be_redirect
|
2017-09-19 03:44:58 -04:00
|
|
|
expect(subject).to disallow_request
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'expects POST requests to be disallowed' do
|
|
|
|
response = request.post('/test_request')
|
|
|
|
|
2018-07-07 00:22:44 -04:00
|
|
|
expect(response).to be_redirect
|
2017-09-19 03:44:58 -04:00
|
|
|
expect(subject).to disallow_request
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'expects a internal POST request to be allowed after a disallowed request' do
|
|
|
|
response = request.post('/test_request')
|
|
|
|
|
2018-07-07 00:22:44 -04:00
|
|
|
expect(response).to be_redirect
|
2017-09-19 03:44:58 -04:00
|
|
|
|
|
|
|
response = request.post("/api/#{API::API.version}/internal")
|
|
|
|
|
2018-07-07 00:22:44 -04:00
|
|
|
expect(response).not_to be_redirect
|
2017-09-19 03:44:58 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'expects DELETE requests to be disallowed' do
|
|
|
|
response = request.delete('/test_request')
|
|
|
|
|
2018-07-07 00:22:44 -04:00
|
|
|
expect(response).to be_redirect
|
2017-09-19 03:44:58 -04:00
|
|
|
expect(subject).to disallow_request
|
|
|
|
end
|
|
|
|
|
2017-11-02 08:50:04 -04:00
|
|
|
it 'expects POST of new file that looks like an LFS batch url to be disallowed' do
|
2017-11-20 18:27:52 -05:00
|
|
|
expect(Rails.application.routes).to receive(:recognize_path).and_call_original
|
2017-11-02 08:50:04 -04:00
|
|
|
response = request.post('/root/gitlab-ce/new/master/app/info/lfs/objects/batch')
|
|
|
|
|
2018-07-07 00:22:44 -04:00
|
|
|
expect(response).to be_redirect
|
2017-11-02 08:50:04 -04:00
|
|
|
expect(subject).to disallow_request
|
|
|
|
end
|
|
|
|
|
2017-11-21 07:29:57 -05:00
|
|
|
it 'returns last_vistited_url for disallowed request' do
|
|
|
|
response = request.post('/test_request')
|
|
|
|
|
|
|
|
expect(response.location).to eq 'http://localhost/'
|
|
|
|
end
|
|
|
|
|
2017-09-19 03:44:58 -04:00
|
|
|
context 'whitelisted requests' do
|
|
|
|
it 'expects a POST internal request to be allowed' do
|
2017-11-20 18:27:52 -05:00
|
|
|
expect(Rails.application.routes).not_to receive(:recognize_path)
|
2017-09-19 03:44:58 -04:00
|
|
|
response = request.post("/api/#{API::API.version}/internal")
|
|
|
|
|
2018-07-07 00:22:44 -04:00
|
|
|
expect(response).not_to be_redirect
|
2017-09-19 03:44:58 -04:00
|
|
|
expect(subject).not_to disallow_request
|
|
|
|
end
|
|
|
|
|
2018-06-12 22:20:10 -04:00
|
|
|
it 'expects requests to sidekiq admin to be allowed' do
|
|
|
|
response = request.post('/admin/sidekiq')
|
2017-09-19 03:44:58 -04:00
|
|
|
|
2018-07-07 00:22:44 -04:00
|
|
|
expect(response).not_to be_redirect
|
2017-09-19 03:44:58 -04:00
|
|
|
expect(subject).not_to disallow_request
|
2017-11-02 08:50:04 -04:00
|
|
|
|
2018-06-12 22:20:10 -04:00
|
|
|
response = request.get('/admin/sidekiq')
|
2017-11-02 08:50:04 -04:00
|
|
|
|
2018-07-07 00:22:44 -04:00
|
|
|
expect(response).not_to be_redirect
|
2017-11-02 08:50:04 -04:00
|
|
|
expect(subject).not_to disallow_request
|
|
|
|
end
|
|
|
|
|
2018-06-12 22:20:10 -04:00
|
|
|
where(:description, :path) do
|
|
|
|
'LFS request to batch' | '/root/rouge.git/info/lfs/objects/batch'
|
|
|
|
'LFS request to locks verify' | '/root/rouge.git/info/lfs/locks/verify'
|
|
|
|
'LFS request to locks create' | '/root/rouge.git/info/lfs/locks'
|
|
|
|
'LFS request to locks unlock' | '/root/rouge.git/info/lfs/locks/1/unlock'
|
|
|
|
'request to git-upload-pack' | '/root/rouge.git/git-upload-pack'
|
|
|
|
'request to git-receive-pack' | '/root/rouge.git/git-receive-pack'
|
|
|
|
end
|
2017-11-02 08:50:04 -04:00
|
|
|
|
2018-06-12 22:20:10 -04:00
|
|
|
with_them do
|
|
|
|
it "expects a POST #{description} URL to be allowed" do
|
|
|
|
expect(Rails.application.routes).to receive(:recognize_path).and_call_original
|
|
|
|
response = request.post(path)
|
2017-11-02 08:50:04 -04:00
|
|
|
|
2018-07-07 00:22:44 -04:00
|
|
|
expect(response).not_to be_redirect
|
2018-06-12 22:20:10 -04:00
|
|
|
expect(subject).not_to disallow_request
|
|
|
|
end
|
2017-11-02 08:50:04 -04:00
|
|
|
end
|
2017-09-19 03:44:58 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'json requests to a read-only GitLab instance' do
|
|
|
|
let(:fake_app) { lambda { |env| [200, { 'Content-Type' => 'application/json' }, ['OK']] } }
|
|
|
|
let(:content_json) { { 'CONTENT_TYPE' => 'application/json' } }
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow(Gitlab::Database).to receive(:read_only?) { true }
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'expects PATCH requests to be disallowed' do
|
|
|
|
response = request.patch('/test_request', content_json)
|
|
|
|
|
|
|
|
expect(response).to disallow_request_in_json
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'expects PUT requests to be disallowed' do
|
|
|
|
response = request.put('/test_request', content_json)
|
|
|
|
|
|
|
|
expect(response).to disallow_request_in_json
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'expects POST requests to be disallowed' do
|
|
|
|
response = request.post('/test_request', content_json)
|
|
|
|
|
|
|
|
expect(response).to disallow_request_in_json
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'expects DELETE requests to be disallowed' do
|
|
|
|
response = request.delete('/test_request', content_json)
|
|
|
|
|
|
|
|
expect(response).to disallow_request_in_json
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|