Skip to content

Commit 0ad8691

Browse files
committed
Merge pull request #2121 from mhxz/master
New option for csrf
2 parents fb6b807 + f2dbf6a commit 0ad8691

File tree

2 files changed

+37
-5
lines changed

2 files changed

+37
-5
lines changed

lib/hooks/csrf/index.js

+11-5
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,16 @@ module.exports = function(sails) {
2828
sails.config.csrf = {
2929
grantTokenViaAjax: true,
3030
protectionEnabled: true,
31-
origin: '-'
31+
origin: '-',
32+
routesDisabled: '-'
3233
};
3334
}
3435
else if (sails.config.csrf === false) {
3536
sails.config.csrf = {
3637
grantTokenViaAjax: false,
3738
protectionEnabled: false,
38-
origin: '-'
39+
origin: '-',
40+
routesDisabled: '-'
3941
};
4042
}
4143
// If user provides ANY object (including empty object), enable all default
@@ -44,7 +46,8 @@ module.exports = function(sails) {
4446
_.defaults(typeof sails.config.csrf === 'object' ? sails.config.csrf : {}, {
4547
grantTokenViaAjax: true,
4648
protectionEnabled: true,
47-
origin: '-'
49+
origin: '-',
50+
routesDisabled: '-'
4851
});
4952
}
5053
// Add the csrfToken directly to the config'd routes, so that the CORS hook can process it
@@ -63,12 +66,15 @@ module.exports = function(sails) {
6366
var connect = require('express/node_modules/connect');
6467

6568
return connect.csrf()(req, res, function(err) {
69+
70+
var isRouteDisabled = sails.config.csrf.routesDisabled.split(',').indexOf(req.path) > -1;
71+
6672
if (util.isSameOrigin(req) || allowCrossOriginCSRF) {
6773
res.locals._csrf = req.csrfToken();
6874
} else {
6975
res.locals._csrf = null;
7076
}
71-
if (err) {
77+
if (err && !isRouteDisabled) {
7278
// Return an Access-Control-Allow-Origin header in case this is a xdomain request
7379
if (req.headers.origin) {
7480
res.set('Access-Control-Allow-Origin', req.headers.origin);
@@ -109,4 +115,4 @@ function csrfToken (req, res, next) {
109115
return res.json({
110116
_csrf: res.locals._csrf
111117
});
112-
}
118+
}

test/integration/hook.cors_csrf.test.js

+26
Original file line numberDiff line numberDiff line change
@@ -771,6 +771,32 @@ describe('CORS and CSRF ::', function() {
771771

772772
});
773773

774+
describe("with CSRF set to {protectionEnabled: true, routesDisabled: '/user'}", function() {
775+
776+
before(function() {
777+
fs.writeFileSync(path.resolve('../', appName, 'config/csrf.js'), "module.exports.csrf = {protectionEnabled: true, routesDisabled: '/user'};");
778+
});
779+
780+
it("a POST request on /user without a CSRF token should result in a 200 response", function (done) {
781+
httpHelper.testRoute("post", 'user', function (err, response) {
782+
if (err) return done(new Error(err));
783+
assert.equal(response.statusCode, 200);
784+
done();
785+
});
786+
787+
});
788+
789+
it("a POST request on /test without a CSRF token should result in a 403 response", function (done) {
790+
httpHelper.testRoute("post", 'test', function (err, response) {
791+
if (err) return done(new Error(err));
792+
assert.equal(response.statusCode, 403);
793+
done();
794+
});
795+
796+
});
797+
798+
});
799+
774800
});
775801

776802
describe("CORS+CSRF ::", function () {

0 commit comments

Comments
 (0)