Correct way to re-generate CSRF for sequential AJAX calls |
I have a vague memory of seeing this being discussed in another thread, but I can't find it now.
Scenario:
Now (at last) to my question: What is best practice to "inform" the initial View of the new CSRF token? I was thinking about simply including it in the response from the AJAX call to the JS code in the View, and then letting the JS code update the form's CSRF field with the new value. Can anyone see any security issues with this? The first call won't succeed if the token is stale or invalid, so there won't be a new one generated. The refreshed CSRF token will only be passed back on a successful call. -joho
I'm not sure there is anything to worry about security with this approach. It is still the same as when the form is submitted directly (without AJAX). When the form is returned back to the user, the CSRF token will be regenerated and updated in the form, for subsequent submissions.
(08-24-2023, 05:25 AM)sammyskills Wrote: I'm not sure there is anything to worry about security with this approach. It is still the same as when the form is submitted directly (without AJAX). When the form is returned back to the user, the CSRF token will be regenerated and updated in the form, for subsequent submissions. That was my thinking, but I was worried I had overlooked something, being a CI noob ![]() -joho
How to Send AJAX request with CSRF token in CodeIgniter 4
What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
(08-24-2023, 11:04 PM)InsiteFX Wrote: How to Send AJAX request with CSRF token in CodeIgniter 4 Indeed. (Though I prefer pure JS.) It's a good article nevertheless. -joho
For me i do something like this
#HTML Code: <!--First Load CSRF Token from PHP--> #JS Code: var formEmail = document.getElementById('dataEmail'); #Controller PHP Code: public function changeEmailUser() |
Welcome Guest, Not a member yet? Register Sign In |