2017-02-21 21:17:23 +00:00
|
|
|
shared_context 'unique ips sign in limit' do
|
2017-02-17 13:44:57 +00:00
|
|
|
include StubENV
|
2017-08-10 22:31:42 +00:00
|
|
|
before do
|
2017-07-11 03:35:47 +00:00
|
|
|
Gitlab::Redis::Cache.with(&:flushall)
|
|
|
|
Gitlab::Redis::Queues.with(&:flushall)
|
|
|
|
Gitlab::Redis::SharedState.with(&:flushall)
|
2017-02-17 12:24:32 +00:00
|
|
|
end
|
|
|
|
|
2017-02-17 11:52:27 +00:00
|
|
|
before do
|
2017-02-17 13:44:57 +00:00
|
|
|
stub_env('IN_MEMORY_APPLICATION_SETTINGS', 'false')
|
|
|
|
|
2018-02-02 18:39:55 +00:00
|
|
|
Gitlab::CurrentSettings.update!(
|
2017-02-17 13:44:57 +00:00
|
|
|
unique_ips_limit_enabled: true,
|
|
|
|
unique_ips_limit_time_window: 10000
|
|
|
|
)
|
2017-02-17 11:52:27 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def change_ip(ip)
|
|
|
|
allow(Gitlab::RequestContext).to receive(:client_ip).and_return(ip)
|
|
|
|
end
|
2017-02-22 13:13:06 +00:00
|
|
|
|
|
|
|
def request_from_ip(ip)
|
|
|
|
change_ip(ip)
|
|
|
|
request
|
|
|
|
response
|
|
|
|
end
|
|
|
|
|
|
|
|
def operation_from_ip(ip)
|
|
|
|
change_ip(ip)
|
|
|
|
operation
|
|
|
|
end
|
2017-02-17 11:52:27 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples 'user login operation with unique ip limit' do
|
2017-02-21 21:17:23 +00:00
|
|
|
include_context 'unique ips sign in limit' do
|
2017-06-14 18:18:56 +00:00
|
|
|
before do
|
2018-02-02 18:39:55 +00:00
|
|
|
Gitlab::CurrentSettings.update!(unique_ips_limit_per_user: 1)
|
2017-06-14 18:18:56 +00:00
|
|
|
end
|
2017-02-17 13:44:57 +00:00
|
|
|
|
2017-02-17 11:52:27 +00:00
|
|
|
it 'allows user authenticating from the same ip' do
|
2017-02-22 13:13:06 +00:00
|
|
|
expect { operation_from_ip('ip') }.not_to raise_error
|
|
|
|
expect { operation_from_ip('ip') }.not_to raise_error
|
2017-02-17 11:52:27 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'blocks user authenticating from two distinct ips' do
|
2017-02-22 13:13:06 +00:00
|
|
|
expect { operation_from_ip('ip') }.not_to raise_error
|
|
|
|
expect { operation_from_ip('ip2') }.to raise_error(Gitlab::Auth::TooManyIps)
|
2017-02-17 11:52:27 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-02-17 13:44:57 +00:00
|
|
|
|
2017-02-21 21:17:23 +00:00
|
|
|
shared_examples 'user login request with unique ip limit' do |success_status = 200|
|
|
|
|
include_context 'unique ips sign in limit' do
|
2017-06-14 18:18:56 +00:00
|
|
|
before do
|
2018-02-02 18:39:55 +00:00
|
|
|
Gitlab::CurrentSettings.update!(unique_ips_limit_per_user: 1)
|
2017-06-14 18:18:56 +00:00
|
|
|
end
|
2017-02-17 13:44:57 +00:00
|
|
|
|
|
|
|
it 'allows user authenticating from the same ip' do
|
2017-10-19 18:28:19 +00:00
|
|
|
expect(request_from_ip('ip')).to have_gitlab_http_status(success_status)
|
|
|
|
expect(request_from_ip('ip')).to have_gitlab_http_status(success_status)
|
2017-02-17 13:44:57 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'blocks user authenticating from two distinct ips' do
|
2017-10-19 18:28:19 +00:00
|
|
|
expect(request_from_ip('ip')).to have_gitlab_http_status(success_status)
|
|
|
|
expect(request_from_ip('ip2')).to have_gitlab_http_status(403)
|
2017-02-17 13:44:57 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|