Skip to content

Commit

Permalink
Support any type in "secret" that crypto.createHmac supports
Browse files Browse the repository at this point in the history
  • Loading branch information
dougwilson committed Jun 9, 2023
1 parent 710ae06 commit d8a8fe2
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 7 deletions.
6 changes: 6 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
unreleased
==========

* Support any type in `secret` that `crypto.createHmac` supports
* deps: cookie-signature@1.0.7

1.17.3 / 2022-05-11
===================

Expand Down
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -274,12 +274,13 @@ it to be saved. *This has been fixed in PassportJS 0.3.0*

**Required option**

This is the secret used to sign the session ID cookie. This can be either a string
for a single secret, or an array of multiple secrets. If an array of secrets is
provided, only the first element will be used to sign the session ID cookie, while
all the elements will be considered when verifying the signature in requests. The
secret itself should be not easily parsed by a human and would best be a random set
of characters. A best practice may include:
This is the secret used to sign the session ID cookie. The secret can be any type
of value that is supported by Node.js `crypto.createHmac` (like a string or a
`Buffer`). This can be either a single secret, or an array of multiple secrets. If
an array of secrets is provided, only the first element will be used to sign the
session ID cookie, while all the elements will be considered when verifying the
signature in requests. The secret itself should be not easily parsed by a human and
would best be a random set of characters. A best practice may include:

- The use of environment variables to store the secret, ensuring the secret itself
does not exist in your repository.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"license": "MIT",
"dependencies": {
"cookie": "0.4.2",
"cookie-signature": "1.0.6",
"cookie-signature": "1.0.7",
"debug": "2.6.9",
"depd": "~2.0.0",
"on-headers": "~1.0.2",
Expand Down
45 changes: 45 additions & 0 deletions test/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
var after = require('after')
var assert = require('assert')
var cookieParser = require('cookie-parser')
var crypto = require('crypto')
var express = require('express')
var fs = require('fs')
var http = require('http')
Expand Down Expand Up @@ -1197,6 +1198,50 @@ describe('session()', function(){
assert.throws(createServer.bind(null, { secret: [] }), /secret option array/);
})

it('should sign and unsign with a string', function (done) {
var server = createServer({ secret: 'awesome cat' }, function (req, res) {
if (!req.session.user) {
req.session.user = 'bob'
res.end('set')
} else {
res.end('get:' + JSON.stringify(req.session.user))
}
})

request(server)
.get('/')
.expect(shouldSetCookie('connect.sid'))
.expect(200, 'set', function (err, res) {
if (err) return done(err)
request(server)
.get('/')
.set('Cookie', cookie(res))
.expect(200, 'get:"bob"', done)
})
})

it('should sign and unsign with a Buffer', function (done) {
var server = createServer({ secret: crypto.randomBytes(32) }, function (req, res) {
if (!req.session.user) {
req.session.user = 'bob'
res.end('set')
} else {
res.end('get:' + JSON.stringify(req.session.user))
}
})

request(server)
.get('/')
.expect(shouldSetCookie('connect.sid'))
.expect(200, 'set', function (err, res) {
if (err) return done(err)
request(server)
.get('/')
.set('Cookie', cookie(res))
.expect(200, 'get:"bob"', done)
})
})

describe('when an array', function () {
it('should sign cookies', function (done) {
var server = createServer({ secret: ['keyboard cat', 'nyan cat'] }, function (req, res) {
Expand Down

0 comments on commit d8a8fe2

Please sign in to comment.