Skip to content
This repository has been archived by the owner on Sep 14, 2022. It is now read-only.

Commit

Permalink
Pass misconfigured error to next() instead of throwing
Browse files Browse the repository at this point in the history
closes #78
closes #88
  • Loading branch information
dougwilson committed May 23, 2016
1 parent ebd8106 commit adb9e3f
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 14 deletions.
1 change: 1 addition & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ unreleased
==========

* Pass invalid csrf token error to `next()` instead of throwing
* Pass misconfigured error to `next()` instead of throwing
* Provide misconfigured error when using cookies without cookie-parser
* deps: [email protected]
- perf: enable strict mode
Expand Down
62 changes: 49 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ function csurf (options) {
var ignoreMethod = getIgnoredMethods(ignoreMethods)

return function csrf (req, res, next) {
// validate the configuration against request
if (!verifyConfiguration(req, sessionKey, cookie)) {
return next(new Error('misconfigured csrf'))
}

// get the secret from the request
var secret = getSecret(req, sessionKey, cookie)
var token

Expand Down Expand Up @@ -190,28 +196,40 @@ function getIgnoredMethods (methods) {
*/

function getSecret (req, sessionKey, cookie) {
var bag
var key
// get the bag & key
var bag = getSecretBag(req, sessionKey, cookie)
var key = cookie ? cookie.key : 'csrfSecret'

if (!bag) {
/* istanbul ignore next: should never actually run */
throw new Error('misconfigured csrf')
}

// return secret from bag
return bag[key]
}

/**
* Get the token secret bag from the request.
*
* @param {IncomingMessage} req
* @param {String} sessionKey
* @param {Object} [cookie]
* @api private
*/

function getSecretBag (req, sessionKey, cookie) {
if (cookie) {
// get secret from cookie
var cookieKey = cookie.signed
? 'signedCookies'
: 'cookies'

bag = req[cookieKey]
key = cookie.key
return req[cookieKey]
} else {
// get secret from session
bag = req[sessionKey]
key = 'csrfSecret'
}

if (!bag) {
throw new Error('misconfigured csrf')
return req[sessionKey]
}

return bag[key]
}

/**
Expand Down Expand Up @@ -253,7 +271,8 @@ function setSecret (req, res, sessionKey, val, cookie) {
var secret = req.secret

if (!secret) {
throw new Error('cookieParser("secret") required for signed cookies')
/* istanbul ignore next: should never actually run */
throw new Error('misconfigured csrf')
}

val = 's:' + sign(val, secret)
Expand All @@ -268,3 +287,20 @@ function setSecret (req, res, sessionKey, val, cookie) {
throw new Error('misconfigured csrf')
}
}

/**
* Verify the configuration against the request.
* @private
*/

function verifyConfiguration (req, sessionKey, cookie) {
if (!getSecretBag(req, sessionKey, cookie)) {
return false
}

if (cookie && cookie.signed && !req.secret) {
return false
}

return true
}
2 changes: 1 addition & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ describe('csurf', function () {

request(app)
.get('/')
.expect(500, /cookieParser.*secret/, done)
.expect(500, /misconfigured csrf/, done)
})

describe('with "ignoreMethods" option', function () {
Expand Down

0 comments on commit adb9e3f

Please sign in to comment.