Skip to content

Commit

Permalink
Merge pull request #119 from jordanhiltunen/align-xsrf-cookie-provisi…
Browse files Browse the repository at this point in the history
…oning-with-laravel-precedent

Align CSRF cookie provisioning with Laravel's precedents to eliminate ActionController::InvalidAuthenticityToken edge cases
  • Loading branch information
bknoles authored May 27, 2024
2 parents 74fab7c + 928ca26 commit 6aa0edf
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/inertia_rails/controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module Controller
helper ::InertiaRails::Helper

after_action do
cookies['XSRF-TOKEN'] = form_authenticity_token unless request.inertia? || !protect_against_forgery?
cookies['XSRF-TOKEN'] = form_authenticity_token unless !protect_against_forgery?
end
end

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class InertiaSessionContinuityTestController < ApplicationController
def initialize_session
render inertia: 'TestNewSessionComponent'
end

def submit_form_to_test_csrf
render inertia: 'TestComponent'
end

def clear_session
session.clear

return redirect_to initialize_session_path
end
end
4 changes: 4 additions & 0 deletions spec/dummy/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,8 @@
get 'merge_instance_props' => 'inertia_merge_instance_props#merge_instance_props'

get 'lamda_shared_props' => 'inertia_lambda_shared_props#lamda_shared_props'

get 'initialize_session' => 'inertia_session_continuity_test#initialize_session'
post 'submit_form_to_test_csrf' => 'inertia_session_continuity_test#submit_form_to_test_csrf'
delete 'clear_session' => 'inertia_session_continuity_test#clear_session'
end
24 changes: 23 additions & 1 deletion spec/inertia/request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@

context 'it is an inertia call' do
let(:headers){ { 'X-Inertia' => true } }
it { is_expected.not_to include('XSRF-TOKEN') }
it { is_expected.to include('XSRF-TOKEN') }
end
end

Expand All @@ -131,5 +131,27 @@
it { is_expected.to be_nil }
end
end

it 'sets the XSRF-TOKEN cookie after the session is cleared during an inertia call' do
with_forgery_protection do
get initialize_session_path
expect(response).to have_http_status(:ok)
initial_xsrf_token_cookie = response.cookies['XSRF-TOKEN']

post submit_form_to_test_csrf_path, headers: { 'X-Inertia' => true, 'X-XSRF-Token' => initial_xsrf_token_cookie }
expect(response).to have_http_status(:ok)

delete clear_session_path, headers: { 'X-Inertia' => true, 'X-XSRF-Token' => initial_xsrf_token_cookie }
expect(response).to have_http_status(:see_other)
expect(response.headers['Location']).to eq('http://www.example.com/initialize_session')

post_logout_xsrf_token_cookie = response.cookies['XSRF-TOKEN']
expect(post_logout_xsrf_token_cookie).not_to be_nil
expect(post_logout_xsrf_token_cookie).not_to eq(initial_xsrf_token_cookie)

post submit_form_to_test_csrf_path, headers: { 'X-Inertia' => true, 'X-XSRF-Token' => post_logout_xsrf_token_cookie }
expect(response).to have_http_status(:ok)
end
end
end
end

0 comments on commit 6aa0edf

Please sign in to comment.