Skip to content

Commit 6aa0edf

Browse files
authored
Merge pull request #119 from jordanhiltunen/align-xsrf-cookie-provisioning-with-laravel-precedent
Align CSRF cookie provisioning with Laravel's precedents to eliminate ActionController::InvalidAuthenticityToken edge cases
2 parents 74fab7c + 928ca26 commit 6aa0edf

File tree

4 files changed

+43
-2
lines changed

4 files changed

+43
-2
lines changed

lib/inertia_rails/controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ module Controller
1313
helper ::InertiaRails::Helper
1414

1515
after_action do
16-
cookies['XSRF-TOKEN'] = form_authenticity_token unless request.inertia? || !protect_against_forgery?
16+
cookies['XSRF-TOKEN'] = form_authenticity_token unless !protect_against_forgery?
1717
end
1818
end
1919

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class InertiaSessionContinuityTestController < ApplicationController
2+
def initialize_session
3+
render inertia: 'TestNewSessionComponent'
4+
end
5+
6+
def submit_form_to_test_csrf
7+
render inertia: 'TestComponent'
8+
end
9+
10+
def clear_session
11+
session.clear
12+
13+
return redirect_to initialize_session_path
14+
end
15+
end

spec/dummy/config/routes.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,8 @@
4141
get 'merge_instance_props' => 'inertia_merge_instance_props#merge_instance_props'
4242

4343
get 'lamda_shared_props' => 'inertia_lambda_shared_props#lamda_shared_props'
44+
45+
get 'initialize_session' => 'inertia_session_continuity_test#initialize_session'
46+
post 'submit_form_to_test_csrf' => 'inertia_session_continuity_test#submit_form_to_test_csrf'
47+
delete 'clear_session' => 'inertia_session_continuity_test#clear_session'
4448
end

spec/inertia/request_spec.rb

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@
113113

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

@@ -131,5 +131,27 @@
131131
it { is_expected.to be_nil }
132132
end
133133
end
134+
135+
it 'sets the XSRF-TOKEN cookie after the session is cleared during an inertia call' do
136+
with_forgery_protection do
137+
get initialize_session_path
138+
expect(response).to have_http_status(:ok)
139+
initial_xsrf_token_cookie = response.cookies['XSRF-TOKEN']
140+
141+
post submit_form_to_test_csrf_path, headers: { 'X-Inertia' => true, 'X-XSRF-Token' => initial_xsrf_token_cookie }
142+
expect(response).to have_http_status(:ok)
143+
144+
delete clear_session_path, headers: { 'X-Inertia' => true, 'X-XSRF-Token' => initial_xsrf_token_cookie }
145+
expect(response).to have_http_status(:see_other)
146+
expect(response.headers['Location']).to eq('http://www.example.com/initialize_session')
147+
148+
post_logout_xsrf_token_cookie = response.cookies['XSRF-TOKEN']
149+
expect(post_logout_xsrf_token_cookie).not_to be_nil
150+
expect(post_logout_xsrf_token_cookie).not_to eq(initial_xsrf_token_cookie)
151+
152+
post submit_form_to_test_csrf_path, headers: { 'X-Inertia' => true, 'X-XSRF-Token' => post_logout_xsrf_token_cookie }
153+
expect(response).to have_http_status(:ok)
154+
end
155+
end
134156
end
135157
end

0 commit comments

Comments
 (0)