Skip to content

Commit e6f76c2

Browse files
Potential fix for code scanning alert no. 7: CSRF protection weakened or disabled
Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com> Signed-off-by: Robert Smith <[email protected]>
1 parent 1598daf commit e6f76c2

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

app/controllers/better_together/omniauth_callbacks_controller.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
module BetterTogether
44
class OmniauthCallbacksController < Devise::OmniauthCallbacksController # rubocop:todo Style/Documentation
55
# See https://github.com/omniauth/omniauth/wiki/FAQ#rails-session-is-clobbered-after-callback-on-developer-strategy
6-
skip_before_action :verify_authenticity_token, only: %i[github]
6+
before_action :verify_oauth_state, only: %i[github]
77

88
before_action :set_person_platform_integration, except: [:failure]
99
before_action :set_user, except: [:failure]
10+
before_action :generate_oauth_state, only: %i[github]
1011

1112
attr_reader :person_platform_integration, :user
1213

@@ -16,6 +17,13 @@ def github
1617

1718
private
1819

20+
def verify_oauth_state
21+
if params[:state] != session[:oauth_state]
22+
flash[:alert] = 'Invalid OAuth state parameter'
23+
redirect_to new_user_registration_path
24+
end
25+
end
26+
1927
def handle_auth(kind) # rubocop:todo Metrics/AbcSize
2028
if user.present?
2129
flash[:success] = t 'devise_omniauth_callbacks.success', kind: kind if is_navigational_format?
@@ -44,6 +52,10 @@ def set_user
4452
)
4553
end
4654

55+
def generate_oauth_state
56+
session[:oauth_state] = SecureRandom.hex(24)
57+
end
58+
4759
def failure
4860
flash[:error] = 'There was a problem signing you in. Please register or try signing in later.'
4961
redirect_to helpers.base_url

0 commit comments

Comments
 (0)