Skip to content

Commit 2a95847

Browse files
authored
Merge pull request #5113 from rubyforgood/5100-fix-unknown-format
#5100: Fix unknown format error on expired session
2 parents fd980e8 + 2833d31 commit 2a95847

10 files changed

Lines changed: 68 additions & 3 deletions

app/controllers/application_controller.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ class ApplicationController < ActionController::Base
1515

1616
rescue_from ActiveRecord::RecordNotFound, with: :not_found!
1717

18-
rescue_from ActionController::InvalidAuthenticityToken do
18+
rescue_from ActionController::InvalidAuthenticityToken, with: :session_expired
19+
20+
def session_expired
1921
flash[:error] = "Your session expired. This could be due to leaving a page open for a long time, or having multiple tabs open. Try resubmitting."
2022
redirect_back fallback_location: root_path
2123
end
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module Validatable
2+
extend ActiveSupport::Concern
3+
4+
included do
5+
rescue_from ActionController::InvalidAuthenticityToken do
6+
if action_name == "validate"
7+
render json: {valid: false}
8+
else
9+
session_expired
10+
end
11+
end
12+
end
13+
end

app/controllers/distributions_controller.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
class DistributionsController < ApplicationController
77
include DateRangeHelper
88
include DistributionHelper
9+
include Validatable
910

1011
before_action :enable_turbo!, only: %i[new show]
1112
skip_before_action :authenticate_user!, only: %i(calendar)

app/controllers/partners/family_requests_controller.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
module Partners
22
class FamilyRequestsController < BaseController
3+
include Validatable
34
before_action :verify_partner_is_active
45
before_action :authorize_verified_partners
56

app/controllers/partners/individuals_requests_controller.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
module Partners
22
class IndividualsRequestsController < BaseController
3+
include Validatable
34
before_action :verify_partner_is_active
45
before_action :authorize_verified_partners
56

app/controllers/partners/requests_controller.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
module Partners
22
class RequestsController < BaseController
3+
include Validatable
34
skip_before_action :require_partner, only: [:new, :create, :validate]
45
before_action :require_partner_or_org_admin, only: [:new, :create, :validate]
56
layout :layout

spec/requests/distributions_requests_spec.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -816,6 +816,17 @@
816816

817817
include_examples "restricts access to organization users/admins"
818818
end
819+
820+
describe 'POST #validate' do
821+
it 'should handle missing CSRF gracefully' do
822+
ActionController::Base.allow_forgery_protection = true
823+
post validate_partners_individuals_requests_path
824+
ActionController::Base.allow_forgery_protection = false
825+
826+
expect(JSON.parse(response.body)).to eq({'valid' => false})
827+
expect(response.status).to eq(200)
828+
end
829+
end
819830
end
820831

821832
context "While not signed in" do

spec/requests/partners/family_requests_controller_spec.rb renamed to spec/requests/partners/family_requests_requests_spec.rb

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
before { sign_in(partner_user) }
1313

14-
describe 'GET #new' do
14+
describe "GET #new" do
1515
subject { get new_partners_family_request_path }
1616

1717
it "does not allow deactivated partners" do
@@ -27,7 +27,7 @@
2727
end
2828
end
2929

30-
describe 'POST #create' do
30+
describe "POST #create" do
3131
before do
3232
# Set one child as deactivated and the other as active but
3333
# without a item_needed_diaperid
@@ -66,4 +66,15 @@
6666
expect(Partners::ChildItemRequest.find_by(child_id: children[2].id)).to be_present
6767
end
6868
end
69+
70+
describe "POST #validate" do
71+
it "should handle missing CSRF gracefully" do
72+
ActionController::Base.allow_forgery_protection = true
73+
post validate_partners_family_requests_path
74+
ActionController::Base.allow_forgery_protection = false
75+
76+
expect(JSON.parse(response.body)).to eq({"valid" => false})
77+
expect(response.status).to eq(200)
78+
end
79+
end
6980
end

spec/requests/partners/individuals_requests_controller_spec.rb renamed to spec/requests/partners/individuals_requests_requests_spec.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,4 +156,15 @@
156156
end
157157
end
158158
end
159+
160+
describe "POST #validate" do
161+
it "should handle missing CSRF gracefully" do
162+
ActionController::Base.allow_forgery_protection = true
163+
post validate_partners_individuals_requests_path
164+
ActionController::Base.allow_forgery_protection = false
165+
166+
expect(JSON.parse(response.body)).to eq({"valid" => false})
167+
expect(response.status).to eq(200)
168+
end
169+
end
159170
end

spec/requests/partners/requests_spec.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,4 +430,17 @@
430430
end
431431
end
432432
end
433+
434+
describe 'POST #validate' do
435+
it 'should handle missing CSRF gracefully' do
436+
sign_in(partner_user)
437+
438+
ActionController::Base.allow_forgery_protection = true
439+
post validate_partners_requests_path
440+
ActionController::Base.allow_forgery_protection = false
441+
442+
expect(JSON.parse(response.body)).to eq({'valid' => false})
443+
expect(response.status).to eq(200)
444+
end
445+
end
433446
end

0 commit comments

Comments
 (0)