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

Commit

Permalink
Provide misconfigured error when using cookies without cookie-parser
Browse files Browse the repository at this point in the history
  • Loading branch information
dougwilson committed May 23, 2016
1 parent 4dd4e9c commit 6158707
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 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
* Provide misconfigured error when using cookies without cookie-parser
* deps: cookie@0.2.4
- perf: enable strict mode
- perf: use for loop in parse
Expand Down
19 changes: 12 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,23 +190,28 @@ function getIgnoredMethods (methods) {
*/

function getsecret (req, sessionKey, cookie) {
var secret
var bag
var key

if (cookie) {
// get secret from cookie
var bag = cookie.signed
var cookieKey = cookie.signed
? 'signedCookies'
: 'cookies'

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

if (!bag) {
throw new Error('misconfigured csrf')
}

return secret
return bag[key]
}

/**
Expand Down
12 changes: 11 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ describe('csurf', function () {
})
})

it('should error without secret storage', function (done) {
it('should error without session secret storage', function (done) {
var app = connect()

app.use(csurf())
Expand All @@ -239,6 +239,16 @@ describe('csurf', function () {
.expect(500, /misconfigured csrf/, done)
})

it('should error without cookie secret storage', function (done) {
var app = connect()

app.use(csurf({ cookie: true }))

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

it('should error without cookieParser secret and signed cookie storage', function (done) {
var app = connect()

Expand Down

0 comments on commit 6158707

Please sign in to comment.