Recently I've made a CSRF protection in needed html forms on website.
This works as follows:
- Server generates hash and puts it in the session variable
- Then hash is inserted into html
input
inside the html form. - When form is submitted, server takes session variable and compares it to
post
from client's form.
So that works fine, but when I need to use JavaScript to submit form and receive response it doesn't works, because token is regenerated when client makes request with that form.
Is it safe to return new token with server response?
Or should I do it differently, like generating hash only one time per session?