I've been trying to follow the documentation on SailsJS security and was implementing CSRF protection when I came across a very difficult problem:
I have turned on CSRF:
module.exports.csrf = true;
I have configured the settings:
module.exports.csrf = {
grantTokenViaAjax: true,
}
I am requesting the csrfToken from the web service in my angular application app.run and putting the _csrf into the default POST headers:
$http(
{
url: $rootScope.serverRootUri + '/csrfToken',
method: 'GET'
} ).success( function( data, status, header, config )
{
$
log.info("[CSRF] Received token ", data._csrf );
$rootScope.csrfToken = data._csrf;
})
.error( function( data, status, header, config )
{
$
log.info("[CSRF] Unable to get token from ", $rootScope.serverRootUri, data );
});
And now I am sending along my $http request - I've tried to shove the _csrf into the headers here and I can see it in the Firebug output in a Request header:
$http(
{
url: $rootScope.serverRootUri + '/apiCall
method: 'POST',
withCredentials: true,
data: {
data: $rootScope.fooBar,
},
headers: {
'_csrf': $rootScope.csrfToken
}
} ).success( function ( data, status, header, config )
{
$location.url('/happyDance');
deferred.resolve( data );
} )
.error( function ( data, status, header, config )
{
$location.url('/error');
deferred.reject( data );
} );
However I am always always getting the 403 back from Sails with a CSRF mismatch. I'm having a hard time figuring out what I'm missing as I've followed the docs and added in stuff in every place I think it might be looking for it, but still I get the CSRF mismatch. Going a bit nutty. Any thoughts? I'm using version 0.11 of sails.