Commit 84cd552f authored by Adam Hegyi's avatar Adam Hegyi

Prevent API access for unconfirmed users

- Add feature flag to disable `unconfirmed` condition.
parent 17fc902d
...@@ -21,6 +21,14 @@ class BasePolicy < DeclarativePolicy::Base ...@@ -21,6 +21,14 @@ class BasePolicy < DeclarativePolicy::Base
with_options scope: :user, score: 0 with_options scope: :user, score: 0
condition(:deactivated) { @user&.deactivated? } condition(:deactivated) { @user&.deactivated? }
desc "User email is unconfirmed or user account is locked"
with_options scope: :user, score: 0
condition(:inactive) do
Feature.enabled?(:inactive_policy_condition, default_enabled: true) &&
@user &&
!@user&.active_for_authentication?
end
with_options scope: :user, score: 0 with_options scope: :user, score: 0
condition(:external_user) { @user.nil? || @user.external? } condition(:external_user) { @user.nil? || @user.external? }
......
...@@ -36,6 +36,13 @@ class GlobalPolicy < BasePolicy ...@@ -36,6 +36,13 @@ class GlobalPolicy < BasePolicy
enable :use_slash_commands enable :use_slash_commands
end end
rule { inactive }.policy do
prevent :log_in
prevent :access_api
prevent :access_git
prevent :use_slash_commands
end
rule { blocked | internal }.policy do rule { blocked | internal }.policy do
prevent :log_in prevent :log_in
prevent :access_api prevent :access_api
......
---
title: Prevent API access for unconfirmed users
merge_request:
author:
type: security
...@@ -141,6 +141,34 @@ describe GlobalPolicy do ...@@ -141,6 +141,34 @@ describe GlobalPolicy do
it { is_expected.to be_allowed(:access_api) } it { is_expected.to be_allowed(:access_api) }
end end
end end
context 'inactive user' do
before do
current_user.update!(confirmed_at: nil, confirmation_sent_at: 5.days.ago)
end
context 'when within the confirmation grace period' do
before do
allow(User).to receive(:allow_unconfirmed_access_for).and_return(10.days)
end
it { is_expected.to be_allowed(:access_api) }
end
context 'when confirmation grace period is expired' do
before do
allow(User).to receive(:allow_unconfirmed_access_for).and_return(2.days)
end
it { is_expected.not_to be_allowed(:access_api) }
end
it 'when `inactive_policy_condition` feature flag is turned off' do
stub_feature_flags(inactive_policy_condition: false)
is_expected.to be_allowed(:access_api)
end
end
end end
describe 'receive notifications' do describe 'receive notifications' do
...@@ -202,6 +230,20 @@ describe GlobalPolicy do ...@@ -202,6 +230,20 @@ describe GlobalPolicy do
it { is_expected.not_to be_allowed(:access_git) } it { is_expected.not_to be_allowed(:access_git) }
end end
describe 'inactive user' do
before do
current_user.update!(confirmed_at: nil)
end
it { is_expected.not_to be_allowed(:access_git) }
it 'when `inactive_policy_condition` feature flag is turned off' do
stub_feature_flags(inactive_policy_condition: false)
is_expected.to be_allowed(:access_git)
end
end
context 'when terms are enforced' do context 'when terms are enforced' do
before do before do
enforce_terms enforce_terms
...@@ -298,6 +340,20 @@ describe GlobalPolicy do ...@@ -298,6 +340,20 @@ describe GlobalPolicy do
it { is_expected.not_to be_allowed(:use_slash_commands) } it { is_expected.not_to be_allowed(:use_slash_commands) }
end end
describe 'inactive user' do
before do
current_user.update!(confirmed_at: nil)
end
it { is_expected.not_to be_allowed(:use_slash_commands) }
it 'when `inactive_policy_condition` feature flag is turned off' do
stub_feature_flags(inactive_policy_condition: false)
is_expected.to be_allowed(:use_slash_commands)
end
end
context 'when access locked' do context 'when access locked' do
before do before do
current_user.lock_access! current_user.lock_access!
......
...@@ -30,26 +30,40 @@ describe 'OAuth tokens' do ...@@ -30,26 +30,40 @@ describe 'OAuth tokens' do
end end
end end
context "when user is blocked" do shared_examples 'does not create an access token' do
it "does not create an access token" do let(:user) { create(:user) }
user = create(:user)
it { expect(response).to have_gitlab_http_status(401) }
end
context 'when user is blocked' do
before do
user.block user.block
request_oauth_token(user) request_oauth_token(user)
expect(response).to have_gitlab_http_status(401)
end end
include_examples 'does not create an access token'
end end
context "when user is ldap_blocked" do context 'when user is ldap_blocked' do
it "does not create an access token" do before do
user = create(:user)
user.ldap_block user.ldap_block
request_oauth_token(user) request_oauth_token(user)
end
expect(response).to have_gitlab_http_status(401) include_examples 'does not create an access token'
end
context 'when user account is not confirmed' do
before do
user.update!(confirmed_at: nil)
request_oauth_token(user)
end end
include_examples 'does not create an access token'
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