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

Commit

Permalink
Accept CSRF-Token and XSRF-Token request headers
Browse files Browse the repository at this point in the history
closes #49
closes #50
  • Loading branch information
gabeio authored and dougwilson committed Feb 15, 2015
1 parent 9bf7554 commit 2fc310b
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 0 deletions.
1 change: 1 addition & 0 deletions HISTORY.md
@@ -1,6 +1,7 @@
unreleased
==========

* Accept `CSRF-Token` and `XSRF-Token` request headers
* Default `cookie.path` to `'/'`, if using cookies
* deps: cookie-signature@1.0.6
* deps: csrf@~2.0.6
Expand Down
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -74,6 +74,8 @@ locations, in order:
- `req.body._csrf` - typically generated by the `body-parser` module.
- `req.query._csrf` - a built-in from Express.js to read from the URL
query string.
- `req.headers['csrf-token']` - the `CSRF-Token` HTTP request header.
- `req.headers['xsrf-token']` - the `XSRF-Token` HTTP request header.
- `req.headers['x-csrf-token']` - the `X-CSRF-Token` HTTP request header.
- `req.headers['x-xsrf-token']` - the `X-XSRF-Token` HTTP request header.

Expand Down
2 changes: 2 additions & 0 deletions index.js
Expand Up @@ -109,6 +109,8 @@ module.exports = function csurf(options) {
function defaultValue(req) {
return (req.body && req.body._csrf)
|| (req.query && req.query._csrf)
|| (req.headers['csrf-token'])
|| (req.headers['xsrf-token'])
|| (req.headers['x-csrf-token'])
|| (req.headers['x-xsrf-token']);
}
Expand Down
34 changes: 34 additions & 0 deletions test/test.js
Expand Up @@ -46,6 +46,40 @@ describe('csurf', function () {
});
});

it('should work in csrf-token header', function(done) {
var server = createServer()

request(server)
.get('/')
.expect(200, function (err, res) {
if (err) return done(err)
var token = res.text;

request(server)
.post('/')
.set('Cookie', cookies(res))
.set('csrf-token', token)
.expect(200, done)
});
});

it('should work in xsrf-token header', function(done) {
var server = createServer()

request(server)
.get('/')
.expect(200, function (err, res) {
if (err) return done(err)
var token = res.text;

request(server)
.post('/')
.set('Cookie', cookies(res))
.set('xsrf-token', token)
.expect(200, done)
});
});

it('should work in x-csrf-token header', function(done) {
var server = createServer()

Expand Down

0 comments on commit 2fc310b

Please sign in to comment.