Connect
defaultValue()
Default value function, checking the req.body
and req.query for the CSRF token.
Source
function defaultValue(req) {
return (req.body && req.body._csrf)
|| (req.query && req.query._csrf)
|| (req.headers['x-csrf-token'])
|| (req.headers['x-xsrf-token']);
}saltedToken()
Return salted token.
Source
function saltedToken(secret) {
return createToken(generateSalt(10), secret);
}createToken()
Creates a CSRF token from a given salt and secret.
Source
function createToken(salt, secret) {
return salt + crypto
.createHash('sha1')
.update(salt + secret)
.digest('base64');
}checkToken()
Checks if a given CSRF token matches the given secret.
Source
function checkToken(token, secret) {
if ('string' != typeof token) return false;
return token === createToken(token.slice(0, 10), secret);
}generateSalt()
Generates a random salt, using a fast non-blocking PRNG (Math.random()).
Source
function generateSalt(length) {
var i, r = [];
for (i = 0; i < length; ++i) {
r.push(SALTCHARS[Math.floor(Math.random() * SALTCHARS.length)]);
}
return r.join('');
}
var SALTCHARS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
Anti CSRF:
CSRF protection middleware.
This middleware adds a
req.csrfToken()function to make a tokenwhich should be added to requests which mutate
state, within a hidden form field, query-string etc. This
token is validated against the visitor's session.
The default
valuefunction checksreq.bodygeneratedby the
bodyParser()middleware,req.querygeneratedby
query(), and the "X-CSRF-Token" header field.This middleware requires session support, thus should be added
somewhere below
session()andcookieParser().Options:
valuea function accepting the request, returning the tokenSource