About 1 in 50 users are having the issue. I've found the Session Sweeping Lottery in config/session.php, it's set to the default value of [2, 100]. Perhaps that has something to do with it.
Intermittent 419 error for some users when POSTing with Axios
I'm using Vue and Axios on the front-end. Some users' POST requests are failing with a 419 error. I'm using TrackJS to monitor console errors on the users' side and I can see it's happening to roughly 1 in 50 users.
Here is the error log:
Severity:
LOG
Message:
{
"data": {
"message": ""
},
"status": 419,
"statusText": "unknown status",
"headers": {
"date": "Mon, 17 Sep 2018 21:48:21 GMT",
"cache-control": "no-cache, private",
"server": "nginx/1.10.3 (Ubuntu)",
"connection": "keep-alive",
"transfer-encoding": "chunked",
"content-type": "application/json"
},
"config": {
"transformRequest": {},
"transformResponse": {},
"timeout": 0,
"xsrfCookieName": "XSRF-TOKEN",
"xsrfHeaderName": "X-XSRF-TOKEN",
"maxContentLength": -1,
"headers": {
"Accept": "application/json, text/plain, */*",
"X-Requested-With": "XMLHttpRequest",
"X-CSRF-TOKEN": "4EcoKUTAtwu5VR3vcxQZoFktgQTAgy0ctfHCbUdP",
"Content-Type": "application/json;charset=utf-8"
},
"method": "post",
"url": "/category/",
"data": "{\"slug\":\"\"}"
},
"request": {
"_trackJs": {
"method": "POST",
"url": "/category/",
"logId": "5f3fd3c1-6677-4e12-a658-9b40e317e9f0"
}
}
}
On my page, I've got the csrf-token set in the meta tag in the page head like so:
<meta name="csrf-token" content="{{ csrf_token() }}"> And of course, I'm registering the CSRF Token as a common header with Axios like so:
let token = document.head.querySelector('meta[name="csrf-token"]');
if (token) {
window.axios.defaults.headers.common['X-CSRF-TOKEN'] = token.content;
} else {
console.error('CSRF token not found: https://laravel.com/docs/csrf#csrf-x-csrf-token');
}
The error log on failed requests is showing that the X-CSRF-TOKEN is present in the header.
At first I was using Cloudflare's CDN, however I've temporarily disabled it in case it was inadvertently hiding session data from some clients.
In config/session.php, my session lifetime is still set to the default 120 minutes, and expire_on_close is false.
Many other 'solutions' for this error recommend excluding the error-prone routes from CSRF verification by adding them to the $except array, moving them to an API route, or switching them to GETs. I require CSRF, so these solutions are not acceptable.
Does anyone have any other suggestions or ideas for further testing/experimentation?
Please or to participate in this conversation.