Skip to content

Commit

Permalink
Merge pull request #267 from DFE-Digital/fix/csrf-patch-for-omniauth
Browse files Browse the repository at this point in the history
CSRF patch for Omniauth vulnerability
  • Loading branch information
tahb authored Apr 1, 2021
2 parents db3c49d + 3d43579 commit 5dab4c8
Show file tree
Hide file tree
Showing 9 changed files with 18 additions and 22 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ gem "pg"
gem "mini_racer"
gem "omniauth"
gem "omniauth_openid_connect"
gem "omniauth-rails_csrf_protection"
gem "puma", "~> 5.2"
gem "redcarpet", "~> 3.5"
gem "redis", "~> 4.2"
Expand Down
4 changes: 4 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,9 @@ GEM
omniauth (1.9.1)
hashie (>= 3.4.6)
rack (>= 1.6.2, < 3)
omniauth-rails_csrf_protection (0.1.2)
actionpack (>= 4.2)
omniauth (>= 1.3.1)
omniauth_openid_connect (0.3.5)
addressable (~> 2.5)
omniauth (~> 1.9)
Expand Down Expand Up @@ -426,6 +429,7 @@ DEPENDENCIES
mini_racer
mock_redis
omniauth
omniauth-rails_csrf_protection
omniauth_openid_connect
pg
pry
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def authenticate_user!
return if current_user

session.delete(:dfe_sign_in_uid)
redirect_to new_dfe_path
redirect_to root_path
end

def current_journey
Expand Down
5 changes: 0 additions & 5 deletions app/controllers/sessions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@ class SessionsController < ApplicationController
skip_before_action :authenticate_user!
protect_from_forgery except: :bypass_callback

def new
# This is defined by the class name of our Omniauth strategy
redirect_to "/auth/dfe"
end

def create
UserSession.new(session: session)
.persist_successful_dfe_sign_in_claim!(omniauth_hash: auth_hash)
Expand Down
4 changes: 2 additions & 2 deletions app/views/pages/specifying_start_page.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
<p class="govuk-body"><%= I18n.t("specifying.start_page.pause_and_resume_body") %></p>

<% if DfESignIn.bypass? %>
<%= link_to "#{I18n.t("generic.button.start")} (bypass DSI)", "/auth/developer", class: "govuk-button" %>
<%= button_to "#{I18n.t("generic.button.start")} (bypass DSI)", "/auth/developer", class: "govuk-button" %>
<% else %>
<%= link_to I18n.t("generic.button.start"), new_dfe_path, class: "govuk-button" %>
<%= button_to I18n.t("generic.button.start"), "/auth/dfe", class: "govuk-button" %>
<% end %>
6 changes: 0 additions & 6 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,6 @@
post "/api/contentful/entry_updated" => "api/contentful/entries#changed"

# DfE Sign In
resource :sessions,
only: %i[create new],
as: :dfe,
path: "/dfe/sessions",
controller: "sessions"

get "/auth/dfe/callback", to: "sessions#create"
get "/auth/dfe/signout", to: "sessions#destroy"
get "/auth/failure", to: "sessions#failure"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@
.with("Sign in failed unexpectedly")
.and_call_original

visit dashboard_path
visit root_path
click_button I18n.t("generic.button.start")

expect(page).to have_content(I18n.t("errors.sign_in.unexpected_failure.page_title"))
expect(page).to have_content(I18n.t("errors.sign_in.unexpected_failure.page_body"))
Expand Down
12 changes: 6 additions & 6 deletions spec/requests/authentication_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
end

it "users can access the new session endpoint" do
get new_dfe_path
post "/auth/dfe"
expect(response).to have_http_status(:found)
end

Expand All @@ -36,29 +36,29 @@
describe "Endpoints that do require authentication" do
it "users cannot access the new journey path" do
get new_journey_path
expect(response).to redirect_to(new_dfe_path)
expect(response).to redirect_to(root_path)
end

it "users cannot access an existing journey" do
journey = create(:journey)
get journey_path(journey)
expect(response).to redirect_to(new_dfe_path)
expect(response).to redirect_to(root_path)
end

it "users cannot edit an answer" do
answer = create(:radio_answer)
get edit_journey_step_path(answer.step.journey, answer.step)
expect(response).to redirect_to(new_dfe_path)
expect(response).to redirect_to(root_path)
end

it "users cannot see the journey map" do
get new_journey_map_path
expect(response).to redirect_to(new_dfe_path)
expect(response).to redirect_to(root_path)
end

it "users cannot see the preview endpoints" do
get preview_entry_path("an-entry-id")
expect(response).to redirect_to(new_dfe_path)
expect(response).to redirect_to(root_path)
end
end

Expand Down
3 changes: 2 additions & 1 deletion spec/support/sign_in_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ def user_sign_in_attempt_fails(dsi_uid: SecureRandom.uuid)
end

def user_starts_the_journey
visit dashboard_path
visit root_path
click_link I18n.t("generic.button.start")
click_link I18n.t("dashboard.create.link")
end

Expand Down

0 comments on commit 5dab4c8

Please sign in to comment.