Last active
December 16, 2015 17:59
-
-
Save Gpx/5474621 to your computer and use it in GitHub Desktop.
Skip CSRF protection for requests in a whitelist for Express
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var express = require('express'); | |
var connect = require('connect'); | |
// Disable CSRF for some requests | |
var conditionalCSRF = function (req, res, next) { | |
var whitelist = [ | |
'/api/req1', | |
'/api/req2' | |
]; | |
req.session._csrf || (req.session._csrf = connect.utils.uid(24)); | |
if (req.method !== 'POST') { | |
next(); | |
return; | |
} | |
if (whitelist.indexOf(req.url) !== -1) { | |
next(); | |
} else { | |
(express.csrf())(req, res, next); | |
} | |
}; | |
app.use(conditionalCSRF); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment