Other prevention approaches: JWT
JSON Web Token is quite a popular alternative to cookies + synchronizer token pattern circa 2020.
What this method does is:
- store a signed token in
window.localStorage
- whenever you want to make an authenticated request to the server, send a header
Authentication: <token>
. Note that this can only be done from JavaScript.
This method works because unlike cookies, localStorage
is only available when you make requests from the website itself (through JavaScript), thus dispensing the synchronizer token.
Then, when users first visit the website, they are initially logged off, and a dummy loading page shows.
Then the browser runs the JavaScript is just received from the server, reads localStorage
(now that we are on the correct domain already) and sends an authenticated GET request to an API path to get only the data without HTML, usually as JSON.
Finally the JavaScript renders that data on the browser.
This approach has become particularly popular due to the popularity of Single Page Applications, where the simplest implementation approach is this two-step get dummy page then populate it with the API data.
So this basically carries the tradeoffs:
- advantages:
- simpler to implement since no synchronizer on every form
- the usual SPA advantages: you get only data after the initial request, not HTML tags
- disadvantages:
- the usual SPA disadvantages:
- during first load the user might see annoying loading dummy page elements
- the website is not visible without JavaScript
- the usual SPA disadvantages:
See also