Skip to content

feat: add option allowedCookie for limit cookie parse to an whitelist #127

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Create a new cookie parser middleware function using the given `secret` and
- `options` an object that is passed to `cookie.parse` as the second option. See
[cookie](https://www.npmjs.org/package/cookie) for more information.
- `decode` a function to decode the value of the cookie
- `allowedCookie` an array of allowed cookie name to parse

The middleware will parse the `Cookie` header on the request and expose the
cookie data as the property `req.cookies` and, if a `secret` was provided, as
Expand Down
16 changes: 16 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,22 @@ function cookieParser (secret, options) {
req.cookies = Object.create(null)
req.signedCookies = Object.create(null)

if( cookies && options && options.allowedCookie ){
var cookiesList = cookies.split(';');
cookies = '';
var found = 0;
for (var cookieItem of cookiesList) {
var cookieParts = cookieItem.trim().split('=');
if (options.allowedCookie.includes(cookieParts[0])) {
cookies += cookieParts[0] + '=' + cookieParts[1] + ';';
found++;
}
if (found === options.allowedCookie.length) {
break;
}
}
}

// no cookies
if (!cookies) {
return next()
Expand Down
21 changes: 19 additions & 2 deletions test/cookieParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -283,8 +283,25 @@ describe('cookieParser.signedCookies(obj, secret)', function () {
})
})

function createServer (secret) {
var _parser = cookieParser(secret)
describe('cookieParser with allowedCookie', function () {
describe('when no cookies are sent', function () {
it('should default req.cookies to {}', function (done) {
request(createServer('keyboard cat', { allowedCookie: ['foo'] }))
.get('/')
.expect(200, '{}', done)
})

it('should req.cookies to allowedCookie', function (done) {
request(createServer('keyboard cat', { allowedCookie: ['foo', 'baz'] }))
.get('/')
.set('Cookie', 'foo=xxx; bar=yyy; baz=zzz;')
.expect(200, '{"foo":"xxx","baz":"zzz"}', done)
})
})
})

function createServer (secret, options) {
var _parser = cookieParser(secret, options)
return http.createServer(function (req, res) {
_parser(req, res, function (err) {
if (err) {
Expand Down